Professional Documents
Culture Documents
Verilog III
Verilog III
1DT109 ASPLOC
2022 VT1-VT2
2022-09-17 1
Agenda
• Conditional statement
• If-else statement
• The “: ?” conditional operator
• Case statement
• Finite state machine (FSM)
• A mini project
• We will do it together
2022-09-17 2
Conditional statement
• If-else statement
• The “: ?” conditional operator
• Case statement
2022-09-17 3
If-else statement
2022-09-17 4
If-else statement
• The if-else statement is used to determine whether
the statements within the if/else block should be
executed.
2022-09-17 5
If-else statement
• The if-else statement is used to determine whether
the statements within the if/else block should be
executed.
• If the expression evaluates to true,
2022-09-17 6
If-else statement
• The if-else statement is used to determine whether
the statements within the if/else block should be
executed.
• If the expression evaluates to true,
any non-zero value,
2022-09-17 7
If-else statement
• The if-else statement is used to determine whether
the statements within the if/else block should be
executed.
• If the expression evaluates to true,
any non-zero value,
all statements within that if block will be executed.
2022-09-17 8
If-else statement
• The if-else statement is used to determine whether
the statements within the if/else block should be
executed.
• If the expression evaluates to true,
any non-zero value,
all statements within that if block will be executed.
• If the expression evaluates to false,
2022-09-17 9
If-else statement
• The if-else statement is used to determine whether
the statements within the if/else block should be
executed.
• If the expression evaluates to true,
any non-zero value,
all statements within that if block will be executed.
• If the expression evaluates to false,
0 or ‘X’ or ‘Z’,
2022-09-17 10
If-else statement
• The if-else statement is used to determine whether the
statements within the if/else block should be executed.
• If the expression evaluates to true,
any non-zero value,
all statements within that if block will be executed.
• If the expression evaluates to false,
0 or ‘X’ or ‘Z’,
the statements within that if block will not be executed.
2022-09-17 11
If-else statement
• The if-else statement is used to determine whether the
statements within the if/else block should be executed.
• If the expression evaluates to true,
any non-zero value,
all statements within that if block will be executed.
• If the expression evaluates to false,
0 or ‘X’ or ‘Z’,
the statements within that if block will not be executed.
• If there is an else statement and the expression is false then
the statements within the else block will be executed (the
“default” case).
2022-09-17 12
If-else statement
• The if-else statement is used to determine whether the
statements within the if/else block should be executed.
• If the expression evaluates to true,
Syntax is very
any non-zero value,similar to C if statement.
But behaviors
all statements within that ifare very
block will different.
be executed.
• If the expression evaluates to false,
0 or ‘X’ or ‘Z’,
the statements within that if block will not be executed.
• If there is an else statement and the expression is false then
the statements within the else block will be executed (the
“default” case).
2022-09-17 13
If-else statement
2022-09-17 14
If-else statement
module demo_1(
input D, clk, n_rst,
output Q);
reg reg_Q;
assign Q = reg_Q;
if(!n_rst)
reg_Q <= 1'b0;
else
reg_Q <= D;
end
endmodule
2022-09-17 15
If-else statement
module demo_1(
input D, clk, n_rst,
output Q);
reg reg_Q;
assign Q = reg_Q;
if(!n_rst)
reg_Q <= 1'b0;
else
reg_Q <= D;
end
endmodule
2022-09-17 16
If-else statement
module demo_1( module demo_2(
input D, clk, n_rst, input D, clk, n_rst,
output Q); output Q);
endmodule endmodule
2022-09-17 17
If-else statement
module demo_1( module demo_2(
input D, clk, n_rst, input D, clk, n_rst,
output Q); output Q);
endmodule endmodule
2022-09-17 18
If-else statement
module demo_1( module demo_2( module demo_3(
input D, clk, n_rst, input D, clk, n_rst, input D, clk, rst,
output Q); output Q); output Q);
always @ (posedge clk) begin always @ (posedge clk, always @ (posedge clk,
negedge n_rst) begin posedge rst) begin
if(!n_rst) if(!n_rst) if(rst)
reg_Q <= 1'b0; reg_Q <= 1'b0; reg_Q <= 1'b0;
else else else
reg_Q <= D; reg_Q <= D; reg_Q <= D;
end end end
always @ (posedge clk) begin always @ (posedge clk, always @ (posedge clk,
negedge n_rst) begin posedge rst) begin
if(!n_rst) if(!n_rst) if(rst)
reg_Q <= 1'b0; reg_Q <= 1'b0; reg_Q <= 1'b0;
else else else
reg_Q <= D; reg_Q <= D; reg_Q <= D;
end end end
if(!en) if(en)
reg_Q <= 1'b0; reg_Q <= D;
else
reg_Q <= D;
end end
endmodule endmodule
2022-09-17 21
If-else statement
module demo_4( module demo_5(
input D, en, input D, en,
output Q); output Q);
if(!en) if(en)
reg_Q <= 1'b0; reg_Q <= D;
else
reg_Q <= D;
• If enabled, the output
end follows the input. end
• Otherwise, the output
endmodule is reset to 0. endmodule
2022-09-17 22
If-else statement
module demo_4( module demo_5(
input D, en, input D, en,
output Q); output Q);
if(!en) if(en)
reg_Q <= 1'b0; reg_Q <= D;
else
reg_Q <= D;
• If enabled, the output
end follows the input. end
• Otherwise, the output
endmodule is reset to 0. endmodule
2022-09-17 23
If-else statement
module demo_4( module demo_5(
input D, en, input D, en,
output Q); output Q);
if(!en) if(en)
reg_Q <= 1'b0; reg_Q <= D;
else
reg_Q <= D;
• If enabled, the output
end follows the input. end
• Otherwise, the output
endmodule is reset to 0. endmodule
2022-09-17 24
If-else statement
module demo_4( module demo_5(
input D, en, input D, en,
output Q); output Q);
if(!en) if(en)
reg_Q <= 1'b0; reg_Q <= D;
else
reg_Q <= D;
• If enabled, the output
end follows the input. end
• Otherwise, the output
endmodule is reset to 0. endmodule
2022-09-17 25
If-else statement
module demo_4( module demo_5(
input D, en, input D, en,
output Q); output Q);
if(!en) if(en)
reg_Q <= 1'b0; reg_Q <= D;
else
reg_Q <= D;
• If enabled, the output
end follows the input. end
• Otherwise, the output
endmodule is reset to 0. endmodule
2022-09-17 26
If-else statement
module demo_4( module demo_5(
input D, en, input D, en,
output Q); output Q);
Rules of thumb
assign Q No.7:
follows the input.
assign Q = reg_Q; = reg_Q; • Otherwise, the output
if(!en) avoid
reg_Q <= 1'b0;
auto-generated
if(en)
reg_Q <= D;
latch.
else
reg_Q <= D;
• If enabled, the output
end follows the input. end
• Otherwise, the output
endmodule is reset to 0. endmodule
2022-09-17 27
Let’s play some tricks
module demo_6 (
input [1:0] sel,
input A, B, C, D,
output Q);
reg reg_Q;
assign Q = reg_Q;
reg reg_Q;
assign Q = reg_Q;
reg reg_Q;
assign Q = reg_Q;
… // demo_7
always @ (A, B, C, D, sel) begin always @ (A, B, C, D, sel) begin
if (sel == 2'b00) if (sel == 2'b00)
reg_Q <= A; reg_Q <= A;
else if (sel == 2'b01) else if (sel == 2'b01)
reg_Q <= B; reg_Q <= B;
else if (sel == 2'b10) else if (sel == 2'b10)
reg_Q <= C; reg_Q <= C;
else else if (sel == 2’b11)
reg_Q <= D; reg_Q <= D;
end end
endmodule
2022-09-17 endmodule 30
Let’s play some tricks
module demo_6 (
input [1:0] sel,
input A, B, C, D,
output Q);
reg reg_Q;
assign Q = reg_Q;
… // demo_7
always @ (A, B, C, D, sel) begin always @ (A, B, C, D, sel) begin
if (sel == 2'b00) if (sel == 2'b00)
reg_Q <= A; reg_Q <= A;
else if (sel == 2'b01) else if (sel == 2'b01)
reg_Q <= B; reg_Q <= B; Equivalent
else if (sel == 2'b10) else if (sel == 2'b10) as before?
reg_Q <= C; reg_Q <= C;
else else if (sel == 2’b11)
reg_Q <= D; reg_Q <= D;
end end
endmodule
2022-09-17 endmodule 31
Let’s play some tricks
module demo_6 (
input [1:0] sel,
input A, B, C, D,
output Q);
reg reg_Q;
assign Q = reg_Q;
… // demo_7
always @ (A, B, C, D, sel) begin always @ (A, B, C, D, sel) begin
if (sel == 2'b00) if (sel == 2'b00)
reg_Q <= A; reg_Q <= A;
else if (sel == 2'b01) else if (sel == 2'b01)
reg_Q <= B; reg_Q <= B; Equivalent
else if (sel == 2'b10) else if (sel == 2'b10) as before?
reg_Q <= C; reg_Q <= C;
else else if (sel == 2’b11)
reg_Q <= D; reg_Q <= D;
end end
endmodule
2022-09-17 endmodule 32
Let’s play some tricks
module demo_6 (
input [1:0] sel,
input A, B, C, D,
output Q);
reg reg_Q;
assign Q = reg_Q;
… // demo_7
always @ (A, B, C, D, sel) begin always @ (A, B, C, D, sel) begin
if (sel == 2'b00) if (sel == 2'b00)
reg_Q <= A; reg_Q <= A;
else if (sel == 2'b01) else if (sel == 2'b01) Always enclose an if
reg_Q <= B; reg_Q <= B; Equivalent with an else to
else if (sel == 2'b10) else if (sel == 2'b10) as before? avoid auto-generated
reg_Q <= C; reg_Q <= C; latch.
else else if (sel == 2’b11)
reg_Q <= D; reg_Q <= D;
end end
endmodule
2022-09-17 endmodule 33
Let’s play some tricks
module demo_6 (
input [1:0] sel,
input A, B, C, D,
output Q);
reg reg_Q;
assign Q = reg_Q;
… // demo_7
always @ (A, B, C, D, sel) begin always @ (A, B, C, D, sel) begin
if (sel == 2'b00) if (sel == 2'b00)
reg_Q <= A; reg_Q <= A;
else if (sel == 2'b01) else if (sel == 2'b01) Always enclose an if
reg_Q <= B; reg_Q <= B; Equivalent with an else to
else if (sel == 2'b10) else if (sel == 2'b10) as before? avoid auto-generated
reg_Q <= C; reg_Q <= C; latch.
else else if (sel == 2’b11)
reg_Q <= D; reg_Q <= D;
end end
endmodule
2022-09-17 endmodule 34
Nested if-else statement
What if we want to do:
1. Q = A when sel == 00
2. Q = B when sel == 01
3. Q = C when all other cases
2022-09-17 35
Nested if-else statement
What if we want to do:
1. Q = A when sel == 00
2. Q = B when sel == 01
3. Q = C when all other cases
end 2022-09-17 36
Nested if-else statement
What if we want to do:
1. Q = A when sel == 00
2. Q = B when sel == 01
3. Q = C when all other cases
Way 1 (demo_8)
always @ (A, B, C, sel) begin
if (sel == 1'b00)
reg_Q <= A;
else if (sel == 1'b01)
reg_Q <= B;
else
reg_Q <= C;
end 2022-09-17 37
Nested if-else statement
What if we want to do:
1. Q = A when sel == 00
2. Q = B when sel == 01
3. Q = C when all other cases
end 2022-09-17 38
Nested if-else statement
What if we want to do:
1. Q = A when sel == 00
2. Q = B when sel == 01
3. Q = C when all other cases
2022-09-17 57
Improve deep if-else
• Insight: balance the
HW structure.
2022-09-17 58
Improve deep if-else
• Insight: balance the
HW structure.
• 3’b000 → 8’h01
2022-09-17 59
Improve deep if-else
• Insight: balance the
HW structure.
• 3’b000 → 8’h01
• 3’b001 → 8’h02
2022-09-17 60
Improve deep if-else
• Insight: balance the
HW structure.
• 3’b000 → 8’h01
• 3’b001 → 8’h02
• 3’b010 → 8’h04
2022-09-17 61
Improve deep if-else
• Insight: balance the
HW structure.
• 3’b000 → 8’h01
• 3’b001 → 8’h02
• 3’b010 → 8’h04
• 3’b011 → 8’h08
2022-09-17 62
Improve deep if-else
• Insight: balance the
HW structure.
• 3’b000 → 8’h01
• 3’b001 → 8’h02
• 3’b010 → 8’h04
• 3’b011 → 8’h08
• 3’b100 → 8’h10
2022-09-17 63
Improve deep if-else
• Insight: balance the
HW structure.
• 3’b000 → 8’h01
• 3’b001 → 8’h02
• 3’b010 → 8’h04
• 3’b011 → 8’h08
• 3’b100 → 8’h10
• 3’b101 → 8’h20
2022-09-17 64
Improve deep if-else
• Insight: balance the
HW structure.
• 3’b000 → 8’h01
• 3’b001 → 8’h02
• 3’b010 → 8’h04
• 3’b011 → 8’h08
• 3’b100 → 8’h10
• 3’b101 → 8’h20
• 3’b110 → 8’h40
2022-09-17 65
Improve deep if-else
• Insight: balance the
HW structure.
• 3’b000 → 8’h01
• 3’b001 → 8’h02
• 3’b010 → 8’h04
• 3’b011 → 8’h08
• 3’b100 → 8’h10
• 3’b101 → 8’h20
• 3’b110 → 8’h40
• 3’b111 → 8’h80
2022-09-17 66
Improve deep if-else
• Insight: balance the
HW structure.
• 3’b000 → 8’h01
• 3’b001 → 8’h02
• 3’b010 → 8’h04
• 3’b011 → 8’h08
• 3’b100 → 8’h10
• 3’b101 → 8’h20
• 3’b110 → 8’h40
• 3’b111 → 8’h80
2022-09-17 67
Improve deep if-else
• Insight: balance the
HW structure.
• 3’b000 → 8’h01
in[2]=0
• 3’b001 → 8’h02
• 3’b010 → 8’h04
• 3’b011 → 8’h08
• 3’b100 → 8’h10
• 3’b101 → 8’h20
• 3’b110 → 8’h40
• 3’b111 → 8’h80
2022-09-17 68
Improve deep if-else
• Insight: balance the
HW structure.
• 3’b000 → 8’h01
in[2]=0
• 3’b001 → 8’h02
• 3’b010 → 8’h04
• 3’b011 → 8’h08
• 3’b100 → 8’h10
• 3’b101 → 8’h20
• 3’b110 → 8’h40
• 3’b111 → 8’h80
2022-09-17 69
Improve deep if-else
• Insight: balance the
HW structure.
• 3’b000 → 8’h01
in[2]=0
• 3’b001 → 8’h02
• 3’b010 → 8’h04
• 3’b011 → 8’h08
• 3’b100 → 8’h10
in[2]=1
• 3’b101 → 8’h20
• 3’b110 → 8’h40
• 3’b111 → 8’h80
2022-09-17 70
Improve deep if-else
• Insight: balance the
HW structure.
• 3’b000 → 8’h01
in[2]=0
• 3’b001 → 8’h02
• 3’b010 → 8’h04
• 3’b011 → 8’h08
• 3’b100 → 8’h10
in[2]=1
• 3’b101 → 8’h20
• 3’b110 → 8’h40
• 3’b111 → 8’h80
2022-09-17 71
Improve deep if-else
• Insight: balance the
HW structure.
• 3’b000 → 8’h01
in[1]=0
in[2]=0
• 3’b001 → 8’h02
• 3’b010 → 8’h04
• 3’b011 → 8’h08
• 3’b100 → 8’h10
in[2]=1
• 3’b101 → 8’h20
• 3’b110 → 8’h40
• 3’b111 → 8’h80
2022-09-17 72
Improve deep if-else
• Insight: balance the
HW structure.
• 3’b000 → 8’h01
in[1]=0
in[2]=0
• 3’b001 → 8’h02
• 3’b010 → 8’h04
• 3’b011 → 8’h08
• 3’b100 → 8’h10
in[2]=1
• 3’b101 → 8’h20
• 3’b110 → 8’h40
• 3’b111 → 8’h80
2022-09-17 73
Improve deep if-else
• Insight: balance the
HW structure.
• 3’b000 → 8’h01
in[1]=0
in[2]=0
• 3’b001 → 8’h02
• 3’b010 → 8’h04
in[1]=1
• 3’b011 → 8’h08
• 3’b100 → 8’h10
in[2]=1
• 3’b101 → 8’h20
• 3’b110 → 8’h40
• 3’b111 → 8’h80
2022-09-17 74
Improve deep if-else
• Insight: balance the
HW structure.
• 3’b000 → 8’h01
in[1]=0
in[2]=0
• 3’b001 → 8’h02
• 3’b010 → 8’h04
in[1]=1
• 3’b011 → 8’h08
• 3’b100 → 8’h10
in[2]=1
• 3’b101 → 8’h20
• 3’b110 → 8’h40
• 3’b111 → 8’h80
2022-09-17 75
Improve deep if-else
• Insight: balance the
HW structure.
• 3’b000 → 8’h01
in[1]=0
in[2]=0
• 3’b001 → 8’h02
• 3’b010 → 8’h04
in[1]=1
• 3’b011 → 8’h08
• 3’b100 → 8’h10
in[1]=0
in[2]=1
• 3’b101 → 8’h20
• 3’b110 → 8’h40
• 3’b111 → 8’h80
2022-09-17 76
Improve deep if-else
• Insight: balance the
HW structure.
• 3’b000 → 8’h01
in[1]=0
in[2]=0
• 3’b001 → 8’h02
• 3’b010 → 8’h04
in[1]=1
• 3’b011 → 8’h08
• 3’b100 → 8’h10
in[1]=0
in[2]=1
• 3’b101 → 8’h20
• 3’b110 → 8’h40
• 3’b111 → 8’h80
2022-09-17 77
Improve deep if-else
• Insight: balance the
HW structure.
• 3’b000 → 8’h01
in[1]=0
in[2]=0
• 3’b001 → 8’h02
in[1]=1
• 3’b010 → 8’h04
• 3’b011 → 8’h08
• 3’b100 → 8’h10
in[1]=0
in[2]=1
• 3’b101 → 8’h20
in[1]=1• 3’b110 → 8’h40
2022-09-17
• 3’b111 → 8’h80 78
always @ (in) begin
if (in[2] == 1'b0)
if(in[1] == 1'b0)
Improve deep if-else if(in[0] == 1'b0)
reg_out = 8'h0_1;
else
reg_out = 8'h0_2;
• Insight: balance the else // in[1] == 1’b1
if(in[0] == 1'b0)
HW structure. reg_out = 8'h0_4;
in[1]=1
• 3’b010 → 8’h04 if(in[0] == 1'b0)
reg_out = 8'h1_0;
• 3’b011 → 8’h08 else
reg_out = 8'h2_0;
• 3’b100 → 8’h10 else // in[1] == 1’b1
in[1]=0 if(in[0] == 1'b0)
in[2]=1
• 3’b101 → 8’h20 reg_out = 8'h4_0;
in[1]=1
• 3’b010 → 8’h04 if(in[0] == 1'b0)
reg_out = 8'h1_0;
• 3’b011 → 8’h08 else
reg_out = 8'h2_0;
• 3’b100 → 8’h10 else // in[1] == 1’b1
in[1]=0 if(in[0] == 1'b0)
in[2]=1
• 3’b101 → 8’h20 reg_out = 8'h4_0;
• 3’b000Balanced
→ 8’h01 data-path, no ROM.
else
reg_out = 8'h0_8;
in[1]=0
in[2]=0 Hard to tune. Not always possible to balance.
• 3’b001 → 8’h02 else
if(in[1] == 1'b0)
in[1]=1
• 3’b010 → 8’h04 if(in[0] == 1'b0)
reg_out = 8'h1_0;
• 3’b011 → 8’h08 else
reg_out = 8'h2_0;
• 3’b100 → 8’h10 else // in[1] == 1’b1
in[1]=0 if(in[0] == 1'b0)
in[2]=1
• 3’b101 → 8’h20 reg_out = 8'h4_0;
• 3’b000Balanced
→ 8’h01 data-path, no ROM.
else
reg_out = 8'h0_8;
in[1]=0
in[2]=0 Hard to tune. Not always possible to balance.
• 3’b001 → 8’h02 else
if(in[1] == 1'b0)
• 3’b010 → 8’h04
in[1]=1
if(in[0] == 1'b0)
Use case statement. reg_out = 8'h1_0;
• 3’b011 → 8’h08 else
reg_out = 8'h2_0;
• 3’b100 → 8’h10 else // in[1] == 1’b1
in[1]=0 if(in[0] == 1'b0)
in[2]=1
• 3’b101 → 8’h20 reg_out = 8'h4_0;
2022-09-17 83
The “: ?” conditional operator
• Also called as inline if.
• Syntax
condition ? value_if_true : value_if_false
… // demo_4
if(!en)
reg_Q <= 1'b0;
else
reg_Q <= D;
end
2022-09-17 84
The “: ?” conditional operator
• Also called as inline if.
• Syntax
condition ? value_if_true : value_if_false
… // demo_4
if(!en)
reg_Q <= 1'b0;
else
reg_Q <= D;
end
2022-09-17 85
The “: ?” conditional operator
demo_4
• Also called as inline if.
• Syntax
condition ? value_if_true : value_if_false
… // demo_4
if(!en)
reg_Q <= 1'b0;
else
reg_Q <= D;
end
2022-09-17 86
The “: ?” conditional operator
demo_4
• Also called as inline if.
• Syntax
condition ? value_if_true : value_if_false
… // demo_4 … // demo_9
end end
2022-09-17 87
The “: ?” conditional operator
demo_4
• Also called as inline if.
• Syntax
condition ? value_if_true : value_if_false
… // demo_4 … // demo_9
end end
2022-09-17 88
The “: ?” conditional operator
demo_4 demo_9
• Also called as inline if.
• Syntax
condition ? value_if_true : value_if_false
… // demo_4 … // demo_9
end end
2022-09-17 89
The “: ?” conditional operator
demo_4 demo_9
• Also called as inline if.
• Syntax
condition ? value_if_true : value_if_false
… // demo_4 … // demo_9 … // demo_13
always @ (D, en) begin always @ (A, B, C, sel) begin assign Q = en ? D : 1’b0;
if (sel == 1'b00)
if(!en) reg_Q <= A; endmodule
reg_Q <= 1'b0; else if (sel == 1'b01)
else reg_Q <= B;
reg_Q <= D; else
reg_Q <= C;
end end
2022-09-17 90
The “: ?” conditional operator
demo_4 demo_9
• Also called as inline if.
• Syntax
condition ? value_if_true : value_if_false
… // demo_4 … // demo_9 … // demo_13
always @ (D, en) begin always @ (A, B, C, sel) begin assign Q = en ? D : 1’b0;
if (sel == 1'b00)
if(!en) reg_Q <= A; endmodule
reg_Q <= 1'b0; else if (sel == 1'b01)
else reg_Q <= B;
reg_Q <= D; else
reg_Q <= C;
end end
2022-09-17 91
The “: ?” conditional operator
demo_4 demo_9
• Also called as inline if.
• Syntax
condition ? value_if_true : value_if_false
… // demo_4 … // demo_9 … // demo_13
end end
2022-09-17 92
The “: ?” conditional operator
demo_4 demo_9
• Also called as inline if.
• Syntax
condition ? value_if_true : value_if_false
… // demo_4 … // demo_9 … // demo_13
2022-09-17 96
Case statement
• The case statement checks if the given expression matches
one among the case branches.
• If none of the case matches the given expression, statements within
the default branch are evaluated.
case (expression)
case_branch_1 : A
case_branch_2,
case_branch_3 : B
case_branch_4 :
begin
C1, C2
end
default: D
2022-09-17 endcase 97
Case statement
• The case statement checks if the given expression matches
one among the case branches.
• If none of the case matches the given expression, statements within
the default branch are evaluated.
reg reg_Q;
assign Q = reg_Q;
2022-09-17 111
The depth of a case (3-8 decoder)
2022-09-17 112
The depth of a case (3-8 decoder)
2022-09-17 113
The don’t-care “?” in case statement
2022-09-17 114
The don’t-care “?” in case statement
end
2022-09-17 115
The don’t-care “?” in case statement
demo_8
always @ (A, B, C, sel) begin
if (sel == 1'b00)
reg_Q <= A;
else if (sel == 1'b01)
reg_Q <= B;
else
reg_Q <= C;
end
2022-09-17 116
The don’t-care “?” in case statement
demo_8
always @ (A, B, C, sel) begin
if (sel == 1'b00)
reg_Q <= A;
else if (sel == 1'b01)
reg_Q <= B;
else
reg_Q <= C;
end
2022-09-17 117
The don’t-care “?” in case statement
demo_8
always @ (A, B, C, sel) begin always @ (A, B, C, sel) begin
if (sel == 1'b00) case(sel)
reg_Q <= A; 2'b00:
else if (sel == 1'b01) reg_Q = A;
reg_Q <= B; 2'b01:
else reg_Q = B;
reg_Q <= C; default:
reg_Q = C;
end endcase
end
2022-09-17 118
The don’t-care “?” in case statement
demo_8 demo_16
always @ (A, B, C, sel) begin always @ (A, B, C, sel) begin
if (sel == 1'b00) case(sel)
reg_Q <= A; 2'b00:
else if (sel == 1'b01) reg_Q = A;
reg_Q <= B; 2'b01:
else reg_Q = B;
reg_Q <= C; default:
reg_Q = C;
end endcase
end
2022-09-17 119
The don’t-care “?” in case statement
demo_8 demo_16
always @ (A, B, C, sel) begin always @ (A, B, C, sel) begin
if (sel == 1'b00) case(sel)
reg_Q <= A; 2'b00:
else if (sel == 1'b01) reg_Q = A;
reg_Q <= B; 2'b01:
else reg_Q = B;
reg_Q <= C; default:
reg_Q = C;
end endcase
end
2022-09-17 120
The don’t-care “?” in case statement
demo_8 demo_16
always @ (A, B, C, sel) begin always @ (A, B, C, sel) begin always @ (A, B, C, sel) begin
if (sel == 1'b00) case(sel) casex(sel) // or casez(sel)
reg_Q <= A; 2'b00: 2'b00:
else if (sel == 1'b01) reg_Q = A; reg_Q = A;
reg_Q <= B; 2'b01: 2'b01:
else reg_Q = B; reg_Q = B;
reg_Q <= C; default: 2’b1?: // for both ’10’ and ’11’
reg_Q = C; reg_Q = C;
end endcase endcase
end end
2022-09-17 121
The don’t-care “?” in case statement
2022-09-17 124
Case, casez, casex
• In Verilog, casez and casex are special case statement.
2022-09-17 125
Case, casez, casex
• In Verilog, casez and casex are special case statement.
• In casez
‘Z’ and ‘?’ are treated as don’t-care
in both case expression and case branch
2022-09-17 126
Case, casez, casex
• In Verilog, casez and casex are special case statement.
• In casez
‘Z’ and ‘?’ are treated as don’t-care
in both case expression and case branch
• In casex
‘Z’, ‘X’ and ‘?’ are treated as don’t-care
in both case expression and case branch
2022-09-17 127
Case, casez, casex
• In Verilog, casez and casex are special case statement.
• In casez
‘Z’ and ‘?’ are treated as don’t-care
in both case expression and case branch
• In casex
‘Z’, ‘X’ and ‘?’ are treated as don’t-care
in both case expression and case branch
• In case
‘1’, ‘0’, ‘X’, ‘Z’ are treated as four different values
Don’t use don’t-care in case, use them in casez/casex
2022-09-17 128
Case, casez, casex
• In Verilog, casez and casex are special case statement.
• In casez
‘Z’ and ‘?’ are treated as don’t-care
in both case expression and case branch
• In casex
‘Z’, ‘X’ and ‘?’ are treated as don’t-care
in both case expression and case branch
• In case
‘1’, ‘0’, ‘X’, ‘Z’ are treated as four different values
Don’t use don’t-care in case, use them in casez/casex
• In casez and casex, if multiple case branches matches
the expression, the first match dominates.
2022-09-17 129
Examples: case, casex, casez
2022-09-17 130
Examples: case, casex, casez
reg sel = 1'bZ;
initial begin
case(sel)
1'b0:
$display("A");
1'b1:
$display("B");
1'bZ:
$display("C");
1'bX:
$display("D");
default:
$display("default");
endcase
end //demo_17
C
2022-09-17 131
Examples: case, casex, casez
reg sel = 1'bZ;
initial begin
case(sel)
1'b0:
$display("A");
1'b1:
$display("B");
1'bZ:
$display("C");
1'bX:
$display("D");
default:
$display("default");
endcase
end //demo_17
C
2022-09-17 132
Examples: case, casex, casez
reg sel = 1'bZ; reg sel = 1'bX;
C C
2022-09-17 133
Examples: case, casex, casez
reg sel = 1'bZ; reg sel = 1'bX;
C C
2022-09-17 134
Examples: case, casex, casez
reg sel = 1'bZ; reg sel = 1'bX; reg sel = 1'bX;
C C C
2022-09-17 135
Examples: case, casex, casez
reg sel = 1'bZ; reg sel = 1'bX; reg sel = 1'bX;
C C C
2022-09-17 136
Examples: case, casex, casez
reg sel = 1'bZ; reg sel = 1'bX; reg sel = 1'bX; reg sel = 1'bZ;
C C C A
2022-09-17 137
Examples: case, casex, casez
reg sel = 1'bZ; reg sel = 1'bX; reg sel = 1'bX; reg sel = 1'bZ;
C C C A
2022-09-17 138
Examples: case, casex, casez
reg sel = 1'bZ; reg sel = 1'bX; reg sel = 1'bX; reg sel = 1'bZ; reg sel = 1'bX;
initial begin initial begin initial begin initial begin initial begin
case(sel) casez(sel) casez(sel) casez(sel) casex(sel)
1'b0: 1'b0: 1'b0: 1'b0: 1'b0:
$display("A"); $display("A"); $display("A"); $display("A"); $display("A");
1'b1: 1'b1: 1'b1: 1'b1: 1'b1:
$display("B"); $display("B"); $display("B"); $display("B"); $display("B");
1'bZ: 1’bZ: 1'bX: 1'bX: 1'bX:
$display("C"); $display("C"); $display("C"); $display("C"); $display("C");
1'bX: 1’b?: 1'bZ: 1'bZ: 1'bZ:
$display("D"); $display("D"); $display("D"); $display("D"); $display("D");
default: 1'bX: 1'b?: 1'b?: 1'b?:
$display("default"); $display("E"); $display("E"); $display("E"); $display("E");
endcase default: default: default: default:
end //demo_17 $display("default"); $display("default"); $display("default"); $display("default");
endcase endcase endcase endcase
end //demo_18 end //demo_19 end //demo_20 end //demo_21
C C C A A
2022-09-17 139
Examples: case, casex, casez
reg sel = 1'bZ; reg sel = 1'bX; reg sel = 1'bX; reg sel = 1'bZ; reg sel = 1'bX;
initial begin initial begin initial begin initial begin initial begin
case(sel) casez(sel) casez(sel) casez(sel) casex(sel)
1'b0: 1'b0: 1'b0: 1'b0: 1'b0:
$display("A"); $display("A"); $display("A"); $display("A"); $display("A");
1'b1: 1'b1: 1'b1: 1'b1: 1'b1:
$display("B"); $display("B"); $display("B"); $display("B"); $display("B");
1'bZ: 1’bZ: 1'bX: 1'bX: 1'bX:
$display("C"); $display("C"); $display("C"); $display("C"); $display("C");
1'bX: 1’b?: 1'bZ: 1'bZ: 1'bZ:
$display("D"); $display("D"); $display("D"); $display("D"); $display("D");
default: 1'bX: 1'b?: 1'b?: 1'b?:
$display("default"); $display("E"); $display("E"); $display("E"); $display("E");
endcase default: default: default: default:
end //demo_17 $display("default"); $display("default"); $display("default"); $display("default");
endcase endcase endcase endcase
end //demo_18 end //demo_19 end //demo_20 end //demo_21
C C C A A
2022-09-17 140
Examples: case, casex, casez
reg sel = 1'bZ; reg sel = 1'bX; reg sel = 1'bX; reg sel = 1'bZ; reg sel = 1'bX;
initial begin initial begin initial begin initial begin initial begin
case(sel) casez(sel) casez(sel) casez(sel) casex(sel)
1'b0: 1'b0: 1'b0: 1'b0: 1'b0:
$display("A"); $display("A"); $display("A"); $display("A"); $display("A");
1'b1: 1'b1: 1'b1: 1'b1: 1'b1:
$display("B");
1'bZ: All don’t-cares are non-synthesizable.
$display("B");
1’bZ:
$display("B");
1'bX:
$display("B");
1'bX:
$display("B");
1'bX:
$display("C"); $display("C"); $display("C"); $display("C"); $display("C");
1'bX: 1’b?: 1'bZ: 1'bZ: 1'bZ:
$display("D"); $display("D"); $display("D"); $display("D"); $display("D");
default: 1'bX: 1'b?: 1'b?: 1'b?:
$display("default"); $display("E"); $display("E"); $display("E"); $display("E");
endcase default: default: default: default:
end //demo_17 $display("default"); $display("default"); $display("default"); $display("default");
endcase endcase endcase endcase
end //demo_18 end //demo_19 end //demo_20 end //demo_21
C C C A A
2022-09-17 141
Examples: case, casex, casez
2022-09-17 142
Examples: case, casex, casez
reg sel = 1'bX;
2022-09-17 146
Finite state machines
• A digital system can generally be divided into control
unit and datapath.
2022-09-17 147
Finite state machines
• A digital system can generally be divided into control
unit and datapath.
• For example, in a processor circuit:
2022-09-17 148
Finite state machines
• A digital system can generally be divided into control
unit and datapath.
• For example, in a processor circuit:
2022-09-17 149
Finite state machines
2022-09-17 150
Finite state machines
• FSM is a method to model control logic.
2022-09-17 151
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
2022-09-17 152
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
1. The combinational logic is used to decide the next state of the FSM.
2022-09-17 153
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
1. The combinational logic is used to decide the next state of the FSM.
Next state
Logic
2022-09-17 154
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
1. The combinational logic is used to decide the next state of the FSM.
Next state
Logic
2022-09-17 155
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
1. The combinational logic is used to decide the next state of the FSM.
Next state
Logic
2022-09-17 156
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
1. The combinational logic is used to decide the next state of the FSM.
Next state
Logic
input
2022-09-17 157
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
1. The combinational logic is used to decide the next state of the FSM.
2. The sequential logic is used to store the current state of the FSM.
Next state
Logic
input
2022-09-17 158
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
1. The combinational logic is used to decide the next state of the FSM.
2. The sequential logic is used to store the current state of the FSM.
input
2022-09-17 159
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
1. The combinational logic is used to decide the next state of the FSM.
2. The sequential logic is used to store the current state of the FSM.
2022-09-17 160
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
1. The combinational logic is used to decide the next state of the FSM.
2. The sequential logic is used to store the current state of the FSM.
2022-09-17 161
clk
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
1. The combinational logic is used to decide the next state of the FSM.
2. The sequential logic is used to store the current state of the FSM.
2022-09-17 162
clk
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
1. The combinational logic is used to decide the next state of the FSM.
2. The sequential logic is used to store the current state of the FSM.
2022-09-17 163
clk
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
1. The combinational logic is used to decide the next state of the FSM.
2. The sequential logic is used to store the current state of the FSM.
2022-09-17 164
clk
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
1. The combinational logic is used to decide the next state of the FSM.
2. The sequential logic is used to store the current state of the FSM.
2022-09-17 165
clk
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
1. The combinational logic is used to decide the next state of the FSM.
2. The sequential logic is used to store the current state of the FSM.
2022-09-17 166
clk
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
1. The combinational logic is used to decide the next state of the FSM.
2. The sequential logic is used to store the current state of the FSM.
2022-09-17 167
clk
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
1. The combinational logic is used to decide the next state of the FSM.
2. The sequential logic is used to store the current state of the FSM.
2022-09-17 168
clk
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
1. The combinational logic is used to decide the next state of the FSM.
2. The sequential logic is used to store the current state of the FSM.
3. The output logic is used to generate output based on the current state of the FSM.
2022-09-17 169
clk
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
1. The combinational logic is used to decide the next state of the FSM.
2. The sequential logic is used to store the current state of the FSM.
3. The output logic is used to generate output based on the current state of the FSM.
2022-09-17 170
clk
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
1. The combinational logic is used to decide the next state of the FSM.
2. The sequential logic is used to store the current state of the FSM.
3. The output logic is used to generate output based on the current state of the FSM.
2022-09-17 171
clk
Finite state machines
• FSM is a method to model control logic.
• An FSM consists of combinational, sequential and output logic.
1. The combinational logic is used to decide the next state of the FSM.
2. The sequential logic is used to store the current state of the FSM.
3. The output logic is used to generate output based on the current state of the FSM.
2022-09-17 183
How to design an FSM
1. Specification analysis
2022-09-17 184
How to design an FSM
1. Specification analysis
2. Create state diagrams
2022-09-17 185
How to design an FSM
1. Specification analysis
2. Create state diagrams
• Choose type Moore or Mealy
2022-09-17 186
How to design an FSM
1. Specification analysis
2. Create state diagrams
• Choose type Moore or Mealy
• Minimize state diagram using Karnaugh map (out of scope here)
2022-09-17 187
How to design an FSM
1. Specification analysis
2. Create state diagrams
• Choose type Moore or Mealy
• Minimize state diagram using Karnaugh map (out of scope here)
3. Assign codes for states
2022-09-17 188
How to design an FSM
1. Specification analysis
2. Create state diagrams
• Choose type Moore or Mealy
• Minimize state diagram using Karnaugh map (out of scope here)
3. Assign codes for states
• One-hot? Binary? Gray code?
2022-09-17 189
How to design an FSM
1. Specification analysis
2. Create state diagrams
• Choose type Moore or Mealy
• Minimize state diagram using Karnaugh map (out of scope here)
3. Assign codes for states
• One-hot? Binary? Gray code?
4. Design seq-logic for storing the next state
2022-09-17 190
How to design an FSM
1. Specification analysis
2. Create state diagrams
• Choose type Moore or Mealy
• Minimize state diagram using Karnaugh map (out of scope here)
3. Assign codes for states
• One-hot? Binary? Gray code?
4. Design seq-logic for storing the next state
5. Design the output logic
2022-09-17 191
How to design an FSM
1. Specification analysis
2. Create state diagrams
• Choose type Moore or Mealy
• Minimize state diagram using Karnaugh map (out of scope here)
3. Assign codes for states
• One-hot? Binary? Gray code?
4. Design seq-logic for storing the next state
5. Design the output logic
• May use both combi- and seq-logic for output generation
2022-09-17 192
How to design an FSM
1. Specification analysis
2. Create state diagrams
• Choose type Moore or Mealy
• Minimize state diagram using Karnaugh map (out of scope here)
3. Assign codes for states
• One-hot? Binary? Gray code?
4. Design seq-logic for storing the next state
5. Design the output logic
• May use both combi- and seq-logic for output generation
6. Design combi-logic for calculating the next state
2022-09-17 193
How to design an FSM
1. Specification analysis
2. Create state diagrams
• Choose type Moore or Mealy
• Minimize state diagram using Karnaugh map (out of scope here)
3. Assign codes for states
• One-hot? Binary? Gray code?
4. Design seq-logic for storing the next state
5. Design the output logic
• May use both combi- and seq-logic for output generation
6. Design combi-logic for calculating the next state
• Core of the FSM
2022-09-17 194
FSM Mini project – Traffic light control
2022-09-17 195
Traffic
light 2 with
2022-09-17 197
Specification analysis
• First thing first, how traffic light works? 2
2022-09-17 198
Specification analysis
• First thing first, how traffic light works? 2
• Traffic lights change their colors in the same
order every time.
2022-09-17 199
Specification analysis
• First thing first, how traffic light works? 2
• Traffic lights change their colors in the same
order every time.
1. Red light on: This tells drivers to stop.
1
2022-09-17 200
Specification analysis
• First thing first, how traffic light works? 2
• Traffic lights change their colors in the same
order every time.
1. Red light on: This tells drivers to stop.
2.Green light on: This means the driver can start driving
or keep driving. 1
2022-09-17 201
Specification analysis
• First thing first, how traffic light works? 2
• Traffic lights change their colors in the same
order every time.
1. Red light on: This tells drivers to stop.
2.Green light on: This means the driver can start driving
or keep driving. 1
3. Yellow light on: This tells drivers to stop when it
is safe to, because the light is about to turn red.
2022-09-17 202
Specification analysis
• First thing first, how traffic light works? 2
• Traffic lights change their colors in the same
order every time.
1. Red light on: This tells drivers to stop.
2.Green light on: This means the driver can start driving
or keep driving. 1
3. Yellow light on: This tells drivers to stop when it
is safe to, because the light is about to turn red.
2022-09-17 203
Specification analysis
• First thing first, how traffic light works? 2
• Traffic lights change their colors in the same
order every time.
1. Red light on: This tells drivers to stop.
2.Green light on: This means the driver can start driving
or keep driving. 1
3. Yellow light on: This tells drivers to stop when it
is safe to, because the light is about to turn red.
• In our case:
2022-09-17 204
Specification analysis
• First thing first, how traffic light works? 2
• Traffic lights change their colors in the same
order every time.
1. Red light on: This tells drivers to stop.
2.Green light on: This means the driver can start driving
or keep driving. 1
3. Yellow light on: This tells drivers to stop when it
is safe to, because the light is about to turn red.
• In our case:
• Car comes
2022-09-17 205
Specification analysis
• First thing first, how traffic light works? 2
• Traffic lights change their colors in the same
order every time.
1. Red light on: This tells drivers to stop.
2.Green light on: This means the driver can start driving
or keep driving. 1
3. Yellow light on: This tells drivers to stop when it
is safe to, because the light is about to turn red.
• In our case:
• Car comes
• Light 1: Green Yellow Red
2022-09-17 206
Specification analysis
• First thing first, how traffic light works? 2
• Traffic lights change their colors in the same
order every time.
1. Red light on: This tells drivers to stop.
2.Green light on: This means the driver can start
1
driving or keep driving.
3. Yellow light on: This tells drivers to stop
when it is safe to, because the light is about
to turn red.
• In our case:
• Car comes
• Light 1: Green Yellow Red
2022-09-17
• Light 2: Red (wait) → Green 207
Specification analysis
• First thing first, how traffic light works? 2
• Traffic lights change their colors in the same
order every time.
1. Red light on: This tells drivers to stop.
2.Green light on: This means the driver can start
1
driving or keep driving.
3. Yellow light on: This tells drivers to stop
when it is safe to, because the light is about
to turn red.
• In our case:
• Car comes
• Light 1: Green Yellow Red
2022-09-17
• Light 2: Red (wait) → Green 208
Specification analysis
• First thing first, how traffic light works? 2
• Traffic lights change their colors in the same
order every time.
1. Red light on: This tells drivers to stop.
2.Green light on: This means the driver can start
1
driving or keep driving.
3. Yellow light on: This tells drivers to stop
when it is safe to, because the light is about
to turn red.
• In our case:
• Car comes T
• Light 1: Green Yellow Red
2022-09-17
• Light 2: Red (wait) → Green 209
Specification analysis
• First thing first, how traffic light works? 2
• Traffic lights change their colors in the same
order every time.
1. Red light on: This tells drivers to stop.
2.Green light on: This means the driver can start
1
driving or keep driving.
3. Yellow light on: This tells drivers to stop
when it is safe to, because the light is about
to turn red.
• In our case:
• Car leaves
• Car comes T
• Light 1: Green Yellow Red
2022-09-17
• Light 2: Red (wait) → Green 210
Specification analysis
• First thing first, how traffic light works? 2
• Traffic lights change their colors in the same
order every time.
1. Red light on: This tells drivers to stop.
2.Green light on: This means the driver can start
1
driving or keep driving.
3. Yellow light on: This tells drivers to stop
when it is safe to, because the light is about
to turn red.
• In our case:
• Car leaves
• Car comes T
• Light 1: Red (wait) → Green
• Light 1: Green Yellow Red
2022-09-17
• Light 2: Red (wait) → Green 211
Specification analysis
• First thing first, how traffic light works? 2
• Traffic lights change their colors in the same
order every time.
1. Red light on: This tells drivers to stop.
2.Green light on: This means the driver can start
1
driving or keep driving.
3. Yellow light on: This tells drivers to stop
when it is safe to, because the light is about
to turn red.
• In our case:
• Car leaves
• Car comes T
• Light 1: Red (wait) → Green
• Light 1: Green Yellow Red • Light 2: Green → Yellow → Red
2022-09-17
• Light 2: Red (wait) → Green 212
Create state diagrams
2022-09-17 213
Create state diagrams
S1
L1: Green
L2: Red
2022-09-17 214
Create state diagrams
Sensor input
car_sensor
S1
L1: Green
L2: Red
2022-09-17 215
Create state diagrams
Sensor input
car_sensor
S1
L1: Green
L2: Red
2022-09-17 216
Create state diagrams
Sensor input S2
car_sensor L1: Yellow
L2: Red
S1
L1: Green
L2: Red
2022-09-17 217
Create state diagrams
Sensor input S2 5s
car_sensor L1: Yellow
L2: Red
S1
L1: Green
L2: Red
2022-09-17 218
Create state diagrams
Sensor input S2 5s
car_sensor L1: Yellow
L2: Red
S1
L1: Green
L2: Red
2022-09-17 219
Create state diagrams
Sensor input S2 5s
car_sensor L1: Yellow
L2: Red
S1 S3
L1: Green L1: Red
L2: Red L2: Green
2022-09-17 220
Create state diagrams
Sensor input S2 5s
car_sensor L1: Yellow
L2: Red
S1 S3
L1: Green L1: Red
L2: Red L2: Green
2022-09-17 221
Create state diagrams
Sensor input S2 5s
car_sensor L1: Yellow
L2: Red
S1 S3
L1: Green L1: Red
L2: Red L2: Green
30s
2022-09-17 222
Create state diagrams
Sensor input S2 5s
car_sensor L1: Yellow
L2: Red
S1 S3
L1: Green L1: Red
L2: Red L2: Green
S4
L1: Red
L2: Yellow 30s
2022-09-17 223
Create state diagrams
Sensor input S2 5s
car_sensor L1: Yellow
L2: Red
S1 S3
L1: Green L1: Red
L2: Red L2: Green
S4
L1: Red
L2: Yellow 30s
2022-09-17 224
Create state diagrams
Sensor input S2 5s
car_sensor L1: Yellow
L2: Red
S1 S3
L1: Green L1: Red
L2: Red L2: Green
S4
L1: Red
5s L2: Yellow 30s
2022-09-17 225
Create state diagrams
Sensor input S2 5s
car_sensor L1: Yellow
L2: Red
Let’s code
S1
the FSM together inS3Vivado
L1: Green L1: Red
L2: Red L2: Green
S4
L1: Red
5s L2: Yellow 30s
2022-09-17 226
Assign codes for states
2022-09-17 227
Assign codes for states
module traffic(
input car_sensor,
input clk,
input n_rst,
output [2:0] light_1, // 3: Red, 2: Yellow, 1: Green
output [2:0] light_2 // 3: Red, 2: Yellow, 1: Green
);
2022-09-17 228
Assign codes for states
2022-09-17 229
Assign codes for states
//------------ Codes for states ------------
localparam
S_1 = 4'b0001,
S_2 = 4'b0010,
S_3 = 4'b0100,
S_4 = 4'b1000;
`timescale 1ms / 1ms
Freq for a traffic light.
reg [2:0] reg_light_1;
module traffic( This affect later how we
reg [2:0] reg_light_2;
input car_sensor, design our counter to
input clk, count time.
assign light_1 = reg_light_1;
input n_rst,
assign light_2 = reg_light_2;
output [2:0] light_1, // 3: Red, 2: Yellow, 1: Green
output [2:0] light_2 // 3: Red, 2: Yellow, 1: Green
reg [3:0] cur_state;
);
reg [3:0] next_state;
integer counter;
reg counter_enable;
2022-09-17 230
Assign codes for states
//------------ Codes for states ------------
localparam We use
S_1 = 4'b0001, one-hot code.
S_2 = 4'b0010, You can choose other
S_3 = 4'b0100, coding style, too.
S_4 = 4'b1000;
`timescale 1ms / 1ms
Freq for a traffic light.
reg [2:0] reg_light_1;
module traffic( This affect later how we
reg [2:0] reg_light_2;
input car_sensor, design our counter to
input clk, count time.
assign light_1 = reg_light_1;
input n_rst,
assign light_2 = reg_light_2;
output [2:0] light_1, // 3: Red, 2: Yellow, 1: Green
output [2:0] light_2 // 3: Red, 2: Yellow, 1: Green
reg [3:0] cur_state;
);
reg [3:0] next_state;
integer counter;
reg counter_enable;
2022-09-17 231
Assign codes for states
//------------ Codes for states ------------
localparam We use
S_1 = 4'b0001, one-hot code.
S_2 = 4'b0010, You can choose other
S_3 = 4'b0100, coding style, too.
S_4 = 4'b1000;
`timescale 1ms / 1ms Internal driving
Freq for a traffic light. signals.
reg [2:0] reg_light_1;
module traffic( This affect later how we
reg [2:0] reg_light_2;
input car_sensor, design our counter to
input clk, count time.
assign light_1 = reg_light_1;
input n_rst,
assign light_2 = reg_light_2;
output [2:0] light_1, // 3: Red, 2: Yellow, 1: Green
output [2:0] light_2 // 3: Red, 2: Yellow, 1: Green
reg [3:0] cur_state;
);
reg [3:0] next_state;
integer counter;
reg counter_enable;
2022-09-17 232
Assign codes for states
//------------ Codes for states ------------
localparam We use
S_1 = 4'b0001, one-hot code.
S_2 = 4'b0010, You can choose other
S_3 = 4'b0100, coding style, too.
S_4 = 4'b1000;
`timescale 1ms / 1ms Internal driving
Freq for a traffic light. signals.
reg [2:0] reg_light_1;
module traffic( This affect later how we
reg [2:0] reg_light_2;
input car_sensor, design our counter to
input clk, count time.
assign light_1 = reg_light_1;
input n_rst, The counter is used
assign light_2 = reg_light_2;
output [2:0] light_1, // 3: Red, 2: Yellow, 1: Green to count time between
output [2:0] light_2 // 3: Red, 2: Yellow, 1: Green light changes.
reg [3:0] cur_state;
); The counter is only
reg [3:0] next_state;
enabled when we are
integer counter; in S_2, S_3, and S_4
reg counter_enable;
2022-09-17 233
Design seq-logic for storing the next state
2022-09-17 234
Design seq-logic for storing the next state
Nothing but a
asynchronous reset
D_FF.
//------------ Seq logic -----------------------------------
always @ (posedge clk, negedge n_rst) begin
if (!n_rst) begin
cur_state <= S_1;
end else begin
cur_state <= next_state;
end
end
2022-09-17 235
Design seq-logic for storing the next state
Nothing but a
asynchronous reset
D_FF.
//------------ Seq logic -----------------------------------
always @ (posedge clk, negedge n_rst) begin
if (!n_rst) begin
cur_state <= S_1;
end else begin
cur_state <= next_state; Reset to S_1.
end
end
2022-09-17 236
Design seq-logic for storing the next state
Nothing but a
asynchronous reset
D_FF.
//------------ Seq logic -----------------------------------
always @ (posedge clk, negedge n_rst) begin
if (!n_rst) begin
cur_state <= S_1;
end else begin
cur_state <= next_state; Reset to S_1.
end
end
2022-09-17 237
Design the output logic
//------------ Output logic -------------------------------- When reset, both Light_1 and Light_2 turn all
always @ (posedge clk, negedge n_rst) begin lights on to test if they work properly.
if (!n_rst) begin
reg_light_1 <= 3'b111; Note! At this moment, the cur_state is also
reg_light_2 <= 3'b111; reset to S_1, but that can only pass to the
end else begin output logic 1 cycle later.
case(cur_state)
S_1: {reg_light_1, reg_light_2} <= {3'b001, 3'b100};
S_2: {reg_light_1, reg_light_2} <= {3'b010, 3'b100};
S_3: {reg_light_1, reg_light_2} <= {3'b100, 3'b001};
S_4: {reg_light_1, reg_light_2} <= {3'b100, 3'b010};
endcase
end
end
2022-09-17 239
Design the output logic Next state Current state Output Logic
Logic Logic
D Q D Q output
D_FF D_FF
input
clk clk
clk
//------------ Output logic -------------------------------- When reset, both Light_1 and Light_2 turn all
always @ (posedge clk, negedge n_rst) begin lights on to test if they work properly.
if (!n_rst) begin
reg_light_1 <= 3'b111; Note! At this moment, the cur_state is also
reg_light_2 <= 3'b111; reset to S_1, but that can only pass to the
end else begin output logic 1 cycle later.
case(cur_state)
S_1: {reg_light_1, reg_light_2} <= {3'b001, 3'b100};
S_2: {reg_light_1, reg_light_2} <= {3'b010, 3'b100};
S_3: {reg_light_1, reg_light_2} <= {3'b100, 3'b001};
S_4: {reg_light_1, reg_light_2} <= {3'b100, 3'b010};
endcase
end
end
2022-09-17 240
Design the output logic Next state Current state Output Logic
Logic Logic
D Q D Q output
D_FF D_FF
input
clk clk
clk
//------------ Output logic -------------------------------- When reset, both Light_1 and Light_2 turn all
always @ (posedge clk, negedge n_rst) begin lights on to test if they work properly.
if (!n_rst) begin
reg_light_1 <= 3'b111; Note! At this moment, the cur_state is also
reg_light_2 <= 3'b111; reset to S_1, but that can only pass to the
end else begin output logic 1 cycle later.
case(cur_state)
S_1: {reg_light_1, reg_light_2} <= {3'b001, 3'b100};
S_2: {reg_light_1, reg_light_2} <= {3'b010, 3'b100};
S_3: {reg_light_1, reg_light_2} <= {3'b100, 3'b001};
S_4: {reg_light_1, reg_light_2} <= {3'b100, 3'b010}; S_1: Light 1 green, Light 2 red
endcase S_2: Light 1 yellow, Light 2 red (car comes!)
end S_3: Light 1 red, Light 2 green
end S_4: Light 1 red, Light 2 yellow
2022-09-17 241
Design the output logic Next state Current state Output Logic
Logic Logic
D Q D Q output
D_FF D_FF
input
clk clk
clk
//------------ Output logic -------------------------------- When reset, both Light_1 and Light_2 turn all
always @ (posedge clk, negedge n_rst) begin lights on to test if they work properly.
if (!n_rst) begin
reg_light_1 <= 3'b111; Note! At this moment, the cur_state is also
reg_light_2 <= 3'b111; reset to S_1, but that can only pass to the
end else begin output logic 1 cycle later.
case(cur_state)
S_1: {reg_light_1, reg_light_2} <= {3'b001, 3'b100};
S_2: {reg_light_1, reg_light_2} <= {3'b010, 3'b100};
S_3: {reg_light_1, reg_light_2} <= {3'b100, 3'b001};
S_4: {reg_light_1, reg_light_2} <= {3'b100, 3'b010}; S_1: Light 1 green, Light 2 red
endcase S_2: Light 1 yellow, Light 2 red (car comes!)
end S_3: Light 1 red, Light 2 green
end S_4: Light 1 red, Light 2 yellow
2022-09-17 242
Design combi-logic for calculating the
next state
2022-09-17 243
Design combi-logic for calculating the
next state
• We need a counter to counter the
time between
2022-09-17 244
Design combi-logic for calculating the
next state
• We need a counter to counter the time
between
• S_2 and S_3
(duration of the yellow light Light 1)
Car comes at Light 2
2022-09-17 245
Design combi-logic for calculating the
next state
• We need a counter to counter the time
between
• S_2 and S_3
(duration of the yellow light Light 1)
Car comes at Light 2
• S_3 and S_4
(duration of the green light Light 2)
Car goes through
2022-09-17 246
Design combi-logic for calculating the
next state
• We need a counter to counter the time
between
• S_2 and S_3
(duration of the yellow light Light 1)
Car comes at Light 2
• S_3 and S_4
(duration of the green light Light 2)
Car goes through
• S_4 and S_1
(duration of the yellow light Light 2)
Car leaves, back to zero
2022-09-17 247
Design combi-logic for calculating the
next state
• We need a counter to counter the time
between
• S_2 and S_3
(duration of the yellow light Light 1)
Car comes at Light 2
• S_3 and S_4
(duration of the green light Light 2)
Car goes through
• S_4 and S_1
(duration of the yellow light Light 2)
Car leaves, back to zero
• The counter is turned on when we
change from S_1 to S_2.
2022-09-17 248
Design combi-logic for calculating the
next state
• We need a counter to counter the
time between
• S_2 and S_3
(duration of the yellow light Light 1)
Car comes at Light 2
• S_3 and S_4
(duration of the green light Light 2)
Car goes through
• S_4 and S_1
(duration of the yellow light Light 2)
Car leaves, back to zero
• The counter is turned on when we
change from S_1 to S_2.
• The counter is turned off and reset
to 0 when we change from S_4 to
S_1.
2022-09-17 249
Design combi-logic for calculating the
next state
• We need a counter to counter the
//------------ Counter logic -------------------------------
time between always @ (posedge clk, negedge n_rst) begin
• S_2 and S_3 if (!n_rst) begin
(duration of the yellow light Light 1)
Car comes at Light 2 counter <= 0;
• S_3 and S_4 end else begin
(duration of the green light Light 2) if (counter_enable) begin
Car goes through counter <= counter + 1;
• S_4 and S_1 end else begin
(duration of the yellow light Light 2) if (counter != 0)
Car leaves, back to zero counter <= 0;
• The counter is turned on when we end
change from S_1 to S_2. end
end
• The counter is turned off and reset
to 0 when we change from S_4 to
S_1.
2022-09-17 250
Design combi-logic for calculating the
next state
• We need a counter to counter the
//------------ Counter logic -------------------------------
time between always @ (posedge clk, negedge n_rst) begin
• S_2 and S_3 if (!n_rst) begin
(duration of the yellow light Light 1)
Car comes at Light 2 counter <= 0;
• S_3 and S_4 end else begin
(duration of the green light Light 2) if (counter_enable) begin
Car goes through counter <= counter + 1;
• S_4 and S_1 end else begin
(duration of the yellow light Light 2) if (counter != 0)
Car leaves, back to zero counter <= 0;
• The counter is turned on when we end
change from S_1 to S_2. end 1. S_2 sets counter_enable to
end true
• The counter is turned off and reset 2. counter_enable remains true
to 0 when we change from S_4 to in S_3 and S_4
S_1. 3. S_1 sets counter_enable to
2022-09-17 false 251
Design combi-logic for calculating the
next state
2022-09-17 252
Design combi-logic for calculating the
next state
//------------ Combinational function
always @ (cur_state, car_sensor, counter) begin
case( cur_state)
S_1: begin
if (car_sensor) begin
next_state = S_2;
counter_enable = 1'b1;
end else begin
next_state = cur_state;
counter_enable = 1’b0;
end
end
...
endcase
end
2022-09-17 253
Design combi-logic for calculating the
next state
//------------ Combinational function
always @ (cur_state, car_sensor, counter) begin
case( cur_state)
S_1: begin
if (car_sensor) begin
next_state = S_2;
counter_enable = 1'b1;
end else begin
next_state = cur_state;
counter_enable = 1’b0;
end
end 1. If car comes, next_state is S_2 and
... we set the counter on.
endcase 2. Other wise we hold at S_1.
end 3. The second counter_enable = 1’b0
is used to turn off the counter when
2022-09-17 we change from S_4 to S_1. 254
Design combi-logic for calculating the
next state S_2: begin
//------------ Combinational function if (counter == 5_000)
always @ (cur_state, car_sensor, counter) begin next_state = S_3;
else
case( cur_state) next_state = cur_state;
S_1: begin end
if (car_sensor) begin
next_state = S_2;
counter_enable = 1'b1;
end else begin
next_state = cur_state;
counter_enable = 1’b0;
end
end 1. If car comes, next_state is S_2 and
... we set the counter on.
endcase 2. Other wise we hold at S_1.
end 3. The second counter_enable = 1’b0
is used to turn off the counter when
2022-09-17 we change from S_4 to S_1. 255
Design combi-logic for calculating the
next state S_2: begin
//------------ Combinational function if (counter == 5_000)
always @ (cur_state, car_sensor, counter) begin next_state = S_3;
else
case( cur_state) next_state = cur_state;
S_1: begin end
if (car_sensor) begin S_3: begin
next_state = S_2; if (counter == 35_000)
counter_enable = 1'b1; next_state = S_4;
end else begin else
next_state = cur_state; next_state = cur_state;
counter_enable = 1’b0; end
end
end 1. If car comes, next_state is S_2 and
... we set the counter on.
endcase 2. Other wise we hold at S_1.
end 3. The second counter_enable = 1’b0
is used to turn off the counter when
2022-09-17 we change from S_4 to S_1. 256
Design combi-logic for calculating the
next state S_2: begin
//------------ Combinational function if (counter == 5_000)
always @ (cur_state, car_sensor, counter) begin next_state = S_3;
else
case( cur_state) next_state = cur_state;
S_1: begin end
if (car_sensor) begin S_3: begin
next_state = S_2; if (counter == 35_000)
counter_enable = 1'b1; next_state = S_4;
end else begin else
next_state = cur_state; next_state = cur_state;
counter_enable = 1’b0; end
end
end 1. If car comes, next_state is S_2 and S_4: begin
... we set the counter on. if (counter == 40_000)
endcase 2. Other wise we hold at S_1. next_state = S_1;
end 3. The second counter_enable = 1’b0 else
is used to turn off the counter when next_state = cur_state;
2022-09-17 we change from S_4 to S_1. end 257
Design combi-logic for calculating the
next state S_2: begin
1. Wait the counter to
//------------ Combinational function if (counter == 5_000)
count 5000 cycles
always @ (cur_state, car_sensor, counter) begin next_state = S_3;
(1 ms per cycle)
else
2. Then change
case( cur_state) next_state = cur_state;
next_state to S_3
S_1: begin end
if (car_sensor) begin S_3: begin
next_state = S_2; if (counter == 35_000)
counter_enable = 1'b1; next_state = S_4;
end else begin else
next_state = cur_state; next_state = cur_state;
counter_enable = 1’b0; end
end
end 1. If car comes, next_state is S_2 and S_4: begin
... we set the counter on. if (counter == 40_000)
endcase 2. Other wise we hold at S_1. next_state = S_1;
end 3. The second counter_enable = 1’b0 else
is used to turn off the counter when next_state = cur_state;
2022-09-17 we change from S_4 to S_1. end 258
Design combi-logic for calculating the
next state S_2: begin
1. Wait the counter to
//------------ Combinational function if (counter == 5_000)
count 5000 cycles
always @ (cur_state, car_sensor, counter) begin next_state = S_3;
(1 ms per cycle)
else
2. Then change
case( cur_state) next_state = cur_state;
next_state to S_3
S_1: begin end
if (car_sensor) begin S_3: begin 1. Wait the counter to
next_state = S_2; if (counter == 35_000) count 30_000
counter_enable = 1'b1; next_state = S_4; cycles more (1 ms
end else begin else per cycle)
next_state = cur_state; next_state = cur_state; 2. Then change
counter_enable = 1’b0; end next_state to S_4
end
end 1. If car comes, next_state is S_2 and S_4: begin
... we set the counter on. if (counter == 40_000)
endcase 2. Other wise we hold at S_1. next_state = S_1;
end 3. The second counter_enable = 1’b0 else
is used to turn off the counter when next_state = cur_state;
2022-09-17 we change from S_4 to S_1. end 259
Design combi-logic for calculating the
next state S_2: begin
1. Wait the counter to
//------------ Combinational function if (counter == 5_000)
count 5000 cycles
always @ (cur_state, car_sensor, counter) begin next_state = S_3;
(1 ms per cycle)
else
2. Then change
case( cur_state) next_state = cur_state;
next_state to S_3
S_1: begin end
if (car_sensor) begin S_3: begin 1. Wait the counter to
next_state = S_2; if (counter == 35_000) count 30_000
counter_enable = 1'b1; next_state = S_4; cycles more (1 ms
end else begin else per cycle)
next_state = cur_state; next_state = cur_state; 2. Then change
counter_enable = 1’b0; end next_state to S_4
end
end 1. If car comes, next_state is S_2 and S_4: begin 1. Wait the counter to
... we set the counter on. if (counter == 40_000) count 5_000 cycles
endcase 2. Other wise we hold at S_1. next_state = S_1; more (1 ms per
end 3. The second counter_enable = 1’b0 else cycle)
is used to turn off the counter when next_state = cur_state; 2. Then change
2022-09-17 we change from S_4 to S_1. end next_state
260to S_1
Testbench
initial begin
clk = 0;
n_rst = 1;
car_sensor = 0;
#1 n_rst = 0;
#1 n_rst = 1;
#100 car_sensor = 1;
#200 car_sensor = 0;
#1000000 car_sensor = 1;
#200 car_sensor = 0;
end
2022-09-17 261
Testbench
initial begin
clk = 0;
n_rst = 1;
car_sensor = 0;
#1 n_rst = 0;
#1 n_rst = 1; Drive the reset signal.
#100 car_sensor = 1;
#200 car_sensor = 0;
#1000000 car_sensor = 1;
#200 car_sensor = 0;
end
2022-09-17 262
Testbench
initial begin
clk = 0;
n_rst = 1;
car_sensor = 0;
#1 n_rst = 0;
#1 n_rst = 1; Drive the reset signal.
#100 car_sensor = 1;
#200 car_sensor = 0;
#1000000 car_sensor = 1;
#200 car_sensor = 0;
end
2022-09-17 263
Testbench
initial begin
clk = 0;
n_rst = 1;
car_sensor = 0;
#1 n_rst = 0;
#1 n_rst = 1; Drive the reset signal.
#100 car_sensor = 1;
#200 car_sensor = 0;
Simulate car arrivals.
#1000000 car_sensor = 1; Sensor signal lasts for 200 ms (0.2s).
#200 car_sensor = 0;
end
2022-09-17 264
Testbench
initial begin
clk = 0;
n_rst = 1;
car_sensor = 0;
#1 n_rst = 0;
#1 n_rst = 1; Drive the reset signal.
#100 car_sensor = 1;
#200 car_sensor = 0;
Simulate car arrivals.
#1000000 car_sensor = 1; Sensor signal lasts for 200 ms (0.2s).
#200 car_sensor = 0;
end
2022-09-17 265
Testbench
initial begin
clk = 0;
n_rst = 1;
car_sensor = 0;
#1 n_rst = 0;
#1 n_rst = 1; Drive the reset signal.
#100 car_sensor = 1;
#200 car_sensor = 0;
Simulate car arrivals.
#1000000 car_sensor = 1; Sensor signal lasts for 200 ms (0.2s).
#200 car_sensor = 0;
end
#1 n_rst = 0;
#1 n_rst = 1; Drive the reset signal.
#100 car_sensor = 1;
#200 car_sensor = 0;
Simulate car arrivals.
#1000000 car_sensor = 1; Sensor signal lasts for 200 ms (0.2s).
#200 car_sensor = 0;
end
2022-09-17 268
Waveform – FSM reset
2022-09-17 269
Waveform – FSM reset
n_rst negedge arrives
1. light_1, light_2 turn all the lights on (3’b111) to
check if they can be lit up properly.
2. cur_state is reset to S_1
• next_state is set to S_1 in the combi-logic
3. counter is reset to 0
• counter_enable is set to 0 in the combi-logic
2022-09-17 270
Waveform – FSM reset
n_rst negedge arrives
1. light_1, light_2 turn all the lights on (3’b111) to
check if they can be lit up properly.
2. cur_state is reset to S_1
• next_state is set to S_1 in the combi-logic
3. counter is reset to 0
• counter_enable is set to 0 in the combi-logic
2022-09-17 271
Waveform – FSM reset
n_rst negedge arrives
1. light_1, light_2 turn all the lights on (3’b111) to
check if they can be lit up properly.
2. cur_state is reset to S_1
• next_state is set to S_1 in the combi-logic
3. counter is reset to 0 n_rst
• counter_enable is set to 0 in the combi-logic leaves
2022-09-17 272
Waveform – FSM reset
n_rst negedge arrives
1. light_1, light_2 turn all the lights on (3’b111) to
check if they can be lit up properly.
2. cur_state is reset to S_1
• next_state is set to S_1 in the combi-logic
3. counter is reset to 0 n_rst
• counter_enable is set to 0 in the combi-logic leaves
2022-09-17 273
Waveform – FSM reset
The first posedge clk since n_rst
leaves. The FSM begins to work.
n_rst negedge arrives 1. Counter logic is disabled.
1. light_1, light_2 turn all the lights on (3’b111) to 2. cur_state, car_sensor, counter
check if they can be lit up properly. all remain silent, FSM combi-logic
2. cur_state is reset to S_1 remains silent. next_state holds.
• next_state is set to S_1 in the combi-logic 3. cur_state holds.
3. counter is reset to 0 n_rst 4. Output updated to the ones for
• counter_enable is set to 0 in the combi-logic leaves S_1. Light 1 green, light 2 red.
2022-09-17 274
Waveform – Car comes, state change S_1 to S_2
2022-09-17 275
Waveform – Car comes, state change S_1 to S_2
2022-09-17 276
Waveform – Car comes, state change S_1 to S_2
2022-09-17 277
Waveform – Car comes, state change S_1 to S_2
2022-09-17 278
Waveform – Car comes, state change S_1 to S_2
Clock posedge arrives. FSM state changes.
cur_state = next_state (S_2). Counter
starts to count.
2022-09-17 279
Waveform – Car comes, state change S_1 to S_2
Clock posedge arrives. FSM state changes.
cur_state = next_state (S_2). Counter
starts to count.
2022-09-17 280
Waveform – Car comes, state change S_1 to S_2
Clock posedge arrives. FSM state changes. Output updated 1 cycle later. Light 1 is
cur_state = next_state (S_2). Counter yellow. Light 2 is red.
starts to count. Max 2 ms lagging between car arrival event
and traffic light changes.
2022-09-17 281
Waveform – State change S_2 to S_3
2022-09-17 282
Waveform – State change S_2 to S_3
2022-09-17 283
Waveform – State change S_2 to S_3
2022-09-17 284
Waveform – State change S_2 to S_3
2022-09-17 285
Waveform – State change S_2 to S_3
FSM changes cur_state to S_3 in
the following cycle.
2022-09-17 286
Waveform – State change S_2 to S_3
FSM changes cur_state to S_3 in
the following cycle.
2022-09-17 287
Waveform – State change S_2 to S_3
FSM changes cur_state to S_3 in
the following cycle.
Counter counts 5,000 cycles (5 s). Output changes 1 cycle later. Light 1
next_state changes from S_2 to is red, Light 2 is green.
S_3.
2022-09-17 288
Waveform – State change S_3 to S_4
2022-09-17 289
Waveform – State change S_3 to S_4
2022-09-17 290
Waveform – State change S_3 to S_4
2022-09-17 291
Waveform – State change S_3 to S_4
2022-09-17 292
Waveform – State change S_3 to S_4
FSM changes cur_state to S_4 in
the following cycle.
2022-09-17 293
Waveform – State change S_3 to S_4
FSM changes cur_state to S_4 in
the following cycle.
2022-09-17 294
Waveform – State change S_3 to S_4
FSM changes cur_state to S_4 in
the following cycle.
Output changes 1 cycle later. Light 1
Counter counts 30,000 cycles more is red, Light 2 is yellow.
(30 s). next_state changes from S_3 Don’t miss it, hurry up!
to S_4.
2022-09-17 295
Waveform – State change S_4 to S_1
2022-09-17 296
Waveform – State change S_4 to S_1
2022-09-17 297
Waveform – State change S_4 to S_1
2022-09-17 298
Waveform – State change S_4 to S_1
2022-09-17 299
Waveform – State change S_4 to S_1
FSM changes cur_state to S_1 in
the following cycle.
counter_enable set to 1’b0.
2022-09-17 300
Waveform – State change S_4 to S_1
FSM changes cur_state to S_1 in
the following cycle.
counter_enable set to 1’b0.
2022-09-17 301
Waveform – State change S_4 to S_1
FSM changes cur_state to S_1 in
the following cycle.
counter_enable set to 1’b0. Output changes 1 cycle later. Light 1
is Green, Light 2 is Red.
Counter reset.
Counter counts 5,000 cycles more (5 s). Back to square one.
next_state changes from S_4 to S_1.
2022-09-17 302
Waveform – State change S_4 to S_1
FSM changes cur_state to S_1 in
the following cycle.
counter_enable set to 1’b0. Output changes 1 cycle later. Light 1
is Green, Light 2 is Red.
Counter reset.
Counter counts 5,000 cycles more (5 s). Back to square one.
next_state changes from S_4 to S_1.
2022-09-17 303
Waveform – State change S_4 to S_1
FSM changes cur_state to S_1 in
the following cycle.
counter_enable set to 1’b0. Output changes 1 cycle later. Light 1
is Green, Light 2 is Red.
Counter reset.
Rules of thumb No.9:
Counter counts 5,000 cycles more (5 s). Back to square one.
next_state changes from S_4 to S_1.
2022-09-17 304
Questions?
2022-09-17 305