blob: 4f34d1bfe7f465ad88bb63ab88fd981366aac304 (
plain)
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
|
/*
* Copyright (C) 2025 dwlr <dweller@cabin.digital>
*
* BSD 3-Clause License (BSD-3-Clause)
* See LICENSE for details
*/
#define BEGIN do{
#define END }while(0)
#define X_C8_PRELUDE \
chip8* c8 = usrdat; \
assert(c8); \
\
if(c8->pc >= sizeof(c8->mem)) \
{ \
report(ERR, "CHIP-8: PC(%04X) Overflow!\n", c8->pc); \
c8->running = false; \
} \
\
if(c8->pc & 0x0001) \
{ \
report(ERR, "CHIP-8: PC(%04X) is not aligned!\n", c8->pc); \
c8->running = false; \
}
#define X_C8_EPILOGUE \
BEGIN \
c8->cycles++; \
if((c8->cycles % C8_CYCLES_PER_FRAME) == 0) \
{ \
if(c8->dt) c8->dt--; \
if(c8->st) c8->st--; \
} \
c8->pc += 2; \
c8->prevkeys = c8->keys; \
END
#define X_C8_ILL \
BEGIN \
c8->running = false; \
report(ERR, "Illegal instruction (%04X) @ %04X\n", instr, offset); \
END
#define X_C8_CLS \
BEGIN \
int i, lines = lengthof(c8->DISPLAY); \
for(i = 0; i < lines; i++) c8->DISPLAY[i] = 0; \
END
#define X_C8_RET \
BEGIN \
c8->pc = c8->STACK[--c8->sp]; \
END
#define X_C8_SYS X_C8_ILL
#define X_C8_JP \
BEGIN \
c8->pc = nnn - 2; \
END
#define X_C8_CALL \
BEGIN \
c8->STACK[c8->sp++] = c8->pc; \
c8->pc = nnn; \
END
#define X_C8_LDI \
BEGIN \
c8->i = nnn; \
END
#define X_C8_JPV \
BEGIN \
c8->pc = nnn + c8->V[0] - 2; \
END
#define X_C8_SEB \
BEGIN \
if(c8->V[x] == kk) c8->pc += 2; \
END
#define X_C8_SNEB \
BEGIN \
if(c8->V[x] != kk) c8->pc += 2; \
END
#define X_C8_LD \
BEGIN \
c8->V[x] = kk; \
END
#define X_C8_ADDB \
BEGIN \
int tmp = (int)c8->V[x] + (int)kk; \
c8->V[x] = tmp & 0xFF; \
END
#define X_C8_RND \
BEGIN \
c8->V[x] = (rand() % 255) & kk; \
END
#define X_C8_SKP \
BEGIN \
if(c8->keys & bit(c8->V[x])) c8->pc += 2; \
END
#define X_C8_SKNP \
BEGIN \
if(!(c8->keys & bit(c8->V[x]))) c8->pc += 2; \
END
#define X_C8_MVDT \
BEGIN \
c8->V[x] = c8->dt; \
END
#define X_C8_LDK \
BEGIN \
int i; \
if(c8->keys == c8->prevkeys) c8->pc -= 2; \
else \
{ \
for(i = 7; i >= 0; i--) \
if(c8->keys & bit(i)) \
{ \
c8->V[x] = i; \
break; \
} \
} \
END
#define X_C8_LDDT \
BEGIN \
c8->dt = c8->V[x]; \
END
#define X_C8_LDST \
BEGIN \
c8->st = c8->V[x]; \
END
#define X_C8_ADDI \
BEGIN \
c8->i += c8->V[x]; \
END
#define X_C8_HEX \
BEGIN \
usize m = (usize)(c8->RAM); \
usize f = (usize)(c8->FONT); \
c8->i = (f - m) + c8->V[x] * 5; \
END
#define X_C8_BCD \
BEGIN \
c8->RAM[c8->i] = c8->V[x] / 100; \
c8->RAM[c8->i + 1] = c8->V[x] % 100 / 10; \
c8->RAM[c8->i + 2] = c8->V[x] % 10; \
END
#define X_C8_SAVE \
BEGIN \
u8 i; \
for(i = 0; i < x; i++) c8->RAM[c8->i + i] = c8->V[i]; \
END
#define X_C8_RESTORE \
BEGIN \
u8 i; \
for(i = 0; i < x; i++) c8->V[i] = c8->RAM[c8->i + i]; \
END
#define X_C8_SE \
BEGIN \
if(c8->V[x] == c8->V[y]) c8->pc += 2; \
END
#define X_C8_MOVE \
BEGIN \
c8->V[x] = c8->V[y]; \
END
#define X_C8_OR \
BEGIN \
c8->V[x] |= c8->V[y]; \
END
#define X_C8_AND \
BEGIN \
c8->V[x] &= c8->V[y]; \
END
#define X_C8_XOR \
BEGIN \
c8->V[x] ^= c8->V[y]; \
END
#define X_C8_ADD \
BEGIN \
int tmp = (int)c8->V[x] + (int)c8->V[y]; \
c8->V[0xf] = tmp > 255; \
c8->V[x] = tmp; \
END
#define X_C8_SUB \
BEGIN \
int tmp = (int)c8->V[x] - (int)c8->V[y]; \
c8->V[0xf] = tmp > 0; \
c8->V[x] = tmp; \
END
#define X_C8_SHR \
BEGIN \
c8->V[0xf] = (c8->V[y] & 0x1) ? 1 : 0; \
c8->V[x] = c8->V[y] >> 1; \
END
#define X_C8_SUBN \
BEGIN \
int tmp = (int)c8->V[y] - (int)c8->V[x]; \
c8->V[0xf] = tmp > 0; \
c8->V[x] = tmp; \
END
#define X_C8_SHL \
BEGIN \
c8->V[0xf] = (c8->V[y] & 0x80) ? 1 : 0; \
c8->V[x] = c8->V[y] << 1; \
END
#define X_C8_SNE \
BEGIN \
if(c8->V[x] != c8->V[y]) c8->pc += 2; \
END
/* FIXME: correct wrapping, test */
#define X_C8_DRW \
BEGIN \
u8 l; \
u8 cx = c8->V[x]; \
u8 cy = c8->V[y]; \
\
for(l = 0; l < n; l++) \
c8->DISPLAY[(cy + l) % 32] ^= ((u64)c8->RAM[c8->i + l]) << (64 - cx); \
END
c8_decode_generate(exec)
#undef BEGIN
#undef END
#include "undef.c"
|