G7.1.1 Traffic Light Controller

MediumImplementIcarus Verilog + Yosys · serverReview the topic

Build a pedestrian crossing as a Moore machine with a timer and a request register: green, yellow, walk, and back.

Hardware context: A pedestrian crossing on a busy road, and a pattern flag that a downstream block needs one cycle sooner. Build a pedestrian crossing with a cycle timer and a request register.

A pedestrian crossing has a light for cars and a walk lamp wired to the cars' red. Cars get green until someone presses the button. Then the light goes yellow, then red while people walk, then green again.

This is an Implement problem. Complete crossing as a Moore machine: each light is decoded from the state only. The starter has the states and the output logic, but it never leaves GREEN.

The check resets the crossing and presses the button in chosen cycles. Over 26 cycles it records which light is on. The design must synthesize with no latches.

Signals in the Timeline

SignalWidthRole
presses26input of each check
Full specification
The module (keep the name and the ports)
module crossing (
  input  logic clk,
  input  logic rst,       // synchronous reset, active high: GREEN, no request waiting
  input  logic button,    // the walk button, seen at each rising edge
  output logic car_g,     // cars green
  output logic car_y,     // cars yellow
  output logic car_r      // cars red (the walk lamp is wired to car_r)
);
  ...
endmodule
StateLightLeaves at the edge that endsGoes to
GREENcar_gits 4th cycle or a later one, if a request is waitingYELLOW
YELLOWcar_yits 2nd cycleWALK
WALKcar_rits 5th cycleGREEN
RuleDetail
Resetrst = 1 at an edge gives GREEN in the next cycle, with no request waiting. That cycle is the first green cycle.
A pressbutton = 1 at an edge. The request is waiting from the next cycle on.
ClearingThe edge that enters WALK clears the request, including a press seen at that same edge. A press during WALK waits for the next round.
Cycle countEach state counts its own cycles from 1. Entering a state starts its count again.

Examples

Example 1

One Press in Cycle 0
Input
button pressed in cycles 0
Output
green_cycles = {0, 1, 2, 3, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}, yellow_cycles = {4, 5}, red_cycles = {6, 7, 8, 9, 10}

The press is seen at the edge that ends cycle 0, so the request waits from cycle 1. Green lasts its minimum, cycles 0 to 3. Yellow is 4 and 5, walk 6 to 10, and green again from 11.

Example 2

A Press After the Minimum Green
Input
button pressed in cycles 7
Output
green_cycles = {0, 1, 2, 3, 4, 5, 6, 7, 8, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}, yellow_cycles = {9, 10}, red_cycles = {11, 12, 13, 14, 15}

Green has already lasted more than 4 cycles. So the light leaves GREEN at the first edge where the request is waiting, one edge after the press.

Example 3

A Press During WALK Starts a Second Round
Input
button pressed in cycles 1, 8
Output
green_cycles = {0, 1, 2, 3, 11, 12, 13, 14, 22, 23, 24, 25}, yellow_cycles = {4, 5, 15, 16}, red_cycles = {6, 7, 8, 9, 10, 17, 18, 19, 20, 21}

The second press comes while people walk. It waits, so after the walk the light is green for its 4-cycle minimum and then goes yellow again.

Constraints and Contract

  • All inputs are known 0/1 values and change only between edges.
  • Keep the module name and the ports. Exactly one light is on in every cycle.

Hints

Hint 1

Count cycles in the present state with a small register. Clear it on every state change, and let it count up while the state stays.

Hint 2

Keep the press in a request register: set by button, cleared at the edge that enters WALK. Leave GREEN only when the request is set and the count has reached the minimum.

Hint 3

Decode each light from the present state, never from the next state, or every change shows a cycle early.

What the grader checks: Eight directed and twelve seeded button patterns over 26 cycles after a reset. In every cycle the three lights are compared with a JavaScript model of the crossing. Synthesis must report no latches.

The submitted design must synthesize with 0 latches and no combinational loop. Counts come from the pinned open-source synthesis flow. They are proxies for logic size and depth, not physical area or timing.

20 checks on Icarus Verilog + Yosys · server. 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 2 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 Press in Cycle 0

Input
button pressed in cycles 0
Expected output
green_cycles = {0, 1, 2, 3, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}, yellow_cycles = {4, 5}, red_cycles = {6, 7, 8, 9, 10}

The press is seen at the edge that ends cycle 0, so the request waits from cycle 1. Green lasts its minimum, cycles 0 to 3. Yellow is 4 and 5, walk 6 to 10, and green again from 11.

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

Icarus Verilog + Yosys · server