-
Notifications
You must be signed in to change notification settings - Fork 0
/
SAP_EATER.cpp
184 lines (131 loc) · 3.95 KB
/
SAP_EATER.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
constexpr auto NOP = 0b00000000;
constexpr auto LDA = 0b00010000;
constexpr auto ADD = 0b00100000;
constexpr auto SUB = 0b00110000;
constexpr auto STA = 0b01000000;
constexpr auto LDI = 0b01010000;
constexpr auto JMP = 0b01100000;
constexpr auto JC = 0b01110000;
constexpr auto JZ = 0b10000000;
constexpr auto OUT = 0b11100000;
constexpr auto HLT = 0b11110000;
int main()
{
//16 bytes of ram
char RAM[16] = {};
//program portion of RAM
RAM[0] = LDA | 0xD;
RAM[1] = SUB | 0xE;
RAM[2] = JZ | 0x7;
RAM[3] = LDA | 0xD;
RAM[4] = ADD | 0xF;
RAM[5] = STA | 0xD;
RAM[6] = JMP | 0x0;
RAM[7] = OUT;
RAM[8] = HLT;
//data portion of RAM
RAM[0xD] = 0x01;
RAM[0xE] = 0x0A;
RAM[0xF] = 0x01;
//A and B registers
int A = 0;
int B = 0;
//output register
int out = 0;
//program counter
int PC = 0;
//instruction register
int IR = 0;
//flagged when result of arithmetic carrys
bool carryFlag = false;
//flagged when result of arithmetic is zero
bool zeroFlag = false;
//breaks loop
bool halt = false;
//need to set the first instruction
IR = (RAM[PC] & 0xF0);
std::cout << std::dec;
while (!halt) {
switch (IR) {
case NOP:
//do nothing
//fetch next instruction
IR = (RAM[++PC] & 0xF0);
std::cout << "NOP" << std::endl;
break;
case LDA:
A = RAM[RAM[PC] & 0x0F];
IR = (RAM[++PC] & 0xF0);
std::cout << "LDA: " << A << std::endl;
break;
case ADD:
carryFlag = false;
zeroFlag = false;
B = RAM[RAM[PC] & 0x0F];
A += B;
if (A == 0) zeroFlag = true;
if (A & 0x100) {
carryFlag = true;
A = A & 0xFF;
}
std::cout << "ADD: " << A << std::endl;
IR = (RAM[++PC] & 0xF0);
break;
case SUB:
//does this need to be here?
//carryFlag = false;
zeroFlag = false;
B = RAM[RAM[PC] & 0x0F];
A -= B;
if (A == 0) zeroFlag = true;
std::cout << "SUB: " << A << std::endl;
IR = (RAM[++PC] & 0xF0);
break;
case STA:
RAM[RAM[PC] & 0x0F] = A;
std::cout << "STA: " << A << " @ " << (RAM[PC] & 0x0F) << std::endl;
IR = (RAM[++PC] & 0xF0);
break;
case LDI:
A = RAM[RAM[PC] & 0x0F];
std::cout << "LDI: " << A << " from: " << (int)(RAM[PC] & 0x0F) << std::endl;
IR = (RAM[++PC] & 0xF0);
break;
case JMP:
PC = RAM[PC] & 0x0F;
std::cout << "JMP: " << (int)PC << std::endl;
IR = (RAM[PC] & 0xF0);
break;
case JC:
if (carryFlag) {
PC = RAM[PC] & 0x0F;
carryFlag = false;
IR = (RAM[PC] & 0xF0);
}
else
IR = (RAM[++PC] & 0xF0);
std::cout << "JC: " << (int)PC << std::endl;
break;
case JZ:
if (zeroFlag) {
PC = RAM[PC] & 0x0F;
zeroFlag = false;
IR = (RAM[PC] & 0xF0);
}
else
IR = (RAM[++PC] & 0xF0);
std::cout << "JZ: " << (int)PC << std::endl;
break;
case OUT:
out = A;
std::cout << "OUT: " << (int)out << std::endl;
IR = (RAM[++PC] & 0xF0);
break;
case HLT:
std::cout << "HLT" << std::endl;
halt = true;
break;
}
}
}