Overview and features
VIC-X is a graphics and memory extension for FPGA-based Commodore 64s. It is one new block beside the stock machine: it reads only its own video memory, takes the VIC-II's finished picture as one input to its mixer, and hands the result to the video output. The VIC-II, SID and CIAs are never altered, and a program that does not unlock VIC-X sees a stock C64.
- 256-color palette of 24-bit RGB entries. The original 16 colors can be redefined too.
- Two display layers, each a tile layer or a bitmap, in 4-bit or 8-bit color, with hardware scrolling.
- 128-sprite engine, separate from the VIC-II's own eight sprites: 8 to 64 pixels each way, up to 256 colors, flips, collision groups.
- 128 KB of video memory (VRAM) reached through two auto-stepping data ports.
- 16 MB of extended memory (XRAM), shared with the RAM Expansion Unit, banked into the CPU's address space through two 8 KB windows.
- DMA engine and blitter: copy, fill and masked copy between C64 RAM, VRAM and XRAM, in straight runs or rectangles, with optional expand, remap, scaled copy, mirroring and display lists.
- Wide window: layers and sprites can extend into the border.
- No cost to the 6510: the new modes add no bad lines and steal no sprite cycles.
Technical specifications
| Item | Specification |
|---|---|
| Palette | 256 entries, 24-bit RGB (8 bits per channel). Entries 0-15 reset to the C64 colors, 16-255 to black. |
| Layers | 2, each independently a tile layer or a bitmap |
| Tile layers | 8×8 or 16×16 tiles, 4-bit (16 colors from 16 palette banks) or 8-bit (256 colors), up to 1024 tiles, flip X and Y per map entry |
| Tile maps | 32, 64, 128 or 256 tiles in each direction; 12-bit scroll X and Y that wrap around the map |
| Bitmaps | 320×200 at 4 bits (32,000 bytes) or 8 bits (64,000 bytes); line stride up to 1024 bytes; optional double-width pixels (160 across) |
| Sprites | 128 table entries (64 on a smaller build; reported by CAP_SPR); 8, 16, 32 or 64 pixels wide and high; 4-bit or 8-bit pixels; flip X and Y; double width; 10-bit positions |
| Sprites per line | 32 sprites (16 on a smaller build; reported by CAP_SPRLINE) and 640 sprite pixels |
| Collisions | Sprite to sprite, by 4 group bits and a hit mask; up to 127 pairs listed per frame |
| VRAM | 128 KB target, 64 KB minimum; the real size is in CAP_VRAM |
| XRAM | Up to 16 MB (the REU's memory); size in CAP_XRAM |
| CPU windows | 2 windows of 8 KB, each mapped to one of 2048 XRAM pages, at $2000, $4000, $6000, $8000, $A000 or $E000 |
| DMA | 4 descriptors that can chain, or a list of descriptors in VRAM; copy, fill, masked copy; optional expand, remap, scaled copy, direction bits |
| DMA speed | Target of at least 8 MB/s for VRAM fills and copies; transfers touching C64 RAM run at one byte per CPU cycle |
| Registers | 32 direct registers at $D040-$D05F, plus a register space reached through the VRAM ports |
| Interrupts | Sprite collision, DMA done, start of frame; shares the VIC-II IRQ line |
| Target machines | FPGA-based Commodore 64s, with the MiSTer as the first target and the Commodore 77 as the dream. On its own the full block takes 29% of the LUTs of the Commodore 64 Ultimate's XC7A50T; whether it fits beside the rest of that machine is not known. See FPGA fit. Running today in a fork of the VICE emulator and on MiSTer. |
Compatibility and unlocking
The machine powers up as a stock C64. VIC-X registers exist only after a three-byte unlock that no existing program writes by accident.
- Unlock: write
$56,$58,$21(the charactersVX!) to$D06Fas three consecutive writes to that address. Any other write to$D000-$D3FFin between cancels the sequence. Writes to RAM and the stack do not. - Relock: write
$00to$D06F. This only hides the registers:CTRL, the palette and the ports keep their values, and layers stay on, until a reset. - While locked,
$D040-$D06Fmirror the VIC-II exactly as on real hardware. - While unlocked, only
$D040-$D06Fchange meaning.$D000-$D03Fand every other mirror keep stock behaviour. - Unlocking shows nothing. A program turns on each layer, the sprite engine and palette remapping individually through
CTRL. - Every reset locks VIC-X, hides all layers and the sprite engine, and restores the palette. VRAM keeps its contents.
Detecting VIC-X
Unlock, then read the ID. On a stock C64 these two addresses are mirrors of sprite 0's X and Y registers, so both bytes must match.
lda #$56 ; unlock: "V" "X" "!" to $D06F
sta $d06f
lda #$58
sta $d06f
lda #$21
sta $d06f
lda $d040 ; ID reads "VX" only on VIC-X
cmp #$56
bne novicx
lda $d041
cmp #$58
bne novicx
After that, read the capability registers and adapt. Software should check them rather than assume 128 KB of VRAM, 128 sprites or a particular blitter operation.
Using VIC-X from BASIC
Everything in VIC-X is reachable with POKE and PEEK. The direct registers sit at 53312-53343, and the unlock register is 53359.
Unlocking
Put the three unlock POKEs together on one line. BASIC does not write to the VIC-II area between them, so the sequence is not broken. Then check both ID bytes:
20 POKE 53359,86:POKE 53359,88:POKE 53359,33
30 IF PEEK(53312)<>86 OR PEEK(53313)<>88 THEN PRINT "NO VIC-X HERE":END
The data ports (53327 and 53331) step their address after every read or write. BASIC's POKE reads the address once before it writes to it, which is how the 6502's store instruction works. With auto-step switched on, each POKE therefore moves the port two places and your data lands on every other byte.
The fix: from BASIC, use step 0 and set the address for every byte you write. The subroutine below does exactly that. PEEK reads only once, so reading a run of bytes with step 1 works as you would expect. Assembly code using STA $D04F is not affected.
A port-write subroutine
Set A to a port address and V to a value, then GOSUB 1000. It writes the byte and adds 1 to A, so a run of bytes is a loop of GOSUBs. Port addresses below 131072 ($20000) are VRAM; from 131072 up they are the register space. Every BASIC example on this page ends with these four lines.
1000 REM --- WRITE V AT PORT ADDRESS A, THEN A=A+1
1010 H=INT(A/65536):M=INT((A-H*65536)/256)
1020 POKE 53324,A-H*65536-M*256:POKE 53325,M:POKE 53326,H
1030 POKE 53327,V:A=A+1:RETURN
When only the low byte of the address changes, as when moving sprites, set the middle and high bytes once and then POKE just 53324 and 53327. BASIC is too slow to fill large areas a byte at a time, so let the blitter do fills and copies: describe the job in a descriptor and start it.
BASIC address reference
| Decimal | Hex | Register | Use from BASIC |
|---|---|---|---|
| 53359 | $D06F | UNLOCK | POKE 53359,86:POKE 53359,88:POKE 53359,33 unlocks; POKE 53359,0 relocks |
| 53312, 53313 | $D040-41 | ID | Read 86 and 88 when VIC-X is present and unlocked |
| 53320 | $D048 | CTRL | Add up: 1 layer 0, 2 layer 1, 4 sprites, 8 remap the 16 colors, 16 transparent background |
| 53323 | $D04B | FRAME | Changes once a frame |
| 53324-53326 | $D04C-4E | P0_ADDR | Port 0 address: low, middle, high. Keep the high byte to 0, 1 or 2 for step 0. |
| 53327 | $D04F | P0_DATA | Read or write the byte at port 0 |
| 53332-53334 | $D054-56 | W0_PAGE, W0_SLOT | XRAM window 0: page low, page high, slot (add 128 to switch it on) |
| 53338 | $D05A | DMA_CMD | POKE 0-3 to start a descriptor; PEEK is over 127 while busy |
To switch everything off again without a reset: POKE 53320,0:POKE 53359,0.
Using VIC-X from assembly
Two include files for the ca65 assembler (part of cc65) name every register, bit and step code, and provide macros for the common jobs. Each example builds with one command:
cl65 -t c64 -C c64-asm.cfg -u __EXEHDR__ --asm-include-dir ../include -o program.prg program.s
| File | Contents |
|---|---|
| vicx.inc | Register names, bits, step codes, and the macros VICX_UNLOCK_SEQ, VICX_LOCK_SEQ and VICX_P0_SET addr, step |
| vicxlib.inc | Helper macros: SET_PAL, LAYER_SET, SPRITE, DMA_DESC, DMA_WAIT, P0_FILL, P0_FILL16, POKE16, PEEK_AT |
Run the unlock with interrupts disabled, so no interrupt handler writes to $D000-$D3FF in the middle of it. A store such as STA $D04F touches a data port exactly once, so auto-stepping works as described and whole tables can be streamed through a port.
Examples
Each example is written twice, in BASIC and in assembly, to do the same thing. Pick a language on any example and the whole page switches to it. All twelve programs were run in the VIC-X emulator, and the picture beside each listing is that program's own output.
{CLR} in a BASIC listing means the clear-screen key, SHIFT and CLR/HOME, typed inside the quotes.
Detect VIC-X
Unlock, check the ID and print what this machine provides. On a stock C64 both versions stop quietly, having changed nothing.
10 REM UNLOCK VIC-X AND REPORT WHAT THIS MACHINE HAS
20 POKE 53359,86:POKE 53359,88:POKE 53359,33
30 IF PEEK(53312)<>86 OR PEEK(53313)<>88 THEN PRINT "NO VIC-X HERE":END
40 PRINT "VIC-X FOUND, SPEC VERSION";PEEK(53314)
50 PRINT "VRAM:";PEEK(53315)*8;"KB"
60 PRINT "XRAM:";PEEK(53316);"MB"
70 PRINT "SPRITES:";PEEK(53317);" PER LINE:";PEEK(53318)
80 PRINT "FEATURES:";PEEK(53319);" BLITTER:";PEEK(53339)
Files: 01-detect.bas · 01-detect-basic.prg
; 01-detect.s - unlock VIC-X and report what this machine has
;
; cl65 -t c64 -C c64-asm.cfg -u __EXEHDR__ --asm-include-dir ../include -o 01-detect.prg 01-detect.s
.include "vicx.inc"
STROUT = $AB1E ; BASIC ROM: print the text at A (low), Y (high)
LINPRT = $BDCD ; BASIC ROM: print the number in X (low), A (high)
.macro PRINT text
lda #<(text)
ldy #>(text)
jsr STROUT
.endmacro
.macro NUMBER register ; print one register as a decimal number
ldx register
lda #0
jsr LINPRT
.endmacro
.code
sei ; nothing may write $D000-$D3FF mid-unlock
VICX_UNLOCK_SEQ
cli
lda VICX_ID0 ; "VX" only reads back on VIC-X
cmp #$56
bne novicx
lda VICX_ID1
cmp #$58
beq found
novicx: PRINT t_none
rts
found: PRINT t_found
NUMBER VICX_VERSION
PRINT t_vram
lda VICX_CAP_VRAM ; 8 KB units: multiply by 8
ldy #0
sty high
asl
rol high
asl
rol high
asl
rol high
tax
lda high
jsr LINPRT
PRINT t_xram
NUMBER VICX_CAP_XRAM
PRINT t_spr
NUMBER VICX_CAP_SPR
PRINT t_line
NUMBER VICX_CAP_SPRLINE
PRINT t_feat
NUMBER VICX_CAP_FEAT
PRINT t_blit
NUMBER VICX_CAP_BLIT
lda #13
jmp $FFD2 ; a final new line
high: .byte 0
t_none: .byte "no vic-x here", 13, 0
t_found: .byte "vic-x found, spec version ", 0
t_vram: .byte 13, "vram: ", 0
t_xram: .byte " kb", 13, "xram: ", 0
t_spr: .byte " mb", 13, "sprites: ", 0
t_line: .byte " per line: ", 0
t_feat: .byte 13, "features: ", 0
t_blit: .byte " blitter: ", 0
The numbers are printed with the BASIC ROM's own number routine, which keeps the listing short.
Files: 01-detect.s · 01-detect-asm.prg
Any sixteen colors
With CTRL bit 3 set, VIC-II color n is shown through palette entry n. Existing text and graphics keep their color numbers and get new colors. This changes the background blue to dark green and the light blue to amber.
10 REM ANY SIXTEEN COLORS: REDEFINE THE C64 PALETTE
20 POKE 53359,86:POKE 53359,88:POKE 53359,33
30 IF PEEK(53312)<>86 OR PEEK(53313)<>88 THEN PRINT "NO VIC-X HERE":END
40 POKE 53320,8:REM CTRL BIT 3: VIC-II COLORS GO THROUGH THE PALETTE
50 REM PALETTE ENTRY N IS 4 BYTES (R,G,B,SPARE) AT $20000+4*N = 131072+4*N
60 C=6:R=0:G=56:B=40:GOSUB 100:REM BACKGROUND BLUE BECOMES DARK GREEN
70 C=14:R=255:G=200:B=64:GOSUB 100:REM LIGHT BLUE BECOMES AMBER
80 PRINT "THE SAME 16 COLOR NUMBERS, NEW COLORS"
90 END
100 A=131072+4*C:V=R:GOSUB 1000:V=G:GOSUB 1000:V=B:GOSUB 1000:RETURN
1000 REM --- WRITE V AT PORT ADDRESS A, THEN A=A+1
1010 H=INT(A/65536):M=INT((A-H*65536)/256)
1020 POKE 53324,A-H*65536-M*256:POKE 53325,M:POKE 53326,H
1030 POKE 53327,V:A=A+1:RETURN
Files: 02-colors.bas · 02-colors-basic.prg
; 02-colors.s - any sixteen colors: redefine the C64 palette
;
; cl65 -t c64 -C c64-asm.cfg -u __EXEHDR__ --asm-include-dir ../include -o 02-colors.prg 02-colors.s
.include "vicx.inc"
.include "vicxlib.inc"
STROUT = $AB1E ; BASIC ROM: print the text at A (low), Y (high)
.code
sei
VICX_UNLOCK_SEQ
cli
lda VICX_ID0
cmp #$56
bne novicx
lda VICX_ID1
cmp #$58
beq found
novicx: rts
found: lda #VICX_CTRL_PALMAP ; VIC-II colors go through the palette
sta VICX_CTRL
; Palette entry n is 4 bytes (R, G, B, spare) at $20000 + 4 * n.
SET_PAL 6, 0, 56, 40 ; background blue becomes dark green
SET_PAL 14, 255, 200, 64 ; light blue becomes amber
lda #<text
ldy #>text
jmp STROUT
text: .byte "the same 16 color numbers, new colors", 13, 0
Files: 02-colors.s · 02-colors-asm.prg
A 256-color bitmap drawn by the blitter
Four blitter descriptors are chained, so one write to DMA_CMD clears the bitmap and draws three rectangles. A diagonal line is then plotted pixel by pixel through a data port. The layer sits behind the normal text screen: CTRL bits 0 and 4 switch layer 0 on and make the text background transparent.
10 REM A 256-COLOR BITMAP BEHIND THE TEXT SCREEN, DRAWN BY THE BLITTER
20 POKE 53359,86:POKE 53359,88:POKE 53359,33
30 IF PEEK(53312)<>86 OR PEEK(53313)<>88 THEN PRINT "NO VIC-X HERE":END
40 PRINT "{CLR}SETTING UP..."
50 REM --- PALETTE ENTRIES 16-20 AT $20040: 4 BYTES EACH (R,G,B,SPARE)
60 A=131072+4*16
70 FOR I=0 TO 4:FOR J=0 TO 2:READ V:GOSUB 1000:NEXT:A=A+1:NEXT
80 DATA 16,16,48, 228,60,40, 250,212,64, 120,178,238, 255,255,255
90 REM --- FOUR BLITTER DESCRIPTORS AT $20880, 32 BYTES APART, CHAINED:
100 REM CLEAR THE BITMAP, THEN THREE RECTANGLES
110 FOR N=0 TO 3:A=133248+32*N:FOR I=0 TO 20:READ V:GOSUB 1000:NEXT:NEXT
120 DATA 1,4, 0,0,0,0, 0,0,0,0, 0,250, 0,0, 0,0, 0,0, 16,0,1
130 DATA 1,20, 0,0,0,0, 40,50,0,0, 240,0, 120,0, 0,0, 64,1, 17,0,2
140 DATA 1,20, 0,0,0,0, 80,75,0,0, 160,0, 80,0, 0,0, 64,1, 18,0,3
150 DATA 1,20, 0,0,0,0, 120,100,0,0, 80,0, 40,0, 0,0, 64,1, 19,0,255
160 POKE 53338,0:REM START AT DESCRIPTOR 0
170 IF PEEK(53338)>127 THEN 170:REM WAIT WHILE THE BLITTER IS BUSY
180 REM --- A DIAGONAL LINE, PIXEL BY PIXEL: ADDRESS = Y*320+X
190 FOR I=0 TO 199:A=60+I*321:V=20:GOSUB 1000:NEXT
200 REM --- LAYER 0 AT $20800: 8-BIT BITMAP AT VRAM 0, 320 BYTES A LINE
210 A=133120:FOR I=0 TO 14:READ V:GOSUB 1000:NEXT
220 DATA 3,0, 0,0,0, 0,0,0, 0,0, 0,0, 64,1, 0
230 REM --- LAYER 0 ON, AND LET IT SHOW THROUGH THE TEXT BACKGROUND
240 POKE 53320,17
250 PRINT "{CLR}TEXT ON TOP OF 256 COLORS"
260 END
1000 REM --- WRITE V AT PORT ADDRESS A, THEN A=A+1
1010 H=INT(A/65536):M=INT((A-H*65536)/256)
1020 POKE 53324,A-H*65536-M*256:POKE 53325,M:POKE 53326,H
1030 POKE 53327,V:A=A+1:RETURN
Setting up takes a little while in BASIC, most of it plotting the 200 pixels of the line one at a time. The blitter does its part almost at once.
Files: 03-bitmap.bas · 03-bitmap-basic.prg
; 03-bitmap.s - a 256-color bitmap behind the text screen, drawn by the blitter
;
; cl65 -t c64 -C c64-asm.cfg -u __EXEHDR__ --asm-include-dir ../include -o 03-bitmap.prg 03-bitmap.s
.include "vicx.inc"
.include "vicxlib.inc"
CLRSCR = $E544 ; KERNAL: clear the text screen
STROUT = $AB1E ; BASIC ROM: print the text at A (low), Y (high)
addr = $FB ; three free zero-page bytes
.code
sei ; nothing may write $D000-$D3FF mid-unlock
VICX_UNLOCK_SEQ
cli
lda VICX_ID0 ; "VX" only reads back on VIC-X
cmp #$56
bne novicx
lda VICX_ID1
cmp #$58
beq found
novicx: rts ; stock C64: nothing has changed
found: SET_PAL 16, 16, 16, 48 ; five colors of our own
SET_PAL 17, 228, 60, 40
SET_PAL 18, 250, 212, 64
SET_PAL 19, 120, 178, 238
SET_PAL 20, 255, 255, 255
; Four chained descriptors: clear the bitmap, then three rectangles.
; n operation flags src dst len rows sstr dstr fill tr next
DMA_DESC 0, VICX_DMA_FILL, VICX_DMA_DST_VRAM, 0, 0, 64000, 0, 0, 0, 16, 0, 1
DMA_DESC 1, VICX_DMA_FILL, VICX_DMA_DST_VRAM | VICX_DMA_2D, 0, 40*320 + 40, 240, 120, 0, 320, 17, 0, 2
DMA_DESC 2, VICX_DMA_FILL, VICX_DMA_DST_VRAM | VICX_DMA_2D, 0, 60*320 + 80, 160, 80, 0, 320, 18, 0, 3
DMA_DESC 3, VICX_DMA_FILL, VICX_DMA_DST_VRAM | VICX_DMA_2D, 0, 80*320 + 120, 80, 40, 0, 320, 19, 0, VICX_DMA_NONE
lda #0
sta VICX_DMA_CMD ; start at descriptor 0
DMA_WAIT ; VRAM transfers run in the background
; A diagonal line, pixel by pixel: address = y * 320 + x.
lda #60
sta addr
lda #0
sta addr+1
sta addr+2
ldx #200
line: lda addr
sta VICX_P0_ADDRL
lda addr+1
sta VICX_P0_ADDRM
lda addr+2 ; no step bits: step 0
sta VICX_P0_ADDRH
lda #20
sta VICX_P0_DATA
clc ; one line down and one pixel right
lda addr
adc #<321
sta addr
lda addr+1
adc #>321
sta addr+1
bcc :+
inc addr+2
: dex
bne line
; Layer 0: an 8-bit bitmap at VRAM 0, 320 bytes per line.
LAYER_SET VICX_LAYER0, VICX_LMODE_BITMAP8, 0, 0, 0, 0, 0, 320, 0
; Layer on, and make the VIC-II background transparent so it shows.
lda #VICX_CTRL_L0 | VICX_CTRL_BGTRANS
sta VICX_CTRL
jsr CLRSCR
lda #<text
ldy #>text
jmp STROUT ; back to BASIC, with the layer behind the text
text: .byte "text on top of 256 colors", 13, 0
Files: 03-bitmap.s · 03-bitmap-asm.prg
A scrolling tile layer
Two 8×8 tiles and a 32×32 map make a chequerboard, which then scrolls diagonally. Scrolling is two register writes a frame and no copying, whatever the size of the map.
10 REM A SCROLLING TILE LAYER: TWO REGISTER WRITES A FRAME, NO COPYING
20 POKE 53359,86:POKE 53359,88:POKE 53359,33
30 IF PEEK(53312)<>86 OR PEEK(53313)<>88 THEN PRINT "NO VIC-X HERE":END
40 PRINT "{CLR}";
50 REM --- PALETTE ENTRIES 16-18
60 A=131072+4*16
70 FOR I=0 TO 2:FOR J=0 TO 2:READ V:GOSUB 1000:NEXT:A=A+1:NEXT
80 DATA 30,40,110, 70,90,190, 250,212,64
90 REM --- TILES ARE 64 BYTES EACH FROM VRAM $4000. TILE 0 STAYS EMPTY,
100 REM TILE 1 IS A FRAMED BLOCK, TILE 2 A PLAIN ONE
110 A=16384+64
120 FOR I=0 TO 63:V=17:IF I<8 OR (I AND 7)=0 THEN V=18
130 GOSUB 1000:NEXT
140 FOR I=0 TO 63:V=16:GOSUB 1000:NEXT
150 REM --- THE MAP: 32X32 ENTRIES OF 2 BYTES AT VRAM 0, MADE BY THE BLITTER
160 REM FIRST CLEAR IT, THEN PUT TILE 1 IN EVERY ENTRY
170 A=133248:FOR I=0 TO 20:READ V:GOSUB 1000:NEXT:GOSUB 500
180 DATA 1,4, 0,0,0,0, 0,0,0,0, 0,8, 0,0, 0,0, 0,0, 0,0,255
190 A=133248:FOR I=0 TO 20:READ V:GOSUB 1000:NEXT:GOSUB 500
200 DATA 1,20, 0,0,0,0, 0,0,0,0, 1,0, 0,4, 0,0, 2,0, 1,0,255
210 REM THEN TILE 2 IN EVERY OTHER ENTRY OF EACH ROW: A CHEQUERBOARD
220 A=133260:V=16:GOSUB 1000:V=0:GOSUB 1000:REM 16 ROWS
225 A=133264:V=4:GOSUB 1000:A=133266:V=2:GOSUB 1000:REM 4 APART; TILE 2
230 FOR R=0 TO 31:D=R*64+2*((R+1) AND 1)
240 A=133254:V=D AND 255:GOSUB 1000:V=INT(D/256):GOSUB 1000:GOSUB 500:NEXT
250 REM --- LAYER 0 AT $20800: 8-BIT TILES, 32X32 MAP AT 0, TILES AT $4000
260 A=133120:FOR I=0 TO 14:READ V:GOSUB 1000:NEXT
270 DATA 1,0, 0,0,0, 0,64,0, 0,0, 0,0, 0,0, 0
280 POKE 53320,17
290 REM --- SCROLL: X IS AT $20808, Y AT $2080A. STEP 0, SO ONE POKE EACH
300 POKE 53325,8:POKE 53326,2:T=0
310 T=T+1:IF T=512 THEN T=0
320 POKE 53324,8:POKE 53327,T AND 255
330 POKE 53324,10:POKE 53327,INT(T/2)
340 GOTO 310
500 REM --- START BLITTER DESCRIPTOR 0 AND WAIT FOR IT
510 POKE 53338,0
520 IF PEEK(53338)>127 THEN 520
530 RETURN
1000 REM --- WRITE V AT PORT ADDRESS A, THEN A=A+1
1010 H=INT(A/65536):M=INT((A-H*65536)/256)
1020 POKE 53324,A-H*65536-M*256:POKE 53325,M:POKE 53326,H
1030 POKE 53327,V:A=A+1:RETURN
BASIC builds the map with the blitter instead of 2,048 separate writes: one fill puts tile 1 everywhere, then one fill per row puts tile 2 in every other entry.
Files: 04-tiles.bas · 04-tiles-basic.prg
; 04-tiles.s - a scrolling tile layer: two register writes a frame, no copying
;
; cl65 -t c64 -C c64-asm.cfg -u __EXEHDR__ --asm-include-dir ../include -o 04-tiles.prg 04-tiles.s
.include "vicx.inc"
.include "vicxlib.inc"
MAP = $0000 ; 32 x 32 entries x 2 bytes = 2 KB of VRAM
TILES = $4000 ; 8-bit 8x8 tiles, 64 bytes each
CLRSCR = $E544 ; KERNAL: clear the text screen
sx = $FB ; two free zero-page bytes
row = $FD
.code
jsr CLRSCR ; start with an empty text screen
sei
VICX_UNLOCK_SEQ
lda VICX_ID0
cmp #$56
beq found
cli
rts
found: SET_PAL 16, 30, 40, 110
SET_PAL 17, 70, 90, 190
SET_PAL 18, 250, 212, 64
; Tile 0 stays empty (color 0 is transparent). Tile 1 is a framed
; block, tile 2 a plain one.
VICX_P0_SET TILES + 64, VICX_STEP_1
ldx #0
t1: cpx #8 ; top row?
bcc edge
txa
and #7 ; left column?
beq edge
lda #17
bne put
edge: lda #18
put: sta VICX_P0_DATA
inx
cpx #64
bne t1
lda #16 ; tile 2: 64 bytes of color 16
P0_FILL 64
; The map: a chequerboard of tiles 1 and 2. Entries are 16 bits.
VICX_P0_SET MAP, VICX_STEP_1
lda #0
sta row
mrow: ldx #0
mcol: txa
eor row
and #1
clc
adc #1 ; tile 1 or 2
sta VICX_P0_DATA
lda #0 ; no flips, bank 0
sta VICX_P0_DATA
inx
cpx #32
bne mcol
inc row
lda row
cmp #32
bne mrow
LAYER_SET VICX_LAYER0, VICX_LMODE_TILE8, VICX_MAP_W32 | VICX_MAP_H32, MAP, TILES, 0, 0, 0, 0
lda #VICX_CTRL_L0 | VICX_CTRL_BGTRANS
sta VICX_CTRL
lda #0
sta sx
sta sx+1
loop: lda VICX_FRAME ; wait for the next frame
wait: cmp VICX_FRAME
beq wait
inc sx
bne :+
inc sx+1
: VICX_P0_SET VICX_LAYER0 + VICX_L_SCROLLX, VICX_STEP_1
lda sx ; scroll X, 12 bits, wraps round the map
sta VICX_P0_DATA
lda sx+1
sta VICX_P0_DATA
lda sx+1 ; scroll Y at half the speed
lsr
tax
lda sx
ror
sta VICX_P0_DATA
stx VICX_P0_DATA
jmp loop ; runs until reset
Files: 04-tiles.s · 04-tiles-asm.prg
Sprites
Sixteen sprites share one 8×8 pattern and move at four different speeds. The sprite table lives in the register space and is latched at the start of each frame.
10 REM SIXTEEN SPRITES FROM THE 128-ENTRY ENGINE
20 POKE 53359,86:POKE 53359,88:POKE 53359,33
30 IF PEEK(53312)<>86 OR PEEK(53313)<>88 THEN PRINT "NO VIC-X HERE":END
40 PRINT "{CLR}";
50 REM --- PALETTE ENTRIES 32-34
60 A=131072+4*32
70 FOR I=0 TO 2:FOR J=0 TO 2:READ V:GOSUB 1000:NEXT:A=A+1:NEXT
80 DATA 255,255,255, 250,212,64, 214,64,52
90 REM --- ONE 8X8 PATTERN AT VRAM $8000, ONE BYTE PER PIXEL, 0 = CLEAR
100 A=32768:FOR I=0 TO 63:READ V:GOSUB 1000:NEXT
110 DATA 0,0,34,34,34,34,0,0
120 DATA 0,34,33,33,33,33,34,0
130 DATA 34,33,32,32,33,33,33,34
140 DATA 34,33,32,33,33,33,33,34
150 DATA 34,33,33,33,33,33,33,34
160 DATA 34,33,33,33,33,33,33,34
170 DATA 0,34,33,33,33,33,34,0
180 DATA 0,0,34,34,34,34,0,0
190 REM --- SPRITE TABLE: 8 BYTES AN ENTRY FROM $20400
200 FOR I=0 TO 15:A=132096+8*I
210 V=8*I:GOSUB 1000:V=0:GOSUB 1000:REM X; 8 WIDE AND HIGH
220 V=12*I:GOSUB 1000:V=4:GOSUB 1000:REM Y; SWITCHED ON
230 V=0:GOSUB 1000:V=4:GOSUB 1000:REM PATTERN $8000/32 = $0400
240 V=16:GOSUB 1000:V=0:GOSUB 1000:REM 8-BIT PIXELS; HIT MASK
250 NEXT
260 POKE 53320,4:REM CTRL BIT 2: SPRITE ENGINE ON
270 REM --- MOVE THEM: EACH X BYTE IS AT $20400+8*I. STEP 0, SO THE
280 REM ADDRESS NEEDS ONLY ITS LOW BYTE CHANGED
290 POKE 53325,4:POKE 53326,2:F=0
300 F=F+1:IF F=256 THEN F=0
310 FOR I=0 TO 15:POKE 53324,8*I
320 POKE 53327,(F*((I AND 3)+1)+8*I) AND 255:NEXT
330 GOTO 300
1000 REM --- WRITE V AT PORT ADDRESS A, THEN A=A+1
1010 H=INT(A/65536):M=INT((A-H*65536)/256)
1020 POKE 53324,A-H*65536-M*256:POKE 53325,M:POKE 53326,H
1030 POKE 53327,V:A=A+1:RETURN
The sprites move more slowly in BASIC, which needs two POKEs for each sprite's X.
Files: 05-sprites.bas · 05-sprites-basic.prg
; 05-sprites.s - sixteen sprites from the 128-entry engine, moved once a frame
;
; cl65 -t c64 -C c64-asm.cfg -u __EXEHDR__ --asm-include-dir ../include -o 05-sprites.prg 05-sprites.s
.include "vicx.inc"
.include "vicxlib.inc"
PATTERN = $8000 ; a multiple of 32
COUNT = 16
CLRSCR = $E544 ; KERNAL: clear the text screen
frame = $FB ; two free zero-page bytes
temp = $FC
.code
jsr CLRSCR ; start with an empty text screen
sei
VICX_UNLOCK_SEQ
lda VICX_ID0
cmp #$56
beq found
cli
rts
found: SET_PAL 32, 255, 255, 255
SET_PAL 33, 250, 212, 64
SET_PAL 34, 214, 64, 52
; One 8x8 pattern, one byte per pixel, color 0 transparent.
VICX_P0_SET PATTERN, VICX_STEP_1
ldx #0
copy: lda shape,x
sta VICX_P0_DATA
inx
cpx #64
bne copy
lda #VICX_CTRL_SPR
sta VICX_CTRL
lda #0
sta frame
loop: lda VICX_FRAME ; the table is latched at the start of a frame
wait: cmp VICX_FRAME
beq wait
inc frame
; Rewrite the first COUNT entries: 8 bytes each, straight through port 0.
VICX_P0_SET VICX_SPRITES, VICX_STEP_1
ldx #0
next: txa ; X = frame x (1, 2 or 3) + 8 x sprite number
and #3
tay
lda #0
clc
speed: adc frame
dey
bpl speed
sta temp
txa
asl
asl
asl
clc
adc temp
sta VICX_P0_DATA ; +0 X low
lda #VICX_SW_8 | VICX_SH_8
sta VICX_P0_DATA ; +1 X high bits, size
txa ; Y = 12 x sprite number
asl
asl
sta temp
asl
clc
adc temp
sta VICX_P0_DATA ; +2 Y low
lda #VICX_S_ON
sta VICX_P0_DATA ; +3 Y high bits, on
lda #<(PATTERN / 32)
sta VICX_P0_DATA ; +4 pattern address / 32
lda #>(PATTERN / 32)
sta VICX_P0_DATA
lda #VICX_S_8BIT
sta VICX_P0_DATA ; +6 8-bit pixels
lda #0
sta VICX_P0_DATA ; +7 hit mask
inx
cpx #COUNT
bne next
jmp loop ; runs until reset
shape: .byte 0, 0,34,34,34,34, 0, 0
.byte 0,34,33,33,33,33,34, 0
.byte 34,33,32,32,33,33,33,34
.byte 34,33,32,33,33,33,33,34
.byte 34,33,33,33,33,33,33,34
.byte 34,33,33,33,33,33,33,34
.byte 0,34,33,33,33,33,34, 0
.byte 0, 0,34,34,34,34, 0, 0
The assembly version rewrites all sixteen entries every frame by streaming them through a data port.
Files: 05-sprites.s · 05-sprites-asm.prg
Banking XRAM into the memory map
One write to a page register swaps 8 KB in a single cycle. This stores a character in two different XRAM pages through the same address, then reads both back and prints 52. XRAM is the REU's memory, so the REU must be on.
10 REM BANK 8 KB PAGES OF THE 16 MB XRAM INTO THE MEMORY MAP
20 POKE 56,128:CLR:REM KEEP BASIC'S OWN DATA BELOW $8000
30 POKE 53359,86:POKE 53359,88:POKE 53359,33
40 IF PEEK(53312)<>86 OR PEEK(53313)<>88 THEN PRINT "NO VIC-X HERE":END
50 IF PEEK(53316)=0 THEN PRINT "NO XRAM: SWITCH THE REU ON":END
60 REM --- WINDOW 0 AT $8000 (SLOT 3), SWITCHED ON (128)
70 POKE 53333,0:POKE 53334,131
80 POKE 53332,5:POKE 32768,53:REM PAGE 5: STORE THE CHARACTER 5
90 POKE 53332,200:POKE 32768,50:REM PAGE 200: STORE THE CHARACTER 2
100 REM --- ONE POKE SWAPS ALL 8 KB BACK
110 POKE 53332,5:PRINT CHR$(PEEK(32768));
120 POKE 53332,200:PRINT CHR$(PEEK(32768))
130 POKE 53334,0:REM WINDOW OFF: $8000 IS C64 RAM AGAIN
Line 20 lowers BASIC's memory limit first, so its own variables stay clear of the window at $8000.
Files: 06-xram.bas · 06-xram-basic.prg
; 06-xram.s - bank 8 KB pages of the 16 MB XRAM into the CPU's memory map
;
; cl65 -t c64 -C c64-asm.cfg -u __EXEHDR__ --asm-include-dir ../include -o 06-xram.prg 06-xram.s
;
; XRAM is the REU's memory, so the REU must be enabled (VICE: -reu -reusize 16384).
.include "vicx.inc"
CHROUT = $FFD2
WINDOW = $8000
.code
sei
VICX_UNLOCK_SEQ
cli
lda VICX_ID0
cmp #$56
bne done
lda VICX_CAP_XRAM ; size in MB; 0 = no REU
beq done
lda #0
sta VICX_W0_PAGEH
lda #VICX_SLOT_8000 | VICX_SLOT_ON
sta VICX_W0_SLOT ; window 0 appears at $8000-$9FFF
lda #5 ; page 5: XRAM $00A000-$00BFFF
sta VICX_W0_PAGEL
lda #$35 ; the character "5"
sta WINDOW
lda #200 ; page 200: XRAM $190000-$191FFF
sta VICX_W0_PAGEL
lda #$32 ; the character "2"
sta WINDOW
lda #5 ; one write swaps all 8 KB back
sta VICX_W0_PAGEL
lda WINDOW
jsr CHROUT ; prints 5
lda #200
sta VICX_W0_PAGEL
lda WINDOW
jsr CHROUT ; prints 2
lda #0
sta VICX_W0_SLOT ; window off: $8000 is C64 RAM again
done: rts
Files: 06-xram.s · 06-xram-asm.prg
Direct registers
These exist only after the unlock. Reserved direct registers read $FF.
| Address | Decimal | Name | R/W | Meaning |
|---|---|---|---|---|
$D040-$D041 | 53312-13 | ID | R | $56 $58 ("VX") |
$D042 | 53314 | VERSION | R | Spec version, major in the high nibble. $01 for draft 0.1. |
$D043 | 53315 | CAP_VRAM | R | VRAM size in 8 KB units ($10 = 128 KB) |
$D044 | 53316 | CAP_XRAM | R | XRAM size in MB, rounded down. 0 with no REU. |
$D045 | 53317 | CAP_SPR | R | Sprite table entries |
$D046 | 53318 | CAP_SPRLINE | R | Sprites per raster line |
$D047 | 53319 | CAP_FEAT | R | Feature bits: 0 tiles, 1 bitmap, 2 sprites, 3 blitter, 4 windows |
$D048 | 53320 | CTRL | R/W | Bit 0 layer 0 on, 1 layer 1 on, 2 sprite engine on, 3 VIC-II colors through the palette, 4 VIC-II background transparent |
$D049 | 53321 | IRQ_EN | R/W | Bit 0 sprite collision, 1 DMA done, 2 start of frame |
$D04A | 53322 | IRQ_STAT | R/W | Same bits; write 1 to clear |
$D04B | 53323 | FRAME | R | Frame counter, wraps at 256 |
$D04C-$D04E | 53324-26 | P0_ADDR | R/W | Port 0 address, 18 bits, low byte first. Bits 3-7 of $D04E hold the step code. |
$D04F | 53327 | P0_DATA | R/W | Read or write at port 0, then step |
$D050-$D052 | 53328-30 | P1_ADDR | R/W | Port 1 address and step |
$D053 | 53331 | P1_DATA | R/W | Read or write at port 1, then step |
$D054-$D055 | 53332-33 | W0_PAGE | R/W | Window 0 page, 11 bits |
$D056 | 53334 | W0_SLOT | R/W | Bits 0-2 slot, bit 7 enable |
$D057-$D059 | 53335-37 | W1_PAGE, W1_SLOT | R/W | Window 1, same layout |
$D05A | 53338 | DMA_CMD | R/W | Write: bits 0-1 start that descriptor; bit 2 run a list in VRAM. Read: bit 7 busy. |
$D05B | 53339 | CAP_BLIT | R | Optional blitter operations: bit 0 expand, 1 remap, 2 scaled copy, 3 direction bits, 4 lists in VRAM |
$D05C-$D06E | 53340-58 | — | — | Reserved |
$D06F | 53359 | UNLOCK | W | Unlock sequence; $00 relocks. Reads $FF while unlocked. |
VRAM ports and register space
Two identical ports give the CPU access to VRAM and to the register space. Each has an 18-bit address and a data register. Reading or writing the data register accesses the byte at the address and then moves the address by the port's step. Two ports let a program copy between two places, or keep one on a table and one on pixel data.
The third address byte holds the top two address bits in bits 0-1 and the step code in bits 3-7. Bit 2 is reserved. Add the code below to the high address bits; add 128 to step backwards.
| Step | 0 | 1 | 2 | 4 | 8 | 16 | 32 | 40 |
|---|---|---|---|---|---|---|---|---|
| Code | 0 | 8 | 16 | 24 | 32 | 40 | 48 | 56 |
| Step | 64 | 80 | 128 | 160 | 256 | 320 | 512 | 640 |
| Code | 64 | 72 | 80 | 88 | 96 | 104 | 112 | 120 |
Steps of 40, 160 and 320 walk down a column of a character map, a 4-bit bitmap and an 8-bit bitmap.
Port address space
| Port address | Decimal | Contents |
|---|---|---|
$00000-$1FFFF | 0-131071 | VRAM |
$20000-$203FF | 131072 | Palette: 256 entries × 4 bytes (R, G, B, spare) |
$20400-$207FF | 132096 | Sprite table: 128 entries × 8 bytes |
$20800-$2081F | 133120 | Layer 0 setup |
$20820-$2083F | 133152 | Layer 1 setup |
$20840 | 133184 | Layer order |
$20841 | 133185 | Sprite engine status |
$20842-$20845 | 133186 | Wide window: left, right, top, bottom |
$2085C-$2085E | 133212 | DMA list address in VRAM |
$20880-$208FF | 133248 | Four DMA descriptors, 32 bytes each |
$20900-$209FF | 133376 | Collision list |
Port addresses from $20A00 up are unassigned and read $FF. Because the register space is in the port address space, one DMA transfer can rewrite a whole palette or sprite table.
Palette
- 256 entries of 24-bit RGB, four bytes each: red, green, blue and a spare byte.
- Entries 0-15 reset to the C64 colors and 16-255 to black.
- With
CTRLbit 3 set, VIC-II color n is shown through palette entry n, so old-style graphics can use 16 freely chosen colors. - Palette writes are immediate, so a raster interrupt can change colors part-way down the screen and a DMA transfer can swap a whole palette in one frame.
- A 4-bit pixel n in palette bank b shows entry b × 16 + n.
Layers
Two layers draw tiles or bitmaps from VRAM. Each is set up by 32 bytes in the register space and switched on by its bit in CTRL. Color 0 is transparent in every mode.
| Mode | Bits per pixel | Colors | VRAM for a full screen |
|---|---|---|---|
| Tile, 4-bit | 4 | 16 per tile, from 16 palette banks | Map + 32 bytes per 8×8 tile |
| Tile, 8-bit | 8 | 256 | Map + 64 bytes per 8×8 tile |
| Bitmap, 4-bit | 4 | 16, from one palette bank | 32,000 bytes |
| Bitmap, 8-bit | 8 | 256 | 64,000 bytes |
Layer setup
Layer 0 is at $20800 and layer 1 at $20820.
| Offset | Field |
|---|---|
+0 | Mode. Bits 0-1: 0 tile 4-bit, 1 tile 8-bit, 2 bitmap 4-bit, 3 bitmap 8-bit. Bit 2: 16×16 tiles. Bit 3: double-width pixels. Bits 4-7 reserved, write 0. |
+1 | Map size. Bits 0-1 width, bits 2-3 height: 0, 1, 2, 3 = 32, 64, 128, 256 tiles. |
+2 to +4 | Map base, or bitmap base, in VRAM |
+5 to +7 | Tile data base in VRAM. Tile n starts at base + n × tile size (32, 64, 128 or 256 bytes). |
+8, +9 | Scroll X, 12 bits |
+10, +11 | Scroll Y, 12 bits |
+12, +13 | Bitmap line stride in bytes, 0-1024. A stride of 0 repeats the same line on every raster line. |
+14 | Palette bank for 4-bit bitmaps, bits 0-3 |
+15 to +31 | Reserved |
Tile maps
- Map entries are two bytes, low byte first: bits 0-9 tile number, bit 10 flip X, bit 11 flip Y, bits 12-15 palette bank (4-bit tiles; 8-bit tiles ignore it).
- The map is stored row by row from the map base. A 128×128 map is 32 KB.
- Tile pixels are stored row by row. In 4-bit tiles the high nibble is the left pixel.
- Scroll X and Y wrap around the map. Scroll counts layer pixels, so with double-width pixels one step moves two screen pixels.
Bitmaps
- A base address and a line stride, so a bitmap wider than the screen scrolls by moving the base. Scroll X and Y are ignored.
- Two 8-bit bitmaps fit in 128 KB, for double buffering.
- 4-bit bitmaps hold two pixels per byte, high nibble first.
Mixing and layer order
Four sources are mixed for every pixel: layer 0, layer 1, the VIC-II picture (with its own sprites) and the sprite engine.
- The layer order register is the byte at
$20840: bits 0-1 layer 0, bits 2-3 layer 1, bits 4-5 VIC-II, bits 6-7 sprite engine. A higher value is further in front. - Equal values fall back to a fixed order, front to back: sprite engine, VIC-II, layer 0, layer 1. The register resets to
$00, so that is the order after a reset. - With
CTRLbit 4 set, VIC-II pixels that come from the background color ($D021) are transparent, so a layer shows behind normal characters. With it clear, the VIC-II covers the whole display window and a layer behind it is hidden. - "Background" means where the pixel came from, not its color: text-mode background, multicolor
%00pixels and idle-state background. Pixels in$D022-$D024, foreground pixels that happen to match, VIC-II sprites and the border stay opaque. - Outside the display window the VIC-II border is always drawn on top, so border effects keep working.
- Where every source is transparent, the
$D021color shows.
Wide window
The display window can grow into the border, for monitors and HDMI outputs that show the whole frame. Four bytes say how far each edge moves out. All four reset to 0, the normal 320×200 window.
| Address | Field | Latched |
|---|---|---|
$20842 | Left: pixels into the left border | Each raster line |
$20843 | Right: pixels into the right border | Each raster line |
$20844 | Top: lines into the top border | Each frame |
$20845 | Bottom: lines into the bottom border | Each frame |
- A value past the visible edge stops at that edge, so
$FFin all four uses the whole visible frame. - Coordinates start at the window's corner: (0,0) is the top-left pixel of the window as widened, for layers and sprites alike. Set the window before positioning things.
- In the widened part, VIC-II border pixels behave like the background: transparent with
CTRLbit 4 set, otherwise drawn in the border color. - The per-line sprite limits stay the same in a wider window.
Sprite engine
A separate engine draws up to 128 sprites from VRAM. It never touches the C64 bus, and the VIC-II's own eight sprites stay exactly as they are. Switch it on with CTRL bit 2.
Sprite table entry
Entry n is eight bytes at $20400 + n × 8. The whole table is latched at the start of each frame.
| Offset | Bits | Field |
|---|---|---|
+0 | 0-7 | X, low byte |
+1 | 0-1 | X, high bits |
| 2-3 | Width: 0, 1, 2, 3 = 8, 16, 32, 64 pixels | |
| 4-5 | Height: 0, 1, 2, 3 = 8, 16, 32, 64 lines | |
| 6, 7 | Flip X, flip Y | |
+2 | 0-7 | Y, low byte |
+3 | 0-1 | Y, high bits |
| 2 | On | |
| 3 | Behind: drawn one step back in the layer order | |
| 4-7 | Collision groups the sprite belongs to | |
+4, +5 | 0-15 | Pattern address in VRAM ÷ 32 |
+6 | 0-3 | Palette bank (4-bit sprites) |
| 4 | 8-bit pixels (256 colors) | |
| 5 | Double-width pixels | |
+7 | 0-3 | Hit mask: the groups this sprite collides with. 0 means its own groups. Bits 4-7 reserved. |
- Coordinates count display-window pixels: (0,0) is the top-left of the display window. X and Y are 10 bits and wrap at 1024, so a sprite at X 1020 shows its right-hand part at the left edge.
- Patterns are stored row by row. An 8-bit row is one byte per pixel; a 4-bit row is half that, with the high nibble on the left. A 16×16 sprite at 4 bits takes 128 bytes; a 64×64 sprite at 8 bits takes 4 KB. Color 0 is transparent.
- Priority: where sprites overlap, the lower table entry is in front.
Limits and overflow
- Each line shows at most
CAP_SPRLINEsprites and at most 640 sprite pixels. A double-width sprite counts twice its width. - Entries are taken in table order. The first sprite that would pass either limit drops out for that line, and so does every higher entry.
- The status byte at
$20841reports the frame just finished: bit 0 a line dropped sprites, bit 1 the collision list was full.
Collisions
- Two sprites collide when non-transparent pixels overlap and one of them hits a group the other belongs to. So bullets in group 2 that hit group 1 ignore each other, but hit a player in group 1.
- Colliding pairs are written to the collision list at
$20900: a count, then up to 127 pairs of entry numbers, lower entry first. Each pair appears once per frame. - The list is written after the display window's last line and
IRQ_STATbit 0 is raised then if it is not empty, so software has the whole vertical blank to react. - Sprite-to-layer collisions are not in this version.
DMA and blitter
One DMA engine copies and fills between C64 RAM, VRAM (including the register space) and XRAM, in straight runs or 2D rectangles. Four descriptors live at $20880, 32 bytes each. Writing a descriptor number to DMA_CMD starts it.
Descriptor
| Offset | Field |
|---|---|
+0 | Operation: 0 copy, 1 fill, 2 masked copy, 3 expand, 4 masked expand, 5 remap, 6 scaled copy. Other values do nothing. |
+1 | Flags. Bits 0-1 source space, bits 2-3 destination space: 0 C64 RAM, 1 VRAM and registers, 2 XRAM. Bit 4 2D. Bit 5 IRQ when done. Bit 6 mirror each row. Bit 7 backwards. |
+2 to +5 | Source address |
+6 to +9 | Destination address |
+10, +11 | Length, or width in bytes when 2D. A length, width or height of 0 moves nothing. |
+12, +13 | Height in rows when 2D |
+14, +15 | Source stride: bytes from one row to the next, signed |
+16, +17 | Destination stride, signed |
+18 | Fill value |
+19 | Transparent value |
+20 | Next descriptor (0-3), or $FF for none |
+21 to +23 | Remap table address in VRAM (operation 5) |
+24 to +27 | X step, then Y step, 8.8 fixed point (operation 6) |
+28 to +31 | Reserved |
Operations in every build
- Copy: source to destination.
- Fill: one byte value to the destination.
- Masked copy: like copy, but source bytes equal to the transparent value are skipped. With 8-bit pixels this pastes a shape over a background.
- Chains: a descriptor can start the next one when it finishes. A chain runs each descriptor at most once per start.
Optional operations
Check CAP_BLIT before using these. On a build without one, a descriptor that uses it does nothing.
- Expand (3) and masked expand (4): the source has 1 bit per pixel, most significant bit first. A 1 bit writes the fill value; a 0 bit writes the transparent value (expand) or leaves the destination alone (masked expand). This draws text, icons and patterns in any two colors without unpacking them first.
- Remap (5): like a masked copy, but each source byte is looked up in a 256-byte table in VRAM and the table's byte is written. This shades, recolors or fades part of a picture without keeping a second copy.
- Scaled copy (6): a masked copy that steps through the source by a fraction.
$0100is 1:1,$0080twice the size,$0200half. - Direction: flag bit 6 mirrors each row. Bit 7 runs the transfer backwards, so a copy can move an area down or right over itself. A negative destination stride writes rows bottom-up.
- Lists in VRAM: with
DMA_CMDbit 2 set, the engine runs 32-byte descriptors one after another from the VRAM address at$2085C, until one whose operation is$FF. A redraw of fifty rectangles is then one write.
Address spaces and timing
- C64 RAM as a DMA space is the 64 KB of RAM itself, never ROM, I/O, color RAM or a window.
- Addresses wrap within each space: 16 bits for C64 RAM, 18 bits for VRAM and registers, the XRAM size for XRAM.
- Transfers between VRAM and XRAM run in the background. The CPU keeps running and polls
DMA_CMDbit 7 or takes the DMA-done interrupt. - Any transfer that touches C64 RAM halts the CPU the way the REU does, one byte per cycle.
- A write to
DMA_CMDwhile the engine is busy is ignored. Wait for busy to clear before relying on the destination.
XRAM and CPU windows
XRAM is the REU's memory: it exists while the REU is enabled and its size is the REU size. The REU at $DF00 stays standard and reaches the same bytes, so existing REU tools and loaders can fill XRAM.
- Two windows, W0 and W1, each 8 KB. Each has an 11-bit page register (2048 pages × 8 KB = 16 MB) and a slot register.
- Slot register: bits 0-2 pick the address, bit 7 switches the window on.
| Slot code | 0 | 1 | 2 | 3 | 4 | 5 | 6, 7 |
|---|---|---|---|---|---|---|---|
| Address | $2000 | $4000 | $6000 | $8000 | $A000 | $E000 | Window off |
- Zero page, the stack and I/O are always reachable:
$0000and$C000-$DFFFare not valid slots. - An enabled window takes priority over RAM, BASIC, KERNAL and cartridge ROM in its slot, whatever
$01says. The VIC-II still sees normal C64 RAM. Code can run from a window. - If both windows sit in one slot, W0 wins.
- Switching pages takes effect on the next CPU access.
- With no REU, windows read
$FFand ignore writes.
Interrupts and timing
IRQ_STATbits are set whether or notIRQ_ENenables them. The IRQ line is active whileIRQ_STATANDIRQ_ENis non-zero. Write 1 to a status bit to clear it.- Bit 2 is set at the start of each frame (raster line 0). VIC-X shares the VIC-II's IRQ line, so a handler checks both.
When writes take effect
| What | Takes effect |
|---|---|
| Palette | Immediately |
CTRL, layer setup (mode, base, scroll), layer order | At the start of each raster line, so raster-interrupt splits work as they do on the VIC-II |
| Wide window left and right | At the start of each raster line |
| Wide window top and bottom | At the start of each frame |
| Sprite table | At the start of each frame |
| Window page and slot | On the next CPU access |
Tools and files
| Example | BASIC | Assembly |
|---|---|---|
| Detect VIC-X | 01-detect.bas · prg | 01-detect.s · prg |
| Any sixteen colors | 02-colors.bas · prg | 02-colors.s · prg |
| A 256-color bitmap drawn by the blitter | 03-bitmap.bas · prg | 03-bitmap.s · prg |
| A scrolling tile layer | 04-tiles.bas · prg | 04-tiles.s · prg |
| Sprites | 05-sprites.bas · prg | 05-sprites.s · prg |
| Banking XRAM into the memory map | 06-xram.bas · prg | 06-xram.s · prg |
| Include files | — | vicx.inc · vicxlib.inc |
The BASIC files are plain text in the format VICE's petcat tool reads; the .prg files are ready to load.
Running your programs
In the VICE fork, VIC-X is off by default. Start the emulator with it enabled, and with an REU if you use XRAM:
x64sc -vicx -reu -reusize 16384 program.prg
The emulator's source is on GitHub at NebulaSleuth/vice-vicx. A Windows build of the emulator and the MiSTer core, with the demos, are under Downloads. The VIC-X hardware and tests are at NebulaSleuth/VIC-X.
- Palette changes take effect from the next line, not mid-line, and a frame shows at most 256 different colors.
- VRAM writes during a line show on the whole line, because layers and sprites are drawn when the line ends.
- DMA moves its data at once, then keeps
DMA_CMDbusy for as long as the transfer would take. Software must still wait for busy to clear, as it must on hardware.
VIC-X is an independent project. It is not affiliated with or endorsed by Commodore.