Skip to content

Commit 0c467b5

Browse files
committed
tmp
1 parent c7eec97 commit 0c467b5

File tree

10 files changed

+288
-33
lines changed

10 files changed

+288
-33
lines changed

.clang-tidy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Checks: >
1616
portability-*,
1717
readability-*,
1818
-readability-braces-around-statements,
19+
-readability-function-cognitive-complexity,
1920
-readability-identifier-length,
2021
-readability-implicit-bool-conversion,
2122
-readability-magic-numbers

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ set(gemu_sources
2727
src/instructions.c
2828
src/log.c
2929
src/macros.c
30+
src/mapper.c
3031
src/num.c
3132
src/sdl.c)
3233

src/data.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "data.h"
2+
#include "macros.h"
23

34
bool CartridgeType_has_ram(const CartridgeType self)
45
{
@@ -18,3 +19,29 @@ bool CartridgeType_has_ram(const CartridgeType self)
1819
self == CartridgeType_Mbc7SensorRumbleRamBattery ||
1920
self == CartridgeType_Huc1RamBattery;
2021
}
22+
23+
size_t rom_banks_from_size_code(const u8 rom_size_code)
24+
{
25+
BAIL_IF(rom_size_code > 0x08, "invalid ROM size code: $%02X",
26+
rom_size_code);
27+
28+
return 1 << (rom_size_code + 1);
29+
}
30+
31+
size_t ram_banks_from_size_code(const u8 ram_size_code)
32+
{
33+
switch (ram_size_code) {
34+
case 0x00:
35+
return 0;
36+
case 0x02:
37+
return 1;
38+
case 0x03:
39+
return 4;
40+
case 0x04:
41+
return 16;
42+
case 0x05:
43+
return 8;
44+
default:
45+
BAIL("invalid RAM size code: $%02X", ram_size_code);
46+
}
47+
}

src/data.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ typedef enum : u8 {
5252
CartridgeType_Huc1RamBattery = 0xFF,
5353
} CartridgeType;
5454

55-
void CartridgeType_log_info(CartridgeType self);
56-
5755
bool CartridgeType_has_ram(CartridgeType self);
5856

57+
size_t rom_banks_from_size_code(u8 rom_size_code);
58+
59+
size_t ram_banks_from_size_code(u8 ram_size_code);
60+
5961
#endif

src/frontend.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,8 @@ static void update_texture(const State *const state)
394394

395395
const SDL_PixelFormatDetails *const pixel_format =
396396
SDL_GetPixelFormatDetails(surface->format);
397-
BAIL_IF(pixel_format == nullptr, "Could not get pixel format: %s",
398-
SDL_GetError());
397+
BAIL_IF_NULL(pixel_format, "Could not get pixel format: %s",
398+
SDL_GetError());
399399

400400
SDL_FillSurfaceRect(surface, nullptr,
401401
SDL_MapRGB(pixel_format, nullptr, 0, 0, 0));

src/game_boy.c

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "data.h"
44
#include "log.h"
55
#include "macros.h"
6+
#include "mapper.h"
67
#include "num.h"
78
#include "stdinc.h"
89
#include "string.h"
@@ -82,16 +83,6 @@ static void GameBoy_reset(GameBoy *const self)
8283

