Develop a program in cartridge ROM
This page was last modified 11:06, 15 September 2019 by Gdx.

Contents

Develop a program in cartridge ROM

The ROM of an MSX cartridge can take a multitude of forms depending on the needs. Only the programming aspect will be explained here.

The Rom Header

A ROM needs a header to be auto-executed by the system when the MSX is initialized.

After finding the RAM and initializing the system variables, the MSX looks for the ROM headers in all the slots on the memory pages 4000h-7FFFh and 8000h-FFFh. The search is done in ascending order. When a primary Slot is expanded, the search is done in the corresponding secondary Slots before going to the next Primary Slot.
When the system finds a header, it selects the ROM slot only on the memory page corresponding to the address specified in INIT then, runs the program in ROM at the same address. (In short, it makes an inter-slot call.)

A header consists of 16 bytes and should be placed at 4000h or 8000h as below.

Header Name Use
+0 ID Put these first two bytes at 041H and 042H ("AB") to indicate that it is an additional ROM.
+2 INIT Address of the routine to call to initialize a work area or I/O ports, or run a game, etc.
+4 STATEMENT Runtime address of a program whose purpose is to add instructions to the MSX-Basic using CALL.
+6 DEVICE Execution address of a program used to control a device built into the cartridge. For example, a disk interface.
+8 TEXT Pointer of the tokenizen Basic program contained in ROM.
+10 Reserved 6 bytes reserved for future updates.

Note: Unused addresses and reserved bytes have to set to 0000h.

Rom Header description

INIT

This is the first address taken into account. When this address is greater than 0000h, the system selects the ROM slot on the memory slot corresponding to the address and executes the program in ROM at the same address. At the time of program execution, the C register contains the slot number of the ROM in the form F000SSPP. The routine must end with a RET. All registers can be modified by routine except SP. In place of an initialization routine, the ROM may very well contain a game.

Trick if your ROM has a size of 32K (4000h-BFFFh):

1. When INIT is an address between 4010h-7FFFh, you can select the second part (8000h-BFFFh) by running the routine below at start.

	ld	a,c
	ld	h,080h	; The ENASLT routine does not take into account the register L
	call	ENASLT	; Select slot ROM

2. When INIT is an address between 8010h-BFFFh, you can select the first part (4000h-7FFFh) by running the routine below at start.

	ld	a,c
	ld	h,040h	; The ENASLT routine does not take into account the register L
	call	ENASLT	; Select slot ROM

STATEMENT

Processing program of the instruction must reside on the page 4000h-7FFFh.

A instruction called by CALL must have the following format:

CALL <Instruction name> [(variable[, variable][,...])]

Name of the instruction can be up to 15 characters. When the BASIC interpreter finds the instruction CALL, it copies its name into the PROCNM work area (0FD89h) and then searches the slots in ascending order for a STATEMENT address greater than 0000h to transmit the control for that instruction. At this point, the double register HL contains the address of the parameter that follows the name of the statement in the listing. The instruction can be processed. At the output, HL must indicate the next instruction to be processed and the Carry flag must indicate if there has been an error.

Here is an example of a procedure with CALL NAME(0,0) followed by A=0:

  1. The listing therefore contains "CALL NAME (0,0): A=0".
    HL points to the character "(".
    Carry flag = 1.
    PROCNM = "N","A","M","E",00h (00h can be also 3Ah)
  2. Processing of the instruction by the routine at the address specified at STATEMENT.
    If the name does not match then leave HL as is and put Carry at 1 before handing over to the interpreter (by a RET).
    If name matches, execute the statement routine and its parameters then, point the next statement with HL and set Carry to 0 if there is no error in the parameters.
  3. End of treatment.
    HL must point the variable A of A=0.
    Give back to the interpreter (by a RET).

Note: Avoid giving an already existing name to your instruction because according to the position of the ROM in the slots, it could not be taken into account or even cause an error because of the parameters.

DEVICE

This address must be between 4000h-7FFFh. The system can control up to 4 devices per cartridge. The device name must be 15 characters maximum. When the BASIC interpreter encounters a device name, it copies it to the PROCNM work area (0FD89h), then sets the register A to 255, and searches the slots in ascending order for a DEVICE address greater than 0000h to transmit control of the corresponding device.

