CP/M memory organization

Post Reply
sgjava
Member
Posts: 22
Joined: Thu Feb 22, 2024 9:25 pm

CP/M memory organization

Post by sgjava »

I'm porting some C code over from HTC that uses TPA memory for the VIC chip. In HTC it was pretty easy since the heap started at the end of the program and grew towards the bottom of the TPA. I could just malloc what I needed to protect that block of memory.

I'm trying to figure out the z88dk memory model (classic). It appears from my tests that the heap starts near the bottom of the TPA and grows towards the top of the TPA (backwards from HTC). I'd like to know where the stack is and any other sections. Or is it better to just go newlib sections?
sgjava
Member
Posts: 22
Joined: Thu Feb 22, 2024 9:25 pm

Re: CP/M memory organization

Post by sgjava »

Hoping RAM model is what CP/M uses? https://github.com/z88dk/z88dk/wiki/New ... e-compiler
User avatar
dom
Well known member
Posts: 2142
Joined: Sun Jul 15, 2007 10:01 pm

Re: CP/M memory organization

Post by dom »

As we found out in an earlier topic, the allocation within the heap starts at the top of the memory allocated for it. Stack by default starts from just below bdos.

The following pragmas should allow you to size and position the heap to avoid the fixed address you're using:
Screenshot 2024-04-07 at 00.02.16.png
(from https://github.com/z88dk/z88dk/wiki/Classic--Pragmas)

That page also specifies the order of the sections, +cpm uses the RAM model.
You do not have the required permissions to view the files attached to this post.
sgjava
Member
Posts: 22
Joined: Thu Feb 22, 2024 9:25 pm

Re: CP/M memory organization

Post by sgjava »

Let me be more specific, Let's say code+data is 0x0100 - 0x3ff and If set my VIC bank to 0x4000 - 0x7fff, then I could start heap at 0x8000 with a size of 16K, so it would start at 0xbfff and work back to 0x08000? I'm already setting the stack size to 1024.

So on C128 CP/M:

0100-E9FF 58 k transient program area (TPA) DEC and MAY versions (0100-EDFF 59 k TPA on AUG version)
E000-FFFF common with BANK 0 (top of TPA, also BDOS, BIOS, etc.)

So stack would start somewhere below 0xe000?
User avatar
dom
Well known member
Posts: 2142
Joined: Sun Jul 15, 2007 10:01 pm

Re: CP/M memory organization

Post by dom »

Okay, so specific, do this to give you a 16k heap at 0x8000:

Code: Select all

// zcc +cpm heap.c
long heap = 0;

int main()
{
   sbrk(0x8000, 0x4000);
}
The stack will be at the top of TPA (actually the address of bdos), so with a 58k TPA it'll end up in the $E000-$FFFF range.

Assuming you limit your program to < ~16k you'll get the memory map you want.
sgjava
Member
Posts: 22
Joined: Thu Feb 22, 2024 9:25 pm

Re: CP/M memory organization

Post by sgjava »

So I'm wondering if I can simplify with:

#pragma output CRT_STACK_SIZE = 1024
// Protect VIC memory < 0x8000
#pragma CRT_HEAP_ADDRESS = 0x8000

So I can have max heap. i.e. I don't want to worry about size, just use what's free.
User avatar
dom
Well known member
Posts: 2142
Joined: Sun Jul 15, 2007 10:01 pm

Re: CP/M memory organization

Post by dom »

Yes, that would work (note the output parameter on the CRT_HEAP_ADDRESS) line:

Code: Select all

#pragma output CRT_STACK_SIZE = 1024
// Protect VIC memory < 0x8000
#pragma output CRT_HEAP_ADDRESS = 0x8000
Post Reply