jefftranter / 6502

Code for the 6502 microprocessor, mostly for the Replica 1 computer.
298 stars 66 forks source link

Copy routines to handle overwrite are reversed #12

Closed w4jbm closed 3 years ago

w4jbm commented 3 years ago

In lines 554 through 562, there is logic that decides whether to call @okUp or @okayDown to handle a copy. This is designed to handle situations where the source and destination overlap.

The routine selected is actually backwards (@okayUp is called when @okayDown should be and visa versa). Pure speculation, but this may be because the intent of the routine names is easy to confuse.

@okayUp starts with the bottom of the Source address and copies to the Destination address by counting upward (by incrementing) the Source and Destination address as it goes. The confusing part (to me anyway, and it took me a while to figure out what was going on) is that this routine is appropriate when the Source address is above the Destination address (in other words, you are copying 'down' from a higher block of memory to a lower block).

In the current code, the call to @okayUp is made when the opposite is true--when the Source address is BELOW the Destination address. Or, put another way, when you are copying "up" in memory.

@okayDown calculates the size of the transfer and then starts copying from the top of the Source block to the top of the Destination block, counting downward (by decrementing). This will prevent overwriting the source when you are copying "up" from one block of memory to a block "higher" in memory.

I did copies such as C 0200 02FF 0280 and C 0300 03FF 0280 when testing and debugging. Unless you are copying a range that actually overlaps, either routine will work fine.

My code eventually was:

@okay1:
  LDA SH
  CMP DH
  BCC @okayDown             ; copy up
  BNE @okayUp               ; copy down
  LDA SL
  CMP DL
  BCC @okayDown
  BCS @okayUp
w4jbm commented 3 years ago

I should have mentioned this is in in jmon.s. Thanks!

jefftranter commented 3 years ago

Thanks, I have committed this fix.