Symbolic Debugger

Software, old and new (but mostly old)

Setting up an Emulated CP/M Development System

Introduction

CP/M was a very popular operating system for 8-bit microcomputers in the 70’s and into the 80’s. At first it only supported the Intel 8080 processor and then later it also supported a few other processes, most notably the Zilog Z80. Eventually there were versions for 16-bit processors as well, but these weren’t as popular as the original 8-bit implementation. I still enjoy using CP/M nowadays just for the fun of it; one of the attractions for me is that it’s possible to understand a system completely from bottom to top, which is not practical with modern systems.

I use a real CP/M system occasionally, but if I’m developing CP/M software it’s easier to first use an emulated system because that way I can take advantage of the host system; e.g. network access, easy backups, quieter and cheaper to run, etc.

This article describes setting up an emulated CP/M 2.2 system using z80pack on macOS.

Building z80pack

The following instructions are based on the official documentation but is simplified in order to "cut to the chase".

First, download z80pack-1.37.tgz from https://www.icl1900.co.uk/unix4fun/z80pack/ftp/

Unpack the tarball into a new directory and then build it:

cd && tar xf ~/Downloads/z80pack-1.37.tgz
cd z80pack-1.37/cpmsim/srcsim
make -f Makefile.osx
make -f Makefile.osx clean
cd ../srctools
CPATH=${HOME}/cpmtools/bin && mkdir -p ${CPATH} && make INSTALLDIR=${CPATH} && make install INSTALLDIR=${CPATH}

This will create ~/z80pack-1.37/cpmsim/cpm2 as well as various other binaries in this tree. Also, ~/cpmtools/bin/ will have been created which includes bin2hex etc.

Using the emulator

For example, to run CP/M 2.2 in console mode with a default environment:

export PATH=${HOME}/cpmtools/bin:$PATH
cd ~/z80pack-1.37/cpmsim
./cpm2

Use the bye command to finish emulation.

Customising the environment

For a more realistic environment on macOS, I use Ghostty and configured it with a custom shader and a fixed size 80x24 terminal display. Then I created a new zsh function in ~/.zshrc like this:

cpm2() {
open -na Ghostty --args \
--working-directory="${HOME}/z80pack-1.37/cpmsim" \
-e /bin/zsh -lc 'export PATH=${HOME}/cpmtools/bin:$PATH; exec ./cpm2 -f 4'
}

This creates a cpm2 command that can be used from any terminal (not just Ghostty) to launch CP/M 2.2 in a sort-of-realistic Ghostty terminal, using CRT effects and running at 4 MHz.

Using cpmtools

