-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.cpp
423 lines (383 loc) · 8.68 KB
/
cpu.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include <iostream>
#include "cpu.h"
#include "memory.h"
#include "emt.h"
CPU::CPU(Memory& mem, uint32_t start) : memory(mem)
{
registers[PC].Value(start);
}
static bool CmpOverflow(uint64_t v, uint32_t v1, uint32_t v2, uint64_t sign)
{
/*
cmp:
V: set if there was arithmetic overflow; that is, operands were
of opposite signs and the sign of the destination was the
same as the sign of the result; cleared otherwise
*/
return ((v & sign) == (v2 & sign)) & ((v1 ^ v2) & sign);
}
static bool SubOverflow(uint64_t v, uint32_t v1, uint32_t v2, uint64_t sign)
{
/* sub:
V: set if there was arithmetic overflow as a result of the oper·
ation, that is if operands were of opposite signs and the sign
of the source was the same as the sign of the result; cleared
otherwise. */
return ((v & sign) == (v1 & sign)) & ((v1 ^ v2) & sign);
}
static bool AddOverflow(uint64_t v, uint32_t v1, uint32_t v2, uint64_t sign)
{
/* add:
V: set if there was arithmetic overflow as a result of the oper·
ation; that is both operands were of the same sign and the
result was of the opposite sign; cleared otherwise
a */
return ((v & sign) != (v1 & sign)) & ((v1 & sign) == (v2 & sign));
}
void CPU::UpdateFlags(uint64_t v, uint32_t v1, uint32_t v2, OperandSize opsize,
OverflowFunc oflow)
{
uint64_t mask;
switch(opsize)
{
case Op8:
mask = 0xff;
break;
case Op16:
mask = 0xffff;
break;
case Op32:
mask = 0xffffffff;
break;
}
uint64_t sign = (mask + 1) >> 1;
flags.z = !(v & mask);
flags.n = v & sign;
flags.c = v & (mask + 1);
if (oflow)
{
flags.v = oflow(v, v1, v2, sign);
}
else
{
flags.v = false;
}
}
static uint32_t SizeFromOpSize(OperandSize opsize)
{
switch(opsize)
{
case Op8:
return 1;
case Op16:
return 2;
case Op32:
return 4;
}
}
uint32_t CPU::GetValue(AddrMode mode, RegName reg, OperandSize opsize)
{
size_t size = SizeFromOpSize(opsize);
size_t regsize = size;
if (reg == PC || reg == SP)
{
regsize = 4;
}
switch(mode)
{
case Direct:
return registers[reg].Value();
case Indir:
return ReadMem(registers[reg].Value(), size);
case IndirAutoInc:
{
uint32_t v = ReadMem(registers[reg].Value(), size);
registers[reg] += regsize;
return v;
}
case AutoDecIndir:
registers[reg] -= regsize;
return ReadMem(registers[reg].Value(), size);
}
return 0xdeadbeef;
}
uint32_t CPU::GetSourceValue(Instruction instr)
{
return GetValue(instr.value.srcMode, instr.value.source, instr.value.size);
}
uint32_t CPU::GetDestValue(Instruction instr)
{
return GetValue(instr.value.destMode, instr.value.dest, instr.value.size);
}
static uint32_t SignExtend(uint32_t value, OperandSize opsize)
{
switch(opsize)
{
case Op32:
break;
case Op16:
if (value & 0x8000)
{
value |= 0xFFFF0000;
}
break;
case Op8:
if (value & 0x80)
{
value |= 0xFFFFFF00;
}
break;
}
return value;
}
void CPU::StoreDestValue(Instruction instr, uint32_t value)
{
size_t size = SizeFromOpSize(instr.value.size);
size_t regsize = size;
RegName reg = instr.value.dest;
if (reg == PC || reg == SP)
{
regsize = 4;
}
switch(instr.value.destMode)
{
case Direct:
value = SignExtend(value, instr.value.size);
registers[reg].Value(value);
break;
case Indir:
WriteMem(registers[reg].Value(), value, size);
break;
case IndirAutoInc:
WriteMem(registers[reg].Value(), value, size);
registers[reg] += regsize;
break;
case AutoDecIndir:
registers[reg] -= regsize;
WriteMem(registers[reg].Value(), value, size);
break;
}
}
void CPU::Emt(uint32_t num)
{
switch(num)
{
case PrintChar:
std::cout << (char)registers[R0].Value() << std::flush;
break;
}
}
void CPU::BranchIfTrue(Instruction instr, bool cond)
{
if (cond)
{
registers[PC] += instr.value.branch;
}
}
void CPU::Move(Instruction instr)
{
uint32_t v = GetSourceValue(instr);
StoreDestValue(instr, v);
UpdateFlags(v, v, v, instr.value.size, 0 );
}
void CPU::Add(Instruction instr)
{
uint32_t v1 = GetSourceValue(instr);
uint32_t v2 = GetDestValue(instr);
uint64_t v = static_cast<uint64_t>(v1) + v2;
StoreDestValue(instr, v);
UpdateFlags(v, v1, v2, instr.value.size, AddOverflow);
}
void CPU::Sub(Instruction instr)
{
uint32_t src = GetSourceValue(instr);
uint32_t dest = GetDestValue(instr);
uint32_t v = static_cast<uint64_t>(dest) - src;
StoreDestValue(instr, v);
UpdateFlags(v, src, dest, instr.value.size, SubOverflow);
}
void CPU::Cmp(Instruction instr)
{
uint32_t src = GetSourceValue(instr);
uint32_t dest = GetDestValue(instr);
uint64_t v = static_cast<uint64_t>(src) - dest;
UpdateFlags(v, src, dest, instr.value.size, CmpOverflow);
}
void CPU::Div(Instruction instr)
{
uint32_t v1 = GetSourceValue(instr);
uint32_t v2 = GetDestValue(instr);
uint32_t v_div = v2 / v1;
uint32_t v_mod = v2 % v1;
StoreDestValue(instr, v_div);
if (instr.value.destMode == Direct)
{
registers[instr.value.dest+1].Value(v_mod);
}
UpdateFlags(v_div, 0, 0, instr.value.size, 0); // TODO: Add ovflowe func.
}
void CPU::Mul(Instruction instr)
{
uint32_t v1 = GetSourceValue(instr);
uint32_t v2 = GetDestValue(instr);
uint64_t v = static_cast<uint64_t>(v2) * v1;
StoreDestValue(instr, v);
UpdateFlags(v, 0, 0, instr.value.size, 0); // TODO: Add overflow func.
}
void CPU::Jmp(Instruction instr)
{
uint32_t v = GetSourceValue(instr);
registers[PC].Value(v);
}
void CPU::Jsr(Instruction instr)
{
uint32_t v = GetSourceValue(instr);
registers[SP] -= 4;
WriteMem(registers[SP].Value(), registers[PC].Value(), 4);
registers[PC].Value(v);
}
void CPU::Ret(Instruction instr)
{
registers[PC].Value(ReadMem(registers[SP].Value(), 4));
registers[SP] += 4;
}
/* Return true for "continue", false for "stop" */
ExecResult CPU::RunOneInstr()
{
Instruction instr = Fetch();
switch(instr.value.op)
{
case HLT:
std::cout << "Hit halt at " << std::hex << registers[PC].Value()
<< std::endl;
return Halt;
case BPT:
return Breakpoint;
case MOV:
Move(instr);
break;
case ADD:
Add(instr);
break;
case SUB:
Sub(instr);
break;
case CMP:
Cmp(instr);
break;
case DIV:
Div(instr);
break;
case MUL:
Mul(instr);
break;
case CLC:
flags.c = false;
break;
case CLV:
flags.v = false;
break;
case CLN:
flags.n = false;
break;
case CLZ:
flags.z = false;
break;
case SEC:
flags.c = true;
break;
case SEV:
flags.v = true;
break;
case SEN:
flags.n = true;
break;
case SEZ:
flags.z = true;
break;
case JMP:
Jmp(instr);
break;
case JSR:
Jsr(instr);
break;
case RET:
Ret(instr);
break;
/*
|0010xx || BNE || Branch if not equal (Z=0)
|0014xx || BEQ || Branch if equal (Z=1)
|0020xx || BGE || Branch if greater than or equal (N|V = 0)
|0024xx || BLT || Branch if less than (N|V = 1)
|0030xx || BGT || Branch if greater than (N^V = 1)
|0034xx || BLE || Branch if less than or equal (N^V = 0)
|1010xx || BHI || Branch if higher than (C|Z = 0)
|1014xx || BLOS|| Branch if lower or same (C|Z = 1)
|1020xx || BVC || Branch if overflow clear (V=0)
|1024xx || BVS || Branch if overflow set (V=1)
|1030xx || BCC || Branch if carry clear (C=0)
| || BHIS|| Branch if higher or same (C=0)
|1034xx || BCS || Branch if carry set (C=1)
| || BLO || Branch if lower than (C=1)
*/
case BNE:
BranchIfTrue(instr, !flags.z);
break;
case BEQ:
BranchIfTrue(instr, flags.z);
break;
case BLT:
BranchIfTrue(instr, flags.n | flags.v);
break;
case BGT:
BranchIfTrue(instr, flags.n ^ flags.v);
break;
case BGE:
BranchIfTrue(instr, !(flags.n | flags.v));
break;
case BLE:
BranchIfTrue(instr, !(flags.n ^ flags.v));
break;
case BHI:
BranchIfTrue(instr, !(flags.c | flags.z));
break;
case BLOS:
BranchIfTrue(instr, flags.c | flags.z);
break;
case BVS:
BranchIfTrue(instr, flags.v);
break;
case BVC:
BranchIfTrue(instr, !flags.v);
break;
case BCC:
BranchIfTrue(instr, !flags.c);
break;
case BCS:
BranchIfTrue(instr, flags.c);
break;
case BPL:
BranchIfTrue(instr, !flags.n);
break;
case BMI:
BranchIfTrue(instr, flags.n);
break;
case BR:
BranchIfTrue(instr, true);
break;
case NOP:
// Nothing to see here, move on!
break;
case EMT:
Emt(instr.value.branch);
break;
default:
std::cerr << "Not yet impelemented function at: "
<< std::hex << registers[PC].Value()
<< std::endl;
std::cerr << "Instr = " << instr.value.word << std::endl;
return Unknown;
break;
}
return Continue;
}