Use cheap local labels for self-modifying code where possible. Fixes #89

This commit is contained in:
Joshua Bell 2018-11-24 11:17:46 -08:00
parent ae2938c58e
commit a93663b1bc
10 changed files with 97 additions and 94 deletions

View File

@ -152,20 +152,23 @@ well in the future.
## Self-modifying code ## Self-modifying code
* Add a label for the value being modified (byte or address) * Add a label for the value being modified (byte or address). Use
[cheap local labels](https://cc65.github.io/doc/ca65.html#ss6.5) via the
`@`-prefix where possible so make self-modification references more
visible.
```asm ```asm
sta jump_addr sta @jump_addr
stx jump_addr+1 stx @jump_addr+1
jump_addr := *+1 @jump_addr := *+1
jmp $0000 jmp $0000
``` ```
```asm ```asm
sty count sty @count
ldy #0 ldy #0
: sta table,y : sta table,y
iny iny
count := *+1 @count := *+1
cpy #count cpy #00
bne :- bne :-
``` ```

View File

@ -1182,13 +1182,13 @@ read: lda #0
;;; ============================================================ ;;; ============================================================
.proc read_file_page .proc read_file_page
copy16 read_params::data_buffer, store_addr copy16 read_params::data_buffer, @store_addr
lda #' ' ; fill buffer with spaces lda #' ' ; fill buffer with spaces
ldx #0 ldx #0
sta RAMWRTOFF sta RAMWRTOFF
store_addr := *+1 @store_addr := *+1
store: sta default_buffer,x ; self-modified store: sta default_buffer,x ; self-modified
inx inx
bne store bne store

View File

@ -2046,15 +2046,15 @@ monitor:
stax ptr stax ptr
ldy #0 ldy #0
lda (ptr),y lda (ptr),y
sta len sta @len
beq done beq done
loop: iny : iny
lda ($06),y lda ($06),y
ora #$80 ora #$80
jsr COUT jsr COUT
len := *+1 @len := *+1
cpy #0 ; self-modified cpy #0 ; self-modified
bne loop bne :-
done: rts done: rts
.endproc .endproc

View File

@ -3503,7 +3503,7 @@ LBDE1: sub16 event_xcoord, portmap::viewloc::xcoord, event_xcoord
rts rts
.proc LBE08 .proc LBE08
copy16 #$0800, LBE37 copy16 #$0800, addr
lda LBFC9 lda LBFC9
jsr LBF10 jsr LBF10
lda LBFCB lda LBFCB
@ -3520,9 +3520,9 @@ LBE27: lda LBE5C
bcs LBE34 bcs LBE34
sta PAGE2ON ; aux $2000-$3FFF sta PAGE2ON ; aux $2000-$3FFF
LBE34: lda ($06),y LBE34: lda ($06),y
LBE37 := * + 1 addr := *+1
sta dummy1234 sta dummy1234
inc16 LBE37 inc16 addr
lda LBE5C lda LBE5C
cmp LBFCC cmp LBFCC
bcs LBE4E bcs LBE4E
@ -3531,7 +3531,7 @@ LBE37 := * + 1
LBE4E: jsr LBF52 LBE4E: jsr LBF52
dex dex
bne LBE21 bne LBE21
ldax LBE37 ldax addr
rts rts
.byte 0 .byte 0
@ -3539,7 +3539,7 @@ LBE5C: .byte 0
.endproc .endproc
.proc LBE5D .proc LBE5D
copy16 #$800, LBEBC copy16 #$800, addr
ldx LBFCD ldx LBFCD
ldy LBFCE ldy LBFCE
lda #$FF lda #$FF
@ -3580,7 +3580,7 @@ LBEAE: lda LBF0B
bcs :+ bcs :+
sta PAGE2ON ; aux $2000-$3FFF sta PAGE2ON ; aux $2000-$3FFF
LBEBC := *+1 addr := *+1
: lda $0800 ; self-modified : lda $0800 ; self-modified
pha pha
@ -3607,7 +3607,7 @@ LBEDD: lda ($06),y
pha pha
LBEEB: pla LBEEB: pla
sta ($06),y sta ($06),y
inc16 LBEBC inc16 addr
lda LBF0B lda LBF0B
cmp LBFCC cmp LBFCC
bcs LBF03 bcs LBF03

View File

@ -748,16 +748,16 @@ L45C7: sta unit_num
and #$80 ; high bit is drive (0=D1, 1=D2) and #$80 ; high bit is drive (0=D1, 1=D2)
beq :+ beq :+
ldx #$21 ldx #$21
: stx bf_lo ; D1=$11, D2=$21 : stx @bf_lsb ; D1=$11, D2=$21
lda unit_num lda unit_num
and #$70 and #$70
lsr a lsr a
lsr a lsr a
lsr a lsr a
clc clc
adc bf_lo adc @bf_lsb
sta bf_lo sta @bf_lsb
bf_lo := *+1 @bf_lsb := *+1
lda $BF00 ; self-modified to $BFds lda $BF00 ; self-modified to $BFds
sta ptr+1 sta ptr+1
lda #0 ; Bug: assumes driver is at $XX00 ??? lda #0 ; Bug: assumes driver is at $XX00 ???
@ -1345,14 +1345,14 @@ L4AEA: jsr L4B5F
.endproc .endproc
.proc copy_LD3EE_str .proc copy_LD3EE_str
stax destptr stax @destptr
sta ALTZPOFF sta ALTZPOFF
lda LCBANK2 lda LCBANK2
lda LCBANK2 lda LCBANK2
ldx LD3EE ldx LD3EE
: lda LD3EE,x : lda LD3EE,x
destptr := *+1 @destptr := *+1
sta dummy1234,x sta dummy1234,x
dex dex
bpl :- bpl :-
@ -1364,14 +1364,14 @@ L4AEA: jsr L4B5F
.endproc .endproc
.proc copy_LD3AD_str .proc copy_LD3AD_str
stax destptr stax @destptr
sta ALTZPOFF sta ALTZPOFF
lda LCBANK2 lda LCBANK2
lda LCBANK2 lda LCBANK2
ldx LD3AD ldx LD3AD
: lda LD3AD,x : lda LD3AD,x
destptr := *+1 @destptr := *+1
sta dummy1234,x sta dummy1234,x
dex dex
bpl :- bpl :-
@ -7660,7 +7660,7 @@ ascii_digits:
adc ($06),y adc ($06),y
sta ($08),y sta ($08),y
lda ($06),y lda ($06),y
sta compare_y sta @compare_y
: inc L84D0 : inc L84D0
iny iny
lda ($06),y lda ($06),y
@ -7668,7 +7668,7 @@ ascii_digits:
ldy L84D0 ldy L84D0
sta ($08),y sta ($08),y
ldy L84CF ldy L84CF
compare_y := *+1 @compare_y := *+1
cpy #0 ; self-modified cpy #0 ; self-modified
bcc :- bcc :-
rts rts
@ -8447,8 +8447,8 @@ create_icon:
lsr a lsr a
lsr a lsr a
ora #$C0 ora #$C0
sta load_CxFB sta @load_CxFB
load_CxFB := *+2 @load_CxFB := *+2
lda $C7FB ; self-modified $CxFB lda $C7FB ; self-modified $CxFB
and #$01 ; $CxFB bit 1 = ProFile, apparently??? and #$01 ; $CxFB bit 1 = ProFile, apparently???
beq use_profile_icon beq use_profile_icon
@ -9098,14 +9098,14 @@ L9051: lda LEBFC
sta ($06),y sta ($06),y
dey dey
lda ($06),y lda ($06),y
sta L906D sta @compare
sta path_buf3,y sta path_buf3,y
L9066: iny : iny
lda ($06),y lda ($06),y
sta path_buf3,y sta path_buf3,y
L906D := *+1 @compare := *+1
cpy #$00 ; self-modified cpy #$00 ; self-modified
bne L9066 bne :-
ldy #$01 ldy #$01
lda #' ' lda #' '
sta ($06),y sta ($06),y
@ -9276,12 +9276,12 @@ L918D: .byte 0
beq do_str2 beq do_str2
;; Copy $8 (str1) ;; Copy $8 (str1)
sta len1 sta @len
: iny : iny
inx inx
lda (str1),y lda (str1),y
sta buf,x sta buf,x
len1 := *+1 @len := *+1
cpy #0 ; self-modified cpy #0 ; self-modified
bne :- bne :-
@ -9295,13 +9295,13 @@ do_str2:
ldy #0 ldy #0
lda (str2),y lda (str2),y
beq done beq done
sta len2 sta @len
iny iny
: iny : iny
inx inx
lda (str2),y lda (str2),y
sta buf,x sta buf,x
len2 := *+1 @len := *+1
cpy #0 ; self-modified cpy #0 ; self-modified
bne :- bne :-
@ -9366,19 +9366,19 @@ index: .byte 0
.proc smartport_eject .proc smartport_eject
ptr := $6 ptr := $6
sta compare sta @compare
ldy #0 ldy #0
loop: lda device_to_icon_map,y : lda device_to_icon_map,y
compare := *+1 @compare := *+1
cmp #0 cmp #0
beq found beq found
cpy DEVCNT cpy DEVCNT
beq exit beq exit
iny iny
bne loop bne :-
exit: rts exit: rts
found: lda DEVLST,y ; found: lda DEVLST,y ;
@ -9390,16 +9390,16 @@ found: lda DEVLST,y ;
and #$80 ; high bit is drive (0=D1, 1=D2) and #$80 ; high bit is drive (0=D1, 1=D2)
beq :+ beq :+
ldx #$21 ldx #$21
: stx bf_lo ; D1=$11, D2=$21 : stx @bf_lsb ; D1=$11, D2=$21
lda unit_num lda unit_num
and #$70 and #$70
lsr a lsr a
lsr a lsr a
lsr a lsr a
clc clc
adc bf_lo adc @bf_lsb
sta bf_lo sta @bf_lsb
bf_lo := *+1 @bf_lsb := *+1
lda $BF00 ; self-modified to $BFds lda $BF00 ; self-modified to $BFds
sta ptr+1 sta ptr+1
lda #0 ; Bug: assumes driver is at $XX00 ??? lda #0 ; Bug: assumes driver is at $XX00 ???
@ -11407,7 +11407,7 @@ dialog_param_addr:
tya tya
asl a asl a
tax tax
copy16 dialog_proc_table,x, jump_addr copy16 dialog_proc_table,x, @jump_addr
lda #0 lda #0
sta prompt_ip_flag sta prompt_ip_flag
@ -11425,7 +11425,7 @@ dialog_param_addr:
copy16 #rts1, jump_relay+1 copy16 #rts1, jump_relay+1
jsr set_cursor_pointer jsr set_cursor_pointer
jump_addr := *+1 @jump_addr := *+1
jmp dummy0000 ; self-modified jmp dummy0000 ; self-modified
.endproc .endproc
@ -14239,17 +14239,17 @@ trash_name: PASCAL_STRING " Trash "
and #$80 ; drive 2? and #$80 ; drive 2?
beq :+ beq :+
ldx #$21 ; LSB DEVADR for slot 1, drive 2 ldx #$21 ; LSB DEVADR for slot 1, drive 2
: stx addr_lsb : stx @addr_lsb
lda L0A02 ; unit_num (again) lda L0A02 ; unit_num (again)
and #$70 ; slot number and #$70 ; slot number
lsr a lsr a
lsr a lsr a
lsr a lsr a
clc clc
adc addr_lsb adc @addr_lsb
sta addr_lsb sta @addr_lsb
addr_lsb := *+1 @addr_lsb := *+1
lda MLI ; self-modified lda MLI ; self-modified
sta ptr+1 ; $Cn firmware address sta ptr+1 ; $Cn firmware address
@ -14707,8 +14707,8 @@ L0D7F: cmp #$0B
lsr a lsr a
lsr a lsr a
ora #$C0 ora #$C0
sta slot_msb sta @slot_msb
slot_msb := *+2 @slot_msb := *+2
lda $C7FB ; self-modified lda $C7FB ; self-modified
and #$01 and #$01
bne L0DA2 bne L0DA2
@ -14721,13 +14721,13 @@ L0DA9: ldax #str_unidisk_xy
L0DAD: stax $06 L0DAD: stax $06
ldy #$00 ldy #$00
lda ($06),y lda ($06),y
sta L0DBE sta @compare
L0DB8: iny : iny
lda ($06),y lda ($06),y
sta ($08),y sta ($08),y
L0DBE := *+1 @compare := *+1
cpy #0 cpy #0
bne L0DB8 bne :-
tay tay
L0DC2: pla L0DC2: pla
pha pha
@ -14749,8 +14749,8 @@ L0DC2: pla
lsr a lsr a
lsr a lsr a
ora #$C0 ora #$C0
sta L0DE3 sta @msb
L0DE3 := *+2 @msb := *+2
lda $C7FB ; self-modified lda $C7FB ; self-modified
and #$01 and #$01
bne L0DEC bne L0DEC
@ -14853,12 +14853,12 @@ prepare:
pha pha
asl a asl a
tax tax
copy16 slot_string_table,x, item_ptr copy16 slot_string_table,x, @item_ptr
ldx startup_menu_item_1 ; replace second-from-last char ldx startup_menu_item_1 ; replace second-from-last char
dex dex
lda char lda char
item_ptr := *+1 @item_ptr := *+1
sta dummy1234,x sta dummy1234,x
pla pla

View File

@ -1037,13 +1037,13 @@ get_srcbits_jmp_addr := *+1
: ldy src_width_bytes : ldy src_width_bytes
loop: :
load_addr := *+1 load_addr := *+1
lda $FFFF,y ; off-screen BMP will be patched here lda $FFFF,y ; off-screen BMP will be patched here
and #$7F and #$7F
sta bitmap_buffer,y sta bitmap_buffer,y
dey dey
bpl loop bpl :-
bmi shift_bits_clc_jmp bmi shift_bits_clc_jmp
.endproc .endproc

View File

@ -763,17 +763,17 @@ L0D26: sta L0D50
and #$80 and #$80
beq L0D34 beq L0D34
ldx #$21 ldx #$21
L0D34: stx load_addr L0D34: stx @load_addr
lda L0D50 lda L0D50
and #$70 and #$70
lsr a lsr a
lsr a lsr a
lsr a lsr a
clc clc
adc load_addr adc @load_addr
sta load_addr sta @load_addr
load_addr := * + 1 @load_addr := *+1
lda MLI ; self-modified lda MLI ; self-modified
sta $07 sta $07
lda #$00 lda #$00

View File

@ -1050,16 +1050,16 @@ L126F: sta L12C0
and #$80 and #$80
beq L1281 beq L1281
ldx #$21 ldx #$21
L1281: stx L1294 L1281: stx @lsb
lda L12C0 lda L12C0
and #$70 and #$70
lsr a lsr a
lsr a lsr a
lsr a lsr a
clc clc
adc L1294 adc @lsb
sta L1294 sta @lsb
L1294 := * + 1 @lsb := *+1
lda MLI lda MLI
sta $07 sta $07
lda #$00 lda #$00
@ -1093,16 +1093,16 @@ L12C1: sta L1306
and #$80 and #$80
beq L12D3 beq L12D3
ldx #$21 ldx #$21
L12D3: stx L12E6 L12D3: stx @lsb
lda L1306 lda L1306
and #$70 and #$70
lsr a lsr a
lsr a lsr a
lsr a lsr a
clc clc
adc L12E6 adc @lsb
sta L12E6 sta @lsb
L12E6 := * + 1 @lsb := *+1
lda MLI lda MLI
sta $07 sta $07
lda #$00 lda #$00
@ -1157,16 +1157,16 @@ L132C: ldy #0
and #$80 and #$80
beq L134D beq L134D
ldx #$21 ldx #$21
L134D: stx L1360 L134D: stx @lsb
lda L124A lda L124A
and #$70 and #$70
lsr a lsr a
lsr a lsr a
lsr a lsr a
clc clc
adc L1360 adc @lsb
sta L1360 sta @lsb
L1360 := * + 1 @lsb := *+1
lda MLI lda MLI
sta $06+1 sta $06+1
lda #$00 lda #$00

View File

@ -36,8 +36,8 @@ L9017: lda $0C00
rts rts
L903C: ldx #$01 L903C: ldx #$01
copy16 selector_menu_addr, load copy16 selector_menu_addr, @load
load := *+1 @load := *+1
lda dummy1234 lda dummy1234
cmp #$0D cmp #$0D
bcc L9052 bcc L9052
@ -1450,31 +1450,31 @@ L9DED: sta ALTZPOFF
txa txa
rts rts
L9E05: stax L9E1B L9E05: stax @addr
sta ALTZPOFF sta ALTZPOFF
lda LCBANK2 lda LCBANK2
lda LCBANK2 lda LCBANK2
ldx LD3EE ldx LD3EE
L9E17: lda LD3EE,x : lda LD3EE,x
L9E1B := *+1 @addr := *+1
sta dummy1234,x sta dummy1234,x
dex dex
bpl L9E17 bpl :-
sta ALTZPON sta ALTZPON
lda LCBANK1 lda LCBANK1
lda LCBANK1 lda LCBANK1
rts rts
L9E2A: stax L9E40 L9E2A: stax @addr
sta ALTZPOFF sta ALTZPOFF
lda LCBANK2 lda LCBANK2
lda LCBANK2 lda LCBANK2
ldx LD3AD ldx LD3AD
L9E3C: lda LD3AD,x : lda LD3AD,x
L9E40 := *+1 @addr := *+1
sta dummy1234,x sta dummy1234,x
dex dex
bpl L9E3C bpl :-
sta ALTZPON sta ALTZPON
lda LCBANK1 lda LCBANK1
lda LCBANK1 lda LCBANK1

View File

@ -50,11 +50,11 @@ routine_table: .addr $7000, $7000, $7000
pla pla
asl a asl a
tax tax
copy16 routine_table,x, jump copy16 routine_table,x, @jump
ldy stash_y ldy stash_y
ldx stash_x ldx stash_x
jump := * + 1 @jump := *+1
jmp dummy1234 jmp dummy1234
stash_x: .byte 0 stash_x: .byte 0