CP/M 3 stack issues.

Post Reply
jacotton
Member
Posts: 26
Joined: Fri Nov 24, 2017 1:35 am

CP/M 3 stack issues.

Post by jacotton »

Have not yet got to the bottom of this issue, but I thought it would be good to ask here first.

CP/M 3.
z88dk
zcc - Frontend for the z88dk Cross-C Compiler - v16128-c78bc0271-20200306

My inettools-z80 code pile, pick one.

Here is the issue we are seeing.

When a submit script is 'submited' and that script has any of my inettools in it,
then the submit program go's bonkers and we start getting crash codes.

The supposition is that the inettools (being large) are pushing the stack up into
the submit program space.

Is there a way to adjust the program stack at build time to account for this
kind of issue? It seems like the kind of thing that would be available in z88dk
but I have not found a way to do it yet.
jacotton
Member
Posts: 26
Joined: Fri Nov 24, 2017 1:35 am

Post by jacotton »

Does this kind of thing apply to the general case ?
#pragma output STACKPTR=49152
jacotton
Member
Posts: 26
Joined: Fri Nov 24, 2017 1:35 am

Post by jacotton »

Looking like I found the solution.

I will hold this open to see if there are any better ways to handle this.
stefano
Well known member
Posts: 1534
Joined: Mon Jul 16, 2007 7:39 pm

Post by stefano »

It it the right way to address the problem.
An option to relocate the stack could be possible only if we find an universal and reliable way to determine the TPA size.
jacotton
Member
Posts: 26
Joined: Fri Nov 24, 2017 1:35 am

Post by jacotton »

I'm not seeing what I expect to get.

This program "ifconfig" Has a pragma to set the stack 0x9000.
But this never seems to produce the desired results.
Perhaps I am not using it correctly.

Here is a hunk of the make file.

ifconfig: addrprint.o w5500.o dhcp.o spi.o socket.o ethernet.o dns.o
zcc +cpm -m -create-app -oifconfig -Wunused addrprint.o w5500.o dhcp.o spi.o socket.o ethernet.o dns.o


