Having some trouble with fopen on cp/m target.

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

Having some trouble with fopen on cp/m target.

Post by jacotton »

I am working on porting my macro-32 code to run on cp/m. And I hit a wall....
here is the repo https://github.com/jayacotton/z80-macro.git

It acts like there is a limit to the number of open files. I think the open file count
is 4 and the include makes 5.

Now, fopen is in main.c and macro.c. The calls in main work just fine. The
call in macro does not work.

The file I want to open is present on the drive and its readable etc. I just don't get
what its doing....

Use the Makefile and put the MACRO.COM in your test system. (SIMH) maybe.
Then load on these files listed below (Uppercase names).

D>macro hello
process this file HELLO.MAC
Can't open the file "COMMON.MAC"

Here is the offending code snip, yes in include stdio.h above.

New = fopen (mylbuff, "r");
if (New == 0)
{
printf ("Can't open the file \"%s\"\n", mylbuff);
fprintf (listfile, "Bad include directive\n%s\n", buff);
CloseOut ();
}

I did try to hard code the file name, no joy there.

Here is code to test the program with.

D>cat hello.mac

#INCLUDE "COMMON.MAC"
#INCLUDE "WTO.MAC"
#INCLUDE "PRE.MAC"
#INCLUDE "POST.MAC"
#INCLUDE "PRESTACK.MAC"

COMMON
ORG 100H
PRE
WTO 'HELLO WORLD'
POST
PRESTACK
END

D>cat common.mac
;
; COMMON DEFINES FOR CPM
;
MACRO COMMON
COMMON
BDOS EQU 5H ; DOS ENTRY POINT
BIOS EQU 5H ; DOS ENTRY POINT
RESET EQU 0 ; JUMP HERE TO RESET
CONSI EQU 1 ; READ CONSOLE
CONSO EQU 2 ; TYPE FUNCTION
GETCOS EQU 11 ; GET CONSOLE STATUS
GETVER EQU 12 ; GET CP/M VERSION
SELDISK EQU 14 ; SELECT DISK
OPENF EQU 15 ; FILE OPEN
CLOSEF EQU 16 ; CLOSE FILE
SRCHF EQU 17 ; GET FIRST DIRECTORY ENTRY
SRCHN EQU 18 ; SEARCH NEXT DIRECTORY ENTRY
DELF EQU 19 ; DELETE FILE
READF EQU 20 ; READ SEQUENCIAL
WRITEF EQU 21 ; WRITE SEQUENCIAL
CREATF EQU 22 ; CREAT FILE
RENF EQU 23 ; RENAME FILE
GETDRIV EQU 24 ; GET A LIST OF LOG-IN DRIVES....
GETCUR EQU 25 ; GET THE CURRENT DRIVER NUMBER
SETDMA EQU 26 ; SET DISK I/O BUFFER ADDRESS
READRAN EQU 33 ; READ RANDOM RECORD
WRITRAN EQU 34 ; WRITE RANDOM RECORD
CFS EQU 35 ; COMPUTE FILE SIZE
CFGTBL EQU 69 ; RETURN THE CONFIG TABLE
;
FCB EQU 5CH ; LOCATION OF FILE CONTROL BLOCK
BUFF EQU 80H ; LOCATION OF DISK INPUT BUFFER
DEFAULT EQU BUFF
;
CR EQU 0DH
LF EQU 0AH
TYPEF EQU CONSO
PRINTF EQU 9
;
; FILE CONTROL BLOCK STUFF
;
FCBDN EQU FCB+0 ; DISK NAME
FCBFN EQU FCB+1 ; FILE NAME
FCBFT EQU FCB+9 ; FILE TYPE
FCBRL EQU FCB+12 ; CURRENT REEL NUMBER
FCBRC EQU FCB+15 ; CURRENT RECORD NUMBER MOD 128
FCBCR EQU FCB+32 ; NEXT RECORD NUMBER
FCBLN EQU FCB+33 ; FCB LENGTH
MEND

