Stealing from ROM: Turning C64 Characters into Moving Hardware Sprites
The Commodore 64 ships with 2 KB of character ROM containing a complete set of uppercase letters, digits, punctuation, and graphics. The VIC-II draws it for every character on the screen. The CPU can read it too, but only if you tell the memory map to show it. Once you have a glyph in your hands, a hardware sprite can carry it anywhere on the screen without touching the text.
In this post we will:
- Look at how the VIC-II fetches text and sprites differently.
- Bank the character ROM in and out of the CPU’s view without taking the machine down.
- Copy the letter
Aout of ROM and turn it into a sprite. - Move that sprite smoothly across the full 320-pixel visible screen.
The code is written for KickAssembler. You can
assemble it with java -jar KickAss.jar stealing.asm and run the resulting .prg in
VICE.
How the VIC-II sees text and sprites
The VIC-II is the video chip, and it does its work independently of the 6510 CPU. It reads memory on its own schedule, in a 16 KB window called a VIC bank. Text and sprites take completely different paths through that window.
Text mode
For each character cell on the screen, the VIC does two fetches:
- Screen RAM (default
$0400, 1000 bytes) gives it a screen code for that cell. - Character memory gives it the 8 bytes that describe that character’s pixels.
The location is set by bits 1-3 of
$D018, in 2 KB steps inside the bank.
The colour of each cell comes from colour RAM at $D800. It is 4 bits per cell
and always sits at the same place from the CPU’s point of view.
Here is the important trick. In VIC bank 0 and bank 2, the VIC sees the character ROM
mirrored at $1000-$1FFF. That is why text appears on your screen even though your
own programs never copied a font. The VIC reads the ROM directly, whatever the CPU is
doing.
Sprite mode
Sprites are a different beast. Each sprite has:
- A pointer at the end of screen RAM:
screen + $03F8throughscreen + $03FF. With the default screen at$0400, sprite 0’s pointer is at$07F8. - A data block of 64 bytes. The pointer value times 64 gives the address inside the
VIC bank. Pointer
$80is$80 * 64 = $2000. - Position and attribute registers in the
$D000range.
The 64-byte block is laid out as 21 rows of 3 bytes (24 pixels wide, 21 pixels tall), plus one unused byte. Each bit is one pixel, with bit 7 on the left.
That layout matters for us. A character is 8 rows of 1 byte. A sprite row is 3 bytes. The bytes do not line up, so a glyph can’t just be pointed at in ROM. We have to copy it and rearrange it.
The character ROM and the processor port
The CPU’s memory map is controlled by a two-bit-plus-one register at address $01,
the processor port. The default value is $37, which shows BASIC, KERNAL, and I/O.
The bits that matter here are:
| Bit | Name | Meaning when set to 1 |
|---|---|---|
| 0 | LORAM | BASIC ROM at $A000, if HIRAM is also 1 |
| 1 | HIRAM | KERNAL ROM at $E000 |
| 2 | CHAREN | 1 = I/O at $D000, 0 = character ROM at $D000 |
If CHAREN is 0 and LORAM or HIRAM is 1, the character ROM appears at
$D000-$DFFF. Writing $33 to $01 gives you exactly that, with the KERNAL still
visible.
Note that $D000-$DFFF is also where all the VIC, SID, colour RAM, and CIA registers
live. Banking in the character ROM hides every one of them. That is the entire source
of danger.
Banking in ROM without crashing
Here is the rule: while the character ROM is visible, the CPU cannot reach the I/O
registers. Any interrupt that touches them will read ROM bytes instead. The KERNAL’s
normal IRQ handler acknowledges the CIA timers through $DC0D, so if an interrupt fires
during a bank switch, you can end up in a loop of confused interrupts or a hard crash.
So the safe pattern is:
- Save the processor port and the interrupt flag.
- Disable interrupts with
sei. - Switch to
$33, copy what you need from$D000, and do nothing else. - Restore the processor port.
- Restore the interrupt flag.
Notice that we copy only 8 bytes while ROM is visible, and do all the rearranging afterwards in RAM. Keep the banked window as short as possible.
Two small details:
- Use
phpandplpinstead ofcli. That way, if interrupts were already disabled when you were called, they stay disabled. - The VIC is not affected by the CPU’s banking. The text on the screen keeps working while you do this.
Turning a glyph into a sprite
We will make the sprite 2x wide and 2x tall using the expand bits, so the 8x8 glyph becomes 16x16 on screen. Only the first byte of each sprite row is used, which keeps the glyph in the leftmost 8 of the 24 logical pixels. The expanded version fills the leftmost 16 screen pixels.
So each glyph row goes into byte 0 of the matching sprite row, and the other two bytes
stay zero. Row r lives at offset r * 3.
Coordinates and the 320-pixel screen
Sprite X and Y positions are not the same as screen pixel positions. The visible 320 x 200 window in 40-column mode starts at:
- X = 24 on the left
- Y = 50 on the top
so it runs from X = 24 to X = 343 and from Y = 50 to Y = 249.
Sprite X is nine bits wide (0-511). The low eight bits are in $D000, and the ninth
bit lives in bit 0 of $D010. You need that ninth bit as soon as you pass X = 255.
Our glyph is 16 pixels wide after expansion, so its left edge can go from 24 to 328. At X = 328 the rightmost lit pixel is at 343, which is the last visible column.
Smooth movement
Smooth motion comes from two things:
- Update once per frame, at a fixed point in the frame. We wait until the raster beam reaches line 251, which is below the sprite. Changing the X position there means the sprite never moves halfway through being drawn.
- Keep the position in a 16-bit word so it can count past 255 and keep the sprite on screen. The velocity is also a 16-bit signed word, so the same add works in both directions.
At one pixel per frame the sprite crosses the 304-pixel path in about six seconds on PAL (50 Hz) or about five on NTSC (60 Hz).
The complete program
// stealing.asm - copy 'A' out of character ROM and move it as sprite 0
.const SCREEN = $0400
.const SPRITE_PTR = SCREEN + $03f8 // sprite 0 pointer
.const SPRITE_DATA = $2000 // VIC bank 0, outside the ROM mirror
.const SPRITE_BLOCK = SPRITE_DATA / 64 // pointer value, $80
.const CHAR_ROM = $d000
.const GLYPH = 1 // screen code for 'A'
.const RASTER_LINE = 251 // below the sprite
.const SPRITE_Y = 130
.const MINX = 24 // left edge of the 320-pixel screen
.const MAXX = 328 // 343 - 15 (glyph is 16 px wide)
.pc = $0801 "Basic Upstart"
:BasicUpstart2(start)
.pc = $0810 "Program"
start:
jsr clearScreen
jsr copyGlyph
jsr buildSprite
jsr setupVIC
mainLoop:
jsr waitFrame
jsr moveSprite
jsr updateX
jmp mainLoop
clearScreen:
lda #$20
ldx #$00
!clear:
sta SCREEN,x
sta SCREEN+$100,x
sta SCREEN+$200,x
sta SCREEN+$300,x
inx
bne !clear-
rts
copyGlyph:
php
sei
lda $01
pha
lda #%00110011 // char ROM at $d000, KERNAL still visible
sta $01
ldx #$00
!copy:
lda CHAR_ROM + GLYPH*8,x
sta glyph,x
inx
cpx #8
bne !copy-
pla
sta $01
plp
rts
buildSprite:
lda #$00
ldx #63
!clear:
sta SPRITE_DATA,x
dex
bpl !clear-
ldx #$00
ldy #$00
!row:
lda glyph,x
sta SPRITE_DATA,y // byte 0 of each 3-byte sprite row
iny
iny
iny
inx
cpx #8
bne !row-
rts
setupVIC:
lda $dd00
ora #%00000011 // VIC bank 0
sta $dd00
lda #SPRITE_BLOCK
sta SPRITE_PTR
lda #$01
sta $d027 // sprite 0 colour: white
lda #$00
sta $d020
sta $d021
lda #%00000001
sta $d015 // enable sprite 0
sta $d017 // expand Y
sta $d01d // expand X
lda #SPRITE_Y
sta $d001
jsr updateX
rts
waitFrame:
!leave:
lda $d012
cmp #RASTER_LINE
beq !leave-
!hit:
lda $d012
cmp #RASTER_LINE
bne !hit-
rts
moveSprite:
clc
lda xpos
adc xspeed
sta xpos
lda xpos+1
adc xspeed+1
sta xpos+1
lda xpos+1
bne !right+
lda xpos
cmp #MINX+1
bcs !done+
lda #1
sta xspeed
lda #0
sta xspeed+1
rts
!right:
lda xpos
cmp #<MAXX
bcc !done+
lda #$ff
sta xspeed
sta xspeed+1
!done:
rts
updateX:
lda xpos
sta $d000
lda $d010
and #%11111110
ora xpos+1
sta $d010
rts
glyph: .fill 8, 0
xpos: .word MINX
xspeed: .word 1
Build it, run it, and watch the A slide back and forth across the whole screen.
What to try next
- Half speed. Move on every other frame with a counter instead of changing the speed word.
- Different glyphs. Change
GLYPHto any screen code from0to255to pick from the uppercase and graphics set at$D000. The lowercase set starts at$D800, so adding$0800to the source address gives you those. - Unexpanded sprite. Clear the expand bits and place the glyph in the middle byte of each row, to see the sprite at its native 24x21 size.
- Two sprites. Point sprite 1 at the same data block and give it its own position. Then count how many CPU cycles the VIC takes when both sprites share a scanline.
Next time we will move from one sprite to several, and deal with the cycle budget that sprite DMA takes out of the CPU.