inline int op(void) { return (INST >> 26) & 0x3FUL; } inline int rs(void) { return (INST >> 21) & 0x1FUL; } inline int rt(void) { return (INST >> 16) & 0x1FUL; } inline int rd(void) { return (INST >> 11) & 0x1FUL; } inline int imm(void) { return INST & 0xFFFFUL; } inline int targ(void) { return INST & 0xFFFFFUL; } inline stdfloat split(const int l) { /* convert long into standard float */ const stdfloat sf = { l >> 31,//sign l >> 23 & 0xFF,//exp ((l & 0x7F800000UL ? 0x800000UL: 0UL) | l) & 0x7FFFFFUL//mantissa }; return sf; } /* ---------------------------------------- */ inline void fnct_error(void) { status = "Message: Undefined function detected\n"; } inline void add(void) { ic[0]++; reg[rd()] = (reg[rs()] + reg[rt()]) & 0xFFFFFFFFUL; if (debug == DEBUG_ON) printf("add done\n"); } inline void addi(void) { ic[1]++; reg[rt()] = (reg[rs()] + imm()) & 0xFFFFFFFFUL; if (debug == DEBUG_ON) printf("addi done\n"); } inline void jl(void) { ic[2]++; dpc = targ() - PC; if (debug == DEBUG_ON) { printf("jl: PC = %u\n", PC); printf("jl done\n"); } } inline void stop(void) { if (debug == DEBUG_ON) printf("stop done\n"); } inline void led(void) { if (debug == DEBUG_ON) printf("led done\n"); } inline void cmp(void) { ic[3]++; if (reg[rs()] == reg[rt()]) cr = EQ; //if (reg[rs()] != reg[rt()]) cr = NE; if (reg[rs()] > reg[rt()]) cr = GT; if (reg[rs()] < reg[rt()]) cr = LS; if (debug == DEBUG_ON) printf("cmp done\n"); } inline void be(void) { ic[4]++; if (cr == EQ) dpc = targ() - PC; cr = INIT; } inline void bg(void) { ic[5]++; if (cr == GT) { dpc = targ() - PC; cr = INIT; } if (debug == DEBUG_ON) printf("bg done\n"); } inline void bl(void) { ic[6]++; if (cr == LS) { dpc = targ() - PC; cr = INIT; } if (debug == DEBUG_ON) printf("bl done\n"); } inline void bne(void) { ic[7]++; if (cr != EQ) { dpc = targ() - PC; cr = INIT; } if (debug == DEBUG_ON) printf("bne done\n"); } inline void jal(void) { ic[8]++; reg[19] = PC + 1; dpc = reg[rs()] - PC; if (debug == DEBUG_ON) printf("jal done\n"); } inline void jr(void) { ic[9]++; if (reg[19] == 0) dpc = 0; else dpc = reg[19] - PC; if (debug == DEBUG_ON) printf("jr done\n"); } inline void sub(void) { ic[10]++; reg[rd()] = (reg[rs()] - reg[rt()]) & 0xFFFFFFFFUL; if (debug == DEBUG_ON) printf("sub done\n"); } inline void subi(void) { ic[11]++; reg[rt()] = (reg[rs()] - imm()) & 0xFFFFFFFFUL; if (debug == DEBUG_ON) printf("subi done\n"); } inline void slli(void) { ic[12]++; reg[rt()] = (reg[rs()] << imm()) & 0xFFFFFFFFUL; if (debug == DEBUG_ON) printf("slli done\n"); } inline void lw(void) { ic[13]++; //const int size = sizeof(mem) / sizeof(int); reg[rs()] = mem[(reg[rt()] + imm())]; } inline void sw(void) { ic[14]++; //const int size = sizeof(mem) / sizeof(int); mem[(reg[rt()] + imm())] = reg[rs()]; //if (debug == DEBUG_ON) printf("sw done\n"); } inline void llo(void) { ic[15]++; reg[rs()] = (imm() & 0x0000FFFFUL); if (debug == DEBUG_ON) { printf("imm() = %u\n", imm()); printf("llo done\n"); } } inline void lhi(void) { ic[16]++; reg[rs()] = (reg[rs()] & 0x0000FFFFUL) | ((imm() << 16) & 0xFFFF0000); if (debug == DEBUG_ON) printf("lhi done\n"); } inline void jall(void) { ic[17]++; reg[19] = PC + 1; dpc = targ() - PC;//PC = target() if (debug == DEBUG_ON) { printf("jl: PC = %u\n", PC); printf("jall done\n"); } } inline void cmpi(void) { ic[18]++; if (reg[rs()] == imm()) cr = EQ; //if (reg[rs()] != imm()) cr = NE; if (reg[rs()] > imm()) cr = GT; if (reg[rs()] < imm()) cr = LS; if (debug == DEBUG_ON) printf("cmpi done\n"); } inline void j(void) { ic[19]++; dpc = reg[rs()] - PC; if (debug == DEBUG_ON) printf("j done\n"); } inline void ble(void) { blec++; if (cr == LS || cr == EQ) { dpc = targ() - PC; cr = INIT; } if (debug == DEBUG_ON) printf("ble done\n"); } inline void bge(void) { bgec++; if (cr == GT || cr == EQ) { dpc = targ() - PC; cr = INIT; } if (debug == DEBUG_ON) printf("bge done\n"); } inline void fadd(void) { ic[20]++; float f1; float f2; memcpy(&f1, ®[rs()], sizeof(float)); memcpy(&f2, ®[rt()], sizeof(float)); f1 = f1 + f2; memcpy(®[rd()], &f1, sizeof(float)); if (debug == DEBUG_ON) printf("fadd done\n"); } inline void fsub(void) { fsubc++; float f1; float f2; memcpy(&f1, ®[rs()], sizeof(float)); memcpy(&f2, ®[rt()], sizeof(float)); f1 = f1 - f2; memcpy(®[rd()], &f1, sizeof(float)); if (debug == DEBUG_ON) printf("fsub done\n"); } inline void fmul(void) { fmulc++; float f1; float f2; memcpy(&f1, ®[rs()], sizeof(float)); memcpy(&f2, ®[rt()], sizeof(float)); f1 = f1 * f2; memcpy(®[rd()], &f1, sizeof(float)); if (debug == DEBUG_ON) printf("fmul done\n"); } inline void fdiv(void) { fdivc++; float f1; float f2; fdivc++; memcpy(&f1, ®[rs()], sizeof(float)); memcpy(&f2, ®[rt()], sizeof(float)); f1 = f1 / f2; memcpy(®[rd()], &f1, sizeof(float)); if (debug == DEBUG_ON) printf("fdiv done\n"); } inline void fcmp(void) { fcmpc++; float fs, ft; memcpy(&fs,®[rs()],4); memcpy(&ft,®[rt()],4); if (fs == ft) cr = EQ; //if (reg[rs()] != reg[rt()]) cr = NE; if (fs > ft) cr = GT; if (fs < ft) cr = LS; if (debug == DEBUG_ON) printf("fcmp done\n"); } inline void fble(void) { fblec++; if (cr == LS || cr == EQ) { dpc = targ() - PC; cr = INIT; } if (debug == DEBUG_ON) printf("fble done\n"); } inline void fbg(void) { fbgc++; if (cr == GT) { dpc = targ() - PC; cr = INIT; } if (debug == DEBUG_ON) printf("fbg done\n"); } inline void fbe(void) { fbec++; if (cr == EQ) { dpc = targ() - PC; cr = INIT; } if (debug == DEBUG_ON) printf("fbe done\n"); } inline void fbne(void) { fbnec++; if (cr != EQ) { dpc = targ() - PC; cr = INIT; } if (debug == DEBUG_ON) printf("fbne done\n"); } inline void call(void) { float f; switch (targ()) { case 1://print_int printf("%d", reg[1]); /* if (debug == DEBUG_OFF) { float f1;in memcpy(&f1, ®[1], sizeof(float)); printf("%5.3f\n", f1); }*/ break; case 2://print_new_line printf("\n"); break; case 3://truncate memcpy(&f, ®[20], sizeof(float)); reg[1] = truncate(f); break; case 4: break; case 5: break; case 6: memcpy(&f, ®[20], sizeof(float)); f = fabs(f); memcpy(®[20], &f, sizeof(float)); break; case 7: memcpy(&f, ®[20], sizeof(float)); f = sqrtf(f); memcpy(®[20], &f, sizeof(float)); sqrtc++; break; case 8: memcpy(&f, ®[20], sizeof(float)); f = cosf(f); memcpy(®[20], &f, sizeof(float)); cosc++; break; case 9: memcpy(&f, ®[20], sizeof(float)); f = sinf(f); memcpy(®[20], &f, sizeof(float)); sinc++; break; case 10: f = 1.0 * reg[1]; memcpy(®[20], &f, sizeof(float)); break; case 11: memcpy(&f, ®[20], sizeof(float)); reg[1] = truncate(f); break; case 12: memcpy(&f, ®[20], sizeof(float)); f = atan(f); memcpy(®[20], &f, sizeof(float)); atanc++; break; case 13://printt_float memcpy(&f, ®[20], sizeof(float)); printf("%5.10f", f); break; default: break; } } inline void in(void) { inc++; float f; if (rs() < 16) { fscanf(input, "%d", ®[rs()]); //printf("%d\n", reg[rs()]); } else { fscanf(input, "%f", &f); memcpy(®[rs()], &f, sizeof(float)); //printf("%5.10f\n", f); } } inline void outb(void) { outbc++; if (argNum == 3) fprintf(output, "%c", (reg[rs()] & 0xFF)); else printf("%c", (reg[rs()] & 0xFF)); } inline void outw(void) { outwc++; if (argNum == 3) fprintf(output, "%d", reg[rs()]); else printf("%d", reg[rs()]); } inline void andi(void) { andic++; reg[rt()] = (reg[rs()] & imm()) & 0xFFFFFFFFUL; if (debug == DEBUG_ON) printf("andi done\n"); } inline void my_floor(void) { floorc++; float f; memcpy(&f, ®[rt()], sizeof(float)); f = floorf(f); memcpy(®[rs()], &f, sizeof(float)); } inline void fneg(void) { fnegc++; float f; memcpy(&f, ®[rt()], sizeof(float)); f = 0.0 - f; memcpy(®[rs()], &f, sizeof(float)); } inline void fispos(void) { fisposc++; float f; memcpy(&f, ®[rt()], sizeof(float)); if (f > 0){ reg[rs()]=1; }else{ reg[rs()]=0; } } inline void fiszero(void) { fiszeroc++; float f; memcpy(&f, ®[rt()], sizeof(float)); if (f == 0){ reg[rs()]=1; }else{ reg[rs()]=0; } } inline void my_fabs(void) { fabsc++; float f; if (reg[rt()] > 0){ ; }else{ memcpy(&f, ®[rt()], sizeof(float)); f = -f; memcpy(®[rs()], &f, 4); } } inline void fisneg(void) { fisnegc++; float f; memcpy(&f, ®[rt()], sizeof(float)); if (f < 0){ reg[rs()]=1; }else{ reg[rs()]=0; } } inline void fless(void) { flessc++; float f1, f2; memcpy(&f1, ®[rs()], sizeof(float)); memcpy(&f2, ®[rt()], 4); if (f1 < f2){ reg[rd()]=1; }else{ reg[rd()]=0; } } inline void setl(void) { setlc++; reg[rs()] = (targ() & 0x000FFFFFUL); if (debug == DEBUG_ON) { printf("imm() = %u\n", imm()); printf("llo done\n"); } } typedef void (*int_fp)(); int_fp execute[maxOpNum] = { call, add, addi, fnct_error, jl, led, stop, cmp, be, bg, bl, bne, jall, jr, sub, subi, slli, lw, sw, jal, cmpi, j, ble, bge, fnct_error, in, outb, fnct_error, outw, fnct_error, lhi, llo, fnct_error, fadd, fsub, fmul, fdiv, fcmp, fble, fbg, fbe, fbne, fnct_error, fnct_error, fnct_error, fnct_error, fnct_error, fneg, fnct_error, fnct_error, fnct_error, fnct_error, fnct_error, fnct_error, fnct_error, fnct_error, fispos, fiszero, my_fabs, fisneg, fless, my_floor, andi, fnct_error }; inline void decode() { char buf[6]; int opcode = op(); int i; if (debug == DEBUG_ON) { for (i = 0; i < opNum; i++) { if (strcmp(my_itoa(opcode, buf, 6, 2), opcodeTable[i][0]) == 0) { printf("opcode = %s ", opcodeTable[i][1]); if (atoi(opcodeTable[i][3]) == 2) printf("rs = %d rt = %d imm = %d\n", rs(), rt(), imm()); if (atoi(opcodeTable[i][3]) == 1) printf("rs = %d rt = %d rd = %d\n", rs(), rt(), rd()); if (atoi(opcodeTable[i][3]) == 3) printf("targ = %d\n", targ()); } } } execute[opcode](); }