D>cat wto.mac
;
; WRITE TO OPERATOR
;
MACRO WTO
%LAB WTO %LINE
GBLB %WTO
AIF ('%LAB' EQ '')&NOLAB
%LAB EQU $
&NOLAB ANOP
PUSH H
PUSH D
PUSH B
JMP A$SYSIN
B$SYSIN DB %LINE
DB CR,LF
DB '$'
A$SYSIN EQU $
LXI D,B$SYSIN
&GEN ANOP
CALL WTO
POP B
POP D
POP H
AIF (%WTO)&DONE
%WTO SETB 1
JMP X$SYSIN
WTO EQU $
MVI C,PRINTF
CALL BDOS
RET
X$SYSIN EQU $
&DONE ANOP
MEND

D>cat post.mac
;
; COMMON POST PROCESS
;
MACRO POST
%LAB POST
AIF ('%LAB' EQ '')&NOLAB
%LAB EQU $
&NOLAB ANOP
LHLD OLDSP
SPHL
RET
MEND

D>cat prestack.mac
MACRO PRESTACK
PRESTACK
OLDSP DW 0
DS 128
NEWSP EQU $
MEND

D>cat pre.mac
;
; COMMON PREAMBLE FOR CPM
;
MACRO PRE
PRE
LXI H,0
DAD SP
SHLD OLDSP
LXI SP,NEWSP
MEND
User avatar
dom
Well known member
Posts: 1428
Joined: Sun Jul 15, 2007 10:01 pm

Re: Having some trouble with fopen on cp/m target.

Post by dom »

Yup, there's a default limit of 3 FCBs: https://github.com/z88dk/z88dk/wiki/Pla ... ile-access - that option configures the number of "fds"

If you use more than 10 FILE handles, then you'll need to tune that as well, CLIB_FOPEN_MAX is the pragma name for configuring that size.
jacotton
Member
Posts: 49
Joined: Fri Nov 24, 2017 1:35 am

Re: Having some trouble with fopen on cp/m target.

Post by jacotton »

Still no joy.

I tried several combinations of pragma's but was not able to get the desired result.
Do these apply to the old clib only ?

An item of interest, these pragma's seem to cause a bit of drama with the
argv stack. I worked around it, but .....

Here is the current Makefile. Am I saying it wrong ?


CFILES = $(wildcard *.c)
OFILES = $(CFILES:.c=.o)

CFLAGS = +cpm -Wall --list -DCPM -pragma-include:zpragma.inc -pragma-define:CLIB_FOPEN_MAX=10 -pragma-define:CLIB_OPEN_FILE=10
LFLAGS = +cpm -m -create-app -pragma-include:zpragma.inc -pragma-define:CLIB_FOPEN_MAX=10 -pragma-define:CLIB_OPEN_FILE=10

macro: $(OFILES)
zcc $(LFLAGS) -omacro $(OFILES)

%.o: %.c
zcc $(CFLAGS) -o $@ -c $^

clean:
rm *.o *.err *.lis *.def *.sym *.exe *.COM macro *.map

install:
sudo cp ./*.COM /var/www/html/.
User avatar
dom
Well known member
Posts: 1428
Joined: Sun Jul 15, 2007 10:01 pm

Re: Having some trouble with fopen on cp/m target.

Post by dom »

Sorry there was a massive "typo" on that page, -pragma-define:CLIB_OPEN_FILE=10 should be -pragma-define:CLIB_OPEN_MAX=10

Tell me about the argv issue - there's been some odd behaviour with that lately so I want to know what's going on for you.
jacotton
Member
Posts: 49
Joined: Fri Nov 24, 2017 1:35 am

Re: Having some trouble with fopen on cp/m target.

Post by jacotton »

WRT argv

had to had the code below to clean up the argv[1] entry. The string looked like this.

macro test
>> printf output TEST MAC.MAC

I have no idea where it found the MAC.MAC part.

memset (listfilename, 0, 80);
p = argv[1];
while(*p != 0){
if(*p == ' ') *p = '\0';
p++;
}
printf("%s\n",argv[1]);
jacotton
Member
Posts: 49
Joined: Fri Nov 24, 2017 1:35 am

Re: Having some trouble with fopen on cp/m target.

Post by jacotton »

BTW the fix to the typo got me around the file open bug.

Now to fix the code that I wrote.....
jacotton
Member
Posts: 49
Joined: Fri Nov 24, 2017 1:35 am

Re: Having some trouble with fopen on cp/m target.

Post by jacotton »

Here is a run with my hack removed

C>macro test
process this file TEST MAC.MAC
Can't open TEST MAC.MAC

I should add, it was not doing this until the expand FCB pragma. Also this was not the
original bug.
Post Reply