Skip to content

Commit 27d42a0

Browse files
committed
feat: remove all restricts and implement cartridge switching
1 parent 6cbbe79 commit 27d42a0

File tree

17 files changed

+340
-244
lines changed

17 files changed

+340
-244
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ endif()
2222
set(gemu_sources
2323
src/control.c
2424
src/cpu.c
25+
src/data.c
2526
src/frontend.c
2627
src/game_boy.c
2728
src/instructions.c

src/cpu.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Cpu Cpu_new(void)
2424
};
2525
}
2626

27-
bool Cpu_read_cc(const Cpu *const restrict self, const CpuTableCc cc)
27+
bool Cpu_read_cc(const Cpu *const self, const CpuTableCc cc)
2828
{
2929
switch (cc) {
3030
case CpuTableCc_NZ:
@@ -40,7 +40,7 @@ bool Cpu_read_cc(const Cpu *const restrict self, const CpuTableCc cc)
4040
}
4141
}
4242

43-
u16 Cpu_read_rp(const Cpu *const restrict self, const CpuTableRp rp)
43+
u16 Cpu_read_rp(const Cpu *const self, const CpuTableRp rp)
4444
{
4545
switch (rp) {
4646
case CpuTableRp_BC:
@@ -56,7 +56,7 @@ u16 Cpu_read_rp(const Cpu *const restrict self, const CpuTableRp rp)
5656
}
5757
}
5858

