Some cleanup, added stack and implemented 0x00EE
							parent
							
								
									588ce89b19
								
							
						
					
					
						commit
						632873078b
					
				| 
						 | 
					@ -2,22 +2,26 @@
 | 
				
			||||||
#define CHIP8_CHIP8_H
 | 
					#define CHIP8_CHIP8_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
 | 
					#include <stack>
 | 
				
			||||||
 | 
					#include "display.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class InstructionHandler;
 | 
					class InstructionHandler;
 | 
				
			||||||
class Display;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Chip8 {
 | 
					class Chip8 {
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
    unsigned char memory[0xFFF] = {};
 | 
					    unsigned char memory[0xFFF] = {};
 | 
				
			||||||
    unsigned char v[0x10] = {};
 | 
					    unsigned char v[0x10] = {};
 | 
				
			||||||
 | 
					    std::stack<unsigned short> stack{};
 | 
				
			||||||
    unsigned short pc;
 | 
					    unsigned short pc;
 | 
				
			||||||
    Display* display;
 | 
					    Display display;
 | 
				
			||||||
    InstructionHandler* instructionHandler;
 | 
					    InstructionHandler* instructionHandler;
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
    Chip8();
 | 
					    Chip8();
 | 
				
			||||||
    ~Chip8();
 | 
					    ~Chip8();
 | 
				
			||||||
    void loadRom(const std::string& file);
 | 
					    void loadRom(const std::string&);
 | 
				
			||||||
    [[nodiscard]] Display* getDisplay() const;
 | 
					    [[nodiscard]] Display getDisplay() const;
 | 
				
			||||||
    void emulateCycle();
 | 
					    void emulateCycle();
 | 
				
			||||||
 | 
					    [[nodiscard]] unsigned short popFromStack();
 | 
				
			||||||
 | 
					    void setProgramCounter(unsigned short);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
#endif //CHIP8_CHIP8_H
 | 
					#endif //CHIP8_CHIP8_H
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,14 +5,13 @@
 | 
				
			||||||
#define STATE_OFF 0
 | 
					#define STATE_OFF 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <SDL2/SDL.h>
 | 
					#include <SDL2/SDL.h>
 | 
				
			||||||
#include "chip8.h"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Display {
 | 
					class Display {
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
    bool flagRedraw = false;
 | 
					    bool flagRedraw = false;
 | 
				
			||||||
    bool display[64 * 32] = {};
 | 
					    bool display[64 * 32] = {};
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
    void draw(SDL_Renderer*, const Chip8&);
 | 
					    void draw(SDL_Renderer*);
 | 
				
			||||||
    [[nodiscard]] bool needsRedraw() const;
 | 
					    [[nodiscard]] bool needsRedraw() const;
 | 
				
