home *** CD-ROM | disk | FTP | other *** search
/ Falcon 030 Power 2 / F030_POWER2.iso / ST_STE / MAGS / TOXIC_14.ARJ / toxic_14 / TOXIC_14.D_2 / LEONARD / LEO_ST.ZIP / ST_PC / SYSTEM / DOPRG.C next >
C/C++ Source or Header  |  1996-04-02  |  4KB  |  238 lines

  1.  
  2. /********************************************************
  3.  *                            *
  4.  *                            *
  5.  *    Do-Prg: Creation d'un PRG atari a partir d'un    *
  6.  *    BIN genere par le PC.                *
  7.  *                            *
  8.  *    (C)oderight OXYGENE 1995 by léo.        *
  9.  *                            *
  10.  *                            *
  11.  ********************************************************/
  12.  
  13.  
  14. // includes.
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <ctype.h>
  19.  
  20. typedef    unsigned long    UD;
  21. typedef    long        SD;
  22. typedef unsigned int    UW;
  23. typedef int        SW;
  24. typedef unsigned char    UB;
  25. typedef char        SB;
  26.  
  27.  
  28. #define    BLOCK_SIZE    1024
  29.  
  30.  
  31. char    mydrive[_MAX_DRIVE];
  32. char    mydir[_MAX_DIR];
  33. char    mypath[_MAX_PATH];
  34.  
  35. /********************************************************************
  36.             SECTION CODE
  37.  ********************************************************************/
  38.  
  39.  
  40.  
  41. UD    file_size(FILE *handle)
  42.  {
  43.  UD    size,pos;
  44.  
  45.     pos=ftell(handle);
  46.     fseek(handle,0,SEEK_END);
  47.     size=ftell(handle);
  48.     fseek(handle,pos,SEEK_SET);
  49.     return size;
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57. void    do_prg(char *name,char *orgstr,int argc)
  58.  {
  59.  UD    org;
  60.  SW    i;
  61.  UW    s;
  62.  UD    digit;
  63.  FILE    *runner,*in,*out;
  64.  UD    run_size,bin_size;
  65.  UD    a;
  66.  UB    *buffer;
  67.  char    *ptr;
  68.  char    name2[128];
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  //----------------------------------------------------------
  76.  // Get le ORG.
  77.  //----------------------------------------------------------
  78.     if (argc==2)
  79.       org=0x5000;
  80.     else {
  81.       orgstr=strupr(orgstr);
  82.       org=0;
  83.       s=0;
  84.       for (i=strlen(orgstr)-1;i>=0;i--) {
  85.  
  86.         digit=orgstr[i];
  87.         if ((digit>='0') && (digit<='9')) {
  88.           digit-='0';
  89.         } else {
  90.           if ((digit>='A') && (digit<='F')) {
  91.         digit-='A';
  92.         digit+=10;
  93.           } else {
  94.         printf("Erreur dans format hexa de ORG.\n");
  95.         exit(1);
  96.           }
  97.         }
  98.         org+=digit<<s;
  99.         s+=4;
  100.         if (org>0x7fffff) {
  101.           printf("ORG impossible. ($%X)\n",org);
  102.           exit(1);
  103.         }
  104.       }
  105.     }
  106.  
  107.  
  108.  
  109.  
  110.  //------------------------------------------------------------
  111.  // Ouverture du BIN
  112.  //------------------------------------------------------------
  113.     in=fopen(name,"rb");
  114.     if (!in) {
  115.       printf("ERROR: Unable to open %s\n",name);
  116.       exit(1);
  117.     }
  118.     bin_size=file_size(in);
  119.     bin_size+=1;
  120.     bin_size&=-2;
  121.  
  122.  //------------------------------------------------------------
  123.  // Ouverture du PRG
  124.  //------------------------------------------------------------
  125.     strcpy(name2,name);
  126.     ptr = strrchr(name2,'.');
  127.     if (ptr==0)
  128.       strcat(name2,".PRG");
  129.     else
  130.       strcpy(ptr,".PRG");
  131.     out=fopen(name2,"wb");
  132.     if (!out) {
  133.       printf("ERROR: Unable to create %s\n",name2);
  134.       exit(1);
  135.     }
  136.  
  137.     printf("Creating %s (ST executable)\n",name2);
  138.     printf("ORG assumed:$%08X\n",org);
  139.  
  140.  //------------------------------------------------------------
  141.  //------------------------------------------------------------
  142.  
  143.     _makepath(mypath,mydrive,mydir,"DOPRG",".DAT");
  144.     runner=fopen(mypath,"rb");
  145.     if (!runner) {
  146.       printf("ERROR: Unable to open \"%s\"\n",mypath);
  147.       exit(1);
  148.     }
  149.  
  150.  
  151.     run_size=file_size(runner);
  152.  
  153.     buffer=(UB *)malloc(run_size);
  154.     if (!buffer) {
  155.       printf("ERROR: MALLOC error for %d bytes.\n",run_size);
  156.       exit(1);
  157.     }
  158.     fread(buffer,1,run_size,runner);
  159.     fclose(runner);
  160.  
  161.  
  162.     // Poke la taille section text.
  163.     a=(run_size+bin_size-0x1c);
  164.  
  165.     buffer[2]=(a>>24)&255;
  166.     buffer[3]=(a>>16)&255;
  167.     buffer[4]=(a>>8)&255;
  168.     buffer[5]=(a)&255;
  169.  
  170.     // ecrit entete du PRG.
  171.     fwrite(buffer,1,run_size-8,out);
  172.  
  173.  
  174.     // Ecrit taille du BIN.
  175.     fputc((bin_size>>24)&255,out);
  176.     fputc((bin_size>>16)&255,out);
  177.     fputc((bin_size>>8)&255,out);
  178.     fputc((bin_size)&255,out);
  179.  
  180.     // ecrit ORG
  181.     fputc((org>>24)&255,out);
  182.     fputc((org>>16)&255,out);
  183.     fputc((org>>8)&255,out);
  184.     fputc((org)&255,out);
  185.  
  186.  
  187.     while (bin_size--) {
  188.       fputc(fgetc(in),out);
  189.     }
  190.  
  191.     fputc(0,out);
  192.     fputc(0,out);
  193.     fputc(0,out);
  194.     fputc(0,out);
  195.  
  196.     fclose(out);
  197.     free(buffer);
  198.  
  199.  
  200.  
  201.  }
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209. void    main(int argc,char *argv[])
  210.  {
  211.  
  212.  
  213.  
  214.  
  215.     printf(    "DOPRG.\n"
  216.         "Make an Atari executable file.\n"
  217.         "(C)OXYGENE 1995 by Leonard.\n\n");
  218.  
  219.  
  220.     if ((argc<2) || (argc>3)) {
  221.       printf("USAGE:\n\n"
  222.            "   DOPRG <binary name> [ORG (hex)]\n"
  223.          "   (Ex: DOPRG mandel.bin 2A00)\n");
  224.  
  225.       exit(0);
  226.     }
  227.  
  228.     _splitpath(argv[0],mydrive,mydir,NULL,NULL);
  229.  
  230.     do_prg(argv[1],argv[2],argc);
  231.  
  232.  
  233.  }
  234.  
  235.  
  236.  
  237.  
  238.