8384
static void GameBoy_validate_rom(const GameBoy *const self)
8485
{
85-
BAIL_IF(self->rom[RomHeader_CartridgeType] != 0x00,
86-
"Unsupported cartridge type (ctype: $%02X)",
87-
self->rom[RomHeader_CartridgeType]);
88-
89-
BAIL_IF(
90-
!CartridgeType_has_ram(self->rom[RomHeader_CartridgeType]) &&
91-
self->rom[RomHeader_RamSize] != 0,
92-
"Cartridge type does not have RAM, but header indicates otherwise (ctype: $%02, RAM size: $%02X)",
93-
self->rom[RomHeader_CartridgeType], self->rom[RomHeader_RamSize]);
94-
9586
BAIL_IF(
9687
self->rom_len != 0x8000 * ((size_t)1 << self->rom[RomHeader_RomSize]),
9788
"Actual ROM size does not match header-specified size. (specified: $%02X, was: $%02X)",
@@ -137,9 +128,11 @@ GameBoy GameBoy_new(const u8 *const boot_rom)
137128
void GameBoy_destroy(GameBoy *const self)
138129
{
139130
free(self->rom);
140-
141131
self->rom = nullptr;
142132
self->rom_len = 0;
133+
134+
Mapper_destroy(self->mapper);
135+
self->mapper = nullptr;
143136
}
144137

145138
void GameBoy_log_cartridge_info(const GameBoy *const self)
@@ -166,19 +159,22 @@ void GameBoy_load_rom(GameBoy *const self, const u8 *const rom,
166159
free(self->rom);
167160

168161
self->rom = malloc(rom_len * sizeof(self->rom[0]));
169-
BAIL_IF(self->rom == nullptr, "Could not allocate memory for new ROM");
162+
BAIL_IF_NULL(self->rom);
170163

171164
memcpy(self->rom, rom, rom_len * sizeof(self->rom[0]));
172165
self->rom_len = rom_len;
173166

174167
GameBoy_validate_rom(self);
168+
169+
Mapper_destroy(self->mapper);
170+
self->mapper = Mapper_from_rom(self->rom, self->rom_len);
171+
175172
GameBoy_reset(self);
176173

177174
if (!self->boot_rom_exists)
178175
GameBoy_simulate_boot(self);
179176
}
180177

181-
// NOLINTNEXTLINE
182178
u8 GameBoy_read_io(const GameBoy *const self, const u16 addr)
183179
{
184180
if (addr == 0xFF00) // FF00 (joypad input)
@@ -271,15 +267,15 @@ u8 GameBoy_read_mem(const void *const ctx, const u16 addr)
271267
if (self->rom == nullptr)
272268
BAIL("Tried to read non-existing ROM");
273269

274-
// 0000-7FFF (ROM bank)
275-
return self->rom[addr];
270+
// 0000-8FFF (from cartridge)
271+
return Mapper_read(self->mapper, self->rom, self->rom_len, addr);
276272
}
277273

278274
if (addr <= 0x9FFF) // 8000-9FFF (VRAM)
279275
return self->vram[addr - 0x8000];
280276

281-
if (addr <= 0xBFFF) // A000-BFFF (External RAM)
282-
BAIL("TODO: GameBoy_read_mem (addr = $%04X)", addr);
277+
if (addr <= 0xBFFF) // A000-BFFF (from cartridge)
278+
return Mapper_read(self->mapper, self->rom, self->rom_len, addr);
283279

284280
if (addr <= 0xDFFF) // C000-DFFF (WRAM)
285281
return self->ram[addr - 0xC000];
@@ -310,7 +306,6 @@ u16 GameBoy_read_mem_u16(GameBoy *const self, u16 addr)
310306
return concat_u16(hi, lo);
311307
}
312308

313-
// NOLINTNEXTLINE
314309
void GameBoy_write_io(GameBoy *const self, const u16 addr, const u8 value)
315310
{
316311
if (addr == 0xFF00) {
@@ -368,12 +363,12 @@ void GameBoy_write_io(GameBoy *const self, const u16 addr, const u8 value)
368363
case 0xFF47: self->bgp = value; break;
369364
case 0xFF48: self->obp0 = value; break;
370365
case 0xFF49: self->obp1 = value; break;
371-
default: BAIL("Unexpected I/O LCD write (addr = $%04X, value = $%02X)", addr, value);
366+
default: log_warn("Unexpected I/O LCD write (addr = $%04X, value = $%02X)", addr, value);
372367
}
373368
// clang-format on
374369
} else if (addr == 0xFF4F) {
375370
// FF4F
376-
BAIL("I/O VRAM bank select write ($%04X, $%02X)", addr, value);
371+
log_warn("I/O VRAM bank select write ($%04X, $%02X)", addr, value);
377372
} else if (addr == 0xFF50) {
378373
// FF50 (boot ROM disable)
379374
if (value != 0)
@@ -384,10 +379,9 @@ void GameBoy_write_io(GameBoy *const self, const u16 addr, const u8 value)
384379
// FF68-FF6B (LCD color palettes, CGB-only)
385380
} else if (addr == 0xFF70) {
386381
// FF70 (WRAM bank select, CGB-only)
387-
} else if (addr == 0xFF7F) {
388-
// Tetris tries to write here. Probably a no-op.
389382
} else {
390-
BAIL("Unexpected I/O write (addr = $%04X, value = $%02X)", addr, value);
383+
log_warn("Unexpected I/O write (addr = $%04X, value = $%02X)", addr,
384+
value);
391385
}
392386
}
393387

@@ -399,14 +393,13 @@ void GameBoy_write_mem(void *const ctx, const u16 addr, const u8 value)
399393

400394
if (addr <= 0x7FFF) {
401395
// 0000-7FFF (ROM bank)
402-
log_debug("TODO: GameBoy_write_mem ROM (addr = $%04X, $%02X)", addr,
403-
value);
396+
Mapper_write(self->mapper, addr, value);
404397
} else if (addr <= 0x9FFF) {
405398
// 8000-9FFF (VRAM)
406399
self->vram[addr - 0x8000] = value;
407400
} else if (addr <= 0xBFFF) {
408401
// A000-BFFF (External RAM)
409-
BAIL("TODO: GameBoy_write_mem ERAM (addr = $%04X, $%02X)", addr, value);
402+
Mapper_write(self->mapper, addr, value);
410403
} else if (addr <= 0xDFFF) {
411404
// C000-DFFF (WRAM)
412405
self->ram[addr - 0xC000] = value;

src/game_boy.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define GEMU_GAME_BOY_H
33

44
#include "cpu.h"
5+
#include "mapper.h"
56
#include <stddef.h>
67

78
constexpr int GB_LCD_WIDTH = 160;
@@ -71,6 +72,7 @@ typedef struct {
7172
typedef struct {
7273
JoypadState joypad;
7374
Cpu cpu;
75+
Mapper *mapper;
7476
bool boot_rom_exists;
7577
bool boot_rom_enable;
7678
u8 ram[0x2000];
@@ -120,7 +122,7 @@ typedef struct {
120122
[[nodiscard]] GameBoy GameBoy_new(const u8 *boot_rom);
121123

122124
/**
123-
* \brief Frees a previously-created GameBoy.
125+
* \brief Cleans up a previously-created GameBoy.
124126
*
125127
* \param self the GameBoy to destruct.
126128
*

src/instructions.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "instructions.h"
2-
#include "macros.h"
32
#include "cpu.h"
43
#include "log.h"
4+
#include "macros.h"
55
#include "num.h"
66
#include "stdinc.h"
77

@@ -848,7 +848,6 @@ static inline void Cpu_instr_prefix(Cpu *const cpu, Memory *const mem)
848848
// clang-format on
849849
}
850850

851-
// NOLINTNEXTLINE
852851
void Cpu_execute(Cpu *const cpu, Memory *const mem, const u8 opcode)
853852
{
854853
// Credit:

0 commit comments

Comments
 (0)