You can find the source code here:
PolyDungeon/MIPSCPUsim (github.com)
1) Instruction Fetch(IF)
Loads the and instruction from the PC
2) Instruction Decode(ID)
Decodes the instruction into a number of control signals
This program follows the basic 5 stage MIPS cpu pipeline.
3) ALU Execution(EX)
Executes the and operation
4) Memory Access(MEM)
Memory instructions either read or write
5) Write Back(WB)
The result from the ALU is written back into registers
When opened the program will give you a text box to input a binary .dat file
An example of a precompiled MIPS ".s" program
The MIPS ".s" file is compiled into a.dat file where the instructions are converted to binary
Instruction fetch
The file from above is loaded into ram/memory one line at a time so that the program can easily be broken down
Instruction Decode
Now the binary instruction is broken down / decoded
00100000001000010000000000000101
Opcode (6 bits): 001000 - an opcode of 8 is the addi (add immediate) instruction.
Source Register (5 bits): 00001 - This corresponds to register 1
Target Register (5 bits): 00001 - This also corresponds to register 1
Immediate (16 bits): 0000000000000101 - This corresponds to the immediate value 5
Instruction Execute
Next, we execute the selected operation
00100000001000010000000000000101
Opcode (6 bits): 001000 - the addi instruction adds a number from a register to an immediate and stores back in a registrer. In this example, addi is used as a way to load values into the registers for further calculations. Because the default value of a register is 0, the first addi to a register will always be the same as the immediate value: 0 + 5 = 5
The Source Register (00001) and the Immediate (0000000000000101) are then added together. In a real CPU this is done by sending our addends (Each value is a 32-bit numbers represented by 32 parallel wires, with each wire carrying an electrical signal corresponding to a binary 0 or 1) through a series of transistors that perform the addition using logic gates, ultimately producing a 32-bit sum as the output.
Instruction Write Back
Finally, we write back result of the operation to the registers
00100000001000010000000000000101
The result of our operation (in this case 5) is then sent back to fill the value of our target register (in this case 1)
Because this operation does not use memory, the instruction memory step is skipped
Our program counter is also updated by +4 following this step indicating we need to move on to the next instruction.
Next, we execute the selected operation
00100000001000010000000000000101
Opcode (6 bits): 001000 - the addi instruction adds a number from a register to an immediate and stores back in a registrer. In this example, addi is used as a way to load values into the registers for further calculations. Because the default value of a register is 0, the first addi to a register will always be the same as the immediate value: 0 + 5 = 5
The Source Register (00001) and the Immediate (0000000000000101) are then added together. In a real CPU this is done by sending our addends (Each value is a 32-bit numbers represented by 32 parallel wires, with each wire carrying an electrical signal corresponding to a binary 0 or 1) through a series of transistors that perform the addition using logic gates, ultimately producing a 32-bit sum as the output.
Instruction Memory
In our simplified instruction set, we have 2 instructions that use memory: Store Word and Load Word
Store word
the value in the source register (20) is stored in the val in target reg + imm(in this case 2 + 3 = 5)
This number is converted to a 16 bit address which represent the place in memory where the input is stored!
Load word
In load word, the value stored in the address of the target register (reg 4) + the Immediate (in this case 2 + 3 = 5) is stored in the source register (in this case register 5)