-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmmu.c
More file actions
534 lines (490 loc) · 25.1 KB
/
mmu.c
File metadata and controls
534 lines (490 loc) · 25.1 KB
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
531
532
533
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>
#include "gbz80.h"
#include "mmu.h"
#include "gpu.h"
#include "keys.h"
#include "apu.h"
#include "gb_apu/GBAPU_Wrapper.h"
mmu_type *mmu = NULL;
char save_fname[50];
void init_mmu() {
unsigned short addr = 0;
for (addr = 0; addr < 0x8000; addr++) {
wb(addr, 0);
}
}
void print_n_bytes(unsigned short n) {
unsigned short i = 0;
while (i < n) {
printf("%x\n", rb(i++));
}
}
void save_sram() {
printf("saving to %s\n", save_fname);
FILE *f = fopen(save_fname, "w+b");
fwrite(mmu->eram, sizeof(unsigned char), sizeof(mmu->eram), f);
fclose(f);
}
void load_bios(char* fname) {
FILE *f = fopen(fname, "rb");
fread(mmu->bios, sizeof(unsigned char), 0x100, f);
fclose(f);
}
void load_rom(char* fname) {
char fname_copy[200];
strcpy(fname_copy, fname);
char *base_name = strtok(fname_copy, ".");
if (base_name != NULL) {
snprintf(save_fname, 50, "%s.sav", base_name);
}
unsigned char cart_type;
FILE *f = fopen(fname, "rb");
unsigned long rom_size;
fseek(f, 0, SEEK_END);
rom_size = (unsigned long)ftell(f);
mmu = (mmu_type *)malloc(sizeof(mmu_type) + rom_size);
fseek(f, 0x147, SEEK_SET);
switch (cart_type = fgetc(f))
{
case 0x00: case 0x01: case 0x02: case 0x03:
case 0x08: case 0x09:
mmu->mbc = 1;
break;
case 0x05: case 0x06:
mmu->mbc = 2;
break;
case 0x0F: case 0x10: case 0x11: case 0x12: case 0x13:
mmu->mbc = 3;
break;
default:
mmu->mbc = 1;
}
printf("using mbc%d\n", mmu->mbc);
rewind(f);
mmu->mode = 0;
if (mmu == NULL) {
fprintf(stderr, "Error using malloc for mmu.");
fclose(f);
exit(1);
}
fseek(f, 0, SEEK_SET);
fread(mmu->rom, sizeof(unsigned char), rom_size, f);
fclose(f);
struct stat buffer;
if (stat(save_fname, &buffer) == 0) {
printf("reading save file %s ...\n", save_fname);
f = fopen(save_fname, "rb");
size_t result = fread(mmu->eram, sizeof(unsigned char), sizeof(mmu->eram), f);
printf("read %lu bytes\n", result);
fclose(f);
} else {
printf("save file %s does not exist\n", save_fname);
memset(mmu->eram, 0, 0x20000);
}
mmu->rom_bank = 1;
mmu->ram_bank = 0;
mmu->eram_enable = false;
mmu->inbios = true;
memset(mmu->vram, 0, 0x2000);
memset(mmu->wram, 0, 0x2000);
memset(mmu->zram, 0, 0x80);
memset(mmu->io, 0, 0x80);
// Need to handle RTC save from file
}
unsigned char rb(unsigned short addr) {
switch (addr & 0xF000) {
/* BIOS (256b) or ROM0 */
case 0x0000:
if (mmu->inbios)
if (addr < 0x0100)
return mmu->bios[addr];
return mmu->rom[addr];
case 0x1000:
case 0x2000:
case 0x3000:
return mmu->rom[addr];
/* ROM1 (banked) (16k) */
case 0x4000:
case 0x5000:
case 0x6000:
case 0x7000:
return mmu->rom[mmu->rom_bank * 0x4000 + (addr & 0x3FFF)];
/* Graphics: VRAM (8k) */
case 0x8000:
case 0x9000:
if (gpu.mode == 3) return 0xFF;
return gpu.vram[addr & 0x1FFF];
/* External RAM (8k) */
case 0xA000:
case 0xB000:
if (mmu->eram_enable) {
if (mmu->mbc == 3) {
switch (mmu->ram_bank)
{
case 0x8:
return mmu->rtc.seconds;
case 0x9:
return mmu->rtc.minutes;
case 0xA:
return mmu->rtc.hours;
case 0xB:
return mmu->rtc.day_counter & 0xFF;
case 0xC:
return (mmu->rtc.halt_flag << 6) |
(mmu->rtc.day_counter_carry << 7) |
GET_BIT(mmu->rtc.day_counter, 8);
}
}
return mmu->eram[mmu->ram_bank * 0x2000 + (addr & 0x1FFF)];
}
return 0xFF;
/* Working RAM (8k) */
case 0xC000:
case 0xD000:
return mmu->wram[(addr & 0x1FFF)];
/* Working RAM shadow (8k) */
case 0xE000:
return mmu->wram[addr & 0x1FFF];
/* Working RAM shadow, I/O, zero-page RAM */
case 0xF000:
switch (addr & 0x0F00)
{
/* Working RAM shadow */
case 0x000: case 0x100: case 0x200: case 0x300:
case 0x400: case 0x500: case 0x600: case 0x700:
case 0x800: case 0x900: case 0xA00: case 0xB00:
case 0xC00: case 0xD00:
return mmu->wram[addr & 0x1FFF];
case 0xE00:
if (addr < 0xFEA0) {
if (gpu.mode & 0x2) return 0xFF;
return gpu.oam[addr & 0xFF];
} else
return 0;
case 0xF00:
if (addr >= 0xFF80) {
return mmu->zram[addr & 0x7F];
} else if (addr >= Gb_Apu_start_addr &&
addr <= Gb_Apu_end_addr) {
return Gb_Apu_read_register(apu,
z80.clock.long_time,
addr);
} else {
switch (addr) {
case 0xFF00:
return key.rows[key.column];
case 0xFF04:
return z80_p->clock.div;
case 0xFF05:
return z80_p->clock.tima;
case 0xFF06:
return z80_p->clock.tma;
case 0xFF07:
return z80_p->clock.tac;
case 0xFF0F:
return z80_p->int_f;
case 0xFF40:
return gpu.gpu_ctrl;
case 0xFF41:
return gpu.gpu_stat;
case 0xFF42:
return gpu.scrollY;
case 0xFF43:
return gpu.scrollX;
case 0xFF44:
return gpu.line;
case 0xFF45:
return gpu.lineYC;
case 0xFF46:
return gpu.DMA;
case 0xFF47:
return gpu.bg_pal;
case 0xFF48:
return gpu.ob_pal0;
case 0xFF49:
return gpu.ob_pal1;
case 0xFF4A:
return gpu.wdow_y;
case 0xFF4B:
return gpu.wdow_x;
case 0xFFFF:
return z80_p->int_en;
default:
return mmu->io[addr & 0x7F];
/* TODO: handle I/O */
}
}
}
}
return 0;
}
unsigned short rw(unsigned short addr) {
return (unsigned short)(rb(addr) | (rb(addr + 1) << 8));
}
void ww(unsigned short addr, unsigned short val) {
wb(addr, val & 0xFF);
wb(addr + 1, val >> 8);
}
// TODO: MBC when writing to ROM
void wb(unsigned short addr, unsigned char val) {
unsigned short threshold, old_threshold;
switch (addr & 0xF000) {
/* BIOS (256b) or ROM0 */
case 0x0000:
case 0x1000:
if (mmu->mbc & 1) {
mmu->eram_enable = ((val & 0x0F) == 0x0A);
if (!mmu->eram_enable) {
save_sram();
}
} else if ((mmu->mbc == 2) && ((addr & 0x100) == 0)) {
mmu->eram_enable = ((val & 0x0F) == 0x0A);
if (!mmu->eram_enable) {
save_sram();
}
}
break;
case 0x2000:
case 0x3000:
if (mmu->mbc == 1) {
mmu->rom_bank = (mmu->rom_bank & ~0x1F) | ((val ? val : val + 1) & 0x1F);
} else if (mmu->mbc == 3) {
mmu->rom_bank = (val ? val : val + 1) & 0x7F;
} else if (mmu->mbc == 2 && (addr & 0x100)) {
mmu->rom_bank = val & 0x0F;
}
break;
/* ROM1 (unbanked) (16k) */
case 0x4000:
case 0x5000:
if (mmu->mbc == 1) {
if (mmu->mode) {
mmu->ram_bank = val & 0x3;
} else {
mmu->rom_bank = (unsigned char)((mmu->rom_bank & 0x1F) | ((val & 0x3) << 5));
}
} else if (mmu->mbc == 3) {
mmu->ram_bank = val & 0x7;
}
break;
case 0x6000:
case 0x7000:
if (mmu->mbc == 1) {
mmu->mode = val & 1; // MBC 1
if (mmu->mode) {
mmu->rom_bank &= 0x1F;
} else {
mmu->ram_bank = 0;
}
} else if (mmu->mbc == 3) {
if ((mmu->rtc.last_latch_write == 0) && (val == 1)) {
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
mmu->rtc.seconds = timeinfo->tm_sec;
mmu->rtc.minutes = timeinfo->tm_min;
mmu->rtc.hours = timeinfo->tm_hour;
mmu->rtc.day_counter = timeinfo->tm_yday;
printf("latched RTC\n");
}
mmu->rtc.last_latch_write = val;
}
break;
/* Graphics: VRAM (8k) */
case 0x8000:
case 0x9000:
if (gpu.mode == 3) return;
gpu.vram[addr & 0x1FFF] = val;
if (addr <= 0x97FF) {
unsigned short tmp_addr = addr & 0x1FFE;
unsigned short tile = (tmp_addr >> 4) & 511;
unsigned char y = (tmp_addr >> 1) & 7;
unsigned char sx, x;
for (x = 0; x < 8; x++) {
sx = (unsigned char)(1 << (7 - x));
gpu.tileset[tile][y][x] =
((gpu.vram[tmp_addr] & sx) ? 1 : 0) +
((gpu.vram[tmp_addr+1] & sx) ? 2 : 0);
}
}
break;
/* External RAM (8k) */
case 0xA000:
case 0xB000:
if (mmu->mbc == 3) {
switch (mmu->ram_bank)
{
case 0x8:
mmu->rtc.seconds = val;
return;
case 0x9:
mmu->rtc.minutes = val;
return;
case 0xA:
mmu->rtc.hours = val;
return;
case 0xB:
mmu->rtc.day_counter = (mmu->rtc.day_counter & 0x100) | val;
return;
case 0xC:
mmu->rtc.day_counter = (mmu->rtc.day_counter & 0xFF) | ((val & 1) << 8);
mmu->rtc.halt_flag = GET_BIT(val, 6);
mmu->rtc.day_counter_carry = GET_BIT(val, 7);
return;
}
}
mmu->eram[mmu->ram_bank * 0x2000 + (addr & 0x1FFF)] = val;
break;
/* Working RAM (8k) */
case 0xC000:
case 0xD000:
mmu->wram[addr & 0x1FFF] = val;
break;
/* Working RAM shadow (8k) */
case 0xE000:
mmu->wram[addr & 0x1FFF] = val;
break;
/* Working RAM shadow, I/O, zero-page RAM */
case 0xF000:
switch (addr & 0x0F00)
{
/* Working RAM shadow */
case 0x000: case 0x100: case 0x200: case 0x300:
case 0x400: case 0x500: case 0x600: case 0x700:
case 0x800: case 0x900: case 0xA00: case 0xB00:
case 0xC00: case 0xD00:
mmu->wram[addr & 0x1FFF] = val;
break;
case 0xE00:
if (addr < 0xFEA0) {
if (gpu.mode & 0x2) return;
gpu.oam[addr & 0xFF] = val;
}
break;
case 0xF00:
if (addr >= 0xFF80) {
mmu->zram[addr & 0x7F] = val;
} else {
mmu->io[addr & 0x7F] = val;
}
if (addr >= Gb_Apu_start_addr &&
addr <= Gb_Apu_end_addr) {
Gb_Apu_write_register(apu,
z80.clock.long_time,
addr, val);
}
switch (addr) {
case 0xFF00:
key.column = ((val & 0x30) >> 4);
break;
case 0xFF02:
if (val == 0x81) {
printf("%c", mmu->io[1]);
fflush(stdout);
}
break;
case 0xFF04:
threshold = 3 + (2 * (((z80_p->clock.tac & 3) - 1) & 3));
printf("writing to DIV\n");
printf("enter div register: 0x%04X\n", z80_p->clock.m);
printf("t time: %d\n", z80_p->t);
printf("threshold: 0x%04X\n", 1 << threshold);
if ((z80_p->clock.tac & 4) && (((z80_p->clock.m /*+ z80_p->t */) >> threshold) & 1)) {
printf("div write tima inc\n");
z80_p->clock.tima++;
if (!z80_p->clock.tima) {
z80_p->clock.tima = z80_p->clock.tma;
z80_p->int_f |= 4;
}
}
z80_p->clock.m = 0;
break;
case 0xFF05:
z80_p->clock.tima = val;
break;
case 0xFF06:
z80_p->clock.tma = val;
break;
case 0xFF07:
z80_p->clock.old_tac = z80_p->clock.tac;
z80_p->clock.tac = val;
old_threshold = 3 + (2 * (((z80_p->clock.old_tac & 3) - 1) & 3));
threshold = 3 + (2 * (((z80_p->clock.tac & 3) - 1) & 3));
if ((z80_p->clock.tac & z80_p->clock.old_tac & 4) &&
(((z80_p->clock.m + z80_p->t - 4) >> old_threshold) & 1) &&
!(((z80_p->clock.m + z80_p->t - 4) >> threshold) & 1)) {
printf("tima inc on tac write 1\n");
z80_p->clock.tima++;
if (!z80_p->clock.tima) {
z80_p->clock.tima = z80_p->clock.tma;
z80_p->int_f |= 4;
}
}
if ((~z80_p->clock.tac & z80_p->clock.old_tac & 4) &&
(((z80_p->clock.m + z80_p->t - 4) >> old_threshold) & 1)) {
printf("tima inc on tac write 2\n");
z80_p->clock.tima++;
if (!z80_p->clock.tima) {
z80_p->clock.tima = z80_p->clock.tma;
z80_p->int_f |= 4;
}
}
// check to see if this results in falling edge, inc TIMA
break;
case 0xFF0F:
z80_p->int_f = val;
break;
case 0xFF40:
gpu.gpu_ctrl = val;
break;
case 0xFF41:
gpu.gpu_stat = val;
break;
case 0xFF42:
gpu.scrollY = val;
break;
case 0xFF43:
gpu.scrollX = val;
break;
case 0xFF44:
gpu.line = val;
break;
case 0xFF45:
gpu.lineYC = val;
break;
case 0xFF46:
gpu.DMA = val;
gpu.do_DMA = true;
break;
case 0xFF47:
gpu.bg_pal = val;
break;
case 0xFF48:
gpu.ob_pal0 = val;
break;
case 0xFF49:
gpu.ob_pal1 = val;
break;
case 0xFF4A:
gpu.wdow_y = val;
break;
case 0xFF4B:
gpu.wdow_x = val;
break;
case 0xFF50:
mmu->inbios = false;
break;
case 0xFFFF:
z80_p->int_en = val;
break;
default:
break;
}
}
}
}