cartridge called twice, memory allocation

Page 1/2
| 2

By hit9918

Prophet (2927)

hit9918's picture

06-02-2014, 16:08

Why does this cartridge get called twice:

base equ #4000
	org base

	db #41
	db #42
	dw start
	dw 0,0,0,0,0,0,0,0
start:
	di
	ld bc,256
	ld hl,(0xfc4a)
	and a
	sbc hl,bc
;	ld (0xfc4a),a
loop:
;	ld (hl),l
	inc hl
	dec bc
	ld a,b
	or c
	jp nz,loop
	ret
end:
	ds #8000 - end

Next question:
what I actualy wanted to do is to make a cartridge that allocates memory in sysarea.
and then see what happens with all the shifted disk ROM addresses.

I outdocumented the instructions that actualy do it and now get the doublecall thing.

how does a cartridge allocate memory?
and what is it with this double calling.

Login or register to post comments

By hit9918

Prophet (2927)

hit9918's picture

06-02-2014, 16:10

ok and in the outdocumented code I just spot a very funny bug. still wondering about the double calling.

By hit9918

Prophet (2927)

hit9918's picture

06-02-2014, 16:25

And the below code again gets called twice.
And afterwards diskROM blindly stomps on my HIMEM allocation.
Is clear, it pastes its fixed address hooks there.

But there is 512 bytes less memory free,
like a catridge allocating 256 bytes and getting called twice.

But how to do it without wreckage.
How to allocate memory?
How to not get called twice?

base equ #4000
	org base

	db #41
	db #42
	dw start
	dw 0,0,0,0,0,0,0,0
start:
	di
	ld bc,256
	ld hl,(0xfc4a)
	and a
	sbc hl,bc
	ld (0xfc4a),hl
loop:
	ld (hl),l
	inc hl
	dec bc
	ld a,b
	or c
	jp nz,loop
	ret
end:
	ds #8000 - end

By hit9918

Prophet (2927)

hit9918's picture

06-02-2014, 19:14

Well funny things happen.

Cartridges get called two times. But no documentation.

But it is NOT like the idea that comes to mind,
first cartridge 1 then cart 2 then again 1 then again 2.
like two rounds so one can wait for another cartridge to init.
no, a cartridge gets called two times in a row, funny.

But the openmsx cbios MSX2+ calls cartridges only one time.
And cartridges behind a disk ROM dont get called at all.

Further, sjasm v0.42 crashes while the .out file is selected as cartridge in openmsx. windows.

BTW is there a disk ROM for cbios?
Like running apps that got no BASIC.

By zeilemaker54

Champion (345)

zeilemaker54's picture

06-02-2014, 19:21

I very much doubt if the methode you use is allowed in the first place, provided you want to interact with MSX-Basic. You are basically overwrite all kind of BASIC stuff already initialzed below HIMEM. If you cartridge needs MSX-Basic to start afterwards, you better reinitialize all kind of BASIC pointers, see http://msxsyssrc.cvs.sourceforge.net/viewvc/msxsyssrc/disk100/disk.mac?view=markup, line 5302

Futhermore, the diskkernel 1.0 does not support that HIMEM is anything other than 0F380H when it is initialized. The static diskkernel variables are hardcoded between 0F1C9H and 0F37FH. So then you must be sure your cartridge starts AFTER the diskkernel is initialized (so it must be in a higher slot than the diskkernel ROM). Diskkernel 2.0 does not even initialize when HIMEM is anything other than 0F380H.
If your cartridge needs to cooperate with the Diskkernel, your cartridge must use hook H.STKE or H.CLEA to gain control AFTER diskkernel is initialized.
Hope this helps

By hit9918

Prophet (2927)

hit9918's picture

06-02-2014, 20:35

@zeilemaker54,
thanks for the pointer to that routine.
Does it also copy the contents of stack to new place?

But, the hooks you told, what should I do with them?

By zeilemaker54

Champion (345)

zeilemaker54's picture

06-02-2014, 21:44

Yes, the routine does copy the stack content to the new place.

So basically the cartridge should do the following:

cart_init:
; hook H.CLEA with cart_clea
ret

cart_clea:
; this is called as soon as MSX-basic is initialized (also works when no diskrom is present)
push hl ; save pointer used by the basic interpreter
; clear H.CLEA
; allocate memory with example routine from diskrom, which does adjust basic pointers and HIMEM
; save pointer to your workarea somewhere (maybe SLTWRK entry ?) for later use
pop hl ; restore pointer
ret

By Guillian

Prophet (3516)

Guillian's picture

06-02-2014, 23:26

Probably it is called twice because that ROM is mirrored. So the BIOS finds it at page 1 and 2.
Try a 32K ROM or set it visible only in one page.

By hit9918

Prophet (2927)

hit9918's picture

07-02-2014, 02:07

lucky there is SLTWRK for 4 pages, so I can save the hook, would not fit in 2 bytes.
So multiple cartridges can install this way.

The hook restorage, things would not work if at the end someone would install a permanent hook.
He would jump to my hook and my cleanup would remove his one, by putting the one I saved, an older one.

By hit9918

Prophet (2927)

hit9918's picture

07-02-2014, 02:40

@Guillian, yes that was it, mirroring.
Then bios reads start address and program counter still ends up in 0x4000 area.
With RAM selected in 0x8000 area like nothing happened.

By hit9918

Prophet (2927)

hit9918's picture

15-03-2014, 16:32

I got a problem "BASIC not respects himem".

I wrote my own allocation routine,
but I got no hope that trying replacement with disc.mac routine does help,
because I intialized all the himem filbuf etc,
so I wanted to ask whether is known the troublemaker.

I get it to work fully when in a diskloader I remove the "BASIC remembers old things" this way:

10 poke &hfc18,peek(&hfc4a) 'save HIMEM so the installer knows the actual thing
20 poke &hfc19,peek(&hfc4b)
30 clear 200,&hc400		'get BASIC down, so later it not remembers high things
100 bload"tst.bin",r,&hc400	'the installer
110 clear 200,peek(&hfc4a)+peek(&hfc4b)*256 'tell BASIC the new himem
					'if the "remembered old HIMEM" were higher than new himem, BASIC CLEAR might mess something there!

In a cartridge installer, that BASIC nannying doesn't work.
I leave H.CLEA with changed himem etc etc but BASIC remembers the old, reports the usual "bytes free"!

again, a clear 200,peekw(himem) fixes things
(and doing so it might still poke above in "old zone", though this time I got no crash, was maybe just luck).

Page 1/2
| 2