Switching RAM in page 2 from a ROM

Page 1/2
| 2

By Metalion

Paragon (1617)

Metalion's picture

03-12-2022, 08:38

Hi everyone,

I'll admit that I'm a complete noob when it comes to slot management. The only thing I do is use a well known (and shared) snippet of code to switch page 2 to ROM at the start of a 32K ROM. But that's all.

I'm working on an MSX1 32K ROM and I'd like to switch, in page 2 (8000h-BFFFh), from ROM to RAM, and vice-versa. How do I do that ?

Thanks.

Login or register to post comments

By aoineko

Paladin (813)

aoineko's picture

03-12-2022, 11:17

If your code that wants to use the RAM on page 2 is on page 1 and the BIOS on page 0, you can use the ENASLT routine (0024h).

In pseudo code, it would look like this:

A = SlotRAM
H = 10000000b ; Page 2
call ENAST
; ... RAM is in slot 2 ...
A = SlotROM
H = 10000000b ; Page 2
call ENAST
; ... Cartridge is back in slot 2 ...

SlotRAM and SlotROM can be easily determined in the boot code of your cartridge.
If your ROM header is on page 1 (at 4000h), then when your code starts, the ROM slot is the one selected on page 1 and the RAM slot is selected on page 3 (It is the BIOS that takes care of setting up this configuration before passing it to the cartridge code).

SlotId format:

 ExxxSSPP
 │   ││└┴─ Primary slot number (00-11)
 │   └┴─── Secondary slot number (00-11)
 └──────── Expanded slot (0 = no, 1 = yes)

To my knowledge, there is no BIOS routine to directly retrieve the ID of the currently selected slot in a page, but it can be done in a few steps.
For example, to retrieve the slot selected in page 2 :

call RSLREG
prim = A & 00110000b >> 4
ext = (EXPTBL + 2) & 10000000b ; Mask only needed for page 0
sub = (SLTTBL + 2) ; Should be 0 is slot not extended
slotId = prim | ext | sub

If you don't want to use the BIOS, I advise you to save all the information (slotId of your cartridge and RAM and content of EXPTBL and SLTTBL) at launch so that you don't have to retrieve them after firing the BIOS.
To retrieve secondary slot information by hand you have to switch page 3 (where the RAM is) to the slot whose register you want to read. You lose your stack for a moment and you have to prevent interrupts. This is not a simple task.

By gdx

Enlighted (6006)

gdx's picture

03-12-2022, 11:40

By Metalion

Paragon (1617)

Metalion's picture

04-12-2022, 18:52

Thanks to both of you for the explanations and code snippets.
I'll make some experimentations.

By sdsnatcher73

Prophet (3810)

sdsnatcher73's picture

04-12-2022, 22:06

Don’t forget that the RAM found and activated by BIOS in page 3 is really just that. There is no guarantee there is also RAM available in page 2 of the same slot.

By gdx

Enlighted (6006)

gdx's picture

05-12-2022, 09:23

The RAM is guarantee on the 8000h-BFFFh page if the MSX has more than 16 KB RAM and the ROM starts on the 4000h-7FFFh page with the header of the same page.

By aoineko

Paladin (813)

aoineko's picture

05-12-2022, 10:40

From what I understood (and this is what the wiki article on memory seems to say), the BIOS stops its scan at the first slot where it finds RAM (on page 3 then 2).
You mean that the scan continues on page 2 even if RAM is found on page 3? Or that the slot on page 3 is also selected on page 2?
This could be a problem if the MSX has only 16 KB (in slot 0) and a RAM expansion cartridge (in a slot >0).

By gdx

Enlighted (6006)

gdx's picture

05-12-2022, 11:05

The system searches for RAM on each page in each slot in the order listed in the Wiki. As soon as the system finds RAM, it goes to the next page below. The RAM search is performed only on pages 3 and 2 in a diskless environment. With disk the research is carried out on the 4 pages and slot numbers are stored at 0F341h-0F344h. Only the Turbo R does the search differently when disks and turbo mode are used.

By aoineko

Paladin (813)

aoineko's picture

05-12-2022, 12:51

Oh. I didn't catch that when I read the wiki.

By Metalion

Paragon (1617)

Metalion's picture

05-12-2022, 14:47

May be a dumb question, but when a 32K ROM starts, at first only the page 1 is switched to the ROM.
So it means that the page 2 is still switched to RAM, before being switched to ROM (by the ROM).
Is that correct ?

By aoineko

Paladin (813)

aoineko's picture

05-12-2022, 19:09

When the BIOS discover a ROM header it switch the page of the starting address to the cartridge's slot (and not necessarly the page where the header is located). So when your program start, only this page point to the cartridge.
If the program don't start in page #2, I thought that page was pointing to Main-ROM but according to GDX it point to first RAM slot found on this page if the machine have 32 KB of RAM or more.

Page 1/2
| 2