Verilog
Verilog
Verilog
Disclaimer: This course was prepared, in its entirety, by Adam Teman. Many materials were copied from sources freely available on the internet. When possible, these sources have been cited;
however, some references may have been cited incorrectly or overlooked. If you feel that a picture, graph, or code example has been copied from you and either needs to be cited or removed,
please feel free to email adam.teman@biu.ac.il and I will address this as soon as possible.
What is a hardware description language?
• HDL is NOT another programming language:
• Textual representation of Hardware constructs
• All statements are executed in parallel
• Code ordering is flexible.
Example:
• a=1;b=2;c=a+b → c==3
• c=a+b;a=1;b=2 → c==3
• Execution of code is triggered by Events
• Sensitivity lists are used to define when a code section is executed
• Different simulators may yield different results
• => Coding style is required
Verilog Syntax
Basic Constructs
• Primitives:
• not, and, or, etc.
or(out, in1, in2);
• Signals:
• 4 states: 0,1,X,Z
• Wires: do not keep states
• Registers: keep states (i.e., outputs)
• Can represent buses or group of signals
wire in1,in2;
reg out;
wire [7:0] data;
reg [31:0] mem [0:7]; //width (bits)=32, depth (words)=8
6 © Adam Teman, 2018
Basic Constructs
• Operators:
• Similar to primitives
• &, |, ~, &&, ||, etc. out = in1 | in2;
• Constants:
• The format is: W’Bval
• Examples:
• 1’b0 – single bit binary 0 (or decimal 0)
• 4’b0011 - 4 bit binary 0011 (or decimal 3)
• 8’hff = 8 bit hexadecimal ff (or decimal 255)
• 8’d255= 8 bit decimal 255
always @*
case (sel)
2’b00: out = in[0];
2’b01: out = in[1]; Module
2’b10: out = in[2]; Body
2’b11: out = i1[3];
default: out = 1’bx;
endcase
endmodule
11 © Adam Teman, 2018
System Tasks
• System tasks are used to provide interface to simulation data
• Identified by a $name syntax
• Printing tasks:
• $display, $strobe: Print once the statement is executed
• $monitor: Print every time there is a change in one of the parameters
• All take the “c” style printf format
Simple Examples
Hello World
• Your first Verilog module:
module main;
initial
begin
$display(“Hello world!”);
$finish;
end
endmodule
1. Inside always blocks (both sequential and combinational) only reg can be used as LHS.
2. For an assign statement, only wire can be used as LHS.
3. Inside an initial block (Testbench) only reg can be used on the LHS.
4. The output of an instantiated module can only connect to a wire.
5. Inputs of a module cannot be a reg.
initial
begin //begins executing at time 0
clk = 0;
end
20
FSM Example
• A 4-bit counter module sm
#(parameter COUNTER_WIDTH = 4)
• Receives 4 inputs: (clk,rst_n,act,up_dwn_n,count,ovflw);
• clk – the system clock input clk;
• rst_n – an active low reset input rst_n;
• act – the activate signal input act;
• up_dwn_n – count up (positive) input up_dwn_n;
or count down (negative) output [COUNTER_WIDTH-1:0] count;
reg [COUNTER_WIDTH-1:0] count;
• Outputs 2 signals: output ovflw;
• count: the current counted value reg ovflw;
• ovflw: an overflow signal reg [3:0] state, next_state;
wikipedia
The IBM PC
always@*
case (state)
• And do not put logic on your reset signal! 1’b1011: if (b || reset )
next_state =© idle;
35 Adam Teman, 2018
Parameterize your design
• “Pretty code” is code that is completely parametrized
• Two approaches to parameterization:
• Compiler directives: `define, `include, and `ifdef
• put all `define statements in external define files.
• Parameters or localparam
• parameters can be overridden through instantiation
• localparam is better for constants, such as FSM encoding