Here is an example of a procedure with OPEN "NAME:":

  1. The Basic listing contains OPEN "NAME:"
    A = Instruction number (see table below)
    Carry flag = 1
    PROCNM = "N","A","M","E",00h (00h can be also 3Ah)
  2. If the name does not match then set register A to 255 and Carry to 1 before returning the hand to the interpreter (by a RET).
    If name does match, run the control routine then, point the next statement with HL and set Carry to 0 if there is no error in the settings.
  3. End of treatment.
    A = Device identifier (0-3)
    Carry flag = 0 if no error
    Give back to the interpreter (by a RET).

Instruction numbers:

Register A Instruction
0 OPEN
2 CLOSE
4 Random access
6 Sequential output
8 Sequential entry
10 LOC function
12 LOF function
14 EOF function
16 Function FPOS
18 Backup character

TEXT

This TEXT pointer indicates the beginning of the Basic program to be executed automatically at MSX start. First byte of the program is always zero. The program can not have a maximum size of 16K and should be between 8000h-BFFFh. It must also be a tokenized format and not an ASCII text format. In addition, the addresses corresponding to the program line numbers must indicate the actual destination addresses in the program.

Method to put a Basic program in ROM:

  1. A Basic program starts at 08000h on a 64k MSX by default. It must be shifted at least to desired address (08012h for this example) to insert the header of the ROM. To do this, enter the following line under Basic:

    POKE &HF676,&H13: POKE &HF677,&H80: POKE &H8012,0: NEW
  2. Load the Basic program to ROM by entering the following instruction.

    LOAD"Name.BAS"
  3. Then save the program by entering the following instruction.

    SAVE"Name2.BAS"
  4. Put the 08012h address to TEXT in the header of the page 8000h-BFFFh, then replace the first byte (FFh) by 00h in the file "Name2.BAS" and copy its content to the ROM at 08012h.

    Example in assembler:
	org 08000h

BasicPRG:
	db	"AB"
	dw	0,0,0,08012h,0,0,0

	nop
	nop

	incbin "NAME2.BAS"	; The first byte (FFh) must be previously replaced by 00h
	
	ds	4000h - ($ - BasicPRG),0

Create a ROM without mapper

In the chapter The Rom Header you can see that the ROM header can be placed to 4000h or 8000h, or even both. In addition, your program can start from almost any address since the system is making an inter-slot call to the address specified by INIT. The only constraints are the header and interrupts. Indeed, the system interrupt routine is at address 0038h. If you put 03000h to INIT, your ROM will need to have a replacement interrupt routine since it will be selected on page 0000h-3FFFh to be executed. The problem also occurs on page C000h-FFFFh because of Hooks and system variables. You need have a high mastery of system and hardware to choose its pages. Better to choose an address between 4000h and BFFFh and if necessary, use the other two pages to put data (text and graphics for example).

The size of a ROM without mapping can vary in theory from 1kB to 64kB. In practice, it is difficult to find ROMs of less than 16kB.

If the ROM is 48kB, the C000h-FFFFh page will contain undefined values ​​(usually 0FFh).

If the ROM is 32kB or less, depending on the hardware connections the unused parts will contain undefined values ​​or mirrors as shown below.

MSX ROM configs.png

You can use these mirrors to confuse people looking to disassemble your program.

When a Rom is executed on page 4000h-7FFFh, the CPU can see the half of the Main-ROM on the page 0000h-3FFFh and the available Main-RAM on the other pages.

MSX ROM 4000h.png

When a Rom is executed on page 8000h-CFFFh, the CPU can see the the Main-ROM on the pages 0000h-3FFFh and 4000h-7FFFh, and the available Main-RAM on the top page.

MSX ROM 8000h.png

Typical examples to make a 32kB ROM

Below is an example for a ROM that start from page 4000h-7FFFh.

LF	equ	0Ah
CR	equ	0Dh

INIT32	equ	006Fh
CHPUT	equ	00A2h	; Address of character output routine of BIOS

PageSize	equ 4000h	; 16kB

LINL32	equ	0F3AFh
EXPTBL	equ	0FCC1h		; Extended slot flags table (4 bytes)

	org 4000h
 
; ### ROM header ###

	db "AB"		; ID for auto-executable ROM
	dw INIT		; Main program execution address.
	dw 0		; STATEMENT
	dw 0		; DEVICE
	dw 0		; TEXT
	dw 0,0,0	; Reserved

INIT:	; Program code entry point label

	ld	a,32
	ld	(LINL32),a	; 32 columns
	call	INIT32		; SCREEN 1

