System Verilog 1
System Verilog 1
Guest lecture
Elena Hammari,
Digital & DFT designer
Nordic Semiconductor ASA 1
Lecture plan
3
Modern digital design HDL description
flow of testbench
HDL description module Mux2to1TestBench
of design logic a = 1’b0;
logic b = 1’b1;
module Mux2to1 logic s,c;
( input logic in0, Mux2to1 MyMux(.in0(a),
input logic in1, .in1(b),.sel(s),.out(c));
input logic sel, initial begin
output logic out); s = 1’b0;
assign out = sel? in1 : in0; assert (c == 1’b0) else
endmodule $error(«Output is not
correct!»);
# 10 ns;
EDA tools* s = 1’b1;
assert (c == 1’b1) else
$error(«Output is not
correct!»);
end
endmodule
EDA
tools
*EDA = Electronic Design Automation, software environments for designing electronic systems 4
Design abstraction levels
state
block diagram, machine,
specification data and control flow ALU
Layout
Circuit level Gate level
HDL
6
Design views and abstraction levels
endmodule
7
SystemVerilog
9
SystemVerilog data types
Data types with 2 states (0,1):
Integer data types: TYPE Description Example
bit user-defined size bit [3:0] a_bit;
byte 8 bits, signed byte a, b;
shortint 16 bits, signed shortint c, d;
int 32 bits, signed int i,j;
longint 64 bits, signed longint lword
12
SystemVerilog number formats
13
SystemVerilog negative numbers
15
SystemVerilog nets and variables
variable
assignment
SystemVerilog nets and variables
optional
Variable declaration:
Style 1: Style 2:
[data type] [vector range] [identifier] var [data type] [vector range] [identifier]
logic myVariable; var logic myVariable;
var myVariable; // assumes logic datatype
int i = 1; // initial value 1 var int i = 1;
byte [3:0] myMemoryWord; var byte [3:0] myMemoryWord;
18
SystemVerilog nets and variables
optional
Net declaration:
[net type] [drive/charge strength] [vector range] [data type] [delay] [identifier]
wire myWire; // assumes logic datatype
tri [7:0] myTristateBus; // a tristate bus with 8 bits
wire #5ps w = varA & varB; // continuous assignment as part of
// declaration
19
SystemVerilog nets and variables
Built-in net types:
wire tri tri0 supply0
wand triand tri1 supply1
wor trior trireg uwire
Truth table for resolving multiple drivers on wire and tri nets:
wire / tri 0 1 X Z
0 0 X X 0
1 X 1 X 1
X X X X X
Z 0 1 X Z
20
SystemVerilog constants
21
SystemVerilog arrays
unpacked packed
23
SystemVerilog arrays
Array operations
Reading and writing an array A = B; //unpacked dim must match!
Reading and writing a one-dim. slice A[I:J] = B[K:L];
Reading and writing an element A[I] = B[J];
Equality of array, slice or element if (A == B) …
(Non-equality) if (A[I:J] != B[K:L]) …
25
SystemVerilog arrays
• Concatenation {}
• Forms a single value from several sized values
• Useful for assignment of packed arrays
logic [11:0] vec;
vec = {4’h0, 8’hff} ; // same as 12’h0ff
• For assignment of unpacked arrays ’{} is used
int array[0:3] = ’{1,2,3,4}; // each element assigned separately
int twobyfour[1:4][1:2] = ’{ ’{1,1}, ’{2,2}, ’{3,3}, ’{4,4}};
• Replication {N{…}} or ’{N{…}}
• Used to describe concatenations where the same value is repeated N times:
vec = {4’h0, 2{4’hf}}; // 12’h0ff
array = ’{4{1}};
27
SystemVerilog assignments
• Assigment is the basic mechanism for placing values into nets and
variables
• Similar to statements in software programming languages
• Values can be numeric or an expression containing other nets and variables and
operators
• Two types:
• continuous assignment
• procedural assignment
SystemVerilog assignments
continuous assignment
• starts with keyword assign (1), or in a net declaration (2)
• the value is assigned with “=” operator
• happens continuously: like a combinational circuit that continuously drives a net
• works on nets or variables
• a single continuous assignment for each variable (3)
(1) (2) (3)
logic a, b, a_or_b; wire enable = powerOn; logic a,b;
assign a_or_b = in_a | in_b; logic enabled; assign a = 1’b1;
assign enabled = 1’b1; assign b = a;
assign a = 1’b0;
Wrong!
SystemVerilog assignments
procedural assignment
• occurs within procedures always, initial, task, function,
• or in a variable declaration
• does not have duration, happens instantaneously
• the variable holds the value until the next assignment
• works only on variables, cannot be used for nets
• can be blocking “=” or non-blocking “<=”
• Symbols similar to C
• Some operators target specific data types
• Operators translate to gates when synthesized
• The logic gate realization depends on several factors:
• coding style
• synthesis tool used
• synthesis optimization settings (area, power, timing)
• Not all operators are synthesizable 16-bit adder
Relational Operators
• Return logical 1 if expression is true
• Return logical 0 if expression is false
• Binary operators: (work on two operands)
> greater-than // Let a = 4, b = 3,
< less-than // x = 4’b1010, y = 4’b1101, z = 4’b1xxx
a <= b // 0
>= greater-than-or-equal-to a > b // 1
<= less-than-or-equal-to y >= x // 1
y < z // x
• If any operand bit has a value ”x”, the result of the expression is all ”x”
36
SystemVerilog operators
37
SystemVerilog operators
Bitwise Operators
• Perform bit-by-bit operations
• Mismatched length operands are zero extended
• x and z treated the same
• Binary operators: (work on two operands)
& and, ~& nand
| or, ~| nor
^ xor,
^- , -^ xnor
• Unary operator: (single one-bit operand)
∼ negation 38
SystemVerilog operators
Bitwise Operators
39
SystemVerilog operators
Bitwise Operators
• Logical operators result in logical 1, 0 or x
• Bitwise operators results in a bit-by-bit value
// let x = 4’b1010, y = 4’b0000
x | y // Bitwise OR, result is 4’b1010
x || y // Logical OR, result is 1
40
SystemVerilog operators
Bitwise Operators
• Give bit-by-bit results:
41
SystemVerilog operators
Reduction Operators
•Operate on only one operand
•Perform a bitwise operation on all bits of the operand
•Return a 1-bit result
•Works from right to left, bit by bit
Unary operators: (work on single operand)
& and, ~& nand
//let x = 4’b1010
| or, ~| nor &x //equivalent to 1 & 0 & 1 & 0. Results in 1’b0
^ xor, |x //equivalent to 1 | 0 | 1 | 0. Results in 1’b1
^x //equivalent to 1 ^ 0 ^ 1 ^ 0. Results in 1’b0
^- , -^ xnor
42
SystemVerilog operators
Reduction Operators
• Example: generation of parity bit using XOR operator
// 8-bit parity generator
// output is one if odd
// number of ones
module parity8(
input [7:0] d_in,
output parity_out);
endmodule
43
SystemVerilog operators
Reduction Operators
•Operate on only one operand
•Perform a bitwise operation on all bits of the operand
•Return a 1-bit result
•Works from right to left, bit by bit
Unary operators: (work on single operand)
& and, ~& nand
// Let x = 4’b1010
| or, ~| nor &x //equivalent to 1 & 0 & 1 & 0. Results in 1’b0
^ xor, |x //equivalent to 1 | 0 | 1 | 0. Results in 1’b1
^x //equivalent to 1 ^ 0 ^ 1 ^ 0. Results in 1’b0
^- , -^ xnor
44
SystemVerilog operators
Shift Operators
• Shift operator shifts a vector operand left or right by a specified number of bits, filling
vacant bit positions with zeros.
• Shifts do not wrap around.
• Arithmetic shift uses context to determine the fill bits (sign-filled shift).
Unary operators: (work on single operand)
>> right shift
// Let x = 4’b1100
<< left shift y = x >> 1; // y is 4’b0110
>>> arithmetic right shift y = x << 1; // y is 4’b1000
<<< arithmetic left shift y = x << 2; // y is 4’b0000
45
SystemVerilog operators
46
SystemVerilog operators
Conditional operator ?:
• Operates like the C statement
conditional expression ? true expression : false expression ;
• The conditional expression is first evaluated
• If the result is true, true expression is evaluated
• If the result is false, false expression is evaluated
• If the result is x:
• Both true and false expressions are evaluated,...
• their results compared bit by bit,...
• returns a value of x if bits differ, OR...
• the value of the bits if they are the same.
• This is an ideal way to model a multiplexer or tri-state buffer.
47
References
• IEEE 1800-2017
• http://web.engr.oregonstate.edu/~traylor/ece474/beamer_lectures/
• https://www.quora.com/What-are-the-various-stages-of-a-modern-
digital-design-flow-using-Gajski-and-Kuhn-s-Y-chart
48