When you're done drawing to the buffer, call the routine and it will copy from the nice linear layout to the nasty cell layout. Here's the BASIC prototype:
SR,DE = SRC AND DEST LOCATIONS
R=ROW,C=COL,B=BYTE 0-7
You can see the relationship between two bytes in each buffer from the copy in line 30. Now, this implementation is slow and pretty useless because if you want a linear buffer, it's probably because you want to access the bitmap screen quickly. Enter Assembly. :-)
I always do a pseudo-code hybrid between BASIC and Assembly first:
Initialize dest and src
Set R loop to #rows
Set C loop to #cols
Set B loop to 0
Copy (src) to (dest)
src=src+40
Inc dest
Loop B up to 7
src=src-319 (C+B*40 when B=B-8 and C=C+1)
No math on dest is necessary because (C*8+B) is continuous as C is looped outside of B's loop from 0 to 7
Loop C down to 0
src=src+320
dest=dest+320
Loop R down to 0
No multiplication whatsoever. We should probably also unroll the inner loop. Heck, we don't even need any tables! Cool...I did this much more complicatedly last time. So let's go on to the final run of assembly.
;Initialize dest and src (and .X for indirect zp accesses)
lda #buf1
sta src+1
lda #buf2
sta dest+1
ldx #$00
;Set up loops
lda #24 ;rows
sta R
loopr:
lda #40 ;cols
sta C
loopc:
;(8 times, unrolled) Copy,src=src+40,inc dest
;We'll use .Y for keeping track of the 8 "inc dest"s.
;1)
ldy #$00
lda (src,x)
sta (dest),y
clc
lda src
adc #40
sta src
bcc *+2
inc src+1
;2)
iny
lda (src,x)
sta (dest),y
clc
lda src
adc #40
sta src
bcc *+2
inc src+1
;3)...
;4)...
;5)...
;6)...
;7)...
;8)
iny
lda (src,x)
sta (dest),y
;src=src-319 (actually src=src-279 because we didn't do the last +40)
sec
lda src
sbc #$17
sta src
lda src+1
sbc #$01
sta src+1
;Reflect all of our "iny"s back into dest
clc
lda dest
adc #$08
sta dest
bcc *+2
inc dest+1
;Loop C down to 0
dec C
bpl loopc
;src=src+320,dest=dest+320
clc
lda src
adc #$40
sta src
lda src+1
adc #$01
sta src+1
lda dest
adc #$40
sta dest
lda dest+1
adc #$01
sta dest+1
;Loop R down to 0
dec R
bpl loopr
rts
Hmm... some more room for optimization remains. Be back later!
All pages were generated with a text editor.
All images (except counter art and host ads) are generated, owned and (c) by me.