Пример #1
0
int sc3000_exec(char *target)

{

    char       filename[FILENAME_MAX+1];

    char       wavfile[FILENAME_MAX+1];

    char       name[17];

    FILE       *fpin, *fpout;

    long       pos, value;

    int        c, i, len, blocklen;

	int        screen_size;



    if ( help )

        return -1;



    if ( binname == NULL || ( crtfile == NULL && origin == -1 ) ) {

        return -1;

    }



    if ( outfile == NULL ) {

        strcpy(filename,binname);

        suffix_change(filename,".tap");

    } else {

        strcpy(filename,outfile);

    }

    

    if ( blockname == NULL )

        blockname = binname;



    if ( origin != -1 ) {

        pos = origin;

    } else {

		if ( !sf7000 )

			if ( ( pos = parameter_search(crtfile,".sym","myzorg") ) == -1 ) {

				myexit("Could not find parameter ZORG (not z88dk compiled?)\n",1);

        }

    }



    if ( (fpin=fopen(binname,"rb") ) == NULL ) {

        fprintf(stderr,"Can't open input file %s\n",binname);

        myexit(NULL,1);

    }





/*

 *        Now we try to determine the size of the file

 *        to be converted

 */

    if (fseek(fpin,0,SEEK_END)) {

        fclose(fpin);

        myexit("Couldn't determine size of file\n",1);

    }



    len=ftell(fpin);

    fseek(fpin,0,SEEK_SET);



  

    if ( (fpout=fopen(filename,"wb") ) == NULL ) {

        fprintf(stderr,"Can't open output file %s\n",filename);

        myexit(NULL,1);

    }





/* Write out the .tap file */



    if ( sf7000 ) {



	/* CLOADM mode header type */

		writeword(24,fpout);					/* header block size */

		fputc(0x26,fpout);						/* M/C header type (SF-7000 only) */



		/* Deal with the filename */

		checksum = 255;

		if (strlen(blockname) >= 16 ) {

			strncpy(name,blockname,16);

		} else {

			strcpy(name,blockname);

			strncat(name,"                ",16-strlen(blockname));

		}

		for (i=0;i<=15;i++)

			writebyte_cksum(name[i],fpout,&checksum);



		/* len */

		writebyte_cksum(len/256,fpout,&checksum);   /* MSB */

		writebyte_cksum(len%256,fpout,&checksum);   /* LSB */

		/* start */

		writebyte_cksum(pos/256,fpout,&checksum);   /* MSB */

		writebyte_cksum(pos%256,fpout,&checksum);   /* LSB */



		fputc((checksum%256)^0xff,fpout);

		/* extra dummy data to prevent read overflows */

		fputc(0,fpout);

		fputc(0,fpout);



	/* CLOADM object file body*/

		writeword(len+4,fpout);     /* header block size */

		fputc(0x27,fpout);        /* M/C data block type */



		checksum = 255;

		for (i=0; i<len;i++) {

			c=getc(fpin);

			writebyte_cksum(c,fpout,&checksum);

		}

		fputc((checksum%256)^0xff,fpout);

		/* extra dummy data to prevent read overflows */

		fputc(0,fpout);

		fputc(0,fpout);



		fclose(fpin);

		fclose(fpout);



    } else {



	/* BASIC loader header */

		writeword(22,fpout);                   /* header block size */

		fputc(0x16,fpout);                     /* BASIC header type */



		/* Deal with the filename */

		checksum = 255;

		if (strlen(blockname) >= 16 ) {

			strncpy(name,blockname,16);

		} else {

			strcpy(name,blockname);

			strncat(name,"                ",16-strlen(blockname));

		}

		for (i=0;i<=15;i++)

			writebyte_cksum(name[i],fpout,&checksum);

		

		/* len */

		writebyte_cksum((24+len)/256,fpout,&checksum);   /* MSB */

		writebyte_cksum((24+len)%256,fpout,&checksum);   /* LSB */

		

		fputc((checksum%256)^0xff,fpout);

		/* extra dummy data to prevent read overflows */

		fputc(0,fpout);

		fputc(0,fpout);





	/* BASIC loader body */

		writeword(24+len+4,fpout);              /* header block size */

		fputc(0x17,fpout);                      /* BASIC block type */



		checksum = 255;



		/* PROGRAM LINE #1 */

		writebyte_cksum(8,fpout,&checksum);     /* prg line length */

		writebyte_cksum(1,fpout,&checksum);     /* prg line number */

		writebyte_cksum(0,fpout,&checksum);

		writebyte_cksum(0,fpout,&checksum);

		writebyte_cksum(0,fpout,&checksum);

		/* Line size count starts here */

		writebyte_cksum(174,fpout,&checksum);   /* CALL */

		writebyte_cksum(' ',fpout,&checksum);

		writebyte_cksum('&',fpout,&checksum);

		writebyte_cksum('H',fpout,&checksum);

		writebyte_cksum('9',fpout,&checksum);

		writebyte_cksum('8',fpout,&checksum);

		writebyte_cksum('1',fpout,&checksum);

		writebyte_cksum('7',fpout,&checksum);

		writebyte_cksum(':',fpout,&checksum);

		writebyte_cksum(151,fpout,&checksum);   /* STOP */

		/* Line size count ends here */

		writebyte_cksum(13,fpout,&checksum);     /* CR */



		/* PROGRAM LINE #2 (code inside) */

		writebyte_cksum(6,fpout,&checksum);      /* prg line length (just a fake value) */

		writebyte_cksum(2,fpout,&checksum);      /* prg line number */

		writebyte_cksum(0,fpout,&checksum);

		writebyte_cksum(0,fpout,&checksum);

		writebyte_cksum(0,fpout,&checksum);

		/* Line size count starts here */

		writebyte_cksum(144,fpout,&checksum);    /* REM */

		writebyte_cksum(' ',fpout,&checksum);

		/* CODE (location $9817) */

	    for (i=0; i<len;i++) {

			c=getc(fpin);

			writebyte_cksum(c,fpout,&checksum);

		}

		/* Line size count ends here */

		writebyte_cksum(13,fpout,&checksum);     /* CR */

		

		fputc((checksum%256)^0xff,fpout);

	

		/* extra dummy data to prevent read overflows */

		fputc(0,fpout);

		fputc(0,fpout);



    }



	fclose(fpin);

	fclose(fpout);







	/* ***************************************** */

	/*  Now, if requested, create the audio file */

	/* ***************************************** */

	if (( audio ) || ( fast )) {

		if ( (fpin=fopen(filename,"rb") ) == NULL ) {

			fprintf(stderr,"Can't open file %s for wave conversion\n",filename);

			myexit(NULL,1);

		}



        if (fseek(fpin,0,SEEK_END)) {

           fclose(fpin);

           myexit("Couldn't determine size of file\n",1);

        }

        len=ftell(fpin);

        fseek(fpin,0,SEEK_SET);



        strcpy(wavfile,filename);



        if ( survivors )

			suffix_change(wavfile,".bit");

        else

			suffix_change(wavfile,".RAW");



		if ( (fpout=fopen(wavfile,"wb") ) == NULL ) {

			fprintf(stderr,"Can't open output raw audio file %s\n",wavfile);

			myexit(NULL,1);

		}



		/* leading silence */

		if ( survivors ) {

			for (i=0; i < 30; i++)

				fputc(' ', fpout);

		} else {



			for (i=0; i < 0x3000; i++)

				fputc(0x20, fpout);

		}



		/* Data blocks */

		while (ftell(fpin) < len) {

			/* leader tone (3600 records of bit 1) */

			for (i=0; i < 3600; i++)

				sc3000_bit (fpout,1);

			/* data block */

			blocklen = (getc(fpin) + 256 * getc(fpin));

			for (i=0; (i < blocklen); i++) {

				c=getc(fpin);

				sc3000_rawout(fpout,c);

			}

		}



			/* trailing silence */

		if ( survivors ) {

			for (i=0; i < 100; i++)

				fputc(' ', fpout);

		} else {

			for (i=0; i < 0x10000; i++)

				fputc(0x20, fpout);

		}



        fclose(fpin);

        fclose(fpout);



		/* Now complete with the WAV header */

        if ( !survivors )

			raw2wav(wavfile);



	}

	

    exit(0);

}
Пример #2
0
int trs80_exec()
{
    char    filename[FILENAME_MAX+1];
    char    wavfile[FILENAME_MAX+1];
	char	name[11];
    FILE    *fpin, *fpout;
    int     c;
    int     i;
    int     len;
    int     pos;
	unsigned char cksum;
	char	ckflag;
    
    if ( help || binname == NULL || !dumb  && ( crtfile == NULL ) ) {
        return -1;
    }

    if ( origin != -1 ) {
        pos = origin;
    } else {
        if ( ( pos = parameter_search(crtfile,".sym","myzorg") ) == -1 ) {
            myexit("Could not find parameter ZORG (not z88dk compiled?)\n",1);
        }
    }

	if ( audio )   cmd = 0;

	if (( blocksz < 10 ) || ( blocksz > 256 )) {
		myexit("Invalid block size: %d\n",blocksz);
	}
	if ( cmd ) blocksz-=2;


	if (dumb) {
		strcpy(filename,binname);
	} else {

		if ( outfile == NULL ) {
			strcpy(filename,binname);
			if ( cmd )
				suffix_change(filename,".cmd");
			else
				suffix_change(filename,".cas");
		} else {
			strcpy(filename,outfile);
		}

		if ( (fpin=fopen(binname,"rb") ) == NULL ) {
			fprintf(stderr,"Can't open input file %s\n",binname);
			myexit(NULL,1);
		}


	/*
	 *        Now we try to determine the size of the file
	 *        to be converted
	 */
		if ( fseek(fpin,0,SEEK_END) ) {
			fclose(fpin);
			myexit("Couldn't determine size of file\n",1);
		}

		len=ftell(fpin);

		fseek(fpin,0L,SEEK_SET);


	/*
	 *        CMD file creation
	 */

		if ( (fpout=fopen(filename,"wb") ) == NULL ) {
			fprintf(stderr,"Can't open output CMD file %s\n",filename);
			myexit(NULL,1);
		}
		
		if ( !cmd ) {
			/*
			 *   CAS file mode
			 */

				for (i=0; i<256; i++)
					fputc( 0, fpout);

				fputc( 0xA5, fpout );
				fputc( 0x55, fpout );

			/* Deal with the filename */
				if (strlen(binname) >= 6 ) {
					strncpy(name,binname,6);
				} else {
					strcpy(name,binname);
					strncat(name,"      ",6-strlen(binname));
				}
				for	(i=0;i<6;i++)
					writebyte(toupper(name[i]),fpout);
		}
		
		
		/*
		 *   Main loop
		 */

		 for (i=0; i<len; i++) {

			if ( (i%blocksz)== 0 ) {
				if ( cmd )
					writebyte (1,fpout);       /* Block signature byte in CMD mode */
				else
					writebyte (0x3c,fpout);    /* Escape char for block signature in CAS mode */

				if ( (i+blocksz) > len )
					if ( cmd )
						writebyte (len-i+2,fpout);      /* last block length (remainder) */
					else
						writebyte (len-i,fpout);      /* last block length (remainder) */
				else
					if ( cmd )
						if (blocksz == 254)
							writebyte (0,fpout);            /* block length (256 bytes) */
						else
							writebyte (blocksz+2,fpout);    /* block length */
					else
						if (blocksz == 256)
							writebyte (0,fpout);            /* block length (256 bytes) */
						else
							writebyte (blocksz,fpout);      /* block length (CAS mode)*/
				
				writeword (pos+i,fpout);        /* block memory location */
				cksum=(pos+i)%256+(pos+i)/256;  /* Checksum (for CAS mode) */
			}
			
			c=getc(fpin);
			cksum +=c;			/* Checksum (for CAS mode) */
			fputc(c,fpout);
			ckflag=1;

			if ( !cmd && ( (i+1)%blocksz == 0 ) ) {
				writebyte (cksum,fpout);	/* Checksum (for CAS mode) */
				ckflag=0;
			}

		}

		if ( cmd ) {
			writebyte (2,fpout);            /* Two bytes end marker in CMD mode*/
			writebyte (2,fpout);
		} else {
			if ( ckflag )
				writebyte (cksum,fpout);	/* Checksum */
			writebyte (0x78,fpout);         /* Escape char for EOF in CAS mode */
		}

		writeword (pos,fpout);      /* Start address */


		fclose(fpin);
		fclose(fpout);
	}
 

	/* ***************************************** */
	/*  Now, if requested, create the audio file */
	/* ***************************************** */
	if (( audio ) || ( fast ) || (loud)) {
		if ( (fpin=fopen(filename,"rb") ) == NULL ) {
			fprintf(stderr,"Can't open file %s for wave conversion\n",filename);
			myexit(NULL,1);
		}
		
        if (fseek(fpin,0,SEEK_END)) {
           fclose(fpin);
           myexit("Couldn't determine size of file\n",1);
        }
        len=ftell(fpin);
        fseek(fpin,0,SEEK_SET);

        strcpy(wavfile,filename);

		suffix_change(wavfile,".RAW");

		if ( (fpout=fopen(wavfile,"wb") ) == NULL ) {
			fprintf(stderr,"Can't open output raw audio file %s\n",wavfile);
			myexit(NULL,1);
		}

		/* leading silence */
		for (i=0; i < 0x3000; i++)
			fputc(0x80, fpout);

		/* Skip the tone leader bytes */
		do {
			len--;
			c=getc(fpin);
		} while ((c == 0) && (len >0));
		
		if ( len < 10 ) {
			fprintf(stderr,"Invalid .CAS file %s\n",filename);
			myexit(NULL,1);
		}
		
		if ( c != 0xA5 ) {
			fprintf(stderr,"Header marker not found:0xA5\n");
			myexit(NULL,1);
		}

		len--;
		c=getc(fpin);
/*
		if ( c != 0x55 ) {
			fprintf(stderr,"Header marker not found:0x55\n");
			myexit(NULL,1);
		}
*/
		for (i=0; i<600;i++) {
			trs80_rawout(fpout,0);
		}

		trs80_rawout(fpout,0xa5);
		trs80_rawout(fpout,c);

		/* Show filename */
		if (dumb) printf("\nInfo: Program Name found in header: ");
		for (i=0; (i < 6); i++) {
			c=getc(fpin);
			if (dumb) printf("%c",c); 
			trs80_rawout(fpout,c);
		}
		if (dumb) printf("\n"); 

		/* Short extra pause */
		for ( i=0; i < 48; i++ )
			fputc (0x80,fpout);

		if (len > 0) {
			for (i=0; i<len;i++) {
				c=getc(fpin);
				trs80_rawout(fpout,c);
			}
		}

        fclose(fpin);
        fclose(fpout);

		/* Now complete with the WAV header */
		raw2wav(wavfile);
			
	}  /* END of WAV CONVERSION BLOCK */

	
    return 0;
}
Пример #3
0
int sos_exec(char *target)
{
    char    filename[FILENAME_MAX+1];
    FILE   *fpin;
    FILE   *fpout;
    int     len;
    long    pos;
    int     c,i;

    if ( help )
        return -1;

    if ( binname == NULL || ( crtfile == NULL && origin == -1 ) ) {
        return -1;
    }

	if ( origin != -1 ) {
		pos = origin;
	} else {
		if ( ( pos = parameter_search(crtfile,".sym","myzorg") ) == -1 ) {
			myexit("Could not find parameter ZORG (not z88dk compiled?)\n",1);
		}
	}

    if ( outfile == NULL ) {
        strcpy(filename,binname);
        suffix_change(filename,".obj");
    } else {
        strcpy(filename,outfile);
    }

    if ( (fpin=fopen(binname,"rb") ) == NULL ) {
        fprintf(stderr,"Can't open input file %s\n",binname);
        myexit(NULL,1);
    } 

    if (fseek(fpin,0,SEEK_END)) {
        fprintf(stderr,"Couldn't determine size of file\n");
        fclose(fpin);
        myexit(NULL,1);
    }

    len=ftell(fpin);

    fseek(fpin,0L,SEEK_SET);

    if ( (fpout=fopen(filename,"wb") ) == NULL ) {
        fclose(fpin);
        myexit("Can't open output file\n",1);
    }

	fprintf(fpout,"_SOS 01 %4X %4X%c",pos,pos,10);

    for ( i = 0; i < len; i++) {
        c = getc(fpin);
        writebyte(c,fpout);
    }

    fclose(fpin);
    fclose(fpout);

    return 0;
}
Пример #4
0
int residos_exec(char *target)
{ 
    FILE    *binfile;        /* Read in bin file */
    long    filesize;
    long    readlen;
    long    table_start;
    long    table_end;
    long    package_id;
    
    if ( help ) {
        return -1;
    }

    if ( binname == NULL || crtfile == NULL ) {
        return -1;
    }

    if ( outfile == NULL ) {
        outfile = binname;
    }

    table_start = parameter_search(crtfile,".map","package_call_table");
    table_end = parameter_search(crtfile,".map","package_call_end");
    package_id = parameter_search(crtfile,".map","residos_package_id");

    if ( table_start == -1 ) {
        myexit("Could not find parameter package_call_table (not a Residos package compile?)\n",1);
    }
    if ( table_end == -1 ) {
        myexit("Could not find parameter package_call_end (not a Residos package compile?)\n",1);
    }
     if ( package_id == -1 ) {
        myexit("Could not find parameter residos_package_id (not a Residos package compile?)\n",1);
    }   

    if ( ( binfile=fopen(binname, "rb") ) == NULL ) {
        myexit("Can't open binary file\n",1);
    }

    if (fseek(binfile, 0, SEEK_END)) {
        fclose(binfile);
        myexit("Couldn't determine the size of the file\n",1);
    }

    filesize=ftell(binfile);
    
    if (filesize > 0x3fc0 ) {
        fclose(binfile);
        myexit("The source binary is over 16,320 bytes in length.\n",1);
    }
    
    if ( ( memory = calloc(16384, 1) ) == NULL ) {
        myexit("Can't allocate memory\n",1);
    }
    
    
    /* Basic header size is 16:
        
    defm    "ZXPKG"                 ; magic identifier
    defb    TESTPKG_CAPS            ; capabilities bitmask
    defb    TESTPKG_ID              ; package ID
    defw    TESTPKG_MIN_RESIDOS     ; minimum ResiDOS version required
    defb    TESTPKG_MAX_CALL_ID     ; highest call ID provided
    defw    TESTPKG_CALL_TABLE      ; address of package call table
        
    defb    0  ; Hook code terminator
    defb    0  ; channel name terminator
    defb    0  ; end of syntax table
    defb    0  ; end of function table
        
    // Binary blob
    */
    
    strcpy(memory,"ZXPKG");
    memory[5] = 0;  /* Capabilities */
    memory[6] = package_id;
    memory[7] = 0x23;    /* Minimum Residos version is v2.23 */
    memory[8] = 0x02;
    memory[9] = (table_end - table_start) / 2;
    memory[10] = table_start % 256;
    memory[11] = table_start / 256;
    memory[12] = 0; /* Hook code terminator */
    memory[13] = 0; /* Channel name terminator */
    memory[14] = 0; /* Syntax table terminator */
    memory[15] = 0; /* Function table terminator */
    fseek(binfile, 0, SEEK_SET);
    readlen = fread(memory + HEADER_SIZE,1,filesize,binfile);        

    if ( filesize != readlen ) {
        fclose(binfile);
        myexit("Couldn't read in binary file\n",1);
    }

    fclose(binfile);

    save_block(filesize + HEADER_SIZE,outfile,".com");

    myexit(0,0);
	return 0;
}
Пример #5
0
int nec_exec(char *target)

