home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xtrsutl.zip / CVTCMD.C next >
C/C++ Source or Header  |  1992-12-31  |  5KB  |  194 lines

  1. /* 
  2.     8/4/92
  3.     
  4.     This program will convert a TRS-80 Disk BASIC program to a SYSTEM loadable 
  5.     format to be used with xtrs1.0
  6.  
  7.     By Abel M. Hernandez  amh@hp835.mitek.com
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <dos.h>
  13. #include <io.h>
  14. #include <fcntl.h>
  15. #include <sys\types.h>
  16. #include <sys\stat.h>
  17.  
  18. int fptr, optr;
  19. unsigned char buffer[256];
  20. char filename[20];      /* source file */
  21. char mod_file[20];      /* destination file */
  22. char cas_fname[10];     /* storage for cassette filename */
  23.  
  24. unsigned char blk_size, lsb, msb, blk_type;
  25. int addr;
  26.  
  27. main(argc, argv)
  28. int argc;
  29. char *argv[];
  30.     {
  31.     int i;
  32.  
  33.     if (argc < 2)
  34.         usage();
  35.     else
  36.         strcpy (filename,argv[1]);
  37.  
  38.     mkupper(filename);
  39.     
  40.     strcpy (cas_fname,"        ");  /* setup cassette filename    */
  41.     strncpy (cas_fname,filename,6);
  42.     
  43.     strcpy (mod_file,filename);     /* setup destination filename */
  44.     strcat (mod_file,".BIN");
  45.             
  46.     strcat (filename,".CMD");   /* assume CMD extension  */
  47.     
  48.     fptr = open(filename, O_RDONLY+O_BINARY);   /* open for read binary */
  49.  
  50.     if ( fptr == -1 )
  51.         {
  52.         printf("\nError! Opening %s \n",filename);
  53.         usage();
  54.         }
  55.  
  56.     optr = creat(mod_file, S_IWRITE);   /* open for write */
  57.     
  58.     if ( optr == -1 )
  59.         {
  60.         printf("\nError! Opening %s\n",mod_file);
  61.         exit(1);
  62.         }
  63.     setmode (optr,O_BINARY);    /* set mode for binary */
  64.  
  65.     for (i=0; i<256; i++)
  66.         buffer[i] = (unsigned int)0;
  67.         
  68.     write(optr,buffer,255);     /* 256 bytes of 0 for leader*/
  69.     write(optr,"\245",1);       /* sync byte 0xa5           */
  70.     write(optr,"\125",1);       /* 0x55 filename header     */
  71.     write(optr,cas_fname,6);    /* 6 char filename          */
  72.     
  73.     printf("\nConverting");
  74.  
  75. loop:
  76.     read(fptr,buffer,1);        /* get block type */
  77.     blk_type=buffer[0];
  78.  
  79.     if (blk_type == 0x01)       /* block type = data? */
  80.         {
  81.         get_blk();              /* get a block of data */
  82.         goto loop;
  83.         }
  84.     else if (blk_type != 0x02)  /* block type = entry point? */
  85.         {
  86.         printf("\nText Block: ");
  87.         read(fptr,buffer,1);        /* get block size */
  88.         blk_size = buffer[0];
  89.         for (i=0; i<blk_size; i++)
  90.             {
  91.             read(fptr,buffer,1);
  92.             printf("%c",buffer[0]);
  93.             }
  94.         printf("\n");
  95.         goto loop;
  96.         }
  97.  
  98.     blk_type = 0x78;
  99.     write(optr,&blk_type,1);    /* write block type */
  100.     
  101.     read(fptr,buffer,1);        /* read pass the block size */
  102.  
  103.     read(fptr,buffer,1);        /* get entry point address */
  104.     lsb  = buffer[0];
  105.     write(optr,&lsb,1);         /* write lsb of destination address */
  106.     
  107.     read(fptr,buffer,1);
  108.     msb  = buffer[0];
  109.     write(optr,&msb,1);         /* write msb of destination address */
  110.  
  111.     
  112.     addr = msb*256+lsb;
  113.     
  114.     printf("\nEntry point=0x%04X\n",addr);
  115.  
  116.     close(fptr);
  117.     close(optr);
  118.  
  119.     }
  120.  
  121. /****************************************************************************/
  122.  
  123. get_blk()
  124.     {
  125.     int bytes_rd, i;
  126.     unsigned char chksum, block_sz;
  127.  
  128.     printf(".");
  129.     blk_type = 0x3c;
  130.     write(optr,&blk_type,1);    /* write block type */
  131.     
  132.     read(fptr,buffer,1);        /* get block size */
  133.     blk_size=buffer[0];
  134.  
  135.     block_sz = blk_size -2;
  136.     write(optr,&block_sz,1);    /* write block size */
  137.     blk_size -= 3;
  138.     
  139.     read(fptr,buffer,1);        /* get destination address */
  140.     lsb  = buffer[0];
  141.     chksum = lsb;
  142.  
  143.     write(optr,&lsb,1);         /* write lsb of destination address */
  144.     
  145.     read(fptr,buffer,1);
  146.     msb  = buffer[0];
  147.     chksum += msb;
  148.     
  149.     write(optr,&msb,1);         /* write msb of destination address */
  150.     
  151.     addr = msb*256+lsb;
  152.  
  153.     bytes_rd = read(fptr,buffer,blk_size+1);        /* read in blk_size of data */
  154.  
  155.     bytes_rd = write(optr,buffer,bytes_rd);         /* write data block */
  156.  
  157.     for (i=0; i<bytes_rd; i++)      /* calculate the checksum */
  158.         chksum += buffer[i];
  159.         
  160.     write(optr,&chksum,1);      /* write checksum */
  161.         
  162.     }
  163.  
  164. /****************************************************************************/
  165.  
  166. mkupper(string)
  167. char *string;
  168.     {
  169.     int i;
  170.  
  171.     i = strlen(string);
  172.     while(i--)
  173.         {
  174.         *string = toupper(*string);
  175.         string++;
  176.         }
  177.     }
  178.  
  179. /****************************************************************************/
  180.  
  181. usage()
  182.     {
  183.     printf("\nTRS-80 Disk to Cassette program convertor\n");
  184.     printf("\nUsage: cvtcmd <filename>\n");
  185.     printf("\nWhere <fileaname> is a TRS-80 Disk object program without\n");
  186.     printf("      an extension. The extension CMD is assumed.\n");
  187.     printf("\nThis program will convert a TRS-80 disk object (machine\n");
  188.     printf("language) program to a cassette SYSTEM loadable format.\n");
  189.     printf("\nby Abel M. Hernandez 8/4/92 amh@hp835.mitek.com.\n");
  190.     exit(1);
  191.     }
  192.  
  193.  
  194.