ZXtool - intimately examine P files

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Fruitcake
Posts: 351
Joined: Wed Sep 01, 2010 10:53 pm

Re: ZXtool - intimately examine P files

Post by Fruitcake »

Fruitcake wrote: Fri May 01, 2020 1:26 am But I've never used it or know what format it outputs non-ASCII characters in... Maybe yet another format...
Modified source "to make it compatible with ZXText2P" is here:
https://github.com/mobluse/zx81-mob/tre ... converters

and previously mentioned here:
https://www.sinclairzxworld.com/viewtopic.php?t=1353
bwinkel67
Posts: 147
Joined: Mon Mar 23, 2020 2:38 am

Re: ZXtool - intimately examine P files

Post by bwinkel67 »

Fruitcake wrote: Fri May 01, 2020 1:26 am I see there is a P2TXT utility here:
http://www.retroisle.com/sinclair/zx81/software.php

But I've never used it or know what format it outputs non-ASCII characters in... Maybe yet another format...
Good find! It's compiled as 16 bit as it seems not to run on 64 bit Windows, but it comes with the source so it'll be pretty easy to recompile. Saves me from writing it from scratch :-/

Source is pretty simple and straightforward:

Code: Select all

/* P2TXT by R. J. Marks 1993
 * (for XTender)
 * Public domain
 */


#include <stdio.h>

typedef unsigned char byte;
#define NAK "#\0"

byte linebuf[32768]; /* BBBBIIIIIIGGGGGG buffer for those lovely m/c REMs */

/**************** the zx81 char set as transposed for PC ******************/

/* nak = 'not a kharacter', of course! */
/* spacing may not end up being *exactly* the same, but is very close */

char charset[256][10]=
{
/* 000-009 */ " \0",NAK,NAK,NAK,NAK,NAK,NAK,NAK,NAK,NAK,
/* 010-019 */ NAK,"\"\0","Ý\0","$\0",":\0","?\0","(\0",")\0",">\0","<\0",
/* 020-029 */ "=\0","+\0","-\0","*\0","/\0",";\0",",\0",".\0","0\0","1\0",
/* 030-039 */ "2\0","3\0","4\0","5\0","6\0","7\0","8\0","9\0","A\0","B\0",
/* 040-049 */ "C\0","D\0","E\0","F\0","G\0","H\0","I\0","J\0","K\0","L\0",
/* 050-059 */ "M\0","N\0","O\0","P\0","Q\0","R\0","S\0","T\0","U\0","V\0",
/* 060-069 */ "W\0","X\0","Y\0","Z\0","RND\0","INKEY$ \0","PI\0",NAK,NAK,NAK,
/* 070-079 */ NAK,NAK,NAK,NAK,NAK, NAK,NAK,NAK,NAK,NAK,
/* 080-089 */ NAK,NAK,NAK,NAK,NAK, NAK,NAK,NAK,NAK,NAK,
/* 090-099 */ NAK,NAK,NAK,NAK,NAK, NAK,NAK,NAK,NAK,NAK,
/* 100-109 */ NAK,NAK,NAK,NAK,NAK, NAK,NAK,NAK,NAK,NAK,
/* 110-119 */ NAK,NAK,NAK,NAK,NAK, NAK,NAK,NAK,NAK,NAK,
/* 120-129 */ NAK,NAK,NAK,NAK,NAK, NAK,NAK,NAK,NAK,NAK,
/* 130-139 */ NAK,NAK,NAK,NAK,NAK, NAK,NAK,NAK,NAK,"\"\0",
/* 140-149 */ "Ý\0","$\0",":\0","?\0","(\0",")\0",">\0","<\0","=\0","+\0",
/* 150-159 */ "-\0","*\0","/\0",";\0",",\0",".\0","0\0","1\0","2\0","3\0",
/* 160-169 */ "4\0","5\0","6\0","7\0","8\0","9\0","a\0","b\0","c\0","d\0",
/* 170-179 */ "e\0","f\0","g\0","h\0","i\0","j\0","k\0","l\0","m\0","n\0",
/* 180-189 */ "o\0","p\0","q\0","r\0","s\0","t\0","u\0","v\0","w\0","x\0",
/* 190-199 */ "y\0","z\0","\"\"\0","AT \0","TAB \0",NAK,"CODE \0","VAL \0",
			"LEN \0","SIN \0",
/* 200-209 */ "COS \0","TAN \0","ASN \0","ACS \0","ATN \0","LN \0","EXP \0",
			"INT \0","SQR \0","SGN \0",
/* 210-219 */ "ABS \0","PEEK \0","USR \0","STR$ \0","CHR$ \0","NOT \0",
			"**\0"," OR \0"," AND \0","<=\0",
/* 220-229 */ ">=\0","<>\0"," THEN\0"," TO \0"," STEP \0"," LPRINT \0",
      " LLIST \0"," STOP\0"," SLOW\0"," FAST\0",
/* 230-239 */ " NEW\0"," SCROLL\0"," CONT \0"," DIM \0"," REM \0",
			" FOR \0"," GOTO \0"," GOSUB \0"," INPUT \0",
			" LOAD \0",
/* 240-249 */ " LIST \0"," LET \0"," PAUSE \0"," NEXT \0"," POKE \0",
			" PRINT \0"," PLOT \0"," RUN \0"," SAVE \0",
			" RAND \0",
/* 250-255 */ " IF \0"," CLS\0"," UNPLOT \0"," CLEAR\0"," RETURN\0",
      " COPY\0"
};

