forked from lualiliu/esp32-gameboy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem.cpp
223 lines (197 loc) · 4.68 KB
/
mem.cpp
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
// If INTER_MODULE_OPT macro is defined,
// this file is included into cpu.cpp
// to make inter module optimizations possible
#ifndef INTER_MODULE_OPT
#include "mem.h"
#include <stdlib.h>
#include <string.h>
#include "cpu.h"
#include "interrupt.h"
#include "lcd.h"
#include "mbc.h"
#include "rom.h"
#include "sdl.h"
#include "timer.h"
static unsigned char *mem;
static int DMA_pending = 0;
static int joypad_select_buttons, joypad_select_directions;
static uint32_t bank_switches = 0;
uint32_t mem_get_bank_switches() { return bank_switches; }
void mem_bank_switch(unsigned int n) {
const unsigned char *b = rom_getbytes();
bank_switches++;
memcpy(&mem[0x4000], &b[n * 0x4000], 0x4000);
}
/* LCD's access to VRAM */
const unsigned char *mem_get_raw() { return mem; }
unsigned char mem_get_byte(unsigned short i) {
unsigned long elapsed;
unsigned char mask = 0;
if (DMA_pending && i < 0xFF80) {
elapsed = cpu_get_cycles() - DMA_pending;
if (elapsed >= 160)
DMA_pending = 0;
else {
return mem[0xFE00 + elapsed];
}
}
if (i < 0xFF00) return mem[i];
switch (i) {
case 0xFF00: /* Joypad */
if (!joypad_select_buttons) mask = sdl_get_buttons();
if (!joypad_select_directions) mask = sdl_get_directions();
return 0xC0 | (0xF ^ mask) |
(joypad_select_buttons | joypad_select_directions);
break;
case 0xFF04:
return timer_get_div();
break;
case 0xFF05:
return timer_get_counter();
break;
case 0xFF06:
return timer_get_modulo();
break;
case 0xFF07:
return timer_get_tac();
break;
case 0xFF0F:
return interrupt_get_IF();
break;
case 0xFF41:
return lcd_get_stat();
break;
case 0xFF44:
return lcd_get_line();
break;
case 0xFF4D: /* GBC speed switch */
return 0xFF;
break;
case 0xFFFF:
return interrupt_get_mask();
break;
}
return mem[i];
}
unsigned short mem_get_word(unsigned short i) {
unsigned long elapsed;
if (DMA_pending && i < 0xFF80) {
elapsed = cpu_get_cycles() - DMA_pending;
if (elapsed >= 160)
DMA_pending = 0;
else {
return mem[0xFE00 + elapsed];
}
}
return mem[i] | (mem[i + 1] << 8);
}
void mem_write_byte(unsigned short d, unsigned char i) {
unsigned int filtered = 0;
switch (rom_get_mapper()) {
case NROM:
if (d < 0x8000) filtered = 1;
break;
case MBC2:
case MBC3:
filtered = MBC3_write_byte(d, i);
break;
case MBC1:
filtered = MBC1_write_byte(d, i);
break;
}
if (filtered) return;
switch (d) {
case 0xFF00: /* Joypad */
joypad_select_buttons = i & 0x20;
joypad_select_directions = i & 0x10;
break;
case 0xFF01: /* Link port data */
// fprintf(stderr, "%c", i);
break;
case 0xFF04:
timer_set_div(i);
break;
case 0xFF05:
timer_set_counter(i);
break;
case 0xFF06:
timer_set_modulo(i);
break;
case 0xFF07:
timer_set_tac(i);
break;
case 0xFF0F:
interrupt_set_IF(i);
break;
case 0xFF40:
lcd_write_control(i);
break;
case 0xFF41:
lcd_write_stat(i);
break;
case 0xFF42:
lcd_write_scroll_y(i);
break;
case 0xFF43:
lcd_write_scroll_x(i);
break;
case 0xFF45:
lcd_set_ly_compare(i);
break;
case 0xFF46: /* OAM DMA */
/* Copy bytes from i*0x100 to OAM */
memcpy(&mem[0xFE00], &mem[i * 0x100], 0xA0);
DMA_pending = cpu_get_cycles();
break;
case 0xFF47:
lcd_write_bg_palette(i);
break;
case 0xFF48:
lcd_write_spr_palette1(i);
break;
case 0xFF49:
lcd_write_spr_palette2(i);
break;
case 0xFF4A:
lcd_set_window_y(i);
break;
case 0xFF4B:
lcd_set_window_x(i);
break;
case 0xFFFF:
interrupt_set_mask(i);
return;
break;
}
mem[d] = i;
}
void mem_write_word(unsigned short d, unsigned short i) {
mem[d] = i & 0xFF;
mem[d + 1] = i >> 8;
}
void gameboy_mem_init(void) {
const unsigned char *bytes = rom_getbytes();
mem = (unsigned char *)calloc(1, 0x10000);
memcpy(&mem[0x0000], &bytes[0x0000], 0x4000);
memcpy(&mem[0x4000], &bytes[0x4000], 0x4000);
mem[0xFF10] = 0x80;
mem[0xFF11] = 0xBF;
mem[0xFF12] = 0xF3;
mem[0xFF14] = 0xBF;
mem[0xFF16] = 0x3F;
mem[0xFF19] = 0xBF;
mem[0xFF1A] = 0x7F;
mem[0xFF1B] = 0xFF;
mem[0xFF1C] = 0x9F;
mem[0xFF1E] = 0xBF;
mem[0xFF20] = 0xFF;
mem[0xFF23] = 0xBF;
mem[0xFF24] = 0x77;
mem[0xFF25] = 0xF3;
mem[0xFF26] = 0xF1;
mem[0xFF40] = 0x91;
mem[0xFF47] = 0xFC;
mem[0xFF48] = 0xFF;
mem[0xFF49] = 0xFF;
}
#endif // INTER_MODULE_OPT