TDEB Top Bar

Trending
Semiconductor Industry News — The Digital Electronics Blog
Follow Us

TDEB Masthead

The Semiconductor Hub

Semiconductors.  Chips and Systems.  Innovation.

Indexed by leading Search Engines, AI and referenced by Engineers worldwide 500+ Articles 10M+ Pageviews 30+ Reports 50+ Citations

Intel Folsom Interview Question: Reviewing VHDL Traffic Light Code

Murugavel Ganesan
by
0
Share Facebook X LinkedIn WhatsApp Email

Your next co-op job is with the Independent Eagle-Eyed Elective, a consulting firm that performs code reviews for other companies. Your first task is to review the VHDL code for a new traffic-light controller that has been written by Valerie Hazelton and David Langsdorf.

The question

Notes:

  1. The traffic-light controller is designed for conventional intersections of two roads, with green, yellow and red lights facing each of the four directions. There are no turn signals or blinking green lights.
  2. Valerie and David have checked that their code is legal and synthesizable.
  3. Val and Dave have not yet simulated or verified their code.
  4. Each of the four directions (North, South, East and West) has a traffic light.
  5. The North- and South-facing lights are both controlled by the NS light output of the controller.
  6. The East- and West-facing lights are both controlled by the EW light output of the controller.
  7. Each of the four directions has a car sensor (e.g. N_car) to detect when a car is waiting for the light to turn green.

The intersection, the entity and the architecture are shown below.














































Identify three changes that VH and DL should make to their code that will have the largest impact in improving the controller (e.g. optimizations or bug fixes). For each change:

  1. Identify the lines of code or signals affected by the change.
  2. Describe the change.
  3. Justify the importance of the change in terms of its impact on the controller.

How to approach a code review question

An interviewer who asks you to review code wants to see you rank problems, not list every one. Safety and correctness come first, then behavior that does not match the specification, then efficiency and style. For a traffic light, the most important property is simple: the two roads must never both have green at the same time. Check that first.

Then read the code the way the hardware will behave: which processes are clocked, which signals become flip-flops, and what every output is in every state, including right after reset.

The three changes, in short

  1. Fix the safety bug: both directions can turn green on the same clock edge.
  2. Fix the state machine structure: the next-state process is clocked, so the state is registered twice.
  3. Fix the light outputs: they are not set in every state or on reset, so the lights can be undefined.

The details, with line numbers, follow.

Deep dive: the numbers and details

This part transcribes the code from the images, then works through each change with line numbers and a cycle-by-cycle trace. Skip it if the overview is all you need.

The code, transcribed

 1) entity trafficlight is
 2)   port (
 3)     clk, reset : in std_logic;
 4)     N_car, S_car, E_car, W_car : in std_logic;
 5)     NS_light, EW_light : out std_logic_vector(2 downto 0)
 6)   );
 7) end trafficlight;
 8) architecture main of trafficlight is
 9)   constant green1_st  : std_logic_vector(5 downto 0) := "100001";
10)   constant green2_st  : std_logic_vector(5 downto 0) := "100010";
11)   constant green3_st  : std_logic_vector(5 downto 0) := "100100";
12)   constant yellow1_st : std_logic_vector(5 downto 0) := "101000";
13)   constant yellow2_st : std_logic_vector(5 downto 0) := "110000";
14)   constant red_st     : std_logic_vector(5 downto 0) := "000000";
16)   constant green  : std_logic_vector(2 downto 0) := "001";
17)   constant yellow : std_logic_vector(2 downto 0) := "010";
18)   constant red    : std_logic_vector(2 downto 0) := "100";
20)   signal NS_cur_st, NS_next_st, EW_cur_st, EW_next_st
21)          : std_logic_vector(5 downto 0);
23) begin
24)   NS: process begin
25)     wait until rising_edge(clk);
26)     case NS_cur_st is
27)       when red_st =>
28)         if (EW_cur_st = red_st) and
29)            ((N_car = '1') or (S_car = '1'))
30)         then
31)           NS_next_st <= green1_st;
32)           NS_light   <= green;
33)         end if;
34)       when green1_st =>
35)         NS_next_st <= green2_st;
36)       when green2_st =>
37)         NS_next_st <= green3_st;
38)       when green3_st =>
39)         NS_next_st <= yellow1_st;
40)         NS_light   <= yellow;
41)       when yellow1_st =>
42)         NS_next_st <= yellow2_st;
43)       when others =>
44)         NS_next_st <= red_st;
45)         NS_light   <= red;
46)     end case;
47)   end process;
48)   EW: process begin
49)     ...symmetric to NS process...
50)   end process;
51)   process (clk) begin
52)     if rising_edge(clk) then
53)       if reset = '1' then
54)         NS_cur_st <= red_st;
55)         EW_cur_st <= red_st;
56)       else
57)         NS_cur_st <= NS_next_st;
58)         EW_cur_st <= EW_next_st;
59)       end if;
60)     end if;
61)   end process;
62) end main;

Change 1: both roads can get green at once (lines 28 to 29, and the EW process)

