MartinC wrote:I'll have a rethink and try an approach without x and y registers
You might have misunderstood chysn's remark. Trying to do the task at hand (addressing into screen), or pretty much anything non-trivial in 65xx assembly involves informed use of the accumulator, and the X, Y index-registers as
scratch pads. All relevant data processing uses the accumulator; address indexing and counting are done using the X and Y registers.
What does not work is treating any of them as 'variables' to hold values for any prolonged time. You will be using memory for that, preferably in the zero page if pointers into memory are involved. A similar question was posed some time ago, where the poster asked for a
non-existent address mode to express his program ideas.
That being said, taking X and Y out of the tool set for a solution inevitably leads to self-modifying code. That type of code has its own pitfalls, bad maintainability being one of them. And not necessary here, either.
You find examples how to address into screen memory in the thread '
A sample programming session in VICMON':
- "BALLPING" uses co-ordinate addressing and derives the screen address of the ball from tables, while the ball co-ordinate values are held in zero page memory,
- "KILLER COMET" keeps running addresses for meteor and rocket into screen memory, and updates these addresses as the objects are moved. +/- 1 moves an object horizontally, +/- 22 moves an object vertically (given the standard screen width of 22 characters/line).
Both methods can be combined in case of need. What you shouldn't do is, just store the running screen address, and then trying to calculate x- and y-co-ordinates from that (for whatever reason) - this needs a division which is very expensive on the 65xx.
Finally, if you are going to initialise the screen from some other buffer in memory, you do not need to consider a co-ordinate system "in the background". Rather, you use the indexed address modes of the 6502 as follows:
Code: Select all
LDX #0
.loop
LDA buffer,X
STA screen,X
LDA buffer+256,X
STA screen+256,X
INX
BNE loop
buffer and screen are symbols supposed to be defined somewhere else in the source. This is a simple two-fold unrolled loop that copies 512 bytes from the buffer to the screen. Counting over whole 256 byte pages keeps this code short and simple, and in most cases it should not matter that the 6 bytes immediately after the visible screen are also being written to.