-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
405 lines (383 loc) · 9 KB
/
main.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
#include <Uefi.h>
#include <Library/UefiApplicationEntryPoint.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Protocol/Rng.h>
#include "Screen.h"
#include "Chip8.h"
#include "Roms.h"
static EFI_RNG_PROTOCOL* pRng = NULL;
static EFI_GUID gRngGuid = EFI_RNG_PROTOCOL_GUID;
EFI_STATUS EFIAPI
UefiMain(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
{
Initialization();
// Print Games
Print(L"1 - PONG\n2 - PONG2\n3 - TETRIS\n4 - BRIX\n5 - TANK");
EFI_INPUT_KEY Key;
UINTN EventIndex;
BOOLEAN KeyOk = FALSE;
while(!KeyOk){
gBS->WaitForEvent(1, &gST->ConIn->WaitForKey, &EventIndex);
gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
KeyOk = TRUE;
switch(Key.UnicodeChar){
case '1':
LoadRom(PONG, PONG_len);
break;
case '2':
LoadRom(PONG2, PONG2_len);
break;
case '3':
LoadRom(TETRIS, TETRIS_len);
break;
case '4':
LoadRom(BRIX, BRIX_len);
break;
case '5':
LoadRom(TANK, TANK_len);
break;
default:
KeyOk = FALSE;
break;
}
}
Emulate();
return EFI_SUCCESS;
}
VOID
Emulate(VOID)
{
UINT32 pc = 0x200;
UINT8 sp = 0xE;
UINT16 I = 0;
UINT16 DT = 0;
UINT16 ST = 0;
SetBackgroundColor(0, 0, 0);
while(TRUE){
if(pc == RAMSIZE - 1) break;
UINT16 instr = Ram[pc] << 8 | Ram[pc+1];
// 00EE RET
if(instr == 0x00EE){
if(sp == 0xE){
Print(L"Stack pointer already on top!\n");
break;
}
pc = Stack[++sp];
}
// 00E0 CLS
else if(instr == 0x00E0){
gBS->SetMem(Screen, WIDTH*HEIGHT, 0);
}
// 0nnn - SYS addr
else if(instr >> 12 == 0){
//Nothing
}
// 1nnn - JP addr
else if(instr >> 12 == 1){
pc = (instr & 0x0FFF);
pc -= 2;
}
// 2nnn - CALL addr
else if(instr >> 12 == 2){
UINT16 addr = instr & 0x0FFF;
Stack[sp--] = pc;
pc = addr - 2;
}
// 3xkk - SE Vx, byte
else if(instr >> 12 == 3){
UINT8 x = instr >> 8 & 0xF;
UINT8 value = instr & 0xFF;
if(Reg[x] == value) pc += 2;
}
// 4xkk - SNE
else if(instr >> 12 == 4){
UINT8 x = instr >> 8 & 0xF;
UINT8 value = instr & 0xFF;
if(Reg[x] != value) pc += 2;
}
// 5xy0 - SE Vx, Vy
else if(instr >> 12 == 5 && (instr & 0xF) == 0){
UINT32 x = (instr >> 8) & 0xF;
UINT32 y = (instr >> 4) & 0xF;
if(Reg[x] == Reg[y]) pc+=2;
}
// 6xkk - LD Vx, byte
else if(instr >> 12 == 6){
UINT32 x = (instr >> 8) & 0xF;
UINT8 value = instr & 0xFF;
Reg[x] = value;
}
// 7xkk - ADD Vx, byte
else if(instr >> 12 == 7){
UINT32 x = (instr >> 8) & 0xF;
UINT8 value = instr & 0xFF;
Reg[x] += value;
}
// 8xy0 - LD Vx, Vy
else if(instr >> 12 == 8)
instruction8(instr);
// 9xy0 - SNE Vx, Vy
else if(instr >> 12 == 9 && (instr & 0xF) == 0){
UINT32 x = (instr >> 8) & 0xF;
UINT32 y = (instr >> 4) & 0xF;
if(Reg[x] != Reg[y]) pc += 2;
}
// Annn - LD I, addr
else if(instr >> 12 == 0xA){
I = instr & 0x0FFF;
}
// Bnnn - JP V0, addr
else if(instr >> 12 == 0xB){
UINT32 addr = instr & 0xFFF;
pc = addr + Reg[0] - 2;
}
// Cxkk - RND Vx, byte
else if(instr >> 12 == 0xC){
UINT32 x = (instr >> 8) & 0xF;
UINT32 value = instr & 0xFF;
UINT8 rand = 0;
if(pRng != NULL){
pRng->GetRNG(pRng, NULL, 1, &rand);
} else {
UINT64 Count;
gBS->GetNextMonotonicCount(&Count);
rand = Count & 0xFF;
}
Reg[x] = (rand++) & value;
}
// Dxyn - DRW Vx, Vy, nibble
else if(instr >> 12 == 0xD){
Reg[0xF] = 0;
UINT32 vx = (instr >> 8) & 0xF;
UINT32 vy = (instr >> 4) & 0xF;
UINT32 x = Reg[vx];
UINT32 y = Reg[vy];
UINT32 n = instr & 0xF;
for(UINT32 i = 0; i < n; i++){
UINT8 b = Ram[I+i];
for(UINT32 j = 0 ; j < 8; j++){
UINT32 bit = (b >> (7 - j)) & 0x1;
if(bit && Screen[y+i][x+j])
Reg[0xF] = 1;
Screen[y+i][x+j] ^= bit;
}
}
}
else if(instr >> 12 == 0xE)
instructionE(instr, &pc);
else if(instr >> 12 == 0xF)
instructionF(instr, &DT, &ST, &I);
else
Print(L"Unknown opcode %x\n", instr);
// Draw the screen
drawScreen();
if(DT != 0) DT--;
if(ST != 0) ST--;
// Slow down the game
gBS->Stall(2500);
pc += 2;
}
}
VOID
drawScreen(VOID)
{
UINT32 i, j;
for(i = 0; i < HEIGHT; i++){
for(j = 0; j < WIDTH; j++){
if(Screen[i][j] == ScreenBefore[i][j]) continue;
if(Screen[i][j])
DrawRect(j*PIXELSIZE, i*PIXELSIZE, PIXELSIZE, PIXELSIZE, white);
else
DrawRect(j*PIXELSIZE, i*PIXELSIZE, PIXELSIZE, PIXELSIZE, black);
}
}
gBS->CopyMem(ScreenBefore, Screen, HEIGHT*WIDTH);
}
VOID
instruction8(UINT32 instr)
{
UINT32 x = (instr >> 8) & 0xF;
UINT32 y = (instr >> 4) & 0xF;
UINT32 code = (instr & 0xF);
switch(code){
case 0:
Reg[x] = Reg[y];
break;
case 1:
Reg[x] |= Reg[y];
break;
case 2:
Reg[x] &= Reg[y];
break;
case 3:
Reg[x] ^= Reg[y];
break;
case 4:
Reg[0xF] = (((UINT32) Reg[x]) + ((UINT32) Reg[y]) > 255);
Reg[x] += Reg[y];
break;
case 5:
Reg[0xF] = (Reg[x] > Reg[y]);
Reg[x] -= Reg[y];
break;
case 6:
Reg[0xF] = Reg[x] & 0x1;
Reg[x] >>= 1;
break;
case 7:
Reg[0xF] = (Reg[y] > Reg[x]);
Reg[x] = Reg[y] - Reg[x];
break;
case 0xE:
Reg[0xF] = (Reg[x] & 0x80) != 0;
Reg[x] <<= 1;
break;
}
}
EFI_INPUT_KEY
keyToValue(UINT8 value)
{
EFI_INPUT_KEY ret = {0, 0};
if(value >= 0 && value <= 9)
ret.UnicodeChar = value + '0';
else if(value >= 0xA && value <= 0xF)
ret.UnicodeChar = value - 0xA + 'a';
return ret;
}
UINT8
valueToKey(EFI_INPUT_KEY key)
{
CHAR16 code = key.UnicodeChar;
if(code >= '0' && code <= '9')
return code - '0';
else if(code >= 'a' && code <= 'f')
return code - 'a' + 10;
return 0xFF;
}
VOID
instructionE(UINT32 instr, UINT32* pc){
UINT32 x = instr >> 8 & 0xF;
UINT32 code = instr & 0xFF;
EFI_STATUS stat;
if(code == 0x9E){
EFI_INPUT_KEY Key, KeyD = keyToValue(Reg[x]);
stat = gST->ConIn->ReadKeyStroke(gST->ConIn, &Key);
if(stat == EFI_SUCCESS && Key.UnicodeChar == KeyD.UnicodeChar)
*pc += 2;
}
else if(code == 0xA1){
EFI_INPUT_KEY Key, KeyD = keyToValue(Reg[x]);
stat = gST->ConIn->ReadKeyStroke(gST->ConIn, &Key);
if(stat != EFI_SUCCESS || Key.UnicodeChar != KeyD.UnicodeChar)
*pc += 2;
}
else{
Print(L"Unknwon opcode %x\n", instr);
}
}
VOID
instructionF(UINT32 instr, UINT16* DT, UINT16* ST, UINT16* I)
{
UINT32 x = (instr >> 8) & 0xF;
UINT32 code = instr & 0xFF;
UINT32 i = 0, ok = 0;
EFI_INPUT_KEY Key;
UINTN EventIndex;
switch(code){
case 0x07:
Reg[x] = *DT;
break;
case 0x0A:
while(!ok){
gBS->WaitForEvent(1, &gST->ConIn->WaitForKey, &EventIndex);
gST->ConIn->ReadKeyStroke(gST->ConIn, &Key);
i = valueToKey(Key);
if(0 <= i && i <= 0xF){
Reg[x] = i;
ok = TRUE;
}
}
break;
case 0x15:
*DT = Reg[x];
break;
case 0x18:
*ST = Reg[x];
break;
case 0x1E:
*I += Reg[x];
break;
case 0x29:
*I = Reg[x] * 5;
break;
case 0x33:
Ram[*I] = Reg[x]/100;
Ram[*I+1] = (Reg[x]/10) % 10;
Ram[*I+2] = Reg[x] % 10;
break;
case 0x55:
for(i = 0; i <= x; i++)
Ram[*I+i] = Reg[i];
break;
case 0x65:
for(i = 0; i <= x; i++)
Reg[i] = Ram[*I+i];
break;
}
}
VOID
Initialization(VOID)
{
EFI_STATUS s = InitScreen();
if(EFI_ERROR(s)){
Print(L"Error initializing screen\nPress any key to exit...\n");
PressKey(FALSE);
Exit(s);
}
PIXELSIZE = ScreenGetWidth()/WIDTH;
gBS->SetMem(Ram, RAMSIZE, 0);
gBS->SetMem(Screen, HEIGHT*WIDTH, 0);
gBS->SetMem(ScreenBefore, HEIGHT*WIDTH, 0);
//Add chars to RAM
UINT8 Chars[] = {0xF0,0x90,0x90,0x90,0xF0,
0x20,0x60,0x20,0x20,0x70,
0xF0,0x10,0xF0,0x80,0xF0,
0xF0,0x10,0xF0,0x10,0xF0,
0x90,0x90,0xF0,0x10,0x10,
0xF0,0x80,0xF0,0x10,0xF0,
0xF0,0x80,0xF0,0x90,0xF0,
0xF0,0x10,0x20,0x40,0x40,
0xF0,0x90,0xF0,0x90,0xF0,
0xF0,0x90,0xF0,0x10,0xF0,
0xF0,0x90,0xF0,0x90,0x90,
0xE0,0x90,0xE0,0x90,0xE0,
0xF0,0x80,0x80,0x80,0xF0,
0xE0,0x90,0x90,0x90,0xE0,
0xF0,0x80,0xF0,0x80,0xF0,
0xF0,0x80,0xF0,0x80,0x80};
gBS->CopyMem(Ram, Chars, sizeof(Chars));
// Init random protocol
s = gBS->LocateProtocol(&gRngGuid, NULL, (VOID**) &pRng);
if( EFI_ERROR(s) || pRng == NULL ){
Print(L"Error initializing random generator protocol (error %d)\n\
Numbers generated won't be random\n\n", s);
}
}
VOID
LoadRom(UINT8* Data, UINT32 Data_len)
{
gBS->CopyMem(Ram+0x200, Data, Data_len);
}
VOID
PressKey(BOOLEAN DisplayText)
{
EFI_INPUT_KEY Key;
//EFI_STATUS Status;
UINTN EventIndex;
if (DisplayText) {
Print(L"\nPress any key to continue ....\n\n");
}
gBS->WaitForEvent(1, &gST->ConIn->WaitForKey, &EventIndex);
gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
}