simulation: simulation updates now happen in a fixed interval per frame
authorLukas Krickl <lukas@krickl.dev>
Wed, 23 Apr 2025 16:01:51 +0000 (18:01 +0200)
committerLukas Krickl <lukas@krickl.dev>
Wed, 23 Apr 2025 16:01:51 +0000 (18:01 +0200)
This makes the game hit its 60fps target.
Currently inputs still get eaten if a frame drops though.

src/defs.s
src/mem.s
src/simulation.s
src/wram.s

index fe813c4b69f46c3a0c0fb4efd0e45887d6584bc6..8f4d4587618e1658db0804986c1bf60166f39063 100644 (file)
@@ -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
index 40037ad5ec40006bdf30261b5cd82c1d631ca7d5..bb74f954fc7e7f8ac11846f2fbbcb7949930c061 100644 (file)
--- 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
index c3a98a907d674e006a07ece86b3f891da3f767c0..dd2f8a0eaf3ca871ed3283453753321f289d1d0a 100644 (file)
@@ -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 
   ; 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 
index e8fbee17a2b3862c8eed92341e54dd4f7e097999..6bfd9fe02a41f98cd77bbc20cc2ee7a5c4806bb7 100644 (file)
@@ -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: