-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsystem.c
530 lines (419 loc) · 15.7 KB
/
system.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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/******************************************************************************
File: system.c
Created: 2019-06-04
Updated: 2019-08-04
Author: Aaron Oman
Notice: Creative Commons Attribution 4.0 International License (CC-BY 4.0)
******************************************************************************/
#include <string.h> // memset
#include <stdlib.h> // malloc, free
#include <stdio.h>
#include <pthread.h>
#include "system.h"
#define GRAPHICS_WIDTH 64
#define GRAPHICS_HEIGHT 32
#define MEMORY_SIZE 4096
#define NUM_REGISTERS 16
#define GRAPHICS_MEM_SIZE GRAPHICS_WIDTH*GRAPHICS_HEIGHT
#define STACK_SIZE 16
#define NUM_KEYS 16
#define FONT_SIZE 80
struct system_wfk { // wait for key
unsigned char reg; // 0 - 16
int waiting;
int justChanged;
pthread_rwlock_t lock;
};
struct system_debug {
int enabled;
int fetchAndDecode;
int execute;
pthread_rwlock_t lock;
};
struct system_private {
struct system_wfk wfk;
struct system_debug debug;
// There are two timer registers that count at 60 Hz. When set above
// zero they will count down to zero.
unsigned char delayTimer;
unsigned char soundTimer;
int soundTimerTriggered;
int shouldQuit; // Inidicates if program is closed or otherwise quit.
pthread_rwlock_t timerRwLock;
pthread_rwlock_t soundRwLock;
pthread_rwlock_t gfxRwLock;
pthread_rwlock_t shouldQuitLock;
pthread_rwlock_t keyLock;
};
static unsigned char MEMORY[MEMORY_SIZE];
static unsigned char GFX[GRAPHICS_MEM_SIZE];
static unsigned char fontset[FONT_SIZE] = {
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
0x20, 0x60, 0x20, 0x20, 0x70, // 1
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
0x90, 0x90, 0xF0, 0x10, 0x10, // 4
0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
0xF0, 0x10, 0x20, 0x40, 0x40, // 7
0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
0xF0, 0x90, 0xF0, 0x90, 0x90, // A
0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
0xF0, 0x80, 0x80, 0x80, 0xF0, // C
0xE0, 0x90, 0x90, 0x90, 0xE0, // D
0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
0xF0, 0x80, 0xF0, 0x80, 0x80 // F
};
struct system *SystemInit(int isDebugEnabled) {
struct system_private *prv = (struct system_private *)malloc(sizeof(struct system_private));
memset(prv, 0, sizeof(struct system_private));
struct system *s = (struct system *)malloc(sizeof(struct system));
memset(s, 0, sizeof(struct system));
s->memory = MEMORY;
memset(MEMORY, 0, MEMORY_SIZE);
s->gfx = GFX;
memset(GFX, 0, GRAPHICS_MEM_SIZE);
s->pc = 0x200;
s->fontp = 0;
for (int i=s->fontp; i<FONT_SIZE; i++) {
s->memory[i] = fontset[i];
}
s->prv = prv;
s->prv->debug.enabled = isDebugEnabled;
s->prv->debug.fetchAndDecode = 1;
s->prv->debug.execute = 0;
pthread_rwlockattr_t attr;
pthread_rwlockattr_init(&attr);
pthread_rwlockattr_setpshared(&attr, 1);
if (0 != pthread_rwlock_init(&s->prv->keyLock, &attr)) {
fprintf(stderr, "Couldn't initialize system key rwlock");
return NULL;
}
if (0 != pthread_rwlock_init(&s->prv->debug.lock, &attr)) {
fprintf(stderr, "Couldn't initialize system debugUi rwlock");
return NULL;
}
if (0 != pthread_rwlock_init(&s->prv->shouldQuitLock, &attr)) {
fprintf(stderr, "Couldn't initialize system shouldQuit rwlock");
return NULL;
}
if (0 != pthread_rwlock_init(&s->prv->wfk.lock, &attr)) {
fprintf(stderr, "Couldn't initialize system wfk rwlock");
return NULL;
}
if (0 != pthread_rwlock_init(&s->prv->timerRwLock, &attr)) {
fprintf(stderr, "Couldn't initialize system timer rwlock");
return NULL;
}
if (0 != pthread_rwlock_init(&s->prv->soundRwLock, &attr)) {
fprintf(stderr, "Couldn't initialize system sound rwlock");
return NULL;
}
if (0 != pthread_rwlock_init(&s->prv->gfxRwLock, &attr)) {
fprintf(stderr, "Couldn't initialize system gfx rwlock");
return NULL;
}
return s;
}
void SystemDeinit(struct system *s) {
if (NULL == s)
return;
if (NULL != s->prv)
free(s->prv);
if (0 != pthread_rwlock_destroy(&s->prv->keyLock)) {
fprintf(stderr, "Couldn't destroy system key rwlock");
}
if (0 != pthread_rwlock_destroy(&s->prv->debug.lock)) {
fprintf(stderr, "Couldn't destroy system debugUi rwlock");
}
if (0 != pthread_rwlock_destroy(&s->prv->shouldQuitLock)) {
fprintf(stderr, "Couldn't destroy system shouldQuit rwlock");
}
if (0 != pthread_rwlock_destroy(&s->prv->wfk.lock)) {
fprintf(stderr, "Couldn't destroy system wfk rwlock");
}
if (0 != pthread_rwlock_destroy(&s->prv->timerRwLock)) {
fprintf(stderr, "Couldn't destroy system timer rwlock");
}
if (0 != pthread_rwlock_destroy(&s->prv->soundRwLock)) {
fprintf(stderr, "Couldn't destroy system sound rwlock");
}
if (0 != pthread_rwlock_destroy(&s->prv->gfxRwLock)) {
fprintf(stderr, "Couldn't destroy system gfx rwlock");
}
free(s);
}
// Each opcode is a two-byte instruction, so we have to double increment each time.
void SystemIncrementPC(struct system *s) {
s->pc += 2;
}
unsigned short SystemFontSprite(struct system *s, unsigned int index) {
return s->fontp + (index * 5);
}
int SystemLoadProgram(struct system *s, unsigned char *m, unsigned int size) {
unsigned char *mem = &s->memory[0x200];
unsigned short max_size = MEMORY_SIZE - 0x200;
if (size > max_size) {
return 0;
}
for (int i=0; i<size; i++) {
mem[i] = m[i];
}
return !0;
}
void SystemStackPush(struct system *s) {
if (s->sp > 0xF) {
return;
fprintf(stderr, "SystemStackPush() Tried to push full stack\n");
}
s->stack[s->sp] = s->pc;
s->sp++;
}
void SystemStackPop(struct system *s) {
if (s->sp < 1) {
return;
fprintf(stderr, "SystemStackPop() Tried to pop empty stack\n");
}
s->sp--;
s->pc = s->stack[s->sp];
}
int SystemGfxLock(struct system *s) {
return pthread_rwlock_rdlock(&s->prv->gfxRwLock);
}
int SystemGfxUnlock(struct system *s) {
return pthread_rwlock_unlock(&s->prv->gfxRwLock);
}
void SystemClearScreen(struct system *s) {
if (0 != pthread_rwlock_rdlock(&s->prv->gfxRwLock)) {
fprintf(stderr, "Failed to lock system gfx rw lock");
return;
}
memset(s->gfx, 0, GRAPHICS_MEM_SIZE);
pthread_rwlock_unlock(&s->prv->gfxRwLock);
}
void SystemDrawSprite(struct system *s, unsigned int x_pos, unsigned int y_pos, unsigned int height) {
if (0 != pthread_rwlock_rdlock(&s->prv->gfxRwLock)) {
fprintf(stderr, "Failed to lock system gfx rw lock");
return;
}
s->v[15] = 0;
for (int y = 0; y < height; y++) {
// I contains a 1-byte bitmap representing a line of the sprite.
// [XXXX XXXX]
unsigned char pixel = s->memory[s->i + y];
for (int x = 0; x < 8; x++) {
if ((pixel & (0x80 >> x)) == 0) { // This line taken from www.multigesture.net/articles/how-to-write-an-emulator-chip-8-interpreter/
continue;
}
int y_off = (y_pos + y) * GRAPHICS_WIDTH;
int x_off = (x_pos + x);
int pos = y_off + x_off;
if (s->gfx[pos] == 0xFF) {
s->v[15] = 1;
}
s->gfx[pos] ^= 0xFF;
}
}
pthread_rwlock_unlock(&s->prv->gfxRwLock);
}
void SystemWFKSet(struct system *s, unsigned char reg) {
if (0 != pthread_rwlock_wrlock(&s->prv->wfk.lock)) {
fprintf(stderr, "Failed to lock system wfk rwlock");
return;
}
s->prv->wfk.waiting = 1;
s->prv->wfk.justChanged = 0;
s->prv->wfk.reg = reg;
pthread_rwlock_unlock(&s->prv->wfk.lock);
}
int SystemWFKWaiting(struct system *s) {
if (0 != pthread_rwlock_rdlock(&s->prv->wfk.lock)) {
fprintf(stderr, "Failed to lock system wfk rwlock");
return 0;
}
int result = s->prv->wfk.waiting;
pthread_rwlock_unlock(&s->prv->wfk.lock);
return result;
}
void SystemWFKOccurred(struct system *s, unsigned char key) {
if (0 != pthread_rwlock_wrlock(&s->prv->wfk.lock)) {
fprintf(stderr, "Failed to lock system wfk rwlock");
return;
}
s->prv->wfk.waiting = 0;
s->prv->wfk.justChanged = 1;
s->v[s->prv->wfk.reg] = key;
pthread_rwlock_unlock(&s->prv->wfk.lock);
}
int SystemWFKChanged(struct system *s) {
if (0 != pthread_rwlock_rdlock(&s->prv->wfk.lock)) {
fprintf(stderr, "Failed to lock system wfk rwlock");
return 0;
}
int result = s->prv->wfk.justChanged;
pthread_rwlock_unlock(&s->prv->wfk.lock);
return result;
}
void SystemWFKStop(struct system *s) {
if (0 != pthread_rwlock_wrlock(&s->prv->wfk.lock)) {
fprintf(stderr, "Failed to lock system wfk rwlock");
return;
}
s->prv->wfk.justChanged = 0;
pthread_rwlock_unlock(&s->prv->wfk.lock);
}
void SystemDecrementTimers(struct system *s) {
if (0 != pthread_rwlock_wrlock(&s->prv->timerRwLock)) {
fprintf(stderr, "Failed to lock system timer rw lock");
return;
}
if (s->prv->delayTimer > 0) {
s->prv->delayTimer--;
}
if (s->prv->soundTimer > 0) {
s->prv->soundTimer--;
}
pthread_rwlock_unlock(&s->prv->timerRwLock);
}
int SystemDelayTimer(struct system *s) {
if (0 != pthread_rwlock_rdlock(&s->prv->timerRwLock)) {
fprintf(stderr, "Failed to lock system timer rw lock");
return -1;
}
int result = s->prv->delayTimer;
pthread_rwlock_unlock(&s->prv->timerRwLock);
return result;
}
int SystemSoundTimer(struct system *s) {
if (0 != pthread_rwlock_rdlock(&s->prv->timerRwLock)) {
fprintf(stderr, "Failed to lock system timer rw lock");
return -1;
}
int result = s->prv->soundTimer;
pthread_rwlock_unlock(&s->prv->timerRwLock);
return result;
}
void SystemSetTimers(struct system *s, int dt, int st) {
if (0 != pthread_rwlock_wrlock(&s->prv->timerRwLock)) {
fprintf(stderr, "Failed to lock system timer rw lock");
return;
}
if (dt != -1) {
s->prv->delayTimer = dt;
}
if (st != -1) {
s->prv->soundTimer = st;
}
pthread_rwlock_unlock(&s->prv->timerRwLock);
}
int SystemSoundTriggered(struct system *s) {
if (0 != pthread_rwlock_rdlock(&s->prv->soundRwLock)) {
fprintf(stderr, "Failed to lock system timer rw lock");
return 0;
}
int result = (s->prv->soundTimerTriggered && s->prv->soundTimer == 0);
pthread_rwlock_unlock(&s->prv->soundRwLock);
return result;
}
void SystemSoundSetTrigger(struct system *s, int v) {
if (0 != pthread_rwlock_wrlock(&s->prv->soundRwLock)) {
fprintf(stderr, "Failed to lock system timer rw lock");
return;
}
s->prv->soundTimerTriggered = v;
pthread_rwlock_unlock(&s->prv->soundRwLock);
}
int SystemShouldQuit(struct system *s) {
if (0 != pthread_rwlock_rdlock(&s->prv->shouldQuitLock)) {
fprintf(stderr, "Failed to lock system shouldQuit rwlock");
return 0;
}
int result = s->prv->shouldQuit;
pthread_rwlock_unlock(&s->prv->shouldQuitLock);
return result;
}
void SystemSignalQuit(struct system *s) {
if (0 != pthread_rwlock_wrlock(&s->prv->shouldQuitLock)) {
fprintf(stderr, "Failed to lock system shouldQuit rwlock");
return;
}
s->prv->shouldQuit = 1;
pthread_rwlock_unlock(&s->prv->shouldQuitLock);
}
int SystemDebugIsEnabled(struct system *s) {
if (0 != pthread_rwlock_rdlock(&s->prv->debug.lock)) {
fprintf(stderr, "Failed to lock system shouldQuit rwlock");
return 0;
}
int result = s->prv->debug.enabled;
pthread_rwlock_unlock(&s->prv->debug.lock);
return result;
}
int SystemDebugShouldFetchAndDecode(struct system *s) {
if (0 != pthread_rwlock_rdlock(&s->prv->debug.lock)) {
fprintf(stderr, "Failed to lock system shouldQuit rwlock");
return 0;
}
int result = s->prv->debug.fetchAndDecode;
pthread_rwlock_unlock(&s->prv->debug.lock);
return result;
}
int SystemDebugShouldExecute(struct system *s) {
if (0 != pthread_rwlock_rdlock(&s->prv->debug.lock)) {
fprintf(stderr, "Failed to lock system shouldQuit rwlock");
return 0;
}
int result = s->prv->debug.execute;
pthread_rwlock_unlock(&s->prv->debug.lock);
return result;
}
void SystemDebugSetEnabled(struct system *s, int onOrOff) {
if (0 != pthread_rwlock_wrlock(&s->prv->debug.lock)) {
fprintf(stderr, "Failed to lock system shouldQuit rwlock");
return;
}
s->prv->debug.enabled = onOrOff;
pthread_rwlock_unlock(&s->prv->debug.lock);
}
void SystemDebugSetFetchAndDecode(struct system *s, int onOrOff) {
if (0 != pthread_rwlock_wrlock(&s->prv->debug.lock)) {
fprintf(stderr, "Failed to lock system shouldQuit rwlock");
return;
}
s->prv->debug.fetchAndDecode = onOrOff;
pthread_rwlock_unlock(&s->prv->debug.lock);
}
void SystemDebugSetExecute(struct system *s, int onOrOff) {
if (0 != pthread_rwlock_wrlock(&s->prv->debug.lock)) {
fprintf(stderr, "Failed to lock system shouldQuit rwlock");
return;
}
s->prv->debug.execute = onOrOff;
pthread_rwlock_unlock(&s->prv->debug.lock);
}
int SystemKeyIsPressed(struct system *s, int key) {
int result = 0;
if (key < 0 || key > 0xF) {
return result;
}
if (0 != pthread_rwlock_rdlock(&s->prv->keyLock)) {
fprintf(stderr, "Failed to lock system key rwlock");
return 0;
}
result = s->key[key];
pthread_rwlock_unlock(&s->prv->keyLock);
return result;
}
void SystemKeySetPressed(struct system *s, int key, int pressed) {
if (0 != pthread_rwlock_wrlock(&s->prv->keyLock)) {
fprintf(stderr, "Failed to lock system key rwlock");
return;
}
if (0 == pressed) {
s->key[key] = 0;
} else {
s->key[key] = 0xFF;
}
pthread_rwlock_unlock(&s->prv->keyLock);
}