Breaking the 64k barrier - Spectrum Memory Banking
HTML-код
- Опубликовано: 17 авг 2024
- How do you get more than 64K of RAM in an 8 bit computer? Let's look at how memory banking works!
So as explained in the previous video, our little Z80 CPU only has 16 bit address lines, and that means the maximum memory it can address is 64K. In this video let's take a look at how memory banking on the 128K Spectrum and the Spectrum Next works, allowing those machines to break through the 64k barrier!
This video is mostly aimed at people trying to program the Spectrum, but how and why this works is sufficiently technical that I hope it's interesting to more than the five people out there who seem to be programming like this!
The Spectrum +3 has a whole 128K of RAM. At the time this was amazing. They and all other Spectrums are based off a Zilog Z80 CPU. There's 16 address lines, and 8 data lines. The 8 data lines are what makes this an 8 bit CPU. Inside the CPU those 8 lines go to the registers, each of which are 8 bits wide.
Since there's 16 address lines, the CPU can address up to 65,536 different locations. And each of those locations can store a single 8 bit number. So 65,536 different locations, each of which stores a single byte gives our CPU 64K of addressable memory. That's it. 64K is loads, surely we don't need much more?
So on the 128K Spectrums, how's the 128K of RAM and 32K of ROM fit inside the 64K of addressable memory the Z80 can see? Magic! No, really, this stuff is proper magic all the magic is inside a chip which on my +3 is a gate array.
You know the ULA on the Spectrum? That all purpose chip that makes the computer a Spectrum? The Gate Array on the +3 is that with extra bits added.
The gate array on the Speccy +3 contains all the logic that was in the ULA, plus a bunch of other logic for bank switching and so on, also defined using logic equations. It's the same as a bunch of hardware logic gates, just in one cost-reduced package.
It's sort of the great grandfather of the FPGA in the Next. Except the FPGA on the Next is that plus the CPU and everything else to create the whole machine, described using logic equations.
I know you can describe any logic circuit using an FPGA, but is the reverse true? Can you describe every FPGA circuit using real hardware?
You know, with appropriate tools you can totally reprogram the Next's hardware to be something different.
The idea is to partition memory up into banks of a certain size, and then let the programmer attach those banks to parts of the CPU's address space when needed. On the original Spectrums, this was done using 16K banks. On the Spectrum +3 it's a bit different - they updated the design to allow a bit more flexibility so CP/M could be run...
The Next can do all of that, plus its own much more flexible memory paging. On the Next, you use 8K blocks and can switch in and out of any 8K area of the address space.
We'll have to be "careful" to the best of our ability to avoid our code growing into the stack if we write too much, or the stack growing down into our code if we use too many function calls at once.
Doing this is left as an exercise for the reader, have fun! I have no idea how to cope with this, and no doubt future me will encounter a stack overflow at some point.
Uses Red arrow animation pointing on a green screen Stock Videos by Vecteezy.
Social Media
============
Twitter: / ncot_tech
Ko-Fi: ko-fi.com/ncot...
Web version: ncot.uk/spectr...
Chapters
========
00:00 - Start
00:46 - RAM in the Spectrum
03:46 - ULAs, Gate Arrays and FPGAs
05:15 - What is Bank Switching
09:42 - About Memory Mapping
18:00 - MMUs and how to use them
20:29 - Ideas for extra ram
22:24 - Outro - Subscribe!
#Programming
#ZX Spectrum Next
#Memory Banking
Video Title: Breaking the 64k barrier - Spectrum Memory Banking Наука
Just for clarification, on the Spectrum Next the hardware only has the eight MMU registers which indicate what ram page is present in each 8k slot of the cpu's 64 space. The bottom two MMU pages (the first 16K) are special in that if the page selected is the out-of-range FF, then ROM appears there as on the original spectrum machines.
Classic spectrum banking via ports 7FFD (128K) and 1FFD (+3 only) are implemented on top of the MMU registers. So for example, an OUT to port 7FFD will actually change the top 16K and the bottom 16K mapping (made FF) by writing into the affected (four) MMU registers in hardware.
You do also have to think about how your programs are started on the spectrum next.
If they are tap files or a basic program then they are started by the spectrum rom just like on the original models. In this case your program loads into the normal ROM / BANK 5 / BANK 2 / BANK 0 memory configuration that is present on the 128K when user programs run. Any extra parts for extra memory banks will have to be loaded separately by your program just like it was done on the 128K.
If they are NEX programs, which is a format specific to the Spectrum Next, then your program is also loaded into the starting configuration but the NEX loader will also load up other memory banks for you, the contents of which are in the NEX file.
Other types are DOT and SNX with the former meant for programs cooperating with basic and the latter a bit obsolete next to the NEX but suitable for toolchains that only know how to generate snapshots for 128K software.
So if you are writing software that is meant to use the bottom 16K as ram you have the additional step to figure out how to start your program from the initial load which places your first executed code in 5/2/0. It's not difficult but there are some technical steps to worry about.
Writing in C with Z88DK you are in exactly the same situation as in ASM, ie banking is all up to you; the compiler is not going to do it for you. But the Z88DK toolchain is aware of banking and will easily & automatically generate bankswitched binaries for you for NEX and DOT type programs. It separates the compile into two parts -- the "main binary" and stuff that goes into banks. If you don't take steps, code and data is going into the main binary which is the stuff loaded into banks 5, 2, 0. From assembler you specify what banks or memory pages things go into with the SECTION directive. For the C code, you tell the compiler to change where it places code/rodata/data/bss to specific banks instead of the main binary. As you can see the C compiler has four main targets for stuff it generates which corresponds to romable code, intiialized data in ram and intially zero data in ram. The necessity for this is clear if you think a bit about what happens when you create a program destined to be stored in ROM. Only read-only code can go there; you can't store variables there. So the compiler must be able to output these areas separately so that stuff that can be in rom is in rom and stuff that cannot is mapped to ram and properly initialized.
When you're banking, like you pointed out, you have to worry about where your interrupts are going, where your stack is and where your common code is. If you bank in some new code and it uses the library function "strlen", well, strlen better still be visible after the bank is changed. You normally have a section of memory that is common that never gets banked out to hold these sorts of routines. Z88DK has a default memory map that makes the most common good choices automatically but if you depart from that, you might find it necessary to make your own memory map to replace the default. I think for almost all cases the default will do.
Just getting into the 8 bit homebrew hobby. Watching these videos is a great way to learn hw these systems work. Hopefully I'll engineer one of my own one day
Just an FYI. On the ZX81, the screen didn't blank when typing as the SLOW keyword was introduced and the computer ran in that mode by default - it was the ZX80 that blanked the screen when typing. You could place the ZX81 In FAST mode, where it would basically work like a ZX80 in these terms and the screen would blank unless the screen was being addressed - great for computational programming where results were more important than looking at the screen 👍
In answer to your question, I primarily watch the videos to learn new things or refresh my knowledge. This is with a view to return to programming, be that BASIC or Machine Code. Having a detailed understanding of how the architecture works is invaluable to the programming endeavour and I found it is essential to understand the hardware when I used to program in the 80s and 90s.
Loving the little green ball version of James. Would be amazing to see him roll onto the screen or bounce away like a basketball during transitions.
You know... That wouldn't be too hard. 🤔
I used to have a special modified spectrum 48/80k version in the 80's.
All we did was remove the bad 32k chips and placed good 64k chips and added a flipflop with an address/out selection circuit so we could already use bank switching before the 128k was out in the Netherlands.
It was fun to use your own "hidden" programma's at that time.
To bad it got completely cooked due to a Lightning strike in 1986 😢
As a builder of homebrew 6502 rigs, It's always helpful to see how the other camp did things.
64K is plenty enough for most purposes but with clever bank switching, it's possible to do some awesome stuff, like stuffing a powerful dedicated graphics processor right on the side of a bog-standard 64k map. or using a modern SBC to act as a bridge to the big wide world or colossal storage capacity. and it's relatively easy to do IF the hardware allows for flexible use of the memory map. (like using an EEPROM or even SRAM for the addressing logic, so it can be re-jiggered on the fly without having to physically change anything) There's so many cool things a humble 8-bit CPU is capable of if you've got the imagination to use it,
Hey there! Just wanted to drop a line that these videos are great. I don't have any experience with the Speccy's as I'm in the US, but I find it fascinating. :D
I don’t have any nostalgia for the home computers of the 80s but I like your videos for general crossover with 8 and 16 bit retro game console programming. 👍
Thanks for the explanation! I had a Superfo 128 Rev 3H that would run 48K ROM but not 128K and would fail video and bank tests of RAM in the 128 tests. Your video helped me figure out which part of the schematic was doing the bank switching. The HC138 at U4 decoded the port and the HC174 at U50 latched the banking lines and more. Diode D3 was in backwards. DOH! Swapped it around and the computer worked properly.
You can also have a lot of framebuffers. I've used it to pre-render the next N frames, N depending on how much RAM you have.
I came here for interest and amusement!
Really interesting. Thanks for the upload👌👍
Shouldn't that be "That's like putting perfume on a pig", from They Live? 😉
ZX81 screen blanking only in fast mode, the ZX80 however screen blanked on key presses always. the '81 used an isr to drive the screen, the '80 did drove the screen with waiting for keypress.
My spectrum was 80K of ram. Enabling the "faulty" upper half of the 64Kram chips.
Sinclair bought those chips because 32 k was nor working.
Never had any faults switching the upper 32K from low ti high.
My hobby is to find new pokes for infinity lives etc. in new ZX Spectrum from itch and other places. And understand the loader for 128k games in part of that process.
With today's processors, potentially a whole 16 EB could be addressed!
If the memory mapping was done with ordinary logic chips then the owners of old Spectrums could have expanded their best friends memory as well.
The most complicated way I've ever seen to explain simple memory banking.
Well I accomplished this (bank switching) about 35yrs ago on my self-build Z80 system and yes, it was simple. I used an LS373 latch to select one of a bank of 62128 static ram chips. I kept the CE low on every chip as that halves the response time of the chip. The bank required was selected by an I/O write to the LS373.
So when you show the banking diagrams in 7:30 - the bank 2 and 5 are permanently in two slots (0400 and 0800), but you can select them also to be in the same slot at 0C00? Seems odd how they didn't use contiguous memory but used bank 5 and 2 for the two permanent allocations. I guess it came down to selecting certain chips with the chip select line on the hardware on the original Spectrum?
The other 8-bit machine that people had at the time had an interesting approach to bank switching in that you could swap out pretty much everything liberating the machine giving you access to nearly all the memory. The successor to the machine uses various tricks to access this memory including DMA and various banking approaches using the Z register.
I'm writing a game for the TI-99/4a at the moment and the memory map is such that the only way you can get extra memory into the machine is through the cartridge port (8K) but to bank this out, you need to run the banking code outside of the cartridge (to switch the code bank) and the only memory you have (without a ram expansion) is in the 256 bytes of chip ram. They certainly made that machine easy to work with!
Also that looks like a Llamasoft T-Shirt you are wearing there as well :)
And is that ZX81 case an original? I was under the impression that the lettering was red on the production models and more black than grey?
@@arronshutt yeah it’s a real ZX81
It was red but easily wore out - very many are now case colour👍
"I know you can describe any logic circuit using FPGA, but is the reverse true? Can you describe every FPGA circuit using real hardware?"
Yes, you can. Doing such was how computers were built before microprocessors were invented. Rather than having the logic gates in the FPGA and selecting and connecting them with software, you can take logic gates made from 7400 series ICs and wire them up into the desired logic circuit. Such computers formed the third generation of computers. An example of a computer that was built in this way was the Data General Eclipse, as related in the classic book Soul Of A New Machine by Tracy Kidder.
en.m.wikipedia.org/wiki/7400-series_integrated_circuits
Alternatively you can just take discrete transistors, resistors, diodes etc, and build logic gates from them, wiring them all into the desired logic circuit. Such computers formed the second generation of computers, taking over from the first generation of computers, which used vacuum tubes to create logic circuits with. An example of such a computer is the IBM System/360.
en.m.wikipedia.org/wiki/Transistor%E2%80%93transistor_logic
The disadvantages of using such kinds of larger components for creating logic circuits are size (obviously), power usage, and (lack of) speed.
An eight bit ecternal data bus doedn't define the CPU as 8bit, the registers do. The 68EC020 uses 8bit RAM , 32 bit internals.
why not use Z180 in the PLLC pakage that support 1MB of RAM and is Z80 backward cmpatible? It;s allow to use old ZX software and write new one more advanced
4km
65k sebenarnya batas bawah analog atau resolution tak hingga
21k
Sebenarnya lebih sulit di bawah 64k
"Strap on" ? Putting lipstick on pigs ?
Good heavens man, we're British !!!!
How's that for a comment?