; Typical routine to select the ROM on page 8000h-BFFFh from page 4000h-7BFFFh

	call	0138h
	rrca
	rrca
	and	3	;Keep bits corresponding to the page 8000h-BFFFh
	ld	c,a
	ld	b,0
	ld	hl,EXPTBL
	add	hl,bc
	ld	a,(hl)
	and	80h
	or	c
	ld	c,a
	inc	hl
	inc	hl
	inc	hl
	inc	hl
	ld	a,(hl)
	and	0Ch
	or	c
	ld	h,080h
	call	024h		; Select the ROM on page 8000h-BFFFh

	ld	hl,Page4000hTXT	; Text pointer into HL
	call	Print		; Call the routine Print below
 
	jp	08000h	; Jump to above page.
 
Print:
	ld	a,(hl)		; Load the byte from memory at address indicated by HL to A.
	and	a		; Same as CP 0 but faster.
	ret	z		; Back behind the call print if A = 0
	call	CHPUT		; Call the routine to display a character.
	inc	hl		; Increment the HL value.
	jr	Print		; Relative jump to the address in the label Print.
 
 
; Message data
Page4000hTXT:			; Text pointer label
	db "Text from page 4000h-7FFFh",LF,CR,0	; Zero indicates the end of text

; Padding with 255 to make a fixed page of 16K size
; (Alternatively, include macros.asm and use ALIGN 4000H)

	ds PageSize - ($ - 4000h),255	; Fill the unused aera in page with 0FFh

; Begin of page 8000h-BFFFh

	ld	hl,Page8000hTXT	; Text pointer
	call	Print		; Call the routine Print
 
Finished:
	jr	Finished	; Jump to itself endlessly.

Page8000hTXT:			; Text pointer label
	db "Text from page 8000h-BFFFh",0	; Zero indicates the end of text.

	ds PageSize - ($ - 8000h),255	; Fill the unused aera with 0FFh

Note: "ds PageSize - ($ - 4000h),255" is here just for the example. You can remove it and replace "ds PageSize - ($ - 8000h),255" at end by "ds PageSize - ($ - 4000h),255" to make your own ROM.

Below is an example for a ROM that start from page 8000h-BFFFh.

LF	equ	0Ah
CR	equ	0Dh

INIT32	equ	006Fh
CHPUT	equ	00A2h	; Address of character output routine of BIOS

PageSize	equ 4000h	; 16kB

LINL32	equ	0F3AFh
EXPTBL	equ	0FCC1h		; Extended slot flags table (4 bytes)
 
	org 4000h
 
	ld	hl,Page4000hTXT	; Text pointer into HL
	call	Print		; Call the routine Print
 
Finished:
	jr	Finished	; Jump to itself endlessly.

; Message data
Page4000hTXT:			; Text pointer label
	db "Text from page 4000h-7FFFh",0	; Zero indicates the end of text

; Padding with 255 to make a fixed page of 16K size
; (Alternatively, include macros.asm and use ALIGN 4000H)

	ds PageSize - ($ - 4000h),255	; Fill the unused aera in page with 0FFh

; Begin of page 8000h-BFFFh

; ### ROM header ###

	db "AB"		; ID for auto-executable ROM
	dw INIT		; Main program execution address.
	dw 0		; STATEMENT
	dw 0		; DEVICE
	dw 0		; TEXT
	dw 0,0,0	; Reserved

INIT:	; Program code entry point label

	ld	a,32
	ld	(LINL32),a	; 32 columns
	call	INIT32		; SCREEN 1

; Typical routine to select the ROM on page 4000h-7FFFh from page 8000h-BFFFh

	call	0138h
	rrca
	rrca
	rrca
	rrca
	and	3	;Keep bits corresponding to the page 4000h-7FFFh
	ld	c,a
	ld	b,0
	ld	hl,EXPTBL
	add	hl,bc
	ld	a,(hl)
	and	80h
	or	c
	ld	c,a
	inc	hl
	inc	hl
	inc	hl
	inc	hl
	ld	a,(hl)
	and	0Ch
	or	c
	ld	h,040h
	call	024h		; Select the ROM on page 4000h-7FFFh

	ld	hl,Page8000hTXT	; Text pointer
	call	Print		; Call the routine Print below
 
	jp	04000h	; Jump to below page.
 
