test_case(p2, addu) { bool a[8], b[8], c[8]; bool output[8]; to_bits8(a, 11); to_bits8(b, 7); to_bits8(c, 18); addu(output, a, b); assert_eq8(c, output); }
bool buffer_remove(Buffer *buf, size_t pos, size_t len) { size_t end; if (len == 0) return true; if (!addu(pos, len, &end) || end > buf->len) return false; memmove(buf->data + pos, buf->data + pos + len, buf->len - pos - len); buf->len -= len; return true; }
void unittest4() { bool overflow = false; assert(addu(2ULL, 3ULL, overflow) == 5); assert(!overflow); assert(addu(1, UINT64_MAX - 1, overflow) == UINT64_MAX); assert(!overflow); assert(addu(0ULL, -1LL, overflow) == UINT64_MAX); assert(!overflow); assert(addu(UINT64_MAX, 1, overflow) == 0ULL); assert(overflow); overflow = false; assert(addu(0ULL + 1, -1LL, overflow) == 0ULL); assert(overflow); assert(addu(0ULL, 0ULL, overflow) == 0); assert(overflow); // sticky }
void unittest3() { bool overflow = false; assert(addu(2U, 3U, overflow) == 5); assert(!overflow); assert(addu(1U, UINT32_MAX - 1U, overflow) == UINT32_MAX); assert(!overflow); assert(addu(0U, -1U, overflow) == UINT32_MAX); assert(!overflow); assert(addu(UINT32_MAX, 1U, overflow) == 0); assert(overflow); overflow = false; assert(addu(0U + 1U, -1U, overflow) == 0); assert(overflow); assert(addu(0U, 0U, overflow) == 0); assert(overflow); // sticky }
int exe(FILE* program) //program指向存有待执行程序机器码的文件 { char* tmp_instru=(char*)malloc(33*sizeof(char)); //读机器码 programTail=programHead; while(fscanf(program,"%s",tmp_instru)!=EOF) { instru=0; int i=0; unsigned j=1; for(i=31;i>=0;i--) { if(tmp_instru[i]=='1') { instru+=j; j*=2; } else { j*=2; } }//将机器码转为unsi unsigned char* tmp_R=&instru; for(i=0;i<4;i++) { writeMymemory(programTail+i,tmp_R+i);//装载指令 } programTail+=4;//最后一条指令的下一条指令的地址,用来判断程序是否执行完 } pcShort=programHead; pc=pcShort; while(pcShort!=programTail) { instru=0; //指令寄存器清零 unsigned char* tmp_R=&instru; unsigned short addr=addrToMyAddr(pc); int i; for(i=0;i<4;i++) { readMymemory(addr+i,tmp_R+i);//取指令 } unsigned tmp=instru>>26;//得到指令op //printf("the op is : %u\n",tmp); unsigned numRs=0,numRt=0,numRd=0,numFs=0,numFt=0,numFd=0,tmp_fuc=0; switch(tmp) { case 0x00000023: numRs=instru<<6>>27; numRt=instru<<11>>27; RS1=myRegister+numRt; RS2=myRegister+numRs; lig=instru<<16>>16; pc=lw(pc); break; case 0x0000002B: numRs=instru<<6>>27; numRt=instru<<11>>27; RS1=myRegister+numRt; RS2=myRegister+numRs; lig=instru<<16>>16; pc=sw(pc); break; case 0x00000008: numRs=instru<<6>>27; numRt=instru<<11>>27; RS1=myRegister+numRt; RS2=myRegister+numRs; lig=instru<<16>>16; pc=addi(pc); break; case 0x00000009: numRs=instru<<6>>27; numRt=instru<<11>>27; RS1=myRegister+numRt; RS2=myRegister+numRs; lig=instru<<16>>16; pc=addiu(pc); break; case 0x0000000A: numRs=instru<<6>>27; numRt=instru<<11>>27; RS1=myRegister+numRt; RS2=myRegister+numRs; lig=instru<<16>>16; pc=slti(pc); break; case 0x0000000B: numRs=instru<<6>>27; numRt=instru<<11>>27; RS1=myRegister+numRt; RS2=myRegister+numRs; lig=instru<<16>>16; pc=sltiu(pc); break; case 0x0000000C: numRs=instru<<6>>27; numRt=instru<<11>>27; RS1=myRegister+numRt; RS2=myRegister+numRs; lig=instru<<16>>16; pc=andi(pc); break; case 0x0000000D: numRs=instru<<6>>27; numRt=instru<<11>>27; RS1=myRegister+numRt; RS2=myRegister+numRs; lig=instru<<16>>16; pc=ori(pc); break; case 0x0000000E: numRs=instru<<6>>27; numRt=instru<<11>>27; RS1=myRegister+numRt; RS2=myRegister+numRs; lig=instru<<16>>16; pc=xori(pc); break; case 0x00000024: numRs=instru<<6>>27; numRt=instru<<11>>27; RS1=myRegister+numRt; RS2=myRegister+numRs; lig=instru<<16>>16; pc=lbu(pc); break; case 0x00000020: numRs=instru<<6>>27; numRt=instru<<11>>27; RS1=myRegister+numRt; RS2=myRegister+numRs; lig=instru<<16>>16; pc=lb(pc); break; case 0x00000028: numRs=instru<<6>>27; numRt=instru<<11>>27; RS1=myRegister+numRt; RS2=myRegister+numRs; lig=instru<<16>>16; pc=sb(pc); break; case 0x0000000F: numRs=instru<<6>>27; numRt=instru<<11>>27; RS1=myRegister+numRt; RS2=myRegister+numRs; lig=instru<<16>>16; pc=lui(pc); break; case 0x00000004: numRs=instru<<6>>27; numRt=instru<<11>>27; RS1=myRegister+numRt; RS2=myRegister+numRs; lig=instru<<16>>16; pc=beq(pc); break; case 0x00000005: numRs=instru<<6>>27; numRt=instru<<11>>27; RS1=myRegister+numRt; RS2=myRegister+numRs; //printf("%u,%u,%u,%u\n",numRt,numRs,*RS1,*RS2); lig=instru<<16>>16; // printf("%u\n",lig); pc=bne(pc); break; case 0x00000006: numRs=instru<<6>>27; numRt=instru<<11>>27; RS1=myRegister+numRt; RS2=myRegister+numRs; lig=instru<<16>>16; pc=blez(pc); break; case 0x00000007: numRs=instru<<6>>27; numRt=instru<<11>>27; RS1=myRegister+numRt; RS2=myRegister+numRs; lig=instru<<16>>16; pc=bgtz(pc); break; case 0x00000001: numRs=instru<<6>>27; numRt=instru<<11>>27; RS1=myRegister+numRt; RS2=myRegister+numRs; lig=instru<<16>>16; pc=bltz(pc); break; case 0x00000002: pc=j(pc); break; case 0x00000003: pc=jal(pc); break; case 0x00000000: numRs=instru<<6>>27; numRt=instru<<11>>27; numRd=instru<<16>>27; RS1=myRegister+numRt; RS2=myRegister+numRs; RD=myRegister+numRd; tmp_fuc=instru%64; switch(tmp_fuc) { case 32: pc=add(pc); break; case 33: pc=addu(pc); break; case 34: pc=sub(pc); break; case 35: pc=subu(pc); break; case 24: pc=mul(pc); break; case 25: pc=mulu(pc); break; case 26: pc=myDiv(pc); break; case 27: pc=divu(pc); break; case 42: pc=slt(pc); break; case 43: pc=sltu(pc); break; case 36: pc=myAnd(pc); break; case 37: pc=myOr(pc); break; case 39: pc=nor(pc); break; case 40: pc=myXor(pc); break; case 8: pc=jr(pc); break; case 9: pc=jalr(pc); break; case 0: pc=nop(pc); break; case 16: pc=mfhi(pc); break; case 18: pc=mflo(pc); break; default: break; } break; case 0x00000010: numRt=instru<<11>>27; numRd=instru<<16>>27; RS1=myRegister+numRt; if(numRd==14) { pc=mfepc(pc); } else if(numRd==13) { pc=mfco(pc); } else return -1; break; case 0x00000031: numRs=instru<<6>>27; numFt=instru<<11>>27; RS2=myRegister+numRs; FS1=myFloatReg+numFt; lig=instru<<16>>16; pc=lwc1(pc); //printf("/********\nL.S %u %u\n****************/\n",numFt,numRs); break; case 0x0000001F: numRs=instru<<6>>27; numFt=instru<<11>>27; RS2=myRegister+numRs; FS1=myFloatReg+numFt; lig=instru<<16>>16; pc=S_D(pc); //printf("/********\nL.D %u %u\n****************/\n",numFt,numRs); break; case 0x0000001E: numRs=instru<<6>>27; numFt=instru<<11>>27; RS2=myRegister+numRs; FS1=myFloatReg+numFt; lig=instru<<16>>16; //printf("/********\nS.D %u %u\n****************/\n",numFt,numRs); pc=S_D(pc); break; case 0x00000039: numRs=instru<<6>>27; numFt=instru<<11>>27; RS2=myRegister+numRs; FS1=myFloatReg+numFt; lig=instru<<16>>16; //printf("/********\nS.S %u %u\n****************/\n",numFt,numRs); pc=swc1(pc); break; case 0x00000011: numFt=instru<<11>>27; numFs=instru<<16>>27; numFd=instru<<21>>27; FS1=myFloatReg+numFt; FS2=myFloatReg+numFs; FD=myFloatReg+numFd; numRs=instru<<6>>27; tmp_fuc=instru%64; //printf("%u %u\n",tmp_fuc,numRs); if(numRs==0) { switch(tmp_fuc) { case 0: pc=add_s(pc); break; case 1: pc=sub_s(pc); break; case 2: pc=mul_s(pc); case 3: pc=div_s(pc); default: break; } } else if(numRs==1) { switch(tmp_fuc) { case 0: pc=add_d(pc); //printf("/****************\nADD.D %u %u %u\n*****************/\n",numFd,numFt,numFs); break; case 1: pc=sub_d(pc); break; case 2: pc=mul_d(pc); case 3: pc=div_d(pc); default: break; } } default:break; } pcShort=pc%0x00010000; //printf("%u %u\n",pc,pcShort); //printf("%u %u\n",pcShort,programTail); } return 0; }
static int main_swapoff(int argc, char *argv[]) { FILE *fp; struct mntent *fstab; int status = 0; int c, i; while ((c = getopt_long(argc, argv, "ahvVL:U:", longswapoffopts, NULL)) != -1) { switch (c) { case 'a': /* all */ ++all; break; case 'h': /* help */ swapoff_usage(stdout, 0); break; case 'v': /* be chatty */ ++verbose; break; case 'V': /* version */ printf(_("%s (%s)\n"), progname, PACKAGE_STRING); exit(EXIT_SUCCESS); case 'L': addl(optarg); break; case 'U': addu(optarg); break; case 0: break; case '?': default: swapoff_usage(stderr, 1); } } argv += optind; if (!all && !llct && !ulct && *argv == NULL) swapoff_usage(stderr, 2); /* * swapoff any explicitly given arguments. * Complain in case the swapoff call fails. */ for (i = 0; i < llct; i++) status |= swapoff_by_label(llist[i], !QUIET); for (i = 0; i < ulct; i++) status |= swapoff_by_uuid(ulist[i], !QUIET); while (*argv != NULL) status |= do_swapoff(*argv++, !QUIET, !CANONIC); if (all) { /* * In case /proc/swaps exists, unswap stuff listed there. * We are quiet but report errors in status. * Errors might mean that /proc/swaps * exists as ordinary file, not in procfs. * do_swapoff() exits immediately on EPERM. */ read_proc_swaps(); for(i=0; i<numSwaps; i++) status |= do_swapoff(swapFiles[i], QUIET, CANONIC); /* * Unswap stuff mentioned in /etc/fstab. * Probably it was unmounted already, so errors are not bad. * Doing swapoff -a twice should not give error messages. */ fp = setmntent(_PATH_MNTTAB, "r"); if (fp == NULL) err(2, _("%s: open failed"), _PATH_MNTTAB); while ((fstab = getmntent(fp)) != NULL) { const char *special; if (!streq(fstab->mnt_type, MNTTYPE_SWAP)) continue; special = fsprobe_get_devname_by_spec(fstab->mnt_fsname); if (!special) continue; if (!is_in_proc_swaps(special)) do_swapoff(special, QUIET, CANONIC); } fclose(fp); } return status; }
static int main_swapon(int argc, char *argv[]) { int status = 0; int c, i; while ((c = getopt_long(argc, argv, "ahdefp:svVL:U:", longswaponopts, NULL)) != -1) { switch (c) { case 'a': /* all */ ++all; break; case 'h': /* help */ swapon_usage(stdout, 0); break; case 'p': /* priority */ priority = atoi(optarg); break; case 'L': addl(optarg); break; case 'U': addu(optarg); break; case 'd': discard = 1; break; case 'e': /* ifexists */ ifexists = 1; break; case 'f': fixpgsz = 1; break; case 's': /* status report */ status = display_summary(); exit(status); case 'v': /* be chatty */ ++verbose; break; case 'V': /* version */ printf(_("%s (%s)\n"), progname, PACKAGE_STRING); exit(EXIT_SUCCESS); case 0: break; case '?': default: swapon_usage(stderr, 1); } } argv += optind; if (!all && !llct && !ulct && *argv == NULL) swapon_usage(stderr, 2); if (ifexists && (!all || strcmp(progname, "swapon"))) swapon_usage(stderr, 1); if (all) status |= swapon_all(); for (i = 0; i < llct; i++) status |= swapon_by_label(llist[i], priority, discard); for (i = 0; i < ulct; i++) status |= swapon_by_uuid(ulist[i], priority, discard); while (*argv != NULL) status |= do_swapon(*argv++, priority, discard, !CANONIC); return status; }
void simulate_MainWindow::simulate() { currInstr=instrList[PC/4]; currBin=binList[PC/4]; vector<string> result; string temp=currInstr.toStdString(); string_split(temp,result); coutString=""; RD=RS=RT=immediate=address=0; v0=v1=v2=v3="None"; v0=result[0]; v1=result[1]; printf("v0=%s\nv1=%s\n",v0.c_str(),v1.c_str()); if(v0=="jr"||v0=="j"||v0=="jal") // 2 parametes { if(v0=="jr") { jr(); } else if(v0=="j") j(); else if(v0=="jal") jal(); } else if(v0=="lui") // 3 parameters { v2=result[2]; lui(); } else // 4 parameters { v2=result[2]; v3=result[3]; if(v0=="add") add(); else if(v0=="addu") addu(); else if(v0=="sub") sub(); else if(v0=="subu") subu(); else if(v0=="and") and_funct(); else if(v0=="or") or_funct(); else if(v0=="xor") xor_funct(); else if(v0=="nor") nor(); else if(v0=="slt") slt(); else if(v0=="sltu") sltu(); else if(v0=="sll") sll(); else if(v0=="srl") srl(); else if(v0=="sllv") sllv(); else if(v0=="srlv") srlv(); else if(v0=="srav") srav(); else if(v0=="addi") addi(); else if(v0=="addiu") addiu(); else if(v0=="andi") andi(); else if(v0=="ori") ori(); else if(v0=="xori") xori(); else if(v0=="sw") sw(); else if(v0=="lw") lw(); else if(v0=="beq") beq(); else if(v0=="bne") bne(); else if(v0=="slti") slti(); else if(v0=="sltiu") sltiu(); } display_all(); }
bool buffer_grow(Buffer *buf, size_t len) { size_t size; if (!addu(buf->len, len, &size)) return false; return buffer_reserve(buf, size); }
void execution(int index){ if(test==1) printf("enter EX, with index=%d\n",index); if(index==0 || index==34){ //NOP or HALT return; } /**R-type instructions**/ else if(index==1){ add(RS,RT,RD); } else if(index==2){ addu(RS,RT,RD); } else if(index==3){ sub(RS,RT,RD); } else if(index==4){ and(RS,RT,RD); } else if(index==5){ or(RS,RT,RD); } else if(index==6){ xor(RS,RT,RD); } else if(index==7){ nor(RS,RT,RD); } else if(index==8){ nand(RS,RT,RD); } else if(index==9){ slt(RS,RT,RD); } else if(index==10){ sll(RT,RD,SHAMT); } else if(index==11){ srl(RT,RD,SHAMT); } else if(index==12){ sra(RT,RD,SHAMT); } /**J-type instructions**/ /*else if(index==13){ jr(RS); } else if(index==14){ jj(C); } else if(index==15){ jal(C); }*/ /**I-type instructions**/ else if(index==16){ addi(RS,RT,C); } else if(index==17){ addiu(RS,RT,C); } else if(index==18){ lw(RS,RT,signedC); } else if(index==19){ lh(RS,RT,signedC); } else if(index==20){ lhu(RS,RT,C); } else if(index==21){ lb(RS,RT,signedC); } else if(index==22){ lbu(RS,RT,C); } else if(index==23){ sw(RS,RT,signedC); } else if(index==24){ sh(RS,RT,signedC); } else if(index==25){ sb(RS,RT,signedC); } else if(index==26){ lui(RT,C); } else if(index==27){ andi(RS,RT,C); } else if(index==28){ or(RS,RT,C); } else if(index==29){ nor(RS,RT,C); } else if(index==30){ slti(RS,RT,C); } /*else if(index==31){ beq(RS,RT,signedC); } else if(index==32){ bne(RS,RT,signedC); } else if(index==33){ bgtz(RS,signedC); }*/ else{ if(test==1) printf("this is error instruction or is done in ID\n"); } EX_prev=index; DM_index=index; }
void read_file(char * filename) { fp = fopen(filename, "r"); char * line = malloc(sizeof(char) * 255); while (fgets(line,255,fp) != NULL) { char * buf[] = {" "," "," "," "}; char * check = strtok(line, " "); int i = 0; while (check != NULL) { if (check != NULL) { buf[i] = malloc(strlen(check) + 1); strcpy(buf[i],check); } check = strtok(NULL, " "); i++; } if(strcmp(buf[0],"addu") == 0) { addu(trim(buf[2], ','),trim(buf[3], ','),trim(buf[1], '\n')); } else if (strcmp(buf[0],"subu") == 0) { subu(trim(buf[2], ','),trim(buf[3], ','),trim(buf[1], '\n')); } else if(strcmp(buf[0],"slt") ==0) { slt(trim(buf[2], ','),trim(buf[3], ','),trim(buf[1], '\n')); } else if(strcmp(buf[0],"and") == 0) { and(trim(buf[2], ','),trim(buf[3], ','),trim(buf[1], '\n')); } else if(strcmp(buf[0],"or") == 0) { or(trim(buf[2], ','),trim(buf[3], ','),trim(buf[1], '\n')); } else if(strcmp(buf[0],"lw") == 0) { lw(rs_I(buf[2]), trim(buf[1], ','), trim(buf[2], '(')); } else if (strcmp(buf[0],"sw") == 0) { sw(rs_I(buf[2]), trim(buf[1], ','), trim(buf[2], '(')); } else if(strcmp(buf[0],"bne") == 0) { bne(trim(buf[2], ','),trim(buf[1], ','),trim(buf[3], 'j')); } else if(strcmp(buf[0],"j")== 0) { j(trim(buf[1], 'j')); } } fclose(fp); printf("End Scan\n"); }