HOME About NCOT Documentation Social Media Mastodon Support Me

Drink tea and make things

Blog Electronics Labs ZX Spectrum Next Computer Science Z80 Homebrew Computer Arduino and Microcontrollers

ZX Spectrum Next Memory Bank Switching

Posted:

Bank switching and memory paging

The Spectrum Next comes with 1 or 2 megabytes of RAM, but being an 8 bit CPU the most it can address at any one time is 64K.

To get around this obvious limitation the RAM is split into banks of 16K (or 8K if you use a different technique to the one shown here, look in the manual). The programmer can then choose which banks are mapped into the Z80’s address space by using special memory banking registers explained on the layer 2 ram page register section of the Spectrum Next wiki.

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;
}

Related Content