src/vm.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
/* 3ByteBadVM: a bad virtual machine using 3-byte instructions x1phosura 2021 VM specifications TODO: document VM stuff here once I write it in Markdown use assert()s to make sure pc, sp, etc... are valid */ #include <assert.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef TRACE // readline is L I T #include <readline/readline.h> #include <readline/history.h> #endif #include "vm.h" #ifdef TRACE // TRACE_VARS struct TRACE_T trace_state; char *trace_bad_cmd = "Unrecognized command.\n"; char *trace_cmd_help = "Supported commands are:\n" "s: step single instruction\n" "c: continue code execution until breakpoint or end\n" "b <addr>: set breakpoint at given memory address\n" "pb: print current breakpoints\n" "pr: print register contents\n" "ps: print stack contents\n" "pm <addr> <num>: print <num> bytes of memory starting at <addr>\n" "pz: print zero page (1st 256 bytes)\n" "h: print VM debugger command help message\n" "q: quit VM debugger\n"; #endif // TRACE_VARS static inline void push(struct CPU *cpu, uint8_t val) { cpu->stack[cpu->sp] = val; if (cpu->sp < STACK_LIM-1) ++cpu->sp; } static inline uint8_t pop(struct CPU *cpu) { uint8_t val; if (cpu->sp > 0) { val = cpu->stack[cpu->sp-1]; --cpu->sp; } else return 0; return val; } /* vm_do_instruction: */ uint16_t vm_do_instruction(struct CPU *cpu, uint8_t *mem, uint8_t instr[3]) { uint16_t pc = cpu->pc; uint16_t sp = cpu->sp; uint8_t val = 0; uint8_t operands[2]; uint8_t opcode = instr[0]; operands[0] = instr[1]; operands[1] = instr[2]; // vvv- this format needed if operands treated as single 16-bit value uint16_t operand = ((uint16_t)instr[2] * 256) + instr[1]; #ifdef TRACE printf("0x%04x: ", pc); print_op_decoded(instr, true); #endif /* Note: the below code will likely be very unsafe. */ switch (opcode) { case HALT: cpu->state = STOPPED; break; case PUSH: push(cpu, mem[operand]); pc += 3; break; case POP: mem[operand] = pop(cpu); pc += 3; break; case PUSHI: push(cpu, operands[0]); pc += 3; break; case LDLR: // TODO: fix!! This is broken // read little-endian cpu->lr = (uint16_t)(mem[operand+1]); // set MSB cpu->lr = cpu->lr << 8; // set MSB cpu->lr += (uint16_t)(mem[operand]); // set LSB pc += 3; break; case STLR: // write little-endian mem[operand] = (uint8_t)((cpu->lr) & 0x00ff); // set LSB mem[operand+1] = (uint8_t)((cpu->lr >> 8) & 0x00ff); // set MSB pc += 3; break; case SETI: // peek ToS, write to memory mem[operand] = cpu->stack[sp-1]; pc += 3; break; case DUP: val = cpu->stack[sp-1]; push(cpu, val); pc += 3; break; case ADD: val = pop(cpu); val += pop(cpu); push(cpu, val); pc += 3; break; case SUB: val = pop(cpu); val = ~val; ++val; val += pop(cpu); push(cpu, val); // subtract 2nd from top by top of stack pc += 3; break; case XOR: val = pop(cpu); val ^= pop(cpu); push(cpu, val); pc += 3; break; case CALL: cpu->lr = pc+3; return operand; break; case RET: return cpu->lr; break; case JMP: return operand; break; case BEQ: if (sp > 1) // if 2 or more items on stack if (sp > 0 && cpu->stack[sp-1] == cpu->stack[sp-2]) return operand; pc += 3; break; case BNQ: if (sp > 1) // if 2 or more items on stack if (sp > 0 && cpu->stack[sp-1] != cpu->stack[sp-2]) return operand; pc += 3; break; case NOP: pc += 3; break; default: /* EVIL idea: have illegal instructions act as NOPs! */ pc += 3; } return pc; } static inline void vm_fetch_instruction(uint16_t pc, uint8_t *mem, uint8_t instr[3]) { assert(pc < RAM_SIZE - 3); instr[0] = mem[pc]; instr[1] = mem[pc+1]; instr[2] = mem[pc+2]; // finish instruction fetch } #ifdef TRACE // TRACE_FUNCS void print_op_decoded(uint8_t i[3], bool pargs) { char fmt[] = " 0x%02x 0x%02x"; uint8_t opcode = i[0]; switch(opcode) { case HALT: printf("HALT"); break; case PUSH: printf("PUSH"); if (pargs) printf(fmt, i[1], i[2]); break; case POP: printf("POP"); if (pargs) printf(fmt, i[1], i[2]); break; case PUSHI: printf("PUSHI"); if (pargs) printf(fmt, i[1], i[2]); break; case LDLR: printf("LDLR"); if (pargs) printf(fmt, i[1], i[2]); break; case STLR: printf("STLR"); if (pargs) printf(fmt, i[1], i[2]); break; case SETI: printf("SETI"); if (pargs) printf(fmt, i[1], i[2]); break; case DUP: printf("DUP"); break; case ADD: printf("ADD"); break; case SUB: printf("SUB"); break; case XOR: printf("XOR"); break; case CALL: printf("CALL"); if (pargs) printf(fmt, i[1], i[2]); break; case RET: printf("RET"); break; case JMP: printf("JMP"); if (pargs) printf(fmt, i[1], i[2]); break; case BEQ: printf("BEQ"); if (pargs) printf(fmt, i[1], i[2]); break; case BNQ: printf("BNQ"); if (pargs) printf(fmt, i[1], i[2]); break; case NOP: printf("NOP"); break; default: printf("0x%02x 0x%02x 0x%02x", i[0], i[1], i[2]); } putchar('\n'); } void print_vm_registers(struct CPU *cpu) { uint16_t pc = cpu->pc; uint16_t lr = cpu->lr; uint16_t sp = cpu->sp; //uint16_t a = cpu->a; printf("pc = %#x, lr = %#x, sp = %#x\n", pc, lr, sp); } void print_vm_stack(struct CPU *cpu) { uint16_t sp = cpu->sp; for (uint8_t i = 0; i < sp; ++i) { if (i % 16 == 0) putchar('\n'); printf("%02x ", cpu->stack[i]); } putchar('\n'); for (int8_t j = 0; j < sp % 17; ++j) printf(" "); printf("^sp = 0x%02x\n", sp); } void print_vm_memory(uint8_t *mem, uint16_t start_addr, uint16_t num_bytes) { for (uint16_t i = 0; i < num_bytes; ++i) { if (i % 16 == 0) printf("\n0x%04x: ", start_addr+i); printf("%02x ", mem[start_addr + i]); } putchar('\n'); } void vm_trace(struct CPU *cpu, uint8_t *mem, struct TRACE_T *tstate) { char *command; //char command[24]; int mem_start = 0; int num_bytes = 0; rl_bind_key('\t', rl_insert); // make readline treat tabs and tabs if (cpu->pc == tstate->breakpoint) { printf("Breakpoint reached!\n"); tstate->mode = STEP; } while(tstate->mode == STEP) { if ((command = readline("trace> ")) == NULL) { printf("Error: readline returned NULL. Aborting...\n"); exit(0); } // Note: technically, command should eventually be freed, but // it's not actually that important here, and the OS will // reclaim heap space when the program finishes anyway. if (strlen(command) > 0) add_history(command); // yeah, yeah, I KNOW a switch-case would be better for this if (command[0] == 's') { break; } else if (command[0] == 'b') { int addr = 0; if (sscanf(command+1, "%i", &addr) == EOF) printf("%s\n%s", trace_bad_cmd, trace_cmd_help); tstate->breakpoint = (uint16_t)addr; printf("Set breakpoint at address 0x%2x\n", addr); } else if (command[0] == 'p') { switch (command[1]) { case 'b': printf("Current breakpoint at 0x%2x\n", tstate->breakpoint); break; case 'r': print_vm_registers(cpu); break; case 's': print_vm_stack(cpu); break; case 'z': print_vm_memory(mem, 0, 0x100); break; case 'm': if (sscanf(command+2,"%i %i", &mem_start, &num_bytes) == EOF) ; // So far, sscanf always returns EOF // (idk why). TODO: fix eventually printf("Dumping %d bytes of memory starting " "at 0x%04x\n", num_bytes, mem_start); print_vm_memory(mem, (uint16_t)mem_start, (uint16_t)num_bytes); break; default: printf("%s\n%s", trace_bad_cmd, trace_cmd_help); } } else if (command[0] == 'd' && command[1] == 'b') { printf("Deleted breakpoint at 0x%x\n", tstate->breakpoint); // vvv - disable breakpoint by not point to instr start tstate->breakpoint = 0x01; } else if (command[0] == 'c') { tstate->mode = CONT; } else if (command[0] == 'h') { printf("%s", trace_cmd_help); } else if (command[0] == 'q') { printf("Exiting debugger and virtual machine...\n"); free(command); exit(0); } else { printf("%s\n%s", trace_bad_cmd, trace_cmd_help); } } // Idea: maybe return value to exit debugger? } #endif // TRACE_FUNCS void vm_run(struct CPU *cpu, uint8_t *mem) { uint16_t pc = cpu->pc; uint8_t curr_instr[3]; // current instruction cpu->state = RUNNING; #ifdef TRACE trace_state.breakpoint = 0xffff; trace_state.mode = STEP; #endif while (cpu->state == RUNNING) { #ifdef TRACE vm_trace(cpu, mem, &trace_state); #endif vm_fetch_instruction(pc, mem, curr_instr); pc = vm_do_instruction(cpu, mem, curr_instr); cpu->pc = pc; } // eventually, maybe make it return some kind of CPU status code } /* vm_init(): initialize CPU/memory starting states */ void vm_init(struct CPU *cpu, uint8_t *mem, uint8_t *memimage, uint16_t imgsize) { uint8_t placeholder_memimage[] = {65, 66, 66, 67, 68, 68, 69, 70, 70,0}; cpu->pc = 0x100; // default: start code execution after zero page cpu->lr = 0; cpu->sp = 0; //cpu->a = 0; cpu->state = STOPPED; memset(cpu->stack, 0, 64); if (memimage == NULL) { imgsize = sizeof(placeholder_memimage); memimage = (uint8_t *)placeholder_memimage; } for (uint16_t i = 0; i < imgsize; ++i) mem[i] = memimage[i]; } |