pk-hack / CoilSnake

https://pk-hack.github.io/CoilSnake/
GNU General Public License v3.0
158 stars 37 forks source link

Patch music engine to avoid echo buffer overwriting sample data #292

Closed charasyn closed 3 months ago

charasyn commented 3 months ago

Recently, we've become aware of a bug. When transitioning from a song with a non-zero echo delay value to a new song which uses instrument samples that would overlap with the previous echo buffer, the samples may become corrupted independent of the echo value used by the new song. This is because the echo feature was not disabled when the instruments for the new song were uploaded.

This branch adds a new feature where CoilSnake will patch the music engine to disable echo when uploading data.

This is the assembly code added to the SPC, assembled by hand:

<          > ; push old echo value
< eb 4d    >     mov y, $4d
< f0 13    >     beq _exit
< 6d       >     push y
<          > ; call existing code at $0b2c with A=0 to disable echo
< e8 00    >     mov a, #0
< 3f 2c 0b >     call $0b2c
<          > ; wait for (old echo) * 16ms = 2ms * (old echo) * 8
< ae       >     pull a
< 1c       >     asl a
< 1c       >     asl a
< 1c       >     asl a
< bc       >     inc a
< fd       >     mov y, a
< e5 fd 00 > -   mov a, $00fd
< f0 fb    >     beq -
< fe f9    >     dbnz y, -
<          > ; continue with normal code
<          > _exit:
< 5f e1 0e >     jmp $0ee1