Browse Source

continue UI work

master
Evan Burkey 12 months ago
parent
commit
c2e23b67c1
  1. 1
      include/vm.h
  2. 12
      src/synacor.cpp
  3. 109
      src/vm.cpp

1
include/vm.h

@ -20,6 +20,7 @@ struct Vm { @@ -20,6 +20,7 @@ struct Vm {
VmState state;
std::vector<std::string> output;
char input[32];
uint16_t ic;
uint16_t val(uint16_t);

12
src/synacor.cpp

@ -78,7 +78,7 @@ bool Simulator::tick() { @@ -78,7 +78,7 @@ bool Simulator::tick() {
return p.first == vm->ic;
});
auto dis = std::distance(disassembly.begin(), it);
if (dis > 50) {
if (dis > 10) {
it -= 10;
} else {
it -= dis;
@ -95,6 +95,11 @@ bool Simulator::tick() { @@ -95,6 +95,11 @@ bool Simulator::tick() {
ImGui::EndChild();
/* Console Controls */
static ImGuiInputTextFlags input_text_flags = ImGuiInputTextFlags_EnterReturnsTrue;
if (ImGui::InputText("Input", vm->input, IM_ARRAYSIZE(vm->input), input_text_flags)) {
vm->state = VmState::Running;
}
ImGui::SameLine();
if (ImGui::Button("Run")) {
vm->state = VmState::Running;
}
@ -111,6 +116,11 @@ bool Simulator::tick() { @@ -111,6 +116,11 @@ bool Simulator::tick() {
if (ImGui::Button("Reset")) {
load_vm("challenge.bin");
}
/* Raw Memory */
ImGui::BeginChild("Memory", ImVec2(viewport->Size.x - 12, viewport->Size.y / 3), true);
ImGui::TextUnformatted("Someday memory output will go here");
ImGui::EndChild();
}
ImGui::End();

109
src/vm.cpp

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
#include <cstdio>
#include <stdexcept>
#include <cstring>
#include "vm.h"
@ -29,6 +30,8 @@ Vm::Vm(const char *path) : ic(0), state(VmState::Paused), wmem_flag(false) { @@ -29,6 +30,8 @@ Vm::Vm(const char *path) : ic(0), state(VmState::Paused), wmem_flag(false) {
for (i = 0; i < 8; i++) {
reg[i] = 0;
}
std::memset(input, '\0', 32);
}
Vm::~Vm() {
@ -42,6 +45,7 @@ void Vm::cycle() { @@ -42,6 +45,7 @@ void Vm::cycle() {
char c;
char buf[32];
uint16_t *r;
static size_t input_idx;
switch (mem[ic]) {
case 0: // halt 0
@ -49,11 +53,37 @@ void Vm::cycle() { @@ -49,11 +53,37 @@ void Vm::cycle() {
break;
case 1: // set 1 a b
r = reg_p(1);
if (r) {
*r = val(2);
ic += 3;
*reg_p(1) = val(2);
ic += 3;
break;
case 2: // push 2 a
stack.push(val(1));
ic += 2;
break;
case 3: // pop 3 a
*reg_p(1) = stack.top();
stack.pop();
ic += 2;
break;
case 4: // eq 4 a b c
if (val(2) == val(3)) {
*reg_p(1) = 1;
} else {
*reg_p(1) = 0;
}
ic += 4;
break;
case 5: // gt 4 a b c
if (val(2) > val(3)) {
*reg_p(1) = 1;
} else {
*reg_p(1) = 0;
}
ic += 4;
break;
case 6: // jmp 6 a
@ -76,6 +106,57 @@ void Vm::cycle() { @@ -76,6 +106,57 @@ void Vm::cycle() {
}
break;
case 9: // add 9 a b c
*reg_p(1) = (val(2) + val(3)) % 32768;
ic += 4;
break;
case 10: // mult 10 a b c
*reg_p(1) = (val(2) * val(3)) % 32768;
ic += 4;
break;
case 11: // mod 11 a b c
*reg_p(1) = val(2) % val(3);
ic += 4;
break;
case 12: // and 12 a b c
*reg_p(1) = val(2) & val(3);
ic += 4;
break;
case 13: // or 13 a b c
*reg_p(1) = val(2) | val(3);
ic += 4;
break;
case 14: // not 14 a b
*reg_p(1) = val(2) ^ 0x7fff;
ic += 3;
break;
case 15: // rmem 15 a b
*reg_p(1) = mem[val(2)];
ic += 3;
break;
case 16: // wmem 16 a b
mem[val(1)] = val(2);
wmem_flag = true;
ic += 3;
break;
case 17: // call 17 a
stack.push(ic + 2);
ic = val(1);
break;
case 18: // ret 18
ic = stack.top();
stack.pop();
break;
case 19: // out 19 a
c = (char)mem[ic + 1];
if (c == '\n') {
@ -86,7 +167,23 @@ void Vm::cycle() { @@ -86,7 +167,23 @@ void Vm::cycle() {
}
ic += 2;
break;
/*
case 20: // in 20 a
if (input[0] == '\0') {
state = VmState::Awaiting;
input_idx = 0;
} else if (input[input_idx] == '\n') {
state = VmState::Running;
*reg_p(1) = (uint16_t)input[input_idx];
input_idx = 0;
std::memset(input, '\0', 32);
} else {
state = VmState::Running;
*reg_p(1) = (uint16_t)input[0];
++input_idx;
}
break;
*/
case 21: // noop
++ic;
break;
@ -111,7 +208,7 @@ uint16_t Vm::val(uint16_t offset) { @@ -111,7 +208,7 @@ uint16_t Vm::val(uint16_t offset) {
uint16_t* Vm::reg_p(uint16_t offset) {
if (mem[ic + offset] >= REG_START && mem[ic + offset] <= REG_END) {
return &(reg[mem[ic + offset]]);
return &(reg[mem[ic + offset] - REG_START]);
}
output.emplace_back("\nERROR: reg_p received value outside reg bounds\n");
state = VmState::HaltError;

Loading…
Cancel
Save