Skip to content

Commit

Permalink
Merge pull request #7 from gapry/op_ret
Browse files Browse the repository at this point in the history
Decode RET Opcode
  • Loading branch information
gapry authored Jun 3, 2024
2 parents 7f4d27b + 0ad423a commit d0c974c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
24 changes: 22 additions & 2 deletions arabica/cpu/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@
namespace arabica {

void CPU::run(const Memory& memory) {
instruction = memory[pc] << 8 | memory[pc + 1];
opcode = static_cast<OP_CODE>(instruction & 0xF000);
instruction = memory[pc] << 8 | memory[pc + 1];
uint16_t prefix = instruction & 0xF000;
uint16_t suffix = instruction & 0x00FF;
opcode = static_cast<OP_CODE>(prefix);

switch (prefix) {
case 0x0: {
switch (suffix) {
case 0xEE: opcode = OP_CODE::RET; break;
default:; break;
}
} break;
default: break;
}

switch (opcode) {
case OP_CODE::JP_addr: {
Expand All @@ -17,6 +29,14 @@ void CPU::run(const Memory& memory) {
uint16_t target = instruction & 0x0FFF;
pc = target;
} break;
case OP_CODE::RET: {
if (!stack.empty()) {
pc = stack.top();
stack.pop();
} else {
// ToDo: raise interrupt? how to test the failed case?
};
} break;
default: {
fmt::print("Unknown opcode: 0x{:X}\n", static_cast<uint16_t>(opcode));
} break;
Expand Down
1 change: 0 additions & 1 deletion arabica/cpu/cpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class CPU : public noncopyable {
uint8_t reg_delay{DEFAULT_RATE_HZ};
uint8_t reg_sound{DEFAULT_RATE_HZ};
uint16_t pc{PC_START};
uint8_t sp{0x00};
std::stack<uint16_t> stack;
uint16_t instruction{0x0000};
OP_CODE opcode{OP_CODE::CLS};
Expand Down
12 changes: 12 additions & 0 deletions test/cpu/cpu_test_suite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ arabica_cpu_test(test_call,
ASSERT_EQ(cpu.stack.size(), 1);
ASSERT_EQ(cpu.stack.top(), 0x202);
)

arabica_cpu_test(test_ret,
memory.write(0x200, 0x26);
memory.write(0x201, 0x00);
cpu.run(memory);

memory.write(0x600, 0x00);
memory.write(0x601, 0xEE);
cpu.run(memory);
ASSERT_EQ(cpu.pc, 0x202);
ASSERT_EQ(cpu.stack.size(), 0);
)

0 comments on commit d0c974c

Please sign in to comment.