Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.11 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Machine;
  4.  
  5. int get_byte(unsigned char* address,int byte)
  6. {
  7.     return int(address[byte]);
  8. }
  9.  
  10. class processor
  11. {
  12. public:
  13.     unsigned char* PC;
  14.     unsigned char* CP;
  15.     unsigned char* ID;
  16.     unsigned char* RR;
  17.     unsigned char* AX;
  18.     unsigned char* AH;
  19.     Machine* Parent;
  20.     processor(Machine*);
  21.     processor();
  22.     unsigned char** register_id;
  23.     void interrupt(unsigned char);
  24.     void execute_instruction();
  25.     void load_instruction();
  26.     void load_memory(unsigned char*,unsigned char*);
  27. };
  28. processor::processor(Machine* parent) {
  29.     Parent = parent;
  30.     PC = new unsigned char[8];
  31.     CP = new unsigned char[8];
  32.     ID = new unsigned char[8];
  33.     RR = new unsigned char[8];
  34.     AX = new unsigned char[8];
  35.     AH = new unsigned char[8];
  36.     register_id = new unsigned char*[6];
  37.     for(int byte = 0;byte < 8;byte++)
  38.     {
  39.         PC[byte] = unsigned char(0);
  40.         CP[byte] = unsigned char(0);
  41.         ID[byte] = unsigned char(0);
  42.         RR[byte] = unsigned char(0);
  43.     }
  44.     register_id[0] = PC;
  45.     register_id[1] = CP;
  46.     register_id[2] = ID;
  47.     register_id[3] = RR;
  48.     register_id[4] = AX;
  49.     register_id[5] = AH;
  50.  
  51. }
  52. processor::processor()
  53. {
  54. }
  55.  
  56.  
  57.  
  58. class Machine
  59. {
  60. public:
  61.     int RAM;
  62.     unsigned char* Memory;
  63.     processor Processor;
  64.     Machine(int);
  65. };
  66.  
  67. Machine::Machine(int max)
  68. {
  69.     Processor = processor(this);
  70.     Memory = new unsigned char[max];
  71.     RAM = max;
  72. }
  73.  
  74.  
  75.  
  76. void processor::load_instruction() //8 byte instruction
  77. {
  78.     int start_address = (*((int*)(CP)) + (*((int*)(PC))*8));
  79.     int a = (*((int*)(PC)) + 1);
  80.     std::memcpy(PC,(unsigned char*)(&a),8);
  81.     for(int byte = 0;byte < 8;byte++)
  82.     {
  83.         ID[byte] = Parent->Memory[start_address + byte];
  84.     }
  85. }
  86.  
  87. void processor::load_memory(unsigned char* start1,unsigned char* start2) {
  88.     for(int byte = 0;byte < 8;byte++)
  89.     {
  90.         start1[byte] = start2[byte];
  91.     }
  92. }
  93.  
  94. void processor::execute_instruction()
  95. {
  96.     int type = get_byte(ID,0);
  97.  
  98.     if(type == 0) // LOAD VALUE TO REGISTER
  99.     {
  100.         unsigned char* value_address = new unsigned char[4];
  101.         value_address[0] = ID[2];
  102.         value_address[1] = ID[3];
  103.         value_address[2] = ID[4];
  104.         value_address[3] = ID[5];
  105.  
  106.         //load_memory(register_id[get_byte(ID,1)],NULL);
  107.         delete[] value_address;
  108.     }
  109.     else if(type == 1) // LOAD VALUE TO MEMORY
  110.     {
  111.     }
  112.     else if(type == 2) // ADD
  113.     {
  114.         RR = (unsigned char*)(*(int*)(AX)+ *(int*)(AH));
  115.  
  116.     }
  117.     else if(type == 3) // SUB
  118.     {
  119.         int ax = *(int*)(AX);
  120.         int ah = *(int*)(AH);
  121.         int sub = ax-ah;
  122.         std::memcpy(RR,((unsigned char*)(&sub)),8);
  123.     }
  124.     else if(type == 4) // MUL
  125.     {
  126.         RR = (unsigned char*)(*(int*)(AX)* *(int*)(AH));
  127.     }
  128.     else if(type == 5) // DIV
  129.     {
  130.         RR = (unsigned char*)(*(int*)(AX)/ *(int*)(AH));
  131.     }
  132.     else if(type == 6) // AND
  133.     {
  134.         RR = (unsigned char*)(*(bool*)(AX) && *(bool*)(AH));
  135.     }
  136.     else if(type == 7) // OR
  137.     {
  138.         RR = (unsigned char*)(*(bool*)(AX) || *(bool*)(AH));
  139.     }
  140.     else if(type == 8) // NOT
  141.     {
  142.         RR = (unsigned char*)(!*(bool*)(AX));
  143.     }
  144.     else if(type == 9) // INT
  145.     {
  146.         interrupt(ID[1]);
  147.     }
  148. }
  149.  
  150. void processor::interrupt(unsigned char code)
  151. {
  152.     if(code == unsigned char(0)) {
  153.         std::cin.get();
  154.     }
  155.     if(code == unsigned char(1)) {
  156.         std::cout << *(int*)(RR);
  157.         std::cin.get();
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement