In this project, you are going to implement a single cycle CPU simulator called MiniCPU using C language.
Implementation of a Single Cycle CPU simulator
In this project, you are going to implement a single cycle CPU simulator called MiniCPU using C language. Your MiniCPU will demonstrate some functions of MIPS processors as well as the principle of the datapath and the control signals. MiniCPU should read in a file containing MIPS machine codes (in the format specified below) and simulate what the MIPS processor does cycle-by-cycle. A C file called component.c will be provided to you which implementing each component of the single-cycle datapath, you are required to modify and fill in the body of the functions in this file.
2.1 Instructions to be simulated
The 14 instructions listed in Figure 1 in the appendix. Note that you are NOT required to treat situations leading to exception.
2.2 Registers to be handled
MiniCPU should handle the 32 general purpose registers. At the start of the program, the registers are initialized to be the values specified in minicpu.c
2.3Memory usage
Mem[0] | ||||
Address | 0x0 | 0x1 | 0x2 | 0x3 |
Content | aa | bb | cc | dd |
If one of the following situations is encountered, the global flag Halt is set to 1, and hence the simulation halts.
2.5Format of the input machine code file
MiniCPU takes hexadecimal formatted machine codes, with filename xxx.asc, as input. An example of .asc file is shown below. Code after “#” on any line is treated as comments.
20010000 #addi $1, $0, 0
200200c8 #addi $2, $0, 200
10220003 #beq $1, $2, 3
00000020 #delay slot
20210001 #addi $1, $1, 1
00000020 #no operation
The simulation ends when an illegal instruction, such as 0x00000000, is encountered.
2.6Note on branch addressing
The branch offset in MIPS, and hence in MiniCPU, is relative to the next instruction, i.e. (PC+4). For example,
3.1Files provided
These files contain the main program and the other supporting functions of the simulator. The code should be self-explanatory. You are required to fill in and modify the functions in component.c. You are not allowed to modify minicpu.c and minicpu.h. All your works should be placed in component.c only. You are not allowed to add new files. Otherwise, your program will not be marked.
3.2 MIPS assembler
A simple assembler minicpuasm.pl is provided for your convenience of testing your MinCPU. The command is:
$minicpuasm.pl filename.asm > filename.asc
where filename.asm is your assembly code file and filename.asc is the output machine code file in hexadecimal format.
Firstly, you are required to complete a function (ALU(…)) in component.c that simulates the operations of an ALU.
void ALU(unsigned A, unsigned B, char ALUControl, unsigned *ALUresult, char *Zero){if(ALUControl==0x0)*ALUresult=A+B; //add}ALUControl | Meaning |
000 | Z = A + B |
001 | Z = A – B |
010 | if A < B, Z = 1; otherwise, Z = 0 |
011 | if A < B, Z = 1; otherwise, Z = 0 (A and B are unsigned integers) |
100 | Z = A AND B |
101 | Z = A OR B |
110 | Shift B left by 16 bits |
111 | Z = NOR(A,B) |
Secondly, you are required to fill in 9 functions in component.c. Each function simulates the operations of a section of the datapath. Figure 2 in the appendix shows the datapath and the sections of the datapath you need to simulate.
In minicpu.c, the function Step() is the core function of the MiniCPU. This function invokes the 9 functions that you are required to implement to simulate the signals and data passing between the components of the datapath. Read Step() thoroughly in order to understand the signals and data passing, and implement the 9 functions.