Tutorial · Chapter 5 of 23

Draw and talk to hardware

Use C64 helpers for display, a volatile pointer for a register, and assembly when you need its operands.

Jump to a section

Choose an interface by the effect you need

EffectInterface
Print the sector and visit countConsole formatting with typed arguments
Draw a tile at a screen coordinatec64.screen or explicit screen RAM writes
Change a VIC-II registerA C64 helper or a typed volatile pointer
Call firmware with values in A, X, and YInline assembly when a built-in helper does not cover the call

The screen and keyboard use different byte conventions. The default screen stores the A glyph as $01. The keyboard returns PETSCII $41 for A in the standard input configuration. The encoding example prints the stored screen bytes so you can check the difference.

Write a register through a volatile pointer

volatile-border.zp64
export fn main() void {
    const border: *volatile u8 = @ptrFromInt(0xd020);
    border.* = 5;
    asm {
        lda #$06
        sta $d021
    }
}

Open the complete source

The address and type explain the operation. The pointer addresses a byte at the VIC-II register. Volatile makes the load or store a device access. The register is available when the C64 mapping exposes I/O. Keep the default mapping for these exercises.

Keep an assembly operation small

asm-border.zp64
const c64 = @import("c64");

export fn main() void {
    c64.console.clear();
    var color: u8 = 2;
    asm {
        ldx #3
    brighten:
        inc @color
        dex
        bne brighten
        lda @color
        sta $d020
    }
    c64.console.print("BORDER {x}\n", .{color});
}

Open the complete source

The loop changes X, A, and status. ZeroPage preserves those across the block. It leaves the source byte and border write in place. To return a firmware value to source code, store it inside the block before the wrapper restores registers. The GETIN example does this for one byte. The checked LOAD example passes a string address and captures a status flag.

Apply it to the survey

Use the survey's green border as a completion indicator. Add a second color for the active survey and set it before entering the input loop. After Q, check that the border changes to green. Use the built-in border assignment first, then try the volatile store from this chapter to see the same effect through a literal hardware address.