The cpmtools package allows the host system (macOS) to read and write CP/M disk images. One approach is to install this package from source (see https://www.moria.de/~michael/cpmtools/) but an easier approach is to install it via Homebrew. Homebrew is a package manager for macOS, see https://brew.sh and once that is installed:

brew install cpmtools

Once that's done, you can do (for example):

cd ~/z80pack-1.37/cpmsim
cpmls disks/drivea.dsk

Refer to the cpmtools web site for some info, and also the install man pages (e.g. man cpmls)

Using disk images

Refer to https://www.icl1900.co.uk/unix4fun/z80pack/doc_cpm_disks.html for documentation about available disk images and how to use them.

The main thing to realise is the cpm2 (etc) command is actually a wrapper script which sets up disks for CP/M 2.2 before running the emulator. This script creates links from two of the disk images in disks/library/ into disks/ each time cpm2 is executed.

My preferred configuration is:

  • Drive A is disks/library/cpm2-1.dsk
  • Drive B is disks/library/cpm2-2.dsk
  • Drive C is empty scratch space
  • Drive D is more empty scratch space
  • Drive I is disks/library/hd-tools.dsk
  • Drive J is disks/library/hd-toolssrc.dsk

To use this configuration, create a new wrapper script with this configuration as ~/z80pack-1.37/cpmsim/cpm2dev :

#!/bin/sh
rm -f disks/drive[abij].dsk
ln disks/library/cpm2-1.dsk disks/drivea.dsk
ln disks/library/cpm2-2.dsk disks/driveb.dsk
[ ! -e disks/drivec.dsk ] && cd disks && ../srctools/mkdskimg c && cd ..
[ ! -e disks/drived.dsk ] && cd disks && ../srctools/mkdskimg d && cd ..
ln disks/library/hd-tools.dsk disks/drivei.dsk
ln disks/library/hd-toolsrc.dsk disks/drivej.dsk
./cpmsim $*

This script will create the two scratch disks if they don't already exist. Don't forget to create a new function in .zshrc to use this configuration.

At this point, drive I: has

  • 0: standard CP/M dev tools
  • 1: DRI PL/I 1.3
  • 2: Aztec C compiler 1.06D
  • 3: DRI Pascal MT+ 5.6.1
  • 8: games

Now we start to add some interesting third-party software.

Add Turbo Pascal into user 4 on I:

From https://www.icl1900.co.uk/unix4fun/z80pack/index.html, download the disk image which contains Turbo Pascal. From it, extract file tp301a.dsk and place it in the ‘library’ used above. Manually mount it as D: (by temporarily copying tp301a.dsk into ~/cpmsim/disks/drived.dsk) and then copy it to drive I: user 4: like so:

A>pip
(press ENTER)
A>user 4
A>i:
I>save 29 pip.com
I>pip i:=d:*.*[g0]
I>tinst

(The above is painful in CP/M 2.2, is easier in CP/M 3. TODO: It could instead be done via cpmcp etc on the host file system rather than PIP within CP/M.)

Select ANSI terminal emulation and a 4MHz clock. Once finished, exit the emulated system by using the ‘bye’ command and then remove the ~/z80pack-1.37/cpmsim/disks/drived.dsk image, it will be replaced on the next run anyway.

Once that's done, you'll be able to run Turbo Pascal in your emulated CP/M system by doing:

A>I:
I>user 4
I>turbo

Add SLR disassembler into user 0 on I:

Ensure that you can properly read the HDD image, like this:

cd ~/z80pack-1.37/cpmsim
cpmls -F -f z80pack-hd ~/z80pack-1.37/cpmsim/disks/library/hd-tools.dsk

This test is important; if you don’t get a meaningful directory listing, you’ve probably got the disk formats wrong in cpmtools diskdefs, fix that before proceeding otherwise you'll corrupt your virtual media.

The supplied disk images which we’re using as a basis already include Z80ASM by SLR Systems; this is the assembler that we’ll use in these examples. But we don’t yet have the corresponding disassembler so we’ll add it now into disk I: in user 0:.

Download http://www.retroarchive.org/cpm/lang/slrdis.zip

Unzip it, and then do:

cpmcp -f z80pack-hd ~/z80pack-1.37/cpmsim/disks/library/hd-tools.dsk z80dis.com 0:
cpmcp -f z80pack-hd -t ~/z80pack-1.37/cpmsim/disks/library/hd-tools.dsk z80dis.doc 0:

Usage of z80dis is explained later on.

Usage Example - Assembly Language

First, we want to create a simple “Hello World” in this environment.

Just to be arbitrary, we will develop this code on the J: disk.

Start the CP/M simulator using the script shown earlier, with ANSI terminal emulation.

Edit

We use the ZDE editor to create a file called hello.z80. Of course you can use whatever editor you prefer, bonus points if you can cope with the "ed" editor supplied with CP/M!

A>j:
J>i:zde hello.z80

Enter the following source code and save it using ESC x

        org     0100h

; do the print call
        ld      hl,msg
        call    vprint

; exit back to the operating system
        ld      c,0
        call    5
        halt

; Print a null-terminated string to CONOUT
; HL = Start address of string
vprint: ld      a,(hl)
        cp      0       ; terminator?
        ret     z       ; return if so
        inc     hl      ; bump string pointer
        push    hl      ; and save it for later
        ld      e,a     ; E = character to print
        ld      c,2     ; C = BDOS conout
        call    5
        pop     hl
        jr      vprint

msg:    db      'Hello',0

Build

We compile hello.z80 using Z80ASM by SLR System. By default, this assembler creates an executable .COM file in one step.

J>a:z80asm hello

Test

Now it is just a simple matter to run the produced binary code in hello.com.

J>hello

Disassemble

It can sometimes be handy to disassemble a binary file back to Z80 source. This isn't quite so useful for your own code but can be very useful when investigating a third-party binary. But in this example we disassemble our own binary file, using Z80DIS by SLR Systems.

The usage of Z80DIS is a little unconventional; in the following example we specify .ccc after the file name; this is not a file extension but instead is three flags specifying the drive on which the binary is found, where the resultant output should be sent, etc. (Note that we do this example on the C: drive to avoid accidentally overwriting our original source code.)

A>c:
C>pip c:=hello.com
C>i:z80dis hello.ccc

Debug

And of course this web site needs an example of a symbolic debugger! On CP/M 3 I normally use ZSID by Digital Research. This does all the basic things that one needs from a symbolic debugger. For its era, it was a good debugger.

C>j:
J>a:zsid hello.com

Quick guide to driving ZSID:

  • use ‘l’ to list, ‘ls’ to list from address ‘s’ (a hex start address)
  • use ‘ds’ to display RAM from address ‘s’ (a hex start address) in both hex and ASCII
  • use ‘x’ to examine CPU state including registers
  • use ‘t’ to single step; this shows the CPU state and THEN does the step
  • use control-c to quit ZSID

Refer to the ZSID manual for more details.

Usage Example - Turbo Pascal

A>j:
J>user 4
J>i:turbo

Edit the source code using keys described at TechTinkering.

To compile and run from RAM just use ‘r’. To compile to a .com file change the compiler options first.

Future additions

Add WordStar, VisiCalc, and dBase just for the memories.