Verilog Tutorial (Part 2) : Announcements
Verilog Tutorial (Part 2) : Announcements
Announcements
fetch unit for your processor Look over description before next lecture! up for mailing lists so you get posts automatically
2
FA
s
module FA( input a, b, cin, output s, cout); assign s = a ^ b ^ c; assign cout = (a & b) | (a & cin) | (b & cin); endmodule
For sequential circuits, use always blocks Always blocks (and assign) are executed in parallel!
module DFF( input clk, d, output q, q_bar); reg q, q_bar; always @ (posedge clk) // triggered on the rising edge of the clock begin q <= d; // non-blocking assignment (LHS not updated until later) q_bar <= ~d; /* q_bar <= ~q will not function correctly! Why not? */ end endmodule
Adapted from Arvind & Asanovics MIT 6.375 lecture
4
Can use continual assignment OR always blocks for combinational circuits Our 1-bit adder using always block
module FA( input a, b, cin, output s, cout); reg s, cout; // when using always block, LHS must be reg type always @ ( * ) // for comb circuits, use * so avoid errors begin s = a ^ b ^ cin; // use blocking assignment here (LHS immediately) cout = (a & b) | (a & cin) | (b & cin); end endmodule
5
d = c + e; c = a + b;
Ex1: c = 3 , d = 8 Ex2: c = 3 , d = 9
Ex1: c = 3 , d = 9 Ex2: c = 3 , d = 9
1: Evaluate RHS (beginning of time step) Step 2: Update LHS (end of time step)
All other modules are strictly structural, i.e., they only wire together sub-modules
Use only positive-edge triggered flip-flops for state Do not assign to the same variable from more than one always block Separate combinational logic from sequential logic Avoid loops like the plague
4'b11_10 ( _ is just for readability) hex: 16'h034f decimal: 32'd270 other formats but these are the most useful
10
wire: of wires
Array
reg
Avoid using %, **, and / because you'll run into problems when trying to synthesis
12
module mystery( input a, b, c, d, input [1:0] sel, output out ); reg out; always @( * ) begin case ( sel ) 2d0 : out = a; 2d1 : out = b; 2d2 : out = c; 2d3 : out = d; default : out = 1bx; endcase end endmodule
Adapted from Arvind & Asanovics MIT 6.375 lecture
13
(min FFs), Gray, One hot (good for FPGA), One cold, etc
14
15
Tips on FSMs
Dont forget to handle the default case Use two different always blocks for next state and state assignment
Can
Machine: Output only depends on state Mealy Machine: Output depends on state and inputs
16
Beware
17