13. Real Numbers
Verilog supports real constants and variables
Verilog converts real numbers to integers by rounding
Real Numbers can not contain 'Z' and 'X'
Real numbers may be specified in either decimal or scientific
notation
< value >.< value >
1) 1.2 1.2
2) 0.6 0.6
< mantissa >E< exponent >
1) 3.5E6 3500000
Real numbers are rounded off to the nearest integer when
assigning to an integer.
13
20. Module Port Connected
語法
1) by Port Order ( C/C++ like)
<module name> <module inst_name>
(<port1>,<port2>,….,<portn>);
1) by Name Order
<module name> <module inst_name>
(.<port1_name>(<port1>),
.<port2_name>(<port2>),
….,
.<portn_name>(<portn>));
20
21. Module Port Connected by Port Order
module add(a,b,sum,co); moduel top(a_in,b_in,sum_out,carry_out);
input a,b; input a_in,b_in;
output sum,co; output sum_out,carry_out;
wire sum,co; wire sum_out,carry_out;
assign sum = a^b; add adder(a_in,b_in,sum_out,carry_out);
assign co = a&b; endmodule
endmodule module inst_name
adder
a_in a sum sum_out
b_in b add co carry_out
module name
top
21
22. Module Port Connected by Name Order
moduel top(a_in,b_in,sum_out,carry_out);
module add(a,b,sum,co); input a_in,b_in;
input a,b; output sum_out,carry_out;
output sum,co; wire sum_out,carry_out
wire sum,co; add adder(.a(a_in),
assign sum = a^b; .b(b_in),
assign co = a&b; .sum(sum_out),
endmodule .co(carry_out));
endmodule
adder
a_in a sum sum_out
b_in b add co carry_out
top
22
23. Module Port Connected by multi-module
moduel top(a1_in,b1_in,
sum1_out,carry1_out,
a2_in,b2_in,
adder1 sum2_out,carry2_out);
input a1_in,b1_in;
a1_in a sum sum1_out
output sum1_out,carry1_out;
input a2_in,b2_in;
b1_in b add co carry1_out output sum2_out,carry2_out;
wire sum1_out,carry1_out;
wire sum2_out,carry2_out;
adder2
add adder1(.a(a1_in),
a2_in a sum sum2_out .b(b1_in),
.sum(sum1_out),
b2_in b co carry2_out .co(carry1_out));
add
add adder2(.a(a2_in),
.b(b2_in),
top .sum(sum2_out),
.co(carry2_out));
endmodule
23
25. Module Port Connected by value
moduel top(a1_in,b1_in,sum1_out,carry1_out,
a2_in,b2_in,sum2_out,carry2_out);
input a1_in,b1_in;
output sum1_out,carry1_out;
input a2_in,b2_in;
output sum2_out,carry2_out;
wire sum1_out,carry1_out;
wire sum2_out,carry2_out;
add adder1(.a(a1_in),.b(b1_in),.sum(sum1_out),.co(carry1_out));
1’b1
add adder2(.a2_in,b2_in,sum2_out,carry2_out);
1’b1
endmodule
25
26. Data type
Verilog has two data types
1) Nets
Represent structural connections between components
1) Registers
Represent variables used to store data
Explicitly declared
1) With a declaration in your Verilog code.
Implicitly declared
1) with no declaration when used to connect structural
building blocks in your code. Implicit declaration is
always a net type "wire" and is one bit wide.
26
27. Data type
module
nets/registers nets/registers
nets nets
nets
nets
27
28. Data Types of Nets
Net Data Type Function
wire, tri Interconnecting wire
wor, trior Wired output ‘OR’ together
wand, triand Wired output ‘AND’ together
tri0, tri1 Net pull-down or pull-up when no
driving
supply0, supply1 Net constant logic 0 or 1 (supply
strength)
trireg Retains last value, when drive by
z(tristate)
註 : 在這麼多的 Net Types 裡 , ‘wire’ 是常用到的 Data type
28
29. Data Types of Register
Net Data Function
Type
reg Unsigned variable
integer Signed variable – 32 bits
time Unsigned integer – 64 bits
real Double precision floating point variable
註 : 在這麼多的 Register Types 裡 , ‘reg’ 是常用到的 Data type
29
31. Verilog Operators - Relational
Operators 功能 Notes
a<b a 是否小於 b 判斷不包
括’ z’ 或’ x’
a>b a 是否大於 b 判斷不包
括’ z’ 或’ x’
a <= b a 是否小於等於 b 判斷不包
條件成立 , 回覆 ‘ 1’ 括’ z’ 或’ x’
a >= b ,‘ x’ 時 ,‘ 0’ ‘ x’
條件失敗 回覆
如果含有 a 是否大於等於 b 判斷不包
回覆
括’ z’ 或’ x’
31
32. Verilog Operators - Equality
Operators 功能 Notes
a === b a 是否等於 b 判斷包括’ z’ 或’ x’
a !== b a 是否不等於 b 判斷包括’ z’ 或’ x’
a == b a 是否等於 b 判斷不包括’ z’ 或’ x’
a != b a 是否不等於 b 判斷不包括’ z’ 或’ x’
條件成立 , 回覆 ‘ 1’
條件失敗 , 回覆 ‘ 0’
32
33. Verilog Operators – Logical
Operators 功能 Notes
! Logical 反相
&& Logical and
|| Logical or
33
34. Verilog Operators – Bit-wise
Operators 功能 Notes
~ 反相 ~x = x
& and 0 & x = 0
1&x=x&x=x
| or 1|x=1
0|x=x|x=x
^ xor 0^x=1^x=x^x=x
^~ or ~^ xnor 0 ~^ x = 1 ^~ x = x ~^ x = x
34
37. Verilog Operators – Replication
語法
1) {n{m}} replicate value m, n times
Example
1) {3{a}} {a,a,a}
2) {b,{3{c,d}}} {b,c,d,c,d,c,d}
註 : 藍色的 ‘ {}’ 是 Concatenation
37
38. Verilog Operators – Conditional
語法
1) <cond_expr> ? <true_expr> : <false_expr>;
Example
1) a = (c==b) ? d : 1’b1;
當 c 等於 b 時 , a 等於 d, 否則 a 等於 ‘ 1’
1) out = enable ? Data_out : 1’bz;
當 enable 為 ‘ 1’ 時 , out 等於 Data_out, 否則 out
等於 ‘ z’
1) y = en1 ? dout1 :
en2 ? dout2 : 1’bz;
當 en1 為 ‘ 1’ 時 , y 等於 dout1, 當 en1 為 ‘ 0’ 且
en2 為 ‘ 1’ 時 , y 等於 dout2, 否則 y 為 ‘ z’
註 : <enable ?> 這樣的寫法等同於 <(enable==1’b1)?>
38
39. Verilog Abstraction Levels
Behavioral
1) Higher level of modeling where behavior of logic
is modeled.
RTL
1) Logic is modeled at register level
Structural
1) Logic is modeled at both register level and gate
level
39
40. Procedural Blocks
initial
1) 只在一時間為零時執行 , 且只執行一次
2) 通常應用在 simulation model
always
1) 始終循環執行
注意 , 在 initial 以及 always 內被指定的 Data type 都會是 reg 的型態
40
41. Procedural Blocks
module initial_bad_style(clk,rst,en,dout); module initial_good_style(clk,rst,en,dout);
output clk,rst,en,dout; output clk,rst,en,dout;
reg clk,rst; reg clk,rst;
wire en,dout; reg en,dout;
initial initial
begin begin
clk = 1’b0; clk = 1’b0;
rst = 1’b0; rst = 1’b0;
en = 1’b0; en = 1’b0;
dout = 1’b1; dout = 1’b1;
end end
endmodule endmodule
41
42. Procedural Blocks
module proc_good_style(clk,en,dout); module proc_good_style(clk,en,dout);
input clk, en input clk, en
output dout; output dout;
wire dout; reg dout;
always @(posedge clk) always @(posedge clk)
begin begin
if(en==1’b1) if(en==1’b1)
dout = 1’b1; dout = 1’b1;
else else
dout = 1’b0; dout = 1’b0;
end end
endmodule endmodule
42
46. Blocking and Non-blocking
Blocking Non-Blocking
reg a = 1’b1; reg a = 1’b1;
reg b = 1’b0; reg b = 1’b0;
always @(posedge clk) always @(posedge clk)
begin begin
a = b; a <= b;
b = a; b <= a;
end end
a a
b
b
46
48. case
語法
1) <case>(< 判斷子 >)
< 條件 1> : begin < 執行式 1> end
< 條件 2> : begin < 執行式 2> end
…..
<default> : begin < 執行式 n> end
<endcase>
48
49. case initial
begin
#0 b[3:0] = 4’b0000;
#1 b[3:0] = 4’b0001;
#1 b[3:0] = 4’b0010;
case(b[3:0]) #1 b[3:0] = 4’b0011;
4’b0000: begin <s1> end #1 b[3:0] = 4’b0100;
4’b0001: begin <s2> end #1 b[3:0] = 4’b0101;
4’b0010: begin <s3> end #1 b[3:0] = 4’b0110;
4’b0100: begin <s4> end #1 b[3:0] = 4’b0111;
4’b1000: begin <s5> end #1 b[3:0] = 4’b1000;
4’b1110: begin <s6> end #1 b[3:0] = 4’b1001;
default: begin <sn> end #1 b[3:0] = 4’b1010;
endcase #1 b[3:0] = 4’b1011;
#1 b[3:0] = 4’b1100;
#1 b[3:0] = 4’b1101;
#1 b[3:0] = 4’b1110;
#1 b[3:0] = 4’b1111;
end
s1 s2 s3 sn s4 sn sn sn s5 sn sn sn sn sn s6 sn sn sn sn sn
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 49
50. casez casex
語法
1) <casez/casex>(< 判斷子 >)
< 條件 1> : begin < 執行式 1> end
< 條件 2> : begin < 執行式 2> end
…..
<default> : begin < 執行式 n> end
<endcase>
casez : ‘z’ as don’t care
casex : ‘z’ and ‘x’ as don’t care
50
52. Edge sensitive event control
語法
1) @(<posedge/negedge> <signal>)
begin
< 執行式 >
end
2) @(<posedge/negedge> <signal> or
<posedge/negedge><signal>)
begin
< 執行式 >
end
52
53. Edge sensitive event control
initial
begin
#0 clk <= 1’b0;
module add(clk,rst,a,b,sum,co); forever #1 clk <= ~ clk;
input clk,rst,a,b; initial
begin end
output sum,co;
reg sum,co; #0 {rst,a,b} <= {1’b0,1’b1,1’b1};
#2 {rst,a,b} <= {1’b1,1’b0,1’b0};
always @(posedge clk or negedge rst) #2 {rst,a,b} <= {1’b1,1’b1,1’b0};
begin #2 {rst,a,b} <= {1’b1,1’b0,1’b1};
if(rst==1’b0)
#2 {rst,a,b} <= {1’b1,1’b1,1’b1};
begin
sum <= 1’b0;
end
co <= 1’b0;
sum
end
else co
begin a
sum <= a ^ b; b
co <= a & b;
rst
end
end clk
endmodule
0 1 2 3 4 5 6 7 8 9
53
54. Level sensitive event control
語法
1) @(<signal>)
begin
< 執行式 >
end
2) @(<signal> or <signal>)
begin
< 執行式 >
end
54
55. Level sensitive event control
module add(clk,rst,a,b,sum,co); initial
input clk,rst,a,b;
begin
output sum,co;
reg sum,co;
#0 {rst,a,b} <= {1’b0,1’b1,1’b1};
#2 {rst,a,b} <= {1’b1,1’b0,1’b0};
always @(a or b or rst) #2 {rst,a,b} <= {1’b1,1’b1,1’b0};
begin #2 {rst,a,b} <= {1’b1,1’b0,1’b1};
if(rst==1’b0) #2 {rst,a,b} <= {1’b1,1’b1,1’b1};
begin end
sum <= 1’b0;
co <= 1’b0;
end sum
else
begin co
sum <= a ^ b; a
co <= a & b;
end b
end rst
endmodule
0 1 2 3 4 5 6 7 8 9
55
56. Tri-State buffer
module tri_buff(in,out,en); initial
input in,en; begin
output out; #0 {in,en} <= {1’b0,1’b0};
wire out; #1 {in,en} <= {1’b0,1’b1};
#1 {in,en} <= {1’b1,1’b0};
assign out = (en==1’b1) ? in : 1’bz; #1 {in,en} <= {1’b1,1’b1};
end
endmodule
in
en
out
0 1 2 3 4 5
56
62. Simulation - $time, $stime, $realtime
These return the current simulation time as a
64-bit integer, a 32-bit integer, and a real
number, respectively
62
63. Simulation - $reset, $stop, $finish
$reset resets the simulation back to time 0;
$stop halts the simulator and puts it in
interactive mode where the user can enter
commands; $finish exits the simulator back to
the operating system.
63
64. Simulation - $scope, $showscope
$scope(hierarchy_name) sets the current
hierarchical scope to hierarchy_name.
$showscopes(n) lists all modules, tasks and
block names in (and below, if n is set to 1) the
current scope.
64
65. Simulation - $random
$random generates a random integer every
time it is called. If the sequence is to be
repeatable, the first time one invokes random
giving it a numerical argument (a seed).
Otherwise the seed is derived from the
computer clock.
65
66. Simulation - $dumpfile, $dumpvar, $dumpon,
$dumpoff, $dumpall
These can dump variable changes to a simulation viewer like Debussy. The
dump files are capable of dumping all the variables in a simulation. This is
convenient for debugging, but can be very slow
語法
1) $dumpfile("filename.vcd")
2) $dumpvar dumps all variables in the design.
3) $dumpvar(1, top) dumps all the variables in module top and below, but not
modules instantiated in top.
4) $dumpvar(2, top) dumps all the variables in module top and 1 level below.
5) $dumpvar(n, top) dumps all the variables in module top and n-1 levels
below.
6) $dumpvar(0, top) dumps all the variables in module top and all level below.
7) $dumpon initiates the dump.
8) $dumpoff stop dumping.
66
67. Simulation - $fopen, $fdisplay, $fstrobe $fmonitor and
$fwrite
These commands write more selectively to files.
$fopen opens an output file and gives the open file a handle for use by the other commands.
$fclose closes the file and lets other programs access it.
$fdisplay and $fwrite write formatted data to a file whenever they are executed. They are the
same except $fdisplay inserts a new line after every execution and $write does not.
$strobe also writes to a file when executed, but it waits until all other operations in the time
step are complete before writing. Thus initial #1 a=1; b=0; $fstrobe(hand1, a,b); b=1; will
write write 1 1 for a and b.
$monitor writes to a file whenever any of its arguments changes.
語法
1) handle1=$fopen("filenam1.suffix")
2) handle2=$fopen("filenam2.suffix")
3) $fstrobe(handle1, format, variable list) //strobe data into filenam1.suffix
4) $fdisplay(handle2, format, variable list) //write data into filenam2.suffix
5) $fwrite(handle2, format, variable list) //write data into filenam2.suffix all on one line. Put in the
format string where a new line is desired
67