This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Arranging timing-sensitive code

Arranging timing-sensitive code
by on (#53645)
How do you usually go about arranging your code and data that mustn't cross pages for timing reasons? I have a few routines and tables to arrange (they are all smaller than 256 bytes, but some are close to that) and was wondering if any of you had any tips about this.

by on (#53646)
It depends on which assembler you're using. I can answer for the cc65 toolchain: for one, your link script needs a segment with align=128 or align=256, and then you need to have a matching .align statement in your source code.

by on (#53648)
I'm using ASM6. Currently I'm just using ".align 256" when necessary, but that wastes space, of course. I might have to wait until I have more subroutines that I could use to fill the gaps. Can other assemblers do that automatically?

by on (#53650)
I'm just doing it manually now. I stick as many timing-sensitive things as I can in one page, then I use something that's not sensitive as the page is crossed and then keep puting the sensitive code/data.

by on (#53651)
for my FF1 disassembly (ca65), I rigged up some macros so it would throw an error if a block of code crossed a page boundary.

*digs up code*

      DEY            ;   +2
      BNE @Loop      ;   +3  (5 cycle loop * 16 iterations = 80-1 = 79 cycles for loop)

  CRITPAGECHECK @Loop      ; ensure above loop does not cross page boundary

 ; for critical page checks (absolutely should not cross page boundary)
  .assert >* = >lbl, error, "Critical timing loop crosses page boundary"

'*' in the macro is the current address. Basically the code checks the high byte of the current address and the high byte of the 'lbl' argument and sees if they match, and throws an error if they don't.

I don't know if ASM6 has something similar. If not, it would be a nice addition.

by on (#53652)
This is a very interesting idea Disch, thanks. I'll try something like this. ASM6 has macros, '$' is the current address, and you can throw errors, so I guess it should work.

by on (#53681)
Nice macro, I will be adding that to my standard macros.asm file.