1 | // a replacement for stobin...
|
---|
2 | // taken from a special version of TEO, the thomson TO8 emulator...
|
---|
3 | #include <stdio.h>
|
---|
4 | #include <stdlib.h>
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Loader au format S19 de Motorola
|
---|
8 | */
|
---|
9 | unsigned char fill=0x00;
|
---|
10 | enum {vectrex,to8} bin_mode;
|
---|
11 |
|
---|
12 | static int
|
---|
13 | LoadS19File(char *S19Fname,char *binFname)
|
---|
14 | {
|
---|
15 | unsigned char mem[0x10000];
|
---|
16 | int low=0xFFFF;
|
---|
17 | int high=0x0000;
|
---|
18 | int i;
|
---|
19 | FILE *f;
|
---|
20 | char c;
|
---|
21 |
|
---|
22 | // clears mem
|
---|
23 | for (i=0;i<0x10000;i++) mem[i]=fill;
|
---|
24 |
|
---|
25 | f=fopen(S19Fname,"r");
|
---|
26 |
|
---|
27 | if (f==NULL) return -1;
|
---|
28 |
|
---|
29 | while (!feof(f))
|
---|
30 | {
|
---|
31 | while (!feof(f) && ((c=fgetc(f))!='S'));
|
---|
32 |
|
---|
33 | if (!feof(f))
|
---|
34 | {
|
---|
35 | int adr;
|
---|
36 | int lg;
|
---|
37 | int i;
|
---|
38 | int val;
|
---|
39 |
|
---|
40 | c=fgetc(f);
|
---|
41 |
|
---|
42 | if (c=='1')
|
---|
43 | {
|
---|
44 | fscanf(f,"%2x%4x",&lg,&adr);
|
---|
45 | for (i=0;i<lg-3;i++)
|
---|
46 | {
|
---|
47 | fscanf(f,"%2x",&val);
|
---|
48 | mem[adr+i]=val;
|
---|
49 | if (adr+i>high) high=adr+i;
|
---|
50 | if (adr+i<low) low=adr+i;
|
---|
51 | }
|
---|
52 | // reads checksum
|
---|
53 | fscanf(f,"%2x",&val);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 | fclose(f);
|
---|
58 | // we now have bin in mem from low to high... that is basiicaly what an stobin is supposed to do...
|
---|
59 | f=fopen(binFname,"wb");
|
---|
60 |
|
---|
61 | switch (bin_mode) {
|
---|
62 | case vectrex :
|
---|
63 | for (i=low;i<=high;i++)
|
---|
64 | fputc(mem[i],f);
|
---|
65 | break;
|
---|
66 | case to8 :
|
---|
67 | fputc(0x00,f);
|
---|
68 | // size 16bit big endian
|
---|
69 | fputc((((high-low+1)&0xFF00)>>8),f);
|
---|
70 | fputc(((high-low+1)&0xFF),f);
|
---|
71 | // begin address
|
---|
72 | fputc(((low&0xFF00)>>8),f);
|
---|
73 | fputc((low&0xFF),f);
|
---|
74 | // data
|
---|
75 | for (i=low;i<=high;i++)
|
---|
76 | fputc(mem[i],f);
|
---|
77 | fputc(0xFF,f);
|
---|
78 | fputc(0x00,f);
|
---|
79 | fputc(0x00,f);
|
---|
80 | // run address (ie begin address here)
|
---|
81 | fputc(((low&0xFF00)>>8),f);
|
---|
82 | fputc((low&0xFF),f);
|
---|
83 | break;
|
---|
84 | }
|
---|
85 |
|
---|
86 | fclose(f);
|
---|
87 |
|
---|
88 | }
|
---|
89 |
|
---|
90 | int
|
---|
91 | main(int argc, char *argv[]) {
|
---|
92 | char *file_in=NULL;
|
---|
93 | char *file_out=NULL;
|
---|
94 |
|
---|
95 | int start=-1;
|
---|
96 | int end=-1;
|
---|
97 | int exec=-1;
|
---|
98 |
|
---|
99 | int argnum;
|
---|
100 | bin_mode=vectrex;
|
---|
101 |
|
---|
102 | for (argnum=1;argnum<argc;argnum++) {
|
---|
103 | if (strncmp(argv[argnum],"-o",2)==0) file_out=&argv[argnum][2];
|
---|
104 | if (strncmp(argv[argnum],"-i",2)==0) file_in=&argv[argnum][2];
|
---|
105 | if (strcmp(argv[argnum],"-to8")==0) bin_mode=to8;
|
---|
106 | }
|
---|
107 |
|
---|
108 | if (file_in==NULL) { fprintf(stderr,"No input file");exit(-1);};
|
---|
109 | if (file_out==NULL) { fprintf(stderr,"No output file");exit(-1);};
|
---|
110 |
|
---|
111 | LoadS19File(file_in,file_out);
|
---|
112 |
|
---|
113 | }
|
---|