forked from Cam1G/2b-Completed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.cpp
More file actions
62 lines (53 loc) · 1.55 KB
/
Copy pathcpu.cpp
File metadata and controls
62 lines (53 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "cpu.h"
#include "mmu.h"
#include <cstdint>
#include <iostream>
#include <string>
class programTooLargeException : public std::exception {
private:
std::string msg = "Program too large to be read into memory.";
public:
const char *what() { return msg.c_str(); }
};
class unreachableOpcodeException : public std::exception {
private:
std::string msg = "Opcode is impossible in the instruction set.";
public:
const char *what() { return msg.c_str(); }
};
two_b_completed::two_b_completed() { two_b_completed::status_flag = true; }
void two_b_completed::read_program(std::vector<uint8_t> &program) {
if (program.size() > BANK_SIZE) {
throw programTooLargeException();
}
for (int i = 0; i < program.size(); i++) {
mmu.write(i, program[i]);
}
}
void two_b_completed::step() {
const uint8_t current_instruction = mmu.read(registers[PC_REGISTER]);
const uint8_t opcode = current_instruction >> 6;
const uint8_t rx = (current_instruction >> 3) & 0x7;
const uint8_t ry = current_instruction & 0x7;
switch (opcode) {
case 0b00: { // LDST rx, [ry]
uint8_t temp = mmu.read(registers[ry]);
mmu.write(registers[ry], registers[rx]);
registers[rx] = temp;
break;
}
case 0b01: // nand rx, ry
registers[rx] = ~(registers[rx] & registers[ry]);
break;
case 0b10: // cmp rx, ry
status_flag = registers[rx] == registers[ry];
break;
case 0b11: // addeq rx, ry
if (status_flag)
registers[rx] += registers[ry];
break;
default:
throw unreachableOpcodeException();
}
registers[PC_REGISTER]++;
}