BitMagic The Complete Development Environment for the Commander X16.

Debugging New Hardware

The emulator can only run what it models. If you’re building an expansion card, a prototype board or anything else the emulator has never heard of, the code that drives it can only run on a real X16. Until now that meant debugging it with PRINTs and a lot of guesswork.

With remote debugging you can step through that code on the machine, with the hardware attached, and see what it does. This page covers how to get the most out of it when the hardware is new.

Where your hardware can go

Expansion cards sit in the I/O area at $9f60 to $9fff (see the X16Community docs for the slots). The debugger needs some things for itself, so keep clear of them:

What Where Description
WiFi card $9fe0 to $9fe7 How the X16 talks to VSCode. Your card can’t use these addresses.
Interrupt handler $0342 to $0369 Always there while debugging.
The debugger a bank of high RAM, 63 by default Change it with x16remote.bank if your program needs 63.
Its stack $0100 to $0117 Only while stopped. What was there is put back before your program runs again.
Vectors $0314, $0316, $0318 IRQ, BRK and NMI.

How to let the debugger see your card

Out of the box the debugger never reads I/O, as a read can clear a status bit or pop a FIFO. To look at your card’s registers, list its addresses in ioAccess in your launch configuration:

{
    "type": "x16remote",
    "request": "launch",
    "name": "Debug on the X16",
    "program": "${workspaceFolder}/project.json",
    "ioAccess": [ "$9f60-$9f6f" ]
}

Or set x16remote.ioAccess in your settings, to use it for every project. Each entry is a range ($9f60-$9f6f) or a single address ($9f60), anywhere in $9f00 to $9fff apart from the WiFi card at $9fe0 to $9fef.

With that in place, the memory view, the I/O view and watches read and write those addresses, and nothing else in I/O. Note: anything in a range is read whenever it’s shown: the memory view whenever it’s open on those addresses, the I/O view whenever it’s open and the program stops. Only list the registers that are safe to read that way, and leave out a data port or a register that clears when read. Hovers never read I/O, so moving the mouse over a register’s name won’t touch it.

How to name your registers

Give each register a name with .const (or .constvar, if you want to give it a type), so the code reads well and the debugger can show it:

.const CARD_STATUS $9f60
.const CARD_CTRL   $9f62
.const CARD_DATA   $9f61   ; a FIFO: left out of ioAccess below, so it's never read by the debugger
"ioAccess": [ "$9f60", "$9f62" ]

Hovering over a name shows its address.

How to see what the hardware returned

With ioAccess set, the variables pane has an I/O view under Globals. It lists every name for an address in your ranges, one byte each, with its value read from the card:

Name Value Type
CARD_STATUS 0x80 byte ($9F60)
CARD_CTRL 0x01 byte ($9F62)

It starts closed, so nothing is read until you open it, and then it’s read each time the program stops. Change a value there and it’s written to the card. If a range covers VERA, or anything else BitMagic has names for, those names show up too.

A watch does the same for one register: watch CARD_STATUS (or $9f60) and it’s read each time the program stops.

For a register that changes when it’s read, let your code do the reading, then look at the result. The hardware then sees exactly the accesses it would without the debugger.

The simplest way is to step over the load and look at the CPU registers:

    lda CARD_STATUS   ; step over this, then A in the CPU view is the status
    and #$80
    beq -wait

To see more than one value, keep a copy in RAM. Typed variables show up in Locals and Globals, so a trace buffer is one click away:

.proc read_block
    ldx #0
.loop:
    lda CARD_DATA     ; each byte the card sends...
    sta trace, x      ; ...ends up in trace, shown in Globals as byte[16]
    inx
    cpx #16
    bne -loop
    rts
.endproc

.var byte[16] trace

Put a breakpoint on the rts and open trace in Globals to see all sixteen bytes. The memory view shows the same bytes at trace’s address.

How to drive the hardware by hand

While the program is stopped you can change any register or RAM, then let the code use it:

  • Write a register directly: with it in ioAccess, change its value in the I/O view, or type into the memory view at its address. The write goes straight to the card.
  • Try a value: stop on a sta CARD_DATA, change A in the CPU view, then step over it. The card gets your value.
  • Run something again: change PC in the CPU view back to the start of a sequence, then step through it again. It’s handy for repeating an initialisation without restarting.
  • Change a setting in RAM: edit a typed variable in Locals or Globals, or a byte in the memory view, before the code reads it.

When the hardware hangs the program

A loop waiting for a status bit that never comes is the classic new hardware bug. You have two ways in:

  • Pause (F6) stops the program wherever it is, so you can see which loop it’s in and what it last read.
  • The NMI button stops it even when interrupts are off. If Pause gets no answer after a few seconds, VSCode tells you to use it.

Note: pause works through the IRQ. If your program installs its own handler at $0314, chain on to the previous one when you’re done (as you should anyway) and pause keeps working. Breakpoints use the BRK vector, so they’re not affected either way.

Things that are different on hardware

  • Time keeps going while you’re stopped. Your hardware carries on, so a FIFO can fill or a timeout can pass while you look around. If it matters, read what you need into RAM first and look afterwards.
  • Interrupts while stopped are missed. The X16 is in the debugger, so an interrupt from your card in that time isn’t seen by your program.
  • Stepping is done with breakpoints, by putting a brk after the instruction. Code that runs from RAM your hardware writes into (by DMA, say) can lose a breakpoint; the Debug Console says so when it happens.

So far this has been tested against the emulator’s WiFi card. If you’re using it to bring up a board and something doesn’t behave, let me know: hardware projects are exactly what it’s for.