{

    char       filename[FILENAME_MAX+1];

    char       wavfile[FILENAME_MAX+1];

    char       name[12];

    FILE       *fpin, *fpout;

    int        c, i, j;

    int        len, stage;

    int        zerocount;



	unsigned char* prefix;

	int        prefix_length;

	int        codeseg;



    if ( binname == NULL ) {

        return -1;

    }





//	if (dumb) {

//		strcpy(filename,binname);

//	} else {

		

		if (origin == -1) {

			if ( ( origin = parameter_search(crtfile,".sym","myzorg") ) == -1 ) {

				fprintf(stderr,"Warning: could not get the 'myzorg' value, ORG defaults to MODE1 ($C437)\n");

				origin = 0xC437;

			}

		}



		if (mode == -1)

		{

			switch (origin)

			{

				case MODE1_ADDRESS_CODESEG:

					mode=MODE1;

					break;

				case MODE2_ADDRESS_CODESEG:

					mode=MODE2;

					break;

				case MODE5_ADDRESS_CODESEG:

					mode=MODE5;

					break;

				case MODEROM_ADDRESS_CODESEG:

					mode=MODEROM;

					break;

				case MODEN88_ADDRESS_CODESEG:

					mode=MODEN88;

					break;

				default:

					fprintf(stderr,"Unhandled value for ORG: %i\n",origin);

					return -1;

			}

		}

			//fprintf(stderr,"Mode: %i\n",mode);



		if ( strcmp(binname,filename) == 0 ) {

			fprintf(stderr,"Input and output file names must be different\n");

			myexit(NULL,1);

		}



		if ( (fpin=fopen(binname,"rb") ) == NULL ) {

			fprintf(stderr,"Can't open input file %s\n",binname);

			myexit(NULL,1);

		}





	/*

	 *        Now we try to determine the size of the file

	 *        to be converted

	 */

		if (fseek(fpin,0,SEEK_END)) {

			fclose(fpin);

			myexit("Couldn't determine size of file\n",1);

		}



		len=ftell(fpin);



		fseek(fpin,0L,SEEK_SET);





		if ( outfile == NULL ) {

			strcpy(filename,binname);

			suffix_change(filename,".cas");

		} else {

			strcpy(filename,outfile);

		}



	  

		if ( (fpout=fopen(filename,"wb") ) == NULL ) {

			fclose(fpin);

			fprintf(stderr,"Can't open output file %s\n",filename);

			myexit(NULL,1);

		}



		// MSB of BASIC RAM start address

		switch (mode) {

		case MODE1:

			prefix = prefix_p6;

			prefix_length = prefix_length_p6;

			prefix[17] = 0xc4;

			prefix[46] = 0xc4;

			prefix[23] = 'C';

			prefix[24] = '4';

			prefix[48] = len%256;

			prefix[49] = len/256;

	codeseg = MODE1_ADDRESS_CODESEG;

			break;

		case MODE5:

			prefix = prefix_p6;

			prefix_length = prefix_length_p6;

			prefix[17] = 0x80;

			prefix[46] = 0x80;

			prefix[23] = '8';

			prefix[24] = '0';

			prefix[48] = len%256;

			prefix[49] = len/256;

			codeseg = MODE5_ADDRESS_CODESEG;

			break;

		case MODEN88:

			prefix = prefix_p88;

			prefix_length = prefix_length_p88;

			codeseg = MODEN88_ADDRESS_CODESEG;

			break;

		case MODEROM:

			prefix = prefix_rom;

			prefix_length = prefix_length_rom;

			codeseg = MODEROM_ADDRESS_CODESEG;

			break;

		case MODE2:

		default:

			prefix = prefix_p6;

			prefix_length = prefix_length_p6;

			//prefix[23] = '8';

			//prefix[24] = '4';

			prefix[48] = len%256;

			prefix[49] = len/256;

			codeseg = MODE2_ADDRESS_CODESEG;

			break;

		}

		// set p6 file name

		for (i = 0; i < 6; i++) {

			prefix[i + 0x0a] = filename[i];

		}



		// write prefix

		for (i = 0; i < prefix_length; i++) {

			fputc(prefix[i],fpout);

		}



		/* ... M/C ...*/

		for (i=0; i<len;i++) {

			c=getc(fpin);

			fputc(c,fpout);

		}



		if (mode != MODEROM) {

			// write suffix

			for (i = 0; i < 12; i++) {

				fputc(0,fpout);

			}

		} else {

			// write trailing bytes

			for (; i < (0x8000-MODEROM_ADDRESS_CODESEG); i++) {

			  //putc(memory[i], out);

				fputc(0,fpout);

			}

		}



		fclose(fpin);

		fclose(fpout);

//	}



	/* ***************************************** */

	/*  Now, if requested, create the audio file */

	/* ***************************************** */

	if (( audio ) || ( fast )) {

		if ( (fpin=fopen(filename,"rb") ) == NULL ) {

			fprintf(stderr,"Can't open file %s for wave conversion\n",filename);

			myexit(NULL,1);

		}



        if (fseek(fpin,0,SEEK_END)) {

           fclose(fpin);

           myexit("Couldn't determine size of file\n",1);

        }

        len=ftell(fpin);

        fseek(fpin,0,SEEK_SET);



        strcpy(wavfile,filename);



			suffix_change(wavfile,".RAW");



		if ( (fpout=fopen(wavfile,"wb") ) == NULL ) {

			fprintf(stderr,"Can't open output raw audio file %s\n",wavfile);

			myexit(NULL,1);

		}



		/* leading silence */

		for (i=0; i < 0x3000; i++)

			fputc(0x20, fpout);





		/* leading tone */

		for (i=0; i < 3600; i++)

			nec_bit (fpout,1);



		stage=0;

		

		for (i=0; (i < len); i++) {

			c=getc(fpin);

			switch(stage)

			{

				// BASIC Header: seek for the filename termination

				case 0:

					if (c==0) {

						nec_rawout(fpout,0);

						stage++;

						for (j=0; j < 600; j++)

							nec_bit (fpout,1);

						c=getc(fpin);

					}

					break;

				// BASIC Block + MC stub: seek for more than 3 zeroes in sequence

				case 1:

					if ((c==0) && (zerocount == 3) ) {

						stage++;

						while ((c=getc(fpin)) == 0)

							zerocount++;

						for (j=3; j <= zerocount; j++)

							nec_rawout(fpout,0);

						zerocount = 0;	// Not necessary

						for (j=0; j < 600; j++)

							nec_bit (fpout,1);

					}

					break;

				default:

					break;

					//fprintf(stderr,"Problems",origin);

					//return -1;

			}

			nec_rawout(fpout,c);

			if (c)

				zerocount = 0;

			else

				zerocount++;

		}



		// tail

		for (j=0; j < 200; j++)

			nec_bit (fpout,1);



		/* Data blocks */

		//while (ftell(fpin) < len) {

			/* leader tone (3600 records of bit 1) */

		//	for (i=0; i < 3600; i++)

		//		nec_bit (fpout,1);

			/* data block */

		//	blocklen = (getc(fpin) + 256 * getc(fpin));

		//	for (i=0; (i < blocklen); i++) {

		//		c=getc(fpin);

		//		nec_rawout(fpout,c);

		//	}

		//}



		/* trailing silence */

		for (i=0; i < 0x10000; i++)

			fputc(0x20, fpout);



        fclose(fpin);

        fclose(fpout);



		/* Now complete with the WAV header */

		raw2wav(wavfile);



	}



    exit(0);

}
Пример #6
0
int newbrain_exec()
{
    char    filename[FILENAME_MAX+1];
    FILE    *fpin, *fpout, *fpout2;
    int    pos;

    int        c;
    int        i,p,l,b;
    int        len;
    int        lnum;
    int        blocks;
    int        blcount;

    unsigned long       checksum;

        
    if ( help || binname == NULL || ( crtfile == NULL && origin == -1 ) ) {
        return -1;
    }

    if ( blockname == NULL ) blockname = binname;

    if ( outfile == NULL ) {
        strcpy(filename,binname);
        suffix_change(filename,".txt");
    } else {
        strcpy(filename,outfile);
    }


    if ( origin != -1 ) {
        pos = origin;
    } else {
        if ( ( pos = parameter_search(crtfile,".sym","MYZORG") ) == -1 ) {
            myexit("Could not find parameter ZORG (not z88dk compiled?)\n",1);
        }
    }


    if ( (fpin=fopen(binname,"rb") ) == NULL ) {
        fprintf(stderr,"Can't open input file %s\n",binname);
        myexit(NULL,1);
    }


/*
 *        Now we try to determine the size of the file
 *        to be converted
 */
    if  (fseek(fpin,0,SEEK_END)) {
        fclose(fpin);
        myexit("Couldn't determine size of file\n",1);
    }

    len=ftell(fpin);


    fseek(fpin,0L,SEEK_SET);


    if ( ascii ) {

            /* All in a BASIC program */

            if ( (fpout=fopen(filename,"w") ) == NULL ) {
                printf("Can't open output text format file %s\n",filename);
                myexit(NULL,1);
            }
                    
            fprintf(fpout,"10 IF TOP>%i THEN RESERVE TOP-%i\n",pos-1,pos-1);
            fprintf(fpout,"20 FOR i=0TO%i:READa:POKE%i+i,a:NEXT i\n",len-1,pos);
            fprintf(fpout,"30 CALL%i\n",pos);
            fprintf(fpout,"40 END",pos);
            lnum=100;
            /* ... M/C ...*/
            for (i=0; i<len;i++) {
                if ((i % 60) == 0) {
                    fprintf(fpout,"\n");
                    fprintf(fpout,"%i DATA ",lnum);
                    lnum=lnum+2;
                }
                else
                    fputc(',',fpout);
                c=getc(fpin);
                fprintf(fpout,"%i",c);
            }
            fprintf(fpout,"\n");
            
            fclose(fpin);
            fclose(fpout);

    } 
    else    /*  Much more efficient LOADER  */
    {

            /* binary block */

            suffix_change(filename,".dat");
        
            if ( (fpout2=fopen(filename,"wb") ) == NULL ) {
                printf("Can't open output dat Binary file %s\n",filename);
                myexit(NULL,1);
            }

            checksum=0x3b;                 /* Init checksum */
                fputc (0,fpout2);
            writeword_cksum(strlen(blockname), fpout2, &checksum);
            for (i=0; i<strlen(blockname); i++) {
                fputc (blockname[i],fpout2);
                checksum += blockname[i];
            }
            writebyte_cksum(0x81, fpout2, &checksum);
            writeword((checksum%65536),fpout2);   /* name checksum */
        
            /* Block */
            for (b=0; b<=(len/1024); b++) {
            
              for (i=0; i<10;i++)          /* block leading zeroes */
                fputc (0,fpout2);
              
              checksum=0x3b;               /* Init checksum */
        
              p = (b * 1024) + 1024 -1;    /* set the pointer at the end of the block */
              l = 1024;
        
              if ( p > (len - 1) ) {       /* is last block smaller ? */
                 p = len - 1;
                 l = len - (b * 1024);
              }
              
              /* Block length*/
              writeword_cksum(l, fpout2, &checksum);
              
              for (i=p; i>(p-l); i--) {
                  fseek(fpin, i, SEEK_SET);
                  c = getc(fpin);
                  writebyte_cksum(c, fpout2, &checksum);
              }
              
              writebyte_cksum(b+1, fpout2, &checksum);
              writeword((checksum%65536), fpout2);
            }
        
            for (i=0; i<9;i++)        /* tail */
              fputc (0,fpout2);

            fclose(fpin);
            fclose(fpout2);



            /* TEMP BASIC loader */     
        
            suffix_change(filename,".tmp");

            if ( (fpout=fopen(filename,"wb") ) == NULL ) {
                printf("Can't open temp output file %s\n",filename);
                myexit(NULL,1);
            }

            fputc(0x0d,fpout);
            
            fprintf(fpout,"10 ");
            fputc(0xAC,fpout);        /* RESERVE */
            fputc(0xA8,fpout);        /* TOP */
            fputc(0x82,fpout);        /* - */
            fprintf(fpout,"%i",pos);  /* memory location */
            fputc(0x0d,fpout);

            fprintf(fpout,"20 ");
            fputc('b',fpout);
            fputc(0x8c,fpout);        /* = */
            fprintf(fpout,"121:");  /* 121 */
            fputc(0x89,fpout);        /* IF */
            fputc(0xa4,fpout);        /* PEEK */
            fprintf(fpout,"(51)");
            fputc(0x8d,fpout);        /* > */
            fprintf(fpout,"127");
            fputc(0xb4,fpout);        /* THEN */
            fprintf(fpout,"b");
            fputc(0x8c,fpout);        /* = */
            fputc(0xa4,fpout);        /* PEEK */
            fprintf(fpout,"(168)");
            fputc(0x81,fpout);        /* + */
            fprintf(fpout,"256");
            fputc(0x83,fpout);        /* * */
            fputc(0xa4,fpout);        /* PEEK */
            fprintf(fpout,"(169):");
            fputc(0x9e,fpout);        /* POKE */
            fprintf(fpout,"169,");
            fputc(0x96,fpout);        /* INT */
            fprintf(fpout,"((b");
            fputc(0x81,fpout);        /* + */
            fprintf(fpout,"23)");
            fputc(0x84,fpout);        /* / */
            fprintf(fpout,"256):");
            fputc(0x9e,fpout);        /* POKE */
            fprintf(fpout,"168,b");
            fputc(0x81,fpout);        /* + */
            fprintf(fpout,"23");
            fputc(0x82,fpout);        /* - */
            fprintf(fpout,"256");
            fputc(0x83,fpout);        /* * */
            fputc(0xa4,fpout);        /* PEEK */
            fprintf(fpout,"(169)");
            fputc(0x0d,fpout);

            fprintf(fpout,"30 ");
            fputc(0x8b,fpout);        /* FOR */
            fprintf(fpout," i ");
            fputc(0x8c,fpout);        /* = */
            fprintf(fpout," 0 ");
            fputc(0xb6,fpout);        /* TO */
            fprintf(fpout," 22:");
            fputc(0x9c,fpout);        /* READ */
            fprintf(fpout," c:");
            fputc(0x9e,fpout);        /* POKE */
            fprintf(fpout," b");
            fputc(0x81,fpout);        /* + */
            fprintf(fpout,"i,c:");
            fputc(0x8c,fpout);        /* NEXT */
            fprintf(fpout," i:");
            fputc(0x9d,fpout);        /* DATA */
            fprintf(fpout," 253,110,32,253,102,33,22,0,30,1,1,"); 
            fprintf(fpout,"%i",len%256);  /* LSB for Length */
            fputc(',',fpout);
            fprintf(fpout,"%i",len/256);  /* MSB for Length */
            fprintf(fpout,",231,49,119,35,11,120,177,200,24,246"); 
            fputc(0x0d,fpout);

            fprintf(fpout,"40 ");
            fputc(0x97,fpout);        /* OPEN */
            //fputc(' ',fpout);
            fputc(0xb3,fpout);        /* # */
            fprintf(fpout,"1,");
            fputc('"',fpout);
            fprintf(fpout,"%s",blockname);
            fputc('"',fpout);
            fputc(0x0d,fpout);
            
            fprintf(fpout,"50 ");
            fputc(0x9f,fpout);        /* CALL */
            fprintf(fpout," b : ");
            fputc(0x9f,fpout);        /* CALL */
            fprintf(fpout," %i",pos);
            fputc(0x0d,fpout);
            
            fputc(0x04,fpout);
            fputc(0x0d,fpout);
            fclose(fpout);            


            
            /* Now convert in tape format */

            if ( (fpin=fopen(filename,"rb") ) == NULL ) {
                fprintf(stderr,"Can't read temp file%s\n",binname);
                myexit(NULL,1);
            }

            if  (fseek(fpin,0,SEEK_END)) {
                fclose(fpin);
                myexit("Couldn't determine size of temp file\n",1);
            }
        
            len=ftell(fpin);

            suffix_change(filename,".bas");
        
            if ( (fpout2=fopen(filename,"wb") ) == NULL ) {
                printf("Can't open output BASIC Binary file %s\n",filename);
                myexit(NULL,1);
            }
                
            /* header for binary block */
        
            checksum=0x3b;              /* Init checksum */

            fputc (0,fpout2);
            writeword_cksum(strlen(filename), fpout2, &checksum);
            for (i=0; i<strlen(filename); i++) {
                fputc (filename[i],fpout2);             // +1 ??
                checksum += filename[i];
            }
            writebyte_cksum(0x81, fpout2, &checksum);
            writeword((checksum%65536),fpout2); /* name checksum */
        
            for (i=0; i<10;i++)         /* zero filling the next part */
                fputc (0,fpout2);

            checksum=0x3b;              /* Init checksum */

            /* Block length*/
            writeword_cksum(len, fpout2, &checksum);
        
            /* Block */
            for (i=len-1; i>=0; i--) {
                  fseek(fpin, i, SEEK_SET);
                  c=getc(fpin);
                  writebyte_cksum(c, fpout2, &checksum);
            }
        
            writebyte_cksum(0x41, fpout2, &checksum);
            
            writeword((checksum%65536),fpout2);  /* block checksum */
            for (i=0; i<9;i++)                   /* tail  */
                fputc (0,fpout2);

            fclose(fpin);
            fclose(fpout2);


            /* TAPE directory */
            
            if ( (fpout=fopen("_dir.txt","w") ) == NULL ) {
                printf("Can't open output text format file %s\n",filename);
                myexit(NULL,1);
            }
            fprintf(fpout,"%s\n",filename);
            suffix_change(filename,".dat");
            fprintf(fpout,"%s\n",filename);
            fclose(fpout);
            
            suffix_change(filename,".tmp");
            remove(filename);

    }    

}