void emit_nand_test(Seq_T stream) { emit(stream, loadval(r2, 42)); emit(stream, loadval(r3, 42)); emit(stream, nand(r1, r2, r3)); emit(stream, nand(r1, r2, r3)); emit(stream, output(r2)); emit(stream, loadval(r1, '\n')); emit(stream, output(r1)); emit(stream, halt()); }
/* * execute: Takes in the decoded instruction as a uint32_t array, the UM * registers, the segmented memory sequence, the deleted memory * queue, and finally the program counter. The program executes * the instruction, and returns the updated program counter (which * will be incremented in every case except the load program opcode). */ unsigned execute(uint32_t arr[], uint32_t registers[], Seq_T segMem, Seq_T delMem, unsigned pc) { pc++; uint32_t opcode = arr[0]; switch (opcode) { case 0: cond_move(®(arr, 3), ®(arr, 1), ®(arr, 2)); break; case 1: seg_load(segMem, ®(arr, 1), ®(arr, 2), ®(arr, 3)); break; case 2: seg_store(segMem, ®(arr, 1), ®(arr, 2), ®(arr, 3)); break; case 3: add(®(arr, 2), ®(arr, 3), ®(arr, 1)); break; case 4: multiply(®(arr, 1), ®(arr, 2), ®(arr, 3)); break; case 5: divide(®(arr, 1), ®(arr, 2), ®(arr, 3)); break; case 6: nand(®(arr, 2), ®(arr, 3), ®(arr, 1)); break; case 7: halt(segMem, delMem, arr); break; case 8: map_seg(segMem, delMem, ®(arr, 2), ®(arr, 3)); break; case 9: unmap_seg(segMem, delMem, ®(arr, 3)); break; case 10: output(®(arr, 3)); break; case 11: input(®(arr, 3)); break; case 12: load_program(segMem, REG(arr, 2)); pc = REG(arr, 3); break; case 13: load_val(®(arr, 1), arr[2]); break; default: fprintf(stderr, "ERROR: Invalid opcode: %d.", opcode); exit(EXIT_FAILURE); break; } //free(arr); return pc; }
void run_command(int argc, char * argv[]) { if (strcmp(argv[0], "help") == 0) { help(argc, argv); return; } if (strcmp(argv[0], "md") == 0) { md(argc, argv); return; } if (strcmp(argv[0], "mw") == 0) { mw(argc, argv); return; } if (strcmp(argv[0], "nand") == 0) nand(argc, argv); if(argc >= 1) wy_printf("Unknown command '%s' - try 'help' \n",argv[0]); return; }
void arith_test(Seq_T stream) { emit(stream, loadval(r2, 20)); emit(stream, loadval(r3, 10)); emit(stream, add(r1, r2 , r3)); emit(stream, multiply(r1, r2, r3)); emit(stream, divide(r1, r2, r3)); emit(stream, nand(r1, r2, r3)); emit(stream, halt()); }
void execALU(){ switch(controle_alu.op_code){ case 1: add(); break; case 2: addinc(); break; case 3: and(); break; case 4: andnota(); break; case 5: asl(); break; case 6: asr(); break; case 7: deca(); break; case 8: inca(); break; case 9: j(); break; case 10: jal(); break; case 11: jf(); break; case 12: jr(); break; case 13: jt(); break; case 14: lch(); break; case 15: lcl(); break; case 16: load(); break; case 17: loadlit(); break; case 18: lsl(); break; case 19: lsr(); break; case 20: nand(); break; case 21: nor(); break; case 22: ones(); break; case 23: or(); break; case 24: ornotb(); break; case 25: passa(); break; case 26: passnota(); break; case 27: store(); break; case 28: sub(); break; case 29: subdec(); break; case 30: xnor(); break; case 31: xor(); break; case 32: zeros(); break; } }
int main(void) { bit a; a = 0x00; bit b; b = 0x00; bit a1; a1 = 0x01; bit b1; b1 = 0x01; printf("\nAND GATE\n"); printf("a b r\n"); printf("-----------\n"); printf("%d %d %d\n",a, b, and(a, b)); printf("%d %d %d\n",a, b1, and(a, b1)); printf("%d %d %d\n",a1, b, and(a1, b)); printf("%d %d %d\n",a1, b1, and(a1, b1)); printf("\nOR GATE\n"); printf("a b r\n"); printf("-----------\n"); printf("%d %d %d\n",a, b, or(a, b)); printf("%d %d %d\n",a, b1, or(a, b1)); printf("%d %d %d\n",a1, b, or(a1, b)); printf("%d %d %d\n",a1, b1, or(a1, b1)); printf("\nNOT GATE\n"); printf("a r\n"); printf("-----------\n"); printf("%d %d\n",a, not(a)); printf("%d %d\n",a1, not(a1)); printf("\nXOR GATE\n"); printf("a b r\n"); printf("-----------\n"); printf("%d %d %d\n",a, b, xor(a, b)); printf("%d %d %d\n",a, b1, xor(a, b1)); printf("%d %d %d\n",a1, b, xor(a1, b)); printf("%d %d %d\n",a1, b1, xor(a1, b1)); printf("\nNOR GATE\n"); printf("a b r\n"); printf("-----------\n"); printf("%d %d %d\n",a, b, nor(a, b)); printf("%d %d %d\n",a, b1, nor(a, b1)); printf("%d %d %d\n",a1, b, nor(a1, b)); printf("%d %d %d\n",a1, b1, nor(a1, b1)); printf("\nNAND GATE\n"); printf("a b r\n"); printf("-----------\n"); printf("%d %d %d\n",a, b, nand(a, b)); printf("%d %d %d\n",a, b1, nand(a, b1)); printf("%d %d %d\n",a1, b, nand(a1, b)); printf("%d %d %d\n",a1, b1, nand(a1, b1)); printf("\n~(~(a)|~(b))\n"); printf("a b r\n"); printf("-----------\n"); printf("%d %d %d\n",a, b, not(or(not(a), not(b)))); printf("%d %d %d\n",a, b1, not(or(not(a), not(b1)))); printf("%d %d %d\n",a1, b, not(or(not(a1), not(b)))); printf("%d %d %d\n",a1, b1, not(or(not(a1), not(b1)))); }
int main(int argc, _TCHAR* argv[]) #endif { int x, r; int vid=0, pid=0; bool err=false; bool doSlow=false; string file=""; enum Action { actionNone=0, actionID, actionRead, actionWrite, actionVerify }; printf("FT2232H-based NAND reader"); //Parse command line options Action action=actionNone; NandChip::AccessType access=NandChip::accessBoth; for (x=1; x<argc; x++) { if (strcmp(argv[x],"-i")==0) { action=actionID; } else if (strcmp(argv[x],"-r")==0 && x<=(argc-2)) { action=actionRead; file=argv[++x]; } else if (strcmp(argv[x],"-w")==0 && x<=(argc-2)) { action=actionWrite; file=argv[++x]; } else if (strcmp(argv[x],"-v")==0 && x<=(argc-2)) { action=actionVerify; file=argv[++x]; } else if (strcmp(argv[x],"-u")==0 && x<=(argc-2)) { action=actionVerify; char *endp; x++; vid=strtol(argv[x], &endp, 16); if (*endp!=':') { err=true; } else { endp++; pid=strtol(endp, NULL, 16); } } else if (strcmp(argv[x],"-t")==0 && x<=(argc-2)) { x++; if (strcmp(argv[x],"main")==0) { access=NandChip::accessMain; } else if (strcmp(argv[x], "oob")==0) { access=NandChip::accessOob; } else if (strcmp(argv[x], "both")==0) { access=NandChip::accessBoth; } else { printf("Must be 'main' 'oob' or 'both': %s\n", argv[x]); err=true; } } else if (strcmp(argv[x],"-s")==0) { doSlow=true; } else { printf("Unknown option or missing argument: %s\n", argv[x]); err=true; } } if (action==actionNone || err || argc==1) { printf("Usage: [-i|-r file|-v file] [-t main|oob|both] [-s]\n"); printf(" -i - Identify chip\n"); printf(" -r file - Read chip to file\n"); printf(" -w file - Write chip from file\n"); printf(" -v file - Verify chip from file data\n"); printf(" -t reg - Select region to read/write (main mem, oob ('spare') data or both, interleaved)\n"); printf(" -s - clock FTDI chip at 12MHz instead of 60MHz\n"); printf(" -u vid:pid - use different FTDI USB vid/pid. Vid and pid are in hex.\n"); exit(0); } FtdiNand ftdi; ftdi.open(vid,pid,doSlow); NandChip nand(&ftdi); if (action==actionID) { nand.showInfo(); } else if (action==actionRead || action==actionVerify) { int f; if (action==actionRead) { f=open(file.c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644); } else { f=open(file.c_str(), O_RDONLY|O_BINARY); } if (f<0) { perror(file.c_str()); exit(1); } NandID *id=nand.getIdPtr(); long pages=(id->getSizeMB()*1024LL*1024LL)/id->getPageSize(); int size=0; if (access==NandChip::accessMain) size=id->getPageSize(); if (access==NandChip::accessOob) size=id->getOobSize(); if (access==NandChip::accessBoth) size=id->getOobSize()+id->getPageSize(); char *pageBuf=new char[size]; char *verifyBuf=new char[size]; int verifyErrors=0; nand.showInfo(); printf("%sing %li pages of %i bytes...\n", action==actionRead?"Read":"Verify", pages, id->getPageSize()); for (x=0; x<pages; x++) { nand.readPage(x, pageBuf, size, access); if (action==actionRead) { r=write(f, pageBuf, size); if (r!=size) { perror("writing data to file"); exit(1); } } else { r=read(f, verifyBuf, size); if (r!=size) { perror("reading data from file"); exit(1); } for (int y=0; y<size; y++) { if (verifyBuf[y]!=pageBuf[y]) { verifyErrors++; printf("Verify error: Page %i, byte %i: file 0x%02hhX flash 0x%02hhX\n", x, y, verifyBuf[y], pageBuf[y]); } } } if ((x&15)==0) { printf("%i/%li\n\033[A", x, pages); } } if (action==actionVerify) { printf("Verify: %i bytes differ between NAND and file.\n", verifyErrors); } } printf("All done.\n"); return 0; }
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; }
static int three_register_execute(UM_machine machine, Um_instruction instruction) { unsigned op = Bitpack_getu(instruction, BITS_PER_OP_CODE, 28); unsigned A = Bitpack_getu(instruction, BITS_PER_REG_NUM, 6); unsigned B = Bitpack_getu(instruction, BITS_PER_REG_NUM, 3); unsigned C = Bitpack_getu(instruction, BITS_PER_REG_NUM, 0); switch(op){ case 0: // movc(machine, A, B, C); return 1; case 1: segl(machine, A, B, C); return 1; case 2: segs(machine, A, B, C); return 1; case 3: add(machine, A, B, C); return 1; case 4: mult(machine, A, B, C); return 1; case 5: divi(machine, A, B, C); return 1; case 6: nand(machine, A, B, C); return 1; case 7: return 0; case 8: map(machine, B, C); return 1; case 9: unmap(machine, C); return 1; case 10: out(machine, C); return 1; case 11: in(machine, C); return 1; case 12: loadp(machine, B, C); return 1; default: return 0; } }
/* finds the value of every led saves it in data structure and in Array (used for drawing screen) */ void findOutput(){ int i, n1, n2, value; output * optr; node * nptr; for(i = 0; i < c.li; i++) { init(&s); MakeStack(i); while(!empty(&s)) { nptr = pop(&s); switch(nptr->type) { case INPUT : PutValue(nptr, value); break; case OUTPUT : GetInputs(&n1, &n2, nptr->ptr); /* Calculating Output */ optr = nptr->ptr; switch(optr->type) { case NAND : value = nand(n1, n2); break; case XNOR : value = xnor(n1, n2); break; case NOR : value = nor(n1, n2); break; case AND : value = and(n1, n2); break; case OR : value = or(n1, n2); break; case XOR : value = xor(n1, n2); break; case NOT : value = not(n1); break; default : break; PutValue(nptr, value); } break; case SWITCH : value = GetValue(nptr->ptr); break; case LED : PutValue(nptr, value); break; } } } ChangeOnscreen(); }
//Perform the operation given by the CPU void ALU::operation(int code, Register a, Register b) { //Reset all flags that aren't required m_flags[1] = 0; m_flags[2] = 0; m_flags[3] = 0; //Check if unary operation if(!(code==0||code==1||code==4||code==9||code==13||code==16||code==17||code==18||code==19||code==20)) { //Compare a and b if possible and set flags if(a.getValue()>b.getValue()) { m_flags[1] = 1; } else if(a.getValue()==b.getValue()) { m_flags[2] = 1; } } //Perform operation based on code (sets carry flag to 0 if it isn't set in the operation) if(code==0) { zero(a); m_flags[0] = 0; } else if(code==1) { one(a); m_flags[0] = 0; } else if(code==2) { add(a,b); } else if(code==3) { addc(a,b); } else if(code==4) { inc(a); } else if(code==5) { sub(a,b); } else if(code==6) { subc(a,b); } else if(code==7) { rsub(a,b); } else if(code==8) { rsubc(a,b); } else if(code==9) { dec(a); } else if(code==10) { mul(a,b); } else if(code==11) { random(a); m_flags[0] = 0; } else if(code==16) { shl(a); } else if(code==17) { shlc(a); } else if(code==18) { shr(a); } else if(code==19) { shrc(a); } else if(code==20) { not(a); m_flags[0] = 0; } else if(code==21) { and(a,b); m_flags[0] = 0; } else if(code==22) { nand(a,b); m_flags[0] = 0; } else if(code==23) { or(a,b); m_flags[0] = 0; } else if(code==24) { nor(a,b); m_flags[0] = 0; } else if(code==25) { xor(a,b); m_flags[0] = 0; } else if(code==26) { xnor(a,b); m_flags[0] = 0; } else if(code==27) { bclr(a,b); m_flags[0] = 0; } else if(code==31) { m_flags[0] = 0; } //Check if output is 0 and set flag accordingly if(a.getValue()) { m_flags[3]=1; } }