59-
void Cpu_write_rp(Cpu *const restrict self, const CpuTableRp rp,
59+
void Cpu_write_rp(Cpu *const self, const CpuTableRp rp,
6060
const u16 value)
6161
{
6262
switch (rp) {
@@ -80,7 +80,7 @@ void Cpu_write_rp(Cpu *const restrict self, const CpuTableRp rp,
8080
}
8181
}
8282

83-
u16 Cpu_read_rp2(const Cpu *const restrict self, const CpuTableRp rp)
83+
u16 Cpu_read_rp2(const Cpu *const self, const CpuTableRp rp)
8484
{
8585
switch (rp) {
8686
case CpuTableRp2_BC:
@@ -96,7 +96,7 @@ u16 Cpu_read_rp2(const Cpu *const restrict self, const CpuTableRp rp)
9696
}
9797
}
9898

99-
void Cpu_write_rp2(Cpu *const restrict self, const CpuTableRp rp,
99+
void Cpu_write_rp2(Cpu *const self, const CpuTableRp rp,
100100
const u16 value)
101101
{
102102
switch (rp) {
@@ -121,65 +121,65 @@ void Cpu_write_rp2(Cpu *const restrict self, const CpuTableRp rp,
121121
}
122122
}
123123

124-
u8 Cpu_read_mem(Cpu *const restrict self, const Memory *const restrict mem,
124+
u8 Cpu_read_mem(Cpu *const self, const Memory *const mem,
125125
const u16 addr)
126126
{
127127
self->cycle_count++;
128128
return mem->read(mem->ctx, addr);
129129
}
130130

131-
u16 Cpu_read_mem_u16(Cpu *const restrict self, const Memory *const restrict mem,
131+
u16 Cpu_read_mem_u16(Cpu *const self, const Memory *const mem,
132132
const u16 addr)
133133
{
134134
const u8 lo = Cpu_read_mem(self, mem, addr);
135135
const u8 hi = Cpu_read_mem(self, mem, addr + 1);
136136
return concat_u16(hi, lo);
137137
}
138138

139-
void Cpu_write_mem(Cpu *const restrict self, Memory *const restrict mem,
139+
void Cpu_write_mem(Cpu *const self, Memory *const mem,
140140
const u16 addr, const u8 value)
141141
{
142142
self->cycle_count++;
143143
mem->write(mem->ctx, addr, value);
144144
}
145145

146-
void Cpu_write_mem_u16(Cpu *const restrict self, Memory *const restrict mem,
146+
void Cpu_write_mem_u16(Cpu *const self, Memory *const mem,
147147
const u16 addr, const u16 value)
148148
{
149149
Cpu_write_mem(self, mem, addr, value & 0xFF);
150150
Cpu_write_mem(self, mem, addr + 1, value >> 8);
151151
}
152152

153-
u8 Cpu_read_pc(Cpu *const restrict self, const Memory *const restrict mem)
153+
u8 Cpu_read_pc(Cpu *const self, const Memory *const mem)
154154
{
155155
const u8 value = Cpu_read_mem(self, mem, self->pc);
156156
self->pc++;
157157
return value;
158158
}
159159

160-
u16 Cpu_read_pc_u16(Cpu *const restrict self, const Memory *const restrict mem)
160+
u16 Cpu_read_pc_u16(Cpu *const self, const Memory *const mem)
161161
{
162162
const u16 value = Cpu_read_mem_u16(self, mem, self->pc);
163163
self->pc += 2;
164164
return value;
165165
}
166166

167-
void Cpu_stack_push_u16(Cpu *const restrict self, Memory *const restrict mem,
167+
void Cpu_stack_push_u16(Cpu *const self, Memory *const mem,
168168
const u16 value)
169169
{
170170
self->sp -= 2;
171171
Cpu_write_mem_u16(self, mem, self->sp, value);
172172
self->cycle_count++;
173173
}
174174

175-
u16 Cpu_stack_pop_u16(Cpu *const restrict self, const Memory *restrict mem)
175+
u16 Cpu_stack_pop_u16(Cpu *const self, const Memory * mem)
176176
{
177177
const u16 value = Cpu_read_mem_u16(self, mem, self->sp);
178178
self->sp += 2;
179179
return value;
180180
}
181181

182-
u8 Cpu_read_r(Cpu *const restrict self, const Memory *const restrict mem,
182+
u8 Cpu_read_r(Cpu *const self, const Memory *const mem,
183183
const CpuTableR r)
184184
{
185185
switch (r) {
@@ -206,7 +206,7 @@ u8 Cpu_read_r(Cpu *const restrict self, const Memory *const restrict mem,
206206
}
207207
}
208208

209-
void Cpu_write_r(Cpu *const restrict self, Memory *const restrict mem,
209+
void Cpu_write_r(Cpu *const self, Memory *const mem,
210210
const CpuTableR r, const u8 value)
211211
{
212212
switch (r) {
@@ -241,7 +241,7 @@ void Cpu_write_r(Cpu *const restrict self, Memory *const restrict mem,
241241
}
242242
}
243243

244-
void Cpu_tick(Cpu *const restrict self, Memory *const restrict mem)
244+
void Cpu_tick(Cpu *const self, Memory *const mem)
245245
{
246246
if (self->mode != CpuMode_Running) {
247247
self->cycle_count++; // Makes the frontend work lmao
@@ -257,7 +257,7 @@ void Cpu_tick(Cpu *const restrict self, Memory *const restrict mem)
257257
Cpu_execute(self, mem, opcode);
258258
}
259259

260-
void Cpu_interrupt(Cpu *const restrict self, Memory *const restrict mem,
260+
void Cpu_interrupt(Cpu *const self, Memory *const mem,
261261
const u8 handler_location)
262262
{
263263
Cpu_stack_push_u16(self, mem, self->pc);

src/cpu.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,41 +84,41 @@ typedef struct {
8484

8585
[[nodiscard]] Cpu Cpu_new(void);
8686

87-
[[nodiscard]] bool Cpu_read_cc(const Cpu *restrict self, CpuTableCc cc);
87+
[[nodiscard]] bool Cpu_read_cc(const Cpu * self, CpuTableCc cc);
8888

89-
[[nodiscard]] u16 Cpu_read_rp(const Cpu *restrict self, CpuTableRp rp);
89+
[[nodiscard]] u16 Cpu_read_rp(const Cpu * self, CpuTableRp rp);
9090

91-
void Cpu_write_rp(Cpu *restrict self, CpuTableRp rp, u16 value);
91+
void Cpu_write_rp(Cpu * self, CpuTableRp rp, u16 value);
9292

93-
[[nodiscard]] u16 Cpu_read_rp2(const Cpu *restrict self, CpuTableRp rp);
93+
[[nodiscard]] u16 Cpu_read_rp2(const Cpu * self, CpuTableRp rp);
9494

95-
void Cpu_write_rp2(Cpu *restrict self, CpuTableRp rp, u16 value);
95+
void Cpu_write_rp2(Cpu * self, CpuTableRp rp, u16 value);
9696

97-
u8 Cpu_read_mem(Cpu *restrict self, const Memory *restrict mem, u16 addr);
97+
u8 Cpu_read_mem(Cpu * self, const Memory * mem, u16 addr);
9898

99-
u16 Cpu_read_mem_u16(Cpu *restrict self, const Memory *restrict mem, u16 addr);
99+
u16 Cpu_read_mem_u16(Cpu * self, const Memory * mem, u16 addr);
100100

101-
void Cpu_write_mem(Cpu *restrict self, Memory *restrict mem, u16 addr,
101+
void Cpu_write_mem(Cpu * self, Memory * mem, u16 addr,
102102
u8 value);
103103

104-
void Cpu_write_mem_u16(Cpu *restrict self, Memory *restrict mem, u16 addr,
104+
void Cpu_write_mem_u16(Cpu * self, Memory * mem, u16 addr,
105105
u16 value);
106106

107-
u8 Cpu_read_pc(Cpu *restrict self, const Memory *restrict mem);
107+
u8 Cpu_read_pc(Cpu * self, const Memory * mem);
108108

109-
u16 Cpu_read_pc_u16(Cpu *restrict self, const Memory *restrict mem);
109+
u16 Cpu_read_pc_u16(Cpu * self, const Memory * mem);
110110

111-
u8 Cpu_read_r(Cpu *restrict self, const Memory *restrict mem, CpuTableR r);
111+
u8 Cpu_read_r(Cpu * self, const Memory * mem, CpuTableR r);
112112

113-
void Cpu_write_r(Cpu *restrict self, Memory *mem, CpuTableR r, u8 value);
113+
void Cpu_write_r(Cpu * self, Memory *mem, CpuTableR r, u8 value);
114114

115-
void Cpu_stack_push_u16(Cpu *restrict self, Memory *restrict mem, u16 value);
115+
void Cpu_stack_push_u16(Cpu * self, Memory * mem, u16 value);
116116

117-
u16 Cpu_stack_pop_u16(Cpu *restrict self, const Memory *restrict mem);
117+
u16 Cpu_stack_pop_u16(Cpu * self, const Memory * mem);
118118

119-
void Cpu_tick(Cpu *restrict self, Memory *restrict mem);
119+
void Cpu_tick(Cpu * self, Memory * mem);
120120

121-
void Cpu_interrupt(Cpu *restrict self, Memory *restrict mem,
121+
void Cpu_interrupt(Cpu * self, Memory * mem,
122122
u8 handler_location);
123123

124124
#endif

src/data.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
11
#include "data.h"
2+
3+
bool CartridgeType_has_ram(const CartridgeType self)
4+
{
5+
return self == CartridgeType_Mcb1Ram ||
6+
self == CartridgeType_Mcb1RamBattery || self == CartridgeType_Mbc2 ||
7+
self == CartridgeType_Mbc2Battery || self == CartridgeType_RomRam ||
8+
self == CartridgeType_RomRamBattery ||
9+
self == CartridgeType_Mmm01Ram ||
10+
self == CartridgeType_Mmm01RamBattery ||
11+
self == CartridgeType_Mbc3TimerRamBattery ||
12+
self == CartridgeType_Mbc3Ram ||
13+
self == CartridgeType_Mbc3RamBattery ||
14+
self == CartridgeType_Mbc5Ram ||
15+
self == CartridgeType_Mbc5RamBattery ||
16+
self == CartridgeType_Mbc5RumbleRam ||
17+
self == CartridgeType_Mbc5RumbleRamBattery ||
18+
self == CartridgeType_Mbc7SensorRumbleRamBattery ||
19+
self == CartridgeType_Huc1RamBattery;
20+
}

src/data.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <stddef.h>
66

77
typedef enum : u16 {
8-
RomHeader_EntryPoint = 0x100,
98
RomHeader_NintendoLogo = 0x104,
109
RomHeader_Title = 0x134,
1110
RomHeader_ManufacturerCode = 0x13F,
@@ -53,4 +52,8 @@ typedef enum : u8 {
5352
CartridgeType_Huc1RamBattery = 0xFF,
5453
} CartridgeType;
5554

55+
void CartridgeType_log_info(CartridgeType self);
56+
57+
bool CartridgeType_has_ram(CartridgeType self);
58+
5659
#endif

src/frontend.c

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#include "frontend.h"
2+
#include "SDL3/SDL_iostream.h"
23
#include "control.h"
34
#include "cpu.h"
45
#include "game_boy.h"
6+
#include "log.h"
57
#include "sdl.h"
68
#include "stdinc.h"
9+
#include <SDL3/SDL_dialog.h>
710
#include <SDL3/SDL_error.h>
811
#include <SDL3/SDL_events.h>
912
#include <SDL3/SDL_keycode.h>
@@ -77,7 +80,7 @@ static u32 map_color_index(const size_t color_index,
7780
color_rgb[2]);
7881
}
7982

80-
static bool *get_joypad_key(GameBoy *const restrict gb, const SDL_Keycode key)
83+
static bool *get_joypad_key(GameBoy *const gb, const SDL_Keycode key)
8184
{
8285
switch (key) {
8386
case SDLK_RETURN:
@@ -101,44 +104,69 @@ static bool *get_joypad_key(GameBoy *const restrict gb, const SDL_Keycode key)
101104
}
102105
}
103106

104-
static void handle_event(State *const restrict state,
105-
const SDL_Event *const restrict event)
107+
static void file_callback(void *const data, const char *const *const files,
108+
[[maybe_unused]] const int filter)
109+
{
110+
if (files == nullptr) {
111+
log_error("Error selecting ROM file: %s", SDL_GetError());
112+
return;
113+
}
114+
115+
if (files[0] == nullptr)
116+
return;
117+
118+
GameBoy *const gb = data;
119+
const char *const rom_file = files[0];
120+
121+
log_info("Loading ROM at %s", rom_file);
122+
123+
size_t rom_len = 0;
124+
u8 *const rom = SDL_LoadFile(rom_file, &rom_len);
125+
126+
if (rom == nullptr) {
127+
log_error("Could not load ROM file: %s", SDL_GetError());
128+
return;
129+
}
130+
131+
GameBoy_load_rom(gb, rom, rom_len);
132+
GameBoy_log_cartridge_info(gb);
133+
}
134+
135+
static void handle_event(State *const state, const SDL_Event *const event)
106136
{
107137
switch (event->type) {
108-
case SDL_EVENT_QUIT: {
138+
case SDL_EVENT_QUIT:
109139
state->quit = true;
110140
break;
111-
}
112-
case SDL_EVENT_WINDOW_RESIZED: {
141+
case SDL_EVENT_WINDOW_RESIZED:
113142
state->window_width = event->window.data1;
114143
state->window_height = event->window.data2;
115144
break;
116-
}
117145
case SDL_EVENT_KEY_DOWN: {
118146
bool *const state_key = get_joypad_key(&state->gb, event->key.key);
119-
if (state_key != nullptr)
147+
if (state_key != nullptr) {
148+
break;
120149
*state_key = true;
150+
}
121151

152+
if (event->key.mod & SDL_KMOD_CTRL && event->key.key == SDLK_O) {
153+
SDL_ShowOpenFileDialog(file_callback, &state->gb, nullptr, nullptr,
154+
0, nullptr, false);
155+
}
122156
break;
123157
}
124158
case SDL_EVENT_KEY_UP: {
125-
if (event->key.mod == SDL_KMOD_CTRL && event->key.key == SDLK_O) {
126-
// TODO: open new ROM
127-
break;
128-
}
129-
130159
bool *const state_key = get_joypad_key(&state->gb, event->key.key);
131160
if (state_key != nullptr)
132161
*state_key = false;
133162

134163
break;
135164
}
136-
default: {
137-
}
165+
default:
138166
}
139167
}
140168

141-
static void update(State *const restrict state, const double delta)
169+
static void update(State *const state, const double delta)
142170
{
143171
Memory memory = (Memory){
144172
.ctx = &state->gb,
@@ -330,7 +358,7 @@ static void draw_objects(const State *const state,
330358
}
331359
}
332360

333-
static void update_texture(const State *const restrict state)
361+
static void update_texture(const State *const state)
334362
{
335363
SDL_Surface *surface = nullptr;
336364

@@ -357,8 +385,7 @@ static void update_texture(const State *const restrict state)
357385
SDL_UnlockTexture(state->screen_texture);
358386
}
359387

360-
static void render(const State *const restrict state,
361-
SDL_Renderer *const restrict renderer)
388+
static void render(const State *const state, SDL_Renderer *const renderer)
362389
{
363390
const float ASPECT_RATIO = (float)GB_LCD_WIDTH / GB_LCD_HEIGHT;
364391

@@ -384,8 +411,7 @@ static void render(const State *const restrict state,
384411
SDL_RenderPresent(renderer);
385412
}
386413

387-
void run_until_quit(State *const restrict state,
388-
SDL_Renderer *const restrict renderer)
414+
void run_until_quit(State *const state, SDL_Renderer *const renderer)
389415
{
390416
double last_time = sdl_get_performance_time();
391417
double time_accumulator = 0.0;

src/frontend.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ typedef struct {
1313
int div_cycle_counter;
1414
int tima_cycle_counter;
1515
bool quit;
16-
SDL_Texture *restrict screen_texture;
16+
SDL_Texture * screen_texture;
1717
} State;
1818

19-
void run_until_quit(State *restrict state, SDL_Renderer *restrict renderer);
19+
void run_until_quit(State * state, SDL_Renderer * renderer);
2020

2121
#endif

0 commit comments

Comments
 (0)