System Verilog
System Verilog
VERILOG
Bit extension
n 8’h ax = 1010xxxx MSB = 0, x or z Þ extend this
n 12’o 3zx7 = 011zzzxxx111 4’b x1 = 4’b xx_x1
4’b z1 = 4’b zz_z1
4’b 01 = 4’b 00_01
o If size is ommitted, it is interpreted as 32 bits MSB = 1 Þ zero extension
e.g. ‘d33 = 4’b 1x = 4’b 00_1x
0000_0000_0000_0000_0000_0000_0010_0001
o If radix is ommitted too .. decimal is assumed
e.g. 15 = <size>’d 15
12 THS. TRINH VU DANG NGUYEN
4. Net
A wire Y; // declaration
Y
B assign Y = A & B;
o Can be thought as hardware wires driven
by logic
wand Y; // declaration o Equal z (high impedance logic level)
assign Y = A; when unconnected
A assign Y = B; o Various types of nets
Y • wire
B • wand (wired-AND)
wor Y; // declaration • wor (wired-OR)
assign Y = A; • tri (tri-state)
assign Y = B;
o In following examples: Y is evaluated,
automatically, every time A or B changes
dr
tri Y; // declaration
A Y
assign Y = (dr) ? A : z;
• a = 4’b1010;
b = 4’b1100;
o & ® bitwise AND
o | ® bitwise OR
o ~ ® bitwise NOT
o ^ ® bitwise XOR
o ~^ or ^~ ® bitwise XNOR c = a ^ b;
a = 4’b1010;
b = 2’b11;
o Declaration
• integer i, k; //default 32-bit
• real r;
o Use as registers (inside procedures)
• i = 1; // assignments occur inside procedure
• r = 2.9;
• k = r; // k is rounded to 3
o Integers are not initialized!!
o Reals are initialized to 0.0
o & ® AND
o | ® OR >> ® logical shift right
o ^ ® XOR << ® logical shift left
o ~& ® NAND
o ~| ® NOR Result is same size as first operand, always zero filled
o ~^ or ^~ ® XNOR a = 4’b1010;
...
o One multi-bit operand ® One single-bit result
d = a >> 2; // d = 0010
• a = 4’b1001; c = a << 1; // c = 0100
• ..
• c = |a; // c = 1|0|0|1 = 1
o == ® logical equality
o != ® logical inequality
o === ® case equality
o !== ® case inequality
A
1
Y
B Y = (sel)? A : B;
0
sel
o+, -, *, /, %
oIf any bit of operand is x or z, then the result is tread as x
oNegative registers:
o regs can be assigned negative but are treated as unsigned
reg [15:0] regA;
..
regA = -5’d12;// stored as 216-12 = 65524
regA/3 // evaluates to 21841
• Negative integers:
• can be assigned negative values
• different treatment depending on base specification or not
reg [15:0] regA;
16-bit two’s complement of (-4)
integer intA;
sign
Use parentheses to
enforce the priority.
• Escaped chars:
• \n newline
• \t tab
• %% %
• \\ \
• \“ “
f
.. // declarations
inN outM
.. // description of f (maybe sequential)
endmodule
q Everything you write in Verilog must be inside a module exception: compiler directives
wire S, C, A, B;
A
S assign S = A ^ B;
B assign C = A & B; //assign C = A && B;
C
endmodule
cin
endmodule
module
• Inputs reg or net net
Internal connection http://www.asic-world.com/verilog/syntax2.html
External connection
• Syntax:
assign #del <id> = <expr>;
• Usage:
nand (out, in1, in2); //2-input NAND without delay
and #2 (out, in1, in2, in3); //3-input AND with 2 t.u. delay
not #1 N1(out, in); //NOT with 1 t.u. delay and instance name
xor X1(out, in1, in2); //2-input XOR with instance name
initial
$display(“I’m first”); Will be displayed
at sim time 0
initial
begin
#50; Will be displayed
$display(“Really?”); at sim time 50 t.u.
end
t.u. = time units
endmodule
•@
always @(signal1 or signal2 or ..)
begin
.. execution triggers every
end
time any signal changes
endmodule
d
initial begin
#5 c = 1; c
#5 b = 0;
#5 d = c; b
end
0 5 10 15
Time
Each assignment is
blocked by its previous one
d
initial begin
fork c
#5 c = 1;
#5 b = 0; b
#5 d = c;
join 0 5 10 15
end Time
Assignments are
THS. TRINH VU DANG NGUYEN
not blocked here
25. Procedural Statements: If
reg [3:0] Y;
wire start;
integer i;
initial
Y = 0;
q Can be used for synthesizable code but has generally been difficult to use.
THS. TRINH VU DANG NGUYEN
28. Procedural Statements: While
initial
repeat (times) stmt; Y = 0;
endmodule
Executes until sim
finishes
simple reg Y;
wire c, clk, res;
res wire n;
c n Y not(n, c); // gate-level
clk
always @(res or posedge clk)
if (res)
Y = 0;
else
Y = n;
endmodule
in[3:0] p_in[3:0]
out[1:0]
wu
Implelementation
without parameters
wd
clk
module dff4bit(Q, D, clk); module dff2bit(Q, D, clk);
output [3:0] Q; output [1:0] Q;
input [3:0] D; input [1:0] D;
input clk; input clk;
endmodule endmodule
Khai báo Để khai báo một signal (tín hiệu), ta cần gán cho nó data type (dạng dữ liệu), trong SystemVerilog, data type
được sử dụng là logic.
logic có 4 giá trị: 0, 1, x, z. Data type này có thể tổng hợp được (synthesizable).
Keyword input và output báo cho compiler biết signal này là port (cổng) của design.
THS. TRINH VU DANG NGUYEN
2. Vector và Number
Vector Trong thực tế, dữ liệu không phải lúc nào cũng chỉ có 1 bit (1 dây signal) nhưng có nhiều, ví dụ 1 byte = 8
bit, hay signal có 8 dây. Khai báo một vector như sau:
o Với một vector signal được khai báo như sau: logic [7:0] signal
• Truy cập bit thứ i (0 ≤ i ≤ 7): signal[i]
• Truy cập một slice bit từ bit thứ i tới bit thứ j (0 ≤ i ≤ j ≤ 7): signal[j:i]
Trong SystemVerilog còn có một dạng gán giá trị như sau:
Như tên của nó, continuous assignment, nên vị trí các dòng assign không ảnh hưởng tới kết quả sau cùng.
Đối với vector, từng cặp bit tương ứng sẽ thực hiện phép toán bitwise với nhau.
Nên sử dụng logical operators với giá trị đơn (1-bit), đặng xác định giá trị true/false (điều kiện).
1. Cần lưu tâm đến việc sử dụng Arithmetic operators, ví dụ cần có FPGA có hỗ trợ bộ nhân mới nên sử dụng phép *
khi assign.
2. Phép toán + và﹣thì hầu hết đều được hỗ trợ, nhưng các phép toán còn lại chỉ nên sử dụng đặng tính toán số học,
ví dụ các thông số, thay vì dùng trong assign.
3.7 Conditional operators – Phép toán điều kiện
Phép toán điều kiện là cách ngắn gọn thay vì tạo một bộ mux và sử dụng nó. Phép toán này có dạng giống như
trong C.
Condition phải mang giá trị true/false, nên cần đặt condition là signal đơn (1-bit), reduction operators, logical
operators, hoặc phép so sánh.
THS. TRINH VU DANG NGUYEN
3.8 Concatenate and replicate operators – Phép toán ghép và lặp số
Đối với việc tạo ra vector có số bit lớn, hoặc ghép nhiều signal lại thành một signal có số bit lớn.
Khi thiết kế cần bảo đảm sự đầy đủ của các giá trị case hoặc có default.
o Lúc này việc truy cập các byte sẽ tiện hơn rất nhiều.
o Nhìn từ trái qua phải, register khai báo ở dạng array được hiểu như sau:
o Có thể hiểu vector như một bảng chỉ có 1
hàng, còn array là một bảng có nhiều hàng,
hoặc nhiều tập hợp bảng.
2. Thiết kế bộ tính toán đơn giản N bit với yêu cầu sau:
a. 3 ngõ vô data0, data1, và sel
b. 1 ngõ ra result
c. 4 phép toán (NONE, ADD, AND, OR)
Từ ví dụ trên, chỉ sử dụng non-blocking statement trong always, còn blocking statement thì trong
always_comb nhưng với sự thông hiểu datapath của mạch.
Tuy nhiên, để báo cho compiler biết ở đó người thiết kế muốn tạo ΟipΟop, always_Λ sẽ được sử dụng thay cho
always thông thường.
Luôn luôn sử dụng always_ff khi thiết kế mạch có liên quan tới flipflop hoặc register.
3. Thiết kế mạch counter 4-bit đếm lên nếu en = 1, ngõ ra counter cho biết giá trị hiện tại, ngõ ra này
luôn bằng 0 nếu rst = 0.
Tuy nhiên, SystemVerilog cung cấp một phương tiện giúp khai báo dễ hơn và dễ đọc hơn.
o typedef giúp tạo User-defined data type, logic[1:0] là storage
type, do ở đây có 3 giá trị nên cần 2 bit.
o Lúc này, light_e là một data type, có 3 giá trị RED, YELLOW,
và GREEN. Khai báo light có data type là light_e.
o Nếu không gán giá trị cho items trong lúc khai báo enum, khi
synthesize, compiler sẽ tự tìm đường tối ưu hơn hết cho
code.
inputs
0
1
outputs
w0 y0
w1 y1
y2
En y3
o Conditional operator
o if-else statement
o case statement
o casez, casex
o Format
• Conditional_expression ? true_expression : false_expression
o Behavior
• If the conditional expression is 1 (true), then the value of true_expression is chosen;
• Otherwise, the value of false_expression is chosen
o Example
• A = (B<C) ? (D+5) : (D+2);
o Can be used in continuous assign and procedural statements
Behavior
w0
s s f f (s, w0 , w1 ) = s w0 + sw1
s f
w0 0 0 w0
f
w1 1 1 w1
Logic expression w1
Gates
THS. TRINH VU DANG NGUYEN
1.1 Conditional Operator
s1
s0 s0
w0
s1
w0 0
w1 w1 1
f
0
w2 f
1
w2 0
w3
w3 1
f = w1; w0 0
f
0 w0
w1 1 w1
endmodule 1
module mux4to1 (w0, w1, w2, w3, S, f); module mux4to1 (W, S, f);
input w0, w1, w2, w3; input [0:3] W;
input [1:0] S; input [1:0] S;
output f; output f;
reg f; reg f;
endmodule endmodule
M[3]
endmodule
w12
w15
A 16-to-1 multiplexer.
THS. TRINH VU DANG NGUYEN
1.3. Case Statement
• Format module mux4to1 (W, S, f); s0
s1 s1 s0 f
input [0:3] W;
case (expression) w0 00 0 0 w0
input [1:0] S; w1 01
f 0 1 w1
alternative1: statement; output f;
w2
w3
10 1 0 w2
11 w3
1 1
alternative2: statement; reg f;
… s1
alternativej: statement; always @(W or S) s0
case (S)
[default: statement;]
0: f = W[0];
endcase 1: f = W[1]; w0 0
endmodule
endmodule
always @(W)
begin
z = 1;
casex(W)
x x 4'b1xxx: Y = 3;
4'b01xx: Y = 2;
4'b001x: Y = 1;
4'b0001: Y = 0;
default: begin
z = 0;
Y = 2'bx;
x: don’t care end
endcase
end
THS. TRINH VU DANG NGUYEN
endmodule
2. Sequential Circuit Blocks
end
endmodule Q
endmodule
Clock Q
begin
g = f | x3;
f = x1 & x2;
x1
end D Q f
endmodule x2
Clock Q
THS. TRINH VU DANG NGUYEN
2.2 Assignments (overview)
module nonblocking_assign (x1, x2, x3, Clock, f, g); x3
D Q g
input x1, x2, x3, Clock;
output f, g;
Q
reg f, g;
always @(posedge Clock)
begin
f <= x1 & x2; x1
g <= f | x3; x2
D Q f
end
endmodule Clock Q
Clock
Mealy-type
Combinational Z
Combinational Q
Flip-flops circuit
circuit
W
reg [2:1] y, Y;
parameter [2:1] A = 2'b00, B = 2'b01, C = 2'b10;
State assignment
output
// Define the next state combinational circuit
always @(w or y)
case (y)
A: if (w) Y = B; Moore-type
else Y = A;
B: if (w) Y = C;
else Y = A;
C: if (w) Y = C;
else Y = A;
default: Y = 2'bxx;
endcase
// Define output Y y
output_signal =….; Next state Output z
end w Flip-flops
circuit circuit
// Define the sequential block
always @(negedge Resetn or posedge Clock)
if (Resetn == 0)y <= STATE1; clock Resetn
else y <= Y;
endmodule
THS. TRINH VU DANG NGUYEN
3.2 Second Verilog Template
module simple (Clock, Resetn, w, z);
input Clock, Resetn, w;
y: present state
output z; Y: next state
reg z;
reg [2:1] y, Y;
parameter [2:1] A = 2'b00, B = 2'b01, C = 2'b10; State assignment
// Define output
assign output_signal=….; y
Next state Y Output z
/*assign cannot be put inside w Flip-flops
the always block*/ circuit circuit
endmodule
clock Resetn
// Define output
assign z = (y == C); /*assign cannot be put inside
endmodule the always block*/
116
Flip-flop with clear capability
module flipflop (D, Clock, Resetn, Q);
input D, Clock, Resetn;
output Q;
reg Q;
endmodule
else
Q <= D;
C lo c k Q Q
endmodule
endmodule
Asynchronous reset: by using sensitivity list and if-else 119
N-bit register module
module regn (D, E, Clock, Q);
parameter n = 8;
input [n-1:0] D; D Q
input E, Clock;
output [n-1:0] Q; regn
reg [n-1:0] Q; Clock
endmodule
121
N-bit shift register
module shift4 (D, E, w, Clock, Q); module shiftn (D, E, w, Clock, Q);
input [3:0] D; parameter n = 16;
input E, w, Clock; input [n-1:0] D;
output [3:0] Q; input E, w, Clock;
output [n-1:0] Q;
reg [3:0] Q; reg [n-1:0] Q;
always @(posedge Clock) integer k;
if (E)
Q <= D; always @(posedge Clock)
else if (E)
begin Q <= D;
Q[0] <= Q[1]; else
Q[1] <= Q[2]; begin
Q[2] <= Q[3]; for (k = 0; k < n-1; k = k+1)
Q[k] <= Q[k+1];
Q[3] <= w; Q[n-1] <= w;
end end
endmodule endmodule
122
Up-counter
module upcount (Resetn, Clock, E, Q);
input Resetn, Clock, E;
output [3:0] Q;
reg [3:0] Q;
endmodule