Write the block that takes an instruction word and gives its immediate, 32 bits wide, for all five formats that have one.
Hardware context: Real RV32I instruction words from the assembler, cut at the six fixed field positions by test code; one 12-bit field widened by zero extension and by sign extension. Reading instruction words in a trace or a memory dump, and specifying an instruction decoder
Every RISC-V processor has a block that reads the instruction word and builds its immediate. The rest of the datapath then never looks at the format again.
Write that block. It has no clock. The bench feeds it real instruction words from an assembler.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
trial | 10 | input of each check |
Full specification
| Opcode, bits 6 to 0 | Instructions | Format |
|---|---|---|
| 0010011, 0000011, 1100111 | arithmetic with an immediate, loads, jalr | I |
| 0100011 | stores | S |
| 1100011 | branches | B |
| 0110111, 0010111 | lui, auipc | U |
| 1101111 | jal | J |
| every other opcode | for example 0110011, the R format | none: the output is 0 |
| Format | The 32-bit output |
|---|---|
| I, S | the immediate of 12 bits, sign-extended |
| B | the offset of 13 bits, sign-extended. Its bit 0 is 0 |
| U | the 20 bits in the upper part, and 0 in bits 11 to 0 |
| J | the offset of 21 bits, sign-extended. Its bit 0 is 0 |
How to read a result: a public case shows the output in hex. A private case feeds many words of one kind, and all_match is 1 when every output was right.
Examples
Example 1
An I Word- Input
the word fff28293: addi x5, x5, -1
- Output
imm = 0xffffffff
An I word. Bits 31 to 20 are twelve ones, and the sign fills the upper 20 bits.
Example 2
An S Word- Input
the word 00752423: sw x7, 8(x10)
- Output
imm = 0x8
An S word. The immediate is bits 31 to 25, then bits 11 to 7.
Example 3
A B Word- Input
the word fe029ce3: bne x5, x0, 0x8, at address 10 in hex
- Output
imm = 0xfffffff8
A B word. The offset is -8: its bit 0 is 0 and is not in the word.
Constraints and Contract
- The output depends only on the word of the same moment. The bench waits 1 ns after it sets a word.
- The format is chosen by bits 6 to 0 alone. Do not look at funct3 or funct7.
- For an opcode that is not in the first table, the output is 0.
Hints
Hint 1
Start with a case statement on the opcode. Give each format one line.
Hint 2
For B and J, write the offset bits in order from the sign down, and say for each where it comes from.
Hint 3
Count the bits of each line. Every line must be 32 bits wide.
What the grader checks: For each format: the immediates 0, 1 and -1, the largest and the smallest. Then every single bit set alone, positive and negative, with random register numbers. Every funct3 of the base instructions, the three shifts, the R format, ten words with other opcodes, and 120 random words.
14 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