home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / enterprs / c128 / util / sfxsrc.arc / RELOCATE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-12  |  2.4 KB  |  135 lines

  1. /*  relocate.c
  2.     ==========================================================================
  3.     Builds a page relocatable executable out of two 6502 executables
  4.     ==========================================================================
  5.     Usage:  1) Assemble 1st with                * = $0100
  6.             2) Assemble a second time with      * = $0200
  7.             3) Run relocate 1st 2nd
  8.  
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <assert.h>
  14.  
  15. #define DONE -1
  16.  
  17. int c1, c2;
  18.  
  19. int fix[3000];
  20. int NumFix=0;
  21.  
  22.  
  23. FILE * OpenFile(char * name, char * mode)
  24. {
  25.     FILE * result = fopen(name,mode);
  26.  
  27.     if (result == 0) {
  28.         printf("Can't open \"%s\"\n",name);
  29.         exit(1);
  30.     }
  31.     else {
  32.         return result;
  33.     }
  34. }
  35.  
  36. int GetC(FILE * f1, FILE * f2)
  37. {
  38.     if (feof(f1) || feof(f2) || ferror(f1) || ferror(f2)) {
  39.         return DONE;
  40.     }
  41.     else {
  42.         c1 = fgetc(f1);
  43.         c2 = fgetc(f2);
  44.  
  45.         return c2-c1;
  46.     }
  47. }
  48.  
  49.  
  50. int main(int argc, char ** argv)
  51. {
  52.     int i;
  53.  
  54.     if (argc < 4) {
  55.         fprintf(stderr,"Usage:  %s file1 file2 outfile\n",argv[0]);
  56.         exit(1);
  57.     }
  58.  
  59.     FILE * fp1 = OpenFile(argv[1],"rb");
  60.     FILE * fp2 = OpenFile(argv[2],"rb");
  61.     FILE * out = OpenFile(argv[3],"wb");
  62.  
  63.     int  delta;
  64.  
  65.     word oldpos = 0;
  66.     word pos    = 0;
  67.          NumFix = 0;
  68.  
  69.     //  Skip past load address and startup sys
  70.  
  71.     for(i=1; i<3; i++)
  72.         GetC(fp1,fp2);
  73.  
  74.     //  Startup sys is before _Corg_
  75.  
  76.     for(i=3; i<6; i++) {
  77.         GetC(fp1,fp2);
  78.         fputc(c1,out);
  79.     }
  80.  
  81.     while(1) {
  82.  
  83.         delta = GetC(fp1,fp2);
  84.  
  85.         if (delta == DONE)
  86.             break;
  87.  
  88.         if (delta) {
  89.             fix[NumFix] = pos-oldpos;
  90.             NumFix++;
  91.             oldpos = pos;
  92.         }
  93.  
  94.         pos++;
  95.         fputc(c1,out);
  96.  
  97.     }
  98.  
  99.     putw(NumFix,out);
  100.  
  101.     for (i=0; i<NumFix; i++) {
  102.  
  103.         assert(fix[i] != 0);            // Must not happen
  104.  
  105.         do {
  106.             if (fix[i] < 255) {
  107.                 fputc(fix[i],out);
  108.                 fix[i] = 0;
  109.             }
  110.             else {
  111.                 fputc(255,out);
  112.                 fix[i] -= 255;
  113.             }
  114.         } while(fix[i]);
  115.     }
  116.  
  117.     fclose(fp1);
  118.     fclose(fp2);
  119.     fclose(out);
  120.  
  121.     printf("%s - %u bytes, %u fixups\n",argv[3],pos,NumFix);
  122.  
  123.     exit(0);
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.