The routine used to copy the ship instance to the zero-page (and to copy it back later) could do with some slight optimisation:

This is the code in the BBC version;

Code: Select all

```
LDY #NI%-1 \ There are NI% bytes in the INWK workspace, so set a
\ counter in Y so we can loop through them
.MAL2
LDA (INF),Y \ Load the Y-th byte of INF and store it in the Y-th
STA INWK,Y \ byte of INWK
DEY \ Decrement the loop counter
BPL MAL2 \ Loop back for the next byte, ending when we have
\ copied the last byte from INF to INWK
```

Here's the same [unoptimised] code in the C64 disassembly to use as reference for the optimised version;

Code: Select all

```
; ; bytes/tally cycles/tally
;---------------------------------------;-------------------------------
ldy # .sizeof(PolyObject)-1 ; +2 2 +2 2
: lda [ZP_POLYOBJ_ADDR], y ; +2 . +5 .
sta ZP_POLYOBJ, y ; +2 . +5 .
dey ; +1 . +2 .
bpl :- ; +2 7 +3 (15)
; loop: ; -1 *37=555
;---------------------------------------;-------------------------------
; total: ; 9 556
```

Here's a slightly optimised version:

Code: Select all

```
; ; bytes/tally cycles/tally
;---------------------------------------;-------------------------------
lda ZP_POLYOBJ_ADDR_LO ; +2 . +3 .
sta @copy+1 ; +3 . +4 .
lda ZP_POLYOBJ_ADDR_HI ; +2 . +3 .
sta @copy+2 ; +3 10 +4 14
ldx # .sizeof(PolyObject)-1 ; +2 2 +2 2
;---------------------------------------;-------------------------------
@copy: lda $8888, x ; +3 . +4 .
sta ZP_POLYOBJ, x ; +2 . +4 .
dex ; +1 . +2 .
bpl @copy ; +2 8 +3 (13)
; loop: ; -1 *37=481
;---------------------------------------;-------------------------------
; total: ; 20 496
```

This saves just 60 cycles, but we can double that for the same optimisation applied to the routine to copy the zero-page back to the ship instance, equals 120 cycles. This saving applies for each ship in play, so for a busy screen with 10 ships floating about that would be 1'200 cycles saved!

(You could also completely unroll the loop and that takes just 370 cycles, one way -- at a cost of 186 bytes!)

**EDIT:** Note that the C64 has 37 bytes in an instance, not 36 like the BBC!