home *** CD-ROM | disk | FTP | other *** search
- /* relocate.c
- ==========================================================================
- Builds a page relocatable executable out of two 6502 executables
- ==========================================================================
- Usage: 1) Assemble 1st with * = $0100
- 2) Assemble a second time with * = $0200
- 3) Run relocate 1st 2nd
-
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
-
- #define DONE -1
-
- int c1, c2;
-
- int fix[3000];
- int NumFix=0;
-
-
- FILE * OpenFile(char * name, char * mode)
- {
- FILE * result = fopen(name,mode);
-
- if (result == 0) {
- printf("Can't open \"%s\"\n",name);
- exit(1);
- }
- else {
- return result;
- }
- }
-
- int GetC(FILE * f1, FILE * f2)
- {
- if (feof(f1) || feof(f2) || ferror(f1) || ferror(f2)) {
- return DONE;
- }
- else {
- c1 = fgetc(f1);
- c2 = fgetc(f2);
-
- return c2-c1;
- }
- }
-
-
- int main(int argc, char ** argv)
- {
- int i;
-
- if (argc < 4) {
- fprintf(stderr,"Usage: %s file1 file2 outfile\n",argv[0]);
- exit(1);
- }
-
- FILE * fp1 = OpenFile(argv[1],"rb");
- FILE * fp2 = OpenFile(argv[2],"rb");
- FILE * out = OpenFile(argv[3],"wb");
-
- int delta;
-
- word oldpos = 0;
- word pos = 0;
- NumFix = 0;
-
- // Skip past load address and startup sys
-
- for(i=1; i<3; i++)
- GetC(fp1,fp2);
-
- // Startup sys is before _Corg_
-
- for(i=3; i<6; i++) {
- GetC(fp1,fp2);
- fputc(c1,out);
- }
-
- while(1) {
-
- delta = GetC(fp1,fp2);
-
- if (delta == DONE)
- break;
-
- if (delta) {
- fix[NumFix] = pos-oldpos;
- NumFix++;
- oldpos = pos;
- }
-
- pos++;
- fputc(c1,out);
-
- }
-
- putw(NumFix,out);
-
- for (i=0; i<NumFix; i++) {
-
- assert(fix[i] != 0); // Must not happen
-
- do {
- if (fix[i] < 255) {
- fputc(fix[i],out);
- fix[i] = 0;
- }
- else {
- fputc(255,out);
- fix[i] -= 255;
- }
- } while(fix[i]);
- }
-
- fclose(fp1);
- fclose(fp2);
- fclose(out);
-
- printf("%s - %u bytes, %u fixups\n",argv[3],pos,NumFix);
-
- exit(0);
- }
-
-
-
-
-
-
-
-
-
-
-