Problem. NS leaves red_st when EW_cur_st = red_st and a car waits on N or S. The EW process is symmetric: it leaves red_st when NS_cur_st = red_st and a car waits on E or W. When both directions are red and cars are waiting on both roads, which is the normal situation right after reset or whenever traffic arrives on both roads during an all-red period, both conditions are true on the same clock edge, and both directions move to green1_st together.

Change. Make the decision in one place. Either replace the two machines with a single intersection state machine whose states are, for example, NS green, NS yellow, all red, EW green, EW yellow, all red; or keep two machines but break the tie, for example by letting EW leave red only if NS_cur_st = red_st and NS is not also requesting (no N or S car), and alternating priority so neither road starves.

Why it matters. Green in both directions is a collision. No optimization is worth anything until this is fixed. It is also a bug that simulation with only one car at a time would never find, which is why the question notes the code has not been verified.

Change 2: the state is registered twice (lines 24 to 25 and 51 to 58)

Problem. The NS process starts with wait until rising_edge(clk), so it is a clocked process: NS_next_st and NS_light become flip-flops. The process at lines 51 to 61 then registers NS_next_st again into NS_cur_st. With two registers in the loop, each edge does next := f(cur) and cur := old next, so NS_cur_st two cycles later equals f(NS_cur_st now). The machine runs as two interleaved copies, one on even clock cycles and one on odd cycles.

A cycle-by-cycle trace, assuming reset is held for edges 0 and 1, both state registers start unknown in simulation, EW stays red, and a car waits on the N/S road throughout ("green1" means green1_st):

EdgeNS_cur_st after the edgeNS_next_st after the edge
0 (reset)redred (unknown state falls to "when others")
1 (reset)redgreen1 (from red, car waiting)
2green1green1 (from red, the old current state)
3green1green2
4green2green2
5green2green3

Even in this best case every state lasts two clock cycles, so all the timing is doubled. Worse, the two copies only stay in step if every input holds steady across both edges. A car sensor that is high on one edge and low on the next (a car that drives off, or a noisy sensor) sends one copy to green1 and leaves the other in red, and NS_cur_st then alternates between them every cycle. The EW machine reads NS_cur_st, so it sees red and green on alternate cycles.

Change. Make the next-state logic combinational: replace wait until rising_edge(clk) with a sensitivity list (process (NS_cur_st, EW_cur_st, N_car, S_car), or process (all) in VHDL-2008), and assign NS_next_st on every path, for example with NS_next_st <= NS_cur_st; as a default at the top so that red_st holds when no car is waiting.

Why it matters. This is the difference between a working state machine and one whose behavior depends on which clock cycle you look at. The fix also removes six unneeded flip-flops per direction and one cycle of latency.

Change 3: the light outputs are not defined in every state or on reset (lines 32, 40, 45)

Problem. NS_light is assigned only on some transitions: green when leaving red, yellow when leaving green3, red in "when others". It is never assigned on reset. In hardware, if the state register powers up in red_st (or reset puts it there) while no car is waiting, no branch assigns NS_light, so the light keeps whatever value its flip-flop powered up with, which could be green, until the first car arrives. The light also changes on the same edge as NS_next_st, a clock before NS_cur_st, so the light and the state disagree for a cycle.

Change. Make the lights a function of the current state only (a Moore output), decoded combinationally:

NS_light <= green  when NS_cur_st = green1_st or NS_cur_st = green2_st
                        or NS_cur_st = green3_st else
            yellow when NS_cur_st = yellow1_st or NS_cur_st = yellow2_st else
            red;

Note that the state encoding already makes this easy: every green state starts with "100", and the yellow states start with "101" and "110", so the decode needs only the upper three bits.

Why it matters. An output that is undefined after reset is unsafe even if the state machine itself is correct. A light derived from the state can never disagree with it.

Other improvements worth mentioning

  • Use a counter for timing. green1 to green3 and yellow1 to yellow2 are just delays. A small counter with one green state and one yellow state is easier to change (real green times are seconds, which at any practical clock rate means thousands of cycles, far too many to write as states).
  • Encoding. The six states fit in 3 bits; the 6-bit encoding is not one-hot (red_st is all zeros) and buys little. Let the synthesis tool choose, or use an enumerated type.
  • Fairness. Once the tie-break in change 1 exists, check that a steady stream of N/S cars cannot keep E/W waiting forever.
  • Synchronize the car sensors. N_car to W_car come from the outside world and should pass through a two-flop synchronizer before the state machine uses them.

Related reading on this blog: Safe and Unsafe State Machines: FSM Encoding and Illegal States.

Murugavel Ganesan
About the Author
Murugavel Ganesan
Product Director | SerDes · IVN · Edge AI · Compute | Ex Intel · Infineon

A semiconductor product leader working across product architecture, definition, strategy, and silicon and software execution, from concept through volume production. He has worked with teams across Asia, Europe and the USA, including at Intel, Infineon and ArrayComm.

His work covers ASICs, SoCs, FPGAs and chiplets for wireless, wireline, automotive, IoT and AI, including PCIe, CXL and DDR interfaces alongside RF and mixed-signal design.

Share Facebook X LinkedIn WhatsApp Email

Post a Comment

0 Comments

Your comments will be moderated before it appears here.

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn more
Ok, Go it!