Print:
	ld	a,(hl)		; Load the byte from memory at address indicated by HL to A.
	and	a		; Same as CP 0 but faster.
	ret	z		; Back behind the call print if A = 0
	call	CHPUT		; Call the routine to display a character.
	inc	hl		; Increment the HL value.
	jr	Print		; Relative jump to the address in the label Print.

Page8000hTXT:			; Text pointer label
	db "Text from page 8000h-BFFFh",LF,CR,0	; Zero indicates the end of text.

	ds PageSize - ($ - 8000h),255	; Fill the unused aera with 0FFh

Example to make a 48kB ROM

Below is an example for a 48kB ROM that start from page 4000h-7FFFh. In this example, interrupts are disabled during page 0000h-3FFFh is selected, and since BIOS routines are absent the text is displayed by making direct access to the VDP. You will also find a routine to put back the BIOS. Better to add an interrupt routine if page 0 needs to be selected longer.

LF	equ	0Ah
CR	equ	0Dh

SETWRT	equ	0053h	; set address to write in VRAM
INIT32	equ	006Fh
CHPUT	equ	00A2h	; Address of character output routine of BIOS

PageSize	equ	4000h	; 16kB

LINL32	equ	0F3AFh
T32NAM  equ	0F3BDh
CSRX	equ	0F3DDh
CSRY	equ	0F3DCh
EXPTBL	equ	0FCC1h		; Extended slot flags table (4 bytes)
 
	org 0000h

	ld	hl,Page0000hTXT	; Text pointer into HL
	call	PrintP0		; Call the routine Print for page 0
	ret

PrintP0:
	ld	a,(hl)		; Load the byte from memory at address indicated by HL to A.
	cp	LF
	jr	z,Code_LF
	cp	CR
	jr	z,Code_CR
	and	a		; Same as CP 0 but faster.
	ret	z		; Back behind the call print if A = 0
	out	(098h),a	; Call the routine to display a character.
	inc	hl		; Increment the HL value.
	push	hl
	ld	hl,CSRX
	inc	(hl)
	pop	hl
	jr	PrintP0		; Relative jump to the address in the label Print.

Code_CR:
	push	af
	ld	a,1
	ld	(CSRX),a
	pop	af
	inc	hl		; Increment the HL value.
	jr	PrintP0
Code_LF:
	push	hl
	ld	hl,CSRY
	inc	(hl)
	pop	hl
	inc	hl		; Increment the HL value.
	jr	PrintP0

; Message data
Page0000hTXT:			; Text pointer label
	db "Text from page 0000h-3FFFh",LF,CR,0	; Zero indicates the end of text

	ds PageSize - $,255	; Fill the unused aera with 0FFh

;--------------------------
; Begin of page 4000h-3FFFh
;--------------------------

; ### ROM header ###

	db "AB"		; ID for auto-executable ROM
	dw INIT		; Main program execution address.
	dw 0		; STATEMENT
	dw 0		; DEVICE
	dw 0		; TEXT
	dw 0,0,0	; Reserved

INIT:	; Program code entry point label

	ld	a,32
	ld	(LINL32),a	; 32 columns
	call	INIT32		; SCREEN 1
	ld	hl,(T32NAM)
	call	SETWRT		; Set the VRAM address to write the text

; Routine to select the ROM on page 0000h-3FFFh from page 4000h-7BFFFh

	in	a,(0A8h)
	and	0FCh		; xxxxxx00
	ld	b,a
	and	0Ch		; 0000xx00
	rrca
	rrca			; 000000xx
	or	b
	di
	out	(0A8h),a	; Select the ROM on page 0000h-3fffh

	ld	a,(EXPTBL)
	bit	7,a
	jr	nz,NO_SS	; Jump if primary slot
	ld	a,(0FFFFh)
	cpl			; reverse all bits
	and	0FCh		; xxxxxx00
	ld	b,a
	and	0Ch		; 0000xx00
	rrca
	rrca			; 000000xx
	or	b
	ld	(0FFFFh),a	; ROM Selection (Secondary Slot)
NO_SS:

	call	0000h

; Routine to re-select the Main-ROM on page 0000h-3FFFh

	in	a,(0A8h)
	and	0FCh		; xxxxxx00
	ld	b,a
	ld	a,(EXPTBL)
	and	3		; 000000xx
	or	b
	out	(0A8h),a	; Select the primary slot of Main-ROM on page 0000h-3fffh

	ld	a,(EXPTBL)
	bit	7,a
	jr	nz,NO_SS2	; jump if primary slot
	and	0F3h		; xxxx00xx
	rrca
	rrca			; 000000xx
	ld	b,a
	ld	a,(0FFFFh)
	cpl
	and	3		; 000000xx
	or	b
	ld	(0FFFFh),a	; Select the secondary slot of Main-ROM on page 0000h-3fffh
