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