00:00:00.000 One line of RTL describes a piece of circuit In software, a line of code is a step. In RTL, a line of code describes a piece of circuit. This lesson pairs fifteen common constructs with the circuits they build. Each one goes through two checks: simulation for behavior, synthesis for the cells. 00:00:21.375 Ask a 1-bit question with a 1-bit operator empty answers one question: is the count zero? That is a 1-bit answer. Comparing with zero gets all three rows right. The tilde inverts each of the four bits, and a 1-bit output keeps only bit 0. With a count of 2 it wrongly reports empty, and no tool warns. 00:00:46.375 The carry needs a bit to land in Two four-bit readings add into a five-bit result. For 3 plus 4, both versions are right. For 15 plus 1, the version wrapped in braces gives 0. The addition runs in four bits and drops the carry first. Neither Icarus nor Yosys warns. 00:01:10.333 case picks one of three channels Three channels carry 0x12, 0xA5 and 0x3C. A case statement inside always_comb becomes a multiplexer. Select 00 picks A, 01 picks B and 10 picks C. The output follows the current inputs and stores nothing. 00:01:37.500 Without default, the output must remember Select 11 has no channel. The version on the left leaves that row out, so the output must remember its last value. Remembering takes a latch. The version on the right adds a default of 0x00. Every case now has a value, and the circuit stays a plain mux. 00:02:00.625 What the tools actually report In simulation, the first select of 11 leaves the bad version at 0xA5. After selecting 00, the next 11 leaves it at 0x12. The output depends on the previous choice. Yosys 0.33 stops the always_comb version with an error: a latch was inferred. 00:02:26.292 always_ff and <= build registers always_ff updates only on the rising clock edge. q1 stores D, and q2 stores q1's value from before the edge, so q2 is always one cycle behind. At E2, q1 is 0x34 and q2 is 0x12. F07 and F08 cover the timing in full. 00:02:56.708 Change <= to = and a stage disappears The left version uses blocking assignment. q1 updates at once, so the next line already reads the new value, and q2 equals q1. Yosys builds one register instead of two. Between registers, always use the nonblocking form. 00:03:19.833 Reset goes in the first branch The order of the if branches is the priority. At one rising edge, reset is asserted, the enable is on and d_i is 0xA5. The right version checks reset first and clears the register. The left version checks the enable first and stores 0xA5, and the testbench prints FAIL. 00:03:47.917 FSM: a state register and next-state logic An FSM has two parts: a register that holds the current state, and combinational logic that computes the next one. Start the next-state logic with state_d equals state_q. Without that line, Yosys again reports a latch. F10 covers drawing and testing the state diagram. 00:04:14.333 Code that only works in simulation These three lines matter in simulation, and synthesis ignores them. The sensitivity list misses b_i, so simulated y stays at 1 while Yosys still builds an AND gate. initial becomes only an init attribute, not a reset. Yosys drops #3 entirely. Use always_comb and a reset instead.