/* * file.c * $Id: file.c,v 1.4 2005/01/12 19:25:58 mah Exp $ * Christoph Berg * * 010814 */ #include #include #include "help.h" #include "dlx.h" #define LINE_LEN 1024 int load_file(char *fname) { FILE *file; char line[LINE_LEN]; int lineno = 0; int adr, d[4]; int nbytes, copybase, i; if ((file = fopen(fname, "rt")) == NULL) { perror(fname); exit(1); } lineno++; if(fgets(line, LINE_LEN, file) == NULL) { perror("fgets"); exit(1); } if(sscanf(line, "start:%x %x %x %x %x %x\n", &(state.start), &(state.maxaddr) , &(state.text), &(state.textsize), &(state.data), &(state.datasize)) != 6) { die("Error while parsing %s line %u: \n%s", fname, lineno, line); } adr = -1; // requires address field in object file while(fgets(line, LINE_LEN, file) != NULL) { lineno++; //printf("line %d: %s", lineno, line); if((nbytes = sscanf(line, "%x:%02x%02x%02x%02x\n", &adr, d+3, d+2, d+1, d) - 1) >= 1) { // -1 because of address field //printf("read %x : %x %x %x %x\n", adr, d[3], d[2], d[1], d[0]); if(adr < 0 || adr >= state.memorysize) die("%s line %u: no memory allocated at address %x", fname, lineno, adr); copybase = 4-nbytes; // copy the *upper* entries of d for(i = copybase; i < 4; i++) state.memory[adr-copybase+i] = d[i]; adr += nbytes; // increase address } else if((nbytes = sscanf(line, ":%02x%02x%02x%02x\n", d+3, d+2, d+1, d)) >= 1) { //printf("read (%x): %x %x %x %x\n", adr, d[3], d[2], d[1], d[0]); if(adr < 0 || adr >= state.memorysize) die("%s line %u: no memory allocated at address %x", fname, lineno, adr); copybase = 4-nbytes; // copy the *upper* entries of d for(i = copybase; i < 4; i++) state.memory[adr-copybase+i] = d[i]; adr += nbytes; // increase address } else { die("Error while parsing %s line %u: \n%s", fname, lineno, line); } } fclose(file); state.dpc = state.start; state.pc = state.dpc + 4; state.stack = state.memorysize; state.gpr[STACK_R] = state.stack; printf("loaded %s (%d lines)\n", fname, lineno); printf("start: 0x%x, maximum address: 0x%x\n", state.start, state.maxaddr); printf("text: 0x%x, 0x%x bytes\n", state.text, state.textsize); printf("data: 0x%x, 0x%x bytes\n", state.data, state.datasize); printf("stack allocated at 0x%x\n", state.stack); return 0; }