2022-11-04 09:38:01 -07:00
|
|
|
#include <intcode/Machine.hpp>
|
|
|
|
|
|
|
|
namespace intcode {
|
|
|
|
|
|
|
|
Machine::Machine() : rom_{}, ram_{}, pc_{0}, base_{0} {}
|
|
|
|
|
2022-11-06 21:12:30 -08:00
|
|
|
Machine::Machine(std::vector<ValueType> program)
|
|
|
|
: rom_{program}, ram_{}, pc_{0}, base_{0} {}
|
2022-11-04 09:38:01 -07:00
|
|
|
|
|
|
|
auto Machine::At(std::size_t i) -> ValueType & {
|
|
|
|
return i < rom_.size() ? rom_[i] : ram_[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Machine::Rel(std::size_t i) -> ValueType & {
|
|
|
|
return At(base_ + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|