All roads lead to Rome: a little coding challenge
Posted: Tue Apr 02, 2024 1:30 pm
For obscure reasons (background below), I got interested in this problem:
We need a relocatable 256 byte 65c02 code sequence where I can safely JSR to any of the 256 locations and have the IP eventually advance to the final location (all roads lead to `rome` ...), without changing any memory location nor the X register.
A naive solution is to fill the 256 locations with NOP ($EA), but that requires an average of 2*128 cycles to get from a random landing point to the end of the region. i.e.
What's the best average number of cycles you can come up with? What if you can't use 65c02 opcodes? I have a version with a ladder of BRA instructions using offsets that are also safe landing instructions which averages about 10-12 cycles.
Background: thinking about more compact literals in TaliForth, which are normally coded as JSR literal_runtime / lsb / msb, i.e. 5 bytes per literal. For byte-sized values, we could instead use JSR (rome - value), capturing the literal value in the JSR address (so 3 bytes instead of 5) which we can peek via the return address on the stack. I don't necessarily think it's a very useful solution but is amusing to think about.
We need a relocatable 256 byte 65c02 code sequence where I can safely JSR to any of the 256 locations and have the IP eventually advance to the final location (all roads lead to `rome` ...), without changing any memory location nor the X register.
A naive solution is to fill the 256 locations with NOP ($EA), but that requires an average of 2*128 cycles to get from a random landing point to the end of the region. i.e.
Code: Select all
rome - 256:
nop
nop
...
rome:
Background: thinking about more compact literals in TaliForth, which are normally coded as JSR literal_runtime / lsb / msb, i.e. 5 bytes per literal. For byte-sized values, we could instead use JSR (rome - value), capturing the literal value in the JSR address (so 3 bytes instead of 5) which we can peek via the return address on the stack. I don't necessarily think it's a very useful solution but is amusing to think about.