Difference between revisions of "Memory map"

From SpecNext official Wiki
Jump to: navigation, search
(SRAM memory map)
Line 1: Line 1:
The Next supports two memory management models which operate in tandem.
+
The Next supports two memory management models which operate in tandem. One is a unique memory management system for the Next. The other is an expanded version of the memory manager from the original Spectrum 128 and +2/+3 series.
  
== 128-style memory management ==
+
== Global Memory Map ==
  
The Memory Map of the Next is based on the memory structure of the ZX Spectrum 128 and +2a/+3.  
+
The total available RAM space of the Next is 512k on an unexpanded Next, or 1536k - 1.5mb - on a Next expanded to 2Mb. (The base Next has 1mb of memory but 512k of it is reserved for the ROMs and firmware.)
  
The Z80's 16-bit bus can only access 64k of memory, which is divided into 4 "slots" as follows. Each of these 4 slots is mapped to a 16k ''bank'' of memory within the larger available address space.
+
The Z80 processor in the next can access only 16k of memory at a time, and so the memory is divided into ''banks'' which are used in determining which memory it sees. Spectrum 128k memory management uses 16k banks. Next memory management uses 8k banks.
  
 
{|class="wikitable"
 
{|class="wikitable"
! Area !! Description
+
! 16k-bank !! 8k-bank !! True Address !! Description
 
|-
 
|-
| $0000-$3fff || Slot 1, usually ROM, but usable for Layer 2.
+
| 0 || 0-1 || $060000-$063fff || Standard RAM, may be used by EsxDOS
 
|-
 
|-
| $4000-$7fff || Slot 2, usually used for the ULA screen. Defaults to bank 5.
+
| 1 || 2-3 || $064000-$067fff || Standard RAM, contended on 128, may be used by EsxDOS
 
|-
 
|-
| $8000-$BFFF || Slot 3, Free RAM. Defaults to bank 2.
+
| 2 || 4-5 || $068000-$06afff || Standard RAM
 
|-
 
|-
| $C000-$FFFF || Slot 4, Free RAM. Defaults to bank 0.
+
| 3 || 6-7 || $06b000-$06ffff || Standard RAM, contended on 128, may be used by EsxDOS
|}
 
 
 
On the 48k Spectrum this is all the available memory there is, so no switching can be performed. On the 128k Spectrum, there are 8 available banks, numbered 0-7. In standard mode, any bank can be switched into Slot 4 (via 128k Spectrum standards) and also any bank can be ''written to'' (but not read) in Slot 1 (via [[Layer 2]] standards)
 
 
 
The active bank at slot 4 is selected by writing to the bottom 3 bits of {{PortNo|$7FFD}}. This can be any bank from 0 to 7, which means it can end up doubling the bank at $8000 (2) or $4000 (5). On Next, there are more than 7 banks as there as a total of 1Mb of system memory. 512k of these is occupied by the hardware ROM and RAM images and the standard Spectrum 128k RAM banks, but the remaining 512k provides 32 additional banks. The necessary extra bits are written to the lowest 3 bits of {{PortNo|$DFFD}}. If the Next has been chip upgraded to 2mb of memory, then the additional memory provides 96 extra banks for a total of 104 banks including the initial 8; however, there are only 6 memory selection bits available in this model, so the highest bank that can be accessed is bank 64.
 
 
 
Layer 2 switching can allow any bank to be written to in Slot 1 by "lying" that it is the address of [[Layer 2]], then writing to it using the Layer 2 paging system without enabling display. Note, however, that data cannot be ''read'' from a bank switched in in this way.
 
 
 
"Special paging mode" (also called "AllRam mode" or "CP/M mode") is enabled by writing a value with the LSB set to {{PortNo|$1FFD}}. Depending on the 3 low bits of this value a memory configuration is selected as follows:
 
{| class="wikitable"
 
! Bits !! Slot 1 !! Slot 2 !! Slot 3 || Slot 4
 
 
|-
 
|-
| %001 || 0 || 1 || 2 || 3  
+
| 4 || 8-9 || $070000-$073fff || Standard RAM, contended on +2/+3
 
|-
 
|-
| %011 || 4 || 5 || 6 || 7
+
| 5 || 10-11 || $074000-$077fff || ULA Screen, contended except on Pentagon
 
|-
 
|-
| %101 || 4 || 5 || 6 || 3
+
| 6 || 12-13 || $078000-$07afff || Standard RAM, contended on +2/+3
 
|-
 
|-
| %111 || 4 || 7 || 6 || 3
+
| 7 || 14-15 || $07b000-$07ffff || ULA Shadow Screen, contended except on Pentagon
|}
 
 
 
Beware that these will have the effect of paging out the ROM. Some programs may assume that they can find ROM service routines at fixed addresses in Slot 1. More importantly, if the default interrupt mode (IM 1) is set, the Z80 '''will''' jump the program counter to $0038 every frame expecting to find an interrupt handler there. If it does not, pain and suffering will likely result. [[Extended Z80 instruction set#DI|DI]] is your friend.
 
 
 
If you are using the standard interrupt handler or OS routines, then any time you write to {{PortNo|$7FFD}} you should also store the value at $5B5C. Any time you write to {{PortNo|$1FFD}} you should also store the value at $5B67. There is no corresponding system variable for the Next-only {{PortNo|$DFFD}} and standard OS routines may not support the extended banks properly.
 
 
 
== ROM paging and selection ==
 
Slot 1 is usually mapped to ROM. ROM is not considered one of the numbered banks; ROM segments are selected by separate methods. The 128k Spectrum has 2 ROM pages which are selected in slot 1 by altering Bit 4 of {{PortNo|$7FFD}}. The +2a/+3 has 4 ROM pages; the extra bit needed to select between these is bit 2 of {{PortNo|$1FFD}}.
 
 
 
Activating [[Layer 2]]'s paging will cause ''writes'' into slot 1 to go to the selected Layer 2 bank instead. Reads will continue to access the selected ROM page (or RAM bank if AllRam mode is enabled). This allows Layer 2 to be used without conflict by repurposing an operation (attempting to write to ROM) that would normally be useless.
 
 
 
== Screen ==
 
Bank 5 is the bank read by the ULA to determine what to show on screen. The ULA connects directly to the larger memory space ignoring mapping; the screen is always Bank 5, no matter where in memory it is (or if it is switched in at all). Setting bit 3 of {{PortNo|$7FFD}} will have the ULA read from bank 7 (the "shadow screen") instead, which can be used as an alternate screen. Beware that '''this does not remap slot 2'''; to alter bank 7 it must be switched in to slot 4. On the 128k Spectrum, banks 1, 3, 5, and 7 are all contended; on the +2a/+3, banks 4, 5, 6, and 7 are contended. Contended memory is subject to delays.
 
 
 
By default, extra banks 8-13 are used for [[Layer 2]], although these can be reselected or reused if you are not using layer 2.
 
 
 
== Next Memory Management ==
 
 
 
The Next also includes its own higher-level memory management tool. This divides memory into 8 slots as follows:
 
 
 
{|class="wikitable"
 
! Area !! Description
 
 
|-
 
|-
| $0000-$1fff || Slot 1, usually ROM, but usable for Layer 2.
+
| 8 || 16-17 || $080000-$083fff || Next RAM, Default Layer 2
 
|-
 
|-
| $2000-$3fff || Slot 2, usually ROM, but usable for Layer 2.
+
| 9-10 || 18-21 || $084000-$08afff || Next RAM
 
|-
 
|-
| $4000-$5fff || Slot 3, usually used for the ULA screen.
+
| 11 || 22-23 || $08b000-$08ffff || Next RAM, Default Layer 2 Shadow Screen
 
|-
 
|-
| $6000-$7fff || Slot 4, usually used for the ULA screen.
+
| 12-39 || 24-79 || $090000-$0fffff || Next RAM
 
|-
 
|-
| $8000-$9fff || Slot 5, Free RAM.
+
| 40-63 || 80-127 || $100000-$15ffff || 2Mb Expanded Next RAM (accessible via 128 memory management)
 
|-
 
|-
| $a000-$bfff || Slot 6, Free RAM.
+
| -- || 128-207 || $160000-$1ffffff || 2Mb Expanded Next RAM (accessible via Next management only)
|-
 
| $c000-$dfff || Slot 7, Free RAM.
 
|-
 
| $c000-$dfff || Slot 8, Free RAM.
 
 
|}
 
|}
  
The page accessed in a slot is selected by writing to the bottom 7 bits of Next registers from {{NextRegNo:$50}} upwards. Since pages are 8k big in this model, this fills in the most significant 3 bits of the 16-bit address and then the remaining 5 bits for the expansion.
+
== Z80 Visible Memory map ==
 
 
Since pages are half the size that they are in the Spectrum 128 model, there are 208 addressable banks in this model, addressed as 0-207. In addition, in Slots 1 and 2 only, the ROM can be paged in by writing $FF to the page. Which ROM is active is selected using the 128k model as above.
 
  
== Slot List ==
+
At start up, the 16-bit address space of the Z80 is mapped to memory as follows:
  
 
{|class="wikitable"
 
{|class="wikitable"
! Banks (128k) !! Banks (Next) !! True Address !! Description
+
! Area !! 128 Slot !! Next Slot !! 128 Default Bank !! Next Default Bank !! Description
 
|-
 
|-
| 0 || 0-1 || $000000-$003fff || Standard RAM, may be used by EsxDOS
+
| $0000-$1fff || rowspan=2 | 1 || 1 || rowspan=2 | ROM || ROM (255) || Normally ROM. Writes mappable by layer 2. IRQ and NMI routines here.
 
|-
 
|-
| 1 || 2-3 || $004000-$007fff || Standard RAM, contended on 128, may be used by EsxDOS
+
| $2000-$3fff || 2 || ROM (255) || Normally ROM. Writes mapped by Layer 2.
 
|-
 
|-
| 2 || 4-5 || $008000-$00afff || Standard RAM
+
| $4000-$5fff || rowspan=2 | 2 || 3 || rowspan=2 | 5 || 10 || Normally used for normal/shadow ULA screen.
 
|-
 
|-
| 3 || 6-7 || $00b000-$00ffff || Standard RAM, contended on 128, may be used by EsxDOS
+
| $6000-$7fff || 4 || 11 || Normally used for normal/shadow ULA screen.
 
|-
 
|-
| 4 || 8-9 || $010000-$013fff || Standard RAM, contended on +2/+3
+
| $8000-$9fff || rowspan=2 | 3 || 5 || rowspan=2 | 2 || 4 || Free RAM.
 
|-
 
|-
| 5 || 10-11 || $014000-$017fff || ULA Screen, contended except on Pentagon
+
| $a000-$bfff || 6 || 5 || Free RAM.
 
|-
 
|-
| 6 || 12-13 || $018000-$01afff || Standard RAM, contended on +2/+3
+
| $c000-$dfff || rowspan=2 | 4 || 7 || rowspan=2 | 0 || 0 || Free RAM. Only this area is remappable by 128 memory management.
 
|-
 
|-
| 7 || 14-15 || $01b000-$01ffff || ULA Shadow Screen, contended except on Pentagon
+
| $e000-$ffff || 8 || 1 || Free RAM. Only this area is remappable by 128 memory management.
 +
|}
 +
 
 +
== 128-style memory management ==
 +
 
 +
128-style memory management can only alter the bank addressed at $c000 (slot 4 in 128 terms, slot 7-8 in Next terms). The active bank at $c000 is selected by writing the 3 LSBs of the 16k-bank number to the bottom 3 bits of {{PortNo|$7FFD}}, and the 3 MSBs to the bottom 3 bits of {{PortNo|$DFFD}}. (The reason for the division is that the original Spectrum 128, having only 128k of memory, only needed 3 bits.)
 +
 
 +
On an unexpanded Next, this allows any 16k-bank to be paged in at $c000. On an expanded next, there are not enough bits available to access the banks at the bottom of the expanded memory, so Next memory management must be used to access these.
 +
 
 +
If you are using the standard interrupt handler or OS routines, then any time you write to {{PortNo|$7FFD}} you should also store the value at $5B5C. Any time you write to {{PortNo|$1FFD}} you should also store the value at $5B67. There is no corresponding system variable for the Next-only {{PortNo|$DFFD}} and standard OS routines may not support the extended banks properly.
 +
 +
== 128 Special Paging Mode ==
 +
"Special paging mode" (also called "AllRam mode" or "CP/M mode") is enabled by writing a value with the LSB set to {{PortNo|$1FFD}}. Depending on the 3 low bits of this value a memory configuration is selected as follows:
 +
{| class="wikitable"
 +
! Bits !! Slot 1 !! Slot 2 !! Slot 3 || Slot 4
 
|-
 
|-
| 8 || 16-17 || $020000-$023fff || Next RAM, Default Layer 2
+
| %001 || 0 || 1 || 2 || 3
 
|-
 
|-
| 9-10 || 18-21 || $024000-$02afff || Next RAM
+
| %011 || 4 || 5 || 6 || 7
 
|-
 
|-
| 11 || 22-23 || $02b000-$02ffff || Next RAM, Default Layer 2 Shadow Screen
+
| %101 || 4 || 5 || 6 || 3
 
|-
 
|-
| 12-39 || 24-79 || $030000-$09ffff || Next RAM
+
| %111 || 4 || 7 || 6 || 3
|-
 
| 40-63 || 80-127 || $0a0000-$0fffff || 2Mb Expanded Next RAM (accessible via 128 memory management)
 
|-
 
| -- || 128-207 || $100000-$19fffff || 2Mb Expanded Next RAM (accessible via Next manegement only)
 
 
|}
 
|}
  
 +
== Layer 2 Switching ==
 +
Layer 2 switching can allow any 16k-bank to be ''written to'' (but not read) in Slot 1, by writing the 16k-bank number to {{NextRegNo:$12}} and then enabling Layer 2 paging by writing a value with the LSB set to {{PortNo:$123B}}.
 +
 +
Writing to this area will then write the appropriate area of memory, whereas ''reading'' from it will give the area mapped by other memory management.
 +
 +
== Next Memory Management ==
 +
The 8k-bank accessed in a slot is selected by writing the 8k-bank number to the bottom 7 bits of the 8 Next registers from {{NextRegNo:$50}} upwards. $50 addresses 8k-slot 0, $51 addresses 8k-slot 1, and so on.
 +
 +
In addition, in Slots 1 and 2 only, the ROM can be paged in by writing $FF to the page.
 +
 +
== ROM paging and selection ==
 +
$0000-$3fff is usually mapped to ROM. This area can only be fully remapped using Next memory management. ROM is not considered one of the numbered banks; it is mapped to the two 8k-banks by default, or by setting their 8k-bank numbers to 255.
 +
 +
The 128k Spectrum has 2 ROM pages. Which of these is mapped is selected by altering Bit 4 of {{PortNo|$7FFD}}. The +2a/+3 has 4 ROM pages; the extra bit needed to select between these is bit 2 of {{PortNo|$1FFD}}. This maintains compatibility with the original machines' ROM paging as long as the ROM is not paged out.
  
 +
== Paging out ROM ==
 +
ROM can be paged out by enabling AllRam mode, or by using Next memory management. Beware that some programs may assume that they can find ROM service routines at fixed addresses between $0000-$3fff. More importantly, if the default interrupt mode (IM 1) is set, the Z80 '''will''' jump the program counter to $0038 every frame expecting to find an interrupt handler there. If it does not, pain and suffering will likely result. [[Extended Z80 instruction set#DI|DI]] is your friend. On the plus side, this does allow you to write your own interrupt handler without the nuisance of using IM 2.
 +
 +
Activating [[Layer 2]]'s paging will not affect ROM operation, because it only remaps ''writes'', not reads.
 +
 +
== Screen ==
 +
16k-Bank 5 is the bank read by the ULA to determine what to show on screen. The ULA connects directly to the larger memory space ignoring mapping; the screen is always 16k-Bank 5, no matter where in memory it is (or if it is switched in at all). Setting bit 3 of {{PortNo|$7FFD}} will have the ULA read from 16k-bank 7 (the "shadow screen") instead, which can be used as an alternate screen. Beware that '''this does not map 16k-bank 7 into RAM'''; to alter 16k-bank 7 it must be mapped by other means.
  
 
== SRAM memory map ==
 
== SRAM memory map ==

Revision as of 19:54, 24 September 2017

The Next supports two memory management models which operate in tandem. One is a unique memory management system for the Next. The other is an expanded version of the memory manager from the original Spectrum 128 and +2/+3 series.

Global Memory Map

The total available RAM space of the Next is 512k on an unexpanded Next, or 1536k - 1.5mb - on a Next expanded to 2Mb. (The base Next has 1mb of memory but 512k of it is reserved for the ROMs and firmware.)

The Z80 processor in the next can access only 16k of memory at a time, and so the memory is divided into banks which are used in determining which memory it sees. Spectrum 128k memory management uses 16k banks. Next memory management uses 8k banks.

16k-bank 8k-bank True Address Description
0 0-1 $060000-$063fff Standard RAM, may be used by EsxDOS
1 2-3 $064000-$067fff Standard RAM, contended on 128, may be used by EsxDOS
2 4-5 $068000-$06afff Standard RAM
3 6-7 $06b000-$06ffff Standard RAM, contended on 128, may be used by EsxDOS
4 8-9 $070000-$073fff Standard RAM, contended on +2/+3
5 10-11 $074000-$077fff ULA Screen, contended except on Pentagon
6 12-13 $078000-$07afff Standard RAM, contended on +2/+3
7 14-15 $07b000-$07ffff ULA Shadow Screen, contended except on Pentagon
8 16-17 $080000-$083fff Next RAM, Default Layer 2
9-10 18-21 $084000-$08afff Next RAM
11 22-23 $08b000-$08ffff Next RAM, Default Layer 2 Shadow Screen
12-39 24-79 $090000-$0fffff Next RAM
40-63 80-127 $100000-$15ffff 2Mb Expanded Next RAM (accessible via 128 memory management)
-- 128-207 $160000-$1ffffff 2Mb Expanded Next RAM (accessible via Next management only)

Z80 Visible Memory map

At start up, the 16-bit address space of the Z80 is mapped to memory as follows:

Area 128 Slot Next Slot 128 Default Bank Next Default Bank Description
$0000-$1fff 1 1 ROM ROM (255) Normally ROM. Writes mappable by layer 2. IRQ and NMI routines here.
$2000-$3fff 2 ROM (255) Normally ROM. Writes mapped by Layer 2.
$4000-$5fff 2 3 5 10 Normally used for normal/shadow ULA screen.
$6000-$7fff 4 11 Normally used for normal/shadow ULA screen.
$8000-$9fff 3 5 2 4 Free RAM.
$a000-$bfff 6 5 Free RAM.
$c000-$dfff 4 7 0 0 Free RAM. Only this area is remappable by 128 memory management.
$e000-$ffff 8 1 Free RAM. Only this area is remappable by 128 memory management.

128-style memory management

128-style memory management can only alter the bank addressed at $c000 (slot 4 in 128 terms, slot 7-8 in Next terms). The active bank at $c000 is selected by writing the 3 LSBs of the 16k-bank number to the bottom 3 bits of Memory Paging Control ($7FFD / 32765), and the 3 MSBs to the bottom 3 bits of Next Memory Bank Select ($DFFD / 57341). (The reason for the division is that the original Spectrum 128, having only 128k of memory, only needed 3 bits.)

On an unexpanded Next, this allows any 16k-bank to be paged in at $c000. On an expanded next, there are not enough bits available to access the banks at the bottom of the expanded memory, so Next memory management must be used to access these.

If you are using the standard interrupt handler or OS routines, then any time you write to Memory Paging Control ($7FFD / 32765) you should also store the value at $5B5C. Any time you write to Plus 3 Memory Paging Control ($1FFD / 8189) you should also store the value at $5B67. There is no corresponding system variable for the Next-only Next Memory Bank Select ($DFFD / 57341) and standard OS routines may not support the extended banks properly.

128 Special Paging Mode

"Special paging mode" (also called "AllRam mode" or "CP/M mode") is enabled by writing a value with the LSB set to Plus 3 Memory Paging Control ($1FFD / 8189). Depending on the 3 low bits of this value a memory configuration is selected as follows:

Bits Slot 1 Slot 2 Slot 3 Slot 4
%001 0 1 2 3
%011 4 5 6 7
%101 4 5 6 3
%111 4 7 6 3

Layer 2 Switching

Layer 2 switching can allow any 16k-bank to be written to (but not read) in Slot 1, by writing the 16k-bank number to Template:NextRegNo:$12 and then enabling Layer 2 paging by writing a value with the LSB set to Template:PortNo:$123B.

Writing to this area will then write the appropriate area of memory, whereas reading from it will give the area mapped by other memory management.

Next Memory Management

The 8k-bank accessed in a slot is selected by writing the 8k-bank number to the bottom 7 bits of the 8 Next registers from Template:NextRegNo:$50 upwards. $50 addresses 8k-slot 0, $51 addresses 8k-slot 1, and so on.

In addition, in Slots 1 and 2 only, the ROM can be paged in by writing $FF to the page.

ROM paging and selection

$0000-$3fff is usually mapped to ROM. This area can only be fully remapped using Next memory management. ROM is not considered one of the numbered banks; it is mapped to the two 8k-banks by default, or by setting their 8k-bank numbers to 255.

The 128k Spectrum has 2 ROM pages. Which of these is mapped is selected by altering Bit 4 of Memory Paging Control ($7FFD / 32765). The +2a/+3 has 4 ROM pages; the extra bit needed to select between these is bit 2 of Plus 3 Memory Paging Control ($1FFD / 8189). This maintains compatibility with the original machines' ROM paging as long as the ROM is not paged out.

Paging out ROM

ROM can be paged out by enabling AllRam mode, or by using Next memory management. Beware that some programs may assume that they can find ROM service routines at fixed addresses between $0000-$3fff. More importantly, if the default interrupt mode (IM 1) is set, the Z80 will jump the program counter to $0038 every frame expecting to find an interrupt handler there. If it does not, pain and suffering will likely result. DI is your friend. On the plus side, this does allow you to write your own interrupt handler without the nuisance of using IM 2.

Activating Layer 2's paging will not affect ROM operation, because it only remaps writes, not reads.

Screen

16k-Bank 5 is the bank read by the ULA to determine what to show on screen. The ULA connects directly to the larger memory space ignoring mapping; the screen is always 16k-Bank 5, no matter where in memory it is (or if it is switched in at all). Setting bit 3 of Memory Paging Control ($7FFD / 32765) will have the ULA read from 16k-bank 7 (the "shadow screen") instead, which can be used as an alternate screen. Beware that this does not map 16k-bank 7 into RAM; to alter 16k-bank 7 it must be mapped by other means.

SRAM memory map

The SpecNext site provided the following raw memory map for the Next's entire static physical memory space without memory management:

  • $000000 - ZX Spectrum Rom
  • $010000 - ESXDOS Rom
  • $014000 - Multiface ROM
  • $018000 - Multiface extra ROM
  • $01c000 - Multiface RAM
  • $020000 - DivMMC RAM
  • $060000 - Spectrum 128k Ram
  • $080000 - Extra RAM
  • $0FFFFF - End of addressable space on standard Next
  • $1FFFFF - End of addressable space on 2mb expanded Next

However it is not clear if any Spectrum code will be able to make use of this memory map since the Z80 cores only support 16-bit addresses. This may therefore only be useful for FPGA development.