General Tips
CMP/FLAGS/Branches
A construct like
bne overstep
bra somewhere
overstep
can be optimized to
beq somewhere
overstep
CMP
lda value
cmpa #0
beq somwhere
is the same as:
lda value
beq somewhere
—
lda value
cmpa #0
blt somwhere
is the same as:
lda value
bmi somewhere
—
lda value
cmpa #0
bge somwhere
is the same as:
lda value
bpl somwhere
Further example:
lda Joy2X
cmpa #0
blt left
bgt right
bra move_end
left:
is the same as:
lda Joy2X
beq move_end
bpl right
; left is „fall thru"
left:
Y / X!
Due to our mathematical background we LIKE to order coordinates like x, y in RAM that would be like:
bss
org $c880
xpos ds 1
ypos ds 1
but Vectrex expects always first y position THAN x positions, a definition like:
bss
org $c880
ypos ds 1
xpos ds 1
makes much more sense, since we than can load and transfer a set of coordinates very often in register “D” – like:
ldd ypos
Loops
Also loops, “naturally” if we would like a loop from 0 to 5 we
start from 0 and compare some variable if we reached 5.
In assembler that is not very performant, compare those two variants:
bss
org $c880
counter ds 1
code
lda #0
sta counter
loopstart
... do something
inc counter
lda counter
cmpa #5
blo loopstart
... loop done
with
bss
org $c880
counter ds 1
code
lda #5
sta counter
loopstart
... do something
dec counter
bne loopstart
... loop done
Struct
Compare these two codes and tell me what is better readable (and which you could explain 2 years after you coded it):
; move bullet
ldd 2,x ; add dx to x
addd 6,x
std 2,x
ldd 8,x ; dy affected by gravity
subd #3
std 8,x
addd 4,x ; add dy to y
std 4,x
...
with
; move bullet
ldd B_XPOS,x ; add dx to x
addd B_DX,x
std B_XPOS,x
ldd B_DY,x ; dy affected by gravity
subd #GRAVITY
std B_DY,x
addd B_YPOS,x ; add dy to y
std B_YPOS,x
...
The easiest way to do that is with the usage of a structure, like:
struct BULLET
ds B_DAMAGE, 2
ds B_XPOS, 2
ds B_YPOS, 2
ds B_DX, 2
ds B_DY, 2
end struct
Which defines all offsets “automatically” (also if you want to insert new variables – you can do that without changing ANY code).
Optimizations
LDB memoryLocation
is 2 cycles faster than
TST memoryLocation
If you have a free register, when you want to test something out – just load it!