BitMagic The Complete Development Environment for the Commander X16.

How It Works

This page explains what the remote debugger does on the X16, so you know what it uses and why it behaves as it does. You don’t need it to use the debugger, but it helps when your program does something unusual.

The pieces

VSCode  <-- debug protocol -->  the adapter (on the PC)  <-- TCP -->  WiFi card  <-- UART -->  the X16
  • The extension starts the adapter and writes SD cards.
  • The adapter does the work on the PC: it builds your project with the BitMagic compiler, waits for the X16 to dial in, and turns VSCode’s requests into commands for the X16.
  • The stub is about 2KB of 65c02 on the X16 that answers those commands.

The X16 dials the PC, rather than the other way round, as that’s what the WiFi card’s modem firmware can do.

What it uses on the X16

What Where Description
Interrupt handler $0342 to $0369 40 bytes the vectors point at. It’s in low RAM so it’s there whatever bank is switched in.
The stub $a000 in a bank of high RAM 63 by default, set with x16remote.bank.
Its stack $0100 to $0117 Only while stopped. The bytes there are saved and put back, so your stack isn’t touched.
Vectors $0314, $0316, $0318 IRQ, BRK and NMI all go to the handler.
WiFi card $9fe0 to $9fe7 The link to the PC.

DEBUG.PRG itself is only needed to start: once the debugger is running, your program can overwrite it.

How it stops

There are three ways into the debugger, all through the handler:

Way in When
brk A breakpoint or a step.
IRQ The UART has data, which means VSCode wants a pause. Every other IRQ goes straight on to the previous handler.
NMI The NMI button.

When it stops, the stub saves the registers, sends them to the PC and waits for commands. It doesn’t touch VERA, so the screen and VRAM stay as your program left them; reading VRAM saves and restores VERA’s address registers.

The I/O area is left alone too. The PC only asks for addresses in $9f00 to $9fff that are in ioAccess, and never the WiFi card’s, so nothing reads a register unless you’ve said it can.

How breakpoints and stepping work

The 65c02 has no breakpoint or single step hardware, so the debugger uses brk:

  • A breakpoint replaces the first byte of an instruction with brk ($00), and the stub keeps the original. While stopped, every original byte is back in place, so memory reads, the disassembly and your program only ever see the real code.
  • A step decodes the instruction on the PC and puts a temporary brk wherever it can go next: both sides of a branch, the target of a jmp or jsr, or the return address for rts.
  • Step over treats a jsr as one step, by putting the brk after it. A jsr into ROM is always stepped over, as there’s nowhere to put a brk in ROM.
  • Continuing from a breakpoint first steps once without stopping, so the breakpoint doesn’t stop it straight away.

When the connection drops

If the WiFi drops, the stub sees the card’s NO CARRIER and dials again, with your program still stopped and its breakpoints intact. Stopping a debug session works the same way: the X16 waits, and the next F5 picks it up where it was.