irmen / prog8

high level programming language and compiler targeting 6502 machines such as the C-64 and CommanderX16
https://prog8.readthedocs.io/
Other
152 stars 18 forks source link

Incorrect 6502 asm generated with indexing a uword pointer using array syntax #137

Closed reltham closed 6 months ago

reltham commented 6 months ago

This line of code: uword sequence_offset = &curr_sequence[sequence_curr_step] Where curr_sequence is a uword, and sequence_curr_step is a ubyte, and all of these end up in ZP. produces this asm output:

.2da2   a4 73           ldy  p8b_Sequencer.p8v_curr_sequence+1
.2da4   a5 72           lda  p8b_Sequencer.p8v_curr_sequence
.2da6   48          pha
.2da7   a5 6c           lda  p8b_Sequencer.p8v_sequence_curr_step
.2da9   85 7b           sta  P8ZP_SCRATCH_REG
.2dab   68          pla
.2dac   18          clc
.2dad   65 7b           adc  P8ZP_SCRATCH_REG
.2daf   d0 01           bne  +
.2db1   c8          iny
.2db2           +
.2db2   85 76           sta  p8v_sequence_offset
.2db4   84 77           sty  p8v_sequence_offset+1

When the address in curr_sequence is close to a page boundary such that sequence_curr_step causes it to wrap the lower byte, it fails to increment the upper byte, because bne is incorrect here. it should be bcc.
In my case curr_sequence is $2CF9 and sequence_curr_step is $0D, and the result it ends up with is $2C06 instead of $2D06 (which is the correct value).
I was able to hand modify the code in memory using box16 while debugging, and changing the bne to bcc made it work correctly. I am using 10.3 release.

irmen commented 6 months ago

Confirmed as bug in the asm gen