How low is too low?

Discuss game and other programming topics not specifically covered in another forum

Moderator: Programming Moderators

Post Reply
gskronn
Posts: 12
Joined: Wed Sep 13, 2017 1:14 pm

How low is too low?

Post by gskronn » Thu May 23, 2019 10:26 am

Hiyas

One thing that I can't seem to be able to find a definite answer for (probably due to my own ineptness :P) is how low can I safely .org my code? Assuming a scenario where I am only using esxdos and layer2 graphics, its going to be somewhere beyond 0x4000 because I'll need the rom to be in place. But where exactly? How much ram is used by the system in this scenario and where is it located?

Thanks in advance
Gerhard

Ped7g
Posts: 90
Joined: Mon Jul 16, 2018 7:11 pm

Re: How low is too low?

Post by Ped7g » Fri May 24, 2019 6:20 am

esx calls don't need RAM (except stack must be valid upon entry, not sure about its size, would rather play it safe and have 50B at least there, but maybe it's just 2-4B needed before ROM pages its own scratch RAM for its internal stack ... I don't have board to try).

So for ESX calls you need (AFAIK) only both ROM pages (MMU0=255, MMU1=255 and that should be enough, if you didn't use ZX128 memory controls to modify particular ROM).

When not calling ESX and using only your own code and Layer2, you can map anything anywhere - I often map whole Layer2 into 0000-BFFF range (MMU0 .. MMU5) and draw into it by code in MMU6, just to avoid any banking. Then remap it back to ROM before calls. And when you have anything mapped anywhere, the lowest possible value for org is of course "0" (you can map your interrupt handler at 0x38 with MMU0 and use IM1 instead of IM2, just keep it disabled when mapping ROM back)

Also it depends how you load the code, if you want classic BASIC loader with classic ULA, then with minimal loader like

Code: Select all

1 CLEAR VAL "24575" : LOAD "" CODE VAL "24576" : RANDOMIZE USR VAL "24576"
you can use `org 24576` (0x6000), just like on classic ZX48/ZX128.

If you switch to Timex modes in BASIC, those use also 0x6000-0x7800 for screen data, so I actually don't know how Next BASIC does cope with this. I guess then the relatively "safe bet" first address is 0x8000.

The ZX128 (and probably Next BASIC) store also extra system/editor variables in bank 7, so if you want ever to return to BASIC, you will have to follow more rules, but A) didn't sound like that. B) I don't know what must be preserved and I'm interested into this info too.

I.e. overall for init/loader purposes 0x8000..BFFF is quite good start (with CLEAR 32767 in BASIC ahead of loading the code, also this range doesn't clash with classic ZX128 bank at C000). After that, depends how much you want to take control over the machine and what you want to preserve, but there's basically no limit if you want full control, and just run ESX calls sometimes.

----

edit: and about Layer2 - the display unit reads next register 0x12: https://specnext.dev/wiki/Layer_2_RAM_Page_Register
For 16ki bank number and draws pixels from the three consecutive banks (they must be from the first memory chip on board, so only banks 0-15 are valid, i.e. "13" is last valid bank number for layer2 beginning). In "MMU" 8k pages terms that's six consecutive pages in range 0-31 (26-31 is the last valid possibility).

You don't need to have these pages in Z80 address space when Layer2 is being displayed, you need to map them only while writing into them (or reading from them). For that either use MMUs as suggested above, or you can switch ON "write over ROM" feature on Layer2 control port, then *any* write into 0000-3FFF range is redirected to selected 16ki bank by the Layer2 port... even if you have RAM paged in MMU0/MMU1 (hint: you can even map source graphics RAM into MMU0-1 and use this "write over ROM" to draw from the same address to the same address, but it will write into the Layer2 bank selected).

But don't forget to switch OFF the write-over-ROM before calling ESX subroutines, as those need writes into ROM area (they map the reserved divMMC pages of RAM there for internal buffers and write into them).

(and you can use NextReg 0x13 ("shadow" Layer2) for the write-over-ROM feature .. that's actually the only usage of NextReg 0x13, it's not "shadow" as if ZX128 shadow RAM, the display is using always 0x12, so to do HW double-buffer of Layer2, you can use 0x13 in ROM range to draw into new memory area, and upon finish setup 0x12 to display it, then flip 0x13 to alternate buffer and start again...)

----

edit2: and "no RAM for ESX" is true for MMU way of thinking (8ki pages 0-95 on default Next with 1MiB memory chips, 0-223 on extended Next with 2MiB chips). ESX will internally page-in divMMC-reserved RAM from the 256kiB memory pool of the first chip, which is not accessible through MMU registers, so you are free to use any of those 0-95/0-223 pages for yourself (as long as you want only ESX services and not NextZXOS, I believe that one requires more state preserved, even some RAM) = at least that's what I know about it. ... If you will get access to one of those 256kiB-reserved banks through other means (DivMMC memory paging mechanism and/or ZX128 +3 extended paging), then you can probably clash also with ESX, but I don't know about these details.

I'm keeping it simple to myself, i.e. just sticking to MMU next regs and not touching the reserved memory myself.

gskronn
Posts: 12
Joined: Wed Sep 13, 2017 1:14 pm

Re: How low is too low?

Post by gskronn » Fri May 24, 2019 8:45 am

Wow! That was super helpful! Thank you very much for that very detailed writeup. Program gets loaded as snapshot. All I need is esx for filesystem access. Right now I am leaving rom as is, leaving IM as is. Code gets .org'ed to $6200, data and stack to $9000 and the three top pages ($a000, $c000, $e000) used for paging assets and additional banked code. Again, many thanks!

Alcoholics Anonymous
Posts: 495
Joined: Mon May 29, 2017 7:00 pm

Re: How low is too low?

Post by Alcoholics Anonymous » Mon May 27, 2019 5:08 pm

A snapshot wipes out the basic system so you've already crossed that bridge :) You just have to be careful with the interrupts as the basic rom interrupt will poke into the system vars area using the iy register as index pointer. So replacing the rom isr or running with interrupts disabled solves that problem.

gskronn
Posts: 12
Joined: Wed Sep 13, 2017 1:14 pm

Re: How low is too low?

Post by gskronn » Mon Jun 10, 2019 10:48 am

Alcoholics Anonymous wrote:
Mon May 27, 2019 5:08 pm
So replacing the rom isr or running with interrupts disabled solves that problem.
Ok, good. Thank you. I've now completely converted to a paged model anyway. Basically I'm just leaving a little "core", providing the services and data commonly needed and the main startup code in the snapshot. Everything else is loaded from .bin files and paged in as needed. This was a painful process at first (mostly because finding the necessary info proved to be not exactly easy ;) ) but in the end it really liberating. Love it. No more worries about code size, its pure bliss ;)

Cheers
Gerhard

Post Reply