Veri Log Tutorial
Veri Log Tutorial
Sat Garcia
[email protected] 2nd
What is Verilog?
Verilog is:
A
hardware design language (HDL) Tool for specifying hardware circuits Syntactically, a lot like C or Java An alternative to VHDL (and more widely used) What you'll be using in 141L HELLA COOL!*
3 * If you are totally into hardware design languages
Structural Level
Lower
level
Is
Functional Level
Higher
Easier to write
4'b11_10 ( _ is just for readability) hex: 16'h034f decimal: 32'd270 other formats but these are the most useful
6
wire: of wires
wire my_wire;
Array
reg
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 *** Note: red means new concept, blue and green are just pretty colors :-p
FA
FA
FA
FA
module 4bitFA( input [3:0] A, B, input cin, output [3:0] S, output cout); wire c0, c1, c2; FA fa0(A[0],B[0],cin,S[0],c0); // implicit binding FA fa1(.a(A[1]), .b(B[1]), .cin(c0), .s(S[1]), .cout(c1)); // explicit binding FA fa2(A[2],B[2],c1,S[2],c2); FA fa3(A[3],B[3],c2,S[3],cout); endmodule
Avoid using %, **, and / because you'll run into problems when trying to synthesis
11
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! */ end endmodule
Adapted from Arvind & Asanovics MIT 6.375 lecture
12
Can use continual assignment AND 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 @ ( a or b or cin ) // for comb circuits, sensitive to ALL inputs begin s = a ^ b ^ cin; // use blocking assignment here (LHS immediately) cout = (a & b) | (a & cin) | (b & cin); end endmodule
13
d = c + e; c = a + b;
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
15
module mux4( 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
16
(min FFs), Gray, One hot (good for FPGA), One cold, etc
17
18
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
19
Thanks to Asanovic & Arvind for slides Lectures 2 and 3 on Verilog http://csg.csail.mit.edu/6.375/handouts.html
Try
making some simple circuits Beware when Googling for verilog tutorial
A lot of code out there isnt synthesizable
20
10