Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- class Machine;
- int get_byte(unsigned char* address,int byte)
- {
- return int(address[byte]);
- }
- class processor
- {
- public:
- unsigned char* PC;
- unsigned char* CP;
- unsigned char* ID;
- unsigned char* RR;
- unsigned char* AX;
- unsigned char* AH;
- Machine* Parent;
- processor(Machine*);
- processor();
- unsigned char** register_id;
- void interrupt(unsigned char);
- void execute_instruction();
- void load_instruction();
- void load_memory(unsigned char*,unsigned char*);
- };
- processor::processor(Machine* parent) {
- Parent = parent;
- PC = new unsigned char[8];
- CP = new unsigned char[8];
- ID = new unsigned char[8];
- RR = new unsigned char[8];
- AX = new unsigned char[8];
- AH = new unsigned char[8];
- register_id = new unsigned char*[6];
- for(int byte = 0;byte < 8;byte++)
- {
- PC[byte] = unsigned char(0);
- CP[byte] = unsigned char(0);
- ID[byte] = unsigned char(0);
- RR[byte] = unsigned char(0);
- }
- register_id[0] = PC;
- register_id[1] = CP;
- register_id[2] = ID;
- register_id[3] = RR;
- register_id[4] = AX;
- register_id[5] = AH;
- }
- processor::processor()
- {
- }
- class Machine
- {
- public:
- int RAM;
- unsigned char* Memory;
- processor Processor;
- Machine(int);
- };
- Machine::Machine(int max)
- {
- Processor = processor(this);
- Memory = new unsigned char[max];
- RAM = max;
- }
- void processor::load_instruction() //8 byte instruction
- {
- int start_address = (*((int*)(CP)) + (*((int*)(PC))*8));
- int a = (*((int*)(PC)) + 1);
- std::memcpy(PC,(unsigned char*)(&a),8);
- for(int byte = 0;byte < 8;byte++)
- {
- ID[byte] = Parent->Memory[start_address + byte];
- }
- }
- void processor::load_memory(unsigned char* start1,unsigned char* start2) {
- for(int byte = 0;byte < 8;byte++)
- {
- start1[byte] = start2[byte];
- }
- }
- void processor::execute_instruction()
- {
- int type = get_byte(ID,0);
- if(type == 0) // LOAD VALUE TO REGISTER
- {
- unsigned char* value_address = new unsigned char[4];
- value_address[0] = ID[2];
- value_address[1] = ID[3];
- value_address[2] = ID[4];
- value_address[3] = ID[5];
- //load_memory(register_id[get_byte(ID,1)],NULL);
- delete[] value_address;
- }
- else if(type == 1) // LOAD VALUE TO MEMORY
- {
- }
- else if(type == 2) // ADD
- {
- RR = (unsigned char*)(*(int*)(AX)+ *(int*)(AH));
- }
- else if(type == 3) // SUB
- {
- int ax = *(int*)(AX);
- int ah = *(int*)(AH);
- int sub = ax-ah;
- std::memcpy(RR,((unsigned char*)(&sub)),8);
- }
- else if(type == 4) // MUL
- {
- RR = (unsigned char*)(*(int*)(AX)* *(int*)(AH));
- }
- else if(type == 5) // DIV
- {
- RR = (unsigned char*)(*(int*)(AX)/ *(int*)(AH));
- }
- else if(type == 6) // AND
- {
- RR = (unsigned char*)(*(bool*)(AX) && *(bool*)(AH));
- }
- else if(type == 7) // OR
- {
- RR = (unsigned char*)(*(bool*)(AX) || *(bool*)(AH));
- }
- else if(type == 8) // NOT
- {
- RR = (unsigned char*)(!*(bool*)(AX));
- }
- else if(type == 9) // INT
- {
- interrupt(ID[1]);
- }
- }
- void processor::interrupt(unsigned char code)
- {
- if(code == unsigned char(0)) {
- std::cin.get();
- }
- if(code == unsigned char(1)) {
- std::cout << *(int*)(RR);
- std::cin.get();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement