#include "Interpreter.hpp" #include #include namespace chocochip8 { Interpreter::Interpreter(unsigned ticksPerSecond, Display &display, Buzzer &buzzer, Keypad &keypad): mvMemory(scMemorySize), mCallStack{}, mrDisplay{display}, mrBuzzer{buzzer}, mrKeypad{keypad}, mcTicksPerSecond{ticksPerSecond}, mvSpecialReg{}, mvReg{} {} void Interpreter::tick() { // decodes all possible machine code arithmetic opcodes constexpr Opcode opcodeMap[16] = { Opcode::SET, Opcode::OR, Opcode::AND, Opcode::XOR, Opcode::ADD, Opcode::SUB, Opcode::RSH, Opcode::SUB2, Opcode::UNIMPL, Opcode::UNIMPL, Opcode::UNIMPL, Opcode::UNIMPL, Opcode::UNIMPL, Opcode::UNIMPL, Opcode::LSH, Opcode::UNIMPL, }; // fetch instruction sreg_t pc = mvSpecialReg[SR_PC]; unsigned inst = (mvMemory[pc] << 8) | mvMemory[pc + 1]; // extract fields unsigned regDst = (inst & 0x0F00) >> 8; // destination register index unsigned regSrc = (inst & 0x00F0) >> 4; // source register index unsigned opcode = (inst & 0x000F); // arithmetic opcode unsigned imm8 = (inst & 0x00FF); // 8-bit immediate unsigned imm12 = (inst & 0x0FFF); // 12-bit immediate switch(inst & 0xF000) { case 0x0000: // 0NNN - call machine language routine switch(inst) { case 0x00E0: // clear display for(auto &scanline : *mrDisplay.mpFramebuffer) { scanline.reset(); } break; case 0x00EE: // return from subroutine mvSpecialReg[SR_PC] = mCallStack.top(); mCallStack.pop(); break; default: throw std::invalid_argument("not implemented"); break; } break; case 0x1000: // 1NNN - unconditional jump mvSpecialReg[SR_PC] = imm12; break; case 0x2000: // 2NNN - call subroutine mCallStack.push(pc); mvSpecialReg[SR_PC] = imm12; break; case 0x3000: // 3XNN - skip if equal immediate executeArithmetic(Opcode::JEQ, regDst, imm8); break; case 0x4000: // 4XNN - skip if nonequal immediate executeArithmetic(Opcode::JNEQ, regDst, imm8); break; case 0x5000: // 5XY0 - skip if equal executeArithmetic(Opcode::JEQ, regDst, mvReg[regSrc]); break; case 0x6000: // 6XNN - load immediate executeArithmetic(Opcode::SET, regDst, imm8); break; case 0x7000: // 7XNN - increment executeArithmetic(Opcode::ADD, regDst, imm8); break; case 0x8000: // 8XNN - general arithmetic executeArithmetic(opcodeMap[opcode], regDst, mvReg[regSrc]); break; case 0x9000: // 9XY0 - skip if nonequal executeArithmetic(Opcode::JNEQ, regDst, mvReg[regSrc]); break; case 0xA000: // ANNN - load I mvSpecialReg[SR_I] = imm12; break; case 0xB000: // BNNN - jump indirect mvSpecialReg[SR_I] = mvReg[R_V0] + imm12; break; case 0xC000: // CXNN - load random executeArithmetic(Opcode::RAND, regDst, imm8); break; case 0xD000: // DXYN - draw break; case 0xE000: // EX9E, EXA1 - keypad access break; case 0xF000: // several unique instructions break; } // increment PC mvSpecialReg[SR_PC] += 2; // decrement timers if(mvSpecialReg[SR_T1] > 0) { mvSpecialReg[SR_T1] -= 1; } if(mvSpecialReg[SR_T2] > 0) { mvSpecialReg[SR_T2] -= 1; if(mvSpecialReg[SR_T2] == 0) { mrBuzzer.off(); } } } void Interpreter::loadProgram(size_t where, char const* data, size_t count) { if(where + count > scMemorySize) { throw std::out_of_range("program exceeds memory bounds or capacity"); } memcpy(mvMemory.data(), data, count); } void Interpreter::executeArithmetic(Opcode opcode, int iReg, reg_t operand) { reg_t tmp; switch(opcode) { case Opcode::SET: mvReg[iReg] = operand; break; case Opcode::AND: mvReg[iReg] &= operand; break; case Opcode::OR: mvReg[iReg] |= operand; break; case Opcode::XOR: mvReg[iReg] ^= operand; break; case Opcode::RAND: mvReg[iReg] = rand() & operand; break; case Opcode::LSH: mvReg[R_VF] = (mvReg[iReg] & 0x80) ? 1 : 0; mvReg[iReg] <<= 1; break; case Opcode::RSH: mvReg[R_VF] = (mvReg[iReg] & 0x01) ? 1 : 0; mvReg[iReg] >>= 1; break; case Opcode::ADD: tmp = mvReg[iReg] + operand; mvReg[R_VF] = (tmp < mvReg[iReg]) ? 1 : 0; mvReg[iReg] = tmp; break; case Opcode::SUB: tmp = mvReg[iReg] - operand; mvReg[R_VF] = (tmp > mvReg[iReg]) ? 1 : 0; mvReg[iReg] = tmp; break; case Opcode::SUB2: tmp = operand - mvReg[iReg]; mvReg[R_VF] = (tmp > operand) ? 1 : 0; mvReg[iReg] = tmp; break; case Opcode::JEQ: if(mvReg[iReg] == operand) { mvSpecialReg[SR_PC] += 2; } break; case Opcode::JNEQ: if(mvReg[iReg] != operand) { mvSpecialReg[SR_PC] += 2; } break; case Opcode::UNIMPL: throw std::invalid_argument("invalid opcode"); break; } } }; // namespace chochochip8