Edit: I figured, since the z80 is single-threaded, this (using interrupts) is probably the only solution
Sorry, back with another one
A subroutine, which I will call inside my main game loop, should be executed with a delay of 1 second.
The subroutine may be called more than once in one execution of the main game loop, every loop will last ca. 0.01s.
Obviously, "Pause" doesn't work, because I want the main loop to continue running, and Pause seems to stop the entire program (the game should continue running).
My attempt at solving this was adding this timer to the main loop (I don't expect J to become larger than 256)
.Initially I and J are 0
I+1→I
If I=65535
J+1→J
End
And then, every time I would want to call the subroutine, instead I would write the "time" (I,J) into the free space of a buffer (Buff(30)→GDB1)
(0th: I/256; 1st: I^256; 2nd: J)
So, for example, when I call the subroutine after 30min (180000s) of running time, "J" will be 2 (180000/65536) and "I" will be 48928 (180000^65536), which results in the timestamp:{191,32,2}. Because I want to execute it one second later, I will add 100 to the first value so I get {36,33,2}.
I don't expect more than 10 subroutine calls to "queue" at the same time, so the Buffer is of size 3*10, as I need 3 bytes of space to store the timestamp.
The main loop checks if there is a timestamp in the Buffer which equals the current time
For(A,0,10)
If ({GDB1+(A*3)}=I/256) and ({GDB1+(A*3)+1}=I^256) and ({GDB1+(A*3)+2}=J)
sub(DELAY)
0→{GDB1+(A*3)}:0→{GDB1+(A*3)+1}:0→{GDB1+(A*3)+2} .Clear space
End
End
Anyway, this is the only solution I could come up with.
This seems way too complicated for a simple task as delay, is there a better solution?