joriszwart.nl

I code dreams™

Tetris inner loop MOS6510 / C=64

A language a year keeps the doc away C=64 Closure Code proud MOS6510 枯淡

Introduction

Once in a century code can make me really proud. Especially when it is concise and no byte can be stripped off. The last time was in 1991. Major things still to come in the third millenium.

            ldx #3                  ; 2
draw        lda chars,x             ; 3
            ldy blocks,x            ; 3
            sta (screenptr),y       ; 2
colorit     lda #color              ; 2
            sta (colorramptr),y     ; 2
            dex                     ; 2
            bpl draw                ; 2
            ...                     ; total: 18 bytes
blocks      byte 0, 1, 41, 42, ...
chars       byte 192, 238, 237, 192, ...
Inner loop drawblock

The lookups to chars and blocks are initialized with an offset to a block/rotation with SMC. The same for color.

; block in x (0..6) rotation in y (0..3)
calcblock   stx colorit+1
            tya
            asl
            asl
            asl
            asl
            stx draw+1
            tax
            stx draw+4
            ...
; TODO this code needs testing and review
Pre-calculate block offset
Tetris block
Tetris block

The screen matrix of the VIC-II has 25 lines of 40 characters each with a linear memory layout.

012345...
404142434445...
808182838485...
120121122123124125...
VIC-II screen matrix

In C it would like this:

char *blocks = { 0, 1, 41, 42, ... };
char *chars = { 192, 238, 237, 192, ... };

for(int x_reg = 4; x_reg--;) {
    char y_reg = blocks[x_reg];
    screen[y_reg] = chars[x_reg];
    color_ram[y_reg] = color;
}
C-version draw block

Of course something will prove that at least one byte can be stripped off...