Bai 4 Module Va Port
-
Upload
constantra -
Category
Documents
-
view
216 -
download
0
description
Transcript of Bai 4 Module Va Port
1
Baøi 4: Module vaø port
Thieát keá soá vôùi Verilog HDL
Ñoái töôïng nghieân cöùu
- Nhaän daïng caùc thaønh phaàn cuûa ñònh nghóa module, chaúng haïn nhö teân module, danh saùch port, thoâng soá, khai baùo bieán, phaùt bieåu luoàng döõ lieäu, phaùt bieåu haønh vi, theå hieän cuûa module vaø task hoaëc haøm (function).
- Caùch thöùc ñònh nghóa danh saùch port vaø khai baùo.
2
Ñoái töôïng nghieân cöùu (tieáp theo)
- Moâ taû qui luaät keát noái port trong theå hieän cuûa module.
- Caùch thöùc keát noái port vôùi tín hieäu beân ngoaøi, theo danh saùch thöù töï vaø theo teân.
- Giaûi thích vieäc tham chieáu teân theo heä thoáng thöù baäc cuûa caùc ñònh danh.
Module
Ta phaân tích chi tieát caùc thaønh phaàn beân trong module. Module bao goàm nhieàu phaàn rieâng bieät, xem hình ôû slide keá tieáp.
Ñònh nghóa module luoân luoân baét ñaàu baèng töø khoùa module. Teân module, danh saùch port, khai baùo port, thoâng soá (parameter) phaûi hieän dieän tröôùc tieân trong ñònh nghóa module. Danh saùch port vaø khai baùo port chæ hieän dieän khi module coù port töông taùc vôùi moâi tröôøng beân ngoaøi.
3
Module name, port list, port declarations (if port present),parameters (optional)
endmodule statement
Declaration of wire(s), reg(s) and other variables
Data flow statements(assign)
Instantation of lowerlevel modules
always and initialblocks (behavioralstatements)
tasks and functions
Module (tieáp theo)
5 thaønh phaàn trong module laø:
- caùc khai baùo bieán
- caùc phaùt bieåu luoàng döõ lieäu.
- theå hieän cuûa caùc module thaáp hôn.
- caùc khoái haønh vi.
- task hoaëc function.
Caùc thaønh phaàn naøy coù theå ôû baát kyø nôi naøo trong module vaø khoâng caàn thöù töï. Phaùt bieåu endmodule laø phaùt bieåu sau cuøng trong ñònh nghóa module.
4
Module (tieáp theo)
Moïi thaønh phaàn (ngoaïi tröø module, teân module vaø endmodule) laø tuøy choïn, coù theå troän laãn vaø töông thích theo yeâu caàu thieát keá. Verilog cho pheùp nhieàu module ñöôïc ñònh nghóa trong moät taäp tin duy nhaát vaø khoâng caàn thöù töï.
Ñeå hieåu roõ hôn caùc thaønh phaàn, ta khaûo saùt thí duï maïch choát SR. Maïch naøy coù 2 ngoõ vaøo S vaø R, hai ngoõ ra Q vaø Qbar, thieát keá töø hai coång NAND.
Module (tieáp theo)
Maïch choát SR
NAND n1
NAND n2
Q
Qbar
Sbar(set)
Rbar(reset)
5
Module (tieáp theo)
// Teân module, danh saùch port. Khoái thieát
// keá.
module SR_latch(Q, Qbar, Sbar, Rbar);
// Khai baùo port.
output Q, Qbar;
input Sbar, Rbar;
// Theå hieän caùc module möùc thaáp hôn
nand n1(Q, Sbar, Qbar);
nand n2(Qbar, Rbar, Q);
endmodule
// Teân module vaø danh saùch port
module Top; // Khoái stimulus
// Khai baùo wire, reg vaø caùc bieán khaùc.
wire q, qbar;
reg set, reset;
// Theå hieän module möùc thaáp hôn
SR_latch l1(q, qbar, ~set, ~reset);
// Khoái haønh vi initial
initial
begin
$monitor($time, " set = %b, reset= %b, q= %b\n", set, reset, q);
6
set = 0;
reset = 0;
#5 reset = 1;
#5 reset = 0;
#5 set = 1;
#5 set = 0; reset = 1;
#5 set = 0; reset = 0;
#5 set = 1; reset = 0;
end
endmodule
Port
Port cung caáp giao dieän qua ñoù module coù theå truyeàn thoâng vôùi moâi tröôøng beân ngoaøi. Caùc thaønh phaàn beân trong module khoâng nhìn thaáy ñöôïc töø moâi tröôøng beân ngoaøi. Söï thay ñoåi caùc thaønh phaàn beân trong khoâng aûnh höôûng ñeán moâi tröôøng beân ngoaøi mieãn laø giao dieän khoâng bò thay ñoåi. Ñieàu naøy cung caáp tính linh hoaït maïnh cho ngöôøi thieát keá. Port cuõng coøn ñöôïc goïi laø caùc ñaàu cuoái (terminals)
7
Port (tieáp theo)
- Danh saùch port.
Danh saùch port laø tuøy choïn. Neáu module khoâng trao ñoåi baát kyø tín hieäu naøo vôùi beân ngoaøi, ta khoâng caàn danh saùch port.
Ta khaûo saùt moät maïch coäng 4-bit ñöôïc theå hieän beân trong module möùc ñænh Top. Giaûn ñoà caùc port ñöôïc cho ôû slide keá tieáp. Trong hình naøy, module Top laø module möùc ñænh, module fulladd4 ñöôïc theå hieän beân döôùi Top.
Port (tieáp theo)
fulladd4 laáy tín hieäu ngoõ vaøo treân caùc port a, b vaø c_in, taïo ra ngoõ ra treân caùc port sum, c_out. Module fulladd4 thöïc hieän pheùp coäng
Top
Full-adder(4-bit)fulladd4
a
b
c_in
sum
c_out
8
Port (tieáp theo)
Module Top laø module möùc ñænh trong moâ phoûng, khoâng caàn truyeàn tín hieäu ñeán hoaëc nhaän tín hieäu töø moâi tröôøng. Module naøy khoâng caàn danh saùch port. Teân module, danh saùch port cuûa caû hai khai baùo module trong Verilog:
module fulladd4(sum, c_out, a, b, c_in);
module Top;
Port (tieáp theo)
- Khai baùo port.
Töø khoùa Loaïi port
input port ngoõ vaøo
output port ngoõ ra
inout port hai chieàu
Löu yù, moïi khai baùo port ñeàu hoaøn toaøn ñöôïc khai baùo döôùi daïng wire. Nhö vaäy, neáu moät port ñöôïc döï ñònh laø wire, port naøy cuõng ñöôïc khai baùo döôùi daïng input, output hoaëc inout. Caùc port input vaø inout thöôøng ñöôïc khai baùo döôùi daïng wire.
9
Port (tieáp theo)
Khai baùo port cho fulladd4
module fulladd4(sum, c_out, a, b, c_in);
output [3:0] sum;
output c_out;
input [3:0] a, b;
input c_in;
. . . . .
endmodule
Port (tieáp theo)
Tuy nhieân neáu port output löu giöõ giaù trò cuûa port, port phaûi ñöôïc khai baùo döôùi daïng reg. Thí duï, trong ñònh nghóa flipflop D, ta muoán ngoõ ra q duy trì giaù trò cho ñeán caïnh xung clock keá tieáp.
Port input vaø inout khoâng theå ñöôïc khai baùo döôùi daïng reg do caùc bieán reg löu giöõ giaù trò coøn port input seõ khoâng löu giöõ giaù trò maø chæ ñôn thuaàn phaûn aûnh söï thay ñoåi cuûa tín hieäu beân ngoaøi maø port naøy keát noái ñeán.
10
Port (tieáp theo)
Khai baùo port cho flipflop D
module DFF(q, d, clk, reset);
output q;
reg q;
input d, clk, reset;
. . . .
endmodule
Port (tieáp theo)
- Qui luaät keát noái port.
Ta coù theå hình dung moät port khi bao goàm hai ñôn vò:, moät ñôn vò ôû beân trong ñôn vò khaùc vaø ñôn vò khaùc naøy ôû beân ngoaøi ñôn vò ôû treân. Hai ñôn vò naøy ñöôïc keát noái. Coù nhieàu qui luaät chi phoái caùc keát noái port khi caùc module ñöôïc theå hieän beân trong module khaùc. Trình moâ phoûng port seõ phaøn naøn neáu ta vi phaïm caùc qui luaät keát noái port. Caùc qui luaät ñöôïc toùm taét nhö sau:
11
Port (tieáp theo)
Input: beân trong, port input phaûi luoân luoân coù kieåu net. Beân ngoaøi, input coù theå ñöôïc keát noái ñeán moät bieán laø reg hoaëc net.
net
net
reg or net
input
net
output
reg or netnet
Port (tieáp theo)
Output: beân trong, output coù theå coù kieåu reg hoaëc net. Beân ngoaøi, output phaûi luoân luoân ñöôïc noái vôùi net, khoâng theå ñöôïc noái vôùi reg.
Inout: beân trong, port inout phaûi luoân luoân coù kieåu net. Beân ngoaøi, port inout phaûi luoân luoân ñöôïc noái vôùi net.
Töông thích ñoä roäng: Vieäc noái caùc muïc beân trong vaø beân ngoaøi coù kích thöôùc khaùc nhau laø hôïp leä khi thöïc hieän keát noái port lieân module (keøm caûnh baùo).
12
Port (tieáp theo)
Port khoâng keát noái: Verilog cho pheùp port duy trì khoâng keát noái. Moät vaøi port ngoõ ra coù theå chæ ñôn thuaàn duøng ñeå gôõ roái. Ta coù theå ñaët moät port ôû tình traïng khoâng keát noái baèng vieäc theå hieän module nhö sau:
fulladd4 fa0(SUM, , A, B, C_IN);
// ngoõ ra c_out khoâng keát noái.
Port (tieáp theo)
Thí duï keát noái port khoâng hôïp leä.
module Top;
reg [3:0] A, B;
reg C_IN;
reg [3:0] SUM;
wire C_OUT;
fulladd4 fa0(SUM, C_OUT, A, B, C_IN);
// sai do port output sum trong module fulladd4 ñöôïc noái ñeán bieán register SUM trong module Top. SUM phaûi khai baùo laø moät net (wire).
13
Port (tieáp theo)
- Keát noái port vôùi tín hieäu ngoaøi.
Coù hai phöông phaùp thöïc hieän keát noái giöõa caùc tín hieäu ñöôïc chæ roõ trong theå hieän module vaø caùc port trong ñònh nghóa module. Hai phöông phaùp naøy khoâng ñöôïc troän laãn nhau.
Keát noái theo danh saùch thöù töï.
Keát noái theo teân.
Port (tieáp theo)
Thí duï: Keát noái theo danh saùch thöù töï.
module Top;
reg [3:0] A, B;
reg C_IN;
wire [3:0] SUM;
wire C_OUT;
fulladd4 fa_ordered(SUM, C_OUT, A, B, C_IN); // theå hieän fulladd4.
. . .
<stimulus>
endmodule
14
Port (tieáp theo)
module fulladd4(sum, c_out, a, b, c_in);
output sum [3:0] sum;
output c_out;
input [3:0] a, b;
input c_in;
. . .
<module internals>
. . .
endmodule
Port (tieáp theo)
Thí duï: Keát noái theo teân.
// Theå hieän module fa_byname vaø keát noái
// caùc tín hieäu ñeán caùc port theo teân.
fulladd4 fa_byname(.c_out(C_OUT), .sum(SUM), .b(B), .c_in(C_IN), .a(A));
// Port khoâng keát noái boû qua.
fulladd4 fa_byname(.sum(SUM), .b(B), .c_in(C_IN), .a(A));
15
Teân theo heä thoáng thöù baäc
Moãi theå hieän module, tín hieäu vaø bieán ñöôïc xaùc ñònh baèng ñònh danh. Moãi ñònh danh cuï theå coù moät vò trí duy nhaát trong heä thoáng thöù baäc.
Tham chieáu teân theo heä thoáng thöù baäc cho pheùp ta bieåu thò moãi ñònh danh trong thöù baäc thieát keá baèng teân duy nhaát. Teân theo heä thoáng thöù baäc laø danh saùch caùc ñònh danh taùch bieät baèng daáu chaám cho moãi möùc cuûa thöù baäc thieát keá.
Teân theo heä thoáng thöù baäc (tieáp theo)
Moät ñònh danh coù theå ñöôïc chæ ra töø vò trí baát kyø trong thieát keá baèng caùch ñôn thuaàn chæ roõ teân ñaày ñuû theo heä thoáng thöù baäc cuûa ñònh danh naøy.
Module möùc ñænh ñöôïc goïi laø root do khoâng ñöôïc theå hieän ôû baát kyø nôi naøo. Ñeå gaùn teân duy nhaát cho moät ñònh danh, ta baét ñaàu töø module möùc ñænh vaø theo doõi ñöôøng daãn theo thöù baäc thieát keá ñeán ñònh danh mong muoán.
Ta haõy khaûo saùt thí duï moâ phoûng maïch choát SR ñaõ bieát.
16
Teân theo heä thoáng thöù baäc (tieáp theo)
Vôùi moâ phoûng naøy, stimulus laø module möùc ñænh, ñaây laø module root.
stimulus(Root level)
m1(SR latch)
n1(nand)
n2(nand)
Q, QbarS, R(signals)
q, qbarset, reset(variables)
Teân theo heä thoáng thöù baäc (tieáp theo)
Caùc ñònh danh ñònh nghóa trong module naøy laø q, qbar, set, reset. Module root theå hieän m1, ñaây laø module cuûa maïch choát SR. Module m1 theå hieän n1 vaø n2, laø caùc coång NAND. Q, Qbar, S, R laø caùc tín hieäu port trong theå hieän m1. Vieäc tham chieáu teân theo heä thoáng thöù baäc seõ gaùn teân duy nhaát cho moãi ñònh danh.
17
Teân theo heä thoáng thöù baäc (tieáp theo)
Ñeå gaùn teân theo heä thoáng thöù baäc, ta söû duïng teân module cuûa module root vaø caùc teân theå hieän cho taát caû theå hieän module beân döôùi module root.
Löu yù daáu chaám cho moãi möùc thöù baäc töø module root ñeán ñònh danh mong muoán.
Vôùi thí duï treân ta coù:
Teân theo heä thoáng thöù baäc (tieáp theo)
stimulus
stimulus.q stimulus.m1.Q
stimulus.qbar stimulus.m1.Qbar
stimulus.set stimulus.m1.S
stimulus.reset stimulus.m1.R
stimulus.m1
stimulus.n1
stimulus.n2