From 6adebd54c99867d48e0a356539222a601dc0a835 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Wed, 23 Apr 2025 18:01:51 +0200 Subject: [PATCH] simulation: simulation updates now happen in a fixed interval per frame This makes the game hit its 60fps target. Currently inputs still get eaten if a frame drops though. --- src/defs.s | 3 +++ src/mem.s | 2 +- src/simulation.s | 55 ++++++++++++++++++++++++++++++++++++++++-------- src/wram.s | 4 ++-- 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/src/defs.s b/src/defs.s index fe813c4..8f4d458 100644 --- a/src/defs.s +++ b/src/defs.s @@ -7,6 +7,9 @@ #define WRAM 0xC000 #define WRAMLEN 0xFFF + ; max update calls per frame +#define SIM_UPDATE_MAX 8 + #define STACK_BEGIN 0xDFFF #define TILE_SIZE 8 diff --git a/src/mem.s b/src/mem.s index 40037ad..bb74f95 100644 --- a/src/mem.s +++ b/src/mem.s @@ -15,7 +15,7 @@ mem_init: ; set up game mode call game_init - + call sim_init call mbc1_init ret diff --git a/src/simulation.s b/src/simulation.s index c3a98a9..dd2f8a0 100644 --- a/src/simulation.s +++ b/src/simulation.s @@ -1,3 +1,23 @@ + ; loads de and bc with initial values and stores them +sim_init: + ld de, state_cells + ld bc, SCRN0 + + ; stores the values of bc and de in + ; cell_ptr and screen_ptr + sim_store: + ld a, e + ld [sim_cell_ptr], a + ld a, d + ld [sim_cell_ptr+1], a + + ld a, c + ld [sim_cell_screen_ptr], a + ld a, b + ld [sim_cell_screen_ptr+1], a + + ret + ; updates all cells ; eihter until the end of the ; end of the cell list is reached @@ -6,24 +26,40 @@ ; increments cell_idx every iteration ; resets cell_idx to 0 when end of cells is reached sim_update: - ; TODO - ; for now we just iterate all - ; cells in a single go - ; later we want a limit - ld de, state_cells - ld bc, SCRN0 + + ; load sim update state + ld a, [sim_cell_ptr] + ld e, a + ld a, [sim_cell_ptr+1] + ld d, a + + ld a, [sim_cell_screen_ptr] + ld c, a + ld a, [sim_cell_screen_ptr+1] + ld b, a + + ld a, SIM_UPDATE_MAX+1 + push af @loop: ; advance to next cell .rep i, c_size, 1, inc de inc bc - + push de push bc call cell_update pop bc pop de + pop af + ; loop counter-- + dec a + cp a, 0 + ; jp and ret + jp z, sim_store + + push af ; check if we need to exit ld a, state_cells_end LO cp a, e @@ -31,5 +67,6 @@ sim_update: ld a, state_cells_end HI cp a, d jp nz, @loop - - ret + ; if not reset and ret + pop af + jp sim_init diff --git a/src/wram.s b/src/wram.s index e8fbee1..6bfd9fe 100644 --- a/src/wram.s +++ b/src/wram.s @@ -67,11 +67,11 @@ r_population: .adv 2 ; current cell index ; for cell update loop ; big endian -cell_ptr: .adv 2 +sim_cell_ptr: .adv 2 ; cell screen ptr ; points to tile that this cell owns -cell_screen_ptr: .adv 2 +sim_cell_screen_ptr: .adv 2 state_cells: .adv c_size * MAP_SIZE state_cells_end: -- 2.30.2