Verilog Session: General Introduction To Verilog HDL: EE282 Spring Quarter, 2001-2002
Verilog Session: General Introduction To Verilog HDL: EE282 Spring Quarter, 2001-2002
EE282
Spring Quarter, 2001-2002
General Features:
• Support for describing circuit connectivity
• High-level programming language support for describing
behavior
• Support for timing information (constraints, etc.)
• Support for concurrency
1
Verilog Simulation Model
• Verilog uses event-driven simulation.
Event Driven Simulation:
2
Key Verilog Features: Module Ports
3
Verilog Logic System
• 0 : zero, low, false, logic low, ground...
• X : unknown...
4
Verilog Variable Declaration
• Declaring a net
wire [<range>] <net_var> [<, net_var>*]
• Declaring a register
reg [<range>] <reg_var> [<, reg_var>*]
• Samples
reg r; // 1-bit reg variable.
wire w1, w2; // 2 1-bit wire variables.
reg [7:0] vreg; // 8-bit register; least significant bit is 0, i.e. vreg[0].
wire [0:11] vw1, vw2; // 2 12-bit nets; LSB is 11, i.e. vw1[11].
• Range information is specified as [MSB:LSB]. If no range exists, the
corresponding variable has a bitwidth of one
5
Behavioral Modelling in Verilog
6
Timing Control in Procedural Blocks
• Delays:
Used to delay the subsequent statement by a specified amount of time.
#10 clk = 1;
#10 clk = 0;
• Triggering control: @ (trigger_event)
Delays execution until trigger_event changes or transitions. The
trigger_event can be a signal/expression or multiple expressions
linked using the keyword or. It is also possible to detect for particular
transitions by using the keywords posedge or negedge.
always @(posedge CLK) q = d;
always @(i0 or i1) o = i0 & i1;
Procedural Assignments
• Regular assignments inside procedural blocks:
lhs_expr = rhs_expr;
• The lhs_expr must be a variable of the type register.
• There are no constraints on data types included in the rhs_expr.
• Common Error:
If a variable is not declared, it defaults to a 1-bit wire.
Thus, if an undeclared variable appears on the left-hand-side of a
procedural assignment, the Verilog compiler will generate an error
message, “Illegal left-hand-side assignment”.
So, use defensive code and declare all signals you read/write.
7
Operators in Verilog
• Arithmetic: + , - , * , / , % (2’s complement)
• Binary bit-wise: ~ , & , | , ^ , ~^
• Unary reduction: & , ~& , | , ~| , ^ , ~^
• Logical: ! , && , || , == , === , != , !==
• Relational: < , < , >= , <=
• Logical shift: >> , <<
• Conditional: ?:
• Concatenation: {}
• Common Error:
A lot of confusion arises between the “==” and “===” operators:
== returns “x” if either if the input bits is “x” or “z” while === does
compare “x”s and “z”s.
Conditional Statements
• If, If-Else Statements:
if (branch_flag > 0)
begin
PC = PCbr;
end
else
PC = PC + 4;
• Case Statements.
case (opcode)
6’b001010 : write_mem = 1;
6’b100011 : enable_alu = 1;
default :
begin
$display(“Unknown opcode: %h”, opcode);
end
endcase
• Could also use casez (compare z values) and casex (compare z and
x).
8
Loop Constructs in Verilog
Repeat Loops:
• Repeats a block of statements a fixed number of times:
repeat (<size>) <block>
For Loops:
• Same as in C:
for (<loop_initialization>; <loop_condition>; <loop_update>) <block>
for (memaddr = 0; memaddr < memsize; memaddr = memaddr + 1)
$display(“Memory at address 0x%h is 0x$h.”, memaddr,
memory[memaddr]);
• Loop executes while loop_condition evaluates to TRUE.
Continuous Assignments
• Procedural assignments are used to assign values into register type
variables.
• Continuous assignments do the same for net type variables.
• Whenever the right-hand-side of the assignment changes, the left-
hand-side is automatically and immediately updated to reflect that
change.
• Generally, continuous assignments are used to model combinational
logic or make a simple connection.
Sample Assignment.
assign o = i0 & i1;
9
Modelling Memory in Verilog
• Declaring memory:
reg [<MSB>:<LSB>] <memory_var> [<start_addr>:<end_addr>];
• Verilog does not provide support for bit accesses into memory arrays.
10
Verilog Lexical Conventions
System Tasks and Functions.
• Representation: $<identifier>.
• $time - returns the current simulation time.
• $display - used for formatted printing like printf in C.
• $stop - stops simulation.
• $finish - ends simulation.
• $readmemh - load memory array from user’s text file in hex format.
Compiler Directives.
• A compiler directive is immediately preceded by a grave accent (‘).
• ‘define - defines a compile-time constant or macro.
• ‘ifdef - ‘else - ‘endif - provide support for conditional compilation.
• ‘include - simple text inclusion.
11