/* * Giovanni Agosta, Andrea Di Biagio * Politecnico di Milano, 2007 * * main.c * Formal Languages & Compilers Machine, 2007/2008 * */ #include #include #include #include "fetch.h" #include "machine.h" #include "decode.h" int main(int argc, char **argv) { FILE *fp; /* pointer to the object file */ int len=0; /* length of the object file */ unsigned int *code=NULL; /* pointer to the code block */ int lcode = 0; /* length of the code block */ int mmgmt=BASIC; /* memory management */ int i; int breakat=-1; /* break execution at instruction # */ int count=0; /* iterations counter */ pc=0; /* PC register is set ot zero in the beginning */ #ifdef DEBUG decoded_instr *current_instr; #endif /* Apertura del file oggetto */ if (argc<2) { /* L'esecuzione termina poichè non è stato specificato il file * oggetto come argomento */ fprintf(stdout, "Formal Languages & Compilers Machine, 2007/2008.\n" "\n\nSyntax:\n\tmace [options] objectfile\n"); return NOARGS; } /* apre il file oggetto specificato come argomento */ fp = fopen(argv[argc-1],"r"); if (fp == NULL) { fprintf(stderr,"Object file %s doesn't exist.\n", argv[argc-1]); /* termina l'esecuzione in quanto è stato specificato un file * oggetto inesistente */ return NOFILE; } for(i=1; i %d.\n",*error,*error,argv[i+1],breakat); #endif return WRONG_ARGS; } i++; /* salta l'argomento gia' letto */ } } #ifdef DEBUG fprintf(stderr,"Running with %s memory \n", (mmgmt == BASIC)? "BASIC" : "SEGMENTED"); #endif /* Resetta i registri e la memoria */ for (i=0; iMEMSIZE) { fprintf(stderr,"Out of memory.\n"); return MEM_FAULT; } code = (unsigned int*)mem; } else { /* Alloca spazio in memoria per il codice */ code = (unsigned int*) malloc(sizeof(unsigned int)*len); } /* Carica il codice macchina in memoria */ for (i=0; i=0; ) { pc = fetch_execute(code,pc); #ifdef DEBUG print_regs(stderr); print_psw(stderr); print_Memory_Dump(stderr, lcode); #endif reg[0]=0; /* reset R0 to 0; R0 is wired to 0, so we ignore all writes */ count++; /* conta le istruzioni eseguite */ if ((breakat>0) && (breakat<=count)) { #ifdef DEBUG fprintf(stderr,"Break after %d instructions.\n",count); #endif return BREAK; } /* Check the HALT condition */ if (pc == _HALT) return OK; #ifdef DEBUG current_instr = decode(code[pc]); fprintf(stderr,"\n\n"); print(stderr, current_instr); free(current_instr); #endif } #ifdef DEBUG fprintf(stderr,"Memory access error.\n"); #endif return pc; }