char writesong () { static char i,*ptr; static unsigned short cont,top,j; writebyte(stdout,0x0C); writebyte(stdout,0x00); cont=2; for(i=0;i<12;i++) { cont+=2; top=channels[i].cont; if(!top) { top++; writeword(stdout,cont+top); writebyte(stdout,0); cont++; } else { writeword(stdout,cont+top); for(j=0,ptr=channels[i].data;j<top;j++) writebyte(stdout,*ptr++); cont+=top; } } writeword(stdout,0); }
static void write_DHTinfo(void) { unsigned i; writeword(0xFFC4); // marker writeword(0x01A2); // length writebyte(0); // HTYDCinfo for (i = 0; i < 16; i++) writebyte(std_dc_luminance_nrcodes[i+1]); for (i = 0; i < 12; i++) writebyte(std_dc_luminance_values[i]); writebyte(0x10); // HTYACinfo for (i = 0; i < 16; i++) writebyte(std_ac_luminance_nrcodes[i+1]); for (i = 0; i < 162; i++) writebyte(std_ac_luminance_values[i]); writebyte(1); // HTCbDCinfo for (i = 0; i < 16; i++) writebyte(std_dc_chrominance_nrcodes[i+1]); for (i = 0; i < 12; i++) writebyte(std_dc_chrominance_values[i]); writebyte(0x11); // HTCbACinfo = 0x11; for (i = 0; i < 16; i++) writebyte(std_ac_chrominance_nrcodes[i+1]); for (i = 0; i < 162; i++) writebyte(std_ac_chrominance_values[i]); }
static int net_send_packet(struct sk_buff *skb, struct net_device *dev) { struct net_local *lp = (struct net_local *)dev->priv; #if 0 if (net_debug > 3) { /*OZH*/ printk("%s: sent %d byte packet of type %x\n", dev->name, skb->len, (skb->data[ETH_ALEN+ETH_ALEN] << 8) | skb->data[ETH_ALEN+ETH_ALEN+1]); } #endif /* keep the upload from being interrupted, since we ask the chip to start transmitting before the whole packet has been completely uploaded. */ spin_lock_irq(&lp->lock); netif_stop_queue(dev); /* initiate a transmit sequence */ writeword(dev, TX_CMD_PORT, lp->send_cmd); writeword(dev, TX_LEN_PORT, skb->len); /* Test to see if the chip has allocated memory for the packet */ if ((readreg(dev, PP_BusST) & READY_FOR_TX_NOW) == 0) { /* * Gasp! It hasn't. But that shouldn't happen since * we're waiting for TxOk, so return 1 and requeue this packet. */ spin_unlock_irq(&lp->lock); if (net_debug) printk("cs89x0: Tx buffer not free!\n"); return 1; } /* Write the contents of the packet */ outsw(dev->base_addr + TX_FRAME_PORT,skb->data,(skb->len+1) >>1); spin_unlock_irq(&lp->lock); dev->trans_start = jiffies; dev_kfree_skb (skb); /* * We DO NOT call netif_wake_queue() here. * We also DO NOT call netif_start_queue(). * * Either of these would cause another bottom half run through * net_send_packet() before this packet has fully gone out. That causes * us to hit the "Gasp!" above and the send is rescheduled. it runs like * a dog. We just return and wait for the Tx completion interrupt handler * to restart the netdevice layer */ return 0; }
static void write_DQTinfo(void) { unsigned i; writeword(0xFFDB); writeword(132); writebyte(0); for (i = 0; i < 64; i++) writebyte(((unsigned char*)qtable_0_lum)[zig[i]]); // zig-zag order writebyte(1); for (i = 0; i < 64; i++) writebyte(((unsigned char*)qtable_0_chrom)[zig[i]]); // zig-zag order }
void OutBuffer::writenl() { #if _WIN32 #if M_UNICODE write4(0x000A000D); // newline is CR,LF on Microsoft OS's #else writeword(0x0A0D); // newline is CR,LF on Microsoft OS's #endif #else #if M_UNICODE writeword('\n'); #else writeByte('\n'); #endif #endif }
static void write_APP0info(void) { writeword(0xFFE0); //marker writeword(16); //length writebyte('J'); writebyte('F'); writebyte('I'); writebyte('F'); writebyte(0); writebyte(1);//versionhi writebyte(1);//versionlo writebyte(0);//xyunits writeword(1);//xdensity writeword(1);//ydensity writebyte(0);//thumbnwidth writebyte(0);//thumbnheight }
void OutBuffer::writenl() { #if _WIN32 writeword(0x0A0D); // newline is CR,LF on Microsoft OS's #else writeByte('\n'); #endif if (doindent) linehead = 1; }
static char write_SOSinfo(image_type_t t) { switch(t) { case Y_IMAGE: writeword(0xFFDA); //marker writeword(8); //length writebyte(1); //nrofcomponents writebyte(1); //IdY writebyte(0); //HTY writebyte(0); //Ss writebyte(0x3F); //Se writebyte(0); //Bf return 1; break; case YCBCR_IMAGE: writeword(0xFFDA); //marker writeword(12); //length writebyte(3); //nrofcomponents writebyte(1); //IdY writebyte(0); //HTY writebyte(2); //IdCb writebyte(0x11); //HTCb writebyte(3); //IdCr writebyte(0x11); //HTCr writebyte(0); //Ss writebyte(0x3F); //Se writebyte(0); //Bf return 1; break; case YIQ_IMAGE: case CMYK_IMAGE: default: return -1; break; } }
// should set width and height and image type before writing static char write_SOF0info(const short height, const short width, image_type_t t) { switch(t) { case Y_IMAGE: writeword(0xFFC0); //marker writeword(11); //length writebyte(8); //precision writeword(height); //height writeword(width); //width writebyte(1); //nrofcomponents writebyte(1); //IdY writebyte(0x22); //HVY, 4:2:0 subsampling writebyte(0); //QTY return 1; break; case YCBCR_IMAGE: writeword(0xFFC0); //marker writeword(17); //length writebyte(8); //precision writeword(height); //height writeword(width); //width writebyte(3); //nrofcomponents writebyte(1); //IdY writebyte(0x22); //HVY, 4:2:0 subsampling writebyte(0); //QTY writebyte(2); //IdCb writebyte(0x11); //HVCb writebyte(1); //QTCb writebyte(3); //IdCr writebyte(0x11); //HVCr writebyte(1); //QTCr return 1; break; case YIQ_IMAGE: case CMYK_IMAGE: default: return -1; break; } return 1; }
int main(int argc, char* argv[]) {int i, wordsleft, l, m, u; char *phrase, *p; k = atoi(argv[1]); wordsleft = atoi(argv[2]); srand((unsigned) time(0)); /*comment this out when debugging*/ word[0] = inputchars; while (scanf("%s", word[nword]) != EOF) { if(nword <= 50) printf("%s:", word[nword]); word[nword+1] = word[nword] + strlen(word[nword]) + 1; nword++; } for (i = 0; i < k; i++) word[nword][i] = 0; for (i = 0; i < k; i++) printf("%s\n", word[i]); qsort(word, nword, sizeof(word[0]), sortcmp); phrase = inputchars; for ( ; wordsleft > 0; wordsleft--) { l = -1; u = nword; while (l+1 != u) { m = (l + u) / 2; if (wordncmp(word[m], phrase) < 0) l = m; else u = m; } for (i = 0; wordncmp(phrase, word[u+i]) == 0; i++) if (rand() % (i+1) == 0) p = word[u+i]; phrase = skip(p, 1); if (strlen(skip(phrase, k-1)) == 0) break; writeword(skip(phrase, k-1)); } return 0; }
int enterprise_exec(char *target) { char filename[FILENAME_MAX+1]; FILE *fpin; FILE *fpout; int len; int c,i; if ( help ) return -1; if ( binname == NULL ) { return -1; } if ( outfile == NULL ) { strcpy(filename,binname); suffix_change(filename, extfile); } else { strcpy(filename,outfile); } if ( (fpin=fopen_bin(binname, NULL) ) == 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); } writebyte(0,fpout); writebyte(5,fpout); writeword(len,fpout); for (i=1;i<=12;i++) writebyte(0,fpout); for ( i = 0; i < len; i++) { c = getc(fpin); writebyte(c,fpout); } fclose(fpin); fclose(fpout); return 0; }
int main(int argc, char *argv[]) { char name[11], mybuf[20]; FILE *fpin, *fpout; int c, i, len, pos=32768; if ((argc < 3 )||(argc > 4 )) { fprintf(stdout,"Usage: %s [code file] [tap file] <ORG location>\n",argv[0]); exit(1); } if (argc == 4 ) { pos=atoi(argv[3]); } if ( (fpin=fopen(argv[1],"rb") ) == NULL ) { fprintf(stderr,"Can't open input file\n"); exit(1); } if (fseek(fpin,0,SEEK_END)) { // get input file size fprintf(stderr,"Couldn't determine size of file\n"); fclose(fpin); exit(1); } len=ftell(fpin); fseek(fpin,0L,SEEK_SET); if ( (fpout=fopen(argv[2],"wb") ) == NULL ) { fprintf(stdout,"Can't open output file\n"); exit(1); } /* M/C program, Write out the code header file */ writeword(19,fpout); /* Header len */ writebyte(0,fpout); /* Header is 0 */ parity=0; writebyte(3,fpout); /* Filetype (Code) */ if (strlen(argv[1]) >= 10 ) { // filename strncpy(name,argv[1],10); } else { strcpy(name,argv[1]); strncat(name," ",10-strlen(argv[1])); } for (i=0;i<=9;i++) writebyte(name[i],fpout); writeword(len,fpout); writeword(pos,fpout); /* load address */ writeword(0,fpout); /* offset */ writebyte(parity,fpout); writeword(len+2,fpout); /* Length of next block */ parity=0; writebyte(255,fpout); /* Data... */ for (i=0; i<len;i++) { c=getc(fpin); writebyte(c,fpout); } writebyte(parity,fpout); fclose(fpin); fclose(fpout); }
int mameql_exec(char *target) { char filename[FILENAME_MAX+1]; struct stat binname_sb; FILE *fpin; FILE *fpout; int i,c; if ( help ) return -1; if ( binname == NULL ) { return -1; } if ( outfile == NULL ) { strcpy(filename,binname); suffix_change(filename,".ql"); } else { strcpy(filename,outfile); } if ( description == NULL ) { description = binname; } if ((origin == -1) && ((crtfile == NULL) || ((origin = get_org_addr(crtfile)) == -1))) { origin = 0; } if ( exec == -1 ) { exec = origin; } if ( stat(binname, &binname_sb) < 0 || ( (fpin=fopen_bin(binname, NULL) ) == NULL )) { exit_log(1,"Can't open input file %s\n",binname); } if ( ( fpout = fopen(binname, "rb")) == NULL ) { exit_log(1,"Can't open input file %s\n", binname); } if ( ( fpout = fopen(filename, "wb")) == NULL ) { exit_log(1,"Can't open output file %s\n", filename); } /* Header */ writebyte(0xfd, fpout); writebyte(0x00, fpout); writebyte(0x80, fpout); writebyte(0x00, fpout); writebyte(0x00, fpout); writebyte(0xf9, fpout); writebyte(0x01, fpout); // Varies, what is it? /* Write the program name */ for ( i = 0; i < strlen(description); i++ ) { writebyte(description[i], fpout); } // Description terminator writebyte(0x1a, fpout); // Exec address writeword(exec, fpout); // Start address writeword(origin, fpout); // End address writeword(origin + binname_sb.st_size - 1, fpout); for ( i = 0; i < binname_sb.st_size; i++) { c = getc(fpin); writebyte(c,fpout); } fclose(fpin); fclose(fpout); return 0; }
int svi_exec(char *target) { char filename[FILENAME_MAX+1]; char name[11]; FILE *fpin, *fpout; int c; int i; int len; if ( help || binname == NULL ) return -1; if ( outfile == NULL ) { strcpy(filename,binname); suffix_change(filename,".cas"); } else { strcpy(filename,outfile); } if ( (fpin=fopen(binname,"rb") ) == NULL ) { printf("Can't open input file\n"); exit(1); } /* * Now we try to determine the size of the file * to be converted */ if (fseek(fpin,0,SEEK_END)) { printf("Couldn't determine size of file\n"); fclose(fpin); exit(1); } len=ftell(fpin); fseek(fpin,0L,SEEK_SET); if ( (fpout=fopen(filename,"wb") ) == NULL ) { printf("Can't open output file\n"); exit(1); } /* Write out the header file */ headtune (fpout); for (i=0; i<10;i++) writebyte(208,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(name[i],fpout); writeword(0,fpout); /* Now, the body */ headtune (fpout); writeword(34816,fpout); /* Start Address */ writeword(34816+len+1,fpout); /* End Address */ writeword(34816,fpout); /* Call Address */ /* (58 bytes written so far...) */ /* We append the binary file */ for (i=0; i<len;i++) { c=getc(fpin); writebyte(c,fpout); } /* Append some zeros, just to be sure not to get an error*/ for (i=0; i<16384;i++) writebyte(0,fpout); fclose(fpin); fclose(fpout); }
int gal_exec(char *target) { char filename[FILENAME_MAX+1]; char wavfile[FILENAME_MAX+1]; int c,i,j; int len; unsigned long checksum; FILE *fpin, *fpout; char basicdef[] = "\001\000A=USR(&2C3A)\015"; int basicdeflen=15; int datalen; if ( help ) return -1; if ( binname == NULL || !dumb && ( crtfile == NULL && origin == -1 ) ) { return -1; } if (loud) { gal_h_lvl = 0xFF; gal_l_lvl = 0; } else { gal_h_lvl = 0xe0; gal_l_lvl = 0x20; } if (dumb) { strcpy(filename,binname); } else { if ( outfile == NULL ) { strcpy(filename,binname); suffix_change(filename,".gtp"); } else { strcpy(filename,outfile); } if ( blockname == NULL ) blockname = binname; /* Tomaz's code insertion starts here */ #if 0 /* basic start addr */ h2le_short(0x2c3a+len, &data[0]); /* basic end addr */ h2le_short(0x2c3a+len+basiclen, &data[2]); #endif if ( (fpin=fopen(binname,"rb") ) == NULL ) { myexit("File open error\n",1); } 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); datalen=4+len+basicdeflen; if ( (fpout=fopen(filename,"wb") ) == NULL ) { printf("Can't open output file %s\n",filename); myexit(NULL,1); } /* **** GTP Header **** */ /* *** Name block *** */ fputc(GTP_BLOCK_NAME,fpout); /* Block ID: NAME */ writeword(strlen(blockname)+1,fpout); /* NAME block size */ fputc(0,fpout); fputc(0,fpout); for (i=0; i<=strlen(blockname);i++) { /* block name string */ fputc(blockname[i],fpout); } /* *** Data block *** */ fputc(GTP_BLOCK_STANDARD,fpout); /* Block ID: STD SPEED DATA */ writeword(datalen+6,fpout); /* block size */ fputc(0,fpout); fputc(0,fpout); checksum=0; /* Init checksum */ writebyte_cksum(0xa5,fpout, &checksum); writeword_cksum(0x2c36,fpout, &checksum); /* ORG address */ writeword_cksum(0x2c36+datalen,fpout, &checksum); /* block end location */ writeword_cksum(0x2c36+4+len,fpout, &checksum); /* BASIC start address */ writeword_cksum(0x2c36+datalen,fpout, &checksum); /* block end location */ /* binary file */ for (i=0; i<len;i++) { c=getc(fpin); writebyte_cksum(c, fpout, &checksum); /* fputc(c,fpout);*/ } /* basic */ for (i=0; i<basicdeflen;i++) { /* block name string */ writebyte_cksum(basicdef[i], fpout, &checksum); /*fputc(basicdef[i],fpout);*/ } writebyte(255-(checksum%256),fpout); /* data checksum */ 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 and tone*/ for (i=0; i < 0x5000; i++) fputc(0x80, fpout); /* sync */ gal_rawout(fpout,0); /* program block */ if (len > 0) { for (i=0; i<len;i++) { c=getc(fpin); gal_rawout(fpout,c); } } /* trailing tone and silence (probably not necessary) */ /* gal_bit(fpout,0); gal_tone(fpout); for (i=0; i < 0x10000; i++) fputc(0x80, fpout); */ fclose(fpin); fclose(fpout); /* Now complete with the WAV header */ raw2wav(wavfile); } /* END of WAV CONVERSION BLOCK */ exit(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); }
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); } }
int mz_exec(char *target) { char filename[FILENAME_MAX+1]; char name[11]; FILE *fpin, *fpout; int c; int i; int len; if ( help || binname == NULL ) return -1; if ( outfile == NULL ) { strcpy(filename,binname); suffix_change(filename,".m12"); } else { strcpy(filename,outfile); } if ( (fpin=fopen(binname,"rb") ) == NULL ) { printf("Can't open input file %s\n",binname); exit(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 ( (fpout=fopen(filename,"wb") ) == NULL ) { fclose(fpin); printf("Can't open output file %s\n",filename); myexit(NULL,1); } /* Write out the MZ file */ fputc(1,fpout); /* MZ80K M/C file */ fputc('P',fpout); fputc('R',fpout); fputc('G',fpout); for (i=0;i<14;i++) fputc(13,fpout); writeword(len,fpout); writeword(4608,fpout); writeword(4608,fpout); for (i=0;i<104;i++) fputc(0,fpout); /* ... M/C ...*/ for (i=0; i<len;i++) { c=getc(fpin); fputc(c,fpout); } fclose(fpin); fclose(fpout); }
int msx_exec(char* target) { char filename[FILENAME_MAX + 1]; char wavfile[FILENAME_MAX + 1]; FILE *fpin, *fpout; char name[11]; int c; int i; int pos; int len; if (help) return -1; if (binname == NULL || (!dumb && (crtfile == NULL && origin == -1))) { return -1; } if (origin != -1) { pos = origin; } else { if ((pos = get_org_addr(crtfile)) == -1) { myexit("Could not find parameter ZORG (not z88dk compiled?)\n", 1); } } if (loud) { msx_h_lvl = 0xFd; msx_l_lvl = 2; } else { msx_h_lvl = 0xe0; msx_l_lvl = 0x20; } if (dumb) { strcpy(filename, binname); } else { if (outfile == NULL) { strcpy(filename, binname); if (fmsx) suffix_change(filename, ".cas"); else suffix_change(filename, ".msx"); } else { strcpy(filename, outfile); } if ((fpin = fopen_bin(binname, crtfile)) == NULL) { printf("Can't open input file %s\n", binname); exit(1); } /* * Now we try to determine the size of the file * to be converted */ if (fseek(fpin, 0, SEEK_END)) { printf("Couldn't determine size of file\n"); fclose(fpin); exit(1); } len = ftell(fpin); fseek(fpin, 0L, SEEK_SET); if ((fpout = fopen(filename, "wb")) == NULL) { printf("Can't open output file\n"); exit(1); } /* Write out the header file */ if (fmsx) { /* Block identifier in a CAS file */ for (i = 0; i < 8; i++) fputc(blockid[i], fpout); for (i = 0; i < 10; i++) fputc(0xd0, 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(name[i], fpout); /* Block identifier in a CAS file */ for (i = 0; i < 8; i++) fputc(blockid[i], fpout); } else fputc(254, fpout); writeword(pos, fpout); /* Start Address */ writeword(pos + len + 1, fpout); /* End Address */ writeword(pos, fpout); /* Call Address */ /* We append the binary file */ for (i = 0; i < len; i++) { c = getc(fpin); writebyte(c, fpout); } if (fmsx) { writeword(0, fpout); } fclose(fpin); fclose(fpout); } if ( disk ) { return fat_write_file_to_image("msxbasic", "raw", NULL, filename, NULL, NULL); } /* ***************************************** */ /* 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 and tone*/ for (i = 0; i < 0x3000; i++) fputc(0x80, fpout); for (i = 0; (i < 8000); i++) msx_bit(fpout, 1); /* Skip the block id bytes */ if (fmsx) { for (i = 0; (i < 8); i++) c = getc(fpin); len -= 8; /* Copy the header */ if (dumb) printf("\nInfo: Program Name found in header: "); for (i = 0; (i < 16); i++) { c = getc(fpin); if (dumb && i > 10 && i < 17) printf("%c", c); msx_rawout(fpout, c); } len -= 16; } /* leading silence and tone*/ for (i = 0; i < 0x8000; i++) fputc(0, fpout); for (i = 0; (i < 2000); i++) msx_bit(fpout, 1); /* Skip the block id bytes */ if (fmsx) { for (i = 0; (i < 8); i++) c = getc(fpin); len -= 8; } /* program block */ if (len > 0) { for (i = 0; i < len; i++) { c = getc(fpin); msx_rawout(fpout, c); } } fclose(fpin); fclose(fpout); /* Now complete with the WAV header */ raw2wav(wavfile); } /* END of WAV CONVERSION BLOCK */ return 0; }
int elf32_write(struct edit_file *efile, struct edit_options *opts) { FILE *fptr; int file_size, data_start, next_data, shdr_start, i, j; struct edit_segment *segs = efile->segments; uint32_t e_shnum; Elf32_Ehdr ehdr; Elf32_Shdr shdr; Elf32_Phdr phdr; int binfo_offset = 0; /* If the file hasn't been modified, then just return -- do NOT unmap the file */ if (!(efile->flags & FL_MOD)) return 0; /* File has been modified, so need to write back to disk */ if ((fptr = fopen(efile->filename, "wb")) == NULL) { /* FIXME (sjw Wed Jul 19 05:24:41 2000 ) --- Should give more info here */ log(LOG_CRITICAL, "Couldn't open target file '%s'", efile->filename); return 1; } /* Find size of file -- include space for header. Segments will be calculated later */ shdr_start = next_data = data_start = file_size = sizeof(Elf32_Ehdr) + (sizeof(Elf32_Phdr) * (efile->nsegments)); for (i = 0; i < efile->nsegments; i++) shdr_start += segs[i].filesize; shdr_start = ALIGN(shdr_start, 4); memset(&ehdr, 0, sizeof(Elf32_Ehdr)); /* Write out header */ ehdr.e_ident[0] = ELF_MAGIC_0; ehdr.e_ident[1] = ELF_MAGIC_1; ehdr.e_ident[2] = ELF_MAGIC_2; ehdr.e_ident[3] = ELF_MAGIC_3; ehdr.e_ident[EI_CLASS] = ELFCLASS32; ehdr.e_ident[EI_DATA] = (efile->endianness == LSB ? ELFDATA2LSB : ELFDATA2MSB); ehdr.e_ident[EI_VERSION] = EV_CURRENT; write16(ET_EXEC, &(ehdr.e_type), efile->endianness); write16(arch2elfmach(efile->arch), &(ehdr.e_machine), efile->endianness); write32(EV_CURRENT, &(ehdr.e_version), efile->endianness); write32(efile->entry + opts->elf_hdr_physaddr, &(ehdr.e_entry), efile->endianness); write32(sizeof(Elf32_Ehdr), &(ehdr.e_phoff), efile->endianness); write32(shdr_start, &(ehdr.e_shoff), efile->endianness); write32(elfarchflags(efile->arch), &(ehdr.e_flags), efile->endianness); write16(sizeof(Elf32_Ehdr), &(ehdr.e_ehsize), efile->endianness); write16(sizeof(Elf32_Phdr), &(ehdr.e_phentsize), efile->endianness); /* FIXME (sjw Wed Jul 19 05:16:11 2000 ) --- Should I include the PHDR segment (spec says not to, I _think_) */ write16(efile->nsegments, &(ehdr.e_phnum), efile->endianness); write16(sizeof(Elf32_Shdr), &(ehdr.e_shentsize), efile->endianness); /* Calculate number of section headers: 3 more than actual amount * because we have to create a section header string table and a "null" entry, and binfo*/ e_shnum = get_num_sections(efile) + 3; write16(e_shnum, &(ehdr.e_shnum), efile->endianness); write16(e_shnum - 1, &(ehdr.e_shstrndx), efile->endianness); /* -1 for 0-based index */ /* Write header to the file */ if (fwrite(&(ehdr), sizeof(Elf32_Ehdr), 1, fptr) != 1) { log(LOG_CRITICAL, "Could not write Ehdr to '%s' (%d)\n", efile->filename, ferror(fptr)); return 1; } memset(&phdr, 0, sizeof(Elf32_Phdr)); /* Set up data that doesn't change between segments */ write32(PT_LOAD, &(phdr.p_type), efile->endianness); /* Write out segment headers, and update next_data */ for (i = 0; i < efile->nsegments; i++) { /* FIXME (sjw Tue Aug 15 00:46:39 2000 ) --- This isn't 'properly' aligned */ /* FIXME (sjw Wed Jul 19 10:04:12 2000 ) --- Flags */ write32(PF_X | PF_R | PF_W, &(phdr.p_flags), efile->endianness); write32(next_data, &(phdr.p_offset), efile->endianness); if (i == efile->binfo_seg_ind) { binfo_offset = next_data; } /* XXX: We shift the vaddr to work around PMON loading segments * into vaddrs on MIPS. Since some segments (the kernel) are * already in kseg0, we use bitwise or to leave them alone. * -- alexw */ write32(segs[i].vaddr | opts->elf_hdr_virtaddr, &(phdr.p_vaddr), efile->endianness); write32(segs[i].paddr + opts->elf_hdr_physaddr, &(phdr.p_paddr), efile->endianness); write32(segs[i].filesize, &(phdr.p_filesz), efile->endianness); write32(segs[i].memsize, &(phdr.p_memsz), efile->endianness); write32(segs[i].align, &(phdr.p_align), efile->endianness); /* Write out header to the file */ if (fwrite(&phdr, sizeof(Elf32_Phdr), 1, fptr) != 1) { log(LOG_CRITICAL, "Could not write Phdr to '%s' (%d) (\n", efile->filename, ferror(fptr)); return 1; } next_data += segs[i].filesize; } /* Write out the actual data */ next_data = data_start; for (i = 0; i < efile->nsegments; i++) { if (segs[i].flags & FL_S_PATCH_DIT && (!(efile->flags & FL_NODIT))) { void *write_at = segs[i].data + (segs[i].patch_addr - segs[i].vaddr); writeword(efile->arch, efile->dit_header_segment->paddr, write_at, efile->endianness); } /* nfd: Don't try writing if we have a data size of 0 */ if (segs[i].filesize != 0 && fwrite(segs[i].data, segs[i].filesize, 1, fptr) != 1) { log(LOG_CRITICAL, "Could not write data to '%s' (%d)\n", efile->filename, ferror(fptr)); return 1; } next_data += segs[i].filesize; } /* Fixup offsets of existing sections. */ /*shdr_start = next_data = data_start = file_size = sizeof(Elf32_Ehdr) + (sizeof(Elf32_Phdr) * (efile->nsegments)); for (i = 0; i < efile->nsegments; i++) shdr_start += segs[i].filesize;*/ struct edit_section *current_section; for (i = 0; i < efile->nsegments; i++) { for (current_section = efile->first_section; current_section; current_section = current_section -> next) { if (current_section->segment_num == i) { /* Fixup this section... */ current_section->output_offset = sizeof(Elf32_Ehdr) + (sizeof(Elf32_Phdr) * (efile->nsegments)); for (j=0; j < i; j++) { current_section->output_offset += segs[j].filesize; } current_section->output_offset += current_section->segment_offset; } } } /* We construct two dummy section headers: * - section header 0, which is empty; * - section header string table. */ /* ... first section */ fseek(fptr, shdr_start, SEEK_SET); current_section = calloc(1, sizeof(struct edit_section)); current_section->next = efile->first_section; efile->first_section = current_section; /* ... final section */ while(current_section->next != NULL) { current_section = current_section->next; } current_section = current_section->next = calloc(1, sizeof(struct edit_section)); current_section->type = SHT_PROGBITS; current_section->addr = segs[efile->binfo_seg_ind].paddr; assert(binfo_offset != 0); current_section->output_offset = binfo_offset; current_section->addralign = 0x1000; current_section->entsize = 0; current_section->flags = SHF_ALLOC; current_section->size = efile->binfo_size; strcpy(current_section->name, ".binfo"); uint32_t strtab_size = 1; current_section = efile->first_section; /* Get first after null segment */ while(current_section->next != NULL) { strtab_size += strlen(current_section->name) + 1; current_section = current_section->next; } strtab_size += strlen(current_section->name) + 1; current_section = current_section->next = calloc(1, sizeof(struct edit_section)); current_section->type = SHT_STRTAB; current_section->addr = 0; current_section->output_offset = shdr_start + (sizeof(shdr) * e_shnum); current_section->addralign = 1; current_section->entsize = 0; current_section->flags = 0; strcpy(current_section->name, ".shstrtab"); strtab_size += strlen(current_section->name) + 1; current_section->size = strtab_size; /* Write out the section headers */ current_section = efile->first_section; uint32_t shstrndx_pos = 1; while(current_section) { /* name, type, addr, size, addralign, entsize */ write32(shstrndx_pos, &(shdr.sh_name), efile->endianness); write32(current_section->type, &(shdr.sh_type), efile->endianness); write32(current_section->flags, &(shdr.sh_flags), efile->endianness); write32(current_section->addr, &(shdr.sh_addr), efile->endianness); write32(current_section->output_offset, &(shdr.sh_offset), efile->endianness); /* FIXME! */ write32(current_section->size, &(shdr.sh_size), efile->endianness); write32(0, &(shdr.sh_link), efile->endianness); write32(0, &(shdr.sh_info), efile->endianness); write32(current_section->addralign, &(shdr.sh_addralign), efile->endianness); write32(current_section->entsize, &(shdr.sh_entsize), efile->endianness); if (fwrite(&shdr, sizeof(Elf32_Shdr), 1, fptr) != 1) { log(LOG_CRITICAL, "Could not write Shdr to '%s' (%d)\n", efile->filename, ferror(fptr)); return 1; } shstrndx_pos += strlen(current_section->name) + 1; current_section = current_section->next; } /* We need to add a section for the bootinfo */ /* ... finally, the section header string table itself. */ current_section = efile->first_section; /* String table always begins with a null. */ fwrite("\0", 1, 1, fptr); while(current_section) { int namelen; namelen = strlen(current_section->name); if (namelen > 0) { fwrite(current_section->name, 1, namelen, fptr); } fwrite("\0", 1, 1, fptr); current_section = current_section->next; } fclose(fptr); return 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; }
int main(int argc, char *argv[]) { char name[11]; FILE *fpin, *fpout; int c; int i; int len; if (argc != 3 ) { fprintf(stdout,"Usage: %s [binary file] [Sharp MZ .m12 file]\n",argv[0]); exit(1); } if ( (fpin=fopen(argv[1],"rb") ) == NULL ) { printf("Can't open input file\n"); exit(1); } /* * Now we try to determine the size of the file * to be converted */ if (fseek(fpin,0,SEEK_END)) { printf("Couldn't determine size of file\n"); fclose(fpin); exit(1); } len=ftell(fpin); fseek(fpin,0L,SEEK_SET); if ( (fpout=fopen(argv[2],"wb") ) == NULL ) { printf("Can't open output file\n"); exit(1); } /* Write out the MZ file */ fputc(1,fpout); /* MZ80K M/C file */ fputc('P',fpout); fputc('R',fpout); fputc('G',fpout); for (i=0;i<14;i++) fputc(13,fpout); writeword(len,fpout); writeword(4608,fpout); writeword(4608,fpout); for (i=0;i<104;i++) fputc(0,fpout); /* ... M/C ...*/ for (i=0; i<len;i++) { c=getc(fpin); fputc(c,fpout); } fclose(fpin); fclose(fpout); }