Layer 2 Bank Switching

The registers explained on the layer 2 ram page register section of the Spectrum Next wiki are used to control where in the Z80 address space the RAM relating to the Layer 2 video mode is placed.

This is used when manipulating the Layer 2 screen as it won’t all fit into RAM in one go. So instead the screen is split into thirds, and the relevant third is banked in instead.

Where the screen is banked in requires some thought though. Slot 1 is reserved for ROM, and banking out ROM will cause the machine to malfunction if the program attempts to use routines stored there, or interrupts. Slot 2 is possibly free but uses “contended memory”. Slots 3 and 4 are likely to contain code and data from the currently running program.

So what we need to do is use Slot 1, making use of a special feature called Write Only Paging. This is explained in the Layer 2 - SpecNext official Wiki page. It makes use of the way ROM can only be read from, and in general the screen is only written to.

It is possible to bank the screen into other slots if reading is needed, but the advantage of using slot 1 is that the screen coordinates happen to line up with the memory locations in slot 1.

Memory banking will be covered later, but for now it’s enough to know the Layer 2 screen is composed from three parts and all we need to do is set the layer 2 paging up by writing to the layer 2 access port

The code from before shows this being used - setting IO_123B to three specific values controls which third of the screen is accessible for writing.

void layer2_plot(uint8_t x, uint8_t y, uint8_t colour)
   uint8_t newy = y;
   // Work out which bit of screen we need
   if (y >= 128) {
      IO_123B = 0x83;   // Bottom
      newy -= 128;
   } else if (y >= 64) {
      IO_123B = 0x43;   // Middle
      newy -= 64;
   } else {
      IO_123B = 0x03;   // Top

   *(uint8_t*)(x + (newy << 8)) = colour;