ET5080E Digital Design Using Verilog HDL: Fall 21
ET5080E Digital Design Using Verilog HDL: Fall 21
2
What Have We Learned?
1) Sequential elements (flops & latches) should be inferred
using non-blocking “<=“ assignments
3
Engineers are paid to think,
Pharmacists are paid to follow rules
Counters are commonly needed blocks.
rst_n
Increment logic & mux are
combinational blocking
0 R
+1 cnt[7:0] Flop is seqential. non-blocking
comb 1
en clk
4
Pill Counter
module pill_cnt(clk,rst_n,en,cnt); Nothing wrong with this code
input clk,rst_n; Just a little verbose. Use DF?
output [7:0] cnt; module pill_cnt(clk,rst_n,en,cnt);
reg [7:0] nxt_cnt,cnt; input clk,rst_n;
output [7:0] cnt;
always @(posedge clk, negedge rst_n)
if (!rst_n) reg [7:0] cnt;
cnt <= 8’h00; wire [7:0] nxt_cnt;
else
cnt <= nxt_cnt; always @(posedge clk, negedge rst_n)
if (!rst_n)
always @(en or cnt) cnt <= 8’h00;
if (en) else
nxt_cnt = cnt + 1; // combinational cnt <= nxt_cnt;
else
nxt_cnt = cnt; // so use blocking assign nxt_cnt (en) ? cnt+1 : cnt;
endmodule endmodule
5
I.Q. Counter (the rebel engineer)
module iq_cnt(clk,rst_n,en,cnt); What 2 rules are broken
here?
input clk,rst_n;
output [7:0] cnt; 1) Code infers
reg [7:0] cnt; combinational using a
non-blocking
always @(posedge clk or negedge rst_n) assignment
if (!rst_n)
cnt <= 8’h00; 2) We are using an if
else if (en) statement without a
cnt <= cnt + 1; // combinational pure else clause
endmodule
Is this OK?
6
Ring Counter
module ring_counter (count, enable, clock, reset);
output reg [7: 0] count;
input enable, reset, clock;
7
Ring Counter (a better way)
module ring_counter (count, enable, clock, reset_n);
output reg [7: 0] count;
input enable, reset, clock;
9
Shifter
always @ (posedge clk) begin
if (rst) Data_Out <= 0;
else case (select[1:0])
2’b00: Data_Out <= Data_Out; // Hold
2’b01: Data_Out <= {Data_Out[3], Data_Out[3:1]}; // ‚ by 2
2’b10: Data_Out <= {Data_Out[2:0], 1’b0}; // X by 2
2’b11: Data_Out <= Data_In; // Parallel Load
endcase
end
endmodule
12
Aside (a quick intro to parameters)
Examples:
parameter Clk2q = 1.5, parameter IDLE = 2’b00;
Tsu = 1, parameter CONV = 2’b01;
Thd = 0; parameter ACCM = 2’b10;
rst = 1 b = 1/ Z = 1, Y=1
15
always @ (state,a,b)
SM Coding case (state)
S0 : if (a) begin
module fsm(clk,rst,a,b,Y,Z); nxt_state = S1;
Z = 1; end
input clk,rst,a,b;
else
output Y,Z;
What problems do nxt_state = S0;
parameter S0 = 2’b00, we have here? S1 : begin
S1 = 2’b01, Y=1;
S2 = 2’b10; if (b) begin
nxt_state = S2;
reg [1:0] state,nxt_state;
Z=1; end
always @(posedge clk, posedge rst) else
if (rst) nxt_state = S1;
state <= S0; end
else S2 : nxt_state = S0;
state <= nxt_state; endcase
endmodule
16
SM Coding (2nd try of combinational)
always @ (state,a,b) S1 : begin
nxt_state = S0; // default to reset Y=1;
Z = 0; // default outputs if (b) begin
Y = 0; // to avoid latches nxt_state = S2;
Z=1; end
case (state) else nxt_state = S1;
S0 : if (a) begin end
nxt_state = S1; default : nxt_state = S0;
Z = 1; endcase
end endmodule
17
SM Coding Guidlines
1) Keep state assignment in separate always block using non-
blocking “<=“ assignment
2) Code state transition logic and output logic together in a
always block using blocking assignments
3) Assign default values to all outputs, and the nxt_state
registers. This helps avoid unintended latches
4) Remember to have a default to the case statement.
• Default should be (if possible) a state that transitions to the same state
as reset would take the SM to.
• Avoids latches
• Makes design more robust to spurious electrical/cosmic events.
18
SM Interacting with SM
• A very common case is a state that needs to be held for a certain time.
The state machine in this case may interact with a timer (counter).
BUS
Cycle
tm_eq_3ms/
wrt_done R tm_eq_3ms
+1 0
tm_eq_3ms/inc_tm comb 1
20
EEPROM Write SM Example [2]
default : begin // is CHRG
//// state transition logic & ////
inc_tm = 1;
//// output logic ////
chrg_pmp_en=1;
always @(state,wrt_eep,tm_eq_3ms)
if (tm_eq_3ms)
begin
begin
nxtState = IDLE; // default all
wrt_done = 1;
bus_wrt = 0; // to avoid
nxtState = IDLE;
clr_tm = 0; // unintended
end
inc_tm = 0; // latches
else nxtState = CHRG;
chrg_pmp_en = 0;
end
case (state) endcase
IDLE : if (wrt_eep) end
nxtState = BUS;
BUS : begin assign eep_r_w_n = ~bus_wrt;
clr_tm = 1; assign eep_cs_n = ~bus_wrt;
bus_wrt = 1; assign eep_bus = (bus_wrt) ?
nxtState = CHRG; wrt_data : 12’bzzz;
end endmodule
21
USART (RS232) Example
LSB MSB
Start
Bit
(fixed period low) Payload goes out must have at least 1
(like 57600 baud) little endian period of high at end
(stop bit)
Assume we have a 4MHz clock running our digital system
4MHz
Cycles 69 69 = 7’b1000101
57600 baud
22
USART Example
module usart_tx(clk,rst_n,strt_tx,tx_data,tx_done,TX);
23
Random Misc Topics
24
Mux With case
module Mux_4_32_(output [31:0] mux_out, input [31:0] data_3,
data_2, data_1, data_0, input [1:0] select, input enable);
reg [31: 0] mux_int;
// choose between the four inputs
always @ ( data_3 or data_2 or data_1 or data_0 or select)
case (select) (* synthesis parallel_case *)
2’b00: mux_int = data_0; Synthesis directive:
2’b01: mux_int = data_1; Lets the synthesis tool know
2’b10: mux_int = data_2; to use parallel (mux) scheme
2’b11: mux_int = data_3; when synthesizing instead of
endcase priority encoding. Called an
// add the enable functionality attribute in the IEEE spec
assign mux_out = enable ? mux_int : 32'bz;
endmodule
26
Priority Encoder With casex
module priority_encoder (output reg [2:0] Code, output valid_data,
input [7:0] Data);
27
Seven Segment Display
module Seven_Seg_Display (Display, BCD, Blanking);
output reg [6: 0] Display; // abc_defg
input [3: 0] BCD;
input Blanking;
parameter BLANK = 7'b111_1111; // active low
parameter ZERO = 7'b000_0001; // h01
parameter ONE = 7'b100_1111; // h4f
parameter TWO = 7'b001_0010; // h12
parameter THREE = 7'b000_0110; // h06
parameter FOUR = 7'b100_1100; // h4c a
parameter FIVE = 7'b010_0100; // h24 f b
parameter SIX = 7'b010_0000; // h20 g
parameter SEVEN = 7'b000_1111; // h0f c
e
parameter EIGHT = 7'b000_0000; // h00
parameter NINE = 7'b000_0100; // h04 d
29
Inter vs Intra Statement Delays
Inter-assignment delays block both evaluation and assignment
• #4 c = d;
• #8 e = f;
10 a=9 10 a=9
31
Intra Statement Delays (Blocking
Statements)
module inter2(); module inter2();
integer a,b; integer a,b;
initial begin Time Event initial begin Time Eval Assign
a=3; a=3; Event Event
#6 b = a + a; 0 a=3 b = #6 a + a; 0 nxt_b= 6 a=3
#4 a = b + a; a = #4 b + a;
end 3 a=1 end 3 -- a=1
32
Non-Blocking: Inter-Assignment Delay
Delays both the evaluation and the update
5 c=9
initial begin
a = 3; b = 2; c = 1; 7 c=12
end
33
Non-Blocking: Inter-Assignment Delay
Delays both the evaluation and the update
2 d=4
initial begin
a = 3; b = 2; c = 1; 5 c=5
end
34
Intra-Assignment Review
module bnb;
reg a, b, c, d, e, f;
Note: In testbenches I
initial begin // blocking assignments
mainly find blocking inter-
a = #10 1; // a will be assigned 1 at time 10 assignment delays to be the
b = #2 0; // b will be assigned 0 at time 12 most useful. Delays really
c = #4 1; // c will be assigned 1 at time 16 not used outside of
end testbenches that much
during the design process.
initial begin // non-blocking assignments
d <= #10 1; // d will be assigned 1 at time 10
e <= #2 0; // e will be assigned 0 at time 2
f <= #4 1; // f will be assigned 1 at time 4
end
endmodule
35