prog.c (1663B)
1 #include <u.h> 2 #include <libc.h> 3 4 #include "prog.h" 5 6 7 void 8 interreset(Inter *inter) 9 { 10 inter->PC = 0; 11 inter->state = InLoad; 12 memset(inter->cmd, 0, 16); 13 memset(inter->mod, 0, sizeof(s16int) * 4); 14 } 15 16 void 17 interstep(Inter *inter, int flags) 18 { 19 switch (inter->state) { 20 case InHalt: 21 return; 22 case InRepeat: 23 interexec(inter, flags); 24 break; 25 case InLoad: 26 memcpy(inter->cmd, inter->progstart + (inter->PC * 16), 16); 27 inter->state = InExec; 28 case InExec: 29 interexec(inter, flags); 30 if (inter->state == InExec) inter->state = InLoad; 31 inter->PC++; 32 break; 33 } 34 if (inter->PC >= ProgMax) { 35 inter->PC = 0; 36 inter->state = InHalt; 37 }; 38 } 39 40 void 41 interexec(Inter *inter, int flag) 42 { 43 int i; 44 for (i = 0; i < 16; i += 4) { 45 Instruction *I = (Instruction *)(inter->cmd + i); 46 if (I->m == ModCtl) { 47 switch (I->op) { 48 case CtlHalt: 49 inter->state = InHalt; 50 break; 51 case CtlRepeat: 52 if (I->arg > 0) { 53 inter->state = InRepeat; 54 I->arg--; 55 } else inter->state = InLoad; 56 break; 57 case CtlHoldJmp: 58 if (flag & FHold) inter->PC = I->arg - 1; 59 break; 60 case CtlJmp: 61 inter->PC = I->arg - 1; 62 break; 63 default: /* not recognized */ 64 break; 65 } 66 } else interinstr(inter, I); 67 } 68 } 69 70 void 71 interinstr(Inter *inter, Instruction *I) 72 { 73 int m; 74 switch (I->m) { 75 default: /* invalid mod index */ 76 case 0: /* noop */ 77 return; 78 case 1: 79 case 2: 80 case 3: 81 case 4: 82 case 5: 83 case 6: 84 m = I->m - 1; 85 break; 86 } 87 switch (I->op) { 88 case ModOpAbs: 89 inter->mod[m] = I->arg; 90 break; 91 case ModOpInc: 92 inter->mod[m] += I->arg; 93 break; 94 case ModOpDec: 95 inter->mod[m] -= I->arg; 96 break; 97 default: /* not recognized */ 98 return; 99 } 100 }