G7.7.1 Split the Controller

HardImplementIcarus · browser samples / server checksReview the topic

Build a parking barrier from a main machine and a timer. Its wait restarts while a car is below.

Hardware context: A parking barrier whose waits would need a state per cycle if drawn as one machine. Build a barrier controller as a main machine and a restartable timer joined by start and done.

A parking barrier rises for a car, stays up while the car passes, and lowers again. Drawn flat, it needs a state per cycle of every wait. The team wants a four-state main machine and one timer instead.

This is an Implement problem. The starter has the main machine, but no timer: each timed state lasts one cycle. Add the timer and the handshake. Both registers are ports, so the check sees the main state and the timer in every cycle.

The check resets the barrier and drives car in chosen cycles. Over 16 cycles it records ms and t.

Signals in the Timeline

SignalWidthRole
cars16input of each check
Full specification
The module (keep the name and the ports)
module gate (
  input  logic       clk,
  input  logic       rst,   // synchronous reset, active high: IDLE, timer at 0
  input  logic       car,   // a car is at the barrier
  output logic [1:0] ms,    // the main machine's state: IDLE 0, RAISE 1, UP 2, LOWER 3
  output logic [2:0] t      // the timer: a down counter
);
  ...
endmodule
State (ms)WaitAt the end of a cycle
IDLE (0)—car: go to RAISE and start a 2-cycle wait
RAISE (1)2 cyclesdone: go to UP and start a 4-cycle wait. car is ignored.
UP (2)4 cyclescar: stay in UP and start the 4-cycle wait again. Else done: go to LOWER and start a 2-cycle wait.
LOWER (3)2 cyclescar: go to RAISE and start a 2-cycle wait. Else done: go to IDLE.
Timer ruleDetail
StartAt an edge where a wait of N cycles starts, t loads N - 1.
CountAt every other edge, t goes down by 1 if it is not 0, and stays at 0 otherwise.
donedone = (t == 0). It is read in the same cycle.
Resetrst = 1 at an edge gives IDLE and t = 0.

Examples

Example 1

One Car, Passing Quickly
Input
car = 1 in cycles 1
Output
ms_cycle0 = IDLE (0), t_cycle0 = 0, ms_cycle1 = IDLE (0), t_cycle1 = 0, ms_cycle2 = RAISE (1), t_cycle2 = 1, ms_cycle3 = RAISE (1), t_cycle3 = 0, ms_cycle4 = UP (2), t_cycle4 = 3, ms_cycle5 = UP (2), t_cycle5 = 2, ms_cycle6 = UP (2), t_cycle6 = 1, ms_cycle7 = UP (2), t_cycle7 = 0, ms_cycle8 = LOWER (3), t_cycle8 = 1, ms_cycle9 = LOWER (3), t_cycle9 = 0, ms_cycle10 = IDLE (0), t_cycle10 = 0, ms_cycle11 = IDLE (0), t_cycle11 = 0, ms_cycle12 = IDLE (0), t_cycle12 = 0, ms_cycle13 = IDLE (0), t_cycle13 = 0, ms_cycle14 = IDLE (0), t_cycle14 = 0, ms_cycle15 = IDLE (0), t_cycle15 = 0

The car is seen at the end of cycle 1. RAISE fills cycles 2-3 with t = 1, 0. UP fills 4-7 with t = 3 down to 0. LOWER fills 8-9, and IDLE follows.

Example 2

A Car Waits Under the Barrier
Input
car = 1 in cycles 1, 2, 3, 4, 5, 6, 7
Output
ms_cycle0 = IDLE (0), t_cycle0 = 0, ms_cycle1 = IDLE (0), t_cycle1 = 0, ms_cycle2 = RAISE (1), t_cycle2 = 1, ms_cycle3 = RAISE (1), t_cycle3 = 0, ms_cycle4 = UP (2), t_cycle4 = 3, ms_cycle5 = UP (2), t_cycle5 = 3, ms_cycle6 = UP (2), t_cycle6 = 3, ms_cycle7 = UP (2), t_cycle7 = 3, ms_cycle8 = UP (2), t_cycle8 = 3, ms_cycle9 = UP (2), t_cycle9 = 2, ms_cycle10 = UP (2), t_cycle10 = 1, ms_cycle11 = UP (2), t_cycle11 = 0, ms_cycle12 = LOWER (3), t_cycle12 = 1, ms_cycle13 = LOWER (3), t_cycle13 = 0, ms_cycle14 = IDLE (0), t_cycle14 = 0, ms_cycle15 = IDLE (0), t_cycle15 = 0

