 
/*
   Build a program which will:
   Split-up a binary file into rom sizes with all the EVEN ordered
   bytes in one rom sized file and all the ODD ordered bytes in
   another rom sized file

   Proposed syntax:
   ThisProgram [sourcefile] [rom size in bytes] <return>

   Output filesnames will be:
   [source]-1even [source]-1odd   [source]-2even [source]-2odd ...

   Thingies to do:
   3) make AmigaDos version
*/

#include "lattice/stdio.h"
#include "lattice/fcntl.h"
#define BUFSIZE 4096  /* must be even # */
#define IS_ODD(num) num&0x01
#define ERROR -1L

main(argc,argv)
int argc;
char *argv[];
{
   int st1, st2, st3;    /* number of valid bytes to buffer */
   int cnt1, cnt2, cnt3; /* indexes to each buffer */
   int numrom=1; /* inc'd number for romsets */
   char strrom[20]; /* string pointer for stringerfied numrom */
   long fn1, fn2, fn3;   /* file numbers */
   long sourcesize, scnt=0, romsize, rcnt=0;
   static char buffer1[BUFSIZE*2], buffer2[BUFSIZE], buffer3[BUFSIZE];
   static char string3[] =
   "A program to split a binary file into S-format for 2 or more Proms\n";
   static char string2[] =
   "FileNames will be: [name]-1.e [name]-1.o, [name]-2.e [name]-2.o ...\n";
   static char string1[] =
   "Ver 0.0 by Bruce Takahashi  - C'dore Repairs (415) 525-6973\n";

   if(argc<3) {
      printf("syntax: %s SourceFile RomSizeInK(2^10)\n", argv[0]);
      printf("%s%s%s", string3,string2,string1);
      goto TheEnd;
   }
   romsize = atol(argv[2])*1024;
   printf("rom size = %d\n",romsize);
   if(IS_ODD(romsize)) {
      printf("#bytes per Rom must be an EVEN number\n");
      goto TheEnd;
   }

   (void)stccpy(string1, argv[1], strlen(string1));

   fn1 = open(string1, O_RDONLY);
   if(!fn1) {
      printf("Cannot open source file %s\n",string1);
      goto TheEnd;
   }
   st1 = lseek(fn1,0L,2);
   if(!st1) goto Cleanup1;
   sourcesize = st1;
   st1 = lseek(fn1,0L,0);
   if(st1 == ERROR) goto Cleanup1;
   printf("source file size = %d\n",sourcesize);

MORESETS:

   (void)stci_d(strrom,numrom++,sizeof(strrom));
   (void)sprintf(string2,"%s-%s.even",string1,strrom);
   (void)sprintf(string3,"%s-%s.odd",string1,strrom);

   printf("Create files => %s and %s\n",string2,string3);

   fn2 = open(string2, O_CREAT);
   if(!fn2) {
      printf("Cannot open %s\n",string2);
      goto Cleanup1;
   }
   fn3 = open(string3, O_CREAT);
   if(!fn3) {
      printf("Cannot open %s\n",string3);
      goto Cleanup2;
   }

   while(st1=read(fn1,buffer1,sizeof(buffer1))) {
      if(IS_ODD(st1)) buffer1[st1++]=0xff;
      for(cnt1=0,cnt2=0,cnt3=0; cnt1<sizeof(buffer1); cnt1++) {
         ++scnt;
         if(!(IS_ODD(cnt1))) {                   /* EVEN bytes */
            buffer2[cnt2++] = buffer1[cnt1];
         }
         else buffer3[cnt3++] = buffer1[cnt1];  /* ODD bytes */
         if(++rcnt == romsize*2) { rcnt=0; break; }
      }

      st2=write(fn2,buffer2,cnt2);
      if(st2 != cnt2) goto Cleanup3;
      st3=write(fn3,buffer3,cnt3);
      if(st3 != cnt3) goto Cleanup3;

      printf("scnt = %d : sourcesize = %d\r",scnt,sourcesize);
      if(scnt == sourcesize) goto Cleanup3;
      if(rcnt == 0) { close(fn2); close(fn3); goto MORESETS; }
   }
Cleanup3:
   close(fn3); if(!st3) printf("Write failure of %s\n",string3);
Cleanup2:
   close(fn2); if(!st2) printf("Write failure of %s\n",string2);
Cleanup1:
   close(fn1); if(st1=0) printf("End of source file\n");
               if(st1<0) printf("Read failure of %s\n",string1);
TheEnd:        printf("\nThe End.\n");
}