/************************* program starts here ****************************/


/* main program */
main(argc,argv)
int argc;
char **argv;
{
FILE *in;

if(argc!=2)
	{
	printf("P2TXT (for XTender) by R. J. Marks (1993) for improbabledesigns\n\n");
	printf("Public domain - must be distributed in an unmodified form.\n\n");
	printf("Lists ZX81 .P files to stdout. Uses lowercase for inverse video.\n");
	printf("Graphics and unused characters give a hash (#) character.\n\n");

	printf("Usage:  p2txt infile.p >outfile.txt   to save to outfile.txt,\n");
	printf("  or    p2txt infile.p >prn           to send to printer.\n");
	}
else
	{
	if((in=fopen(argv[1],"rb"))==NULL)
		printf("Error: couldn't open file '%s'\n",argv[1]);
	else
		{
		thrashfile(in);      /* process it */
		fclose(in);
		}
	}
}

/* process (opened) .P file to stdout */
thrashfile(in)
FILE *in;
{
int inbyte,b1,b2;
int f,linelen,linenum,d_file,total;

for(f=1;f<=3;f++) inbyte=fgetc(in);   /* ignore sys vars */
b1=fgetc(in); b2=fgetc(in);           /* except d_file */
d_file=b1+256*b2;
for(f=1;f<=111;f++) inbyte=fgetc(in); /* ignore more sys vars */

total=d_file-16509; /* if d_file is after, prog is 16509 to d_file-1 right? */

/* run through 'total' bytes, interpreting them as ZX81 program lines */
getline(in,&linenum,&linelen,&total);
while(total>=0)
	{
	printf("%4d",linenum);
	xlatline(linelen);
	getline(in,&linenum,&linelen,&total);
	}
}

/* get a single ZX81 program line & line number & length */
getline(in,linenum,linelen,t)
FILE *in;
int *linenum,*linelen,*t;
{
int b1,b2,f;

b1=fgetc(in);
b2=fgetc(in);
*linenum=b1*256+b2;
(*t)-=2;

b1=fgetc(in);
b2=fgetc(in);
*linelen=b1+256*b2;
(*t)-=2;

for(f=0;f<*linelen;f++)
	{
	linebuf[f]=fgetc(in);
	(*t)--;
	}
}

