Example #1
0
int main(int argc, char *argv[])
{
  
  InstInfo curInst;
  //InstInfo* instArray[4];
  //int top = 0;
  //  InstInfo *f = &curInst;
  int instnum = 0;
  int maxpc;
  FILE *program;
  int i;
  for (i =0 ; i < 32; i++) {
   regfile[i]=0;
  }
  
  if (argc != 2)
    {
      printf("Usage: sim filename\n");
      exit(0);
    }
  
  maxpc = load(argv[1]) - 1;
  InstInfo* n; n= malloc(sizeof(*n));
  InstInfo* f; f=malloc(sizeof(*f));
  InstInfo* d; d=malloc(sizeof(*d));
  InstInfo* x; x=malloc(sizeof(*x));
  InstInfo* m; m=malloc(sizeof(*m));
  InstInfo* w; w=malloc(sizeof(*w));
  
  fetch(f);
  decode(f);
  i = 0;
  InstInfo* nop = malloc(sizeof(*nop));
  nop->inst = 31;
  int nopnop=0; 
  int MAXPC4 = maxpc + 4;
  int k = 0;
  int zapzap=0;
  int setPC;
  while (i<=MAXPC4) {
    
    writeback(w);
    memory(m);
    execute(x);
    decode(d);
    setPCWithInfo(w);  
    printP2(f,d,x,m,w,instnum++); 

    printf("-------------------------------------------------\n");
    //printf("PC %d \n", pc);
    int memoryResult = m->memout;//w->memout;
    int executeResult=x->aluout;
    int forwardA=0, forwardB=0;
    int xRegWrite = x->signals.rw;
    int mRegWrite = m->signals.rw;
    int wRegWrite = m->signals.rw;
    int wrd = w->destreg;
    int mrd = m->destreg;
    int xrd = x->destreg;
    int drs = d->sourcereg;
    int drt = d->targetreg;
    int dmr = x->signals.mr;
    int drd = x->destreg;
    int frs = d->sourcereg;
    int frt = d->targetreg;
    int dBType = f->signals.btype;
    int zapped;
    int zapper;
    //if (zapzap == 2) {
      
    //}
    if (zapzap == 1) {
	  //  zapzap++; }
      //  if (zapzap == 2) {
      //      *w = *m;
      //*m = *x;
      //*x =*d;
      //*d = *f;
      printf("IN XAAPZAP\n");
      zapzap=0;
      zapped = 1;
    }
    //    int zapped = 1;
    int guessedNotTaken = pc+1;
    if (dBType == 1 || dBType ==2 || dBType == 3) {
      if (guessedNotTaken != d->aluout) {
	zapzap=1;
	//zapped = 0;
	int setPC = d->aluout;
	//setPCWithInfo(d);
      }
    }
    
    printf("PC %d , setPC: %d \n", pc, setPC);
    printf("ZAPZAP %d btype %d \n",zapzap, dBType);//zapzap);
    
    // int k = 0;

    //printf("memorytest %d\n", memoryResult);
    if (dmr == 1 && ( drd == frs || drd ==frt) ) {
      nopnop = 1;
      //printf("SETTING NOPNOP! %d\n", nopnop);
    }
    else {
      //printf("NOT SETTING NOPNOP %d\n", nopnop);
    }    
    if (wRegWrite == 1 && wrd != 0 && (wrd == drs)) { 
      forwardA=3; 
      
      //printf("3a %d \n", regfile[wrd]);
    }
    else {
      if (mRegWrite == 1 && mrd !=0 && (mrd == drs)) {
    forwardA= 1;
    //printf("1a %d \n", memoryResult);//regfile[mrd]);
      }
      else {
    if (xRegWrite == 1 && xrd !=0 && (xrd==drs)) {
      //printf("2a %d \n", regfile[xrd]);
      forwardA = 2;
    }
      }
    }
    
    if (wRegWrite == 1 && wrd != 0 && (wrd == drt)) { 
      forwardB=3; 
      //printf("3b  %d \n", regfile[wrd]);
    }
    else {
      if (mRegWrite == 1 && mrd !=0 && (mrd == drt)) {
    //printf("1b %d \n", regfile[mrd]);
    forwardB= 1;
      }
      else {
    if (xRegWrite == 1 && xrd !=0 && (xrd==drt)) {
      forwardB = 2;
      //printf("2b  %d \n", regfile[xrd]);
    }
      }
    }
    
    switch (forwardA) {
    case 2: //from execute
      d->input1 = executeResult;
      break;
    case 1: //from mem 
      d->input1 = m->memout;//memoryResult;
      break;
    case 3:
      d->input1 = w->destdata;
      break;
    default:
      break;
    }
    // printf("D %d input1: %d\n", forwardA, d->input1);
    
    switch (forwardB) {
    case 2:
      d->input2 = executeResult;
      break;
    case 1:
      d->input2 = memoryResult;
      break;
    case 3:
      d->input2 = w->destdata;
      break;
    default:
      break;
    }

    i++;

    if (nopnop== 0) {// && zapped != 1)  { 
      printf("NO NOP!\n");
      // if (zapzap==0)
	*w=*m; *m=*x; *x=*d; *d=*f;
	if (zapped ==1) {
	  d=nop;
	  zapper = 1;
	}
    }
    else {
      //printf("NO NOP\n");
      *w = *m;
      *m = *x;
      *x = *nop;
      //*w=*m; *m=*x; *x=*d; *d=*f;
      i--;
      k++;
      pc--;
    }

    if (i <= maxpc){// && dBType ==0) {
      fetch(f);
      decode(f);
    }
    else {
      *f = *n;
    }
    nopnop = 0;
    //if (zapzap == 1 and zapped = 1) {
    zapped=0;
    //}
  }
  printf("Cycles: %d\nInstructions Executed: %d\n", i+k, maxpc+1);
  free(d);  free(x);  free(m);  free(w);  free(n); free(f);
  exit(0);
}
Example #2
0
int main(int argc, char *argv[])
{
	InstInfo curInst[5];
	InstInfo newInst;
	InstInfo * saved;
	int instnum = 0;
	int maxpc;
	int count=0;
	int cycles;			// total cycles in the pipeline
	int needsFetch = 1;	// fetching flag, 1 to fetch, 0 not to fetch
	int stall = 0;		// stall flag, 1 to stall, 0 not to stall
	int isTaken = 0;
	int i=0;
	int j=0;

	FILE *program;
	if (argc != 2)
	{
		printf("Usage: sim filename\n");
		exit(0);
	}

	maxpc = load(argv[1]);
	cycles = maxpc + 4;//**************************change to + 4 for REAL output

	if(debug)printf("cycles : %d \n" , cycles);

	if(debug)printLoad(maxpc);

	// initialize 5 instructions with 0
	for(i=0; i<5; i++){
		pipelineInsts[i]= &curInst[i];
		pipelineInsts[i]->inst=0;
		if(debug)printf("DEBUG: pipelineInsts[%d]: %d\n", i, pipelineInsts[i]->inst);
	}

	do{
		doStage(needsFetch, &stall, &cycles, &count);	// doStage calls stage functions
		saved = pipelineInsts[4];		// save the unused pointer for reuse
			
		// setting needsFetch flag
		if(count < maxpc)
			needsFetch = 1;
		else
			needsFetch = 0;

		if(debug)printf("DEBUG: stall value: %d\n", stall);

		// if stall is not being set
		if(!stall){
			if(debug)printf("DEBUG: no stall\n");
			if(debug)printf("DEBUG: not stall before, pipelineInst[0] ----> %d, %s\n", pipelineInsts[0]->inst, pipelineInsts[0]->string);
			printP2(pipelineInsts[0], pipelineInsts[1], pipelineInsts[2], pipelineInsts[3], pipelineInsts[4],  count);
	
			 //shift down 4 instructions
			for(i=4; i>0; i--){
				pipelineInsts[i]=pipelineInsts[i-1];
			}
			//printf("DEBUG: no stall, swap....\n");
			//printP2(pipelineInsts[0], pipelineInsts[1], pipelineInsts[2], pipelineInsts[3], pipelineInsts[4],  count);
			//saved->inst = 0;			// reset inst = 0
			pipelineInsts[0] = saved;	// reuse the unused inst pointer
			clearInst(pipelineInsts[0]);
			if(debug)printf("DEBUG: not stall after, pipelineInst[0] ----> %d, %s\n", pipelineInsts[0]->inst, pipelineInsts[0]->string);
		}
		// if stall is being set
		else{

			if(debug)printf("DEBUG: stall before, pipelineInst[0] ----> %d, %s\n", pipelineInsts[0]->inst, pipelineInsts[0]->string);
			if(debug)printf("DEBUG: stalls\n");
			printP2(pipelineInsts[0], pipelineInsts[1], pipelineInsts[2], pipelineInsts[3], pipelineInsts[4],  count);

			pipelineInsts[4] = pipelineInsts[3];	// shift down memory stage
			pipelineInsts[3] = pipelineInsts[2];	// shitf down execute stage
			saved->inst = 0;						// reset the unused inst to be 0
			pipelineInsts[2] = saved;				// stall the execute stage
			clearInst(pipelineInsts[2]);
			stall = 0;								// reset stall flag
			cycles++;
			if(debug)printf("DEBUG: stall after, pipelineInst[0] ----> %d, %s\n", pipelineInsts[0]->inst, pipelineInsts[0]->string);
			//printf("DEBUG: stall, swap....\n");
			//printP2(pipelineInsts[0], pipelineInsts[1], pipelineInsts[2], pipelineInsts[3], pipelineInsts[4],  count);
		}
		count++;					// increment count for cycles
	}while(count < cycles);

	// put in your own variables
	printf("Cycles: %d\n", count);
	printf("Instructions Executed: %d\n", maxpc);
  	exit(0);
}