-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.c
359 lines (314 loc) · 7.98 KB
/
interpreter.c
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
#include "common.h"
#include "chip8.h"
#include "interpreter.h"
#include "inst.h"
#include <math.h>
#include "chip8mem.h"
#include "eeprom.h"
#include <stdint.h>
uint16 g_Stack[16];
byte g_StackPointer;
#define INST Ch8Inst *
#define X CH8_VREG[inst->x]
#define Y CH8_VREG[inst->y]
#define _X CH8_VREG[inst.x]
#define _Y CH8_VREG[inst.y]
#define FLAG CH8_VREG[0xF]
#define I g_Ch8IRegister
#define CH8VIDEO_XMAX 63
#define CH8VIDEO_YMAX 31
#define DRAW_WRAP_X
#define DRAW_WRAP_Y
//#define INT_DEBUG
void __inline OPCODE_READ_REGS(const INST inst)
{
uint16 i = 0;
for (i = 0; i <= inst->x; i++)
{
CH8_VREG[i] = Ch8MemReadByte(I + i);
}
}
void __inline__ OPCODE_WRITE_REGS(const INST inst)
{
uint16 i = 0;
for (i = 0; i <= inst->x; i++)
{
Ch8MemWriteByte(I + i, CH8_VREG[i]);
}
}
void __inline__ OPCODE_RPL_WRITE_REGS(const INST inst)
{
int i = 0;
for (i = 0; i <= inst->x; i++)
{
g_RplRegs[i] = CH8_VREG[i];
}
}
void __inline__ OPCODE_RPL_READ_REGS(const INST inst)
{
int i = 0;
for (i = 0; i <= inst->x; i++)
{
CH8_VREG[i] = g_RplRegs[i];
}
}
void __inline__ OPCODE_BCD(const byte value)
{
Ch8MemWriteByte(I + 0, value / 100);
Ch8MemWriteByte(I + 1, (value % 100) / 10);
Ch8MemWriteByte(I + 2, (value % 100) % 10);
}
void __inline__ OPCODE_CLR ()
{
#if defined(GLCD) & !defined(INT_DEBUG)
glcd_clear();
#endif
}
void __inline__ OPCODE_RET ()
{
if (g_StackPointer - 1 < 0)
{
/* Cannot go below 0 */
return;
}
PC = g_Stack[--g_StackPointer];
}
void __inline__ OPCODE_CALL(const INST inst)
{
if (g_StackPointer + 1 > 15)
{
/* Cannot go beyond 16 levels */
return;
}
g_Stack[g_StackPointer++] = PC;
PC = inst->nnn;
}
void __inline__ OPCODE_SE(const byte a, const byte b)
{
if (a == b) PC += 2;
}
void __inline__ OPCODE_SNE(const byte a, const byte b)
{
if (a != b) PC += 2;
}
void __inline__ OPCODE_LD(const INST inst, const byte value)
{
X = value;
}
void __inline__ OPCODE_ADD_OVERFLOW(const INST inst, const byte value, int overflowCheck)
{
uint16 result = X + value;
if (overflowCheck)
{
FLAG = result > 0xFF;
}
else
{
FLAG = 0;
}
X = (byte)(result & 0xFF);
}
void __inline__ OPCODE_ADD(const INST inst, const byte value)
{
OPCODE_ADD_OVERFLOW(inst, value, 0);
}
void __inline__ OPCODE_ADD_I (const INST inst, const byte value)
{
if ( (I + value) >= 0x1000 )
{
I = 0x1000;
FLAG = 1;
}
else
{
I += value;
}
}
void __inline__ OPCODE_OR(const INST inst)
{
X |= Y;
FLAG = 0;
}
void __inline__ OPCODE_AND(const INST inst)
{
X &= Y;
FLAG = 0;
}
void __inline__ OPCODE_XOR(const INST inst)
{
X ^= Y;
FLAG = 0;
}
void __inline__ OPCODE_SUB(const INST inst, const byte a, const byte b)
{
FLAG = a >= b;
X = a - b;
}
void __inline__ OPCODE_SHR(const INST inst)
{
FLAG = ((X & 1) == 1);
X = X / 2;
}
void __inline__ OPCODE_SHL(const INST inst)
{
FLAG = ((X & 0x80) >> 7) == 1;
X = X * 2;
}
void __inline__ OPCODE_DRAW(const INST inst)
{
FLAG = 0;
#if defined(GLCD) & !defined(INT_DEBUG)
uint8_t x = X;
uint8_t y = Y;
uint8_t height = inst->n;
byte pixel = 0;
uint8_t readPixel = 0;
int yline = 0;
int xline = 0;
FLAG = 0;
for (yline = 0; yline < height; yline++)
{
/* Read the pixel data directly from EEPROM 1 */
EEPROMReadBytes(&pixel, 1, 1, I + yline);
for (xline = 0; xline < 8; xline++)
{
if (pixel & 0x80)
{
readPixel = glcd_get_pixel(10 + ((x + xline) & 63), 8 + ((y + yline) & 31));
if (readPixel) FLAG = 1;
glcd_set_pixel(10 + ((x + xline) & 63), 8 + ((y + yline) & 31), readPixel ^ 1);
}
pixel <<= 1;
}
}
glcd_write();
#endif
}
unsigned int ComputePeroid(unsigned int frequency)
{
return (unsigned int)((frequency * FCY) - 1);
}
void StartTimer1(int32 frequency)
{
T1CONbits.TON = 0; /* Disable */
T1CONbits.TCS = 0;
T1CONbits.TCKPS = 0;
T1CONbits.TGATE = 0;
TMR1 = 0x00; // Clear timer register
PR1 = ComputePeroid(frequency); // Load the period value
IPC0bits.T1IP = 1;
IFS0bits.T1IF = 0; // Clear Timer2 Interrupt Flag
IEC0bits.T1IE = 1; // Enable Timer2 interrupt
T1CONbits.TON = 1; /* Enable */
}
void StopTimer1()
{
T1CONbits.TON = 0;
TMR1 = 0;
}
void __inline__ SetDelayTimer(byte value)
{
g_Ch8DelayRegister = value;
StartTimer1(17);
}
void __attribute__((interrupt(auto_psv))) _T1Interrupt(void)
{
if (g_Ch8DelayRegister > 0)
g_Ch8DelayRegister--;
else
StopTimer1();
/* Clear the interrupt flag */
IFS0bits.T1IF = 0;
}
uint16 OpcodeMask(const Ch8Inst * inst)
{
uint16 mask = 0xF000;
byte opcode = (inst->inst & 0xF000) >> 12;
switch (opcode)
{
case 0x0:
{
if (inst->inst)
{
if ((inst->inst & 0xFFF0) == 0x00C0)
{
mask = 0xFFF0;
}
else
{
mask = 0xFFFF;
}
}
break;
}
case 0xF:
case 0xE: mask = 0xF0FF; break;
case 0x8: mask = 0xF00F; break;
default: mask = 0xF000; break;
}
return mask;
}
void Ch8Interpreter_ExecuteInst(void)
{
/* Fetch - Load 2 bytes from memory at PC position */
byte h = CH8_MEMORY_GETBYTE(PC++);
byte l = CH8_MEMORY_GETBYTE(PC++);
Ch8Inst inst;
inst.inst = (h << 8) | l;
uint16 mask = OpcodeMask(&inst);
uint16 op = inst.inst & mask;
/* Decode and execute, and write back */
#ifdef INT_DEBUG
debugWriteLcd = 1;
DEBUG_WRITE("%04X:%04X", PC - 2, op);
__delay_ms(700);
#endif
switch (op)
{
case 0x00E0: OPCODE_CLR(); break;
case 0x00EE: OPCODE_RET(); break;
case 0x1000: PC = inst.nnn; break;
case 0x2000: OPCODE_CALL(&inst); break;
case 0x3000: OPCODE_SE(_X, inst.kk); break;
case 0x4000: OPCODE_SNE(_X, inst.kk); break;
case 0x5000: OPCODE_SE(_X, _Y); break;
case 0x6000: OPCODE_LD(&inst, inst.kk); break;
case 0x7000: OPCODE_ADD(&inst, inst.kk); break;
case 0x8000: OPCODE_LD(&inst, _Y); break;
case 0x8001: OPCODE_OR(&inst); break;
case 0x8002: OPCODE_AND(&inst); break;
case 0x8003: OPCODE_XOR(&inst); break;
case 0x8004: OPCODE_ADD_OVERFLOW(&inst, _Y, 1); break;
case 0x8005: OPCODE_SUB(&inst, _X, _Y); break;
case 0x8006: OPCODE_SHR(&inst); break;
case 0x8007: OPCODE_SUB(&inst, _Y, _X); break;
case 0x800E: OPCODE_SHL(&inst); break;
case 0x9000: OPCODE_SNE(_X, _Y); break;
case 0xA000: I = inst.nnn; break;
case 0xB000: PC = inst.nnn + CH8_VREG[0]; break;
case 0xC000: _X = ((byte)(rand() % 256) & inst.kk); break;
case 0xD000: OPCODE_DRAW(&inst); break;
case 0xE09E: OPCODE_SE(_X, g_Ch8LastKeyPress); break;
case 0xE0A1: OPCODE_SNE(_X, g_Ch8LastKeyPress); break;
case 0xF007: OPCODE_LD(&inst, g_Ch8DelayRegister); break;
case 0xF00A: while (!g_WaitForKey); OPCODE_LD(&inst, g_Ch8LastKeyPress); g_WaitForKey = 0; break;
case 0xF015: SetDelayTimer(_X); break;
case 0xF018: g_Ch8AudioRegister = _Y; break;
case 0xF01E: OPCODE_ADD_I(&inst, _X); break;
case 0xF029: I = _X * 5; break;
case 0xF033: OPCODE_BCD(_X); break;
case 0xF055: OPCODE_WRITE_REGS(&inst); break;
case 0xF065: OPCODE_READ_REGS(&inst); break;
case 0xF075: OPCODE_RPL_WRITE_REGS(&inst); break;
case 0xF085: OPCODE_RPL_READ_REGS(&inst); break;
default:
{
#ifdef INT_DEBUG
DEBUG_WRITE("ERR: 0x%04X", inst.inst);
while(1) {}
#else
break;
#endif
}
}
}