S2.7.2 Carry the Control Bits

MediumImplement35 minIcarus Verilog · serverReview the topic

The decoder works in ID. The ALU, the data memory and the register file work in later stages. Write the registers that bring each control signal to its stage, with its instruction.

Hardware context: The five stages of ap_rv32_p5 cut down to arithmetic, lw and sw, with the register file and the ALU of the real core and no hazard logic. Reading a pipeline diagram and a pipeline trace of a processor

In a five-stage pipeline, the decoder sees an instruction once, while it is in ID. One cycle later that instruction is in EX, then in MEM, then in WB. Each of those stages needs some of what the decoder said.

Write the block that carries four of those signals. The ALU operation is used in EX. The store enable is used in MEM. The register write enable and the register number are used in WB.

Signals in the Timeline

SignalWidthRole
trial10input of each check
Full specification
What each stage must see
An instruction is in EX one clock edge after it was in ID. It is in MEM after two edges and in WB after three.
OutputUsed inIts value
alu_operation_eEXthe alu_operation_d of the instruction that is now in EX
mem_write_mMEM1 when the instruction now in MEM is a store. 0 for any other instruction and for a bubble
reg_write_wWB1 when the instruction now in WB writes its rd. 0 for any other instruction and for a bubble
rd_wWBthe rd_d of the instruction that is now in WB

A bubble is a cycle in which valid_d is 0. ID holds no instruction then, and the other inputs may have any value. A bubble moves through the stages like an instruction, and it must store nothing and write nothing.

How to read a result: a public case shows the four outputs after a few clock edges. A private case runs many cycles and checks every one, and all_match is 1 when every output was right.

Examples

Example 1

Three Instructions in a Row
Input
sub x7; then a store; then and x12
Output
alu_operation_e = 7, mem_write_m = 1, reg_write_w = 1, rd_w = 7

After three clock edges the and is in EX, the store is in MEM and the sub is in WB. Each stage sees the signals of its own instruction.

Example 2

A Bubble in MEM
Input
add x5; then a bubble with mem_write_d = 1; then or x6
Output
alu_operation_e = 6, mem_write_m = 0, reg_write_w = 1, rd_w = 5

The bubble is in MEM. It must not store, whatever the decoder said while ID was empty. The add in WB writes x5.

Example 3

After a Reset
Input
after a reset: a store; then xor x3
Output
alu_operation_e = 4, mem_write_m = 1, reg_write_w = 0, rd_w = 0

The store is in MEM and the xor is in EX. WB is still empty after the reset, so nothing is written.

Constraints and Contract

  • The reset is asynchronous and active low. After it, EX, MEM and WB are empty: they hold bubbles.
  • Nothing ever waits in this pipeline: every instruction moves one stage at each rising clock edge.
  • The outputs must not depend on the inputs of the same cycle. The bench reads them before the clock edge.
  • alu_operation_e is checked only while EX holds an instruction. rd_w is checked only while reg_write_w must be 1.

Hints

Hint 1

Count the clock edges between ID and the stage that uses a signal. That is the number of registers the signal needs.

Hint 2

Decide in one place what a bubble does. A signal that is 0 when it enters the first register stays 0 all the way.

Hint 3

The register number is not a control signal, but it needs the same care. It must arrive in WB with its reg_write.

What the grader checks: Single instructions between bubbles, instructions in every cycle, and stores between register writes. Bubbles whose inputs ask for a store and a write. The register numbers 0 and 31, and random streams. Every cycle is checked, from the first cycle after a reset.

11 checks on Icarus Verilog · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain your source only; samples require the simulation service. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 3 problems on this path

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Three Instructions in a Row

Input
sub x7; then a store; then and x12
Expected output
alu_operation_e = 7, mem_write_m = 1, reg_write_w = 1, rd_w = 7

After three clock edges the and is in EX, the store is in MEM and the sub is in WB. Each stage sees the signals of its own instruction.

Run samples executes all 3 public checks. Check all tests runs 11 checks on the server.

Icarus Verilog · server