Page 1 of 1

Segment after the software stack, without setting a size

Posted: Thu Aug 03, 2017 5:56 am
by DRW
Is there a way to position a RAM segment after the C software stack without having to declare an absolute size for it?

If we take this cfg file (ROM and zeropage have been removed since they are not relevant here):

Code: Select all

SYMBOLS
{
	__STACKSIZE__  = $0100;
	__RAMSTART__   = $0200;
	__RAMSIZE__    = $0800 - __RAMSTART__ - __STACKSIZE__;
	__STACKSTART__ = __RAMSTART__ + __RAMSIZE__;
}

MEMORY
{
	RAM:     type = rw, start = __RAMSTART__,   size = __RAMSIZE__,   file = "";
	STACK:   type = rw, start = __STACKSTART__, size = __STACKSIZE__, file = "", define = yes;
}

SEGMENTS
{
	SPRITES:  load = RAM, type = bss, align = $0100;
	BSS:      load = RAM, type = bss;
}
I'd like to declare a code part that comes after the stack, but I don't want to declare a size for it here in the config file. The size shall be dependent on the actual number of bytes that are used for variables in this segment/memory location. (Just like the size of two segments in the same memory location is dependent on their actual use.)

So, the three RAM sections shall be calculated like this:

Post-stack-segment:
Size: The actual number of variables declared for this segment, not more.
Start = RAM end minus post-stack-segment size.

Stack:
Size = Defined by me, for example 256 or 512
Start = RAM end minus post-stack-segment size minus stack size.

Regular RAM:
Size: RAM size minus stack size minus post-stack-segment size as a maximum.
Start: RAM start

Is it possible to declare this in a cfg file?

Re: Segment after the software stack, without setting a size

Posted: Thu Aug 03, 2017 7:32 am
by thefox
If you set define=yes for a specific SEGMENT or MEMORY declaration, the linker will define symbols which give you some information about their addresses and sizes. You can utilize those in further MEMORY/SEGMENT declarations.

See http://www.cc65.org/doc/ld65.html#toc5.2 and http://www.cc65.org/doc/ld65.html#toc5.5 for details.

See here for one example from a while ago: viewtopic.php?f=2&t=13434&p=158354#p158354

Re: Segment after the software stack, without setting a size

Posted: Thu Aug 03, 2017 7:52 am
by DRW
So, I can use the defined symbols within the CGF file? I thought they are only for Assembly files.

However, I'm still not sure how I can declare a segment "right-aligned" in memory. The segment that I'm using is supposed to be situated at the end of the RAM.

Pseudo code declaration:
MySegment.Start = RAM.Start + RAM.Size - MySegment.Size

Is this possible at all? The start location of a segment being calculated depending on its own size?