/* translate line into keywords using the charset array */
xlatline(linelen)
int linelen;
{
int f;

for(f=0;f<linelen-1;f++)
	{
	if((linebuf[0]!=234)&&(linebuf[f]==126))
		f+=5;  /* avoid inline FP numbers - but ok for REMs */
	else
		printf("%s",charset[linebuf[f]]);
	}
printf("\n");
}
bwinkel67
Posts: 147
Joined: Mon Mar 23, 2020 2:38 am

Re: ZXtool - intimately examine P files

Post by bwinkel67 »

Fruitcake wrote: Fri May 01, 2020 1:33 am Modified source "to make it compatible with ZXText2P" is here:
https://github.com/mobluse/zx81-mob/tre ... converters
So the original one had this:

"\"\"\0"

The revised one changed it to this:

"\\\""

Not sure why the former kept adding '\0' into the end of a character string in C since it gets added by default.

Wish I could just find a 32/64 bit compatible executable so I don't have to get gcc and compile it.
bwinkel67
Posts: 147
Joined: Mon Mar 23, 2020 2:38 am

Re: ZXtool - intimately examine P files

Post by bwinkel67 »

I compiled it to run in 32 bit (on a 64 bit machine using the -m32 flag in gcc so hopefully it works on non-64 bit machines)
p2text32.zip
(21.79 KiB) Downloaded 310 times

So if the following code (saved in file called test.bas) is converted first to .p via zxtext2p.exe:

Code: Select all

10 PRINT "SAY \"HI\" TO YOU"
...which works in EightyOne and ZXSimulator...it can then be converted back to text with the above and it will retrain the \" sequence.

Note if you do the same sequence and use zxtool.exe, it changes the "\ to "" (i.e. zxtool.exe in essence corrupts the file) so p2text is a bit more compatible.

Also note that if you start with this:

Code: Select all

10 PRINT "SAY ""HI"" TO YOU"
...zxtext2p.exe will not change it and it won't work with EightyOne or ZXSimulator. If you then convert the .p file back to text with p2text.exe it will keep the "" as well so at least zxtext2p.exe and p2text.exe work the same way and are much better bookends to each other than zxtool.exe. My CDO makes me want to change the name to p2zxtext.exe (or probably better zxp2text.exe) but, well, I didn't write it :-/

I might add flags to both to allow the forcing of "" vs \" if I get some time. Anyone wanna help me think what command line arguments ought to be. As default I will leave it as \" but one should be able to say "" if they choose. I don't think -"" is a good flag. The zxtext2p.exe already uses the following: -h, -i, -l, -o, and -s.
Fruitcake
Posts: 351
Joined: Wed Sep 01, 2010 10:53 pm

Re: ZXtool - intimately examine P files

Post by Fruitcake »

bwinkel67 wrote: Fri May 01, 2020 12:41 am Oh, no I was asking for zxtool.exe source. Have been looking for it without success.
I spoke to SirMorris and he's updated zxtool for this issue (although I note not yet updated the version for download on the original post and so I've suggested he does that).

Should you still want the source then it (and the built executable) is available here.
bwinkel67
Posts: 147
Joined: Mon Mar 23, 2020 2:38 am

Re: ZXtool - intimately examine P files

Post by bwinkel67 »

Fruitcake wrote: Fri May 01, 2020 12:42 pm Should you still want the source then it (and the built executable) is available here.
Thanks again! I tried compiling the source in g++ but it failed, complaining about missing string functions like strncmp. I added the string.h include and it compiled and runs but spits out junk when I pass it in a .p file. I just tried it on a different computer and now it won't run because it is missing a DLL file so I suppose this compile with g++ requires some part of the g++ library: libgcc_s_dw2-1.dll included (the 2nd machine doesn't have g++ installed). I'll poke around with it a bit more but p2text (the one I compiled in p2text32.zip) was a bit easier to compile with gcc and runs on the machine without gcc installed.
Post Reply