This commit is contained in:
2022-11-04 08:29:02 -07:00
parent e88863ed37
commit fd39209ff0
9 changed files with 136 additions and 92 deletions

View File

@@ -36,9 +36,6 @@ public:
Machine();
explicit Machine(std::vector<ValueType> ram);
/// Advance machine until next side effect
auto Step() -> std::variant<Input, Output, Halt>;
/// Access memory at absolute address
/// @param address
/// @return reference to memory at given address
@@ -48,8 +45,14 @@ public:
/// @param offset from base pointer
/// @return reference to memory at given offset
auto Rel(std::size_t offset) -> ValueType &;
auto Next() -> ValueType &;
auto Goto(std::size_t address) -> void;
auto Rebase(std::size_t offset) -> void;
};
auto Step(Machine & m) -> std::variant<Input, Output, Halt>;
struct BadInstruction : public std::runtime_error {
explicit BadInstruction(char const* what);
};

View File

@@ -11,23 +11,6 @@ Machine::Machine() : rom_{}, ram_{}, pc_{0}, base_{0} {}
Machine::Machine(std::vector<ValueType> ram)
: rom_{ram}, ram_{}, pc_{0}, base_{0} {}
namespace {
auto Ref(Machine & m, ValueType instruction, std::size_t k, ValueType p)
-> ValueType & {
auto &v = m.At(k);
switch (instruction / p % 10) {
case 0:
return m.At(v);
case 1:
return v;
case 2:
return m.Rel(v);
default:
throw BadInstruction{"invalid addressing mode"};
}
}
}
auto Machine::At(std::size_t i) -> ValueType & {
return i < rom_.size() ? rom_[i] : ram_[i];
}
@@ -36,57 +19,95 @@ auto Machine::Rel(std::size_t i) -> ValueType & {
return At(base_ + i);
}
auto Machine::Step() -> std::variant<Input, Output, Halt> {
auto Machine::Rebase(std::size_t offset) -> void {
base_ += offset;
}
auto Machine::Next() -> ValueType & {
return At(pc_++);
}
auto Machine::Goto(std::size_t address) -> void {
pc_ = address;
}
auto Step(Machine & m) -> std::variant<Input, Output, Halt> {
for (;;) {
auto instruction = At(pc_);
auto a = [=]() -> auto & { return Ref(*this, instruction, pc_+1, 100); };
auto b = [=]() -> auto & { return Ref(*this, instruction, pc_+2, 1000); };
auto c = [=]() -> auto & { return Ref(*this, instruction, pc_+3, 10000); };
auto instruction = m.Next();
auto modes = instruction / 10;
auto arg = [&]() -> ValueType & {
auto &v = m.Next();
switch ((modes /= 10) % 10) {
case 0:
return m.At(v);
case 1:
return v;
case 2:
return m.Rel(v);
default:
throw BadInstruction{"invalid addressing mode"};
}
};
switch (instruction % 100) {
case 1:
c() = a() + b();
pc_ += 4;
case 1: {
auto a = arg();
auto b = arg();
auto& c = arg();
c = a + b;
break;
}
case 2:
c() = a() * b();
pc_ += 4;
case 2: {
auto a = arg();
auto b = arg();
auto& c = arg();
c = a * b;
break;
}
case 3: {
auto &pos = a();
pc_ += 2;
return Input{pos};
return Input{arg()};
}
case 4: {
auto val = a();
pc_ += 2;
return Output{val};
return Output{arg()};
}
case 5:
pc_ = a() ? b() : pc_ + 3;
case 5: {
auto a = arg();
auto b = arg();
if (a) { m.Goto(b); }
break;
}
case 6:
pc_ = a() ? pc_ + 3 : b();
case 6: {
auto a = arg();
auto b = arg();
if (!a) { m.Goto(b); }
break;
}
case 7:
c() = a() < b();
pc_ += 4;
case 7: {
auto a = arg();
auto b = arg();
auto& c = arg();
c = a < b;
break;
}
case 8:
c() = a() == b();
pc_ += 4;
case 8: {
auto a = arg();
auto b = arg();
auto& c = arg();
c = a == b;
break;
}
case 9:
base_ += a();
pc_ += 2;
m.Rebase(arg());
break;
case 99: