So I'm trying to wrap my head around, on how to use local variables efficiently and not make mistakes.
It is not really important is it assembler of C, because cc65 does not encourage using local stack variables anyway. I don't say I really like x86, but arbitrary stack access in 6502 would be nice... Or at least fast stack.
Anyway, from what I learnt and saw, regardless of the compiler/assembler the only sane way is to use global variables.
Pseudo code example
So now I really have two options:
1. Fast and dangerous - be careful with using local variables, and do manual memory management, making sure that variables and registers will not be ruined
2. Slow and more comfortable - Use hardware stack, which is so-so for A register, but quite slow for anything else as it would require lda mem, pha, pla, sta mem for every variable
Am I correct? Should I take approach with hardware stack or rather forget about it and just try to be careful and spend wonderful hours of happy debugging?
~30000 cycles per frame, this number makes me uncomfortable every time I add a new line of code.
It is not really important is it assembler of C, because cc65 does not encourage using local stack variables anyway. I don't say I really like x86, but arbitrary stack access in 6502 would be nice... Or at least fast stack.
Anyway, from what I learnt and saw, regardless of the compiler/assembler the only sane way is to use global variables.
Pseudo code example
Code:
foo1():
use tmp1, tmp2
cal bar1():
use tmp3, tmp4():
cal foo2():
use tmp1
use tmp1 // !! And now I have garbled data in tmp1 !!
use tmp1, tmp2
cal bar1():
use tmp3, tmp4():
cal foo2():
use tmp1
use tmp1 // !! And now I have garbled data in tmp1 !!
So now I really have two options:
1. Fast and dangerous - be careful with using local variables, and do manual memory management, making sure that variables and registers will not be ruined
2. Slow and more comfortable - Use hardware stack, which is so-so for A register, but quite slow for anything else as it would require lda mem, pha, pla, sta mem for every variable
Am I correct? Should I take approach with hardware stack or rather forget about it and just try to be careful and spend wonderful hours of happy debugging?
~30000 cycles per frame, this number makes me uncomfortable every time I add a new line of code.