Mercurial > hg > Members > kono > os9 > sbc09
view os9/makerom.c @ 0:9a224bd9b45f
os9 emulation
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 02 Jul 2018 02:12:31 +0900 |
parents | |
children | 3c736a81b886 |
line wrap: on
line source
/* makerom.c build ROM image file os9.rom from module list */ #include <stdio.h> #include <stdlib.h> #include <strings.h> #include <sys/stat.h> #define IOBASE 0xe000 #define IOSIZE 0x800 typedef struct os9module { int size; int location; int ioflag; unsigned char *mod; char *name; struct os9module *next; } *MPTR ; struct os9module * readOS9module(char *filename) { FILE *fp = fopen(filename,"rb"); if (fp==0) { fprintf(stderr,"cannot read %s\n",filename); exit(1); } struct stat st; fstat(fileno(fp),&st); int size = st.st_size; struct os9module *m = malloc(size + sizeof(struct os9module)); m->size = size; m->next = 0; m->ioflag = 0; m->mod = (unsigned char*)m + sizeof(struct os9module); fread(m->mod , size, 1, fp); m->name = (char*) (m->mod + (m->mod[4]*256 + m->mod[5]) ); fclose(fp); return m; } void fputword(unsigned short x, FILE *fp) { fputc((x>>8)&0xff,fp); fputc(x&0xff,fp); } void printOs9Str(char *p) { while((*p & 0x80)==0) { putchar(*p); p++; } putchar(*p & 0x7f); } int findLocation(MPTR m, int loc) { if (m==0) return loc; int top = findLocation(m->next, loc) - m->size; if (m->next==0) top = 0xf800; // OS9p1 if (!(( top+m->size < IOBASE ) || ( IOBASE+IOSIZE < top)) ) { top = IOBASE-m->size-1; m->ioflag = 1; // printf("*"); } m->location = top; // printf("mod "); // printOs9Str(m->name); // printf(" \t: 0x%x - 0x%x\n",top, top + m->size); return top; } int main(int ac, char *av[]) { int vectable = 0; struct os9module *m = 0, root ; root.size = 0; root.mod = 0; m = &root; for(int i = 2 ; i<ac ; i++ ) { struct os9module *cur; cur = readOS9module(av[i]); m->next = cur; m = cur; } FILE *romfile; unsigned pos; romfile=fopen(av[1],"wb"); if(!romfile) { fprintf(stderr,"Cannot create file %s\n",av[1]); exit(1); } int start = findLocation(root.next,0); start = start&0xf800; printf("\n\n"); pos = start; for(struct os9module *cur = root.next; cur ; cur = cur->next ) { if ( cur->size && (cur->name[0]=='O' && cur->name[1]=='S' && cur->name[2]== -71)) { for(; pos < 0xf800 ; pos++) { fputc(0xff,romfile); } } printf("mod "); printOs9Str(cur->name); fwrite(cur->mod, cur->size, 1, romfile); printf(" \t: 0x%x - 0x%x\n",pos, pos + cur->size); // printf(" \t: 0x%x - 0x%x : 0x%lx \n",pos, pos + cur->size, ftell(romfile)+start); pos = pos+cur->size; if (cur->ioflag) { for(; pos < IOBASE + IOSIZE; pos++) { fputc(0xff,romfile); } printf("*"); } } printf("os9 end %x\n",pos); vectable = 0x10000 - 2*7; for( ; pos<vectable; pos++) fputc(0xff,romfile); printf("vectbl %x\n",pos); fputword(0xF82d,romfile); fputword(0xF831,romfile); fputword(0xF835,romfile); fputword(0xF839,romfile); fputword(0xF83d,romfile); fputword(0xF841,romfile); fputword(0xF876,romfile); printf("boot rom from 0x%lx\n",0x10000-ftell(romfile)); fclose(romfile); return 0; }