This corresponds to a pull-request I have just made with the solution (https://github.com/z88dk/z88dk/pull/2714), but wanted to share it with you in case it can help someone in the future. This bug was a nasty one and has been hunting me for days. In short, this is a race condition with interrupts in the sdcc math32 library. Since sdcc passed 8 bits values as just 1 octect in the stack, it is common to see the following pattern:
Code: Select all
pop bc ;return value
dec sp
pop af ;1 octect value goes to a
push bc
...
Code: Select all
pop bc ;return
pop hl ;value goes to L
dec sp
push bc
ld h,0
...
Code: Select all
pop bc ;return
dec sp
pop hl ;value goes to H now, but stack is not exposed to undesired writes from an interruption
push bc
ld l,h
ld h,0