ref: 67f1be8c8bf3f883569a4a39fd3f15c5b543414d
dir: /5551e.c/
/* 5551 cpu emulator */ #include <stdbool.h> #include <stdio.h> #include "5551.h" typedef struct { bool flags[8]; int regs[8]; }reg_t; int btoi(bool b[], int len) { char arr[10]; int res = 0, i = 0, j = len; while (i <= len) { arr[i] = b[j] ? '1' : '0'; i++; j--; } sscanf(arr, "%d", &res); return res; } int main() { reg_t r; unsigned int memory[MMAX]; memory[MMAX-1] = -1; char string[16]; r.flags[ON] = 1; r.flags[NSIGN] = 0; unsigned int op, a, b, c, ind = 0; bool barr[10]; /* there is no space between op and args since some can have no args */ while(r.flags[ON] && scanf("%u %[^\n]", &op, string)) { sscanf(string, "%u %u %u", &a, &b, &c); switch(op) { case DIE: r.flags[ON] = 0; break; case OUT: /* just in case output is "manfi" */ if(r.flags[NSIGN]) printf("-%u\n", r.regs[a]); else printf("%u\n", r.regs[a]); break; case NOT: r.regs[b] = !a; break; case AND: r.regs[c] = a & b; break; case NOR: r.regs[c] = !(a | b); break; case XOR: r.regs[c] = a ^ b; break; case SUB: b = ~b; r.regs[CARRY] = 1; r.flags[NSIGN] = (a < b); r.regs[c] = r.flags[NSIGN] ? b - a : a - b; case ADD: r.flags[NSIGN] = false; r.regs[c] = a + b; break; r.flags[NSIGN] = false; r.regs[b] = a + 1; break; case DEC: r.regs[b] = a - 1; r.flags[NSIGN] = (r.regs[b] < 0); break; case SHR: r.regs[c] = a >> b; break; case SHL: r.regs[c] = a << b; break; case PUT: memory[b] = a; case MOV: memory[b] = r.regs[a]; break; case LDM: r.regs[b] = memory[a]; break; case NTH: r.regs[c] = a >> b; break; default: fprintf(stderr, "unknown op\n"); return 1; } /* clear them */ a = 0; b = 0; op = 0; string[0] = '\0'; } return 0; }