Verilog Tutorial
Verilog Tutorial
Introduction to Verilog
Table of Contents
1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2. Lexical Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
White Space, Comments, Numbers, Identifiers, Operators, Verilog Keywords
3. Gate-Level Modelling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Basic Gates, buf, not Gates, Three-State Gates; bufif1, bufif0, notif1, notif0
4. Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Value Set, Wire, Reg, Input, Output, Inout Integer, Supply0, Supply1 Time, Parameter
5. Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Arithmetic Operators, Relational Operators, Bit-wise Operators, Logical Operators Reduction Operators, Shift Operators, Concatenation Operator, Conditional Operator: ? Operator Precedence
6. Operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Literals, Wires, Regs, and Parameters, Bit-Selects x[3] and Part-Selects x[5:3] Function Calls
7. Modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Module Declaration, Continuous Assignment, Module Instantiations, Parameterized Modules
8. Behavioral Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Procedural Assignments, Delay in Assignment, Blocking and Nonblocking Assignments begin ... end, for Loops, while Loops, forever Loops, repeat, disable, if ... else if ... else case, casex, casez
9. Timing Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Delay Control, Event Control, @, Wait Statement, Intra-Assignment Delay
11. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Function Declaration, Function Return Value, Function Call, Function Rules, Example
Introduction to Verilog
Peter M. Nyasulu
Introduction to Verilog
1. Introduction
Verilog HDL is one of the two most common Hardware Description Languages (HDL) used by integrated circuit (IC) designers. The other one is VHDL. HDLs allows the design to be simulated earlier in the design cycle in order to correct errors or experiment with different architectures. Designs described in HDL are technology-independent, easy to design and debug, and are usually more readable than schematics, particularly for large circuits. Verilog can be used to describe designs at four levels of abstraction: (i) Algorithmic level (much like c code with if, case and loop statements). (ii) Register transfer level (RTL uses registers connected by Boolean equations). (iii) Gate level (interconnected AND, NOR etc.). (iv) Switch level (the switches are MOS transistors inside gates). The language also defines constructs that can be used to control the input and output of simulation. More recently Verilog is used as an input for synthesis programs which will generate a gate-level description (a netlist) for the circuit. Some Verilog constructs are not synthesizable. Also the way the code is written will greatly effect the size and speed of the synthesized circuit. Most readers will want to synthesize their circuits, so nonsynthesizable constructs should be used only for test benches. These are program modules used to generate I/O needed to simulate the rest of the design. The words not synthesizable will be used for examples and constructs as needed that do not synthesize. There are two types of code in most HDLs: Structural, which is a verbal wiring diagram without storage. assign a=b & c | d; /* | is a OR */ assign d = e & (~c); Here the order of the statements does not matter. Changing e will change a. Procedural which is used for circuits with storage, or as a convenient way to write conditional logic. always @(posedge clk) // Execute the next statement on every rising clock edge. count <= count+1; Procedural code is written like c code and assumes every assignment is stored in memory until over written. For synthesis, with flip-flop storage, this type of thinking generates too much storage. However people prefer procedural code because it is usually much easier to write, for example, if and case statements are only allowed in procedural code. As a result, the synthesizers have been constructed which can recognize certain styles of procedural code as actually combinational. They generate a flip-flop only for left-hand variables which truly need to be stored. However if you stray from this style, beware. Your synthesis will start to fill with superfluous latches. This manual introduces the basic and most common Verilog behavioral and gate-level modelling constructs, as well as Verilog compiler directives and system functions. Full description of the language can be found in Cadence Verilog-XL Reference Manual and Synopsys HDL Compiler for Verilog Reference Manual . The latter emphasizes only those Verilog constructs that are supported for synthesis by the Synopsys Design Compiler synthesis tool. In all examples, Verilog keyword are shown in boldface . Comments are shown in italics.
Peter M. Nyasulu
Introduction to Verilog
2. Lexical Tokens
Verilog source text files consists of the following lexical tokens:
2.2. Comments
Comments can be specified in two ways (exactly the same way as in C/C++): Begin the comment with double slashes ( //). All text between these characters and the end of the line will be ignored by the Verilog compiler. Enclose comments between the characters /* and */. Using this method allows you to continue comments on more than one line. This is good for commenting out many lines code, or for very brief in-line comments. Example 2 .1 a = c + d; // this is a simple comment /* however, this comment continues on more than one line */ assign y = temp_reg; assign x=ABC /* plus its compliment*/ + ABC_
2.3. Numbers
Number storage is defined as a number of bits, but values can be specified in binary, octal, decimal or hexadecimal (See Sect. 6.1. for details on number notation). Examples are 3b001, a 3-bit number, 5d30, (=5b11110), and 16h5ED4, ( =16d24276)
2.4. Identifiers
Identifiers are user-defined words for variables, function names, module names, block names and instance names. Identifiers begin with a letter or underscore (Not with a number or $) and can include any number of letters, digits and underscores. Identifiers in Verilog are case-sensitive. Syntax allowed symbols ABCDE . . . abcdef. . . 1234567890 _$ not allowed: anything else especially - &#@ Example 2 .2 adder // use underscores to make your by_8_shifter // identifiers more meaningful _ABC_ /* is not the same as */ _abc_ Read_ // is often used for NOT Read
2.5. Operators
Operators are one, two and sometimes three characters used to perform operations on variables. Examples include >, +, ~, &, !=. Operators are described in detail in Operators on p. 6.
Peter M. Nyasulu
Introduction to Verilog
3. Gate-Level Modelling
Primitive logic gates are part of the Verilog language. Two properties can be specified, drive_strength and delay. Drive_strength specifies the strength at the gate outputs. The strongest output is a direct connection to a source, next comes a connection through a conducting transistor, then a resistive pull-up/down. The drive strength is usually not specified, in which case the strengths defaults to strong1 and strong0. Refer to Cadence Verilog-XL Reference Manual for more details on strengths. Delays: If no delay is specified, then the gate has no propagation delay; if two delays are specified, the first represent the rise delay, the second the fall delay; if only one delay is specified, then rise and fall are equal. Delays are ignored in synthesis. This method of specifying delay is a special case of Parameterized Modules on page 11. The parameters for the primitive gates have been predefined as delays.
notif0
En
Example 3 .3 bufif0 #(5) not_1 (BUS, A, CTRL); /* BUS = A 5 time units after CTRL goes low. */ notif1 #(3,4,6) c1 (bus, a, b, cntr); /* bus goes tri-state 6 time units after ctrl goes low. */
bufif1
En
notif1
En
Peter M. Nyasulu
Introduction to Verilog
4. Data Types
4.1. Value Set
Verilog consists of only four basic values. Almost all Verilog data types store all these values: 0 (logic zero, or false condition) 1 (logic one, or true condition) x (unknown logic value) x and z have limited use for synthesis. z (high impedance state)
4.2. Wire
A wire represents a physical wire in a circuit and is used to connect gates or modules. The value of a wire can be read, but not assigned to, in a function or block. See Functions on p. 19, and Procedures: Always and Initial Blocks on p. 18. A wire does not store its value but must be driven by a continuous assignment statement or by connecting it to the output of a gate or module. Other specific types of wires include: wand (wired-AND);:the value of a wand depend on logical AND of all the drivers connected to it. wor (wired-OR);: the value of a wor depend on logical OR of all the drivers connected to it. tri (three-state;): all drivers connected to a tri must be z, except one (which determines the value of the tri). Syntax wire [msb:lsb] wire_variable_list; wand [msb:lsb] wand_variable_list; wor [msb:lsb] wor_variable_list; tri [msb:lsb] tri_variable_list; Example 4 .1 wire c wand d; assign d = a; assign d = b; wire [9:0] A; // simple wire // value of d is the logical AND of // a and b // a cable (vector) of 10 wires.
4.3. Reg
A reg (register) is a data object that holds its value from one procedural assignment to the next. They are used only in functions and procedural blocks. See Wire on p. 4 above. A reg is a Verilog variable type and does not necessarily imply a physical register. In multi-bit registers, data is stored as unsigned numbers and no sign extension is done for what the user might have thought were twos complement numbers. Syntax reg [msb:lsb] reg_variable_list; Example 4 .2 reg a; reg [7:0] tom; reg [5:0] b, c; // single 1-bit register variable // an 8-bit vector; a bank of 8 registers. // two 6-bit variables
Peter M. Nyasulu
Introduction to Verilog
4.5. Integer
Integers are general-purpose variables. For synthesois they are used mainly loops-indicies, parameters, and constants. SeeParameter on p. 5. They are of implicitly of type reg. However they store data as signed numbers whereas explicitly declared reg types store them as unsigned. If they hold numbers which are not defined at compile time, their size will default to 32-bits. If they hold constants, the synthesizer adjusts them to the minimum width needed at compilation. Syntax integer integer_variable_list; ... integer_constant ... ; Example 4 .4 integer a; assign b=63; // single 32-bit integer // 63 defaults to a 7-bit variable.
4.7. Time
Time is a 64-bit quantity that can be used in conjunction with the $time system task to hold simulation time. Time is not supported for synthesis and hence is used only for simulation purposes. Syntax time time_variable_list; Example 4 .6 time c; c = $time;
4.8. Parameter
A parameter defines a constant that can be set when you instantiate a module. This allows customization of a module during instantiation. See also Parameterized Modules on page 11. Syntax parameter par_1 = value, par_2 = value, .....; parameter [range] parm_3 = value Example 4 .7 parameter add = 2b00, sub = 3b111; parameter n = 4; parameter n = 4; parameter [3:0] param2 = 4b1010; ... reg [n-1:0] harry; /* A 4-bit register whose length is set by parameter n above. */ always @(x) y = {{(add - sub){x}}; // The replication operator Sect. 5.8. if (x) begin state = param2[1]; else state = param2[2]; end
Peter M. Nyasulu
Introduction to Verilog
5. Operators
5.1. Arithmetic Operators
These perform arithmetic operations. The + and - can be used as either unary (-z) or binary (x-y) operators. Operators + * / % (addition) (subtraction) (multiplication) (division) (modulus) Example 5 .1 parameter n = 4; reg[3:0] a, c, f, g, count; f = a + c; g = c - n; count = (count +1)%16;
c(0
b 2
a(1) b(1)
c(1)
Peter M. Nyasulu
Introduction to Verilog
{ } (concatenation)
For synthesis, Synopsis did not like a zero replication. For example:parameter n=5, m=5; assign x= {(n-m){a}}
Peter M. Nyasulu
Introduction to Verilog
Peter M. Nyasulu
Introduction to Verilog
6. Operands
6.1. Literals
Literals are constant-valued operands that can be used in Verilog expressions. The two common Verilog literals are: (a) String: A string literal is a one-dimensional array of characters enclosed in double quotes ( ). (b) Numeric: constant numbers specified in binary, octal, decimal or hexadecimal. Number Syntax nFddd..., where n - integer representing number of bits F - one of four possible base formats: b (binary), o (octal), d (decimal), h (hexadecimal). Default is d. dddd - legal digits for the base format Example 6 .1 time is// string literal 267 // 32-bit decimal number 2b01 // 2-bit binary 20hB36F// 20-bit hexadecimal number o62 // 32-bit octal number
Peter M. Nyasulu
Introduction to Verilog
7. Modules
7.1. Module Declaration
A module is the principal design entity in Verilog. The first line of a module declaration specifies the name and port list (arguments). The next few lines specifies the i/o type ( input, output or inout, see Sect. 4.4. ) and width of each port. The default port width is 1 bit. Then the port variables must be declared wire, wand,. . ., reg (See Sect. 4. ). The default is wire. Typically inputs are wire since their data is latched outside the module. Outputs are type reg if their signals were stored inside an always or initial block (See Sect. 10. ). Syntax module module_name (port_list); input [msb:lsb] input_port_list; output [msb:lsb] output_port_list; inout [msb:lsb] inout_port_list; ... statements ... endmodule Example 7 .1 module add_sub(add, in1, in2, oot); input add; // defaults to wire input [7:0] in1, in2; wire in1, in2; output [7:0] oot; reg oot; ... statements ... endmodule
add in1 8 add_sub in2 8 8
oot
10
Peter M. Nyasulu
Introduction to Verilog
Syntax for Instantiation Example 7 .3 // MODULE INSTANTIATIONS module_name wire [3:0] in1, in2; instance_name_1 (port_connection_list), // MODULE DEFINITION wire [3:0] o1, o2; instance_name_2 (port_connection_list), /* C1 is an instance of module and4 ...... module and4(a, b, c); C1 ports referenced by position */ instance_name_n (port_connection_list); input [3:0] a, b; and4 C1 (in1, in2, o1); output [3:0] c; /* C2 is another instance of and4. assign c = a & b; C2 ports are referenced to the endmodule declaration by name. */ and4 C2 ( .c(o2), .a(in1), .b(in2));
Modules may not be instantiated inside procedural blocks. See Procedures: Always and Initial Blocks on page 18.
Synthesis does not support the defparam keyword which is an alternate way of changing parameters.
11
Peter M. Nyasulu
Introduction to Verilog
8. Behavioral Modeling
Verilog has four levels of modelling: 1) The switch level which includes MOS transistors modelled as switches. This is not discussed here. 2) The gate level. See Gate-Level Modelling on p. 3 3) The Data-Flow level. See Example 7 .4 on page 11 4) The Behavioral or procedural level described below. Verilog procedural statements are used to model a design at a higher level of abstraction than the other levels. They provide powerful ways of doing complex designs. However small changes n coding methods can cause large changes in the hardware generated. Procedural statements can only be used in procedures. Verilog procedures are described later in Procedures: Always and Initial Blocks on page 18,Functions on page 19, and Tasks Not Synthesizable on page 21.
1D C1
1D C1
1D C1
1D z C1
12
Peter M. Nyasulu
Introduction to Verilog
For synthesis One must not mix <= or = in the same procedure. <= best mimics what physical flip-flops do; use it for always @ (posedge clk ..) type procedures. = best corresponds to what c/c++ code would do; use it for combinational procedures. :
Syntax Non-Blocking variable <= expression; variable <= # t expression; #t variable <= expression; Example 0 .1. For simulation initial begin #3 b <= a; /* grab a at t=0 Deliver b at t=3. #6 x <= b + c; // grab b+c at t=0, wait and assign x at t=6. x is unaffected by bs change. */ Example 0 .2. For synthesis always @( posedge clk) begin Z<=Y; Y<=X; // shift register y<=x; z <=y; //also a shift register. X
1D C1 1D C1
1D C1 1D C1
Example 8 .3. Use <= to transform a variable into itself. reg G[7:0]; always @( posedge clk) G <= { G[6:0], G[7]}; // End around rotate 8-bit register.
The following example shows interactions between blocking and non-blocking for simulation. Do not mix the two types in one procedure for synthesis. Syntax Non-Blocking variable <= expression; variable <= # t expression; ?#t variable <=expression; Blocking variable = expression; variable = #t expression; #t variable = expression; Example 8 .4 for simulation only initial begin a=1; b=2; c=3; x=4; #5 a = b + c; // wait for 5 units, then grab b,c and execute a=2+3. d = a; // Time continues from last line, d=5 = b+c at t=5. x <= #6 b + c;// grab b+c now at t=5, dont stop, make x=5 at t=11. b <= #2 a; /* grab a at t=5 (end of last blocking statement). Deliver b=5 at t=7. previous x is unaffected by b change. */ y <= #1 b + c;// grab b+c at t=5, dont stop, make x=5 at t=6. #3 z = b + c; // grab b+c at t=8 (#5+#3), make z=5 at t=8. w <= x // make w=4 at t=8. Starting at last blocking assignm.
13
Peter M. Nyasulu
Introduction to Verilog
Syntax begin : block_name reg [msb:lsb] reg_variable_list; integer [msb:lsb] integer_list; parameter [msb:lsb] parameter_list; ... statements ... end
Example 8 .5 function trivial_one; // The block name is trivial_one. input a; begin : adder_blk; // block named adder, with integer i; // local integer i ... statements ... end
14
Peter M. Nyasulu
Introduction to Verilog
Example 8 .9 repeat (2) begin // after 50, a = 00, #50 a = 2b00; // after 100, a = 01, #50 a = 2b01; // after 150, a = 00, end// after 200, a = 01
8.10. disable
Execution of a disable statement terminates a block and passes control to the next statement after the block. It is like the C break statement except it can terminate any loop, not just the one in which it appears. Disable statements can only be used with named blocks. Syntax Example 8 .10 disable block_name; begin: accumulate forever begin @(posedge clk); a = a + 1; if (a == 2b0111) disable accumulate; end end
15
Peter M. Nyasulu
Introduction to Verilog
8.12. case
The case statement allows a multipath branch based on comparing the expression with a list of case choices. Statements in the default block executes when none of the case choice comparisons are true (similar to the else block in the if ... else if ... else). If no comparisons , including delault, are true, synthesizers will generate unwanted latches. Good practice says to make a habit of puting in a default whether you need it or not. If the defaults are dont cares, define them as x and the logic minimizer will treat them as dont cares. Case choices may be a simple constant or expression, or a comma-separated list of same. Syntax case (expression) case_choice1: begin ... statements ... end case_choice2: begin ... statements ... end ... more case choices blocks ... default: begin ... statements ... end endcase Example 0 .1 case (alu_ctr) 2b00: aluout = a + b; 2b01: aluout = a - b; 2b10: aluout = a & b; default: aluout = 1bx; // Treated as dont cares for endcase // minimum logic generation. Example 0 .2 case (x, y, z) 2b00: aluout = a + b; // case if x or y or z is 2b00. 2b01: aluout = a - b; 2b10: aluout = a & b; default: aluout = a | b; endcase
8.13. casex
In casex(a) the case choices constant a may contain z, x or ? which are used as dont cares for comparison. With case the corresponding simulation variable would have to match a tri-state, unknown, or either signal. In short, case uses x to compare with an unknown signal. Casex uses x as a dont care which can be used to minimize logic. Syntax same as for case statement (Section 8.10) Example 8 .12 casex (a) 2b1x: msb = 1; // msb = 1 if a = 10 or a = 11 // If this were case(a) then only a=1x would match. default: msb = 0; endcase
8.14. casez
Casez is the same as casex except only ? and z (not x) are used in the case choice constants as dont cares. Casez is favored over casex since in simulation, an inadvertent x signal, will not be matched by a 0 or 1 in the case choice. Syntax same as for case statement (Section 8.10) Example 8 .13 casez (d) 3b1??: b = 2b11; // b = 11 if d = 100 or greater 3b01?: b = 2b10; // b = 10 if d = 010 or 011 default: b = 2b00; endcase
16
Peter M. Nyasulu
Introduction to Verilog
9. Timing Controls
9.1. Delay Control Not synthesizable
This specifies the delay time units before a statement is executed during simulation. A delay time of zero can also be specified to force the statement to the end of the list of statements to be evaluated at the current simulation time. Syntax #delay statement; Example 9 .1 #5 a = b + c; #0 a = b + c;
// evaluated and assigned after 5 time units // very last statement to be evaluated
17
Peter M. Nyasulu
Introduction to Verilog
18
Peter M. Nyasulu
Introduction to Verilog
11. Functions
Functions are declared within a module, and can be called from continuous assignments, always blocks or other functions. In a continuous assignment, they are evaluated when any of its declared inputs change. In a procedure, they are evaluated when invoked. Functions describe combinational logic, and by do not generate latches. Thus an if without an else will simulate as though it had a latch but synthesize without one. This is a particularly bad case of synthesis not following the simulation. It is a good idea to code functions so they would not generate latches if the code were used in a procedure. Functions are a good way to reuse procedural code, since modules cannot be invoked from a procedure.
19
Peter M. Nyasulu
Introduction to Verilog
20
Peter M. Nyasulu
Introduction to Verilog
Syntax task task_name; input [msb:lsb] input_port_list; output [msb:lsb] output_port_list; reg [msb:lsb] reg_variable_list; parameter [msb:lsb] parameter_list; integer [msb:lsb] integer_list; ... statements ... endtask
Example 12 .1 module alu (func, a, b, c); input [1:0] func; input [3:0] a, b; output [3:0] c; reg [3:0] c; // so it can be assigned in always block task my_and; input[3:0] a, b; output [3:0] andout; integer i; begin for (i = 3; i >= 0; i = i - 1) andout[i] = a[i] & b[i]; end endtask always @(func or a or b) begin case (func) 2b00: my_and (a, b, c); 2b01: c = a | b; 2b10: c = a - b; default: c = a + b; endcase end endmodule
21
Peter M. Nyasulu
Introduction to Verilog
Example 0 .2 An Enabled Counter reg [7:0] count; wire enable; always @(posedge clk or posedge rst) // Do not include enable. begin ; if (rst) count<=0; else if (enable) count <= count+1; end; // 8 flip-flops will be generated.
22
Peter M. Nyasulu
Introduction to Verilog
13.2. Multiplexers
A multiplexer is inferred by assigning a variable to different variables/values in each branch of an if or case statement. You can avoid specifying each and every possible branch by using the else and default branches. Note that a latch will be inferred if a variable is not assigned to for all the possible branch conditions. To improve readability of your code, use the case statement to model large multiplexers. Syntax See Sections 8.9 and 8.10 for if ... else if ... else and case statements Example 13 .2
sel
a y b
sel[1:0]
a
b
c d
13.3. Adders/Subtracters
The +/- operators infer an adder/subtracter whose width depend on the width of the larger operand. Syntax See Section 7 for operators Example 13 .3 if (sel == 1) y = a + b; else y = c + d;
sel
a c
sel b
23
Peter M. Nyasulu
Introduction to Verilog
For synthesis
When modeling finite state machines, it is recommended to separate the sequential current-state logic from the combinational next-state and output logic. State Diagram for lack of space the outputs are not shown on the state diagram, but are: in state0: Zot = 000, in state1: Zot = 101, in state2: Zot = 111, in state3: Zot = 001. Example 14 .1 module my_fsm (clk, rst, start, skip3, wait3, Zot); input clk, rst, start, skip3, wait3; output [2:0] Zot; // Zot is declared reg so that it can reg [2:0] Zot; // be assigned in an always block. parameter state0=0, state1=1, state2=2, state3=3; reg [1:0] state, nxt_st; always @ (state or start or skip3 or wait3) begin : next_state_logic //Name of always procedure. case (state) state0: begin if (start) nxt_st = state1; else nxt_st = state0; end state1: begin nxt_st = state2; end state2: begin if (skip3) nxt_st = state0; else nxt_st = state3; end state3: begin if (wait3) nxt_st = state3; else nxt_st = state0; end default: nxt_st = state0; endcase // default is optional since all 4 cases are end // covered specifically. Good practice says uses it. always @(posedge clk or posedge rst) begin : register_generation if (rst) state = state0; else state = nxt_st; end always @(state) begin : output_logic case (state) state0: Zot = 3b000; state1: Zot = 3b101; state2: Zot = 3b111; state3: Zot = 3b001; default: Zot = 3b000;// default avoids latches endcase end endmodule
skip3=1
state1
state2
Using Macros for state definition As an alternative forparameter state0=0, state1=1, state2=2, state3=3; one can use macros. For example after the definition below 2'd0 will be textually substituted whenever `state0 is used. `define state0 2'd0 `define state1 2'd1 `define state2 2'd `define state3 2'd3; When using macro definitions one must put a back quote in front. For example: case (state) `state0: Zot = 3b000; `state1: Zot = 3b101; `state2: Zot = 3b111; `state3: Zot = 3b001;
24
Peter M. Nyasulu
Introduction to Verilog
14.1.
14.2. Counters
Counters are a simple type of finite-state machine where separation of the flip-flop generation code and the next-state generation code is not worth the effort. In such code, use the nonblocking <= assignment operator. Binary Counter Using toggle flip-flops
TC
1T C1 1T C1 1T C1 1T C1 CLK
Example 14 .2 reg [3:0] count; wire TC; // Terminal count (Carry out) always @(posedge clk or posedge rset) begin if (rset) count <= 0; else count <= count+1; end assign TC = & count; // See Reduction Operators on page 7
Q[3]
1D C1
Q[2]
1D C1
Q[1]
1D C1
Q[0]
1D C1 CLK
Q[3]
1D C1
Q[2]
1D C1
Q[1]
1D C1
Q[0]
1D C1
CLK
25
Peter M. Nyasulu
Introduction to Verilog
26
Peter M. Nyasulu
Introduction to Verilog
16.4. $deposit
$deposit sets a net to a particular value. Syntax $deposit (net_name, value); Example 16 .2 $deposit (b, 1b0); $deposit (outp, 4b001x);// outp is a 4-bit bus
16.6. $list
$list (hierarchical_name) lists line-numbered source code of the named module, task, function or named-block.
27
Peter M. Nyasulu
Introduction to Verilog
16.7. $random
$random generates a random integer every time it is called. If the sequence is to be repeatable, the first time one invokes random give it a numerical argument (a seed). Otherwise the seed is derived from the computer clock. Syntax Example 16 .3 reg [3:0] xyz; xzz = $random[(integer)]; initial begin xyz= $random (7); // Seed the generator so number // sequence will repeat if simulation is restarted. forever xyz = #20 $random; // The 4 lsb bits of the random integers will transfer into the // xyz. Thus xyz will be a random integer 0 xyz 15.
These can dump variable changes to a simulation viewer like cwaves. The dump files are capable of dumping all the variables in a simulation. This is convenient for debugging, but can be very slow. Syntax $dumpfile(filename.dmp) $dumpvar dumps all variables in the design. $dumpvar( 1, top) dumps all the variables in module top and below, but not modules instantiated in top. $dumpvar( 2, top) dumps all the variables in module top and 1 level below. $dumpvar( n, top) dumps all the variables in module top and n-1 levels below. $dumpvar( 0, top) dumps all the variables in module top and all level below. $dumpon initiates the dump. $dumpoff stop dumping. Example 16 .4 // Test Bench module testbench: reg a, b; wire c; initial begin; $dumpfile (cwave_data.dmp); $dumpvar //Dump all the variables // Alternately instead of $dumpvar, one could use $dumpvar(1, top) //Dump variables in the top module. // Ready to turn on the dump. $dumpon a=1; b=0; topmodule top(a, b, c); end
These are special commands for the Simulation History Manager for Cadence cwaves only. They will save variable changes for later display. Syntax $shm_open (cwave_dump.dm) $shm_probe (var1,var2, var3); /* Dump all changes in the above 3 variables. */ $shm_probe (a, b, inst1.var1, inst1.var2); /* Use the qualifier inst1. to look inside the hierarchy. Here inside module instance inst1 the variables var1 and var2 will be dumped.*/ Example 16 .5 // Test Bench module testbench: reg a, b; wire c; initial begin; $shm_open (cwave_data.dmp); $shm_probe(a, b, c) /* See also the testbench example in Test Benches on p. 29
28
Peter M. Nyasulu
Introduction to Verilog
Note also
var=$random wait(condition) statement
29
Peter M. Nyasulu
Introduction to Verilog
30
Peter M. Nyasulu