Advertisement
MAKMED1337

Lab Processor Tikhoniuk

Mar 22nd, 2023
1,040
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <bitset>
  4. #include <cassert>
  5. #include <conio.h>
  6.  
  7. //2 address
  8. constexpr int BITS = 22, BYTES = (BITS + 7) / 8;;
  9. using WORD = uint32_t;
  10.  
  11. WORD print_and_return_idk(WORD v) {
  12.     WORD res = 0;
  13.     for(int i = BYTES - 1; i >= 0; --i) {
  14.         for(int j = 7; j >= 0; --j)
  15.             std::cout << (v >> (8 * i + j) & 1);
  16.        
  17.         auto val = v >> (8 * i) & 0xFF;
  18.         assert(val <= 9);
  19.         res += val;
  20.         std::cout << "(" << val << ") ";
  21.     }
  22.     std::cout << "\n";
  23.     return res;
  24. }
  25.  
  26. int main() {
  27.     std::ifstream file("commands.txt");
  28.     std::string command, arg1, arg2;
  29.    
  30.     WORD R1 = 0, R2 = 0, PC = 0, TC, PS = 0;
  31.    
  32.     auto val = [&](std::string_view s) {
  33.         if (s == "R1")
  34.             return R1;
  35.         if (s == "R2")
  36.             return R2;
  37.         if (s == "PC")
  38.             return PC;
  39.         if (s == "TC")
  40.             return TC;
  41.         if (s == "PS")
  42.             return PS;
  43.         if (s.starts_with('L')) {
  44.             s.remove_prefix(1);
  45.             auto num = std::stoull(std::string{s});
  46.             WORD res = 0;
  47.             for(int i = 0; num; ++i, num /= 10) {
  48.                 assert(i < BYTES);
  49.                 res |= (num % 10) << (8 * i);
  50.             }
  51.             assert(res < (1ULL << BITS));
  52.             return res;
  53.         }
  54.         auto res = std::stoll(std::string{s});
  55.         if (res < 0)
  56.             res += 1ULL << BITS;
  57.         assert(res >= 0);
  58.         return WORD(res);
  59.     };
  60.    
  61.     auto ref = [&](std::string_view s) -> WORD& {
  62.         if (s == "R1")
  63.             return R1;
  64.         if (s == "R2")
  65.             return R2;
  66.         if (s == "PC")
  67.             return PC;
  68.         if (s == "TC")
  69.             return TC;
  70.         if (s == "PS")
  71.             return PS;
  72.         assert(false);
  73.     };
  74.    
  75.     auto dump = [&]() {
  76.         std::cout << "PC = " << PC << " TC = " << TC << "\n";
  77.         std::cout << "IR = " << command << " | " << arg1 << " | " << arg2 << "\n";
  78.         std::cout << "R1 = " << std::bitset<BITS>(R1) << " R2 = " << std::bitset<BITS>(R2) << "\n";
  79.         std::cout << "PS = " << PS << "\n\n\n";
  80.     };
  81.    
  82.     while(file >> command >> arg1 >> arg2) {
  83.         getch();
  84.         TC = 1;
  85.         dump();
  86.        
  87.         getch();
  88.         TC = 2;
  89.         if (command == "mov")
  90.             ref(arg1) = val(arg2);
  91.         else if (command == "IDK") {
  92.             std::cout << arg1 << " = ";
  93.             auto sum = print_and_return_idk(val(arg1));
  94.             sum %= val(arg2);
  95.             ref(arg1) = sum;
  96.         }
  97.        
  98.         PS = val(arg1) >> (BITS - 1);
  99.         dump();
  100.         ++PC;
  101.     }
  102.     getch();
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement