News:

Don't forget to visit the main site! There's lots of helpful docs, patches, and more!

Main Menu

How to set a delay ?

Started by Vener, April 24, 2017, 11:53:37 AM

Previous topic - Next topic

Vener

Hello there 

Well , i guess the title say all ; i never understood how to make a simple frame delay in asm code .
Exemple : You have a 60 frames counter , then a check process the branche 'A' until the counter reach zero , and at zero ,it process branche 'B' . A simple timer check ...

Sometimes i saw a value loaded in a register , followed by a decrementation of this register . I supose that the running take out 1 frame each time the routine is ran , but i'm really not sure about this , since if the value is not stored in a RAM adress , but in a register only , i can't understand how the game can keep track of the decrementation process of this value .

Thanks for the one which will explain .   

monktroid

#1
1) set a delay value at a free point in the RAM (you can check what's free via Geiger, look for the high space at the top of the RAM table that's full of 55 55 55 ...)

2) find some free space in the ROM at a suitable location. 

3) write the function you wanna perform so that it hijacks an area of memory that is called every frame. have your function run a decrement operation on your delay value and a test for when that value goes below zero using BMI.  when the counter is done, perform your operation and, just before you return to the hijack point, reset your value in RAM to its original value.

Let's say your delay was #$0009 (ie. run once every 10 frames) and stored at $FE02

Something along the lines of:

LDX $FE02 ; pointer to your value in RAM
DEX       ; x - 1
CPX #$0000 : BMI + : RTS ; NB. be sure to have your code perform the instruction you hijacked before RTS!!
+                        ; if the result of CPX sets the N in P flag register, it'll branch.
***YOUR CODE TO RUN ON EVERY 10th FRAME***
LDX #$0009 : STA $FE02 ; reset counter
RTS   ; NB. be sure to have your code perform the instruction you hijacked before RTS!!


if any of the terminology in the above is confusing, I strongly suggest you read the following:

https://wiki.superfamicom.org/snes/show/Jay%27s+ASM+Tutorial