A pipeline simulator
The problem
Write a simulator for the five-stage pipeline that reads a short assembly program, finds the data hazards, and reports the cycles with and without forwarding, together with a pipeline diagram.
Requirements
- Parse add, sub, addi, lw, sw and beq instructions with their registers
- Find every read-after-write dependence and the stall it needs, with and without forwarding
- Add a branch penalty for taken branches and report the total cycles and CPI
- Print a pipeline diagram with one row per instruction and one column per cycle
Technologies
- Python 3
- Regular expressions or string splitting
- Dictionaries
- Text formatting
Step by step
- Parse the program into a list of instructions with destination and source registers.
- Compute the ID cycle of every instruction with the stall rules from the lesson.
- Add the branch penalty and compute the cycle count and CPI.
- Draw the diagram and compare the result with the pipeline widget in the lesson.
Expected result
A tool that explains where each stall in a small program comes from, and what forwarding saves.
How it is evaluated
- The textbook load-use example gives 1 stall with forwarding and 2 without
- A program without dependences runs in k + n − 1 cycles
- The diagram agrees with the cycle count
- Reordering instructions to avoid a load-use stall removes it in the report
Ways to improve it
- Add a 2-bit branch predictor in place of the fixed penalty
- Let the user choose the number of stages
- Add a scheduler that reorders independent instructions to remove stalls