NO_SS2:
	ei

; Typical routine to select the ROM on page 8000h-BFFFh from page 4000h-7BFFFh

	call	0138h
	rrca
	rrca
	and	3	;Keep bits corresponding to the page 8000h-BFFFh
	ld	c,a
	ld	b,0
	ld	hl,EXPTBL
	add	hl,bc
	ld	a,(hl)
	and	80h
	or	c
	ld	c,a
	inc	hl
	inc	hl
	inc	hl
	inc	hl
	ld	a,(hl)
	and	0Ch
	or	c
	ld	h,080h
	call	024h		; Select the ROM on page 8000h-BFFFh

	ld	hl,Page4000hTXT	; Text pointer into HL
	call	Print		; Call the routine Print below
 

	jp	08000h	; Jump to above page.
 
Print:
	ld	a,(hl)		; Load the byte from memory at address indicated by HL to A.
	and	a		; Same as CP 0 but faster.
	ret	z		; Back behind the call print if A = 0
	call	CHPUT		; Call the routine to display a character.
	inc	hl		; Increment the HL value.
	jr	Print		; Relative jump to the address in the label Print.
 
 
; Message data

Page4000hTXT:			; Text pointer label

	db "Text from page 4000h-7FFFh",LF,CR,0	; Zero indicates the end of text

; Padding with 255 to make a fixed page of 16K size
; (Alternatively, include macros.asm and use ALIGN 4000H)

	ds PageSize - ($ - 4000h),255	; Fill the unused area in page with 0FFh

;--------------------------
; Begin of page 8000h-BFFFh
;--------------------------

	ld	hl,Page8000hTXT	; Text pointer
	call	Print		; Call the routine Print
 
Finished:
	jr	Finished	; Jump to itself endlessly.

Page8000hTXT:			; Text pointer label
	db "Text from page 8000h-BFFFh",0	; Zero indicates the end of text.

	ds PageSize - ($ - 8000h),255	; Fill the unused aera with 0FFh

Create a ROM with disks support

There are two methods create a ROM with disks support. The first uses the hook H.STKE and the second is to launch the ROM from a small Basic program in ROM.

Method that uses the hook H.STKE

H.STKE (0FEDAh) is called after searching in each Slot the executable ROMs when initializing the MSX, just before the system starts the Basic environment. This Hook can therefore allow you to automatically run your ROM with the installed disks.

This example below saves 16 bytes (C500h-C500Fh) in the file "DATA.DAT" on the current disk. Because the error are not fully treated the back to Basic environment if the floppy disk not insered, etc.

LF	equ	0Ah
CR	equ	0Dh

INIT32	equ	006Fh
CHPUT		equ 0A2h	; Set the address of character output routine of main Rom BIOS

PageSize	equ 4000h	; 16kB
FCBinRAM	equ 0C000h

FCBBASE	equ	0F353h
LINL32	equ	0F3AFh
BDOS	equ	0F37Dh
EXPTBL	equ	0FCC1h		; Extended slot flags table (4 bytes)
H_STKE	equ	0FEDAh
H_PHYD	equ	0FFA7h

	org 4000h
 
; ROM header (Put 0000h as address when unused)

	db "AB"		; ID for auto-executable ROM
	dw INIT		; Main program execution address.
	dw 0		; STATEMENT
	dw	0	; DEVICE
	dw 0		; TEXT (Unused on this page)
	dw 0,0,0	; Reserved
 

INIT:	; Program code entry point label

	ld	a,c
	ld	(H_STKE+1),a

	ld	hl,NewH_STKE
	ld	de,H_STKE
	ld	bc,4
	ldir		; Copy the routine to execute the ROM to the hook

	ret	; Back to slots scanning

; Routine to execute the ROM

NewH_STKE:
	rst	030h	; Inter-slot call
	db	1	; This byte will be replaced by the slot number of ROM
	dw	ROM_Exe	; Address to execute the ROM