While car = 1 in UP, every edge reloads t with 3. UP lasts until four cycles after the last cycle with car = 1.

Example 3

A Car Arrives While the Barrier Lowers
Input
car = 1 in cycles 0, 8
Output
ms_cycle0 = IDLE (0), t_cycle0 = 0, ms_cycle1 = RAISE (1), t_cycle1 = 1, ms_cycle2 = RAISE (1), t_cycle2 = 0, ms_cycle3 = UP (2), t_cycle3 = 3, ms_cycle4 = UP (2), t_cycle4 = 2, ms_cycle5 = UP (2), t_cycle5 = 1, ms_cycle6 = UP (2), t_cycle6 = 0, ms_cycle7 = LOWER (3), t_cycle7 = 1, ms_cycle8 = LOWER (3), t_cycle8 = 0, ms_cycle9 = RAISE (1), t_cycle9 = 1, ms_cycle10 = RAISE (1), t_cycle10 = 0, ms_cycle11 = UP (2), t_cycle11 = 3, ms_cycle12 = UP (2), t_cycle12 = 2, ms_cycle13 = UP (2), t_cycle13 = 1, ms_cycle14 = UP (2), t_cycle14 = 0, ms_cycle15 = LOWER (3), t_cycle15 = 1

A car in LOWER sends the barrier back to RAISE with a fresh 2-cycle wait, before done can end LOWER.

Constraints and Contract

  • car is a known 0/1 value and changes only between edges.
  • Keep the module name and the ports. ms and t must be the two machines' registers.

Hints

Hint 1

Add start and len to the main machine's always_comb. Set them on every arc that starts a wait, including the self-loop of UP when car = 1.

Hint 2

Write the timer as its own always_ff: load len - 1 on start, else count down to 0. Then done is one compare.

Hint 3

In UP and LOWER, car comes first and done second. Check the cycle where both are 1.

What the grader checks: Ten directed and twelve seeded car patterns over 16 cycles after a reset. In every cycle the main state and the timer value are compared with a JavaScript model of the two machines.

22 checks on Icarus · browser samples / server checks. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 1 steps and the final capstone

Report a problem with this page

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

One Car, Passing Quickly

Input
car = 1 in cycles 1
Expected output
ms_cycle0 = IDLE (0), t_cycle0 = 0, ms_cycle1 = IDLE (0), t_cycle1 = 0, ms_cycle2 = RAISE (1), t_cycle2 = 1, ms_cycle3 = RAISE (1), t_cycle3 = 0, ms_cycle4 = UP (2), t_cycle4 = 3, ms_cycle5 = UP (2), t_cycle5 = 2, ms_cycle6 = UP (2), t_cycle6 = 1, ms_cycle7 = UP (2), t_cycle7 = 0, ms_cycle8 = LOWER (3), t_cycle8 = 1, ms_cycle9 = LOWER (3), t_cycle9 = 0, ms_cycle10 = IDLE (0), t_cycle10 = 0, ms_cycle11 = IDLE (0), t_cycle11 = 0, ms_cycle12 = IDLE (0), t_cycle12 = 0, ms_cycle13 = IDLE (0), t_cycle13 = 0, ms_cycle14 = IDLE (0), t_cycle14 = 0, ms_cycle15 = IDLE (0), t_cycle15 = 0

The car is seen at the end of cycle 1. RAISE fills cycles 2-3 with t = 1, 0. UP fills 4-7 with t = 3 down to 0. LOWER fills 8-9, and IDLE follows.

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

Icarus · browser samples / server checks