示例#1
0
void doStuff(InstInfo* FI, InstInfo* DI, InstInfo* XI, InstInfo* MI, InstInfo* WI) {
  //fetch(FI);
  //printf("%s\n", FI->string);
  decode(DI);
  execute(XI);
  memory(MI);
  writeback(WI); 
  setPCWithInfo(MI);
}
示例#2
0
void decode(InstInfo *instruction)
{
	// helper variables
	int val = instruction->inst;
	int op, func;
	short imm;
	instruction->fields.op = (val >> 26) & 0x03f;
	

	op = instruction->fields.op;
	// if (op==same_op)
	// {
	instruction->fields.func = val & 0x03f;
	func  = instruction->fields.func;
	// }
	// printf("\nop:%d\n",op);
	// printf("\nfunc:%d\n",func);
	// op = addi;
	// hard-coded tests for each funciton signal/field
	//fields
	instruction->fields.rs = (val >> 21) & 0x01f;
	instruction->fields.rt = (val >> 16) & 0x01f;
	instruction->fields.rd = (val >> 11) & 0x01f;
	imm = (val & 0x0ffff);
	instruction->fields.imm = imm;//signExt((val & 0x0ffff));
	//signals
	switch (op)
	{
		case addi:
		{
			instruction->signals.aluop = 0;
			instruction->signals.mw = 0;
			instruction->signals.mr = 0;
			instruction->signals.mtr = 0;
			instruction->signals.asrc = 1;
			instruction->signals.btype = 0;
			instruction->signals.rdst = 0;
			instruction->signals.rw = 1;
			sprintf(instruction->string,"addi $%d, $%d, %d",
				instruction->fields.rt, instruction->fields.rs, 
				instruction->fields.imm);
			break;
		}
		case blt:
		{
			instruction->signals.aluop = 7;
			instruction->signals.mw = 0;
			instruction->signals.mr = 0;
			instruction->signals.mtr = -1;
			instruction->signals.asrc = 0;
			instruction->signals.btype = 3;
			instruction->signals.rdst = -1;
			instruction->signals.rw = 0;
			sprintf(instruction->string,"blt $%d, $%d, %d",
				instruction->fields.rs, instruction->fields.rt, 
				instruction->fields.imm);
			break;
		}
		case same_op:
		{
			switch(func)
			{
				case and_func:
				{
					instruction->signals.aluop = 2;
					instruction->signals.mw = 0;
					instruction->signals.mr = 0;
					instruction->signals.mtr = 0;
					instruction->signals.asrc = 0;
					instruction->signals.btype = 0;
					instruction->signals.rdst = 1;
					instruction->signals.rw = 1;
					sprintf(instruction->string,"and $%d, $%d, $%d",
						instruction->fields.rd, instruction->fields.rs, 
						instruction->fields.rt);
					break;
				}
				case sub_func:
				{
					instruction->signals.aluop = 1;
					instruction->signals.mw = 0;
					instruction->signals.mr = 0;
					instruction->signals.mtr = 0;
					instruction->signals.asrc = 0;
					instruction->signals.btype = 0;
					instruction->signals.rdst = 1;
					instruction->signals.rw = 1;
					sprintf(instruction->string,"sub $%d, $%d, $%d",
						instruction->fields.rd, instruction->fields.rs, 
						instruction->fields.rt);
					break;
				}
				case sgt_func:
				{
					instruction->signals.aluop = 6;
					instruction->signals.mw = 0;
					instruction->signals.mr = 0;
					instruction->signals.mtr = 0;
					instruction->signals.asrc = 0;
					instruction->signals.btype = 0;
					instruction->signals.rdst = 1;
					instruction->signals.rw = 1;
					sprintf(instruction->string,"sgt $%d, $%d, $%d",
						instruction->fields.rd, instruction->fields.rs, 
						instruction->fields.rt);
					break;
				}
			}
			break;
		}//eof case same_op
		case lw:
		{
			instruction->signals.aluop = 0;
			instruction->signals.mw = 0;
			instruction->signals.mr = 1;
			instruction->signals.mtr = 1;
			instruction->signals.asrc = 1;
			instruction->signals.btype = 0;
			instruction->signals.rdst = 0;
			instruction->signals.rw = 1;
			sprintf(instruction->string,"lw $%d, %d($%d)",
				instruction->fields.rt, instruction->fields.imm, 
				instruction->fields.rs);
			break;
		}
		case sw:
 		{
			instruction->signals.aluop = 0;
			instruction->signals.mw = 1;
			instruction->signals.mr = 0;
			instruction->signals.mtr = -1;
			instruction->signals.asrc = 1;
			instruction->signals.btype = 0;
			instruction->signals.rdst = -1;
			instruction->signals.rw = 0;
			sprintf(instruction->string,"sw $%d,%d($%d)",
				instruction->fields.rt, instruction->fields.imm, 
				instruction->fields.rs);
			break;
		}
		case jr:
		{
			instruction->signals.aluop = -1;
			instruction->signals.mw = 0;
			instruction->signals.mr = 0;
			instruction->signals.mtr = -1;
			instruction->signals.asrc = -1;
			instruction->signals.btype = 2;
			instruction->signals.rdst = -1;
			instruction->signals.rw = 0;
			sprintf(instruction->string,"jr $%d",
				instruction->fields.rs);			
			break;
		}
		case jal:
		{
			instruction->signals.aluop = -1;
			instruction->signals.mw = 0;
			instruction->signals.mr = 0;
			instruction->signals.mtr = 2;
			instruction->signals.asrc = -1;
			instruction->signals.btype = 1;
			instruction->signals.rdst = 2;
			instruction->signals.rw = 1;
			sprintf(instruction->string,"jal %d",
				instruction->fields.imm);
						// printf("\nimm:%d\n",instruction->fields.imm);

			break;
		}
	}	

	instruction->sourcereg=instruction->fields.rs;
	instruction->targetreg=instruction->fields.rt;
	if(instruction->signals.rdst==0)
		instruction->destreg=instruction->targetreg;
	else if(instruction->signals.rdst==1)
		instruction->destreg=instruction->fields.rd;
	else if(instruction->signals.rdst==2)
		instruction->destreg=31;
	// read from the register file
	instruction->s1data = regfile[instruction->fields.rs];
	instruction->s2data = regfile[instruction->fields.rt];
	instruction->input1 = instruction->s1data;
	// fill in s1data and input2
	if (instruction->signals.asrc==0)
		instruction->input2 = instruction->s2data;
	else if(instruction->signals.asrc==1)
		instruction->input2 = instruction->fields.imm;
	
	// printf("\ninput2:%d\n",instruction->input2);
	// printf("\ninput1:%d\n",instruction->input1);
	// setPCWithInfo(instruction);
	// execute(instruction);
	// printf("aluout: %d\n", instruction->aluout);
	setPCWithInfo(instruction);

}
示例#3
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);
}