Last modified on 25 July 2022, at 08:20

Programming:Cartridges

Overview

The main media to distribute software on the CPC were cassettes and 3 inch floppy disks. The Plus models, including the GX4000 console, introduced cartridges.

As the original media is harder to find (3" discs specially), cartridges are becoming one of the best options to distribute CPC software, for example:

  • Dandanator mini –page only in Spanish– (*): is a cartridge format to be plugged in the expansion port that provides a number of generic and advanced features, including support for 512K of ROM (in 16K banks), and it works on any CPC without jumpers or special configuration.
  • DES: the Dandanator Entertainment System (*), which is an interface that goes to the expansion port and supports cartridges. It has the same functionality as the Dandanator, with the difference that the expansion contains most of the functionality, and the cartridges are simpler –and cheaper–.
  • Plus2CPC: provides a cheap way to plug CPC Plus cartridges on any CPC and, as long as the cartridge doesn’t use any Plus features, it will work. It also offers 512K of ROM, just like the Dandanator, and uses the regular ROM mapping functions of the CPC.
  • the M4 board: can load CPR files on any CPC.
  • C4CPC: can load CPR files on the GX4000.

(*) Gerber files and documentation to build your own Dandanator or DES are available.

Strategy

When the CPC boots from cartridge, the code will start executing at address 0x0000 and we have the first 16K bank mapped on that address (lower ROM).

Address Size Type Function
0x0000 16K ROM Code
0x4000 16K RAM R/W Data
0x8000 16K RAM R/W Data
0xc000 16K RAM/ROM Video memory/Mapped cart bank

When a ROM bank is mapped into lower ROM (0x0000) or higher ROM (0xC000), reads will go to ROM and writes will go to RAM and mapped ROM is only seen by the CPU. This is important because the gate array can only see RAM when drawing the screen.

The idea is mapping on higher ROM any of the banks as we need them, and we can use that data with RAM from 0x4000 to 0xffff, leaving on lower ROM the bank 0 with our code.

Important: when mapping data on high ROM with video RAM on 0xc000 you won't be able to draw masked sprites directly from that mapped ROM (reading video RAM is required to apply the mask). Either don't use 0xc000 for video memory or make a copy of those sprites to RAM from 0x4000 to 0xbfff and draw them from there.

However the RAM mode 0x7fc3 allows you to move the video RAM from 0xc000 to 0x4000, which allows it to be accessed directly from the upper ROM.

It is easy to support both CPC+ and Dandanator from a single code base using macros and generating both CPR and Dandanator ROMs is trivial. For example, these simple tools to make/dump CPC+ CPR cartridge files can generate both formats (use RAW and padding flags to get a Dandanator ROM).

Dandanator

Init the cart (not needed in CPC+ carts):

   ; SDCC syntax
   ; the byte pointed by iy will be overwritten
   di
   ld a, #0x8a
   .db 0xfd, 0xfd
   ld 0 (iy), a
   ei

Bank switch higher ROM:

   ; SDCC syntax
   ; defined as __z88dk_fastcall in C - parameter in l
   ; the byte pointed by iy will be overwritten
   di
   ld c, l
   .db 0xfd, 0xfd
   ld 0 (iy), c
   ei

Unmap higher ROM (have regular RAM in 0xc000):

   ; SDCC syntax
   ; the byte pointed by iy will be overwritten
   di
   ld c, #32
   .db 0xfd, 0xfd
   ld 0 (iy), c
   ei

CPC+ carts (CPR)

Bank switch higher ROM:

   ; SDCC syntax
   ; defined as __z88dk_fastcall in C - parameter in l
   di
   ld a, l
   ; cart slots 0 - 31 start at 128
   or #128
   ld c, a
   ld b, #0xdf
   out (c),c
   ld bc, #0x7f80
   out (c),c
   ei

Unmap higher ROM (have regular RAM in 0xc000):

   ; SDCC syntax
   di
   ld bc, #0x7f88
   out (c),c
   ei

This is an example of CRT0 for SDCC for a CPR. It shows how to setup the CPC when booting from cartridge.

References

Sources (used with permission; CC BY NC SA 4.0):