Can I have multiple sections for C code?

ZX80, ZX 81, ZX Spectrum, TS2068 and other clones
Post Reply
derekfountain
Member
Posts: 124
Joined: Mon Mar 26, 2018 1:49 pm

Can I have multiple sections for C code?

Post by derekfountain »

I'm using SDCC.

Is it possible to have multiple sections for my C code? For example, I have my main game code which I want at 0x8000, but also have some screens data and their decoder in C and that can happily go into the contended memory area. So can I say "I want this C code ORGed at 0x8000, but this C code at 0x6200"?
User avatar
dom
Well known member
Posts: 2269
Joined: Sun Jul 15, 2007 10:01 pm

Re: Can I have multiple sections for C code?

Post by dom »

In case you’ve not found it #pragma codeseg name where name is your choice will push compiler code in that file into a new section.

You’ll need to fiddle with the section definitions of course.
derekfountain
Member
Posts: 124
Joined: Mon Mar 26, 2018 1:49 pm

Re: Can I have multiple sections for C code?

Post by derekfountain »

I had not found the codeseg pragma, I'd settled on the hacky approach you suggested elsewhere:

Code: Select all

static void set_section() __naked {
__asm
  section contended
__endasm;
}
That worked, mostly (see below), as does the codeseg approach, so double thanks.

