Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <bitset>
- #include <cassert>
- #include <conio.h>
- //2 address
- constexpr int BITS = 22, BYTES = (BITS + 7) / 8;;
- using WORD = uint32_t;
- WORD print_and_return_idk(WORD v) {
- WORD res = 0;
- for(int i = BYTES - 1; i >= 0; --i) {
- for(int j = 7; j >= 0; --j)
- std::cout << (v >> (8 * i + j) & 1);
- auto val = v >> (8 * i) & 0xFF;
- assert(val <= 9);
- res += val;
- std::cout << "(" << val << ") ";
- }
- std::cout << "\n";
- return res;
- }
- int main() {
- std::ifstream file("commands.txt");
- std::string command, arg1, arg2;
- WORD R1 = 0, R2 = 0, PC = 0, TC, PS = 0;
- auto val = [&](std::string_view s) {
- if (s == "R1")
- return R1;
- if (s == "R2")
- return R2;
- if (s == "PC")
- return PC;
- if (s == "TC")
- return TC;
- if (s == "PS")
- return PS;
- if (s.starts_with('L')) {
- s.remove_prefix(1);
- auto num = std::stoull(std::string{s});
- WORD res = 0;
- for(int i = 0; num; ++i, num /= 10) {
- assert(i < BYTES);
- res |= (num % 10) << (8 * i);
- }
- assert(res < (1ULL << BITS));
- return res;
- }
- auto res = std::stoll(std::string{s});
- if (res < 0)
- res += 1ULL << BITS;
- assert(res >= 0);
- return WORD(res);
- };
- auto ref = [&](std::string_view s) -> WORD& {
- if (s == "R1")
- return R1;
- if (s == "R2")
- return R2;
- if (s == "PC")
- return PC;
- if (s == "TC")
- return TC;
- if (s == "PS")
- return PS;
- assert(false);
- };
- auto dump = [&]() {
- std::cout << "PC = " << PC << " TC = " << TC << "\n";
- std::cout << "IR = " << command << " | " << arg1 << " | " << arg2 << "\n";
- std::cout << "R1 = " << std::bitset<BITS>(R1) << " R2 = " << std::bitset<BITS>(R2) << "\n";
- std::cout << "PS = " << PS << "\n\n\n";
- };
- while(file >> command >> arg1 >> arg2) {
- getch();
- TC = 1;
- dump();
- getch();
- TC = 2;
- if (command == "mov")
- ref(arg1) = val(arg2);
- else if (command == "IDK") {
- std::cout << arg1 << " = ";
- auto sum = print_and_return_idk(val(arg1));
- sum %= val(arg2);
- ref(arg1) = sum;
- }
- PS = val(arg1) >> (BITS - 1);
- dump();
- ++PC;
- }
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement