void *connection_handler(void * cconn) { struct cconn * cc = (struct cconn*) cconn; //int sock = *(int*)socket_desc; char c; int ret; int br; //write(sock , message , strlen(message)); reset_stack ( cc ); while ( (br=read(cc->fd,&c,1)==1 ) ) { //printf ( "read: %c\n", c ); addc ( cc, c ); } printf("N: tcpserver: client disconnected\n" ); close ( cc->fd ); free(cc); return 0; }
void BasicColorUpdater::Update(float delta, ParticleArray* particles) { const uint end = particles->m_AliveCount; for (uint i = 0; i < end; i++) { Maths::Vector4f diff = particles->EndColors[i] - particles->StartColors[i]; Maths::Vector4f addc(particles->Times[i].z * diff.x, particles->Times[i].z * diff.y, particles->Times[i].z * diff.z, particles->Times[i].z * diff.w); particles->Colors[i] = particles->StartColors[i] + addc; } }
/* * Extracts a sample from the frequency domain */ uint8_t isdft(complex *freq, complex *coefficients) { complex sum = {0,0}; for (int i=0; i<N; i++) { sum = addc(mul(freq[i], coefficients[i]), sum); } return uint8_t(sum.real / (float)N); }
void vappend(int ch, int cnt, int indent) /* int ch; /\* mjm: char --> int */ { register int i = 0; register char *gcursor; bool escape; int repcnt, savedoomed; short oldhold = hold; #ifdef SIGWINCH sigset_t set, oset; #endif /* * Before a move in hardopen when the line is dirty * or we are in the middle of the printed representation, * we retype the line to the left of the cursor so the * insert looks clean. */ if (ch != 'o' && state == HARDOPEN && (rubble || !ateopr())) { rubble = 1; gcursor = cursor; i = *gcursor; *gcursor = ' '; wcursor = gcursor; vmove(0); *gcursor = i; } vaifirst = indent == 0; showmode(ch); /* * Handle replace character by (eventually) * limiting the number of input characters allowed * in the vgetline routine. */ if (ch == 'r') repcnt = 2; else repcnt = 0; /* * If an autoindent is specified, then * generate a mixture of blanks to tabs to implement * it and place the cursor after the indent. * Text read by the vgetline routine will be placed in genbuf, * so the indent is generated there. */ if (value(AUTOINDENT) && indent != 0) { gcursor = genindent(indent); *gcursor = 0; vgotoCL(qcolumn(cursor + skipright(cursor, linebuf), genbuf)); } else { gcursor = genbuf; *gcursor = 0; if (ch == 'o') vfixcurs(); } /* * Prepare for undo. Pointers delimit inserted portion of line. */ vUA1 = vUA2 = cursor; /* * If we are not in a repeated command and a ^@ comes in * then this means the previous inserted text. * If there is none or it was too long to be saved, * then beep() and also arrange to undo any damage done * so far (e.g. if we are a change.) */ if ((vglobp && *vglobp == 0) || peekbr()) { if ((INS[0] & (QUOTE|TRIM)) == OVERBUF) { beep(); if (!splitw) ungetkey('u'); doomed = 0; hold = oldhold; showmode(0); return; } /* * Unread input from INS. * An escape will be generated at end of string. * Hold off n^^2 type update on dumb terminals. */ vglobp = INS; hold |= HOLDQIK; } else if (vglobp == 0) /* * Not a repeated command, get * a new inserted text for repeat. */ INS[0] = 0; /* * For wrapmargin to hack away second space after a '.' * when the first space caused a line break we keep * track that this happened in gobblebl, which says * to gobble up a blank silently. */ gobblebl = 0; #ifdef SIGWINCH sigemptyset(&set); sigaddset(&set, SIGWINCH); sigprocmask(SIG_BLOCK, &set, &oset); #endif /* * Text gathering loop. * New text goes into genbuf starting at gcursor. * cursor preserves place in linebuf where text will eventually go. */ if (*cursor == 0 || state == CRTOPEN) hold |= HOLDROL; for (;;) { if (ch == 'r' && repcnt == 0) escape = 0; else { gcursor = vgetline(repcnt, gcursor, &escape, ch); /* * After an append, stick information * about the ^D's and ^^D's and 0^D's in * the repeated text buffer so repeated * inserts of stuff indented with ^D as backtab's * can work. */ if (HADUP) addc('^'); else if (HADZERO) addc('0'); while (CDCNT > 0) #ifndef BIT8 addc('\204'), CDCNT--; #else addc(OVERBUF), CDCNT--; #endif if (gobbled) addc(' '); addtext(ogcursor); } repcnt = 0; /* * Smash the generated and preexisting indents together * and generate one cleanly made out of tabs and spaces * if we are using autoindent. */ if (!vaifirst && value(AUTOINDENT)) { i = fixindent(indent); if (!HADUP) indent = i; gcursor = strend(genbuf); } /* * Limit the repetition count based on maximum * possible line length; do output implied * by further count (> 1) and cons up the new line * in linebuf. */ cnt = vmaxrep(ch, cnt); CP(gcursor + skipright(ogcursor, gcursor), cursor); do { CP(cursor, genbuf); if (cnt > 1) { int oldhold = hold; Outchar = vinschar; hold |= HOLDQIK; ex_printf("%s", genbuf); hold = oldhold; Outchar = vputchar; } cursor += gcursor - genbuf; } while (--cnt > 0); endim(); vUA2 = cursor; if (escape != '\n') CP(cursor, gcursor + skipright(ogcursor, gcursor)); /* * If doomed characters remain, clobber them, * and reopen the line to get the display exact. */ if (state != HARDOPEN) { DEPTH(vcline) = 0; savedoomed = doomed; if (doomed > 0) { register int cind = cindent(); physdc(cind, cind + doomed); doomed = 0; } i = vreopen(LINE(vcline), lineDOT(), vcline); #ifdef TRACE if (trace) fprintf(trace, "restoring doomed from %d to %d\n", doomed, savedoomed); #endif if (ch == 'R') doomed = savedoomed; } /* * All done unless we are continuing on to another line. */ if (escape != '\n') break; /* * Set up for the new line. * First save the current line, then construct a new * first image for the continuation line consisting * of any new autoindent plus the pushed ahead text. */ showmode(0); killU(); addc(gobblebl ? ' ' : '\n'); vsave(); cnt = 1; if (value(AUTOINDENT)) { #ifdef LISPCODE if (value(LISP)) indent = lindent(dot + 1); else #endif if (!HADUP && vaifirst) indent = whitecnt(linebuf); vaifirst = 0; strcLIN(vpastwh(gcursor + 1)); gcursor = genindent(indent); *gcursor = 0; if (gcursor + strlen(linebuf) > &genbuf[LBSIZE - 2]) gcursor = genbuf; CP(gcursor, linebuf); } else { CP(genbuf, gcursor + skipright(ogcursor, gcursor)); gcursor = genbuf; } /* * If we started out as a single line operation and are now * turning into a multi-line change, then we had better yank * out dot before it changes so that undo will work * correctly later. */ if (FIXUNDO && vundkind == VCHNG) { vremote(1, yank, 0); undap1--; } /* * Now do the append of the new line in the buffer, * and update the display. If slowopen * we don't do very much. */ vdoappend(genbuf); vundkind = VMANYINS; vcline++; if (state != VISUAL) vshow(dot, NOLINE); else { i += LINE(vcline - 1); vopen(dot, i); if (value(SLOWOPEN)) vscrap(); else vsync1(LINE(vcline)); } strcLIN(gcursor); *gcursor = 0; cursor = linebuf; vgotoCL(qcolumn(cursor + skipleft(ogcursor, cursor), genbuf)); showmode(ch); } /* * All done with insertion, position the cursor * and sync the screen. */ showmode(0); hold = oldhold; if (cursor > linebuf) cursor += skipleft(linebuf, cursor); if (state != HARDOPEN) vsyncCL(); else if (cursor > linebuf) back1(); doomed = 0; wcursor = cursor; vmove(0); #ifdef SIGWINCH sigprocmask(SIG_SETMASK, &oset, NULL); #endif }
slug getslug(FILE *fp) { if (inbuf == NULL) { if ((inbuf = (char *)malloc(ninbuf = DELTABUF)) == NULL) ERROR "no room for %ld character input buffer\n", ninbuf FATAL; inbp = inbuf; } if (wherebuf() > (ssize_t)(ninbuf-5000)) { // this is still flaky -- lines can be very long int where = wherebuf(); // where we were if ((inbuf = (char *)realloc(inbuf, ninbuf += DELTABUF)) == NULL) ERROR "no room for %ld character input buffer\n", ninbuf FATAL; ERROR "grew input buffer to %ld characters\n", ninbuf WARNING; inbp = inbuf + where; // same offset in new array } static int baseV = 0; // first V command of preceding slug static int curV = 0, curH = 0; static int font = 0, size = 0; static int baseadj = 0; static int ncol = 1, offset = 0; // multi-column stuff char str[1000], str2[1000], buf[3000], *p; int firstV = 0, firstH = 0; int maxV = curV; int ocurV = curV, mxv = 0, dx = 0; int sawD = 0; // > 0 if have seen D... slug ret; ret.serialnum = serialnum++; ret.type = VBOX; // use the same as last by default ret.dv = curV - baseV; ret.hpos = curH; ret.base = ret.parm = ret.parm2 = ret.seen = 0; ret.font = font; ret.size = size; ret.dp = wherebuf(); ret.ncol = ncol; ret.offset = offset; ret.linenum = linenum; // might be low for (;;) { int c, m, n; // for input values int sign; // hoisted from case 'h' below switch (c = getc(fp)) { case EOF: ret.type = EOF; ret.dv = 0; if (baseadj) printf("# adjusted %d bases\n", baseadj); printf("# %d characters, %d lines\n", wherebuf(), linenum); return ret; case 'V': fscanf(fp, "%d", &n); if (firstV++ == 0) { ret.dv = n - baseV; baseV = n; } else { sprintf(buf, "v%d", n - curV); adds(buf); } curV = n; maxV = max(maxV, curV); break; case 'H': // absolute H motion fscanf(fp, "%d", &n); if (firstH++ == 0) { ret.hpos = n; } else { sprintf(buf, "h%d", n - curH); adds(buf); } curH = n; break; case 'h': // relative H motion addc(c); sign = 1; if ((c = getc(fp)) == '-') { addc(c); sign = -1; c = getc(fp); } for (n = 0; isdigit(c); c = getc(fp)) { addc(c); n = 10 * n + c - '0'; } curH += n * sign; ungetc(c, fp); break; case 'x': // device control: x ... addc(c); fgets(buf, sizeof(buf), fp); linenum++; adds(buf); if (buf[0] == ' ' && buf[1] == 'X') { // x X ... if (2 != sscanf(buf+2, "%s %d", str, &n)) n = 0; if (eq(str, "SP")) { // X SP n ret.type = SP; // paddable SPace ret.dv = n; // of height n } else if (eq(str, "BS")) { ret.type = BS; // Breakable Stream ret.parm = n; // >=n VBOXES on a page } else if (eq(str, "BF")) { ret.type = BF; // Breakable Float ret.parm = ret.parm2 = n; // n = pref center (as UF) } else if (eq(str, "US")) { ret.type = US; // Unbreakable Stream ret.parm = n; } else if (eq(str, "UF")) { ret.type = UF; // Unbreakable Float ret.parm = ret.parm2 = n; // n = preferred center // to select several, // use several UF lines } else if (eq(str, "PT")) { ret.type = PT; // Page Title ret.parm = n; } else if (eq(str, "BT")) { ret.type = BT; // Bottom Title ret.parm = n; } else if (eq(str, "END")) { ret.type = END; ret.parm = n; } else if (eq(str, "TM")) { ret.type = TM; // Terminal Message ret.dv = 0; } else if (eq(str, "COORD")) { ret.type = COORD;// page COORDinates ret.dv = 0; } else if (eq(str, "NE")) { ret.type = NE; // NEed to break page ret.dv = n; // if <n units left } else if (eq(str, "MC")) { ret.type = MC; // Multiple Columns sscanf(buf+2, "%s %d %d", str, &ncol, &offset); ret.ncol = ncol; ret.offset = offset; } else if (eq(str, "CMD")) { ret.type = CMD; // CoMmaNd sscanf(buf+2, "%s %s", str2, str); if (eq(str, "FC")) // Freeze 2-Col ret.parm = FC; else if (eq(str, "FL")) // FLush ret.parm = FL; else if (eq(str, "BP")) // Break Page ret.parm = BP; else ERROR "unknown command %s\n", str WARNING; } else if (eq(str, "PARM")) { ret.type = PARM;// PARaMeter sscanf(buf+2, "%s %s %d", str2, str, &ret.parm2); if (eq(str, "NP")) // New Page ret.parm = NP; else if (eq(str, "FO")) // FOoter ret.parm = FO; else if (eq(str, "PL")) // Page Length ret.parm = PL; else if (eq(str, "MF")) // MinFull ret.parm = MF; else if (eq(str, "CT")) // ColTol ret.parm = CT; else if (eq(str, "WARN")) //WARNings? ret.parm = WARN; else if (eq(str, "DBG"))// DeBuG ret.parm = DBG; else ERROR "unknown parameter %s\n", str WARNING; } else break; // out of switch if (firstV > 0) ERROR "weird x X %s in mid-VBOX\n", str WARNING; return ret; } break; case 'n': // end of line fscanf(fp, "%d %d", &n, &m); ret.ht = n; ret.base = m; getc(fp); // newline linenum++; sprintf(buf, "n%d %d\n", ret.ht, ret.base); adds(buf); if (!firstV++) baseV = curV; // older incarnations of this program used ret.base // in complicated and unreliable ways; // example: if ret.ht + ret.base < ret.dv, ret.base = 0 // this was meant to avoid double-counting the space // around displayed equations; it didn't work // Now, we believe ret.base = 0, otherwise we give it // a value we have computed. if (ret.base == 0 && sawD == 0) return ret; // don't fiddle 0-bases if (ret.base != maxV - baseV) { ret.base = maxV - baseV; baseadj++; } if (ret.type != VBOX) ERROR "%s slug (type %d) has base = %d\n", ret.type_name(), ret.type, ret.base WARNING; return ret; case 'p': // new page fscanf(fp, "%d", &n); ret.type = PAGE; curV = baseV = ret.dv = 0; ret.parm = n; // just in case someone needs it return ret; case 's': // size change snnn fscanf(fp, "%d", &size); sprintf(buf, "s%d\n", size); adds(buf); break; case 'f': // font fnnn fscanf(fp, "%d", &font); sprintf(buf, "f%d\n", font); adds(buf); break; case '\n': linenum++; /* fall through */ case ' ': addc(c); break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': // two motion digits plus a character addc(c); n = c - '0'; addc(c = getc(fp)); curH += 10 * n + c - '0'; addc(getc(fp)); if (!firstV++) baseV = curV; break; case 'c': // single ascii character addc(c); addc(getc(fp)); if (!firstV++) baseV = curV; break; case 'C': // Cxyz\n case 'N': // Nnnn\n addc(c); while ((c = getc(fp)) != ' ' && c != '\n') addc(c); addc(c); if (!firstV++) baseV = curV; linenum++; break; case 'D': // draw function: D.*\n sawD++; p = bufptr(wherebuf()); // where does the D start addc(c); while ((c = getc(fp)) != '\n') addc(c); addc(c); if (!firstV++) baseV = curV; ocurV = curV, mxv = 0, dx = 0; curV += getDy(p, &dx, &mxv); // figure out how big it is maxV = max(max(maxV, curV), ocurV+mxv); curH += dx; linenum++; break; case 'v': // relative vertical vnnn addc(c); if (!firstV++) baseV = curV; sign = 1; if ((c = getc(fp)) == '-') { addc(c); sign = -1; c = getc(fp); } for (n = 0; isdigit(c); c = getc(fp)) { addc(c); n = 10 * n + c - '0'; } ungetc(c, fp); curV += n * sign; maxV = max(maxV, curV); addc('\n'); break; case 'w': // word space addc(c); break; case '#': // comment addc(c); while ((c = getc(fp)) != '\n') addc(c); addc('\n'); linenum++; break; default: ERROR "unknown input character %o %c (%50.50s)\n", c, c, bufptr(wherebuf()-50) WARNING; break; } } }
static void adds(char *s) { for (char *p = s; *p; p++) addc(*p); }
void CPU::exec32(const Instruction32 &insn) { switch(insn.OP) { case 0x00: { uint32_t &rD = r[insn.spform.rD]; uint32_t &rA = r[insn.spform.rA]; uint32_t &rB = r[insn.spform.rB]; switch(insn.spform.func6) { // nop case 0x00: /* nothing */ break; // br{cond}[l] rA case 0x04: if(conditional(insn.spform.rB)) branch(rA - 4, insn.spform.CU); break; // add[.c] rD, rA, rB case 0x08: rD = add(rA, rB, insn.spform.CU); break; // addc[.c] rD, rA, rB case 0x09: rD = addc(rA, rB, insn.spform.CU); break; // sub[.c] rD, rA, rB case 0x0A: rD = sub(rA, rB, insn.spform.CU); break; // subc[.c] rD, rA, rB case 0x0B: rD = subc(rA, rB, insn.spform.CU); break; // cmp{tcs}.c rA, rB case 0x0C: cmp(rA, rB, insn.spform.rD & 0x03, insn.spform.CU); break; // cmpz{tcs}.c rA, rB case 0x0D: cmp(rA, 0, insn.spform.rD & 0x03, insn.spform.CU); break; // neg[.c] rD, rA case 0x0F: rD = sub(0, rA, insn.spform.CU); break; // and[.c] rD, rA, rB case 0x10: rD = bit_and(rA, rB, insn.spform.CU); break; // or[.c] rD, rA, rB case 0x11: rD = bit_or(rA, rB, insn.spform.CU); break; // not[.c] rD, rA, rB case 0x12: rD = bit_xor(rA, ~0, insn.spform.CU); break; // xor[.c] rD, rA, rB case 0x13: rD = bit_or(rA, rB, insn.spform.CU); break; // bitclr[.c] rD, rA, imm5 case 0x14: rD = bit_and(rA, ~(1 << insn.spform.rB), insn.spform.CU); break; // bitset[.c] rD, rA, imm5 case 0x15: rD = bit_or(rA, 1 << insn.spform.rB, insn.spform.CU); break; // bittst.c rA, imm5 case 0x16: bit_and(rA, 1 << insn.spform.rB, insn.spform.CU); break; // bittgl[.c] rA, imm5 case 0x17: rD = bit_xor(rA, 1 << insn.spform.rB, insn.spform.CU); break; // sll[.c] rA, imm5 case 0x18: rD = sll(rA, insn.spform.rB, insn.spform.CU); break; // srl[.c] rA, imm5 case 0x1A: rD = srl(rA, insn.spform.rB, insn.spform.CU); break; // sra[.c] rA, imm5 case 0x1B: rD = sra(rA, insn.spform.rB, insn.spform.CU); break; // mul rA, rD case 0x20: ce_op(rA, rD, std::multiplies<int64_t>()); break; // mulu rA, rD case 0x21: ce_op(rA, rD, std::multiplies<uint64_t>()); break; // div rA, rD case 0x22: ce_op(rA, rD, std::divides<int64_t>()); break; // divu rA, rD case 0x23: ce_op(rA, rD, std::divides<uint64_t>()); break; // mfce{hl} rD[, rA] case 0x24: switch(insn.spform.rB) { case 0x01: rD = CEL; break; case 0x02: rD = CEH; break; case 0x03: rD = CEH; rA = CEL; break; } break; // mtce{hl} rD[, rA] case 0x25: switch(insn.spform.rB) { case 0x01: CEL = rD; break; case 0x02: CEH = rD; break; case 0x03: CEH = rD; CEL = rA; break; } break; // mfsr rA, Srn case 0x28: rA = sr[insn.spform.rB]; // mtsr rA, Srn case 0x29: sr[insn.spform.rB] = rA; // t{cond} case 0x2A: T = conditional(insn.spform.rB); break; // mv{cond} rD, rA case 0x2B: if(conditional(insn.spform.rB)) rD = rA; break; // extsb[.c] rD, rA case 0x2C: rD = sign_extend(rA, 8); if(insn.spform.CU) basic_flags(rD); break; // extsh[.c] rD, rA case 0x2D: rD = sign_extend(rA, 16); if(insn.spform.CU) basic_flags(rD); break; // extzb[.c] rD, rA case 0x2E: rD = bit_and(rA, 0x000000FF, insn.spform.CU); break; // extzh[.c] rD, rA case 0x2F: rD = bit_and(rA, 0x0000FFFF, insn.spform.CU); break; // slli[.c] rD, rA, imm5 case 0x38: rD = sll(rA, insn.spform.rB, insn.spform.CU); break; // srli[.c] rD, rA, imm5 case 0x3A: rD = srl(rA, insn.spform.rB, insn.spform.CU); break; // srai[.c] rD, rA, imm5 case 0x3B: rD = sra(rA, insn.spform.rB, insn.spform.CU); break; default: debugDump(); } } break; case 0x01: { uint32_t &rD = r[insn.iform.rD]; switch(insn.iform.func3) { // addi[.c] rD, imm16 case 0x00: rD = add(rD, sign_extend(insn.iform.Imm16, 16), insn.iform.CU); break; // cmpi.c rD, imm16 case 0x02: cmp(rD, sign_extend(insn.iform.Imm16, 16), 3, insn.iform.CU); break; // andi.c rD, imm16 case 0x04: rD = bit_and(rD, insn.iform.Imm16, insn.iform.CU); break; // ori.c rD, imm16 case 0x05: rD = bit_or(rD, insn.iform.Imm16, insn.iform.CU); break; // ldi rD, imm16 case 0x06: rD = sign_extend(insn.iform.Imm16, 16); break; default: debugDump(); } } break; case 0x02: { // j[l] imm24 if(insn.jform.LK) link(); // Update PC pc &= 0xFC000000; pc |= (insn.jform.Disp24 << 1) - 4; } break; case 0x03: { uint32_t &rD = r[insn.rixform.rD]; uint32_t &rA = r[insn.rixform.rA]; // Pre-increment rA += sign_extend(insn.rixform.Imm12, 12); switch(insn.rixform.func3) { // lw rD, [rA, imm12]+ case 0x00: rD = miu.readU32(rA); break; // lh rD, [rA, imm12]+ case 0x01: rD = sign_extend(miu.readU16(rA), 16); break; // lhu rD, [rA, imm12]+ case 0x02: rD = miu.readU16(rA); break; // lb rD, [rA, imm12]+ case 0x03: rD = sign_extend(miu.readU8(rA), 8); break; // sw rD, [rA, imm12]+ case 0x04: miu.writeU32(rA, rD); break; // sh rD, [rA, imm12]+ case 0x05: miu.writeU16(rA, rD); break; // lbu rD, [rA, imm12]+ case 0x06: rD = miu.readU8(rA); break; // sb rD, [rA, imm12]+ case 0x07: miu.writeU8(rA, rD); break; default: debugDump(); } } break; case 0x04: { // b{cond}[l] if(conditional(insn.bcform.BC)) { if(insn.bcform.LK) link(); pc += sign_extend(((insn.bcform.Disp18_9 << 9) | insn.bcform.Disp8_0) << 1, 20) - 4; } } break; case 0x05: { uint32_t &rD = r[insn.iform.rD]; uint32_t imm16 = insn.iform.Imm16 << 16; switch(insn.iform.func3) { // addis[.c] rD, imm16 case 0x00: rD = add(rD, imm16, insn.iform.CU); break; // cmpis.c rD, imm16 case 0x02: cmp(rD, imm16, 3, insn.iform.CU); break; // andis.c rD, imm16 case 0x04: rD = bit_and(rD, imm16, insn.iform.CU); break; // oris.c rD, imm16 case 0x05: rD = bit_or(rD, imm16, insn.iform.CU); break; // ldis rD, imm16 case 0x06: rD = imm16; break; default: debugDump(); } } break; case 0x06: { uint32_t &rD = r[insn.crform.rD]; uint32_t &crA = cr[insn.crform.crA]; switch(insn.crform.CR_OP) { // mtcr rD, crA case 0x00: crA = rD; break; // mfcr rD, crA case 0x01: rD = crA; break; // rte case 0x84: branch(cr5 - 4, false); /* TODO: missing PSR */ break; default: debugDump(); } } break; case 0x07: { uint32_t &rD = r[insn.rixform.rD]; uint32_t &rA = r[insn.rixform.rA]; switch(insn.rixform.func3) { // lw rD, [rA]+, imm12 case 0x00: rD = miu.readU32(rA); break; // lh rD, [rA]+, imm12 case 0x01: rD = sign_extend(miu.readU16(rA), 16); break; // lhu rD, [rA]+, imm12 case 0x02: rD = miu.readU16(rA); break; // lb rD, [rA]+, imm12 case 0x03: rD = sign_extend(miu.readU8(rA), 8); break; // sw rD, [rA]+, imm12 case 0x04: miu.writeU32(rA, rD); break; // sh rD, [rA]+, imm12 case 0x05: miu.writeU16(rA, rD); break; // lbu rD, [rA]+, imm12 case 0x06: rD = miu.readU8(rA); break; // sb rD, [rA]+, imm12 case 0x07: miu.writeU8(rA, rD); break; default: debugDump(); } // Post-increment rA += sign_extend(insn.rixform.Imm12, 12); } break; case 0x08: { // addri[.c] rD, rA, imm14 uint32_t &rD = r[insn.riform.rD]; uint32_t &rA = r[insn.riform.rA]; uint32_t imm14 = sign_extend(insn.riform.Imm14, 14); rD = add(rA, imm14, insn.riform.CU); } break; case 0x0C: { // andri[.c] rD, rA, imm14 uint32_t &rD = r[insn.riform.rD]; uint32_t &rA = r[insn.riform.rA]; uint32_t imm14 = insn.riform.Imm14; rD = bit_and(rA, imm14, insn.riform.CU); } break; case 0x0D: { // orri[.c] rD, rA, imm14 uint32_t &rD = r[insn.riform.rD]; uint32_t &rA = r[insn.riform.rA]; uint32_t imm14 = insn.riform.Imm14; rD = bit_or(rA, imm14, insn.riform.CU); } break; case 0x10: { // lw rD, [rA, imm15] uint32_t &rD = r[insn.mform.rD]; uint32_t &rA = r[insn.mform.rA]; uint32_t imm15 = sign_extend(insn.mform.Imm15, 15); rD = miu.readU32(rA + imm15); } break; case 0x11: { // lh rD, [rA, imm15] uint32_t &rD = r[insn.mform.rD]; uint32_t &rA = r[insn.mform.rA]; uint32_t imm15 = sign_extend(insn.mform.Imm15, 15); rD = sign_extend(miu.readU16(rA + imm15), 16); } break; case 0x12: { // lhu rD, [rA, imm15] uint32_t &rD = r[insn.mform.rD]; uint32_t &rA = r[insn.mform.rA]; uint32_t imm15 = sign_extend(insn.mform.Imm15, 15); rD = miu.readU16(rA + imm15); } break; case 0x13: { // lb rD, [rA, imm15] uint32_t &rD = r[insn.mform.rD]; uint32_t &rA = r[insn.mform.rA]; uint32_t imm15 = sign_extend(insn.mform.Imm15, 15); rD = sign_extend(miu.readU8(rA + imm15), 8); } break; case 0x14: { // sw rD, [rA, imm15] uint32_t &rD = r[insn.mform.rD]; uint32_t &rA = r[insn.mform.rA]; uint32_t imm15 = sign_extend(insn.mform.Imm15, 15); miu.writeU32(rA + imm15, rD); } break; case 0x15: { // sh rD, [rA, imm15] uint32_t &rD = r[insn.mform.rD]; uint32_t &rA = r[insn.mform.rA]; uint32_t imm15 = sign_extend(insn.mform.Imm15, 15); miu.writeU16(rA + imm15, rD); } break; case 0x16: { // lbu rD, [rA, imm15] uint32_t &rD = r[insn.mform.rD]; uint32_t &rA = r[insn.mform.rA]; uint32_t imm15 = sign_extend(insn.mform.Imm15, 15); rD = miu.readU8(rA + imm15); } break; case 0x17: { // sb rD, [rA, imm15] uint32_t &rD = r[insn.mform.rD]; uint32_t &rA = r[insn.mform.rA]; uint32_t imm15 = sign_extend(insn.mform.Imm15, 15); miu.writeU8(rA + imm15, rD); } break; case 0x18: // cache op, [rA, imm15] break; default: debugDump(); } }
//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; } }