Improved display code.
Refactored display code into its own function.
This commit is contained in:
11
src/main.cpp
11
src/main.cpp
@@ -25,7 +25,8 @@ int main() {
|
|||||||
const auto registerState = registerStates[i];
|
const auto registerState = registerStates[i];
|
||||||
|
|
||||||
registers.insert(std::make_pair(i, registerState));
|
registers.insert(std::make_pair(i, registerState));
|
||||||
std::cout << std::setw(registerIndexPadding) << i << ": " << registerState << "\n";
|
std::cout << std::setw(registerIndexPadding) << i << ": " << registerState << " => ";
|
||||||
|
printInstruction(std::cout, registerState) << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << std::setw(registerIndexPadding) << i << ": 0...\n\n";
|
std::cout << std::setw(registerIndexPadding) << i << ": 0...\n\n";
|
||||||
@@ -36,9 +37,9 @@ int main() {
|
|||||||
while (executing) {
|
while (executing) {
|
||||||
std::cout << "L" << pc << "\t";
|
std::cout << "L" << pc << "\t";
|
||||||
Register instruction = getOrDefault(registers, pc);
|
Register instruction = getOrDefault(registers, pc);
|
||||||
|
printInstruction(std::cout, instruction) << std::endl;
|
||||||
|
|
||||||
if (instruction == 0) { /* HALT instruction. */
|
if (instruction == 0) { /* HALT instruction. */
|
||||||
std::cout << "HALT" << std::endl;
|
|
||||||
executing = false;
|
executing = false;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -49,9 +50,6 @@ int main() {
|
|||||||
RegIndex sourceRegisterIndex = splitInstruction.first.get_ui();
|
RegIndex sourceRegisterIndex = splitInstruction.first.get_ui();
|
||||||
RegIndex destinationLabel = splitInstruction.second.get_ui();
|
RegIndex destinationLabel = splitInstruction.second.get_ui();
|
||||||
|
|
||||||
std::cout << "R" << sourceRegisterIndex << u8"\u207a \u21a3 " << destinationLabel
|
|
||||||
<< "\n";
|
|
||||||
|
|
||||||
getOrDefault(registers, sourceRegisterIndex)++;
|
getOrDefault(registers, sourceRegisterIndex)++;
|
||||||
pc = destinationLabel;
|
pc = destinationLabel;
|
||||||
} else {
|
} else {
|
||||||
@@ -61,9 +59,6 @@ int main() {
|
|||||||
RegIndex trueLabel = branches.first.get_ui();
|
RegIndex trueLabel = branches.first.get_ui();
|
||||||
RegIndex falseLabel = branches.second.get_ui();
|
RegIndex falseLabel = branches.second.get_ui();
|
||||||
|
|
||||||
std::cout << "R" << sourceRegisterIndex << "\u207b \u21a3 " << trueLabel << ", "
|
|
||||||
<< falseLabel << "\n";
|
|
||||||
|
|
||||||
auto& sourceRegister = getOrDefault(registers, sourceRegisterIndex);
|
auto& sourceRegister = getOrDefault(registers, sourceRegisterIndex);
|
||||||
|
|
||||||
if (sourceRegister > 0) {
|
if (sourceRegister > 0) {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "register.hpp"
|
#include "register.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
std::pair<Register, Register> toPair<false>(Register unsplit) {
|
std::pair<Register, Register> toPair<false>(Register unsplit) {
|
||||||
@@ -41,3 +42,20 @@ std::vector<Register> toList(Register unsplit) {
|
|||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::ostream& printInstruction(std::ostream& sout, Register instruction) {
|
||||||
|
if (instruction == 0)
|
||||||
|
return sout << "BREAK";
|
||||||
|
|
||||||
|
const auto splitInstruction = toPair<false>(instruction);
|
||||||
|
const auto command = splitInstruction.first / 2;
|
||||||
|
|
||||||
|
if (splitInstruction.first % 2 == 0) {
|
||||||
|
/* We have an increment instruction, so second parameter is just label. */
|
||||||
|
return sout << "R" << command << "\u207a \u21a3 " << splitInstruction.second;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto labels = toPair<true>(splitInstruction.second);
|
||||||
|
|
||||||
|
return sout << "R" << command << "\u207b \u21a3 " << labels.first << ", " << labels.second;
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,11 +4,12 @@
|
|||||||
#include <gmpxx.h>
|
#include <gmpxx.h>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <ios>
|
||||||
|
|
||||||
using Register = mpz_class;
|
using Register = mpz_class;
|
||||||
using RegIndex = uint16_t;
|
using RegIndex = uint16_t;
|
||||||
|
|
||||||
template <bool mapZero>
|
template <bool mapZero>
|
||||||
std::pair<Register, Register> toPair(Register unsplit);
|
std::pair<Register, Register> toPair(Register unsplit);
|
||||||
|
|
||||||
std::vector<Register> toList(Register unsplit);
|
std::vector<Register> toList(Register unsplit);
|
||||||
|
std::ostream& printInstruction(std::ostream& sout, Register instruction);
|
||||||
|
|||||||
Reference in New Issue
Block a user