#include #include #include "../include/chip8.h" #include "../include/instruction.h" #include "../include/instruction_handler.h" Chip8::Chip8() : instructionHandler(), display() { this->pc = 0x200; } Chip8::~Chip8() { delete instructionHandler; } /** * Reads all bytes from 0x200 onward and put them in the memory. * If the file does not exist, the program is exited. * * @param file The Chip8 rom file * @todo Check if the Chip8 rom file is a valid rom file */ void Chip8::loadRom(const std::string& file) { FILE* game = fopen(("roms/" + file).c_str(), "rb"); if (game == nullptr) { exit(EXIT_FAILURE); } fread(this->memory + 0x200, sizeof(char), 0xFFF - 0x200, game); fclose(game); } Display Chip8::getDisplay() const { return this->display; } /** * This emulates one cycle by getting the opcode and handling the instruction corresponding that opcode */ 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)); } /** * Gets the value from the top of the stack, pop the top of the stack and returns the value * @return A 16-bit value from the stack */ unsigned short Chip8::popFromStack() { unsigned short value = this->stack.top(); this->stack.pop(); return value; } void Chip8::pushToStack(unsigned short value) { this->stack.push(value); } void Chip8::setProgramCounter(unsigned short value) { this->pc = value; } unsigned short Chip8::getProgramCounter() const { return this->pc; }