Skip to content

Commit

Permalink
compiles for macos
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisledet committed Nov 11, 2023
1 parent ffe8bb7 commit 6f8ebe8
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ dkms.conf

# Build artifacts
build/
.DS_Store
1 change: 1 addition & 0 deletions include/cart.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ typedef struct {
cart_context *get_cart_context();
bool cart_load(const char *cart_filepath);
u8 cart_read(u16 addr);
void cart_debug();
2 changes: 1 addition & 1 deletion include/instruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ static cpu_instruction instructions[0x100] = {
[0xBF] = {INSTRUCT_CP, MODE_REG_TO_REG, REG_A, REG_A},

[0xC0] = {INSTRUCT_RET, .flag = FLAG_NZ},
[0xC1] = {INSTRUCT_POP, MODE_ADDR_TO_REG, }, REG_SP,
[0xC1] = {INSTRUCT_POP, MODE_ADDR_TO_REG, REG_SP},
[0xC2] = {INSTRUCT_JP, MODE_A16, .flag = FLAG_NZ, .byte_length = 3},
[0xC3] = {INSTRUCT_JP, MODE_A16, .byte_length = 3},
[0xC4] = {INSTRUCT_CALL, MODE_A16, .flag = FLAG_Z, .byte_length = 3},
Expand Down
2 changes: 1 addition & 1 deletion include/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ typedef enum {
void timer_init();
u8 timer_read(u16 addr);
void timer_write(u16 addr, u8 val);
bool timer_tick();
bool timer_tick(u8 cycles);
4 changes: 1 addition & 3 deletions src/bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,15 @@ u16 bus_read16(u16 addr) {

void bus_write(u16 addr, u8 val) {
if (addr < 0x1FFF) {

// mbc1 logic
if (val == 0xA)
ctx.ram_enabled = true;
else if (!val)
ctx.ram_enabled = false;

} else if (0x2000 <= addr && addr < 0x8000) {
// TODO: ROM / RAM switch
} else if (0x8000 <= addr && addr < 0xA000) {
ctx.vram[(ctx.vram_bank + VRAM_BANK_SIZE) + (addr - 0x8000)];
ctx.vram[(ctx.vram_bank + VRAM_BANK_SIZE) + (addr - 0x8000)] = val;
} else if (0xA000 <= addr && addr < 0xC000) {
// cart ram (save progress)
ctx.ram[(ctx.ram_bank * RAM_BANK_SIZE) + (addr - 0xA000)] = val;
Expand Down
27 changes: 14 additions & 13 deletions src/cart.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ u8 cart_read(u16 addr) {
bool cart_load(const char *cart_filepath) {
ctx.filepath = cart_filepath;

FILE *file;
int ferr = fopen_s(&file, cart_filepath, "r");
if (ferr != 0 && !file) {
FILE *file = fopen(cart_filepath, "rb");
if (!file) {
perror("Failed to open file.");
return false;
}
Expand All @@ -39,17 +38,18 @@ bool cart_load(const char *cart_filepath) {
ctx.rom_data = malloc(ctx.rom_size);
if (ctx.rom_data == NULL) {
fprintf(stderr, "memory allocation failed!\n");
fclose(file);
return false;
}

rewind(file);
const size_t fread_rc = fread(ctx.rom_data, ctx.rom_size, 1, file);
const u32 last_read = ftell(file);
fclose(file);
// still confused if rc is supppose to be 1 or not
// if (fread_rc == 1) {

if (ctx.rom_size != last_read) {
fprintf(stderr, "WARN: cart read wasnt successful: %zu, failed at %lu\n", fread_rc, last_read);
fprintf(stderr, "ERROR: failed to read cartridge data from %s\n", cart_filepath);
free(ctx.rom_data);
return false;
}

Expand All @@ -58,18 +58,21 @@ bool cart_load(const char *cart_filepath) {
ctx.header->game_title[15] = 0; // ensure str termination

if (memcmp(&ctx.rom_data[0x104], scrolling_logo, sizeof(scrolling_logo)) != 0) {
fprintf(stderr, "ERR: cart missing logo header)");
fprintf(stderr, "ERROR: cartridge missing logo header\n");
free(ctx.rom_data);
return false;
}

// run checksum
uint16_t checksum = 0;
for (uint16_t i = 0x0134; i <= 0x014C; i++) {
u16 checksum = 0;
for (u16 i = 0x0134; i <= 0x014C; i++)
checksum = checksum - (ctx.rom_data[i] - 1);
}
bool r_checksum = (checksum & 0xFF);
printf("DEBUG: CHECKSUM: %2.2X (%s)\n", ctx.header->checksum, r_checksum ? "PASSED" : "FAILED");
return r_checksum;
}

// debug result
void cart_debug() {
printf("CARTRIDGE LOADED:\n");
printf("\tPATH : %s\n", ctx.filepath);
printf("\tTITLE : %s\n", ctx.header->game_title);
Expand All @@ -84,6 +87,4 @@ bool cart_load(const char *cart_filepath) {
ctx.header->entry_point[2],
ctx.header->entry_point[3]
);
printf("\tCHECKSUM : %2.2X (%s)\n", ctx.header->checksum, r_checksum ? "PASSED" : "FAILED");
return r_checksum;
}
2 changes: 2 additions & 0 deletions src/gbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ int gbc_run(const char *rom_filepath) {
return -1;
}

cart_debug();

// set up cpu
cart_context *cart_ctx = get_cart_context();
bus_init(cart_ctx);
Expand Down

0 comments on commit 6f8ebe8

Please sign in to comment.