Skip to content

Commit a5008f3

Browse files
committed
GBC: Added BIOS support
1 parent 868c191 commit a5008f3

File tree

7 files changed

+64
-5
lines changed

7 files changed

+64
-5
lines changed

gnuboy-go/components/gnuboy/cpu.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,12 @@ void cpu_reset(bool hard)
280280
IME = 0;
281281
IMA = 0;
282282

283-
PC = 0x0100;
283+
PC = hw.bios ? 0x0000 : 0x0100;
284284
SP = 0xFFFE;
285-
AF = 0x01B0;
285+
AF = hw.cgb ? 0x11B0 : 0x01B0;
286286
BC = 0x0013;
287287
DE = 0x00D8;
288288
HL = 0x014D;
289-
290-
if (hw.cgb) A = 0x11;
291289
}
292290

293291
/* cnt - time to emulate, expressed in real clock cycles */

gnuboy-go/components/gnuboy/hw.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ typedef struct
2828
un32 cgb;
2929
n32 hdma;
3030
n32 serial;
31+
un8 *bios;
3132
} hw_t;
3233

3334

gnuboy-go/components/gnuboy/loader.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ int rom_loadbank(int bank)
170170

171171
int rom_load(const char *file)
172172
{
173-
MESSAGE_INFO("Loading file: '%s'\n", file);
173+
MESSAGE_INFO("Loading file: '%s'\n", file);
174174

175175
fpRomFile = fopen(file, "rb");
176176
if (fpRomFile == NULL)
@@ -565,6 +565,9 @@ int state_load(const char *file)
565565
fclose(fp);
566566
free(buf);
567567

568+
// Disable BIOS. This is a hack to support old saves
569+
R_BIOS = 0x1;
570+
568571
pal_dirty();
569572
sound_dirty();
570573
mem_updatemap();
@@ -577,3 +580,29 @@ int state_load(const char *file)
577580

578581
return -1;
579582
}
583+
584+
585+
int bios_load(const char *file)
586+
{
587+
MESSAGE_INFO("Loading BIOS file: '%s'\n", file);
588+
589+
FILE *fpBiosFile = fopen(file, "rb");
590+
if (fpBiosFile == NULL)
591+
{
592+
MESSAGE_ERROR("BIOS fopen failed");
593+
return -1;
594+
}
595+
596+
hw.bios = malloc(0x900);
597+
if (!hw.bios)
598+
emu_die("Out of memory");
599+
600+
if (fread(hw.bios, 1, 0x900, fpBiosFile) >= 0x100)
601+
{
602+
MESSAGE_INFO("BIOS loaded\n");
603+
}
604+
605+
fclose(fpBiosFile);
606+
607+
return 0;
608+
}

gnuboy-go/components/gnuboy/loader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
int rom_loadbank(int);
55
int rom_load(const char *file);
66
void rom_unload(void);
7+
int bios_load(const char *file);
8+
void bios_unload(void);
79

810
int sram_load(const char *file);
911
int sram_save(const char *file);

gnuboy-go/components/gnuboy/mem.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ void IRAM_ATTR mem_updatemap()
4444
mbc.rmap[0x2] = rom.bank[0];
4545
mbc.rmap[0x3] = rom.bank[0];
4646

47+
// Force bios to go through mem_read (speed doesn't really matter here)
48+
if (hw.bios && (R_BIOS & 1) == 0)
49+
{
50+
mbc.rmap[0x0] = NULL;
51+
}
52+
4753
if (mbc.rombank < mbc.romsize)
4854
{
4955
mbc.rmap[0x4] = rom.bank[mbc.rombank] - 0x4000;
@@ -198,6 +204,10 @@ static inline void ioreg_write(byte r, byte b)
198204
case RI_KEY1:
199205
REG(r) = (REG(r) & 0x80) | (b & 0x01);
200206
break;
207+
case RI_BIOS:
208+
REG(r) = b;
209+
mem_updatemap();
210+
break;
201211
case RI_HDMA1:
202212
case RI_HDMA2:
203213
case RI_HDMA3:
@@ -250,6 +260,7 @@ static inline byte ioreg_read(byte r)
250260
case RI_OCPD:
251261
case RI_SVBK:
252262
case RI_KEY1:
263+
case RI_BIOS:
253264
case RI_HDMA1:
254265
case RI_HDMA2:
255266
case RI_HDMA3:
@@ -514,6 +525,14 @@ byte IRAM_ATTR mem_read(addr_t a)
514525
switch (ha)
515526
{
516527
case 0x0:
528+
if (a < 0x900 && (R_BIOS & 1) == 0)
529+
{
530+
if (a < 0x100)
531+
return hw.bios[a];
532+
if (hw.cgb && a >= 0x200)
533+
return hw.bios[a];
534+
}
535+
// fall through
517536
case 0x2:
518537
return rom.bank[0][a & 0x3fff];
519538

gnuboy-go/components/gnuboy/regs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545

4646
#define RI_VBK 0x4F
4747

48+
#define RI_BIOS 0x50
49+
4850
#define RI_HDMA1 0x51
4951
#define RI_HDMA2 0x52
5052
#define RI_HDMA3 0x53
@@ -128,6 +130,8 @@
128130
#define R_WY REG(RI_WY)
129131
#define R_WX REG(RI_WX)
130132

133+
#define R_BIOS REG(RI_BIOS)
134+
131135
#define R_VBK REG(RI_VBK)
132136

133137
#define R_HDMA1 REG(RI_HDMA1)

gnuboy-go/main/main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,12 @@ void app_main(void)
254254
// Load ROM
255255
rom_load(app->romPath);
256256

257+
// Load BIOS
258+
if (hw.cgb)
259+
bios_load(RG_BASE_PATH "/bios/gbc_bios.bin");
260+
else
261+
bios_load(RG_BASE_PATH "/bios/gb_bios.bin");
262+
257263
// Set palette for non-gbc games (must be after rom_load)
258264
pal_set_dmg(rg_settings_get_app_int32(SETTING_PALETTE, 0));
259265

0 commit comments

Comments
 (0)