void opc_pop_handler(char *opd1,char *opd2,char *offset,char *immi){ int ans; int dec_opd2=bintodec(opd2); int val_esp=get_reg_val(8); if(immi[1]=='0'){ if(immi[0]=='0'){ int dec_opd1=bintodec(opd1); int val_mem=get_mem_val(mem+val_esp); ans=val_mem; set_reg_val(regfile+dec_opd1,ans); } } int new_esp=get_reg_val(8)+1; set_reg_val(regfile+8,new_esp); }
int greytodec(int* grey, int bits){ int* bin = greytobin(grey,bits); int dec = bintodec(bin,bits); free(bin); return dec; }
void opc_jmp_handler(char *opd1,char *opd2,char *offset,char *immi){ int ans; int dec_opd2=bintodec(opd2); if(immi[1]=='0'){ if(immi[0]=='0'){ int dec_opd1=bintodec(opd1); ans=get_reg_val(dec_opd1)-1; set_reg_val(regfile+16,ans); } else{ int dec_im_opd1=bintodec(opd1); ans=dec_im_opd1-1; set_reg_val(regfile+16,ans); } } pending_bds=2; }
void opc_div_handler(char *opd1,char *opd2,char *offset,char *immi){ int ans; int dec_opd2=bintodec(opd2); if(immi[1]=='0'){ if(immi[0]=='0'){ int dec_opd1=bintodec(opd1); int val_eax=get_reg_val(0); ans=val_eax/get_reg_val(dec_opd1); set_reg_val(regfile+0,ans); } else{ int dec_im_opd1=bintodec(opd1); ans=get_reg_val(0)/dec_im_opd1; set_reg_val(regfile+0,ans); } } }
void opc_push_handler(char *opd1,char *opd2,char *offset,char *immi){ int ans; int dec_opd2=bintodec(opd2); int val_esp=get_reg_val(8); if(immi[1]=='0'){ if(immi[0]=='0'){ int dec_opd1=bintodec(opd1); ans=get_reg_val(dec_opd1); set_reg_val(mem+val_esp,ans); } else{ int dec_im_opd1=bintodec(opd1); ans=dec_im_opd1; set_reg_val(mem+val_esp,ans); } } int new_esp=get_reg_val(8)-1; set_reg_val(regfile+8,new_esp); }
// ---------------------------------------------------------------------------------------------------- int imm_to_dec(string imm) { int n; if (imm.substr(imm.size() - 1, imm.size() - 1).compare("h") == 0) { // hex sscanf(imm.substr(0, imm.size() - 1).c_str(), "%x", &n); return n; } else if (imm.substr(imm.size() - 1, imm.size() - 1).compare("b") == 0) { // binary return bintodec(atoi(imm.substr(0, imm.size() - 1).c_str())); } else { // decimal return atoi(imm.c_str()); } }
void opc_cmp_handler(char *opd1,char *opd2,char *offset,char *immi){ int ans; int dec_opd2=bintodec(opd2); if(immi[1]=='0'){ if(immi[0]=='0'){ int dec_opd1=bintodec(opd1); int val_opd2=get_reg_val(dec_opd2); ans=val_opd2-get_reg_val(dec_opd1); } else{ int dec_im_opd1=bintodec(opd1); ans=get_reg_val(dec_opd2)-dec_im_opd1; } } else{ int dec_off=bintodec(offset); if(immi[0]=='0'){ int dec_opd1=bintodec(opd1); ans=get_mem_val(dec_opd2+dec_off)-get_reg_val(dec_opd1); } else{ int dec_im_opd1=bintodec(opd1); ans=get_mem_val(dec_opd2+dec_off)-dec_im_opd1; } } if(ans==0){ set_reg_val(regfile+R_FLAGS,0); } else if(ans>0){ set_reg_val(regfile+R_FLAGS,1); } else{ set_reg_val(regfile+R_FLAGS,2); } }
void execute(_decoder_output *decoded_instr){ instr_handlers[0]=opc_add_handler; instr_handlers[1]=opc_sub_handler; instr_handlers[2]=opc_mov_handler; instr_handlers[3]=opc_and_handler; instr_handlers[4]=opc_xor_handler; instr_handlers[5]=opc_or_handler; instr_handlers[6]=opc_cmp_handler; instr_handlers[8]=opc_xchange_handler; instr_handlers[9]=opc_push_handler; instr_handlers[10]=opc_pop_handler; instr_handlers[11]=opc_mul_handler; instr_handlers[12]=opc_mul_handler; instr_handlers[13]=opc_div_handler; instr_handlers[14]=opc_div_handler; instr_handlers[15]=opc_jmp_handler; instr_handlers[16]=opc_je_handler; instr_handlers[17]=opc_jne_handler; instr_handlers[18]=opc_jg_handler; instr_handlers[19]=opc_jge_handler; instr_handlers[20]=opc_jl_handler; instr_handlers[21]=opc_jle_handler; instr_handlers[22]=opc_jmp_handler; if(clock>2){ int dec_opc=bintodec(decoded_instr->opc); if(dec_opc==63){ printf("\n\npc is %d\n",get_reg_val(15)); printf("\n\nregister file is\n"); print(regfile); printf("\n\nmemory is\n"); print(mem); printf("exiting"); exit(0); } instr_handlers[dec_opc](decoded_instr->opd1,decoded_instr->opd2,decoded_instr->offset,decoded_instr->immi); } switch(pending_bds){ case 2: set_reg_val(regfile+REG_PC,get_reg_val(REG_PC)+1); pending_bds--; break; case 1: set_reg_val(regfile+REG_PC,get_reg_val(REG_NEXTPC)-2); pending_bds--; break; case 0: set_reg_val(regfile+REG_PC,get_reg_val(REG_PC)+1); break; } }
void opc_xchange_handler(char *opd1,char *opd2,char *offset,char *immi){ int ans1,ans2; int dec_opd2=bintodec(opd2); if(immi[1]=='0'){ if(immi[0]=='0'){ int dec_opd1=bintodec(opd1); int val_opd2=get_reg_val(dec_opd2); ans1=get_reg_val(dec_opd1); set_reg_val(regfile+dec_opd2,ans1); ans2=get_reg_val(dec_opd2); set_reg_val(regfile+dec_opd1,ans2); } } else{ int dec_off=bintodec(offset); if(immi[0]=='0'){ int dec_opd1=bintodec(opd1); ans1=get_mem_val(dec_opd2+dec_off)+get_reg_val(dec_opd1); set_reg_val(mem+dec_opd2+dec_off,ans1); ans2=get_mem_val(dec_opd2+dec_off); set_reg_val(regfile+dec_opd1,ans2); } } }
void decode() { flag3=0; hextobin(instruction[0]); strcpy(cond,arr); hextobin(instruction[1]); F[0]=arr[0]; F[1]=arr[1]; I=arr[2]; opcode[0]=arr[3]; b_opcode[0]=arr[2]; b_opcode[1]=arr[3]; hextobin(instruction[2]); opcode[1]=arr[0]; opcode[2]=arr[1]; opcode[3]=arr[2]; b_offset[0]=arr[0]; b_offset[1]=arr[1]; b_offset[2]=arr[2]; S=arr[3]; b_offset[3]=arr[3]; hextobin(instruction[3]); strcpy(Rn,arr); b_offset[4]=arr[0]; b_offset[5]=arr[1]; b_offset[6]=arr[2]; b_offset[7]=arr[3]; hextobin(instruction[4]); strcpy(Rd,arr); b_offset[8]=arr[0]; b_offset[9]=arr[1]; b_offset[10]=arr[2]; b_offset[11]=arr[3]; hextobin(instruction[5]); offset[0]=arr[0]; offset[1]=arr[1]; offset[2]=arr[2]; offset[3]=arr[3]; b_offset[12]=arr[0]; b_offset[13]=arr[1]; b_offset[14]=arr[2]; b_offset[15]=arr[3]; hextobin(instruction[6]); Imm[0]=arr[0]; Imm[1]=arr[1]; Imm[2]=arr[2]; Imm[3]=arr[3]; offset[4]=arr[0]; offset[5]=arr[1]; offset[6]=arr[2]; offset[7]=arr[3]; b_offset[16]=arr[0]; b_offset[17]=arr[1]; b_offset[18]=arr[2]; b_offset[19]=arr[3]; hextobin(instruction[7]); strcpy(Rm,arr); offset[8]=arr[0]; offset[9]=arr[1]; offset[10]=arr[2]; offset[11]=arr[3]; b_offset[20]=arr[0]; b_offset[21]=arr[1]; b_offset[22]=arr[2]; b_offset[23]=arr[3]; Imm[4]=arr[0]; Imm[5]=arr[1]; Imm[6]=arr[2]; Imm[7]=arr[3]; sum=0; bintodec(Rn); n=sum; sum=0; bintodec(Rm); mr=sum; sum=0; bintodec(Imm); mi=sum; sum=0; bintodec(Rd); d=sum; sum=0; bintodec(offset); off=sum; if(instruction[2]=='F') { flag3=1; if(instruction[7]=='E') b_off=-1; if(instruction[7]=='D') b_off=-2; if(instruction[7]=='C') b_off=-3; if(instruction[7]=='B') b_off=-4; if(instruction[7]=='A') b_off=-5; if(instruction[7]=='9') b_off=-6; if(instruction[7]=='8') b_off=-7; if(instruction[7]=='7') b_off=-8; if(instruction[7]=='6') b_off=-9; if(instruction[7]=='5') b_off=-10; if(instruction[7]=='4') b_off=-11; if(instruction[7]=='3') b_off=-12; if(instruction[7]=='2') b_off=-13; if(instruction[7]=='1') b_off=-14; if(instruction[7]=='0') b_off=-15; } else {sum=0; bintodec(b_offset); b_off=sum; } //printf("\n f= %s b_opcode= %s, b_off = %d \n",F,b_opcode,b_off); if((strcmp(F,"00")==0)&&(strcmp(opcode,"1101")==0) && I=='1') { printf(" Operation is MOV , immediate value is %d , Destination register is R%d \n",mi,d); control=1; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"1101")==0) && I=='0') { printf(" Operation is MOV , register is R%d , Destination register is R%d \n",mr,d); control=0; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"0100")==0 )&&(I=='0')) { printf(" Operation is ADD , first operand is R%d ,Second operand is R%d , Destination register is R%d \n",n,mr,d); control=2; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"0100")==0 )&&(I=='1')) { //printreg();/////////////////////////// //u++; //////////////////////// printf(" Operation is ADD , first operand is R%d ,immediate value is %d , Destination register is R%d \n",n,mi,d); control=5; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"0010")==0 )&&(I=='0')) { printf(" Operation is SUB , first operand is R%d ,Second operand is R%d , Destination register is R%d \n",n,mr,d); control=6; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"0010")==0 )&&(I=='1')) { printf(" Operation is SUB , first operand is R%d ,immediate value is %d , Destination register is R%d \n",n,mi,d); control=7; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"1010")==0 )&&(I=='0')) { printf(" Operation is CMP , first operand is R%d ,Second operand is R%d \n",n,mr); printf("\n R[4]== %d\n",R[4]); control=9; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"1010")==0 )&&(I=='1')) { printf(" Operation is CMP , first operand is R%d ,immediate value is %d \n",n,mi); printf("\n R[4]== %d\n",R[4]); control=11; if(z==0) {w=mi;z=1;} } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"0100")==0 )&&(I=='0')) { printf(" Operation is AND , first operand is R%d ,Second operand is R%d , Destination register is R%d \n",n,mr,d); control=20; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"0100")==0 )&&(I=='1')) { printf(" Operation is AND , first operand is R%d ,immediate value is %d , Destination register is R%d \n",n,mi,d); control=21; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"0100")==0 )&&(I=='0')) { printf(" Operation is OR , first operand is R%d ,Second operand is R%d , Destination register is R%d \n",n,mr,d); control=22; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"0100")==0 )&&(I=='1')) { printf(" Operation is OR , first operand is R%d ,immediate value is %d , Destination register is R%d \n",n,mi,d); control=23; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"0100")==0 )&&(I=='0')) { printf(" Operation is EOR , first operand is R%d ,Second operand is R%d , Destination register is R%d \n",n,mr,d); control=24; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"0100")==0 )&&(I=='1')) { printf(" Operation is EOR , first operand is R%d ,immediate value is %d , Destination register is R%d \n",n,mi,d); control=25; } if((strcmp(F,"01")==0)&&(I=='0')&&(strcmp(opcode,"1100")==0)) { printf(" Operation is LDR , base address is %d , offset is %d , Destination register is R%d \n",n,off,d); control=3; } /*else if((strcmp(F,"01")==0)&&(I=='0')&&(strcmp(opcode,"1100")==0)) //$$$$$$$$ for register offsets { printf(" Operation is LDR , base address is %d , offset is %d , Destination register is R%d \n",n,off,d); control=26; } */ else if((strcmp(F,"01")==0)&&(I=='0')&&(strcmp(opcode,"1100")==0)) { printf(" Operation is STR , base address is %d , offset is %d , register whose value is to be stored is R%d \n",n,off,d); control=4; } else if((strcmp(F,"01")==0)&&(I=='1')&&(strcmp(opcode,"1100")==0)) //$$$$$$$$$$ for register offsets { printf(" Operation is STR , base address is stored in R%d , offset register is R%d , register whose value is to be stored is R%d \n",n,mr,d); control=27; } else if((strcmp(F,"10")==0)&&(strcmp(b_opcode,"10")==0)&&(strcmp(cond,"0000")==0)) { printf(" Operation is Branch EQ , branch offset is %d\n",b_off); control=8; } else if((strcmp(F,"10")==0)&&(strcmp(b_opcode,"10")==0)&&(strcmp(cond,"0001")==0)) { printf(" Operation is Branch NE , branch offset is %d\n",b_off); control=12; } else if((strcmp(F,"10")==0)&&(strcmp(b_opcode,"10")==0)&&(strcmp(cond,"1011")==0)) { printf(" Operation is Branch LT , branch offset is %d\n",b_off); control=13; } else if((strcmp(F,"10")==0)&&(strcmp(b_opcode,"10")==0)&&(strcmp(cond,"1101")==0)) { printf(" Operation is Branch LE , branch offset is %d\n",b_off); control=14; } else if((strcmp(F,"10")==0)&&(strcmp(b_opcode,"10")==0)&&(strcmp(cond,"1100")==0)) { printf(" Operation is Branch GT , branch offset is %d\n",b_off); control=15; } else if((strcmp(F,"10")==0)&&(strcmp(b_opcode,"10")==0)&&(strcmp(cond,"1010")==0)) { printf(" Operation is Branch GE , branch offset is %d\n",b_off); control=16; } else if((strcmp(F,"10")==0)&&(strcmp(b_opcode,"10")==0)&&(strcmp(cond,"1110")==0)) { //return; ///////////////////////////////////////////////////////////////////////// printf(" Operation is Branch ALL , branch offset is %d\n",b_off); control=17; } else if(strcmp(opcode,"1000")==0) { printf(" EXIT \n"); control=10; } /*else if(instruction[4]=='5') R[5]= */ }