WEBVTT

1
00:00:00.250 --> 00:00:03.430
In software, a line of code is a step.

2
00:00:04.150 --> 00:00:08.530
In RTL, a line of code describes a piece
of circuit.

3
00:00:09.470 --> 00:00:14.230
This lesson pairs fifteen common
constructs with the circuits they build.

4
00:00:14.930 --> 00:00:16.670
Each one goes through two checks:

5
00:00:17.190 --> 00:00:20.530
simulation for behavior, synthesis for the
cells.

6
00:00:21.625 --> 00:00:25.785
empty answers one question: is the count
zero?

7
00:00:26.485 --> 00:00:28.105
That is a 1-bit answer.

8
00:00:29.085 --> 00:00:32.445
Comparing with zero gets all three rows
right.

9
00:00:33.465 --> 00:00:39.965
The tilde inverts each of the four bits,
and a 1-bit output keeps only bit 0.

10
00:00:40.605 --> 00:00:45.725
With a count of 2 it wrongly reports
empty, and no tool warns.

11
00:00:46.625 --> 00:00:50.725
Two four-bit readings add into a five-bit
result.

12
00:00:51.485 --> 00:00:54.965
For 3 plus 4, both versions are right.

13
00:00:55.745 --> 00:01:01.045
For 15 plus 1, the version wrapped in
braces gives 0.

14
00:01:01.745 --> 00:01:05.605
The addition runs in four bits and drops
the carry first.

15
00:01:06.545 --> 00:01:09.665
Neither Icarus nor Yosys warns.

16
00:01:10.583 --> 00:01:17.983
Three channels carry 0x12, 0xA5 and 0x3C.

17
00:01:18.963 --> 00:01:25.163
A case statement inside always_comb
becomes a multiplexer.

18
00:01:25.763 --> 00:01:32.083
Select 00 picks A, 01 picks B and 10 picks
C.

19
00:01:32.843 --> 00:01:36.643
The output follows the current inputs and
stores nothing.

20
00:01:37.750 --> 00:01:40.390
Select 11 has no channel.

21
00:01:41.210 --> 00:01:43.930
The version on the left leaves that row
out,

22
00:01:44.350 --> 00:01:47.090
so the output must remember its last
value.

23
00:01:47.750 --> 00:01:49.070
Remembering takes a latch.

24
00:01:50.130 --> 00:01:54.170
The version on the right adds a default of
0x00.

25
00:01:54.990 --> 00:01:59.670
Every case now has a value, and the
circuit stays a plain mux.

26
00:02:00.875 --> 00:02:07.775
In simulation, the first select of 11
leaves the bad version at 0xA5.

27
00:02:08.595 --> 00:02:13.795
After selecting 00, the next 11 leaves it
at 0x12.

28
00:02:14.715 --> 00:02:17.195
The output depends on the previous choice.

29
00:02:17.598 --> 00:02:25.635
Yosys 0.33 stops the always_comb version
with an error: a latch was inferred.

30
00:02:26.542 --> 00:02:32.282
always_ff updates only on the rising clock
edge.

31
00:02:32.922 --> 00:02:39.402
q1 stores D, and q2 stores q1's value from
before

32
00:02:39.402 --> 00:02:43.962
the edge, so q2 is always one cycle
behind.

33
00:02:44.942 --> 00:02:51.302
At E2, q1 is 0x34 and q2 is 0x12.

34
00:02:52.002 --> 00:02:55.822
F07 and F08 cover the timing in full.

35
00:02:56.958 --> 00:03:00.138
The left version uses blocking assignment.

36
00:03:01.118 --> 00:03:05.678
q1 updates at once, so the next line
already

37
00:03:05.678 --> 00:03:09.838
reads the new value, and q2 equals q1.

38
00:03:10.878 --> 00:03:14.018
Yosys builds one register instead of two.

39
00:03:14.978 --> 00:03:18.978
Between registers, always use the
nonblocking form.

40
00:03:20.083 --> 00:03:23.423
The order of the if branches is the
priority.

41
00:03:24.443 --> 00:03:32.863
At one rising edge, reset is asserted, the
enable is on and d_i is 0xA5.

42
00:03:33.983 --> 00:03:38.483
The right version checks reset first and
clears the register.

43
00:03:39.363 --> 00:03:42.003
The left version checks the enable first

44
00:03:42.003 --> 00:03:47.183
and stores 0xA5, and the testbench prints
FAIL.

45
00:03:48.167 --> 00:03:53.067
An FSM has two parts: a register that
holds the current state,

46
00:03:53.067 --> 00:03:57.167
and combinational logic that computes the
next one.

47
00:03:57.887 --> 00:04:04.467
Start the next-state logic with state_d
equals state_q.

48
00:04:05.247 --> 00:04:09.147
Without that line, Yosys again reports a
latch.

49
00:04:09.807 --> 00:04:13.327
F10 covers drawing and testing the state
diagram.

50
00:04:14.583 --> 00:04:20.463
These three lines matter in simulation,
and synthesis ignores them.

51
00:04:21.423 --> 00:04:26.223
The sensitivity list misses b_i, so
simulated y

52
00:04:26.223 --> 00:04:30.183
stays at 1 while Yosys still builds an AND
gate.

53
00:04:31.123 --> 00:04:35.743
initial becomes only an init attribute,
not a reset.

54
00:04:36.483 --> 00:04:39.143
Yosys drops #3 entirely.

55
00:04:40.043 --> 00:04:43.283
Use always_comb and a reset instead.

