-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgb.c
274 lines (244 loc) · 7.02 KB
/
gb.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
#include <stdint.h>
#include <stdio.h>
#include "gb.h"
#include "ff.h"
#define __not_in_flash_func(func_name) func_name
#include "peanut_gb.h"
#define MAX_SRAM_SIZE (0x2000 *4) // Max 32KB SRAM
struct priv_t
{
/* Pointer to allocated memory holding GB file. */
uint8_t *rom;
/* Pointer to allocated memory holding save file. */
uint8_t *cart_ram;
/* Frame buffer */
};
static struct priv_t priv;
enum gb_init_error_e ret;
static struct gb_s gb;
uint8_t *GBaddress; // pointer to the GB ROM file
#if ENABLE_SOUND
#define AUDIO_BUFFER_SIZE (AUDIO_SAMPLES * sizeof(u_int32_t))
uint16_t *audio_stream;
#endif
char *GetfileNameFromFullPath(char *fullPath)
{
char *fileName = fullPath;
char *ptr = fullPath;
while (*ptr)
{
if (*ptr == '/')
{
fileName = ptr + 1;
}
ptr++;
}
return fileName;
}
void stripextensionfromfilename(char *filename)
{
char *ptr = filename;
char *lastdot = filename;
while (*ptr)
{
if (*ptr == '.')
{
lastdot = ptr;
}
ptr++;
}
*lastdot = 0;
}
/**
* Returns a byte from the ROM file at the given address. Not used. Emulator reads directly from GBaddress.
*/
//uint8_t *address = (uint8_t *)GB_FILE_ADDR;
uint8_t __not_in_flash_func(gb_rom_read)(struct gb_s *gb, const uint_fast32_t addr)
{
// const struct priv_t * const p = gb->direct.priv;
// const struct priv_t *const p = static_cast<const struct priv_t *>(gb->direct.priv);
return GBaddress[addr];
}
/**
* Returns a byte from the cartridge RAM at the given address.
*/
uint8_t __not_in_flash_func(gb_cart_ram_read)(struct gb_s *gb, const uint_fast32_t addr)
{
// const struct priv_t * const p = gb->direct.priv;
const struct priv_t *const p = (const struct priv_t *)(gb->direct.priv);
return p->cart_ram[addr];
}
/**
* Writes a given byte to the cartridge RAM at the given address.
*/
void __not_in_flash_func(gb_cart_ram_write)(struct gb_s *gb, const uint_fast32_t addr,
const uint8_t val)
{
// const struct priv_t * const p = gb->direct.priv;
const struct priv_t *const p = (const struct priv_t *)(gb->direct.priv);
p->cart_ram[addr] = val;
}
/**
* Returns a pointer to the allocated space containing the ROM. Must be freed.
*/
uint8_t *read_rom_to_ram(const char *file_name)
{
uint8_t *rom = NULL;
#if 0
FILE *rom_file = fopen(file_name, "rb");
size_t rom_size;
if(rom_file == NULL)
return NULL;
fseek(rom_file, 0, SEEK_END);
rom_size = ftell(rom_file);
rewind(rom_file);
rom = malloc(rom_size);
if(fread(rom, sizeof(uint8_t), rom_size, rom_file) != rom_size)
{
free(rom);
fclose(rom_file);
return NULL;
}
fclose(rom_file);
return rom;
#endif
return rom;
}
/**
* Ignore all errors.
*/
void gb_error(struct gb_s *gb, const enum gb_error_e gb_err, const uint16_t val)
{
const char *gb_err_str[GB_INVALID_MAX] = {
"UNKNOWN",
"INVALID OPCODE",
"INVALID READ",
"INVALID WRITE",
"HALT FOREVER"};
// struct priv_t *priv = gb->direct.priv;
const struct priv_t *const priv = (const struct priv_t *)(gb->direct.priv);
printf("Error %d occurred: %s at %04X\n. Exiting.\n",
gb_err, gb_err_str[gb_err], val);
/* Free memory and then exit. */
free(priv->cart_ram);
free(priv->rom);
exit(EXIT_FAILURE);
}
// read cart ram from file
void loadsram(char *romname, const char *savedir)
{
char pad[FF_MAX_LFN];
int bytesread;
char fileName[FF_MAX_LFN];
FILINFO fno;
strcpy(fileName, GetfileNameFromFullPath(romname));
stripextensionfromfilename(fileName);
snprintf(pad, FF_MAX_LFN, "%s/%s.SAV", savedir, fileName);
printf("Loading SRAM from %s\n", pad);
if (f_stat(pad, &fno) == FR_OK)
{
FIL file;
if (f_open(&file, pad, FA_READ) == FR_OK)
{
if (f_read(&file, priv.cart_ram, fno.fsize, &bytesread) != FR_OK)
{
printf("Error reading SRAM\n");
}
f_close(&file);
}
}
else
{
printf("No SRAM file found\n");
}
}
// save SRAM to file
void savesram(char *romname, const char *savedir) {
char pad[FF_MAX_LFN];
char fileName[FF_MAX_LFN];
strcpy(fileName, GetfileNameFromFullPath(romname));
stripextensionfromfilename(fileName);
snprintf(pad, FF_MAX_LFN, "%s/%s.SAV", savedir, fileName);
printf("Saving SRAM to %s\n", pad);
FIL file;
if (f_open(&file, pad, FA_WRITE | FA_CREATE_ALWAYS) == FR_OK)
{
UINT byteswritten;
if (f_write(&file, priv.cart_ram, gb_get_save_size(&gb), &byteswritten) != FR_OK)
{
printf("Error writing SRAM\n");
}
f_close(&file);
}
}
int startemulation(uint8_t *rom, char *romname, const char *savedir, char *ErrorMessage)
{
ErrorMessage[0] = 0;
priv.rom = GBaddress = rom;
ret = gb_init(&gb, &gb_rom_read, &gb_cart_ram_read,
&gb_cart_ram_write, &gb_error, &priv);
if (ret != GB_INIT_NO_ERROR)
{
snprintf(ErrorMessage, 40, "Cannot init emulator %d", ret);
printf("%s\n", ErrorMessage);
return 0;
}
printf("Emulator initialized, rom name.\n");
#if ENABLE_SOUND
printf("Starting audio\n");
printf("Number of %d-byte samples per frame: %d\n", sizeof(u_int32_t), AUDIO_SAMPLES);
printf("Allocating %d bytes for sample buffer (%d * %d).\n", AUDIO_BUFFER_SIZE, AUDIO_SAMPLES, sizeof(u_int32_t));
printf("Audio Samples per frame: %d\n", AUDIO_SAMPLES);
// Audiobuffer is a 32 bit array of AUDIO_SAMPLES
audio_stream = (uint16_t *)malloc(AUDIO_BUFFER_SIZE);
audio_init();
#endif
uint32_t save_size = gb_get_save_size(&gb);
printf("Allocating %d bytes for cart ram.\n", save_size);
priv.cart_ram = NULL;
if (save_size > 0 && save_size <= MAX_SRAM_SIZE)
{
priv.cart_ram = (uint8_t *)malloc(save_size);
memset(priv.cart_ram, 0, save_size);
if (priv.cart_ram == NULL)
{
strcpy(ErrorMessage, "Cannot allocate memory for save file");
printf("%s\n", ErrorMessage);
return 0;
} else {
loadsram(romname, savedir);
}
}
if (save_size > MAX_SRAM_SIZE)
{
strcpy(ErrorMessage, "Save size too large, max 32KB");
printf("%s\n", ErrorMessage);
return 0;
}
return 1;
}
void emu_init_lcd(void (*lcd_draw_line)(const uint_fast8_t line)) {
gb_init_lcd(&gb, lcd_draw_line);
gb.direct.interlace = false;
gb.direct.frame_skip = false;
}
void emu_run_frame()
{
gb_run_frame(&gb);
#if ENABLE_SOUND
// send audio buffer to playback device
audio_callback(NULL, (uint8_t *)audio_stream, AUDIO_BUFFER_SIZE);
#endif
}
void emu_set_gamepad(uint8_t joypad) {
gb.direct.joypad = joypad;
}
void stopemulation(char *romname, const char *savedir) {
if ( priv.cart_ram != NULL ) {
savesram(romname, savedir);
free(priv.cart_ram);
}
if (audio_stream != NULL) {
free(audio_stream);
}
}