This project simulates a 5-stage pipelined CPU with hazard support and was jointly done by Shivanshu and Mikhil.
simulate.cpp runs a simulator to simulate pipelined processing on a binary with the following features:
- 256-byte instruction cache and 256-byte data cache, direct-mapped caches, bare-metal operation without any misses.
- Register file contains 16 8-bit registers.
- Byte-addressable memory.
- Instructions are encoded as
opcode + destination_register + source_register1 + source_register2, each component being 4 bits.
This architecture supports 16 instructions, divided into the following categories:
add rd rs1 rs2
sub rd rs1 rs2
mul rd rs1 rs2
inc r
and rd rs1 rs2
or rd rs1 rs2
xor rd rs1 rs2
not rd rs
slli rd rs1 imm(4)
srli rd rs1 imm(4)
li rd imm(8)
ld rd rs1 imm(4)
st rd rs1 imm(4)
jmp imm(8)
beqz rs imm(8)
hlt
| Opcode | Encoding | Opcode | Encoding |
|---|---|---|---|
| ADD | 0000 | SLLI | 1000 |
| SUB | 0001 | SRLI | 1001 |
| MUL | 0010 | LI | 1010 |
| INC | 0011 | LD | 1011 |
| AND | 0100 | ST | 1100 |
| OR | 0101 | JMP | 1101 |
| XOR | 0110 | BEQZ | 1110 |
| NOT | 0111 | HLT | 1111 |
- For
ADD,SUB,MUL,AND,OR,XOR, the format isrd = op(rs1, rs2), whereopis one of the operations. - For
INC,rd = rd + 1and the source registers are ignored. - For
NOT, only one sourcersis required andrd = ~rs(logical not). - For
SLLI, SRLI,rs2represents a 4-bit immediate andrd = op(rs, imm), whereopis left or right shift. - For
LI,rs1andrs2combined represent an 8-bit immediate, andrd = imm. - For
LDandST,rdis loaded from/stored at (respectively) an addressrs1 + imm(rs2)in the data cache. - For
JMP,rdandrs1represent an 8-bit immediate, which indicates jump the from currentPC. The last four bits are discarded. - For
BEQZ,rdis compared with0; if the comparison is true,PCjumps by the 8-bit immediate (formed by combiningrs1andrs2). - For
HLT, the remaining 12 bits are discarded, and the program halts.
Note: All immediates are signed.
- Clone the repository:
git clone https://github.com/123shivanshukumar/Pipelining_simulation.git
cd Pipelining_simulation- Use the
inputandoutputdirectories to store the inputs and outputs of the simulation.
- To store the inputs, create a directory (for example,
Sample) inside theinputdirectory. Inside it, create 3 files:ICache.txt,DCache.txt, andRF.txt, containing 256, 256, and 16 8-bit entries respectively, represented as hexadecimal numbers. These represent the contents of the instruction cache, data cache, and register file before simulating the program.
Note: The
PCstarts at the first instruction inICache.txt.
- Similarly, to store the outputs, create a directory (for example,
Sample) with the same name as its corresponding input directory inside theoutputdirectory. Inside it, create 3 files:DCache.txt,RF.txt, andOutput.txt.DCache.txtandRF.txtcontain the contents of the data cache and register file, respectively, after simulation.Output.txtprovides the program metrics.
-
If the input data is not available, then write the assembly program in the file
program.s. This will be used to populate the instruction cache in theProgramdirectory. -
Run the following command to populate the cache (if program
program.sis used) and generate the binary for the simulation:
make- Run the following command to get the output of the simulation:
./simulate [Name of the directory]For example:
- To simulate the input in
Sample:
./simulate Sample- To simulate for the program written in
program.s:
./simulate ProgramNote: Use the
Programdirectory only if you want to simulate the program written inprogram.s.