From 213c08fa1871a2b17c895e5c1f752969090094e9 Mon Sep 17 00:00:00 2001 From: Max Regan Date: Mon, 2 Jul 2018 05:24:36 +0000 Subject: [PATCH] gbdb: remove some conditionals from mem_read() By reading directly from a "RAM" array, and copying in the cart data over the old bootrom. Also, some other fixups because I'm too lazy to properly split patches for a personal project. --- gbdb_boot_test | 1 + src/apps/gbdb.c | 15 +++++++-------- src/gbemu/cpu.c | 4 +--- src/gbemu/video.c | 7 +++---- 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/gbdb_boot_test b/gbdb_boot_test index 7ef5297..9f29a38 100644 --- a/gbdb_boot_test +++ b/gbdb_boot_test @@ -1,3 +1,4 @@ +load ../harvest_moon.gb bootrom ../bootrom.gb echo Running test... set quiet 1 diff --git a/src/apps/gbdb.c b/src/apps/gbdb.c index f842a79..912359c 100644 --- a/src/apps/gbdb.c +++ b/src/apps/gbdb.c @@ -19,7 +19,7 @@ #include "common/tri.h" #define INPUT_MAX_LEN 512 -#define MAX_BREAKPTS 2 /* Should be plenty for anyone */ +#define MAX_BREAKPTS 16 /* Should be plenty for anyone */ #define MAX_RAM_LEN (1 << 20) /* Up to 8Mb Cartridge */ #define UNMAP_BOOTROM_ADDR 0xff50 @@ -46,6 +46,7 @@ static struct lr35902_state cpu; static struct gb_video video; static unsigned char bootrom[0x100] = {0}; +static unsigned char cart_low[0x100] = {0}; static int bootrom_mapped = 1; @@ -97,12 +98,6 @@ static uint8_t mem_read(struct lr35902_state *cpu, uint16_t addr) { switch (addr) { - case 0 ... 0x100-1: - if (bootrom_mapped) { - return bootrom[addr]; - } else { - return ram[addr]; - } case 0xFF40 ... 0xFF4B: return gb_video_mem_read(&video, addr); default: @@ -117,6 +112,7 @@ static void mem_write(struct lr35902_state *cpu, uint16_t addr, uint8_t val) case UNMAP_BOOTROM_ADDR: if (val == 1) { bootrom_mapped = 0; + memcpy(ram, cart_low, sizeof(cart_low)); gb_log("bootrom unmapped\n"); } break; @@ -375,7 +371,7 @@ static void assert(char *arg_list) return; fail: printf("ASSERT: %s %s %s\n", val0_str, operator_str, val1_str); - printf("%s=%ld, %s=%ld\n", val0_str, val0, val1_str, val1); + printf("%s=%lld, %s=%lld\n", val0_str, val0, val1_str, val1); regs(NULL); stats(NULL); exit(1); @@ -449,6 +445,8 @@ static void load(char *arg_list) strip_newline(token); load_file_to_buffer(token, ram, sizeof(ram)); + load_file_to_buffer(token, cart_low, sizeof(cart_low)); + } static void load_bootrom(char *arg_list) @@ -461,6 +459,7 @@ static void load_bootrom(char *arg_list) strip_newline(token); load_file_to_buffer(token, bootrom, sizeof(bootrom)); + load_file_to_buffer(token, ram, sizeof(bootrom)); } diff --git a/src/gbemu/cpu.c b/src/gbemu/cpu.c index 0a66a42..c7ba00d 100644 --- a/src/gbemu/cpu.c +++ b/src/gbemu/cpu.c @@ -326,12 +326,10 @@ void lr35902_cycle(struct lr35902_state *cpu) if (cpu->stall_cycles > 0) { cpu->stall_cycles--; - if (cpu->stall_cycles == 0) { - cpu->metrics.retired_instrs++; - } return; } + cpu->metrics.retired_instrs++; inst = cpu->mem_read(cpu, cpu->pc++); cpu->stall_cycles = op_extra_cycles[inst]; diff --git a/src/gbemu/video.c b/src/gbemu/video.c index ca88f5c..d5e00f4 100644 --- a/src/gbemu/video.c +++ b/src/gbemu/video.c @@ -14,16 +14,15 @@ void gb_video_init(struct gb_video *video) void gb_video_cycle(struct gb_video *video) { - video->line_counter++; - - if (video->line_counter >= CYCLES_PER_LINE) { + if (video->line_counter < CYCLES_PER_LINE - 1) { + video->line_counter++; + } else { video->line_counter = 0; video->curline += 1; if (video->curline > LCD_Y_MAX) { video->curline = 0; } } - } uint8_t gb_video_mem_read(struct gb_video *video, uint16_t addr)