(Once you know what you're looking for it's easier to find the answer. There's an old post from Alvin here which describes things in more details, which might be useful to anyone who arrives here via Google.)

One outstanding issue though: my "contended" section is currently ORGed at address 30000. My main() code starts at 32768 as normal. But when I use z88dk-appmake to create the TAP file the code is started with RANDOMISE USR 30000, because it seems to assume that the entry point of the code is the address where the code is ORGed. I want to LOAD to 30000, but RANDOMISE USR 32768. I can write my own BASIC loader, of course, or hack the source to z88dk-appmake (which is what I did), but am I right in thinking there's no way round this limitation with the current z88dk-appmake utility?
fraespre
Member
Posts: 59
Joined: Mon Aug 19, 2019 8:08 pm

Re: Can I have multiple sections for C code?

Post by fraespre »

I was looking for something similar some time ago, I tried putting the section between "__asm" definition, but it didn't work for my case.

Another related thread with this topic, that I found via google too:
https://foro.speccy.org/viewtopic.php?t=5032
User avatar
dom
Well known member
Posts: 2269
Joined: Sun Jul 15, 2007 10:01 pm

Re: Can I have multiple sections for C code?

Post by dom »

derekfountain wrote: Sun Apr 24, 2022 4:06 pmOne outstanding issue though: my "contended" section is currently ORGed at address 30000. My main() code starts at 32768 as normal. But when I use z88dk-appmake to create the TAP file the code is started with RANDOMISE USR 30000, because it seems to assume that the entry point of the code is the address where the code is ORGed. I want to LOAD to 30000, but RANDOMISE USR 32768. I can write my own BASIC loader, of course, or hack the source to z88dk-appmake (which is what I did), but am I right in thinking there's no way round this limitation with the current z88dk-appmake utility?
The zx appmake just has a lot of features, I thought there was a way to do that, but it looks like load address == start address, so a new option/pragma would need to be added.
abacaxi
New member
Posts: 8
Joined: Tue Nov 26, 2024 5:39 pm

Re: Can I have multiple sections for C code?

Post by abacaxi »

Perhaps some slight thread necrophilia here but what's really the best solution to this? using appmake --noloader for each section bin then bas2tap to create a loader, then just cat:ing the taps together?
abacaxi
New member
Posts: 8
Joined: Tue Nov 26, 2024 5:39 pm

Re: Can I have multiple sections for C code?

Post by abacaxi »

Perhaps I misunderstood the issue, I just thought the easiest solution would be to make a file for each section and use a basic loader to put them at the desired addresses.
Timmy
Well known member
Posts: 423
Joined: Sat Mar 10, 2012 4:18 pm

Re: Can I have multiple sections for C code?

Post by Timmy »

I usually just can't be bothered with sections, as I still can't understand them.

If I have data/mc at fixed location, and it's for the Spectrum, I just add the tape with these data, and then call the tape loading routine.
abacaxi
New member
Posts: 8
Joined: Tue Nov 26, 2024 5:39 pm

Re: Can I have multiple sections for C code?

Post by abacaxi »

yeah but if you want to fill the 48k you need to decide what you want to put in contended memory or not. you can always use manual counting and padding but it takes trial and error and many adjustments. for the record i noticed that appmaker has separate options for org and usr these days so that problem in particular is solved.
stefano
Well known member
Posts: 2298
Joined: Mon Jul 16, 2007 7:39 pm

Re: Can I have multiple sections for C code?

Post by stefano »

One of the turbo loader modes (the one doing RLE compression) in z88dk-appmake allows 2 separate binary blocks, usually good to add a title screen, but it can be your first block at 30000 as well.

EDIT
Nope, you're right it's doable but even more difficult
User avatar
dom
Well known member
Posts: 2269
Joined: Sun Jul 15, 2007 10:01 pm

Re: Can I have multiple sections for C code?

Post by dom »

I think it's easiest if you break it down into multiple questions.

1. Can you have multiple sections for C code.

Yes! Just use the pragmas to place things in the section you desire.

2. If you do this, does appmake handle it in one step.

No. Appmake doesn't handle it. As noted in the previous thread on a similar issue: viewtopic.php?p=23516#p23516 it's not too hard to add. It's just that no one has yet.
Timmy
Well known member
Posts: 423
Joined: Sat Mar 10, 2012 4:18 pm

Re: Can I have multiple sections for C code?

Post by Timmy »

abacaxi wrote: Thu Feb 06, 2025 11:05 pm yeah but if you want to fill the 48k you need to decide what you want to put in contended memory or not.
yes, maybe. i've released enough games to say that it probably doesn't matter that much where you put your code/data. Perhaps it's because programming in C has its own overhead, or perhaps because i care more about stable framerates instead of super fast every frame, or perhaps my main problem is that I'm already glad I have any free bytes left, let alone where it's located.
(Your problems might vary.)
you can always use manual counting and padding but it takes trial and error and many adjustments.
It's only 48k, so you can probably just count them.
This is also really a question of experience by trial and error.
Also, this will always be manually counting and padding, unless you don't need that many free bytes for a game.
Also note, that if you label your code correctly, you can just refer your code/data by labels, and then it doesn't matter much where the code/data is actually located.
Separate tapes with different parts in them also makes solving this problem easier.
derekfountain
Member
Posts: 124
Joined: Mon Mar 26, 2018 1:49 pm

Re: Can I have multiple sections for C code?

Post by derekfountain »

abacaxi wrote: Thu Feb 06, 2025 11:05 pm yeah but if you want to fill the 48k you need to decide what you want to put in contended memory or not. you can always use manual counting and padding but it takes trial and error and many adjustments. for the record i noticed that appmaker has separate options for org and usr these days so that problem in particular is solved.
This is a thread I created nearly 3 years ago. I can't remember what project I was working on at the time, but I do know I separated out this particular problem (of compiling C code into different sections) and made a separate test project from it. I wanted a simple example to experiment with. About a year ago I put that test project in my Github here:

https://github.com/derekfountain/z88dk- ... -lower-ram

That project solves the problem in what I believe is still the correct manner. No hacks or inline ASM tricks, it defines a new section (in ASM) to go into contended memory, then guides the compiler to target that new section as appropriate.

You can, if you want, still compile each section into a separate BIN file, then use a custom BASIC loader to LOAD each one into the correct location. The better alternative, as used in the example project, is to "glue" those binaries together into a single, large piece of code, then use appmake to make a TAP out of that. I submitted a patch to z88dk which gives appmake the ability to set a specific start address, which was needed for this approach to work.

There is another alternative to this approach, which is to write and use a memory map file which describes the exact layout you want. I didn't try this, it looked rather complicated. But I did see this Spectrum Next blog post from Allen a couple of months ago:

https://specnext.dev/blog/2024/12/10/ev ... id-to-ask/

That demonstrates the mmap.inc approach. If you can wade through the fabulous level of Next-specific detail he provides I think you'll find the alternative answer is in there.
abacaxi
New member
Posts: 8
Joined: Tue Nov 26, 2024 5:39 pm

Re: Can I have multiple sections for C code?

Post by abacaxi »

Great stuff. Thank you.
Post Reply