Skip to content
code0.HDL

Chisel Practice

Build fluency with short Chisel problems. Edit the circuit generator, run the chiseltest testbench, then submit your solution.

Your drafts and solved problems stay in this browser.

Jump to a chapter

A Module, its IO, and the := connection

1 problem

Read a Chisel module as a Scala class that builds a circuit, tell the connection := apart from a Scala name, predict what the testbench prints, run it, then change one parameter and see a different circuit.

  1. 01ImplementationBuild a constant multiplier in a generator loopUse a Scala if on the factor parameter so the elaboration loop builds only the adders that in * factor needs.

Bool, UInt, and the operators

1 problem

Tell a one-bit Bool from a multi-bit UInt, apply the bitwise, bit-field, Cat and reduction operators to an 8-bit value by hand, run the operator example, see what shifts, arithmetic and comparisons build and how wide their results are, then build a parity frame.

  1. 05ImplementationFrame data with a parity bitBuild an 8-bit frame that carries seven data bits and an even-parity bit on top.

Widths, SInt, and arithmetic

2 problems

Give a port a width with .W, read the width Chisel assigns to a literal, follow a signed value through a connection and a cast, then see why + loses a carry that +& keeps.

  1. 03VerificationAdd a signed offsetFind why a negative offset moves an 8-bit address forward.
  2. 04ImplementationKeep the carry with +&Make a 4-bit Chisel adder keep its carry in a 5-bit sum.

Mux, when, Wire, and a default for every path

1 problem

Choose between values with Mux and when, give a Wire a default, read the last-connection-wins rule off a trace, and see what happens to a path nothing drives.

  1. 06VerificationLet a fault override the fanFind why a sensor fault fails to force the fan to full speed.

Selecting with MuxCase, MuxLookup, PriorityMux and Mux1H

3 problems

Choose among many inputs with MuxCase, MuxLookup, PriorityMux and Mux1H, and decide which one a selection calls for by how its selectors behave.

  1. 20VerificationRepair the burst-length tableFind why a MuxLookup burst-length table returns the wrong beat count for two codes.
  2. 21ImplementationCheckpoint A: select one of four operationsWrite a combinational ALU slice from scratch: a 2-bit sel picks which of four bitwise operations on a and b reaches y.
  3. 22ImplementationGrant the lowest requestSelect the data of the lowest-numbered requester, with an explicit 0 when nobody asks.

Conditional update, hold, and reset

1 problem

Update a register only under a when and let it hold otherwise, decide which registers a reset changes from how each was created, see that the implicit reset acts at a clock edge, and write priority as source order.

  1. 13VerificationClear a sticky flag on resetFind why an error flag survives reset while the error counter next to it clears.

RegNext, delays, and waveforms

3 problems

Delay a signal by one edge with RegNext and by two with a second one, build a rising-edge detector from the delayed copy, then read reset, pokes, and register changes off a recorded waveform.

  1. 11ImplementationCount rising edges, not high cyclesGive an event counter a registered copy of its input so it counts each 0-to-1 transition once.
  2. 29ImplementationLatch a PWM duty at the period boundaryLoad the duty register only at the edge that ends a period, and confirm the timing against count and level in the waveform.
  3. 30VerificationKeep a tick divider's phase across pausesTicks after a pause land in the wrong cycles; find in the waveform what phase does while en is low.

Bundles and Vecs

2 problems

Group named fields in a Bundle and indexed elements in a Vec, read the bit layout that asUInt and asTypeOf follow, tell a Scala index from a hardware index, and use Flipped to put one interface declaration on both ends of a link.

  1. 07VerificationMatch a status word formatFind why a status Bundle packs and unpacks a 16-bit word in the wrong layout.
  2. 08ImplementationReverse the bytes of a wordView a 32-bit word as a Vec of bytes and pack it back in reverse byte order.

Child modules, parameters, and generators

3 problems

Place a module inside another with Module(new ...), drive every child input, build a family of definitions from one parameterized class with require and desiredName, call a module through a companion apply, and read a generator written with for and def by counting what it built.

  1. 02VerificationFind the parameter a generator loop ignoresCount the ELABORATE lines to find why one gain output is wrong while the other is right.
  2. 09ImplementationSort three values with child modulesComplete a three-input sorter by instantiating and wiring one more two-input sorter.
  3. 10VerificationGive every lane its own unitFind why four lanes of absolute differences all report the same value.

ChiselEnum and state machines

2 problems

Give each state a name with ChiselEnum, hold it in a register, branch on it with switch and is, convert between an enum and raw bits safely, and read the width Chisel picked.

  1. 18ImplementationAdd the yellow phaseExtend a two-state ChiselEnum traffic light with a yellow state between green and red.
  2. 19VerificationFind the misplaced opcodeFind why a ChiselEnum opcode decoder rejects one legal instruction encoding.

Design from a requirement

1 problem

Turn a written requirement into ports, a register with a reset value, a when chain whose order is the priority, and a wrapping add, keep the functional clear apart from the implicit reset, and check the design against a cycle table before grading.

  1. 12ImplementationCheckpoint B: a counter with clear and enableWrite the update of a 4-bit counter whose synchronous clear beats enable, counts only while enabled, and wraps from 15 to 0.

Memories

2 problems

Read-only tables with VecInit and Counter, the combinational-read Mem, the synchronous-read SyncReadMem with its one-cycle latency and undefined-read cases, write masks and the SRAM API, and how to choose between them.

  1. 14ImplementationFlag read data from a SyncReadMemRaise a valid flag in the cycle where a synchronous-read memory actually returns the requested word.
  2. 15VerificationRead a register file in the same cycleFind why both operand ports of a register file return the previous instruction's registers.

Interfaces and connections

2 problems

Describe an interface as a Bundle, connect two of them in bulk with <>, see what happens to a field with no partner, and run a ready-valid link with Decoupled.

  1. 16VerificationKeep a tag with its requestFind why a one-cycle retiming stage delivers each request with the next request's tag.
  2. 17ImplementationClose a ready-valid valve in both directionsDrive the backward ready signal of a gated Decoupled stage so that producer and consumer agree on every transfer.

Polymorphism and parameterization

2 problems

Separate Scala type parameters and their upper bounds from value parameters, then use type templates, configuration objects, optional ports, and by-name module arguments to build related circuits.

  1. 23ImplementationBuild a generic hold registerGive a type-parameterized module a register shaped by its gen template.
  2. 24VerificationSize the counter for its limitFind why a parameterized wrap counter never reaches some configured limits.

Printing from hardware

2 problems

Print from inside the circuit with printf and the cf interpolator, tell hardware printing from elaboration-time println, and read the signal names the compiler gives your vals.

  1. 25ImplementationTurn a hex digit into its ASCII characterMap digits 10 to 15 to the letters a to f, checking each cycle with a printf that shows %x next to %c.
  2. 26VerificationRestart a frame checksum on its start byteRead the printf trace to see why a new frame keeps adding to the previous frame's sum.

Testbench techniques

2 problems

Read a failed expectation down to the step that produced it, wait for an event without hanging the test, and use the idle-cycle timeout to tell a slow design from a stuck one.

  1. 27ImplementationStretch a trigger into a three-cycle pulseImplement the counter behind a pulse stretcher and use the step number in each failed expect to find the cycle that is still wrong.
  2. 28VerificationFix a countdown that never reports doneA wait loop hits its idle-cycle timeout; use the WAIT trace to find where the counter stops.