			||||||
    void clear();
 | 
					    void clear();
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -7,11 +7,11 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class InstructionHandler {
 | 
					class InstructionHandler {
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
    typedef void (*Handler)(const Chip8&, Instruction);
 | 
					    typedef void (*Handler)(Chip8&, Instruction);
 | 
				
			||||||
    std::map<unsigned short, Handler> handlers = {};
 | 
					    std::map<unsigned short, Handler> handlers = {};
 | 
				
			||||||
    static void handle0000(const Chip8&, Instruction);
 | 
					    static void handle0000(Chip8&, Instruction);
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
    InstructionHandler();
 | 
					    InstructionHandler();
 | 
				
			||||||
    void handleInstruction(unsigned short, const Chip8&, Instruction);
 | 
					    void handleInstruction(unsigned short, Chip8&, Instruction);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
#endif //CHIP8_INSTRUCTION_HANDLER_H
 | 
					#endif //CHIP8_INSTRUCTION_HANDLER_H
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -4,14 +4,12 @@
 | 
				
			||||||
#include "../include/chip8.h"
 | 
					#include "../include/chip8.h"
 | 
				
			||||||
#include "../include/instruction.h"
 | 
					#include "../include/instruction.h"
 | 
				
			||||||
#include "../include/instruction_handler.h"
 | 
					#include "../include/instruction_handler.h"
 | 
				
			||||||
#include "../include/display.h"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
Chip8::Chip8() : instructionHandler(), display() {
 | 
					Chip8::Chip8() : instructionHandler(), display() {
 | 
				
			||||||
    this->pc = 0x200;
 | 
					    this->pc = 0x200;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Chip8::~Chip8() {
 | 
					Chip8::~Chip8() {
 | 
				
			||||||
    delete display;
 | 
					 | 
				
			||||||
    delete instructionHandler;
 | 
					    delete instructionHandler;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -26,11 +24,22 @@ void Chip8::loadRom(const std::string& file) {
 | 
				
			||||||
    fclose(game);
 | 
					    fclose(game);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Display* Chip8::getDisplay() const {
 | 
					Display Chip8::getDisplay() const {
 | 
				
			||||||
    return this->display;
 | 
					    return this->display;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Chip8::emulateCycle() {
 | 
					void Chip8::emulateCycle() {
 | 
				
			||||||
    unsigned short opcode = (this->memory[this->pc] << 8 | this->memory[this->pc + 1]);
 | 
					    unsigned short opcode = (this->memory[this->pc] << 8 | this->memory[this->pc + 1]);
 | 
				
			||||||
 | 
					    this->pc += 2;
 | 
				
			||||||
    this->instructionHandler->handleInstruction(opcode & 0xF000, *this, Instruction(opcode));
 | 
					    this->instructionHandler->handleInstruction(opcode & 0xF000, *this, Instruction(opcode));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					unsigned short Chip8::popFromStack() {
 | 
				
			||||||
 | 
					    unsigned short value = this->stack.top();
 | 
				
			||||||
 | 
					    this->stack.pop();
 | 
				
			||||||
 | 
					    return value;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Chip8::setProgramCounter(unsigned short value) {
 | 
				
			||||||
 | 
					    this->pc = value;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,6 @@
 | 
				
			||||||
#include "../include/display.h"
 | 
					#include "../include/display.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Display::draw(SDL_Renderer * renderer, const Chip8& chip8) {
 | 
					void Display::draw(SDL_Renderer * renderer) {
 | 
				
			||||||
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
 | 
					    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
 | 
				
			||||||
    SDL_RenderClear(renderer);
 | 
					    SDL_RenderClear(renderer);
 | 
				
			||||||
    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
 | 
					    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,7 +6,7 @@ InstructionHandler::InstructionHandler() {
 | 
				
			||||||
    this->handlers[0x0000] = handle0000;
 | 
					    this->handlers[0x0000] = handle0000;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void InstructionHandler::handleInstruction(unsigned short instructionNumber, const Chip8& chip8, Instruction instruction) {
 | 
					void InstructionHandler::handleInstruction(unsigned short instructionNumber, Chip8& chip8, Instruction instruction) {
 | 
				
			||||||
    if (!this->handlers.contains(instructionNumber)) {
 | 
					    if (!this->handlers.contains(instructionNumber)) {
 | 
				
			||||||
        Logger::error("Unregistered instruction", instructionNumber);
 | 
					        Logger::error("Unregistered instruction", instructionNumber);
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
| 
						 | 
					@ -15,13 +15,13 @@ void InstructionHandler::handleInstruction(unsigned short instructionNumber, con
 | 
				
			||||||
    this->handlers[instructionNumber](chip8, instruction);
 | 
					    this->handlers[instructionNumber](chip8, instruction);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void InstructionHandler::handle0000(const Chip8& chip8, Instruction instruction) {
 | 
					void InstructionHandler::handle0000(Chip8& chip8, Instruction instruction) {
 | 
				
			||||||
    switch (instruction.kk()) {
 | 
					    switch (instruction.kk()) {
 | 
				
			||||||
        case 0x00E0:
 | 
					        case 0x00E0:
 | 
				
			||||||
            chip8.getDisplay()->clear();
 | 
					            chip8.getDisplay().clear();
 | 
				
			||||||
            break;
 | 
					            break;
 | 
				
			||||||
        case 0x00EE:
 | 
					        case 0x00EE:
 | 
				
			||||||
 | 
					            chip8.setProgramCounter(chip8.popFromStack());
 | 
				
			||||||
            break;
 | 
					            break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue