int main(int argc, char *argv[]){ if (argc != 3){ printf("Usage: %s <cycles> <filename>\n", argv[0]); exit(EXIT_FAILURE); } int cycles; xcpu *c = malloc(sizeof(xcpu)); init_cpu(c); FILE *fd=load_file(argv[2]); load_programme(c, fd); // loads the bytes of fd into c->memory cycles = atoi(argv[1]); // the IHandler type is defined in xcpu.h, and the IHandler jump table // is implemented in xcpu.c IHandler *table = build_jump_table(); // It is a jump table to the functions handling each instruction. char *graceful = "CPU has halted."; char *out_of_time = "CPU ran out of time."; int halted, i=0; while (i++ < cycles || !cycles){ if (MOREDEBUG) fprintf(LOG, "<CYCLE %d>\n",i-1); if (halted = !xcpu_execute(c, table)) break; } // on halt, halted gets 0; otherwise halted remains 1 char *exit_msg = (halted)? graceful : out_of_time; fprintf(stdout, "%s\n", exit_msg); fprintf(LOG, "(%d cycles completed.)\n", i-1); destroy_jump_table(table); shutdown(c); return !halted; }
static void * execution_loop(void * cpu){ // expects pointer to an xcpu struct xcpu *c = (xcpu *) cpu; char graceful[40]; char out_of_time[40]; sprintf(graceful, "CPU %d has halted.", c->id); sprintf(out_of_time, "CPU %d out of time.", c->id); int halted, i=0; fprintf(LOG,"c->pc: %d %4.4x\n",c->pc,FETCH_WORD(c->pc)); while (i++ < cycles || !cycles){ if (MOREDEBUG) fprintf(LOG, "<CYCLE %d> <CPU %d>\n",i-1,c->id); if (i != 0 && interrupt_freq != 0 && i % interrupt_freq == 0) if (!xcpu_exception(c, X_E_INTR)){ fprintf(stderr, "Exception error at 0x%4.4x. CPU has halted.\n", c->pc); return NULL; //pthread_exit(NULL); //exit(EXIT_FAILURE); } if ((halted = !xcpu_execute(c, table))) break; if (MOREDEBUG) xcpu_pretty_print(c); } // on halt, halted gets 0; otherwise halted remains 1 char *exit_msg = (halted)? graceful : out_of_time; fprintf(stdout, "%s\n", exit_msg); fprintf(LOG, "(%d cycles completed.)\n", i-1); // pthread_exit(NULL); return NULL; //((void *) c); }
static void * run_core( void *arg ) { xcpu *cs = (xcpu *) arg; unsigned int i; for( i = 0; ( ticks < 1 ) || ( i < ticks ); i++ ) { if( i && ( quantum > 0 ) && !( i % quantum ) ) { if( !xcpu_exception( cs, X_E_INTR ) ) { fprintf( stderr, "Exception error, CPU %d has halted.\n", cs->id ); return 0; } } if( !xcpu_execute( cs ) ) { fprintf( stderr, "CPU %d has halted.\n", cs->id ); return 0; } } fprintf( stderr, "CPU %d ran out of time.\n", cs->id ); return NULL; }
int main( int argc, char **argv ) { FILE *fp; struct stat fs; xcpu cs; unsigned char *mem; int ticks; int interrupt; unsigned int i; unsigned int j; if( ( argc < 4 ) || ( sscanf( argv[TICK_ARG], "%d", &ticks ) != 1 ) || ( ticks < 0 )|| (sscanf(argv[INTERRUP_ARG], "%d", &interrupt ) != 1) ) { printf( "usage: xsim <ticks> <obj file>\n" ); printf( " <ticks> is number instructions to execute (0 = forever)\n" ); printf( " <image file> xis object file created by or xasxld\n" ); return 1; } mem = (unsigned char *)malloc( XIS_MEM_SIZE ); if( !mem ) { printf( "error: memory allocation (%d) failed\n", XIS_MEM_SIZE ); exit( 1 ); } memset( mem, I_BAD, XIS_MEM_SIZE ); if( stat( argv[IMAGE_ARG], &fs ) ) { printf( "error: could not stat image file %s\n", argv[IMAGE_ARG] ); return 1; } else if( fs.st_size > XIS_MEM_SIZE ) { printf( "Not enough memory to run all the programs." ); return 1; } fp = fopen( argv[IMAGE_ARG], "rb" ); if( !fp ) { printf( "error: could not open image file %s\n", argv[IMAGE_ARG] ); return 1; } else if( fread( mem, 1, fs.st_size, fp ) != fs.st_size ) { printf( "error: could not read file %s\n", argv[IMAGE_ARG] ); return 1; } fclose( fp ); memset( &cs, 0, sizeof( xcpu ) ); cs.memory = mem; for( i = 0; ( ticks < 1 ) || ( i < ticks ); i++ ) { if( (interrupt != 0)&& (i % interrupt == 0 ) && ( i != 0)){ // fprintf(stdout,"round times : %d\n", i); if( !(xcpu_exception(&cs, X_E_INTR) ) ){ fprintf(stdout,"Exception error, CPU has halted.\n"); return 0; } } if( !xcpu_execute( &cs ) ) { printf( "CPU has halted.\n" ); return 0; } } printf( "CPU ran out of time.\n" ); return 0; }