Thursday, December 23, 2021

Trace C64 I/O Kernal Jump Table Calls

See what your C64 programs are up to!   Debug, or help transition from BASIC to assembly language I/O programming.

c64-io_monitor is a program that allows monitoring or tracing input/output calls on the Commodore 64.  It can show counts or a trace log of what calls have occurred.   

The screenshots show the initial copyright screen, with usage hints, and status (includes trace log bytes available), followed by an example program, and the counts and trace log from running this program.

Basic usage includes loading io_monitor program from disk at absolute address $C000.   It is recommended to lower BASIC by poking to memory address 56 (e.g. POKE 56, 128).  Then NEW to both clear variables and reset BASIC program pointers.  And use SYS 49152 to initialize the trace/monitor.

    LOAD "io?monitor",8,1
    POKE 56,128

The monitor copies BASIC/KERNAL ROMs to RAM, patches them to hook the I/O entry points in the Commodore 64 kernal jump table at the end of ROM within the $FF00-$FFFF range.  The hooks take care of counting calls, and logging trace information in available RAM.  While each call is made, the border color is incremented to show it is working.

At this point, you may make any I/O calls including keyboard input (plus RETURN), screen output, disk/tape I/O, etc.   The I/O calls will be counted/logged.

When you are finished with the monitoring session, it is recommended to press STOP+RESTORE to reset back to the KERNAL ROMs, then the counts and tracing is turned off.

Counts can be displayed with SYS 49155, and the trace log can be displayed with SYS 49158.

The trace log can be sent to a file with statements like the following:

    OPEN 1,8,3,"@0:TESTLOG,S,W"
    CMD 1
    SYS 49155: REM COUNTS
    SYS 49158: REM VIEW LOG
    CLOSE 1

Why did I create this?  I have been developing some simple (text only) 6502 Commodore emulators (one, two, three, four, five) for multiple targets (web, Windows console/terminal, Linux terminal, Mac terminal, Arduino UART, ESP32 UART, Teensy LCD, STM32F4 LCD, and more embedded targets) and am looking at supporting file I/O emulation (open, input, get, print, close, etc.) above and beyond my D64 emulation.  One step along the way is understanding Commodore I/O some more from the machine language level.  Seeing a trace log provides a closer in-action view of the kernal I/O routines.


The D64 disk image contains the machine code for io_monitor (also available as PRG), and some sample programs that can be used to demonstrate the functionality, including viewlog which opens and displays a sequential file (e.g. testlog saved to disk like above).  The source code posted to github is open source (MIT Licensed meaning free to use/alter/distribute, giving credit where credit is due, with no warranties).

Hackaday linked to this article

1 comment:

  1. Note work is (was?) in progress to also log calls to vectors that get redirected to the Kernal. Counts are already implemented, at least in a separate github branch.