Memory banking in 128K model

ZX80, ZX 81, ZX Spectrum, TS2068 and other clones
Post Reply
User avatar
jorgegv
Well known member
Posts: 287
Joined: Wed Nov 18, 2020 5:08 pm

Memory banking in 128K model

Post by jorgegv »

Hi people,

I have done my due diligence on the issue of memory banking for the 128K: how to do it inside the program, how to organize the memory for it, and how to instruct the compiler for putting code in each of the banks, and also how to load all this in memory at program load time (the TAP file). I found a great reference about the subject at https://zxspectrumcoding.wordpress.com/ ... ng-part-1/ and the subsequent articles.

These references basically amount to:
  • compile the code for the different sections
  • create a trivial bank switcher routine
  • create a basic loader that calls that routine and loads each bank's contents, then loads the main program in the home bank
  • convert the basic loader to TAP with BAS2TAP
Is there something in Z88DK for automating this kind of setup (since I presume it's done almost always in a similar way for all 128K programs), or the above link is all there is to it? It's not difficult at all and the reference is great, But I'd like to know if special support for it exists in Z88DK, just in case.

Thanks
Jorge
andydansby
Member
Posts: 51
Joined: Fri May 27, 2016 8:58 pm

Re: Memory banking in 128K model

Post by andydansby »

At the time that I wrote that article, there is not an in-built function, all done manually. I'm still working at 1.99C so something might have change in later versions of Z88DK. I wrote that article at the time with Mr. Albright's help.

As for the setup, it's a bit of a pain, but not horrible. I've got a simplified setup at
https://github.com/andydansby/zx-spectr ... ing-sccz80

I might have another bouncing around somewhere as well.

Andy
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Re: Memory banking in 128K model

Post by dom »

If you’re using classic then take a look at the example linked from this section: https://github.com/z88dk/z88dk/wiki/Mor ... -and-zsdcc
User avatar
jorgegv
Well known member
Posts: 287
Joined: Wed Nov 18, 2020 5:08 pm

Re: Memory banking in 128K model

Post by jorgegv »

Mmmm thanks! From the example it seems that using banking is dead easy. Also I'll need to check not onl code pointers, but data as well. My main need for banking now is due to data, not code.

My current setup fpr my engine is newlib+sdcc, but I'll dig into it. Thanks again.

J.
andydansby
Member
Posts: 51
Joined: Fri May 27, 2016 8:58 pm

Re: Memory banking in 128K model

Post by andydansby »

jorgegv wrote: Sun Jun 20, 2021 8:34 am Mmmm thanks! From the example it seems that using banking is dead easy. Also I'll need to check not onl code pointers, but data as well. My main need for banking now is due to data, not code.

My current setup fpr my engine is newlib+sdcc, but I'll dig into it. Thanks again.

J.
User avatar
jorgegv
Well known member
Posts: 287
Joined: Wed Nov 18, 2020 5:08 pm

Re: Memory banking in 128K model

Post by jorgegv »

Hi,

as a personal exercise I have written my design document for memory banking and memory map, you can read it here: https://github.com/jorgegv/rage1/blob/m ... -DESIGN.md

This document is intended for personal use, but any comments are welcome, of course.

Regards
J.
cborn
Well known member
Posts: 267
Joined: Tue Oct 06, 2020 7:45 pm

Re: Memory banking in 128K model

Post by cborn »

Hi,
Do people make a difference between the logic and the fysic banks?
https://worldofspectrum.org/forums/disc ... ent_987738

The ramdrive uses both and uses the table in above link to recalc "page" from"bank"
actualy i am not sure which is which but in basic i can fetch it in a two commands now, rather easy actualy

Recounted RAM BANK, from logic to fysic,
LET logicbank=PEEK (rt+12)
LET rambank =VAL "134670"(logicbank+1)

in c i can TRY, but if it works and if its needed??

Code: Select all

#DEFINE unsigned char rambank[7]="134670"
unsigned char page=rambank[logicbank_number]
where can i find some more specific bankswitch coding?
i searched for ram and page etc but i get to much results.

i wrote a ramdisc viewer in basic and want to try to rewrite it in C, with few or no asm.
https://worldofspectrum.org/forums/disc ... ent_988827
if anyone feels like doing it instead, your very welcome,
Timmy
Well known member
Posts: 392
Joined: Sat Mar 10, 2012 4:18 pm

Re: Memory banking in 128K model

Post by Timmy »

Thanks for this page... I haven't used anything banking related yet. I'd need some time to read about it.

By the way, is this still relevant with classic and sccz80?
nuc1e0n
Member
Posts: 49
Joined: Wed Jun 10, 2020 12:34 am

Re: Memory banking in 128K model

Post by nuc1e0n »

Not sure if it's relevant but the +3 manual documents a function call named DOS_SET_1346 that can shrink the size of the ramdrive.

I have code which calls it like this:

Code: Select all

  DOS_SET_1346 equ 0x013f
  ...
  ld de, 0x601c ; just first half of page 6
  ld hl, 0x7c04 ; just second half of page 6
  ld iy, DOS_SET_1346
  call dodos
cborn
Well known member
Posts: 267
Joined: Tue Oct 06, 2020 7:45 pm

Re: Memory banking in 128K model

Post by cborn »

nuc1e0n wrote: Sat Mar 05, 2022 9:25 am Not sure if it's relevant but the +3 manual documents a function call named DOS_SET_1346 that can shrink the size of the ramdrive.

I have code which calls it like this:

Code: Select all

  DOS_SET_1346 equ 0x013f
  ...
  ld de, 0x601c ; just first half of page 6
  ld hl, 0x7c04 ; just second half of page 6
  ld iy, DOS_SET_1346
  call dodos
that is correct, i dont know all in&out but +3 indeed reserves a memory block for DOS instructions. I think its just above the 2nd screen which is easily overwritten by the ramdrive and visaversa with a new screen. I could try to fetch some info from it, but actualy its the hardware drive ofcourse , the real +3 discdrive. it could be that the RAMfile directory in bank 7 gets corrupted while the file in eg bank 1 remains unharmed. but an 'unerase' on ramdisk, thats probably a tricky one.
but the DOS area should be mapped indeed.
BTW page 6, is that bank 7? to stay in the spirit off 'logic' vs 'fysic', then thats probably that map !!! lets check (somewhere)
nuc1e0n
Member
Posts: 49
Joined: Wed Jun 10, 2020 12:34 am

Re: Memory banking in 128K model

Post by nuc1e0n »

This controls the memory in what the +3 manual calls banks 1, 3, 4 and 6 (hence the name). The second screen is in what the manual calls bank 7. I don't know what you're referring to with this logical vs physical bank distinction. I've just been taking the +3 manual as definitive in this regard.

This DOS_SET_1346 controls the usage of the floppy disk cache size as well as the ramdisk size. The manual also claims the disk cache size can be set to 0 and the the minimum ramdisk size is 4 'blocks'.

(physical ?) bank 7 contains the second screen as you say, but isn't used for the ramdisk as far as I know but rather housekeeping variables for the disk system and editor. At least that's what I was able to glean from reading the rom disassembly of rom 1 at http://www.fruitcake.plus.com/Sinclair/ ... embly4.htm
nuc1e0n
Member
Posts: 49
Joined: Wed Jun 10, 2020 12:34 am

Re: Memory banking in 128K model

Post by nuc1e0n »

Also, bank 0 isn't controlled at all by this call. My understanding is bank 0 is mapped to the top of memory for BASIC's usage.

In my project, I'm using banks 0, 1, 3 and 4 to keep my pages of code in. I have a queue of the most recently used pages and load others from disk or divmmc memory as needed by overwriting the least recently used bank.
cborn
Well known member
Posts: 267
Joined: Tue Oct 06, 2020 7:45 pm

Re: Memory banking in 128K model

Post by cborn »

Hi
the original 128 has in ROM 0

Code: Select all

; ========================
; PAGING ROUTINES - PART 1
; ========================

; ---------------------
; Page Logical RAM Bank
; ---------------------
; This routine converts between logical and physical RAM banks and pages the
; selected bank in.
; Entry: A=Logical RAM bank.

L1C64:  PUSH HL           ; Save BC and HL.
        PUSH BC           ;

        LD   HL,L1C81     ; Physical banks used by RAM disk.
        LD   B,$00        ;
        LD   C,A          ; BC=Logical RAM bank.
        ADD  HL,BC        ; Point to table entry.
        LD   C,(HL)       ; Look up physical page.

        DI                ; Disable interrupts whilst paging.
        LD   A,(BANK_M)   ; $5B5C. Fetch the current configuration.
        AND  $F8          ; Mask off current RAM bank.
        OR   C            ; Include new RAM bank.
        LD   (BANK_M),A   ; $5B5C. Store the new configuration.
        LD   BC,$7FFD     ;
        OUT  (C),A        ; Perform the page.
        EI                ; Re-enable interrupts.

        POP  BC           ; Restore BC and HL.
        POP  HL           ;
        RET               ;

; -------------------------------
; Physical RAM Bank Mapping Table
; -------------------------------
L1C81:  DEFB $01          ; Logical bank $00.
        DEFB $03          ; Logical bank $01.
        DEFB $04          ; Logical bank $02.
        DEFB $06          ; Logical bank $03.
        DEFB $07          ; Logical bank $04.
        DEFB $00          ; Logical bank $05.
you can find it on fruitcake aswell under the first 128 link. i will look to the other configs aswell.
the table at L1C81 is what my proposed definition is refering to. hence my question, what is a "page" and what is a "bank"
the logical bank has to be defined beside the fysical bank.
You refer to the fysical bank purely.

as definition it seems logical to have continues pages from 0 to 5 and those pages all have their own fysic place called rambanks, with their own reference number.
i hope i see it correct. Looking for definition first check the use:
BUT, the above ASm uses both term totaly trouugh each other
-Page Logical RAM Bank , the title.
-between logical and physical RAM banks and pages
-A=Logical RAM bank
-Physical banks
-BC=Logical RAM bank
-physical page.
-whilst paging
-current RAM bank
-Perform the page
- Physical RAM Bank Mapping Table
- DEFB $01 ; Logical bank $00.

there is no difference in above part so:
i state we need a clear definition and i PROPOSE
Fysic = BANK 1 3 4 6 7 0
Logic = PAGE 0 1 2 3 4 5



+3 has more banks?? or more ROMS ?
cborn
Well known member
Posts: 267
Joined: Tue Oct 06, 2020 7:45 pm

Re: Memory banking in 128K model

Post by cborn »

(edit time is realy extreem short)

the RamDrive works with the logical numbering which is the result off "paging", aka going to the next page.
ROM 0 use:
L1DF4: LD A,$04 ; Page in logical RAM bank 4 (physical RAM bank 7).
CALL L1C64 ;

LD E,(IX+$10) ;
LD D,(IX+$11) ; Fetch the address from the current logical RAM bank.
LD A,(IX+$12) ; Logical RAM bank.
CALL L1C64 ; Page in appropriate logical RAM bank.
nuc1e0n
Member
Posts: 49
Joined: Wed Jun 10, 2020 12:34 am

Re: Memory banking in 128K model

Post by nuc1e0n »

The plus 3 still only has 128k of ram, but also has 64k of rom divided into 4 banks of 16k. The +2/+3 machines also have a memory banking mode that puts ram from address 0 to address 65535 which the toastrack 128 doesn't have that is used to run cp/m. It also seems the +3 entry point to resize the ramdisk may not be there on a toastrack.
cborn
Well known member
Posts: 267
Joined: Tue Oct 06, 2020 7:45 pm

Re: Memory banking in 128K model

Post by cborn »

nuc1e0n wrote: Sun Mar 13, 2022 2:33 pm It also seems the +3 entry point to resize the ramdisk may not be there on a toastrack.
I am entirely sure about that one,
on the+2/3 the ramdrive uses a disc-image like the +3 disc has, with tracks and sectors.
The toastrack just has a simplistic field as ramdrive with the apperently later abondend "logic vs fysic" calculations.
I think the +2/3 has not such an convertion at all.
Which means that there have to be TWO distinct memory SYSTEM aproaches with both their on preset of standard values and variables.
Just like DISCiPLE and plusD actualy, but that is a different chapter/thread although you included exdos in the compiling

For memory banking there could be that exdos system or basic system??
then for both all neccecities have to be collected and set in a table ??
nuc1e0n
Member
Posts: 49
Joined: Wed Jun 10, 2020 12:34 am

Re: Memory banking in 128K model

Post by nuc1e0n »

For memory banking there could be that exdos system or basic system??
divmmc and other devices supported by esxdos utilise a completely separate banking mechanism that swaps the rom with divmmc ram banks when io port e3 is written to IIRC. having the program counter register go to 0x1ffa disables the divmmc ram and reenables the rom. I have some code that does this here:

https://github.com/pjshumphreys/querycs ... e2page.asm

Of course, if a device supported by esxdos is plugged into the expansion port of a 128k machine then both memory banking schemes can be used together.

I find divmmc style ram replacing the rom isn't terribly useful on its own, so I use it as a ramdisk to store pages of 16k that get loaded to the top of the z80 address space (128k ram banks) when needed. If divmmc ram isn't available I load them from disk instead which provides a (much much slower) fallback.
nuc1e0n
Member
Posts: 49
Joined: Wed Jun 10, 2020 12:34 am

Re: Memory banking in 128K model

Post by nuc1e0n »

PS. my code won't fit even in the extra 128k that many divmmc devices seem to have. However, loading 16kb of data from a file on a fat32 formatted sd card is super quick (practically equivalent in speed to ldir), so it's not a problem for what I need.
cborn
Well known member
Posts: 267
Joined: Tue Oct 06, 2020 7:45 pm

Re: Memory banking in 128K model

Post by cborn »

a first and small attempt for a toastrack ramdrive viewer
it counts the amount off files. it will have to bankswitch to bank 7 to read the headerinfo
thats the script that fails. i asume the c kernel needs the stack which MIGHT be on bank 0, perhaps.
its just a start
www.cborn.nl/zxfiles/ramdrive018e.c
www.cborn.nl/zxfiles/ramdr018e+test.tap
based on my basic ramdrive viewer:
www.cborn.nl/zxfiles/RAMLOAD012.bas

the struct is not used, i am only collecting header info and once the bankswitching it self works without crashing i can try to implement it
if someone has solutions or other idears , those are welcome.
cborn
Well known member
Posts: 267
Joined: Tue Oct 06, 2020 7:45 pm

Re: Memory banking in 128K model

Post by cborn »

// attempt to build nececary parts
struct rd_header // has to point to ram area in BANK 7
{
unsigned char name[10] ; // FOR y=x TO VAL "x+9": LET u$=u$+CHR$ PEEK y: NEXT y:
unsigned char logicbank ; // : LET logicbank=PEEK VAL "x+12"
unsigned char bk2 ; // : LET bk2=PEEK VAL "x+15":
unsigned char bk3 ;

unsigned int length ; // : LET bk2=PEEK VAL "x+15"
unsigned int strt ; // : LET strt=FN p(VAL "x+10")

unsigned long leng ; // LET leng=FN z(VAL "x+13")
unsigned long start ; // LET start=FN z(VAL "x+10")
unsigned long end ; // : LET end=FN z(VAL "x+16")
};
cborn
Well known member
Posts: 267
Joined: Tue Oct 06, 2020 7:45 pm

Re: Memory banking in 128K model

Post by cborn »

i used a "non exsisting" code : IN A,(c) with BC=32765 , has no INPUT values?? else we would not need sysvar 23388 like Jorge ommits in his example ...
and there is the clear warning RAM0 is used by the c kernel so i have to fetch the headerinfo only and directly return to RAM0, gonna test that
Post Reply