0
|
1 /* makerom.c
|
|
2 build ROM image file os9.rom from module list
|
|
3 */
|
|
4
|
|
5 #include <stdio.h>
|
|
6 #include <stdlib.h>
|
|
7 #include <strings.h>
|
|
8 #include <sys/stat.h>
|
|
9
|
6
|
10 int IOBASE = 0xe000;
|
|
11 int IOSIZE = 0x100;
|
|
12 char * outfile ;
|
0
|
13
|
|
14 typedef struct os9module {
|
|
15 int size;
|
|
16 int location;
|
|
17 int ioflag;
|
|
18 unsigned char *mod;
|
|
19 char *name;
|
|
20 struct os9module *next;
|
|
21 } *MPTR ;
|
|
22
|
|
23 struct os9module *
|
|
24 readOS9module(char *filename)
|
|
25 {
|
|
26 FILE *fp = fopen(filename,"rb");
|
|
27 if (fp==0) {
|
|
28 fprintf(stderr,"cannot read %s\n",filename);
|
|
29 exit(1);
|
|
30 }
|
|
31 struct stat st;
|
|
32 fstat(fileno(fp),&st);
|
|
33 int size = st.st_size;
|
|
34 struct os9module *m = malloc(size + sizeof(struct os9module));
|
|
35 m->size = size;
|
|
36 m->next = 0;
|
|
37 m->ioflag = 0;
|
|
38 m->mod = (unsigned char*)m + sizeof(struct os9module);
|
|
39 fread(m->mod , size, 1, fp);
|
|
40 m->name = (char*) (m->mod + (m->mod[4]*256 + m->mod[5]) );
|
|
41 fclose(fp);
|
|
42 return m;
|
|
43 }
|
|
44
|
|
45 void
|
|
46 fputword(unsigned short x, FILE *fp)
|
|
47 {
|
|
48 fputc((x>>8)&0xff,fp);
|
|
49 fputc(x&0xff,fp);
|
|
50 }
|
|
51
|
|
52 void printOs9Str(char *p)
|
|
53 {
|
|
54 while((*p & 0x80)==0) {
|
|
55 putchar(*p);
|
|
56 p++;
|
|
57 }
|
|
58 putchar(*p & 0x7f);
|
|
59 }
|
|
60
|
1
|
61 // calcurate position from the botton
|
|
62 // avoid v09 IO map on 0xe000-0xe800
|
|
63 // os9p1 have to be last and at 0xf800
|
0
|
64 int findLocation(MPTR m, int loc) {
|
|
65 if (m==0) return loc;
|
|
66 int top = findLocation(m->next, loc) - m->size;
|
|
67 if (m->next==0) top = 0xf800; // OS9p1
|
|
68 if (!(( top+m->size < IOBASE ) || ( IOBASE+IOSIZE < top)) ) {
|
|
69 top = IOBASE-m->size-1;
|
|
70 m->ioflag = 1;
|
|
71 // printf("*");
|
|
72 }
|
|
73 m->location = top;
|
|
74 // printf("mod ");
|
|
75 // printOs9Str(m->name);
|
|
76 // printf(" \t: 0x%x - 0x%x\n",top, top + m->size);
|
|
77 return top;
|
|
78 }
|
|
79
|
|
80 int
|
|
81 main(int ac, char *av[])
|
|
82 {
|
|
83 int vectable = 0;
|
|
84 struct os9module *m = 0, root ;
|
|
85 root.size = 0;
|
|
86 root.mod = 0;
|
|
87 m = &root;
|
|
88
|
6
|
89 for(int i = 1 ; i<ac ; i++ ) {
|
|
90 if (*av[i]=='-') {
|
|
91 if (av[i][1] =='i') {
|
|
92 IOBASE = strtol(av[i],(char**)0,0);
|
|
93 i += 1;
|
|
94 } else if (av[i][1] =='o') {
|
|
95 outfile = av[i+1];
|
|
96 i += 1;
|
|
97 } else {
|
|
98 return 1;
|
|
99 }
|
|
100 continue;
|
|
101 }
|
0
|
102 struct os9module *cur;
|
|
103 cur = readOS9module(av[i]);
|
|
104 m->next = cur;
|
|
105 m = cur;
|
|
106 }
|
|
107
|
|
108 FILE *romfile;
|
|
109 unsigned pos;
|
6
|
110 if (outfile==0) return 1;
|
0
|
111
|
6
|
112 romfile=fopen(outfile,"wb");
|
0
|
113 if(!romfile) {
|
|
114 fprintf(stderr,"Cannot create file %s\n",av[1]);
|
|
115 exit(1);
|
|
116 }
|
|
117
|
|
118
|
|
119 int start = findLocation(root.next,0);
|
|
120 start = start&0xf800;
|
|
121 printf("\n\n");
|
|
122
|
|
123 pos = start;
|
|
124 for(struct os9module *cur = root.next; cur ; cur = cur->next ) {
|
6
|
125 // last module have to os9p1
|
|
126 if ( cur->next == 0 ) { // cur->size && (cur->name[0]=='O' && cur->name[1]=='S' && cur->name[2]== -71)) {
|
1
|
127 for(; pos < 0xf800 ; pos++) { // os9p1 begins at 0xf800
|
0
|
128 fputc(0xff,romfile);
|
|
129 }
|
|
130 }
|
|
131 printf("mod ");
|
|
132 printOs9Str(cur->name);
|
|
133 fwrite(cur->mod, cur->size, 1, romfile);
|
|
134 printf(" \t: 0x%x - 0x%x\n",pos, pos + cur->size);
|
|
135 // printf(" \t: 0x%x - 0x%x : 0x%lx \n",pos, pos + cur->size, ftell(romfile)+start);
|
|
136 pos = pos+cur->size;
|
|
137 if (cur->ioflag) {
|
|
138 for(; pos < IOBASE + IOSIZE; pos++) {
|
|
139 fputc(0xff,romfile);
|
|
140 }
|
|
141 printf("*");
|
|
142 }
|
|
143 }
|
|
144 printf("os9 end %x\n",pos);
|
|
145 vectable = 0x10000 - 2*7;
|
|
146 for( ; pos<vectable; pos++) fputc(0xff,romfile);
|
|
147 printf("vectbl %x\n",pos);
|
|
148 fputword(0xF82d,romfile);
|
|
149 fputword(0xF831,romfile);
|
|
150 fputword(0xF835,romfile);
|
|
151 fputword(0xF839,romfile);
|
|
152 fputword(0xF83d,romfile);
|
|
153 fputword(0xF841,romfile);
|
|
154 fputword(0xF876,romfile);
|
|
155
|
|
156 printf("boot rom from 0x%lx\n",0x10000-ftell(romfile));
|
|
157 fclose(romfile);
|
|
158 return 0;
|
|
159 }
|