ROM_Exe:
	ld	a,0C9h
	ld	(H_STKE),a	; Remove the hook 

	ld	a,32
	ld	(LINL32),a	; 32 columns
	call	INIT32		; SCREEN 1

	ld	a,(H_PHYD)
	cp	0C9h		; 
	jr	nz,DSK_Found	; Jump if disk installed 

	ld	hl, NoDisk_TXT	; Text pointer into HL
	call	Print		; Call the routine Print below

	jr	NeverEndLoop

DSK_Found:
	ld	hl,Save_TXT	; Text pointer into HL
	call	Print		; Call the routine Print below

	ld	hl,FCBinRAM
	ld	(FCBBASE),hl	; Set FCB pointer to 0C000h
	ex	hl,de
	ld	hl,FCB
	ld	bc,128
	ldir		; Initialises the FCB data in RAM

	ld	c,1Ah	; pointer to data to save
	ld	de,0C500h
	call	BDOS

	ld	c,016h	; Create file
	ld	de,FCBinRAM
	call	BDOS
	or	a
	jp	nz,ERROR

	ld	hl,1
	ld	(FCBinRAM+14),hl	; Record size = 1 byte

	ld	c,026h	; Write file
	ld	de,FCBinRAM
	ld	hl,10h
	call	BDOS	; Save 16 bytes (0C500h-0C50Fh)
	or	a
	jp	nz,ERROR

	ld	c,010h	; Close file
	ld	de,FCBinRAM
	call	BDOS
	or	a
	jp	nz,ERROR

	ld	hl,SaveOK_TXT	; Text pointer into HL
	jr	SaveMES
ERROR:
	ld	hl,SaveERR_TXT
SaveMES:
	call	Print		; Call the routine Print below

NeverEndLoop:
	jr	NeverEndLoop

Print:
	ld	a,(hl)		; Load the byte from memory at address indicated by HL to A.
	and	a		; Same as CP 0 but faster.
	ret	z		; Back behind the call print if A = 0
	call	CHPUT		; Call the routine to display a character.
	inc	hl		; Increment the HL value.
	jr	Print		; Relative jump to the address in the label Print.

; Data
NoDisk_TXT:			; Text pointer label
	db "No disk installed!",LF,CR
	db "Turn off the MSX.",0	; Zero indicates the end of text
Save_TXT:
	db "Saving",022h,"DATA.DAT",022h,"...",LF,CR,0
SaveOK_TXT:
	db "File saved",LF,CR,0
SaveERR_TXT:
	db "File error!!!",LF,CR,0
FCB:
	db 0,"DATA    DAT"
	ds 116,0	; Fill the rest of FCB with 00h

	ds PageSize - ($ - 4000h),255	; Fill the unused aera in page with 0FFh

Create a ROM with mapper

Examples to make a 128kB ROM for ASCII 16k mapper

The method varies depending on the assembler used. Please refer to the manual for how to manage the segments.

Example for Glass assembler:

; Example to create an MegaRom of 128kB that use an ASCII 16K Mapper
; for glass assembler

Seg0:	ds	4000H
Seg1:	ds	4000H
Seg2:	ds	4000H
Seg3:	ds	4000H
Seg4:	ds	4000H
Seg5:	ds	4000H
Seg6:	ds	4000H
Seg7:	ds	4000H

LF	equ	0Ah
CR	equ	0Dh

INIT32	equ	006Fh
CHPUT	equ	0A2h	; Set the address of character output routine of main Rom BIOS

PageSize	equ	04000h	; 16kB
Seg_P8000_SW	equ	07000h	; Segment switch on page 8000h-BFFFh (ASCII 16k Mapper)

LINL32	equ	0F3AFh
EXPTBL	equ	0FCC1h		; Extended slot flags table (4 bytes)


SECTION	Seg0

	org	4000h
	db	41h,42h
	dw	INIT,0,0,0,0,0,0

INIT:
	ld	a,32
	ld	(LINL32),a	; 32 columns
	call	INIT32		; SCREEN 1

; Typical routine to select the ROM on page 8000h-BFFFh from page 4000h-7BFFFh

	call	0138h
	rrca
	rrca
	and	3	;Keep bits corresponding to the page 8000h-BFFFh
	ld	c,a
	ld	b,0
	ld	hl,EXPTBL
	add	hl,bc
	ld	a,(hl)
	and	80h
	or	c
	ld	c,a
	inc	hl
	inc	hl
	inc	hl
	inc	hl
	ld	a,(hl)
	and	0Ch
	or	c
	ld	h,080h
	call	024h		; Select the ROM on page 8000h-BFFFh

	ld	a,1
