home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Brotikasten
/
BROTCD01.iso
/
spectrum
/
jpp
/
spconv
/
spconv.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-12-13
|
24KB
|
1,346 lines
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>
#include <alloc.h>
void read_image(char * s);
void write_image(char * s);
void read_jpp(char * s);
void write_jpp(char * s);
void read_vgaspec_header(char * s);
void read_vgaspec(char * s);
void write_vgaspec(char * s);
void read_old_vgaspec(char * s);
void write_old_vgaspec(char * s);
void read_raw(char * s);
void read_z80(char * s);
void write_z80(char * s);
void read_prg(char * s);
void write_prg(char * s);
void jpp_to_vgaspec();
void vgaspec_to_jpp();
void raw_to_jpp();
void jpp_to_z80();
void z80_to_jpp();
void jpp_to_prg();
void prg_to_jpp();
void z80_uncompress();
void z80_uncompress();
int z80_compress();
union header_u {
char in[9];
struct {
char type;
unsigned int length;
unsigned int start;
char var;
char res1;
int line;
} header;
} h;
char expect[]={
0x03, /* type CODE */
0x00,0xc0, /* image size */
0x00,0x40, /* image start */
0xff, /* var */
0xff, /* res1 */
0xff,0xff /* line */
};
struct jpp_s {
unsigned char i;
unsigned char lax;
unsigned char hax;
unsigned char eax;
unsigned char dax;
unsigned char cax;
unsigned char bax;
unsigned char fax;
unsigned char aax;
unsigned char l;
unsigned char h;
unsigned char e;
unsigned char d;
unsigned char c;
unsigned char b;
unsigned char iyl;
unsigned char iyh;
unsigned char ixl;
unsigned char ixh;
unsigned char iff2;
unsigned char r;
unsigned char f;
unsigned char a;
unsigned char spl;
unsigned char sph;
unsigned char im;
unsigned char border;
} jpp;
struct vga_s {
/*00*/ unsigned char S;
/*01*/ unsigned char P;
/*02*/ unsigned int len;
/*04*/ unsigned int start;
/*06*/ unsigned char c;
/*07*/ unsigned char b;
/*08*/ unsigned char e;
/*09*/ unsigned char d;
/*0A*/ unsigned char l;
/*0B*/ unsigned char h;
/*0C*/ unsigned char f;
/*0D*/ unsigned char a;
/*0E*/ unsigned char ixl;
/*0F*/ unsigned char ixh;
/*10*/ unsigned char iyl;
/*11*/ unsigned char iyh;
/*12*/ unsigned char cax;
/*13*/ unsigned char bax;
/*14*/ unsigned char eax;
/*15*/ unsigned char dax;
/*16*/ unsigned char lax;
/*17*/ unsigned char hax;
/*18*/ unsigned char fax;
/*19*/ unsigned char aax;
/*1A*/ unsigned char r;
/*1B*/ unsigned char i;
/*1C*/ unsigned char spl;
/*1D*/ unsigned char sph;
/*1E*/ unsigned char pcl;
/*1F*/ unsigned char pch;
/*20*/ unsigned char res2;
/*21*/ unsigned char res3;
/*22*/ unsigned char border;
/*23*/ unsigned char res4;
/*24*/ unsigned char im;
/*25*/ unsigned char res5;
} vga;
struct z80_s {
/*00*/ unsigned char a;
/*01*/ unsigned char f;
/*02*/ unsigned char c;
/*03*/ unsigned char b;
/*04*/ unsigned char l;
/*05*/ unsigned char h;
/*06*/ unsigned char pcl;
/*07*/ unsigned char pch;
/*08*/ unsigned char spl;
/*09*/ unsigned char sph;
/*0A*/ unsigned char i;
/*0B*/ unsigned char r;
/*0C*/ unsigned char data;
/*0D*/ unsigned char e;
/*0E*/ unsigned char d;
/*0F*/ unsigned char cax;
/*10*/ unsigned char bax;
/*11*/ unsigned char eax;
/*12*/ unsigned char dax;
/*13*/ unsigned char lax;
/*14*/ unsigned char hax;
/*15*/ unsigned char aax;
/*16*/ unsigned char fax;
/*17*/ unsigned char iyl;
/*18*/ unsigned char iyh;
/*19*/ unsigned char ixl;
/*1A*/ unsigned char ixh;
/*1B*/ unsigned char iff1;
/*1C*/ unsigned char iff2;
/*1D*/ unsigned char im;
} z80;
struct prg_s {
/*00*/ char name[10];
/*0A*/ char nullbyte;
/*0B*/ unsigned char contains_0x61;
/*0C*/ unsigned char contains_0x35;
/*0D*/ unsigned char contains_0x03;
/*0E*/ unsigned char contains_0x00[0xdc-0x0e];
/*DC*/ unsigned char iyl;
/*DD*/ unsigned char iyh;
/*DE*/ unsigned char ixl;
/*DF*/ unsigned char ixh;
/*E0*/ unsigned char eax;
/*E1*/ unsigned char dax;
/*E2*/ unsigned char cax;
/*E3*/ unsigned char bax;
/*E4*/ unsigned char lax;
/*E5*/ unsigned char hax;
/*E6*/ unsigned char fax;
/*E7*/ unsigned char aax;
/*E8*/ unsigned char e;
/*E9*/ unsigned char d;
/*EA*/ unsigned char c;
/*EB*/ unsigned char b;
/*EC*/ unsigned char l;
/*ED*/ unsigned char h;
/*EE*/ unsigned char iff2;
/*EF*/ unsigned char i;
/*F0*/ unsigned char spl;
/*F1*/ unsigned char sph;
/*F2*/ unsigned char filler[0x0e];
} prg;
#define IMSIZE 49152
unsigned char image[IMSIZE];
unsigned int z80_size;
int intype;
int outtype;
#define RAW 1
#define JPP 2
#define SPECTRUM 3
#define Z80 4
#define PRG 5
#define UNKNOWN 6
unsigned int addr;
unsigned int sp;
unsigned int pc;
int fd;
main(argc,argv)
int argc;
char *argv[];
{
int i;
char *p;
struct stat status;
char * fromstring;
char * tostring;
char * outfile;
if(argc != 3)
{
fprintf(stderr,"SPCONV version 1.02 - %s\n\n",__DATE__);
fprintf(stderr,"Usage: spconv <source> <target>\n\n");
fprintf(stderr,"Source must be a valid .SNA, .SP, .Z80, .PRG or RAW file.\n");
fprintf(stderr,"Target must be a .SNA, .SP, .Z80 or .PRG file.\n\n");
fprintf(stderr,"If the second parameter contains only a suffix, the prefix\n");
fprintf(stderr,"of the input file will be used (i.e. 'spconv file.sna .z80')\n\n");
fprintf(stderr,"Output .SP files are in the new format, .Z80 files are compressed.\n\n");
fprintf(stderr,"If <source> and <target> are .SP files, convertion from old\n");
fprintf(stderr,"to new format or from new to old format will be performed.\n");
fprintf(stderr,"If <source> and <target> are of the same type an error message\n");
fprintf(stderr,"will be generated (unless they are both .SP files)\n");
fprintf(stderr,"\n\nPublic Domain, H. de Groot 1992\n\n");
return 1;
}
if((strchr(argv[1],'*')!=NULL) || (strrchr(argv[1],'?')!=NULL) ||
(strchr(argv[2],'*')!=NULL) || (strrchr(argv[2],'?')!=NULL))
{
fprintf(stderr,"This program can't handle wildcards, sorry!\n");
return 1;
}
if(stat(argv[1],&status)<0)
{
perror(argv[1]);
return 1;
}
/*
* recognize input type on filename:
*
* .SNA -> JPP file
* .SP -> SPECTRUM file (was VGASPEC)
* .Z80 -> Z80 file
* .PRG -> PRG file
* other -> if exact 48+header -> raw file
* otherwise unknown
*/
intype=UNKNOWN;
p=strrchr(argv[1],'.');
if(p==NULL) p=argv[1]; /* not found, set at begin of string */
if((strcmp(p,".SNA")==0) || (strcmp(p,".sna")==0))
{
fromstring="from .SNA";
intype=JPP;
}
if((strcmp(p,".Z80")==0) || (strcmp(p,".z80")==0))
{
fromstring="from .Z80";
intype=Z80;
}
if((strcmp(p,".SP")==0) || (strcmp(p,".sp")==0))
{
fromstring="from .SP";
intype=SPECTRUM;
}
if((strcmp(p,".PRG")==0) || (strcmp(p,".prg")==0))
{
fromstring="from .PRG";
intype=PRG;
}
if(intype==UNKNOWN)
if (status.st_size == (sizeof(h)+IMSIZE))
{
fromstring="from RAW";
intype=RAW;
}
/*
* recognize output type on filename:
*
* .SNA -> JPP file
* .SP -> SPECTRUM file (was VGASPEC)
* .Z80 -> Z80 file
* .PRG -> PRG file
* otherwise unknown
*/
outtype=UNKNOWN;
p=strrchr(argv[2],'.');
if(p==NULL) p=argv[2]; /* not found, set at begin of string */
if((strcmp(p,".SNA")==0) || (strcmp(p,".sna")==0))
{
tostring="to .SNA";
outtype=JPP;
}
if((strcmp(p,".Z80")==0) || (strcmp(p,".z80")==0))
{
tostring="to .Z80";
outtype=Z80;
}
if((strcmp(p,".SP")==0) || (strcmp(p,".sp")==0))
{
tostring="to .SP";
outtype=SPECTRUM;
}
if((strcmp(p,".PRG")==0) || (strcmp(p,".prg")==0))
{
tostring="to .PRG";
outtype=PRG;
}
if(intype==UNKNOWN)
{
fprintf(stderr,"Unknown input file format. Must be a valid .SNA, .SP, .Z80 or .PRG file,\r");
fprintf(stderr,"or a Raw file\n");
return 3;
}
if(outtype==UNKNOWN)
{
fprintf(stderr,"Unknown output file format. Must be a .SNA, .SP or .Z80 or .PRG file\n");
return 4;
}
/*
if argv[2] only contains the suffix then use the prefix of
argv[1];
*/
if(argv[2][0]=='.')
{
outfile=malloc(strlen(argv[1])+strlen(argv[2])+1);
strcpy(outfile,argv[1]); /* copy prefix */
p=strrchr(outfile,'.');
if(p!=NULL) *p='\0'; /* put end of st