From 36a428bcea7a7139fc5c6cca5baf2a0b9f47ca1a Mon Sep 17 00:00:00 2001 From: Samuel Walker Date: Sat, 1 Feb 2025 20:53:13 -0700 Subject: [PATCH] fixed scroll? --- include/ppu.h | 2 ++ lib/cart.c | 3 +- lib/ppu.c | 2 ++ lib/ppu_pipeline.c | 79 ++++++++++++++++++++++++++++++---------------- lib/ppu_sm.c | 1 + 5 files changed, 59 insertions(+), 28 deletions(-) diff --git a/include/ppu.h b/include/ppu.h index aa005de..a21151d 100644 --- a/include/ppu.h +++ b/include/ppu.h @@ -77,6 +77,8 @@ typedef struct { u32 current_frame; u32 line_ticks; u32 *video_buffer; + + bool rendering_window; } ppu_context; void ppu_init(); diff --git a/lib/cart.c b/lib/cart.c index 36eadb9..8462b2e 100644 --- a/lib/cart.c +++ b/lib/cart.c @@ -318,7 +318,8 @@ bool cart_battery_load(){ FILE *fp = fopen(fn, "rb"); if(!fp) { - fprintf(stderr, "unable to open: %s\n", fn); + printf("No battery save found."); + return true; } fread(ctx.ram_bank, 0x2000, 1, fp); diff --git a/lib/ppu.c b/lib/ppu.c index 152e69f..80252cf 100644 --- a/lib/ppu.c +++ b/lib/ppu.c @@ -28,6 +28,8 @@ void ppu_init() { ctx.fetched_entry_count = 0; ctx.window_line = 0; + ctx.rendering_window = false; + lcd_init(); LCDS_MODE_SET(MODE_OAM); diff --git a/lib/ppu_pipeline.c b/lib/ppu_pipeline.c index 4ba19d0..7deea90 100644 --- a/lib/ppu_pipeline.c +++ b/lib/ppu_pipeline.c @@ -162,19 +162,16 @@ void pipeline_load_window_tile() { u8 window_y = lcd_get_context()->win_y; - if (ppu_get_context()->pfc.fetch_x + 7 >= lcd_get_context()->win_x && - ppu_get_context()->pfc.fetch_x + 7 < lcd_get_context()->win_x + YRES + 14) { - if(lcd_get_context()->ly >= window_y && lcd_get_context()->ly < window_y + XRES) { - u8 w_tile_y = ppu_get_context()->window_line / 8; + if (ppu_get_context()->rendering_window) { + u8 w_tile_y = ppu_get_context()->window_line / 8; - ppu_get_context()->pfc.bgw_fetch_data[0] = bus_read(LCDC_WIN_MAP_AREA + - ((ppu_get_context()->pfc.fetch_x + 7 - lcd_get_context()->win_x) / 8) + - (w_tile_y * 32)); + ppu_get_context()->pfc.bgw_fetch_data[0] = bus_read(LCDC_WIN_MAP_AREA + + ((ppu_get_context()->pfc.fetch_x + 7 - lcd_get_context()->win_x) / 8) + + (w_tile_y * 32)); - if(LCDC_BGW_DATA_AREA == 0x8800){ - ppu_get_context()->pfc.bgw_fetch_data[0] += 128; - } + if(LCDC_BGW_DATA_AREA == 0x8800){ + ppu_get_context()->pfc.bgw_fetch_data[0] += 128; } } } @@ -204,9 +201,15 @@ void pipeline_fetch() { } break; case FS_DATA0: { - ppu_get_context()->pfc.bgw_fetch_data[1] = bus_read(LCDC_BGW_DATA_AREA + - (ppu_get_context()->pfc.bgw_fetch_data[0] * 16) + - ppu_get_context()->pfc.tile_y); + if(ppu_get_context()->rendering_window){ + ppu_get_context()->pfc.bgw_fetch_data[1] = bus_read(LCDC_BGW_DATA_AREA + + (ppu_get_context()->pfc.bgw_fetch_data[0] * 16) + + (lcd_get_context()->ly % 8) * 2); + } else { + ppu_get_context()->pfc.bgw_fetch_data[1] = bus_read(LCDC_BGW_DATA_AREA + + (ppu_get_context()->pfc.bgw_fetch_data[0] * 16) + + ppu_get_context()->pfc.tile_y); + } pipeline_load_sprite_data(0); @@ -214,9 +217,15 @@ void pipeline_fetch() { } break; case FS_DATA1: { - ppu_get_context()->pfc.bgw_fetch_data[2] = bus_read(LCDC_BGW_DATA_AREA + - (ppu_get_context()->pfc.bgw_fetch_data[0] * 16) + - ppu_get_context()->pfc.tile_y + 1); + if(ppu_get_context()->rendering_window){ + ppu_get_context()->pfc.bgw_fetch_data[2] = bus_read(LCDC_BGW_DATA_AREA + + (ppu_get_context()->pfc.bgw_fetch_data[0] * 16) + 1 + + (lcd_get_context()->ly % 8) * 2); + } else { + ppu_get_context()->pfc.bgw_fetch_data[2] = bus_read(LCDC_BGW_DATA_AREA + + (ppu_get_context()->pfc.bgw_fetch_data[0] * 16) + + ppu_get_context()->pfc.tile_y + 1); + } pipeline_load_sprite_data(1); @@ -235,15 +244,39 @@ void pipeline_fetch() { } } +void pipeline_fifo_reset() { + while(ppu_get_context()->pfc.pixel_fifo.size) { + pixel_fifo_pop(); + } + + ppu_get_context()->pfc.pixel_fifo.head = 0; +} + void pipeline_push_pixel() { if (ppu_get_context()->pfc.pixel_fifo.size > 8) { u32 pixel_data = pixel_fifo_pop(); - if(ppu_get_context()->pfc.line_x >= (lcd_get_context()->scroll_x % 8)) { - ppu_get_context()->video_buffer[ppu_get_context()->pfc.pushed_x + - (lcd_get_context()->ly * XRES)] = pixel_data; + if(ppu_get_context()->pfc.pushed_x+7 >= lcd_get_context()->win_x && lcd_get_context()->ly >= lcd_get_context()->win_y && !ppu_get_context()->rendering_window && window_visible()) { + ppu_get_context()->rendering_window = true; + pipeline_fifo_reset(); + ppu_get_context()->pfc.cur_fetch_state = FS_TILE; + printf("Window X: %d\n", lcd_get_context()->win_x); + ppu_get_context()->pfc.fetch_x = 0; + ppu_get_context()->pfc.fifo_x = 0; + return; + } + if(ppu_get_context()->rendering_window) { + ppu_get_context()->video_buffer[ppu_get_context()->pfc.pushed_x + + (lcd_get_context()->ly * XRES)] = pixel_data; ppu_get_context()->pfc.pushed_x++; + }else { + if(ppu_get_context()->pfc.line_x >= (lcd_get_context()->scroll_x % 8)) { + ppu_get_context()->video_buffer[ppu_get_context()->pfc.pushed_x + + (lcd_get_context()->ly * XRES)] = pixel_data; + + ppu_get_context()->pfc.pushed_x++; + } } ppu_get_context()->pfc.line_x++; @@ -260,12 +293,4 @@ void pipeline_process() { } pipeline_push_pixel(); -} - -void pipeline_fifo_reset() { - while(ppu_get_context()->pfc.pixel_fifo.size) { - pixel_fifo_pop(); - } - - ppu_get_context()->pfc.pixel_fifo.head = 0; } \ No newline at end of file diff --git a/lib/ppu_sm.c b/lib/ppu_sm.c index 8953c85..942fdc0 100644 --- a/lib/ppu_sm.c +++ b/lib/ppu_sm.c @@ -113,6 +113,7 @@ void ppu_mode_xfer() { if(LCDS_STAT_INT(SS_HBLANK)) { cpu_request_interrupt(IT_LCD_STAT); } + ppu_get_context()->rendering_window = false; } }