LOOP:
	ld	(Seg_P8000_SW),a

	push	af
	ld	hl,Seg1_TXT	; Text pointer into HL
	call	Print		; Call the routine Print below
	pop	af

	inc	a
	cp	8
	jr	nz, LOOP	; Jump to LOOP if A<8
 
Finished:
	jr	Finished	; Jump to itself endlessly.

Print:
	ld	a,(hl)		; Load the byte from memory at address indicated by HL to A.
	and	a		; Same as CP 0 but faster.
	ret	z		; Back behind the call print if A = 0
	call	CHPUT		; Call the routine to display a character.
	inc	hl		; Increment the HL value.
	jr	Print		; Jump to the address in the label Print.
 
SECTION	Seg1
	org	8000h
Seg1_TXT:			; Text pointer label
	db "Text from segment 1",LF,CR,0	; Zero indicates the end of text.
ENDS

SECTION	Seg2
	org	8000h
	db "Text from segment 2",LF,CR,0
ENDS

SECTION	Seg3
	org	8000h
	db "Text from segment 3",LF,CR,0
ENDS

SECTION   Seg4
	org	8000h
	db "Text from segment 4",LF,CR,0
ENDS

SECTION   Seg5
	org	8000h
	db "Text from segment 5",LF,CR,0
ENDS

SECTION   Seg6
	org	8000h
	db "Text from segment 6",LF,CR,0
ENDS

SECTION   Seg7
	org	8000h
	db "Text from segment 7",LF,CR,0
ENDS

Example for zasm assembler:

; Example to create an MegaRom of 128kB that use an ASCII 16K Mapper
; for zasm assembler

LF	equ	0Ah
CR	equ	0Dh

INIT32	equ	006Fh
CHPUT	equ	0A2h	; Set the address of character output routine of main Rom BIOS

PageSize	equ	04000h	; 16kB
Seg_P8000_SW	equ	07000h	; Segment switch on page 8000h-BFFFh (ASCII 16k Mapper)

LINL32	equ	0F3AFh
EXPTBL	equ	0FCC1h		; Extended slot flags table (4 bytes)


#target rom
#code	Seg0,04000h,04000h

	db	41h,42h
	dw	INIT,0,0,0,0,0,0

INIT:
	ld	a,32
	ld	(LINL32),a	; 32 columns
	call	INIT32		; SCREEN 1

; Typical routine to select the ROM on page 8000h-BFFFh from page 4000h-7BFFFh

	call	0138h
	rrca
	rrca
	and	3	;Keep bits corresponding to the page 8000h-BFFFh
	ld	c,a
	ld	b,0
	ld	hl,EXPTBL
	add	hl,bc
	ld	a,(hl)
	and	80h
	or	c
	ld	c,a
	inc	hl
	inc	hl
	inc	hl
	inc	hl
	ld	a,(hl)
	and	0Ch
	or	c
	ld	h,080h
	call	024h		; Select the ROM on page 8000h-BFFFh

	ld	a,1
LOOP:
	ld	(Seg_P8000_SW),a

	push	af
	ld	hl,Seg1_TXT	; Text pointer into HL
	call	Print		; Call the routine Print below
	pop	af

	inc	a
	cp	8
	jr	nz, LOOP	; Jump to LOOP if A<8
 
Finished:
	jr	Finished	; Jump to itself endlessly.

Print:
	ld	a,(hl)		; Load the byte from memory at address indicated by HL to A.
	and	a		; Same as CP 0 but faster.
	ret	z		; Back behind the call print if A = 0
	call	CHPUT		; Call the routine to display a character.
	inc	hl		; Increment the HL value.
	jr	Print		; Jump to the address in the label Print.
 
#code	Seg1,08000h,04000h

Seg1_TXT:			; Text pointer label
	db "Text from segment 1",LF,CR,0	; Zero indicates the end of text.

#code   Seg2,08000h,04000h

	db "Text from segment 2",LF,CR,0

#code   Seg3,08000h,04000h

	db "Text from segment 3",LF,CR,0

#code   Seg4,08000h,04000h

	db "Text from segment 4",LF,CR,0

#code   Seg5,08000h,04000h

	db "Text from segment 5",LF,CR,0

#code   Seg6,08000h,04000h

	db "Text from segment 6",LF,CR,0

#code   Seg7,08000h,04000h

	db "Text from segment 7",LF,CR,0

END