Here is a hunk of the code.
#pragma output REGISTER_SP = 0x9000
void main()
{
printf("Stack 0x%04x\n",get_sp());

The stack is reporting at 0xf1b2. ?? way up there.

So, the problem is that a: its not 9000 and b: its mapped to middle of CPM3
RESBDOS3 SPR F100 0600
This seems to allow "ifconfig" to poke holes in the FCB of RESBDOS3, and it then
renders the system unstable.

Here is the stack reporting code I put in ifconfig.
unsigned int get_sp()
{
#asm
ld hl,0
add hl,sp
ret
#endasm
return 0;
}

printf("Stack 0x%04x\n",get_sp());
output.
A>ifconfig
Stack 0xf1b2
Shift Register SPI Wiznet v1.0
Stack 0xf1b2
inet 192.168.0.172 Stack 0xf1b2
netmask 255.255.255.0
Stack 0xf1b2
Stack 0xf1b2
dns server 192.168.0.1
ether 98.76.b6.11.00.c3
User avatar
dom
Well known member
Posts: 1187
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

I think I know what's going on, it's a bit annoying, but we've run into it before so there's a way around it.

The results of the pragmas are written the zcc_opt.def which is cleaned on every invocation of zcc. So if you're using a makefile then it gets wiped all the time. However if the compilation is all in one step then it works just fine.

The solution is to put all the pragmas into a separate file (say zpragma.inc) and include that file at the linking stage with the option: -pragma-include:zpragma.inc
jacotton
Member
Posts: 26
Joined: Fri Nov 24, 2017 1:35 am

Post by jacotton »

I think that you hit the root cause of the problem. But, in order to get it to work I had to compile the entire program in one bite.

This command worked.
zcc +cpm -create-app -oifconfig addrprint.c w5500.c dhcp.c spi.c socket.c ethernet.c dns.c

This one did not.
zcc +cpm -m -create-app -oifconfig -Wunused -pramga-include:zpragma.inc addrprint.o w5500.o dhcp.o spi.o socket.o ethernet.o dns.o

Here is the include file contents.
IF !DEFINED_REGISTER_SP
defc DEFINED_REGISTER_SP = 1
defc REGISTER_SP = 0x9000
IFNDEF REGISTER_SP
ENDIF
ENDIF

I have some doubt about this file, seems like it might be incorrect.
User avatar
dom
Well known member
Posts: 1187
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

The zpragma.inc should just contain pragmas:

#pragma output REGISTER_SP = 0x9000

In the same format as you were putting into the source files.
jacotton
Member
Posts: 26
Joined: Fri Nov 24, 2017 1:35 am

Post by jacotton »

Still not there.

Here is a log of the build, with a make file.

jay@jay-Ultra-27:~/projects/inettools-z80$ make ifconfig
zcc +cpm --list --c-code-in-asm -Wall -pragma-include:zpragma.inc -c addrprint.c
zcc +cpm --list --c-code-in-asm -Wall -pragma-include:zpragma.inc -c w5500.c
zcc +cpm --list --c-code-in-asm -Wall -pragma-include:zpragma.inc -c dhcp.c
zcc +cpm --list --c-code-in-asm -Wall -pragma-include:zpragma.inc -c spi.c
zcc +cpm --list --c-code-in-asm -Wall -pragma-include:zpragma.inc -c socket.c
zcc +cpm --list --c-code-in-asm -Wall -pragma-include:zpragma.inc -c ethernet.c
zcc +cpm --list --c-code-in-asm -Wall -pragma-include:zpragma.inc -c dns.c
zcc +cpm -m -create-app -oifconfig -Wunused -pramga-include:zpragma.inc addrprint.o w5500.o dhcp.o spi.o socket.o ethernet.o dns.o
cp IFCONFIG.COM ~/HostFileBdos/c/ifconfig.com

Here is the pragma file.

cat zpragma.inc
#pragma output REGISTER_SP = 0x9000

Here is the result of the build.

A>ifconfig
Stack 0xf1b2
Shift Register SPI Wiznet v1.0
Stack 0xf1b2
inet 192.168.0.172 Stack 0xf1b2
netmask 255.255.255.0
Stack 0xf1b2
Stack 0xf1b2
dns server 192.168.0.1
ether 98.76.b6.11.00.c3

Note that the stack address is still at f1b2.

Now this command will produce the correct output.

zcc +cpm -create-app -oifconfig addrprint.c w5500.c dhcp.c spi.c socket.c ethernet.c dns.c

Where the pragma is in the file addrprint.c

A>ifconfig
Stack 0x8fb4
Shift Register SPI Wiznet v1.0
Stack 0x8fb4
inet 192.168.0.172 Stack 0x8fb4
netmask 255.255.255.0
Stack 0x8fb4
Stack 0x8fb4
dns server 192.168.0.1
ether 98.76.b6.11.00.c3


Is there a tool/method to check the .COM file for the stack address ?
Looking at the .map file, nothing of interest there.

The zcc_opt.def file is null for the make file build case. Is that expected ?
jacotton
Member
Posts: 26
Joined: Fri Nov 24, 2017 1:35 am

Post by jacotton »

I modified the Makefile to print out the zcc_opt.def file after each segment of code is compiled. It looks correct i.e. each module has a
IF !DEFINED_REGISTER_SP
defc DEFINED_REGISTER_SP = 1
defc REGISTER_SP = 0x9000
IFNDEF REGISTER_SP
ENDIF
ENDIF
In it.
However the last step, the link phase the zcc_opt.def file is void.
jacotton
Member
Posts: 26
Joined: Fri Nov 24, 2017 1:35 am

Post by jacotton »

O.k I found the correct combination. This command will build the correct (desired) stack pointer value.
Note that what I did was to compile the .c file that holds the 'main' entry point and the rest are all object files.

zcc +cpm -m -create-app -oifconfig -Wunused -pragma-include:zpragma.inc addrprint.c w5500.o dhcp.o spi.o socket.o ethernet.o dns.o

I can make this work for my environment and it will clear up a few problems.
User avatar
dom
Well known member
Posts: 1187
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

The only reason why I can see that it didn't work would be the typo in -pragma-include=zpragma.inc you had -pramga.

I've just tried it out with:

Code: Select all

OBJS = main.o func.o


all: program.com

program.com: $(OBJS)
        zcc +cpm -v $(OBJS) -o $@ -pragma-include=zpragma.inc

%.o: %.c
        zcc +cpm -c $^ -o $@

clean:
        $(RM) $(OBJS) program.com

Code: Select all

extern void func();

int main() {
        func();
}

Code: Select all

#include <stdio.h>

void func() {
        printf("In func\n");
}

Code: Select all

#pragma define REGISTER_SP = 0x9000
Disassembling program.com gives:

Code: Select all

                    ld      hl,$0000                        ;[0136] 21 00 00
                    add     hl,sp                           ;[0139] 39
                    ld      ($01ed),hl                      ;[013a] 22 ed 01
                    ld      sp,$9000                        ;[013d] 31 00 90
                    ld      hl,$ffc0                        ;[0140] 21 c0 ff
                    add     hl,sp                           ;[0143] 39
So we can see the pragma is picked up correctly.
jacotton
Member
Posts: 26
Joined: Fri Nov 24, 2017 1:35 am

Post by jacotton »

The changes made, seem to be a fix for the problem I was working... tnx for all the help.
User avatar
dom
Well known member
Posts: 1187
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

In case someone in the future stumbles upon this thread due to having the same issues, I've written it up on the wiki: https://github.com/z88dk/z88dk/wiki/Com ... plications

I've also written a Makefile (https://github.com/z88dk/z88dk/blob/mas ... d/Makefile) that assumes a (hopefully uncontroversial) project structure and permits incremental builds for multiple z88dk targets.
Post Reply