From 2543ba04b59a035815b1dce38221787802dde49c Mon Sep 17 00:00:00 2001 From: Samuel Walker Date: Sat, 31 May 2025 09:47:01 -0600 Subject: [PATCH] IO initial states are correct --- include/serial.h | 15 +++++++++++++++ lib/audio.c | 33 +++++++++++++++++---------------- lib/cpu.c | 2 +- lib/emu.c | 2 ++ lib/io.c | 21 +++++---------------- lib/lcd.c | 1 + lib/serial.c | 34 ++++++++++++++++++++++++++++++++++ lib/timer.c | 1 + 8 files changed, 76 insertions(+), 33 deletions(-) create mode 100644 include/serial.h create mode 100644 lib/serial.c diff --git a/include/serial.h b/include/serial.h new file mode 100644 index 0000000..176a5b0 --- /dev/null +++ b/include/serial.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +typedef struct { + u8 serial_data; + u8 serial_control; +} serial_context; + +serial_context *serial_get_context(); + +void serial_init(); + +void serial_write(u16 address, u8 value); +u8 serial_read(u16 address); \ No newline at end of file diff --git a/lib/audio.c b/lib/audio.c index 745bff4..0b9ba83 100644 --- a/lib/audio.c +++ b/lib/audio.c @@ -85,33 +85,34 @@ void audio_init(){ memset(ctx.left_audio_buffer, 0, sizeof(float) * FRAMES_PER_BUFFER); memset(ctx.right_audio_buffer, 0, sizeof(float) * FRAMES_PER_BUFFER); - ctx.audio_enabled = false; - ctx.ch1_left = false; - ctx.ch1_right = false; - ctx.ch2_left = false; - ctx.ch2_right = false; - ctx.ch3_left = false; + ctx.audio_enabled = true; + ctx.ch1_left = true; + ctx.ch1_right = true; + ctx.ch2_left = true; + ctx.ch2_right = true; + ctx.ch3_left = true; ctx.ch3_right = false; - ctx.ch4_left = false; + ctx.ch4_left = true; ctx.ch4_right = false; - ctx.volume_left = 0x1; - ctx.volume_right = 0x1; + ctx.volume_left = 0x7; + ctx.volume_right = 0x7; - ctx.sq1_duty = 0x0; - ctx.sq1_volume = 0x0; + ctx.sq1_duty = 0x2; + ctx.sq1_volume = 0xF; ctx.sq1_sample = 0x0; ctx.sq1_period_reset = 0x0; ctx.sq1_period_timer = 0x0; - ctx.sq1_enable = false; + ctx.sq1_enable = true; ctx.sq1_len_enable = false; ctx.sq1_sweep_pace = 0x0; ctx.sq1_sweep_direction = false; - ctx.sq1_initial_len = 0x0; + ctx.sq1_initial_len = 0x3F; ctx.sq1_len = 0x0; - ctx.sq1_initial_volume = 0x0; + ctx.sq1_initial_volume = 0xF; ctx.sq1_env_direction = false; - ctx.sq1_env_pace = 0x0; + ctx.sq1_env_pace = 0x3; + ctx.sq1_env_pace_buffer = 0x3; ctx.sq1_env_timer = 0x0; ctx.sq1_sweep_step = 0x0; @@ -122,7 +123,7 @@ void audio_init(){ ctx.sq2_period_timer = 0x0; ctx.sq2_enable = false; ctx.sq2_len_enable = false; - ctx.sq2_initial_len = 0x0; + ctx.sq2_initial_len = 0x3F; ctx.sq2_len = 0x0; ctx.sq2_initial_volume = 0x0; ctx.sq2_env_direction = false; diff --git a/lib/cpu.c b/lib/cpu.c index 1017ffb..a777c54 100644 --- a/lib/cpu.c +++ b/lib/cpu.c @@ -28,7 +28,7 @@ void cpu_init() { ctx.regs.sp = 0xFFFE; ctx.regs.pc = 0x100; ctx.ie_register = 0; - ctx.int_flags = 0; + ctx.int_flags = 0xE1; ctx.int_master_enabled = false; ctx.enabling_ime = false; diff --git a/lib/emu.c b/lib/emu.c index a47916f..3f21323 100644 --- a/lib/emu.c +++ b/lib/emu.c @@ -9,6 +9,7 @@ #include #include #include +#include #ifdef _WIN32 #include @@ -69,6 +70,7 @@ void emu_reset() { cpu_init(); audio_init(); cart_init(); + serial_init(); } void emu_start() { diff --git a/lib/io.c b/lib/io.c index eb13764..b493317 100644 --- a/lib/io.c +++ b/lib/io.c @@ -5,19 +5,14 @@ #include #include #include - -static char serial_data[2]; +#include u8 io_read(u16 address){ if(address == 0xFF00) { return gamepad_get_output(); } - if(address == 0xFF01) { - return serial_data[0]; - } - - if(address == 0xFF02) { - return serial_data[1]; + if(address == 0xFF01 || address == 0xFF02) { + return serial_read(address); } if(BETWEEN(address, 0xFF04, 0xFF07)){ @@ -46,14 +41,8 @@ void io_write(u16 address, u8 value){ gamepad_set_sel(value); return; } - if(address == 0xFF01) { - serial_data[0] = value; - //printf("%c", value); - return; - } - - if(address == 0xFF02) { - serial_data[1] = value; + if(address == 0xFF01 || address == 0xFF02) { + serial_write(address, value); return; } diff --git a/lib/lcd.c b/lib/lcd.c index a198315..f12ed31 100644 --- a/lib/lcd.c +++ b/lib/lcd.c @@ -21,6 +21,7 @@ void lcd_init() { ctx.obj_palette[1] = 0xFF; ctx.win_x = 0; ctx.win_y = 0; + ctx.lcds = 0x80; for(int i = 0; i < 4; i++) { ctx.bg_colors[i] = colors_default[i]; diff --git a/lib/serial.c b/lib/serial.c new file mode 100644 index 0000000..623d745 --- /dev/null +++ b/lib/serial.c @@ -0,0 +1,34 @@ +#include + +static serial_context ctx; + +serial_context *serial_get_context() { + return &ctx; +}; + +void serial_init() { + ctx.serial_control = 0x7E; + ctx.serial_data = 0; +} + +void serial_write(u16 address, u8 value) { + if(address == 0xFF01) { + ctx.serial_data = value; + return; + } + + if(address == 0xFF02) { + ctx.serial_control = value; + return; + } +} + +u8 serial_read(u16 address) { + if(address == 0xFF01) { + return ctx.serial_data; + } + + if(address == 0xFF02) { + return ctx.serial_control; + } +} \ No newline at end of file diff --git a/lib/timer.c b/lib/timer.c index eb0dd10..361c31c 100644 --- a/lib/timer.c +++ b/lib/timer.c @@ -34,6 +34,7 @@ void inc_tima() { void timer_init() { ctx.div = 0XAC00; + ctx.tac = 0xF8; } void timer_tick() {