Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Simulator Phase 1
- #ifndef SIMULATORLIB_H_INCLUDED
- #define SIMULATORLIB_H_INCLUDED
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <iomanip>
- using namespace std;
- ///DEFINITIONS:
- // Part 1: Define the CPU
- // OS Specialized Registers.
- int PC, AC, SP, IR, TIR, //IR -> stores the instruction given by PC.
- // Constant Value Registers.
- ZERO = 0, PLUSone = 1, MINUSone = -1,
- // For masking operations by the OS.
- AMASK, BMASK,
- // General Purpose Registers.
- A, B, C, D, E, F;
- // CPU Buses
- int Abus, Bbus, Cbus, addressOut, data_in, data_out;
- int Alatch, Blatch;
- int AMUX, ALU, shifter;
- // Gateways to/from Memory.
- int MAR, MBR;
- // Memory of 4K represented as a one-d array.
- int RAM[4095];
- // Instructions are 4 characters long.
- char inst[4];
- int NumberOfInstructions = 0; //
- int opcode, address, constant, stackop;
- int const CODE_START_ADDRESS = 1000;
- int const DATA_START_ADDRESS = 2000;
- string currentOperation;
- int stop = 0;
- /*
- Part 2: Define the Operations of the Assembly Intructions; each instruction
- is represented by a function That represents its internal work.
- (done in the lib file)
- */
- ///##############################################################################
- /// Assembly Functions:
- void LODD(int address) //Load Direct
- {
- AC = RAM[address];
- }
- void STOD(int address) //Store Direct
- {
- RAM[address] = AC;
- }
- void ADDD(int address) //Add Direct
- {
- AC = AC + RAM[address];
- }
- void SUBD(int address) //Subtract Direct
- {
- AC = AC - RAM[address];
- }
- void JPOS(int address) //Jump Positive
- {
- if(AC >= 0) PC =address;
- }
- void JZER(int address) //Jump Zero
- {
- if(AC == 0) PC =address;
- }
- void JUMP(int address) //Jump to Adress
- {
- PC = address;
- }
- void LOCO(int address) //Load Constant
- {
- AC = address;
- }
- void LODL(int address) //Load Local
- {
- AC = RAM[address+SP];
- }
- void STOL(int address) //Store Local
- {
- RAM[address+SP] = AC;
- }
- void ADDL(int address) //Add Local
- {
- AC += RAM[address+SP];
- }
- void SUBL(int address) //Subtract Local
- {
- AC -= RAM[address+SP];
- }
- void JNEG(int address) //Jump Negative
- {
- if(AC < 0) PC=address;
- }
- void JNZE(int address) //Jump Nonzero
- {
- if(AC !=0 ) PC=address;
- }
- void CALL(int address) //Call Procedure
- {
- SP--;
- RAM[SP] = PC;
- PC = address;
- }
- void PSHI(int address) //Push Indirect
- {
- SP--;
- RAM[SP] = RAM[AC];
- }
- void POPI(int address) //Push Onto Stack
- {
- RAM[AC] = RAM[PC];
- SP++;
- }
- void PUSH(int address) //Push From Stack
- {
- SP--;
- RAM[SP] = AC;
- }
- void POP(int address) //Pop From Stack
- {
- AC = RAM[SP];
- SP++;
- }
- void RETN(int address) //Return
- {
- PC = RAM[SP];
- SP++;
- }
- void SWAP(int address) //Swap Accumulator and Stack Pointer
- {
- int tmp = AC;
- AC = SP;
- SP = tmp;
- }
- void INSP(int address) //Increment Stack Pointer
- {
- SP += address;
- }
- void DESP(int address) //Decrement Stack Pointer
- {
- SP -= address;
- }
- ///##############################################################################
- /// CPU Cycle functions: fetch -> decode -> execute
- void LoadText() // This simulates compiling and loading a program into RAM.
- {
- string currentLine;
- ifstream instructionstxt ("Instructions.txt");
- int i = CODE_START_ADDRESS;
- if(instructionstxt.is_open())
- {
- while (getline(instructionstxt,currentLine))
- {
- instructionstxt >> hex >> RAM[i];
- i++;
- NumberOfInstructions++; //counts number of instructions in the program
- } //end while
- instructionstxt.close();
- } //end if
- else
- cout << "Error: Instructions.txt not found";
- }
- void fetch()
- {
- MAR = PC; //Gets next instruction
- MBR = RAM[MAR];
- IR = MBR;
- PC = PC + 1;
- }
- void decode()
- {
- // [var] >> [number of times to bit shift right]
- opcode = (IR >> 12); //if 0001 xxxxxxxxxxxx then becomes 000000000000 0001
- stackop = (IR >> 0x8) & 0x0007; //shifting
- }
- // Call the assembly instructions in hex here.
- void calculateAddress()
- {
- address = (IR & 0x0FFF); // Mask with 0000111111111111
- constant = (IR & 0X00FF); // Mask with 0000000011111111
- }
- void execute()
- {
- switch(opcode)
- {
- case 0: LODD (address); currentOperation = "LODD" ; break;
- case 1: STOD (address); currentOperation = "STOD" ; break;
- case 2: ADDD (address); currentOperation = "ADDD" ; break;
- case 3: SUBD (address); currentOperation = "SUBD" ; break;
- case 4: JPOS (address); currentOperation = "JPOS" ; break;
- case 5: JZER (address); currentOperation = "JZER" ; break;
- case 6: JUMP (address); currentOperation = "JUMP" ; break;
- case 7: LOCO (address); currentOperation = "LOCO" ; break;
- case 8: LODL (address); currentOperation = "LODL" ; break;
- case 9: STOL (address); currentOperation = "STOL" ; break;
- case 10: ADDL (address); currentOperation = "ADDL" ; break;
- case 11: SUBL (address); currentOperation = "SUBL" ; break;
- case 12: JNEG (address); currentOperation = "JNEG" ; break;
- case 13: JNZE (address); currentOperation = "JNZE" ; break;
- case 14: CALL (address); currentOperation = "CALL" ; break;
- case 15: switch(stackop)
- {
- case 0: PSHI (address); currentOperation = "PSHI" ; break;
- case 1: POPI (address); currentOperation = "POPI" ; break;
- case 2: PUSH (address); currentOperation = "PUSH" ; break;
- case 3: POP (address); currentOperation = "POP" ; break;
- case 4: RETN (address); currentOperation = "RETN" ; break;
- case 5: SWAP (address); currentOperation = "SWAP" ; break;
- case 6: INSP (address); currentOperation = "INSP" ; break;
- case 7: DESP (address); currentOperation = "DESP" ; break;
- } //end inner switch
- } //end outer switch
- } //end execute function
- //void print(int InstructionNumber)
- //{
- // cout << endl
- // <<"Instuction Number:" << InstructionNumber
- // <<" | Current Operation:" << currentOperation
- // <<" | Address:" << address
- // << endl << endl
- // <<"MAR=" << setw(5) << MAR <<" | "
- // <<"MBR=" << setw(5) << MBR <<" | "
- // <<"PC=" << setw(5) << PC <<" | "
- // <<"AC=" << setw(3) << AC <<" | "
- // <<"SP=" << setw(3) << SP <<" | "
- // <<"IR=" << setw(5) << IR <<"\n";
- // cout<<"\n=========================================================\n";
- //}
- void print(int InstructionNumber)
- {
- if (InstructionNumber == 1) //print the header if first instruction.
- {
- cout << " MAR MBR PC AC SP IR \n";
- cout<<"=========================================================\n";
- }
- cout <<"\nInstruction number " << InstructionNumber << " Current Operation: " << currentOperation << endl << endl;
- cout << " "<< setw(4) << MAR << setw(7) << MBR << setw(7) << PC << setw(5) <<AC<<setw(5)<<SP<<setw(9)<<IR<<setw(5)<<"\n\n";
- cout<<"------------------------------------------------------------------------------\n";
- }
- void status(int i)
- {
- if(i == NumberOfInstructions)
- stop = 1;
- }
- #endif // SIMULATORLIB_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement