Giao Trinh Verilog HDL Full 3053
Giao Trinh Verilog HDL Full 3053
Giao Trinh Verilog HDL Full 3053
Gio trnh
NGN NG M T PHN CNG
VERILOG
Bin son: TS. V c Lung
ThS. Lm c Khi
Ks. Phan nh Duy
2012
Li ni u
Ngy nay, khi mch thit k vi hng triu cng logic c tch hp trong mt con Chip th
vic thit k mch v i dy kt ni bng tay tr nn bt kh thi, chnh t l do mt khi nim
ngn ng c mc tru tng cao dng m t thit k phn cng c ra i, chnh
l Verilog. Cng vi s ra i ca ngn ng m t phn cng Verilog l hng lot cc cng c
EDA (Electronic Design Automation) v CAD (Computer Aided Design) gip cho nhng
k s thit k phn cng to nn nhng con Chip c tch hp rt cao, tc siu vit v
chc nng a dng.
Gio trnh Ngn ng m t phn cng Verilog nhm gip sinh vin trang b kin thc
v thit k vi mch. Gio trnh tp trung vo mng thit k cc mch s vi mch t hp v
mch tun t. Gio trnh cng gii thiu v cc bc cn thc hin trong qu trnh thit k vi
mch t vic m t thit k, kim tra, phn tch cho n tng hp phn cng ca thit k.
Gio trnh Ngn ng m t phn cng Verilog dng cho sinh vin chuyn ngnh K thut
my tnh v sinh vin cc khi in t. tip nhn kin thc d dng, sinh vin cn trang b
trc kin thc v thit k s v h thng s.
Gio trnh ny c bin dch v tng hp t kinh nghim nghin cu ging dy ca tc gi
v ba ngun ti liu chnh:
IEEE Standard for Verilog Hardware Description Language, 2006;
Verilog Digital System Design, Second Edition, McGraw-Hill;
The Complete Verilog Book, Vivek Sagdeo, Sun Micro System, Inc.
Nhm cung cp mt lung kin thc mch lc, gio trnh c chia ra lm 9 chng:
Chng 5: Gii thiu cu trc ca mt thit k, phng thc s dng thit k con.
Chng 6: Trnh by phng php thit k s dng m hnh cu trc, trong phng
[Type text]
Page 2
Chng 7: Trnh by phng thc thit k s dng m hnh RTL bi php gn ni tip
v m hnh hnh vi s dng ngn ng c tnh tru tng cao tng t nh ngn ng lp
trnh. Phn thit k my trng thi s dng m hnh hnh vi cng c nu ra trong
chng ny.
Chng 9: Gii thiu cc phng php kim tra chc nng ca thit k.
Do thi gian cng nh khi lng trnh by gio trnh khng cho php tc gi i su hn
v mi kha cnh ca thit k vi mch nh phn tch nh thi, tng hp phn cng, ... c
c nhng kin thc ny, c gi c th tham kho trong cc ti liu tham kho m gio trnh
ny cung cp.
Mc d nhm tc gi c gng bin son k lng tuy nhin cng kh trnh khi
nhng thiu st. Nhm tc gi mong nhn c nhng ng gp mang tnh xy dng t qu
c gi nhm chnh sa gio trnh hon thin hn.
Nhm tc gi
[Type text]
Page 3
Contents
Li ni u.......................................................................................................................................2
1 Chng 1. Dn nhp thit k h thng s vi Verilog..........................................................10
1.1
Qui trnh thit k s.........................................................................................................10
1.1.1
Dn nhp thit k.....................................................................................................12
1.1.2
Testbench trong Verilog...........................................................................................13
1.1.3
nh gi thit k......................................................................................................13
1.1.3.1
1.1.3.2
1.1.3.3
1.1.4
1.1.4.1
1.1.4.2
1.1.4.3
1.1.4.4
1.1.4.5
M phng.......................................................................................................................13
K thut chn kim tra (assertion)..................................................................................15
Kim tra thng thng...................................................................................................16
1.1.5
M phng sau khi tng hp thit k........................................................................19
1.1.6
Phn tch thi gian...................................................................................................20
1.1.7
To linh kin phn cng..........................................................................................20
1.2
Ngn ng m t phn cng Verilog (VerilogHDL)................................................................20
1.2.1
Qu trnh pht trin Verilog.....................................................................................20
1.2.2
Nhng c tnh ca Verilog.....................................................................................21
1.2.2.1
1.2.2.2
1.2.2.3
1.2.2.4
1.2.2.5
1.2.2.6
1.2.2.7
Mc chuyn mch.....................................................................................................21
Mc cng..................................................................................................................21
tr hon gia pin n pin...........................................................................................22
M t Bus.......................................................................................................................22
Mc hnh vi..............................................................................................................22
Nhng tin ch h thng.................................................................................................22
PLI.................................................................................................................................22
1.2.3
S lc v Verilog...................................................................................................22
1.3
Tng kt..........................................................................................................................23
1.4
Bi tp.............................................................................................................................23
2 Chng 2. Qui c v t kha...........................................................................................25
2.1
Khong trng...................................................................................................................25
2.2
Ch thch.........................................................................................................................25
2.3
Ton t............................................................................................................................25
2.4
S hc..............................................................................................................................25
2.4.1
Hng s nguyn.......................................................................................................26
2.4.2
Hng s thc............................................................................................................29
2.4.3
S o......................................................................................................................30
2.5
Chui...............................................................................................................................30
2.5.1.1
2.5.1.2
2.5.1.3
2.6
nh danh, t kha v tn h thng................................................................................31
2.6.1
nh danh vi k t .............................................................................................32
2.6.2
Tc v h thng v hm h thng............................................................................32
2.7
Bi tp.............................................................................................................................33
3 Chng 3. Loi d liu trong Verilog...................................................................................70
3.1
Khi qut.........................................................................................................................70
3.2
Nhng h thng gi tr....................................................................................................70
[Type text]
Page 4
3.4.4
Tri reg net.................................................................................................................76
3.4.5
Tri 0 v Tri 1 Nets....................................................................................................77
3.4.6
Supply0 v Supply1 Nets.........................................................................................77
3.4.7
Thi gian tr hon trn net.......................................................................................78
3.5
Khai bo loi d liu bin - reg.......................................................................................79
3.6
Khai bo port...................................................................................................................79
3.6.1
Gii thiu.................................................................................................................79
3.6.2
input.........................................................................................................................79
3.6.3
output.......................................................................................................................80
3.6.4
inout.........................................................................................................................80
3.7
Khai bo mng v phn t nh mt v hai chiu............................................................80
3.7.1
Gii thiu.................................................................................................................80
3.7.2
Mng net..................................................................................................................81
3.7.3
Mng thanh ghi........................................................................................................81
3.7.4
Mng phn t nh....................................................................................................82
3.8
Khai bo loi d liu bin...............................................................................................83
3.8.1
Gii thiu.................................................................................................................83
3.8.2
Integer......................................................................................................................83
3.8.3
Time.........................................................................................................................83
3.8.4
S thc (real) v thi gian thc (realtime)..............................................................84
3.9
Khai bo tham s............................................................................................................85
3.9.1
Gii thiu.................................................................................................................85
3.9.2
Tham s module (module parameter).......................................................................85
3.9.2.1 Parameter.......................................................................................................................85
3.9.2.1.1 Gii thiu.................................................................................................................85
3.9.2.1.2 Thay i gi tr ca tham s khai bo parameter......................................................86
3.9.2.1.2.1 Pht biu defparam............................................................................................86
3.9.2.1.2.2 Php gn gi tr tham s khi gi instance ca module.......................................87
3.9.2.1.3 S ph thuc tham s...............................................................................................92
3.9.2.2 Tham s cc b (local parameter).................................................................................92
3.9.3
Tham s c t (specify parameter)........................................................................92
3.10 Bi tp.............................................................................................................................94
4 Chng 4. Ton t, Ton hng v Biu thc.....................................................................95
4.1
Biu thc gi tr hng s.................................................................................................95
4.2
Ton t............................................................................................................................96
4.2.1
Ton t vi ton hng s thc.................................................................................96
4.2.2
Ton t u tin.........................................................................................................97
4.2.3
S dng s nguyn trong biu thc.........................................................................98
4.2.4
Th t tnh ton trong biu thc..............................................................................99
4.2.5
Ton t s hc (+, -, *, /, %, **, +, -)......................................................................99
4.2.6
Biu thc s hc vi tp thanh ghi (regs) v s nguyn (integer)........................101
4.2.7
Ton t quan h (>, <, >=, <=)..............................................................................102
4.2.8
Ton t so snh bng (==, !=, ===, !==)...............................................................103
4.2.9
Ton t logic (&&, ||, !).........................................................................................104
[Type text]
Page 5
4.2.14.1
4.2.14.2
4.3
Ton hng......................................................................................................................111
4.3.1
Vector bit-select v part-select addressing.............................................................111
4.3.2
a ch mng v phn t nh.................................................................................112
4.3.3
Chui......................................................................................................................114
4.3.3.1
4.3.3.2
4.3.3.3
Ton t chui................................................................................................................114
Gi tr chui m v vn tim n.............................................................................114
Chui rng...................................................................................................................115
4.4
Biu thc tr hon thi gian ti thiu, trung bnh, v ti a..........................................115
4.5
Biu thc di bit.......................................................................................................117
4.5.1
Qui lut cho biu thc di bit...........................................................................117
4.5.2
V d minh ha vn v biu thc di bit......................................................118
4.5.3
V d minh ha v biu thc t xc nh...............................................................119
4.6
Biu thc c du...........................................................................................................120
4.6.1
Qui nh cho nhng loi biu thc........................................................................120
4.6.2
Nhng bc nh gi mt biu thc......................................................................121
4.6.3
Nhng bc nh gi mt php gn......................................................................122
4.6.4
Tnh ton nhng biu thc ca hai s c du X v Z............................................122
4.7
Nhng php gn v php rt gn..................................................................................122
4.8
Bi tp...........................................................................................................................123
5 Chng 5. Cu trc phn cp v module.............................................................................124
5.1
Cu trc phn cp..........................................................................................................124
5.2
Module..........................................................................................................................124
5.2.1
Khai bo module....................................................................................................124
5.2.2
Module mc cao nht............................................................................................126
5.2.3
Gi v gn c tnh mt module (instantiate)........................................................126
5.2.4
Khai bo port.........................................................................................................129
5.2.4.1
5.2.4.2
5.2.4.3
5.2.4.4
5.2.4.5
5.2.4.6
5.2.4.7
5.2.4.8
5.2.4.9
5.2.4.10
5.2.4.11
nh ngha port............................................................................................................129
Lit k port...................................................................................................................130
Khai bo port trong thn module..................................................................................130
Khai bo port u module............................................................................................131
Kt ni cc port ca module c gi bng danh sch th t.......................................132
Kt ni cc port ca module c gi bng tn............................................................133
S thc trong kt ni port.............................................................................................134
Kt ni nhng port khng tng t nhau.....................................................................135
Nhng qui nh khi kt ni port...................................................................................135
Loi net to ra t vic kt ni port khng tng t nhau..........................................136
Kt ni nhng gi tr c du thng qua (port)..........................................................137
5.3
Bi tp...........................................................................................................................137
Chng 6. M hnh thit k cu trc (Structural model).....................................................138
6.1
Gii thiu......................................................................................................................138
6.2
Nhng linh kin c bn.................................................................................................138
6.2.1
Cng and, nand, or, nor, xor, xnor.........................................................................138
6.2.2
Cng buf v not.....................................................................................................139
6.2.3
Cng ba trng thi bufif1, bufif0, notif1, v notif0...............................................140
6.2.4
Cng tc MOS.......................................................................................................141
6.2.5
Cng tc truyn hai chiu......................................................................................142
[Type text]
Page 6
Cng tc CMOS.....................................................................................................143
Ngun pullup v pulldown....................................................................................144
M hnh mnh logic..........................................................................................145
mnh v gi tr ca nhng tn hiu kt hp.....................................................147
6.2.9.1
6.2.9.2
6.2.9.3
6.2.10
6.2.11
6.2.12
6.2.12.1
6.2.12.2
6.2.12.3
6.2.13
6.2.14
6.2.15
6.2.15.1
6.2.15.2
6.3
Nhng phn t c bn ngi dng t nh ngha (UDP).............................................164
6.3.1
nh ngha phn t c bn UDP............................................................................164
6.3.1.1
6.3.1.2
6.3.1.3
6.3.1.4
6.3.1.5
6.3.1.6
Tiu UDP................................................................................................................166
Khai bo cng (port) UDP...........................................................................................167
Khai bo khi to UDP tun t....................................................................................167
Bng khai bo UDP......................................................................................................167
Gi tr Z trong UDP.....................................................................................................168
Tng hp cc k hiu...................................................................................................168
6.3.2
UDP t hp............................................................................................................169
6.3.3
UDP tun t tch cc mc.....................................................................................170
6.3.4
UDP tun t tch cc cnh.....................................................................................171
6.3.5
Mch hn hp gia UDP mch tch cc mc v UDP tch cc cnh...................172
6.3.6
Gi s dng UDP...................................................................................................173
6.4
M t mch t hp v mch tun t s dng m hnh cu trc....................................174
6.4.1
M t mch t hp.................................................................................................174
6.4.2
M t mch tun t................................................................................................177
6.5
Bi tp...........................................................................................................................179
7 Chng 7. M hnh thit k hnh vi (Behavioral model)....................................................220
7.1
Khi qut.......................................................................................................................220
7.2
Php gn ni tip hay php gn lin tc - m hnh thit k RTL (continuous assignment)
220
7.2.1
Gii thiu...............................................................................................................220
7.2.2
Php gn ni tip khi khai bo net.........................................................................220
7.2.3
Pht biu php gn ni tip tng minh assign.....................................................221
7.2.4
To tr hon (delay) cho php gn....................................................................222
7.2.5
mnh php gn.................................................................................................223
7.3
Php gn qui trnh - m hnh thit k mc thut ton (procedural assignment)...224
7.3.1
Php gn khai bo bin..........................................................................................227
7.3.2
Php gn qui trnh kn (blocking assignment) '='..................................................228
7.3.2.1
7.3.3
7.3.3.1
7.4
Pht biu c iu kin...................................................................................................234
7.4.1
Cu trc if-else-if...................................................................................................235
7.5
Pht biu Case...............................................................................................................237
[Type text]
Page 7
end.........................................................................................................................287
9.3
Kim tra thit k............................................................................................................288
9.4
K thut chn (assertion) dng kim tra thit k......................................................289
9.4.1
Li ch ca k thut chn kim tra........................................................................289
9.4.2
Th vin thit k m (OVL)..................................................................................290
9.4.3
S dng k thut chn gim st.............................................................................291
[Type text]
Page 8
Bi tp...........................................................................................................................293
[Type text]
Page 9
Page 10
Page 11
1.1.1
Dn nhp thit k
1.1.2
Mt h thng c thit k dng Verilog phi c m phng v kim tra xem thit k
[Type text]
Page 12
1.1.3
nh gi thit k
1.1.3.1 M phng
Chy m phng dng trong vic nh gi thit k, c thc hin trc khi thit k c
tng hp. Bc chy m phng ny c hiu nh m phng mc hnh vi, mc RTL
hay tin tng hp. mc RTL, mt thit k bao gm xung thi gian clock nhng khng bao
gm tr hon thi gian trn cng v dy kt ni (wire). Chy m phng mc ny s chnh
xc theo xung clock. Thi gian ca vic chy m phng mc RTL l theo tn hiu xung
clock, khng quan tm n nhng vn nh: nguy him tim n c th khin thit k b li
(hazards, glitch), hin tng chy ua khng kim sot gia nhng tn hiu (race conditions),
nhng vi phm v thi gian setup v hold ca tn hiu ng vo, v nhng vn lin quan n
nh thi khc. u im ca vic m phng ny l tc chy m phng nhanh so vi chy m
phng mc cng hoc mc transistor.
Chy m phng cho mt thit k i hi d liu kim tra. Thng thng trong mi trng
m phng Verilog s cung cp nhiu phng php khc nhau a d liu kim tra ny
vo thit k kim tra. D liu kim tra c th c to ra bng ha, s dng nhng
cng c son tho dng sng, hoc bng testbench. Hnh 1.2 m t hai cch khc nhau nh
ngha d liu kim tra ng vo ca mt cng c m phng. Nhng ng ra ca cng c m
phng l nhng dng sng ng ra (c th quan st trc quan).
[Type text]
Page 13
Hnh 1.2. Hai cch khc nhau nh ngha d liu kim tra ng vo
chy m phng vi Verilog testbench, trong testbench s gi h thng thit k ra
kim tra, lc ny h thng thit k c xem nh l mt phn ca testbench, testbench s
cung cp d liu kim tra n ng vo ca h thng thit k. Hnh 1.3 m t mt on code ca
mt mch m, testbench ca n, cng nh kt qu chy m phng ca n di dng sng ng
ra. Quan st hnh ta thy vic chy m phng s nh gi chc nng ca mch m. Vi mi
xung clock th ng ra b m s tng ln 1. Ch rng, theo biu thi gian th ng ra b
m thay i ti cnh ln xung clock v khng c thi gian tr hon do cng cng nh tr hon
trn ng truyn. Kt qu chy m phng cho thy chc nng ca mch m l chnh xc m
khng cn quan tm n tn s xung clock.
Hin nhin, nhng linh kin phn cng thc s s c p ng khc nhau. Da trn nh
thi v thi gian tr hon ca nhng khi c s dng, thi gian t cnh ln xung clock
n ng ra ca b m s c tr hon khc khng. Hn na, nu tn s xung clock c cp
vo mch thc s qu nhanh so vi tc truyn tn hiu bn trong cc cng v transistor ca
thit k th ng ra ca thit k s khng th bit c.
Vic m phng ny khng cung cp chi tit v cc vn nh thi ca h thng thit k
c m phng. Do , nhng vn tim n v nh thi ca phn cng do tr hon trn cng
s khng th pht hin c. y l vn in hnh ca qu trnh m phng tin tng hp
hoc m phng mc hnh vi. iu bit c trong Hnh 1.3 l b m ca ta m s
nh phn. Thit k hot ng nhanh chm th no, hot ng c tn s no ch c th bit
c bng vic kim tra thit k sau tng hp.
[Type text]
Page 14
[Type text]
Page 15
1.1.4
[Type text]
Page 16
1.1.4.3 Ti u logic
Bc k tip ca qu trnh tng hp, sau khi mt thit k c chuyn i sang mt
chui nhng biu thc Boolean, bc ti u logic c thc hin. Bc ny nhm mc ch
lm gim nhng biu thc vi ng vo khng i, loi b nhng biu thc lp li, ti thiu hai
mc, ti thiu nhiu mc. y l qu trnh tnh ton rt hao tn thi gian v cng sc, mt s
cng c cho php ngi thit k quyt nh mc ti u. Kt qu ng ra ca bc ny cng
di dng nhng biu thc Boolean, m t logic di dng bng, hoc netlist gm nhng cng
c bn.
[Type text]
Page 17
1.1.4.5 Sp xp cell v i dy kt ni
Bc sp xp v i dy kt ni s quyt nh vic t v tr ca cc linh kin trn thit b
phn cng mc ch. Vic kt ni cc ng vo v ng ra ca nhng linh kin ny dng h
thng dy lin kt v vng chuyn mch trn thit b phn cng mc ch, c quyt nh
bi bc sp xp cell v i dy kt ni ny. Kt qu ng ra ca bc ny c a ti thit b
phn cng mc ch, nh np ln FPLD, hay dng sn xut ASIC.
Mt v d minh ha v qu trnh tng hp c ch ra trn Hnh 1.5. Trong hnh ny, mch
m c dng chy m phng trong Hnh 1 . 3 c tng hp. Ngoi vic m t phn
cng thit k dng Verilog, cng c tng hp i hi nhng thng tin m t thit b phn cng
mc ch tin hnh qu trnh tng hp ca mnh. Kt qu ng ra ca cng c tng hp l
danh sch cc cng, cc flip-flop c sn trong thit b phn cng ch v h thng dy kt ni
gia chng. Hnh 1.5 cng ch ra mt kt qu ng ra mang tnh trc quan c to ra t ng
bng cng c tng hp ca Altera Quartus II.
[Type text]
Page 18
1.1.5
Sau khi qu trnh tng hp hon thnh, cng c tng hp s to ra mt netlist hon chnh
cha nhng linh kin ca thit b phn cng ch v cc gi tr nh thi ca n. Nhng thng
tin chi tit v cc cng c dng hin thc thit k cng c m t trong netlist ny.
Netlist ny cng bao gm nhng thng tin v tr hon trn ng dy v nhng tc ng ca
ti ln cc cng dng trong qu trnh hu tng hp. C nhiu nh dng netlist ng ra c th
c to ra bao gm c nh dng Verilog. Mt netlist nh vy c th c dng m phng,
v m phng ny c gi l m phng hu tng hp. Nhng vn v nh thi, v tn s
xung clock, v hin tng chy ua khng kim sot, nhng nguy him tim n ca thit k ch
c th kim tra bng m phng hu tng hp thc hin sau khi thit k c tng hp. Nh trn
Hnh 1.1, ta c th s dng d liu kim tra m dng cho qu trnh m phng tin tng hp
dng cho qu trnh m phng hu tng hp.
Do tr hon trn ng dy v cc cng, p ng ca thit k sau khi chy m phng
hu tng hp s khc vi p ng ca thit k m ngi thit k mong mun. Trong
trng hp ny, ngi thit k phi sa li thit k v c gng trnh nhng sai st v nh thi
v hin tng chy ua gia nhng tn hiu khng th kim sot.
[Type text]
Page 19
Quan st trn Hnh 1.1, bc phn tch thi gian l mt phn trong qu trnh bin dch, hoc
trong mt s cng c th bc phn tch thi gian ny c thc hin sau qu trnh bin dch.
Bc ny s to ra kh nng xu nht v tr hon, tc xung clock, tr hon t
cng ny n cng khc, cng nh thi gian cho vic thit lp v gi tn hiu.
Kt qu ca bc phn tch thi gian c th hin di dng bng hoc biu . Ngi
thit k s dng nhng thng tin ny xc nh tc xung clock, hay ni cch khc l xc
nh tc hot ng ca mch thit k.
1.1.7
Bc cui cng trong qui trnh thit k t ng da trn Verilog l to ra phn cng
thc s cho thit k. Bc ny c th to ra mt netlist dng sn xut ASIC, mt chng
trnh np vo FPLD, hay mt mch in cho mch IC.
1.2.1
Page 20
1.2.2
1.2.2.2 Mc cng
Nhng cng c bn vi nhng thng s c nh ngha trc s cung cp mt kh nng
thun tin trong vic th hin netlist v m phng mc cng. i vi vic m phng mc
cng vi mc ch chi tit v c bit, nhng linh kin cng c th c nh ngha
mc hnh vi. Verilog cng cung cp nhng cng c cho vic nh ngha nhng phn t
c bn vi nhng chc nng c bit. Mt h thng s logic 4 gi tr n gin (0,1,x,z) c s
dng trong Verilog th hin gi tr cho tn hiu. Tuy nhin, m hnh mc logic chnh xc
hn, nhng tn hiu Verilog gm 16 mc gi tr v mnh c thm vo 4 gi tr n gin
[Type text]
Page 21
1.2.2.4 M t Bus
Nhng tin ch v m hnh bus v thanh ghi cng c cung cp bi Verilog. i vi nhiu
cu trc bus khc nhau, Verilog h tr chc nng phn gii bus v wire vi h thng logic 4 gi
tr (0,1,x,z). Vi s kt hp gia chc nng bus logic v chc nng phn gii, n cho php m
hnh ha c hu ht cc loi bus. i vi vic m hnh ha thanh ghi, vic m t xung clock
mc cao v nhng cu trc iu khin nh thi, c th c s dng m t thanh ghi vi
nhng tn hiu xung clock v tn hiu reset khc nhau.
1.2.2.5 Mc hnh vi
Nhng khi qui trnh (procedural blocks) ca Verilog cho php m t thut ton ca
nhng cu trc phn cng. Nhng cu trc ny tng t vi ngn ng lp trnh phn mm
nhng c kh nng m t phn cng.
1.2.2.7 PLI
Cng c tng tc ngn ng lp trnh (PLI) ca Verilog cung cp mt mi trng cho
vic truy xut cu trc d liu Verilog, s dng mt th vin cha cc hm ca ngn ng C.
1.2.3
S lc v Verilog
Page 22
1.3 Tng kt
Phn ny cung cp mt ci nhn tng quan v nhng c ch, nhng cng c v
nhng qui trnh dng trong vic m t mt thit k t bc thit k n qu trnh hin thc phn
cng. Phn ny cng ni s lc v thng tin kin thc m ta s i su trong cc phn sau. Bn
cnh , n cng cung cp n ngi c lch s pht trin ca Verilog. Cng vi vic pht
trin chun Verilog HDL ny l s pht trin khng ngng ca cc cng ty nghin cu, xy
dng v hon thin cc cng c h tr i km, kt qu l to ra nhng cng c tt hn v nhng
mi trng thit k ng b hn.
1.4 Bi tp
1. Verilog l g ? Ti sao ta phi s dng ngn ng m t phn cng Verilog trong thit k
Chip?
2. Tm hiu mi trng thit k trn FPGA l QuartusII ca Altera v tm hiu mi trng
m phng, mi trng tng hp ca n. Hy lin tng, so snh mi trng thit k ny vi
mi trng m phng v tng hp c trnh by trong phn ny.
3. Nu s khc bit gia ngn ng m t phn cng ni chung (ngn ng Verilog HDL ni
ring) v ngn ng lp trnh ni chung (ngn ng C ni ring)?
4. Tm hiu s khc bit gia hai loi ngn ng m t phn cng Verilog HDL v
VHDL?
5. Qu trnh tng hp (synthesis) l g?
[Type text]
Page 23
[Type text]
Page 24
2 Chng 2.
Qui c v t kha
2.2 Ch thch
Ngn ng Verilog HDL c hai cch to ch thch:
Ch thch mt dng: bt u bng hai k t // cho n cui dng.
Ch thch mt khi: bt u bng hai k t /* v kt thc bng hai k t */.
Ch thch mt khi khng nn qu ri rm, khng c c s lng nhau gia nhng ch
thch khi, nhng c php lng ch thch 1 dng trong ch thch khi (trong ch thch khi th
hai k t // khng mang ngha g c bit c).
V d:
x = y && y; // y l ch thch mt dng
/* y l ch thch
nhiu dng */
/* y l /* ch thch */ khng hp l */
/* y l //ch thch hp l */
2.3 Ton t
Nhng ton t nh chui k t n, kp hay gm ba k t c dng trong nhng biu thc.
Trong phn tho lun v biu thc (Chng 4) ta s trnh by v cch s dng cc ton t trong
biu thc nh th no.
Nhng ton t n thng xut hin bn tri ca ton hng ca chng (--i). Nhng
ton t kp thng xut hin gia nhng ton hng ca chng (a & b). Ton t c iu kin
thng c hai ton t k t c phn bit bi ba ton hng ( (m>n) ? m : n ).
2.4 S hc
Hng s c m t nh l hng s nguyn hoc hng s thc.
V d 2.1
1.4E9
// s thc 1.4x10
-5d18
4b1011
8hEF
16o56
4bxxxx
4bzzzz
2.4.1
Hng s nguyn
+374
-374
// s bt phn
d, D : H thp phn
h, H : H lc phn
o, O : H bt phn
b, B : H nh phn
5EB
10o567
8 b 1001
c m t trong thnh phn th hai. Thnh phn s khng du ny c th theo sau ngay
thnh phn c s hoc c th theo sau thnh phn c s bi mt khong trng. Nhng k t
t a n f ca s thp lc phn c th l k t thng hoc k t hoa.
V d 2.3 Hng s c rng bit
4b1011
// s nh phn 4 bit
5 D 5
3B10x
12hx
16Hz
Nhng s thp phn n gin khng km theo rng bit v nh dng c s c xem nh
l nhng s nguyn c du.
Nhng s c m t bi nh dng c s c xem nh nhng s nguyn c du khi thnh
phn ch nh s (hoc S) c km thm vo, hoc n s c xem nh nhng s nguyn
khng du khi ch c thnh phn nh dng c s c s dng. Thnh phn ch nh s
c du s (hoc S) khng nh hng n mu bit c m t m n ch nh hng trong qu
trnh bin dch.
Ton t cng hay tr ng trc hng s rng l mt ton t n cng (+) hay tr (-), hai
ton t ny nu c t nm gia thnh phn nh dng c s v s l khng ng c php.
Nhng s m c biu din di dng b hai.
V d 2.4 S dng du vi hng s
6 d -7
// c php khng ng
-6 d 7
// s b 2 ca 7, tng ng vi (6d 7)
4 shf
-4 sd15
8sd?
// tng ng 8sbz
Cc gi tr s c bit x v z:
Mt s x dng biu din mt gi tr khng xc nh trong nhng hng s thp lc
[Type text] Page 27
// to ra xxx
n = h 4x;
// to ra 04x
p = h z5;
// to ra zz5
q = h 0z8;
// to ra 0z8
end
reg [15:0] e, f, g;
// e, f, g u c 16 bit
e = h4;
// to ra {13{1b0}, 3b100}
f = hx
// to ra {16{1hx}}
g = hz;
// to ra {16{1hz}}
2.4.2
Hng s thc
2.4.3
S o
S thc c th bin i sang s nguyn bng cch lm trn s thc n s nguyn gn nht
thay v ct xn s bit ca n. Bin i khng tng minh c th thc hin khi mt s thc c
gn n mt s nguyn. Nhng ci ui nn c lm trn khc 0.
V d:
Hai s thc 48.8 v 48.5 u tr thnh 49 khi c bin i sang s nguyn, v s 48.3
s tr thnh 48.
Bin i s thc -5.5 sang s nguyn s c -6, bin i s 5.5 sang s nguyn s c
6.
2.5 Chui
Mt chui l mt dy cc k t c nm trong hai du nhy kp() v c ghi trn mt
dng n. Nhng chui c dng nh l nhng ton hng trong biu thc v trong nhng
php gn c xem nh l nhng hng s nguyn khng du v c biu din bi mt
dy k t 8 bit ASCII. Mt k t ASCII biu din bng 8 bit.
2.5.1.2 X l chui
Chui c th c x l bng vic s dng cc ton t Verilog HDL. Gi tr m c x l
bi ton t l mt dy gi tr 8 bit ASCII. Cc ton t x l chui c th hin chi tit hn
trong phn 4.3.3.
K t to bi chui escape
\n
K t xung dng
\t
K t tab
\\
K t \
K t
\ddd
2.6.1
nh danh vi k t \
2.6.2
Tc v h thng v hm h thng
Du dollar ($) m u mt cu trc ngn ng s cho php pht trin nhng tc v h thng
v hm h thng do ngi dng nh ngha. Nhng cu trc h thng khng phi l ngn ng
thit k, m n mun ni n chc nng m phng. Mt tn theo sau du $ c bin dch
nh l mt tc v h thng hoc hm h thng.
Tc v h thng/hm h thng c th c nh ngha trong ba vi tr:
Mt tp hp chun nhng tc v h thng v hm h thng.
Nhng tc v h thng v hm h thng thm vo c nh ngha dng cho PLI
(Programming Language Interface).
Nhng tc v h thng v hm h thng thm vo c nh ngha bi phn mm thc
thi.
V d 2.11
$time
$display
$stop
$finish
$monitor
2.7 Bi tp
1. Nu tc dng v s khc bit gia hai hm h thng $monitor v $display khi s
dng hai hm h thng ny trong qu trnh m phng?
[Type text] Page 32
Lm sao c th c v ghi mt file d liu trong m t phn cng Verilog HDL (gi s
file cha ni dung b nh khi to)?
3 Chng 3.
128 loi trng thi: gm 4 trng thi v 64 mnh (8 cho mnh 0 v 8 cho
mnh 1) .
Gii thiu
Nhng loi d liu khc nhau trong Verilog c khai bo bng pht biu khai bo d liu.
Nhng pht biu ny xut hin trong nhng nh ngha module trc khi s dng v mt s
trong chng c th c khai bo bn trong nhng khi tun t c t tn. Thm vo ,
nhng loi gi tr c th phn bit vi nhng loi ca d liu khc, nhng c tnh phn
cng ca wires so vi registers cng c phn bit nh l nhng khai bo net so vi khai
bo reg trong Verilog.
T driving ngha l iu khin c dng trong nhng m t phn cng m t cch thc
mt gi tr c gn n mt phn t. Nets v regs l hai phn t d liu chnh trong
Verilog. Nets c iu khin mt cch ni tip t nhng php gn ni tip (continuous
assignments) hoc t nhng phn t cu trc nh module ports, gates, transistors hoc nhng
phn t c bn do ngi dng t nh ngha. Regs c iu khin mt cch cht ch t nhng
khi hnh vi (behavioural blocks). Nets thng thng c thc thi nh l wires trong phn
cng v regs th c th l wires hoc phn t tm hoc flip-flops (registers).
Nhng loi d liu khc nhau trong Verilog c khai bo gm nhng loi sau:
parameter: Loi ny l nhng biu thc gi tr hng s c phn tch sau qu trnh
bin dch v cho php modules c gn tham s.
input, output, inout: Nhng loi d liu ny nh ngha chiu v rng ca mt port.
net : y l loi d liu dng cho vic kt ni hoc wire (dy ni) trong phn cng vi s
phn tch khc nhau.
reg: y l loi d liu tru tng ging nh l mt thanh ghi (register) v c iu khin
theo hnh vi.
time: y l loi d liu lu tr khong thi gian nh tr hon v thi gian m phng.
integer: y l loi d liu s nguyn.
real: y l loi d liu floating point hay s thc
event: y l d liu ch ra rng mt c hiu c bt tch cc.
Gii thiu
3.4.2
tri/wire
triand/wand
trior/wor
tri0
trireg
tri1
Wire v Tri
Loi d liu wire l mt loi n gin kt ni gia hai linh kin. D liu wire dng cho
nhng net c iu khin bi mt cng linh kin n hay trong php gn ni tip (continuous
assignments). Trong V d 3.2 nhng khai bo 2-wire c to ra. Khai bo u tin m t wire
n (scalar wire) a1. Khai bo th hai m t mt mng (vector) b2 vi 3 bits. Bit trng s
[Type text] Page 73
2b00: m = 1b1;
2b01: n = 1b0;
2b10: p = 1b1;
endcase
[Type text] Page 74
3.4.3
Wired net
Wired Nets bao gm nhng loi d liu wor, wand, trior v triand. Chng c dng m
hnh gi tr logic ca net. Nhng wired net trn c bng s tht khc nhau phn gii nhng
xung t nu xy ra khi c nhiu cng linh kin cng iu khin mt net.
//out = b1 and b2
3.4.4
3.4.5
Net tri0 v tri1 dng m hnh nhng net vi linh kin in tr ko ln hoc ko xung.
Mt net tri0 s tng ng vi mt net c iu khin lin tc bi gi tr 0 vi mnh
pull. Mt net tri1 s tng ng vi mt net c iu khin lin tc bi gi tr 1 vi mnh
pull.
Khi khng c linh kin iu khin net tri0, gi tr ca n vn l 0 vi mnh pull. Khi
khng c linh kin iu khin net tri1, gi tr ca n vn l 1 vi mnh pull. Khi c nhiu
linh kin iu khin net tri0 hoc tri1, th s phn gii mnh ca cc linh kin iu khin vi
mnh pull ca net tri0 hoc tri1, s xc nh gi tr ca net.
3.4.6
3.4.7
Trong thc t bt k net no trong mch in t cng to ra tr hon trn net. Trong
Verilog, tr hon c th c khai bo kt hp trong pht biu khai bo net. Nhng gi
[Type text] Page 77
Gii thiu
Ta phi khai bo tht tng minh v chiu (input, output hay bidirectional) ca mi port
xut hin trong danh sch khai bo port. Trong Verilog nh ngha ba loi port khc nhau, l
input, output v inout. Loi d liu ca port c th l net hoc reg. Loai d liu reg ch c th
xut hin port output. Hng s v biu thc lun nm pha di khai bo port.
3.6.2
input
3.6.3
output
3.6.4
inout
Ta c th khai bo port hai chiu (bidirectional) vi pht biu inout. Mt port inout c loi d
liu l wire v c iu khin bi c php ca wire. Ta phi khai bo port inout trc khi n
c s dng.
V d 3.13
inout a:
inout [2:0] b;
V d 3.14
module fulladder(cout, sum, in1, in2, in3);
input in1, in2, in3;
// khai bo 3 ng vo
//khai bo 2 ng ra
endmodule
Gii thiu
Mng net
V d 3.15
wire [63:0] bus;
V d 3.15 m t vic khai bo mt mng wire c rng 64 bits.
V d 3.16
wire vectored [31:0] bus1;
wire scalared [31:0] bus2;
V d 3.16, ta s dng hai t kha ch dn vectored v scalared, chng u c dng
khai bo multi-bit Nets, tuy nhin chng khc nhau ch c cho php m t tng bit hay
tng phn ca net hay khng, vi vectored net, vic chn tng bit l khng c php, cn vi
scalared net th c php:
assign bus1 [1] = 1b1;
Trnh bin dch chp nhn c php ca nhng cu trc m t Verilog ny, tuy nhin chng
s b b qua khi mch c tng hp ra phn cng.
3.7.3
3.7.4
Mng phn t nh
//
3.8
Gii thiu
Thm vo kh nng m hnh ha cho phn cng trong Verilog, ta c th s dng thm mt
s loi d liu bin khc ngoi d liu bin reg. Mc d bin d liu reg c th c dng cho
nhng chc nng tng qut nh m thi gian, lu gi s thay i gi tr ca net, bin d
liu integer v time th cung cp s thun li v d c hiu hn trong vic m t thit k.
3.8.2
Integer
Loi d liu integer l bin c chc nng tng qut c dng tnh ton s lng.
N khng c xem nh l thanh ghi trong phn cng thit k. Loi d liu integer c rng
32 bit, n c th c gn v s dng hon ton ging nh loi bin d liu reg. Php gn qui
trnh (procedural assignment) c dng kch s thay i gi tr ca loi d liu integer.
Nhng php tnh trn bin d liu integer s to ra nhng kt qu di dng b 2.
V d 3.19: Khai bo bin integer
integer i1, i2;
3.8.3
Time
3.8.4
Bn cnh bin d liu integer v time, Verilog cn c h tr vic s dng hng s thc v
bin d liu thc (real). Ngoi tr 3 ngoi l nh trnh by pha di y th bin d liu real c
th c s dng tng t nh integer v time:
1. Khng phi tt c cc php ton trong Verilog c th c s dng vi nhng s thc.
Cc bng 3.2 v 3.3 cho bit cc ton t v php ton khng c php dng vi s thc
v ton t s thc (xem thm Mc 4.2.1).
Bng 3.2 Danh sch cc ton t khng c php s dng i vi s thc
unary + unary -
+ - *
Ton t s hc (Arithmetic)
/ **
! && ||
== !=
?:
Bng 3.3 Danh sch cc ton t khng c php s dng i vi ton t s thc
{} {{}}
=== !==
~ , &, |, ^, ^~, ~^
2. Bin d liu khng c khai bo rng ca bin, vic tnh ton c thc hin dng
chun nh dng IEEE floating point.
3. Bin d liu c gi tr mc nh l 0.
Thi gian thc (realtime) c khai bo v s dng tng t nh s thc (real), chng c
th hon i cho nhau.
V d 3.21
real float;
realtime rtime;
3.9
Khai bo tham s
3.9.1
Gii thiu
Trong Verilog HDL, loi d liu tham s (parameter) khng thuc loi d liu bin (reg,
integer, time, real, realtime) cng nh loi d liu net (wire, tri, wand, wor,...). D liu tham
s khng phi l bin m chng l hng s. C hai loi tham s trong Verilog l:
Tham s module (module parameter): parameter v localparam.
Tham s c t (specify parameter): specparam.
C hai loi tham s trn u c php khai bo rng. Mc nh, parameter v
specparam s c rng cha gi tr ca hng s, ngoi tr khi tham s c khai bo
rng. Vic khai bo trng tn gia net, bin hay tham s l khng c php.
[Type text] Page 84
Tham s module c hai loi khai bo: parameter v localparam (local parameter).
3.9.2.1 Parameter
3.9.2.1.1
Gii thiu
parameter r = 46.7;
parameter
byte_size = 9,
byte_mask = byte_size - 6;
parameter average_delay = (r + f) / 2;
parameter signed [3:0] mux_selector = 0;
parameter real r1 = 3.6e19;
parameter p1 = 13'h7e;
parameter [31:0] dec_const = 1'b1;
// gi tr c i sang 32 bit
parameter newconst = 4;
3.9.2.1.2
top.m1.delay = 10,
top.m2.size = 10,
top.m2.delay = 20;
endmodule
Trong V d 3.22, module annotate c pht biu defparam, gi tr t pht biu ny s ln
nhng gi tr tham s size v delay trong instance m1 v m2 trong module top. Hai module
top v annotate u c xem nh module top-level.
3.9.2.1.2.2 Php gn gi tr tham s khi gi instance ca module
Trong Verilog c mt phng php khc dng gn gi tr n mt tham s bn trong
instance ca mt module, l s dng mt trong hai dng ca php gn gi tr tham s trong
instance ca module. Mt l php gn theo th t danh sch tham s, hai l php gn bi tn.
Hai dng php gn ny khng th t ln ln vi nhau m chng ch c th l mt trong hai
dng cho ton b instance ca module.
Vic gn gi tr tham s instance ca module theo th t danh sch tham s, tng t nh
vic gn gi tr tr hon cho nhng cng ca instance; cn vic gn gi tr tham s instance ca
module theo tn tham s, th tng t nh vic kt ni port ca module bi tn. N gn nhng
gi tr tham s cho nhng instance c th m trong module ca nhng instance ny nh
ngha nhng tham s trn.
Mt tham s m c khai bo trong mt block, mt tc v hay mt hm ch c th khai
bo li mt cch trc tip dng pht biu defparam. Tuy nhin, nu gi tr tham s ny ph
thuc vo mt tham s th hai, th vic nh ngha li gi tr tham s th hai cng s cp
nht gi tr ca tham s th nht.
Sau y ta xt chi tit hai php gn tham s ny.
1. Php gn gi tr tham s theo th t danh sch tham s
Th t ca nhng php gn trong php gn gi tr tham s theo th t danh sch tham s
instance ca module, s theo th t tham s lc khai bo bn trong module. N khng cn thit
phi gn gi tr cho tt c cc tham s c bn trong module khi dng phng php ny. Tuy
nhin, ta khng th nhy qua mt tham s, do gn nhng gi tr cho mt phn nhng tham
s trong tt c cc tham s khai bo trong module, th nhng php gn thay th gi tr ca
[Type text] Page 87
...
endmodule
module top;
...
my_mem #(12, 16) m(addr,data);
endmodule
2. Php gn gi tr tham s bi tn
Php gn gi tr tham s bi tn bao gm tn tng minh ca tham s v gi tr mi ca n.
Tn ca tham s s l tn c m t trong instance ca module. Ta khng cn thit gn nhng
gi tr n tt c cc tham s bn trong module khi s dng phng php ny. Ch nhng
tham s no m c gn gi tr mi th mi cn c ch ra.
Biu thc tham s c th l mt la chn vic gi instance ca module c th ghi li vic
hin din ca mt tham s, m khng cn bt k mt php gn n n. Nhng du ng m
ngoc c i hi, v trong trng hp ny tham s s gi gi tr mc nh ca n. Khi mt
tham s c gn mt gi tr, th mt php gn khc n tn tham s ny l khng c php.
Xt V d 3.26, trong v d ny c nhng tham s ca mod_a v ch mt tham s ca mod_c
v mod_d b thay i trong khi gi instance ca module.
V d 3.26
[Type text] Page 89
S ph thuc tham s
3.9.3
c t (specify block)
4 Chng 4.
Page 95
4.2 Ton t
K hiu cho ton t trong ngn ng m t phn cng Verilog tng t nh trong ngn ng
lp trnh C. Bng 4.1 l danh sch cc ton t ny.
Bng 4.1 Danh sch cc ton t
{} {{}}
++ - * / **
%
> >= < <=
!
&&
||
==
!=
===
!==
~
&
|
^
^~ hoc ~^
<<
>>
<<<
>>>
?:
4.2.1
+ - *
Ton t s hc (Arithmetic)
/ **
== !=
?:
Bng 4.3 Danh sch cc ton t khng c php s dng i vi ton t s thc
{} {{}}
=== !==
~ , &, |, ^, ^~, ~^
4.2.2
Ton t u tin
**
u tin cao nh
* / %
<< >> <<< >>>
< <= > >=
== != === !==
&(ton t 2 ngi)
^ ^~ ~^(ton t 2 ngi)
|(ton t 2 ngi)
&&
||
?:
u tin thp nh
{} {{}}
Cc ton t trong cng mt dng trong Bng 4.4 c th t u tin nh nhau. Cc dng c
sp xp theo th t tng dn u tin. V d cc ton t *, /, v % c cng u tin v u
tin ca n cao hn ton t + v -.
Cloud 2013
Page 97
4.2.3
Page 98
// kt qu l -4.
IntA = -'d 12 / 3;
// kt qu l 1431655761.
IntA = -'sd 12 / 3;
// s c du m 12 (1000000 1100-32bit)=>kt qu l
-4.
IntA = -4'sd 12 / 3;
4.2.4
Ton t phi thc hin theo cc quy tc kt hp trong khi nh gi mt biu thc nh c
miu t trong Mc 4.2.2. Tuy nhin, nu kt qu cui cng ca biu thc c th c pht hin
sm hn, th ton b biu thc khng cn c nh gi ht. iu ny gi l ngn mch (shortcircuiting) mt nh gi biu thc, vic ngn mch nh gi mt biu thc xy ra khi trong biu
thc dng cc ton t n (&, |, ) thay v ton t i (&&, ||).
V d 4.2
Reg regA, regB, regC, result;
Result = regA&(regB|regC)
Nu gi tr ca regA l 0 th kt qu ca biu thc c pht hin l 0 m khng cn tnh
ton gi tr ca biu thc con (regB|regC).
4.2.5
a cng b
a-b
a tr b
a*b
a nhn b
a/b
a chia b
a%b
a chia b ly d
a**b
a ly tha b
Php chia: Trong php chia s nguyn, cn phn tch phn s khi mu s l s 0. i
vi php chia v php chia ly phn nguyn (/), nu ton hng th 2 l 0 th kt qu ca ton b
biu thc phi l x. Trong php chia ly phn d (%), v d y%z, cho ra kt qu l phn d
khi ly y chia cho z, v vy khi z = 0 th kt qu chnh l y, khi kt qu ca php chia ly d
c gn bng ton hng u tin.
Cloud 2013
Page 99
Kt qu
Ch thch
10%3
10 chia 3 d 1
12%3
12 chia 3 khng d
-10%3
-1
11%-3
-1
dng > 1
op1**op2
bx
-4d12 c gi tr l 1
3**2
3*3
2**3
2*2*2
2**0
2.0**-3sb1
0.5
2**-3sb1
0**-1
bx
0 ly tha s m l mt s khng xc nh
9**0.5
3.0
Kt qu l mt s thc
Cloud 2013
Page 100
1.0
kt qu l 0
-3.0**2.0
9.0
Kt qu l mt s thc
-m
Ton t mt ngi tr m
4.2.6
Gii thch
net khng du
Khng du
net c du
C du, b 2
reg khng du
Khng du
reg c du
C du, b 2
integer
C du, b 2
time
Khng du
real, realtime
C du, du chm ng
Theo V d 4.3 s cho thy nhiu cch khc nhau chia tr 12 chia 3- s dng d
liu loi integer v reg trong biu thc.
Cloud 2013
Page 101
regS = -12 / 3;
regS = -4'sd12 / 3;
4.2.7
a nh hn b
a>b
a ln hn b
a<=b
a nh hn hoc bng b
a>=b
a ln hn hoc bng b
Page 102
Nhng . . .
foo-(1<a)
Khi foo-(1<a) c tnh ton, biu thc quan h s c tnh ton u tin, v sau hoc
l 0, hoc l 1 s c tr bi foo. Cn khi foo-1<a c tnh ton th gi tr ca ton hng foo
s tr i 1 sau em so snh vi a.
4.2.8
Ton t so snh bng c u tin thp hn so vi ton t quan h. Bng 4.11 lit k v
nh ngha ton t so snh bng
Bng 4.11 Ton t so snh bng
a===b
a bng b, bao gm c x v z
a!==b
a==b
a!=b
C bn ton t so snh bng s c u tin ging nhau. Bn ton t ny so snh tng bit
ca cc ton hng. Ging nh ton t quan h, kt qu s l 0 nu so snh sai v 1 nu so snh
ng.
Nu ton hng khng bng nhau v chiu di bit v nu mt hoc c hai ton hng l khng
du, th ton hng c s bit nh hn s thm bit 0 vo trc, cho bng kch thc ca ton
hng ln hn. Nu c hai l c du th ton hng c s bit nh hn s thm bit du vo trc,
cho bng kch thc ca ton hng c s bit ln hn.
Nu mt ton hng l mt s thc, th ton hng cn li s chuyn v kiu s thc v biu
thc c xem nh l php so snh gia hai s thc.
Trong ton t == v !=, nu ton hng l khng xc nh (x) hoc tr khng cao (z) th quan
h l khng xc nh, v kt qu s l mt bit c gi tr khng xc nh (x).
Trong ton t === v !==, s so snh s hon thnh nh l mt cu lnh case. Bit x hoc z
Cloud 2013
Page 103
4.2.9
Ton t logic and (&&) v or (||) l ton t logic lin kt. Kt qu ca s tnh ton so
snh logic s l 1, 0 hoc x nu kt qu khng r rng. u tin ca && ln hn || v c hai
c u tin thp hn ton t quan h v ton t so snh bng. Khi s dng hai ton t ny th
tt c cc biu thc con trong biu thc u c tnh ton (khng c s ngn mch nh gi
mt biu thc).
Ton t logic th 3 l ton t nghch o logic 1 ngi (!). Ton t nghch o chuyn i
ton hng khng phi s 0 hoc 1 thnh s 0 v chuyn s 0 hoc sai thnh 1. Kt qu gi tr
ng khng r rng s l x.
V d 4.5
V d 1: Nu reg alpha gi gi tr integer 237 v beta gi gi tr l 0, th v d cho php
thc thi nh m t:
regA=alpha && beta
//regA c ci t l 0
//regB c ci t l 1
V d 2: Biu thc cho php thc thi mt ton t logic v ba biu thc con m khng
cn bt k du ngoc n no
a < size -1 && b != c && index != lastone
Tuy nhin, khuyn khch s dng du ngoc n lm cho biu r rng hn v u
tin, nh cch vit trong v d di y:
(a < size -1) && (b != c) && (index != lastone)
V d 3: Thng thng s dng ton t ! trong mt cu trc nh:
if(!inword)
Trong mt vi trng hp, cu trc trn lm cho ngi c chng trnh kh hiu hn
cu trc: if (inword ==0).
4.2.10
Ton t thao tc trn bit s thc thi thao tc trn tng bit ca ton hng, y l ton t kt
hp tng bit trn mi ton hng, vi bit tng ng trn ton hng kia, tnh ton ra 1 bit kt
qu. Cc bng t 4-12 n 14-16 s cho thy kt qu mi php ton c th trn bit.
Bng 4.12 Ton t &
&
Cloud 2013
Page 104
^~, ~^
Cloud 2013
Page 105
Khi cc ton hng khng bng nhau v chiu di, th ton hng ngn hn s thm bit 0 vo
v tr bit c ngha nht (MSB).
Cloud 2013
Page 106
Bng 4.20 cho thy kt qu ca vic p dng ton t gim trn cc ton hng khc nhau.
Bng 4.20 Ton t gim trn cc ton hng khc nhau.
Ton hng
&
~&
~|
~^
Ch thch
4b0000
Tt c cc bit l 0
4b1111
Tt c cc bit l 1
4b0110
S chn l 1
4b1000
S l l 1
Page 107
4.2.13
Page 108
V d 4.7
Theo v d ny s c 3 trng thi bus u ra minh ho vic s dng ton t iu kin
thng thng.
wire [15:0]busa=drive_busa?data:16'bz;
Bus data s c li vo busa khi bit drive_busa l 1, cn khi bit drive_busa l 0 th
data s nhn 16'bz . Nu bit drive_busa khng xc nh, th mt gi tr khng xc nh s
c li vo busa, ni cch khc busa khng xc nh.
4.2.14
4.2.14.1
Ton t ghp ni {}
4.2.14.2
Ton t lp {{}}
Cloud 2013
Page 109
// tp cc gi tr {b, a, b, a, b, a, b}
// Hp l cho tt c P t 1 ti 32
Result = {4{func(w)}}
S tnh ton nh l :
y = func(w)
Result = {y, y, y, y}
Page 110
4.3.1
Bit-select: Trch ra mt bit ring bit t bin vector net, vector reg, integer, hoc time, hoc
parameter. Cc bit c th c nh a ch bng mt biu thc. Nu mt bit-select nm ngoi
gii hn hoc bit-select l x hoc z, th gi tr tr v c tham chiu s l x. Mt bit-select
hoc part-select ca mt gi tr v hng, hoc ca mt bin, hoc tham s thuc loi real hoc
realtime, s khng hp l.
Part-select: Mt s bit lin k nhau trong mt bin vector net, vector reg, integer, hoc time,
hoc tham s c th nh a ch, c gi l mt part-select. C hai loi part-select, part-select
hng s v part-select ch s. Part-select hng s ca mt vector net hoc reg c a ra theo
c php bn di:
Vect [msb_expr: lsb_expr]
C msb_expr v lsb_expr s l biu thc s nguyn khng i, gia hai biu thc ny c
du : ngn cch, biu thc u c a ch c ngha hn biu thc th hai.
Part-select ch s ca mt bin vector net, vector reg, integer hoc time, hoc tham s c
a ra theo c php bn di:
// khai bo hai vector reg
reg [15:0] big_vect;
reg [0:15] little_vect;
// cc c php part-select:
(a) big_vect[lsb_base_expr +: width_expr]
(b) little_vect[msb_base_expr +: width_expr]
(c) big_vect[msb_base_expr -: width_expr]
(d) little_vect[lsb_base_expr -: width_expr]
Cloud 2013
Page 111
// == big_vect[ 7 : 0]
little_vect[ 0 +: 8]
// == little_vect[0 : 7]
big_vect[15 -: 8]
// == big_vect[15 : 8]
// c php (c)
little_vect[15 -: 8]
// == little_vect[8 :15]
// c php (d)
dword[8*sel +: 8]
4.3.2
// c php (a)
// c php (b)
a ch mng v phn t nh
Vic khai bo mng v b nh (mng thanh ghi mt chiu) c tho lun Mc 3.7.
Trong phn ny s i vo vn nh a ch mng.
V d 4.11
Khai bo mt b nh 1024 t 8 bit:
reg [7:0] mem_name[0:1023];
C php cho a ch b nh s bao gm tn vng nh v biu thc a ch, theo nh dng
sau:
mem_name[addr_expr];
Trong addr_expr l mt biu thc nguyn bt k; v vy mt b nh gin tip c th
ch ra nh l mt biu thc n.V d sau minh ha cho b nh gin tip:
mem_name[mem_name[3]];
y, t nh a ch mem_name[3] s dng lm biu thc cho vic truy cp b nh
Cloud 2013
Page 112
twod_array[1][3][6]
twod_array[1][3][sel]
threed_array[14][1][3:0]
// Khng hp l
4.3.3
Chui
Page 113
initial begin
s1="Hello";
s2=" world!";
if ({s1,s2}=="Hello world!")
$display("strings are equal");
end
Vic so snh trong V d 4.15 khng cho kt qu nh mong mun, bi v trong qu trnh
gn vo bin chui, gi tr thm vo cc bin s1, s2 c lu vo nh sau:
s1 = 000000000048656c6c6f
s2 = 00000020776f726c6421
Cloud 2013
Page 114
4.4
Biu thc tr hon trong ngn ng Verilog HDL thng thng c ba gi tr, c m t bi
ba biu thc ngn cch nhau bi du hai chm (:) v gp li vi nhau bng du ngoc n (()).
iu theo th t th hin cc gi tr i din cho thi gian ti thiu, trung bnh v ti a
(min:typ:max). Ba gi tr ny cho php thit k cc chng trnh kim tra vi gi tr tr hon ti
thiu, trung bnh v ti a.
Cc gi tr th hin trong nh dng (min:typ:max) c th c s dng trong cc biu thc.
nh dng (min:typ:max) c th s dng bt k biu thc no.
C php min:typ:max c a ra theo c C php 4-2.
C php 4-2
constant_expression ::= constant_primary
| unary_operator { attribute_instance } constant_primary
| constant_expression binary_operator { attribute_instance }constant_expression
|
constant_expression
attribute_instance
constant_expression
constant_expression
constant_mintypmax_expression ::= constant_expression
| constant_expression : constant_expression : constant_expression
expression ::= primary
| unary_operator { attribute_instance } primary
| expression binary_operator { attribute_instance } expression
| conditional_expression
mintypmax_expression ::= expression
Cloud 2013
Page 115
4.5
Kim sot s lng bit c s dng trong vic tnh ton cc biu thc l rt quan trng nu
ph hp vi kt qu t c. Mt vi tnh hung c gii php n gin; v d, nu mt bit v
ton t c quy nh trn hai thanh ghi 16 bit, th kt qu s l mt gi tr 16 bit. Tuy nhin,
trong mt vi tnh hung, khng bit r rng l c bao nhiu bit c s dng trong vic tnh
ton biu thc, hoc kch c ca kt qu l bao nhiu.
V d, thc hin tnh ton php cng s hc ca hai thanh ghi 16 bit, cn s dng 16 bit,
hoc cn s dng 17 bit c th cha c bit trn? Cu tr li ph thuc vo loi thit b
Cloud 2013
Page 116
sumA = a + b;
sumB = a + b;
4.5.1
Cc lut qun l biu thc di bit c trnh bi r rng cc tnh hung thc t c
mt gii php t nhin.
S lng bit ca mt biu thc (cn gi l kch c ca biu thc) s c xc nh bng
ton hng c gi trong biu thc v ni dung ca biu thc a ra.
Mt biu thc t xc nh, l biu thc m di bit ca n c xc nh duy nht bi t
biu thc , v d, biu thc th hin gi tr tr hon.
Mt biu thc xc nh ton b, l biu thc m di bit ca n c xc nh bng
di bit ca biu thc v mt phn ca biu thc c lin quan khc. V d, kch c bit ca
biu thc bn phi php gn ph thuc vo t n v kch c ca biu thc bn tri.
Bng 4.22 th hin cch cc biu thc thng thng xc nh di bit ca kt qu biu
thc. Trong Bng 4.22, i, j v k l cc ton hng ca biu thc, v L(i) th hin di bit ca
ton hng i.
Ton hng nhn c th thc hin m khng mt bt k bit trn no bng cch gn kt qu
rng cha n.
Bng 4.22 Biu thc xc nh di bit ca kt qu biu thc
Biu thc l:
di bit
Ch thch
& | ^ ^~ ~^
i op j, vi op l : + - ~
Cloud 2013
s
max (L(i),L(j))
L(i)
Page 117
===
!== 1bit
1bit
(L(i),L(j))
Tt c cc ton hng t xc nh
i op j, vi op l:
1bit
Tt c cc ton hng t xc nh
& ~& | ~| ^ ~^ ^~ !
i op j, vi op l:
L(i)
Ton hng j t xc nh
max(L(j),L(k)
Ton hng i t xc nh
{i,...,j}
L(i)+..+L(j)
Tt c cc ton hng t xc nh
i *(L(j)+..+L(k))
Tt c cc ton hng t xc nh
// ton t ghp ni
{i{j,..,k}} // ton t lp
4.5.2
Trong sut qu trnh tnh ton mt biu thc, kt qu tm thi s ly kch c ca ton hng
ln hn (trong trng hp ton t gn, n cn bao gm c bn tri php gn). S thn trng
s ngn chn vic mt bit du trong qu trnh tnh ton. V d sau y m t cch di bit
ca ton t c th lm cho kt qu mt bit du.
V d 4.18
reg [15:0] a, b, answer;
// s thc thi ng
Xt v d tip theo:
V d 4.19
module bitlength();
reg [3:0] a,b,c; // a, b, c l 4 bit
reg [6:0] d;
// d l 7 bit
initial begin
Cloud 2013
Page 118
end
endmodule
Cu lnh $display s hin th:
answer = 0001000
// 7 bit
Bng cch t n, biu thc a&b c chiu di l 4 bit, nhng bi v trong ni dung ca biu
thc iu kin, n s s dng di bit ln nht (Bng 4.2.2), vy nn biu thc a&b s c
di l 7, l di ca d.
4.5.3
V d 4.20
reg [3:0] a;
reg [5:0] b;
reg [15:0] c;
initial begin
a = 4'hF;
b = 6'hA;
$display("a*b=%h", a*b);
c = {a**b};
$display("a**b=%h", c);
c = a**b;
$display("c=%h", c);
end
Kt qu m phng ca v d ny:
a*b=16 // 'h96 b ct b cn 'h16 v kch thc ca biu thc l 6
a**b=1 // kch thc ca biu thc 4 bit (kch thc ca a)
c=ac61 // kch thc ca biu thc 16 bit (kch thc ca c)
Page 119
tr v mt gi tr c du
$unsigned
tr v mt gi tr khng du
V d 4.21
reg [7:0] regA, regB;
reg signed [7:0] regS;
regA = $unsigned(-4);
// regA = 8'b11111100
regB = $unsigned(-4'sd4);
// regB = 8'b00001100
// regS = -4
4.6.1
// b[7:0] l khng du
Cloud 2013
Page 120
4.6.3
Nhng bc nh gi mt php gn
4.6.4
Page 121
[5:0] a;
b = 8'hff;
end
V d 2:
reg
[0:5] a;
[7:0] a;
d = b;
end
4.8 Bi tp
1. Nu cc ton t thng dng v u tin ca chng?
Cloud 2013
Page 122
a = 255; b = 255; c = a + b;
c = 9'b0 + a + b;
d={a,b};
c = &b;
Cloud 2013
Page 123
Cu trc phn cp
Ngn ng m t phn cng Verilog h tr cu trc phn cp bng cch cho php module
c nhng trong module khc, module cp cao hn to th hin ca module cp thp
hn v giao tip vi chng thng qua cc u vo, u ra v u vo-ra 2 chiu. Cc cng vo ra
c th l v hng hoc l vector.
Cu trc phn cp gip ngi thit k chia mt h thng thit k ra thnh cc module nh
hn d thit k v kim sot lung d liu trong qu trnh thit k.
Nh mt v d cho h thng module phn cp, hy xem xt mt h thng bao gm cc
bng mch in (PCBs).
5.2
Module
5.2.1
Khai bo module
Trong mc ny cung cp c php thng thng cho mt nh ngha module v c php cho
vic ci t module, cng vi mt v d v nh ngha module v ci t module.
Mt nh ngha module c bao gia bi hai t kha module v endmodule. Cc nh
danh km theo sau t kha module s l tn nh ngha ca module; danh sch cc ty chn
ca tham s c nh ngha, s ch r mt danh sch theo th t cc tham s ca module;
danh sch cc ty chn ca cng hoc khai bo cng c nh ngha, s ch r mt danh sch
theo th t cc cng ca module. Th t c s dng trong nh ngha danh sch cc
tham s v trong danh sch cng, c th c ngha trong vic ci t cc module. Cc nh
danh trong danh sch ny s khai bo li trong cc cu lnh input, output, v inout trong nh
ngha module. Khai bo cng trong danh sch khai bo cng s khng khai bo li trong thn
module. Cc mc ca module nh ngha ci to thnh module, v chng bao gm nhiu loi
khai bo v nh ngha khc nhau, nhiu trong s c gii thiu.
T kha macromodule c th dng thay th t kha module nh ngha mt module.
Mt qu trnh thc thi c th chn gii quyt module c nh ngha bt u vi th kha
macromodule khc nhau. C php khai bo module c cho trong c php 5-1:
C php 5-1
module_declaration ::=
{attribute_instance} module_keyword
module_identifier [module_parameter_port_list ]
list_of_ports ; { module_item }
endmodule
|{ attribute_instance } module_keyword
Cloud
Page 124
Page 125
5.2.2
5.2.3
Page 126
Page 127
parameter d = 10;
// th hin ca mch ffnand, tn l "ff", v c t u ra ca cc kt ni IO bn trong
ffnand ff(out1, out2, in1, in2);
// nh ngh dng sng m phng mch
initial begin
#d in1 = 0; in2 = 1;
#d in1 = 1;
#d in2 = 0;
#d in2 = 1;
end
endmodule
V d 2: V d ny to ra 2 th hin ca module flip-flop ffnand c nh ngha trong v
d 1. N kt ni ch vi u ra q vo mt th hin v ch mt u ra qbar vo mt th
hin khc.
// dng sng m t kim tra nand flip-flop, khng c cng u ra
module ffnand_wave;
reg in1,in2;//bin iu khin mch
parameter d=10;
// to hai bn sao ca mch ff nand
// ff1 c qbar khng kt ni, ff2 c q khng kt ni
ffnand ff1(out1,,in1,in2),
Cloud
Page 128
5.2.4
Khai bo port
Page 129
Page 130
input signed [7:0] b, c, d; // nhiu cng cng chia s mt thuc tnh khai bo
output [7:0] e;
output [7:0] e,
Page 131
Cng wb kt ni ti v[3].
Cng c kt ni ti w.
Cng d kt ni ti v[4].
Page 132
Page 133
Cloud
Page 134
Mt net v hng.
Mt vector net.
Cloud
Bin.
Mt net v hng
2.
Mt vector net.
3.
4.
Lut 3: Nu net hai bn ca cng l loi net wire, mt cnh bo s xy ra n net khng gp
li vo trong mt net n nh m t trong Mc 5.2.4.10.
5.2.4.10
Khi cc loi net khc nhau kt ni vi nhau thng qua mt module, th cc net ca tt c
cc cng phi a v cho ging loi vi nhau. Kt qu loi net c xc nh theo bng 5-1.
Trong bng ny, net ngoi ngha l net ch ra trong th hin ca module, net ni ngha l
net ch ra trong module nh ngha. Net m loi ca n c s dng gi l dominating net. Net
m loi ca n b thay i gi l dominated net. N c quyn hp cc dominating v dominatr
net vo trong mt net n, loi ny c loi nh l mt dominating net. Kt qu ca net gi l
simulated net v dominated net gi l collapsed net.
Loi simulated net s thc hin delay ch ra dominating net. Nu dominating net l loi
trireg, bt k gi tr mnh no ch ra cho trireg s p dng cho simulated net.
Bng 5.1 T hp gia net ni v net ngoi
Net ni
Net ngoi
wire, tri
ext
ext
ext
ext
ext
ext
ext
ext
ext
wand,
int
ext
ext
ext
ext
ext
ext
ext
ext
warn
ext
warn
ext
warn
ext
warn
ext
ext
ext
warn
ext
warn
ext
warn
ext
warn
ext
ext
ext
ext
ext
warn
ext
ext
ext
int
ext
warn
ext
warn
ext
ext
ext
int
warn
ext
ext
ext
warn
int
int
ext
ext
ext
warn
ext
triand
wor,
int
ext
warn
ext
trior
trireg
int
warn
ext
ext
int
warn
ext
warn
ext
int
warn
ext
warn
ext
int
warn
int
warn
int
int
warn
int
int
warn
int
warn
int
warn
int
warn
int
tri0
tri1
uwire
supply0
supply1
int
int
int
int
int
int
int
int
warn
T kha:
Cloud
5.2.4.11
Thuc tnh du khng c thng qua trong cu trc phn cp. Trong th t c
loi c du qua cu trc phn cp, t kha signed phi c s dng trong khai bo i tng
mt cp khc trong cu trc phn cp. Bt k biu thc no trong mt cng s c xem nh
l bt k biu thc no khc trong php gn. N s c loi, k c, nh gi v gi tr kt qu gn
ti i tng bn khc ca cng s dng ging lut nh mt php gn.
5.3
Bi tp
Cloud
Page 137
Gii thiu
M hnh thit k cu trc m t cc h thng di dng cc cng linh kin hay cc khi linh
kin c kt ni li vi nhau thc hin c nhng chc nng mong mun. M hnh thit
k cu trc c m t mt cch trc quan h thng thit k s, do n thc s gn ging
vi m t vt l phn cng ca h thng.
Ngi thit k thng s dng m hnh thit k cu trc cho nhng module nh cn ti u
v timing, din tch, v s dng m hnh ny th phn cng thit k sau khi tng hp ra mch s
ging vi m t thit k trn Verilog. Tuy nhin, i vi mt h thng ln th vic s dng m
hnh cu trc l khng kh thi, bi v s cng knh ca n khi ghp hng ngn hng vn cng c
bn li vi nhau, cng nh tiu tn thi gian rt ln cho vic chy m phng kim tra thit k.
6.2
6.2.1
nand
or
nor
xor
xnor
0
0
0
0
0
1
0
1
x
x
x
0
x
x
x
z
0
x
x
x
nand
0
1
x
z
0
1
1
1
1
1
1
0
x
x
x
1
x
x
x
z
1
x
x
x
or
0
0
0
1
1
x
x
z
x
xor
0
0
0
1
1
x
x
z
x
[Type text]
Page 138
1
x
x
1
1
1
1
x
x
1
x
x
1
x
z
1
x
x
0
x
x
x
x
x
x
x
x
nor
0
1
x
z
0
1
0
x
x
1
0
0
0
0
x
x
0
x
x
z
x
0
x
x
xnor
0
1
x
z
0
1
0
x
x
1
0
1
x
x
x
x
x
x
x
z
x
x
x
x
6.2.2
Hai cng logic ny c mt u vo v mt hoc nhiu u ra. Tham s cui cng trong
danh sch cc tham s s kt ni vi u vo ca cng logic, cc tham s khc kt ni ti u ra.
Khai bo th hin ca mt cng logic nhiu u ra loi ny s bt u vi mt trong nhng t
kha sau:
buf
not
not
u ra
0
1
x
z
u vo
0
1
x
z
u ra
1
0
z
x
V d 6.2
[Type text]
Page 139
6.2.3
bufif1
notif1
notif0
INPUT
notif0
[Type text]
0
0
1
x
z
0
1
x
x
0
CONTROL
1
x
z
z
z
z
L
H
x
x
CONTROL
1
x
z
L
H
x
x
z
bufif1
INPUT
notif1
0
0
1
x
z
z
z
x
x
0
CONTROL
1
x
0
1
z
z
L
H
x
x
CONTROL
1
x
z
L
H
x
x
z
Page 140
INPUT
1
0
x
x
z
z
z
z
L
H
x
x
L
H
x
x
INPUT
0
1
x
z
z
z
x
x
1
0
z
z
L
H
x
x
L
H
x
x
V d 6.3
V d sau khai bo mt th hin ca cng bufif1:
bufif1 bf1 (outw, inw, controlw);
Trong u ra l outw, u vo l inw, u vo iu khin l controlw, th hin
tn l bf1
6.2.4
Cng tc MOS
nmos
pmos
rcmos
rnmos
rpmos
Page 141
CONTROL
rpmos
INPUT
cmos
CONTROL
rcmos
INPUT
V d 6.4
V d ny khai bo mt cng tc pmos:
pmos p1 (out, data, control);
Trong u ra l out, u vo l data, u iu khin l control v tn th hin l p1.
6.2.5
tranif1
tranif0
rtran
rtranif1
rtranif0
Cng thc truyn hai chiu s khng tr hon tn hiu truyn qua chng. Khi thit b tranif0,
tranif1, rtranif0 hoc rtranif1 l tt, chng s chn tn hiu; v khi chng m th chng s
cho tn hiu i qua. Thit b tran v rtran khng th tt v chng lun lun cho tn hiu qua
chng.
c t tr hon cho cc thit b tranif1, tranif0, rtranif1, v rtranif0 l 0, 1 hoc 2 tr hon.
Nu c t tr hon bao gm 2 tr hon, tr hon u s xc nh u ra tr hon m, tr hon th
hai s xc nh u ra tr hon ng, v nh hn trong 2 tr hon s xc nh tr hon ca chuyn
tip ti x v z. Nu ch c mt tr hon c a ra th n c t cho c tr hon m v ng.
Nu khng c c t tr hon th s khng c tr hon ng v m cho cng tc truyn hai chiu.
Cng tc truyn hai chiu tran v rtran s khng chp nhn c t tr hon.
[Type text]
Page 142
6.2.6
Cng tc CMOS
rcmos
Page 143
6.2.7
pulldown
Page 144
Ngn ng Verilog cung cp cho m hnh chnh xc ca tn hiu tranh chp, cng truyn hai
chiu, thit b MOS in tr, MOS ng, chia s thay i, v nhng cu hnh mng khc ph
thuc k thut bng cch cho php tn hiu net v hng c gi tr khng y v c nhiu
cp mnh khc nhau hoc t hp cc cp mnh. M hnh logic nhiu cp
mnh gii quyt t hp tn hiu trong cc gi tr bit v khng bit biu din cho hnh vi ca
phn cng thc hin chnh xc hn.
Chi tit v mnh s c hai yu t:
mnh phn 0 ca gi tr net, gi l strength0, thit k nh mt trong cc t kha sau:
supply0
strong0
pull0
weak0
highz0
strong1
pull1
weak1
highz1
Cp mnh
7
6
5
4
3
2
1
0
0
1
2
3
4
5
6
Page 145
strong
pull
weak
medium
small
Tn hiu vi mng thay i do lu tr s hnh thnh trong loi net triregC th ngh rng
mnh ca tn hiu trong Bng 6.5 nh v tr trn thc t l trong Hnh 6.2.
strength0
7
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
Hnh 6.2 mnh cc mc logic 0 v 1
Tho lun v t hp tn hiu sau phn ny dng hnh v tng t nh Hnh 6.2
Nu gi tr tn hiu ca mt net l bit, tt c mnh ca n s nm trong mt trong hai
phn: phn strength0 ca thc t l trn Hnh 6.2 hoc phn stregth1. Nu gi tr ca mt
net l khng bit, n s c cp mnh cu trong phn strength1 v strength0. Mt net
vi gi tr z c cp mnh trong mt phn con ca thc t l.
6.2.9
Page 146
Nhiu cu hnh c th to ra cc tn hiu vi mnh khng r rng. Khi hai tn hiu bng
nhau v mnh v ngc nhau v gi tr kt hp, kt qu s l gi tr x, cng vi cp
mnh ca c hai tn hiu v cc cp mnh nh hn.
V d: Trong Hnh 6.4 ch ra mt t hp ca tn hiu weak vi gi tr 1 v tn hiu weak
vi gi tr 0 cho ra mt tn hiu c mnh weak v c gi tr l x.
Hnh 6.4 Hai tn hiu c mnh bng nhau cng iu khin mt net
[Type text]
Page 147
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
Page 148
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
[Type text]
Page 149
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
Page 150
strength1
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
[Type text]
Page 151
strength0
4
3
2
strength1
2
3
4
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
Hnh 6.17 mnh ca tn hiu ng ra StH
Hiz0 trong phn ca kt qu bi v c t mnh cho cng trong cu hi xc inh
mnh cho u ra vi gi tr 0. c t mnh khc ngoi tr khng cao cho gi tr 0 kt qu u
ra trong mt u ra cng c gi tr x. u ra ca cng and bn di l weak 0 nh m t
trong Hnh 6.18.
strength0
7
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
Hnh 6.18 mnh ca tn hiu ng ra We0
Khi tn hiu t hp, kt qu c phm vi (36x) nh m t trong Hnh 6.19
strength0
7
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
Page 152
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
strength0
7
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
strength0
[Type text]
strength1
Page 153
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
strength0
7
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
Page 154
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
strength0
7
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
strength0
7
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
[Type text]
Page 155
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
Signal1
strength0
7
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
Signal2
Signal2
Gi tr
mnh
Kt qu
Gi tr
mnh
Gi tr
Kt qu ca tn hiu:
strength0
7
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
Page 156
Signal2
Gi tr
mnh
Kt qu
Gi tr
mnh
Gi tr
Kt qu ca tn hiu:
strength0
7
strength1
2
Su0 St0 Pu0 La0 We0 Me0 Sm0 HiZ0 HiZ1 Sm1 Me1 We1 La1 Pu1 St1 Su1
mnh gim
Supplydrive
Pulldrive
Strongdrive
Pulldrive
Pulldrive
Weak drive
Largecapacitor
Mediumcapacitor
Weak drive
Mediumcapacitor
Mediumcapacitor
Smallcapacitor
Smallcapacitor
Smallcapacitor
Highimpedance
Highimpedance
[Type text]
Page 157
Page 158
Ti gi tr:
2 tr hon
3 tr hon
d1
d1
min(d1,d2)
min(d1,d2,d3)
min(d1,d2)
d3
d2
d2
min(d1,d2)
min(d1,d2,d3)
min(d1,d2)
d3
d2
d2
d1
d1
min(d1,d2)
d3
d2
d2
d1
d1
min(d1,d2)
min(d1,d2,d3)
V d 6.8
V d 1: V d ny c t mt, hai v ba tr hon:
and #(10) a1 (out, in1, in2);
and #(10,12) a2 (out, in1, in2);
bufif0 #(10,12,11) b3 (out, in, ctrl);
// ch c mt tr hon
// tr hon cnh ln v cnh xung
// tr hon cnh ln, cnh xung, v tt
Page 159
n1 (ndata,data);
#(3,5)
n2 (wa,data,clock),
n3 (wb,ndata,clock);
nand #(12,15)
n4 (q,nq,wa),
n5 (nq,q,wb);
Page 160
Page 161
begin
Page 162
6.3.1
Page 163
Page 164
[Type text]
Page 165
[Type text]
Ghi ch
Page 166
Logic 0
Logic 1
Khng xc nh
Lp li cc gi tr 0,1
Khng thay i
(vw)
Gi tr thay i t v ti v v w c th l mt gi tr bt k trong 0, 1, x, ?,
w
Nh (??)
Nh (01)
Cnh tng ca u vo
Nh (10)
Cnh gim ca u vo
Lp li cc gi tr (01), Cnh m ca in th u vo
(0x) v (x0)
6.3.2
UDP t hp
Page 167
Page 168
6.3.3
Hnh vi ca mch tun t tch cc mc cng ging vi hnh vi ca mnh t hp, ngoi tr
u ra khai bo l mt loi reg v thm vo mt trng trong mi mc ca bng. Trng mi
ny s biu din trng thi hin ti ca UDP. Trng u ra trong UDP tun t biu din
trng thi tip theo.
Xem xt v d mch cht:
V d 6.14
primitive latch (q, clock, data);
output q; reg q;
input clock, data;
table
// clock data q q+
01 : ? : 1 ;
00 : ? : 0 ;
1? : ? : - ;// - = khng thay i
endtable
endprimitive
6.3.4
Page 169
Thut ng (01) biu din cho qu trnh chuyn tip ca gi tr u vo. C th, (01) biu
din mt chuyn tip t 0 ti 1. Dng u tin trong bng nh ngha UDP trc c hiu nh
sau: khi clock thay i gi tr t 0 ti 1 v d liu bng 0, u ra s l 0 bt k gi tr hin hnh.
Chuyn tip ca clock t 0 ti x vi d liu bng 0 v trng thi hin ti l 1 th kt qu u
ra q s l x.
6.3.5
nh ngha UDP cho php trn ln ln gia cu trc tch cc mc v tch cc cnh trong
[Type text]
Page 170
Trong v d ny, bin logic preset v clear l tch cc mc. Bt k khi no t hp preset
v clear l 01, th u ra s l 1. Tng t, khi t hp preser v clear l 10, th gi tr u ra s l
0.
Cc logic cn li l tch cc cnh vi clock. Trong trng hp clock thng thng, flip-flop
tch cc vi cnh ln ca clock, nh ch nh r trong trng clock trong bng bn trn. Trng
hp khng tch cc vi cnh xung ca clock c ch ra bi mt du gch (-) trong trng
u ra ( Bng 6.8) cho mi mu vi f l gi tr ca clock. Nh rng u ra mong mun cho cc
chuyn tip u vo trnh gi tr khng mong mun x u ra. Hai mu cui cng ch ra
chuyn tip trong u vo j v k khng thay i u ra khi ng h n nh mc thp hoc
[Type text]
Page 171
6.3.6
Gi s dng UDP
begin
data = 1; clock = 1;
#(20 * p1) $finish;
end
always #p1 clock = ~clock;
always #p2 data = ~data;
endmodule
[Type text]
Page 172
M t mch t hp
Page 173
Page 174
6.4.2
M t mch tun t
Page 175
V d 6.28 M t b m 4 bit
module counter(Q , clock, clear);
[Type text]
Page 176
6.5 Bi tp
1. Bn hiu th no v m hnh cu trc (structural model)?
2. Nu mt s cng logic c bn m bn bit v cc gi chng trong m hnh cu trc bng
ngn ng Verilog?
3. Cc m hnh mnh logic trong Verilog?
4. S kt hp mnh logic ca nhng tn hiu khng r rng trong Verilog nh th
no?
5. Nu cc t t tr hon cng v net ?
6. UDP l g? Nu cc loi UDP c bn?
7. Cch khai bo v s dng mt UDP c bn?
8. To mt UDP theo cng thc boolean sau: out = (a1 & a2 & a3 ) | (b1 & b2)
[Type text]
Page 177
Gii thiu
Php gn ni tip m hnh thit k RTL, thng thng m t lung d liu bn trong
nhng h thng ging nh lung d liu gia nhng thanh ghi. Php gn ni tip m hnh
thit k RTL a s c s dng trong vic thit k mch t hp.
Php gn ni tip m hnh thit k RTL, dng gn mt gi tr n net, net y c
th l net n hoc mt mng (vector) cc net, do biu thc bn tri php gn ni tip phi
c d liu l net, khng th l loi d liu thanh ghi (register). Php gn ny c thc hin
ngay khi c s thay i gi tr bn phi ca php gn. Php gn ni tip m hnh thit k
RTL, cung cp mt phng php m hnh mch t hp m khng cn m t s kt ni gia
cc cng vi nhau, m thay vo n m t biu thc logic iu khin net. Hay ni cch
khc, php gn ni tip iu khin net theo nh cch m cc cng linh kin iu khin net, trong
biu thc bn phi php gn c th c xem nh l mt mch t hp iu khin net mt
cch lin tc.
V d 7.1
assign m = 1b1;
assign a = b & c;
assign #10 a = 1bz;
7.2.2
Verilog cho php mt php gn ni tip c t trn cng pht biu khai bo net
V d 7.2
wire (strong1, pull0) mynet = enable ; //khai bo v gn
wire a = b & c; //khai bo v gn
[Type text]
Page 220
7.2.3
Trong pht biu php gn ni tip tng minh, ta dng mt cch tng minh mt php gn
ni tip vi t kha assign gn gi tr cho net sau khi net c khai bo. Nhng php gn
trn cc net s c thc hin lin tc mt cch t ng. Hay ni cch khc, bt c khi no gi
tr ca mt ton hng bn biu thc pha phi ca php gn thay i th ton b gi tr cc net
bn tri php gn s cp nht ngay li gi tr. Nu gi tr mi khc vi gi tr trc th gi tr
mi s c gn vo net bn tri php gn.
V d 7.3
wire a; //khai bo
parameter Zee = 1'bz;
assign a = Zee; //gn 1
assign a = b & c; //gn 2
Nhng dng hp l ca biu thc bn tri ca php gn ni tip phi l loi d liu net :
V d 7.4
module adder (sum_out, carry_out, carry_in, ina, inb);
output [3:0] sum_out;
output carry_out;
input [3:0] ina, inb;
input carry_in;
wire carry_out, carry_in;
wire [3:0] sum_out, ina, inb;
assign {carry_out, sum_out} = ina + inb + carry_in;
[Type text]
Page 221
endmodule
Trong v d trn ta thy net data c th nhn gi tr t nhiu php gn ni tip khc nhau.
7.2.4
Nu biu thc bn phi php gn to ra s thay i t trng thi khc 0 n trng thi
0, th tr hon thi gian xung (falling) s c s dng.
[Type text]
Page 222
7.2.5
mnh php gn
tri
trireg
wand
triand
tri0
[Type text]
Page 223
trior
tri1
pull1
weak1
highz1
pull0
weak0
highz0
Th t ca s m t hai mnh trn l ty . Hai nguyn tc sau s rng buc vic s dng
s m t mnh iu khin:
Nhng m t mnh (highz1, highz0) v (highz0, highz1) s c xem nh l khng
hp l.
Nu mnh iu khin khng c m t th mnh mc nh s l (strong1,
strong0).
7.3
Php gn qui trnh m hnh thit k mc thut ton s dng mt chui cc lnh c
th ca nhng pht biu nh ngha chui cc php ton trong h thng.
Vic m t ny ging nh vic m t chng trnh s dng ngn ng cp cao khc
chng hn nh C. Php gn qui trnh - m hnh thit k mc thut ton a s c s dng
trong vic thit k mch tun t. Php gn qui trnh s cung cp kh nng tru tng cn thit
m t mt h thng phc tp mc cao chng hn nh h thng vi x l hoc thc thi vic
kim tra nh thi phc tp, m ta kh c th thc hin c chng nu s dng m hnh cu
trc hoc m hnh RTL (php gn ni tip).
Php gn qui trnh c dng cp nht gi tr vo cho loi d liu bin (variable) nh
reg, integer, time, real, realtime v memory. Php gn ny khng mt thi gian, m thay
vo bin s lu gi gi tr ca php gn cho n khi c mt php gn qui trnh k tip cho
[Type text]
Page 224
Vic kt hp dng php {} ny khin vic phn chia cc phn kt qu ca gi tr biu thc
bn phi v gn nhng phn ny vo nhng phn khc nhau ca cc bin trong php {} bn tay
tri theo th t r rng.
Ch : Php gn n mt bin c kiu d liu bin l reg s khc so vi php gn n
bin c kiu d liu bin l real, realtime, time, hay integer khi m s bt bn phi php
gn t hn so vi bn tri php gn. Php gn n reg s khng sign-extend.
Php gn qui trnh xut hin bn trong nhng khi qui trnh (procedure) nh l always,
initial, task, function v nhng t kha ny c th c xem nh l s kch khi ca cc php
gn qui trnh.
Cc khi qui trnh always v khi qui trnh initial bt u theo nhng lung hot
ng c lp.
Cc khi qui trnh task v function, ta s xem xt trong chng sau. Ta xt mt v d n
gin hon chnh v m hnh thit k qui trnh procedure.
V d 7.6
module behave;
reg [1:0] a, b;
[Type text]
Page 225
Page 226
7.3.1
[Type text]
Page 227
Page 228
iu kin 2: Tn hiu output f nm trong c hai lung iu khin (sel==1) v (sel ==0).
Ta cn ch y, nu trong m t thit k mch t hp ta khng tun theo hai iu kin
trn th thit k s khng sai v c php nhng chc nng ca thit k s khng nh ta mong
mun, xem v d sau:
V d 7.10
module mux (f, g, a, b, c);
output f, g;
input a, b, c;
reg f, g;
always @ (a or b or c)
if (a == 1)
f= b;
else g = c;
endmodule
Trong V d 7.10, iu kin 1 c p ng tuy nhin iu kin 2 khng c p ng,
trong lung iu khin u tin (a==1) ch c output f c gn, vy output g khng c gi tr
xc nh, tip n lung iu khin k tip (a==0) ch c output g c gn, vy output f khng
c gi tr xc nh. Nh vy y khng th l mt m t thit k cho mch t hp bi v trong
mch t hp khi tn hiu input xc nh th tt c cc gi tr output cng phi xc nh.
7.3.3
Php gn qui trnh h cho php cc tt c cc php gn trong khi sequential block (beginend) c thc thi gn mt cch c lp m khng ph thuc vo qu trnh gn ca php gn
trc chng. Hay c th ni cch khc, tt c cc php gn trong sequential block (begin-end) s
c gn ng thi ngay ti mt thi im m khng cn quan tm n th t cng nh s ph
thuc vo cc php gn trc .
Theo php gn qui trnh h <= l ton t gn ca php gn qui trnh h. iu
khin vic nh thi (timing) cho php gn c th dng mt iu khin tr hon (delay) (v
d: #6) hay mt iu khin s kin (v d: @(posedge clk) ). Nu php gn i hi mt gi
tr t bn phi php gn, gi tr ny s c gn n cng ti thi im biu thc bn phi c
[Type text]
Page 229
[Type text]
Page 230
Page 231
Non-blocking assignments
Page 232
Page 233
[Type text]
Page 234
7.4.1
Cu trc if-else-if
Page 235
Page 236
Page 237
7.5.1
Hai loi khc ca pht biu case c cung cp cho php x l nhng iu kin dont care
trong vic so snh case. Mt l x l gi tr tng tr cao (z) nh l dont care, hai l x
l nhng gi tr tng tr cao (z) v gi tr khng xc nh (x) nh l dont care.
[Type text]
Page 238
[Type text]
Page 239
Biu thc hng s c th c dng trong biu thc ca case. Gi tr ca biu thc hng s
s c so snh vi biu thc ca case item.
V d 7.27 Minh ha vic s dng mch m ha u tin 3bit
reg [2:0] encode ;
case (1)
encode[2] : $display(Select Line 2) ; encode[1] : $display(Select Line 1) ;
encode[0] : $display(Select Line 0) ;
default $display(Error: One of the bits expected ON);
endcase
Ch rng, biu thc trong case l mt biu thc hng s (1). Cc case item l biu thc
(bit-selects) v s c so snh vi biu thc hng s. Nh vy, ch khi mt trong cc bit ca
encode bng 1 th biu thc i km vi n mi c thc thi.
V d 7.28
reg [2:0] encode ;
case (0)
encode[2] : $display(Select Line 2) ; encode[1] : $display(Select Line 1) ;
encode[0] : $display(Select Line 0) ;
default $display(Error: One of the bits expected ON);
endcase
Vi v d trn, ch khi mt trong cc bit ca encode bng 0 th biu thc i km vi n
mi c thc thi.
7.6
7.6.1
Cc pht biu lp
Trong Verilog h tr bn loi pht biu lp vng. Nhng pht biu ny cung cp phng
tin kim sot mt pht biu phi cn thc thi bao nhiu ln, c th l mt ln, nhiu ln hoc
c th l khng ln no.
forever: Thc thi mt pht biu lin tc.
repeat: Thc thi mt pht biu vi mt s ln c nh. Nu biu thc s ln lp c gi tr
l khng xc nh (x) hoc tng tr cao (z), n s c xem nh c gi tr zero v khng
c pht biu no c thc thi.
[Type text]
Page 240
7.6.2
C php
looping_statements ::=
| forever statement
| repeat ( expression ) statement
| while ( expression ) statement
| for ( reg_assignment ; expression ; reg_assignment ) statement
V d 7.29 Pht biu repeat mch nhn s dng ton t add v shift.
parameter size = 8, longsize = 16;
reg [size:1] opa, opb;
reg [longsize:1] result;
initial begin //mult
reg [longsize:1] shift_opa, shift_opb;
shift_opa = opa;
shift_opb = opb;
result = 0;
repeat (size) begin
if (shift_opb[1]) result = result + shift_opa;
shift_opa = shift_opa << 1;
shift_opb = shift_opb >> 1;
end
end
[Type text]
Page 241
[Type text]
Page 242
Page 243
7.7
Verilog HDL h tr ba phng php iu khin nh thi tng minh khi nhng pht
biu qui trnh xut hin. Loi u tin l iu khin tr hon delay control, loi th hai
l iu khin s kin event control. Loi th ba l pht biu wait.
Mt iu khin tr hon, c nhn din bt u vi k hiu #.
Mt iu khin s kin, c nhn din bt du vi k hiu @.
Pht biu wait, hot ng ca n l s kt hp gia iu khin s kin v vng lp while
nhng i lp v chc nng.
V d 7.34
#150 regm = regn;
@(posedge clock) regm = regn;
@(a, b, c) y = (a & b) | (b & c) | (a & c);
Vic m t thi gian tr hon cho cng v net s dng trong m phng c cp
trong cc phn trn, trong phn ny ch tho lun v ba phng php iu khin nh thi trong
cc php gn qui trnh.
7.7.1
Mt pht biu qui trnh theo sau mt iu khin tr hon s b tr hon vic thc thi
mt khong thi gian c m t trong iu khin tr hon. Nu biu thc tr hon c gi tr l
khng xc nh hoc tng tr cao, n s c xem nh c tr hon bng 0. Nu biu thc tr
[Type text]
Page 244
7.7.2
No edge
negedge
negedge
negedge
posedge
No edge
posedge
posedge
posedge
negedge
No edge
No edge
posedge
negedge
No edge
No edge
Nu gi tr ca biu thc nhiu hn 1 bit, s chuyn trng thi cnh s c d tm trn bit
c trng s thp nht ca gi tr . S thay i gi tr trong bt k ton hng no m khng c
s thay i gi tr trn bit c trng s thp nht ca biu thc th s chuyn trng thi cnh
khng th c d thy.
V d 7.36 Minh ha nhng pht biu iu khin s kin
@r rega = regb; // c iu khin bi bt k s thay i gi tr trn thanh ghi r.
[Type text]
Page 245
7.7.3
Vic thc thi mt pht biu qui trnh c th c tr hon cho n khi mt iu kin tr
thnh ng (true). iu ny t c bng s dng pht biu wait, y l mt dng c bit ca
iu khin s kin. Mc nh ca pht biu wait l tch cc mc, iu ny tri ngc vi
pht biu iu khin s kin l tch cc cnh.
Pht biu wait s tnh gi tr ca iu kin, nu gi tr sai (false), nhng pht biu qui trnh
[Type text]
Page 246
Page 247
7.8.1
Khi tun t
[Type text]
Page 248
V d 7.43 iu khin tr hon c th c dng trong khi tun t phn bit hai
php gn theo thi gian
begin
areg = breg;
@(posedge clock) creg = areg; // php gn b tr hon cho n
end
7.8.2
Page 249
7.8.3
Tn khi
N cho php khi c tham chiu trong nhng pht biu chng hn nh
pht biu disable.
Page 250
7.9.1
Cu trc initial
7.9.2
Cu trc always
Page 251
Page 252
Hnh 7.3 Lu my trng thi hu hn Moore c chc nng d tm chui 101 lin tc.
V d 7.48 M t thit k my trng thi Moore
[Type text]
Page 253
Page 254
Hnh 7.4 Mch pht hin chui 101 sau tng hp s dng FSM Moore
Page 255
Hnh 7.6 Lu my trng thi hu hn Mealy c chc nng d tm chui 101 lin tc.
V d 7.49
module Mealy101Detector (dataIn, found, clock, reset);
//Khai bo cng ng vo, ng ra
input
dataIn; input
clock; input
reset; output
found;
Page 256
[Type text]
Page 257
Hnh 7.7 Mch pht hin chui 101 sau tng hp s dng FSM Mealy
Hnh 7.7. Mch gm ba phn ng nh h thng my trng thi Mealy: mch t hp trng
thi k tip, mch chuyn trng thi v mch t hp ng ra, trong ng ra ph thuc vo gi tr
ng vo v trng thi hin ti.
7.11Bi tp
1. Nu s khc bit gia m hnh cu trc v m hnh hnh vi trong m t phn cng Verilog
HDL. Nu u im v khuyt im ca tng m hnh.
2. Nu s khc bit gia php gn qui trnh kn v php gn qui trnh h.
3. Trong m hnh hnh vi, nu s khc bit gia php gn lin tc (cn gi l continuous
assignment hay php gn RTL) vi php gn qui trnh.
4. Ti sao ta phi s dng hm v tc v trong m t phn cng Verilog HDL?
5. Nu s khc bit gia hm, tc v v module trong m t phn cng Verilog HDL?
6. Nu nhng rng buc khi s dng php gn qui trnh kn trong m t mch t hp?
7. Nu s khc bit gia pht biu case v casex.
8. Nu s khc nhau gia my trng thi Moore v my trng thi Mealy trong m t
phn cng Verilog HDL. Nu u im v khuyt im ca mi loi. Trnh by s tng ng
gia cc phn trong mch phn cng v cc phn trong m t Verilog khi s dng phng
php my trng thi Moore hoc Mealy.
9. Nu nhng phng php iu khin nh thi trong m t phn cng Verilog HDL ?
10. C my loi cu trc qui trnh trong m t phn cng Verilog HDL ?. Nu s khc bit
gia cu trc initial v cu trc always ? Ti sao cu trc initial khng th tng hp ra
[Type text]
Page 258
[Type text]
Page 259
14. on code sau thc hin chc nng g? Vit li on m t li s dng php gn kn
always @(posedge clock) begin
a <= b;
b <= a;
end
15. on code sau thc hin chc nng g? Vit li on m t li s dng php gn kn
always @(posedge clock)
#0 a <= b + c;
always @(posedge clock)
b <= a;
16. V dng sng ca tn hiu d trn ton b thi gian m phng
`timescale 1ns/100ps
module test; reg b,c,d; initial begin
b=1b1;
c=1b0;
#10 b=1b0;
end
initial d = #25(b|c);
endmodule
17. V dng sng ca a, b v c trong 100ns u tin ca qu trnh m phng.
module test;
wire a, b;
reg c;
assign #60 a = 1 ;
initial begin
#20 c = b;
#20 c = a;
#20;
end endmodule
18. Kim tra xem on m t sau c thc hin chc nng tm gi tr ln nht c khng?
[Type text]
Page 260
[RAMSIZE - 1 : 0] decoded_addr;
integer
i;
[RAMSIZE - 1 : 0] decoded_addr;
integer
i;
Page 261
[Type text]
Page 262
8 Chng 8.
Tc v (task) v hm (function)
task v function cung cp kh nng thc thi cc th tc chung t nhiu ni khc nhau trong
mt m t thit k. Chng cung cp mt phng tin chia nh nhng m t thit k ln, phc
tp thnh nhng phn nh hn d dng trong vic c v g ri cc m t thit k ngun.
Nh ta bit, trong m hnh cu trc ta c th gi (instantiate) mt sub-module thc hin
mt chc nng no ra s dng bt k u m khng cn phi m t li thit k ca module
. Tuy nhin, trong m hnh hnh vi (behavioural model), ta khng th gi module ra ging
nh vy c. Do , gii quyt yu cu c th s dng mt m t thit k c chc nng
no nhiu ln trong m hnh hnh vi m khng cn phi m t li th ta s s dng hm
(function) hoc tc v (task).
Phn ny s tho lun s khc nhau gia task v function, m t cch nh ngha v gi task
v function, cc v d m t cho mi phn.
8.1
Page 263
8.2.1
nh ngha task
Page 264
8.2.2
Khai bo task
Page 265
i s u vo ra.
Tt c cc kiu d liu c th khai bo trong mt khi th tc.
C php th hai bt u vi t kha task, theo sau l tn ca task v danh sch cc cng
ca task nm trong du ngoc n. Danh sch cc cng ca task c th khng c hoc c
nhiu cc cng ngn cch nhau bi u phy. V c mt du chm phy sau du ngoc n.
Tip theo l phn thn ca task v kt thc bng t kha endtask.
Trong c hai c php, khai bo cc cng ging vi c php c nh ngha bi
tf_input_declaration, tf_output_declaration, tf_inout_declaration c m t trong C php 8-1
bn trn.
Task m khng cha t kha ty chn automatic l mt task tnh, vi tt c cc mc khai
bo s c phn b c nh. Nhng mc ny s c chia s thng qua tt c cc s dng
ca task thc thi hin ti. Task bao gm t kha automatic s l mt task ng. Tt c cc mc
khai bo trong task ng s c phn b ng trong mi ln task c gi. Cc mc ca
task ng khng th truy cp theo cu trc phn cp. Task ng c th c gi s dng thng
qua tn phn cp.
8.2.3
Cu lnh kch hot task s thng qua cc i s nh mt danh sch cc biu thc nm trong
du ngoc n ngn cch vi nhau bi du phy. C php kch hot task c m t trong C
php 8-2.
C php 8-2
task_enable ::=
hierarchical_task_identifier [ ( expression { , expression } ) ] ;
Nu nh ngha mt task khng c i s, danh sch i s s khng c cung cp trong
cu lnh kch hot task. Ngc li, nu nh ngha task c i s, th s c mt danh sch
cc biu thc theo th t tng ng vi kch thc v thc t ca danh sch cc i s trong
nh ngha task. Mt biu thc rng khng c xem l mt i s trong cu lnh kch hot task.
Nu mt i s trong task c khai bo l input, th biu thc tng ng vi i s l
mt biu thc bt k. Trnh t nh gi mt biu thc trong danh sch cc i s l khng c
nh ngha trc. Nu mt i s trong task c khai bo l output hoc inout, th biu thc
tng ng s gii hn l mt biu thc ph hp vi biu thc bn tri trong th tc gn (phn
9.2). Cc mc sau y p ng yu cu ny:
Cc bin reg, integer, real, realtime, v time.
[Type text]
Page 266
B nh tham chiu.
Cc bin kt ni ca reg, integer, v time.
Kt ni ca b nh tham chiu
Cc bin bit-selects v part-selects ca reg, integer, v time.
Vic thc thi cu lnh kch hot task s thng qua gi tr input t danh sch cc biu
thc trong cu lnh kch hot ph hp vi i s ca task. Vic thc thi s tr v gi tr t
task thng qua cc gi tr t cc i s loi output hoc inout ca task tng ng vi bin trong
cu lnh kch hot task. Tt c cc i s trong task s thng qua cc gi tr hn l tham chiu
(l mt con tr n gi tr).
V d 8.1 m t cu trc c bn ca nh ngha mt task vi nm i s:
V d 8.1
task my_task;
input a, b;
inout c;
output d, e;
begin
. . . // cc cu lnh thc thi nhim vca task.
...
c = foo1; // gn trng thi ban u cho thanh ghi kt qu.
d = foo2;
e = foo3;
end
endtask
Hoc s dng hnh thc th 2 ca khai bo task, task c th nh
ngha nh sau:
task my_task (inputa, b, inoutc, outputd, e);
begin
. . . // cc cu lnh thc thi nhim vca task.
...
c = foo1; // gn trng thi ban u cho thanh ghi kt qu.
d = foo2;
e = foo3;
end
[Type text]
Page 267
endtask
[Type text]
Page 268
Page 269
8.2.4
8.3 Hm v vic gi hm
Mc ch ca mt function l tr v mt gi tr c s dng trong mt biu thc.
Phn tip theo ca chng ny s m t cc nh ngha v s dng function.
8.3.1
Khai bo hm
Page 270
Page 271
[Type text]
Page 272
8.3.2
Tr v mt gi tr t hm
8.3.3
Vic gi hm
8.3.4
Nhng qui tc v hm
Page 273
Page 274
8.3.5
Page 275
8.4 Bi tp
1. Ti sao phi dng task v function trong khi Verilog HDL h tr module?
2. Phn bit task v function?
3. Cch khai bo v s dng task?
4. Cch khai bo v s dng function?
5. S dng function cn tun theo nhng quy tt no?
[Type text]
Page 276
GiaotrinhVerilogHDL_v19
9 Chng 9.
Mt h thng c thit k dng Verilog phi c m phng v kim tra xem thit k
ng chc nng cha trc khi to ra phn cng. Trong qu trnh chy m phng ny, nhng li
thit k v s khng tng thch gia nhng linh kin dng trong thit k c th c pht hin.
Chy m phng mt thit k i hi vic to ra mt d liu ng vo kim tra v qu trnh quan
st kt qu sau khi chy m phng, d liu ng vo dng kim tra ny c th c to bng
hai cch, mt l to dng sng (waveform) bng tay s dng trnh waveform editor, tuy nhin
cch ny ch kh thi cho nhng thit k nh vi s lng tn hiu ng vo t, cn i vi mt
thit k h thng ln, phc tp vi nhiu tn hiu ng cn hng ngn chu k kim tra th ta
phi s dng testbench m t d liu ng vo kim tra. Testbench s dng cu trc mc cao
ca Verilog to ra d liu kim tra, quan st p ng ng ra, v c vic bt tay gia nhng
tn hiu trong thit k.
Bn trong testbench, h thng thit k cn chy m phng s c gi ra (instantiate) trong
testbench. D liu testbench cng vi h thng thit k s to ra mt m hnh m phng m s
c s dng bi mt cng c m phng Verilog.
Chng ny s tho lun v vic s dng ngn ng Verilog kim tra vic thit kt
module. Chng ta s thy rng thi gian v th tc hin th s tr nn quan trng hn khi tip
xc vi module testbench. Chng ny cho thy cch cu trc ca ngn ng Verilog c s
dng p dng d liu cho module trong qu trnh kim tra (module under test (MUT)), v
cch module p ng s c hin th v nh du. Trong phn u ca chng ny s tho
lun v d liu ng dng v theo di p ng. Trong phn cui s tho lun v k thut chn
kim tra thit k nhm to gii php tt nht cho vic thit k module.
9.1 Testbench
Mi trng m phng Verilog cung cp mt cng c ha hoc vn bn hin th
hin th cc kt qu m phng. Mt s mi trng m phng i xa hn, n cung cp mt cng
c ha cho vic chnh sa u vo d liu kim tra ti thit k module trong qu trnh kim
tra. Nh cc cng c bin tp dng sng, chng thng ph hp cho nhng thit k nh. i vi
cc thit k ln vi nhiu bus v d liu iu khin th bin tp dng sng tr nn phc
tp. Mt vn trong bin tp dng sng l mi mi trng m phng s dng mt tp cc
th tc khc nhau chnh sa dng sng, v vy khi chuyn sang mt mi trng m phng
mi i hi phi hc li mt tp cc th tc chnh sa dng sng mi.
Vn ny c th c gii quyt bng cch s dng testbenches ca Verilog. Mt
[Type text]
Page 277
GiaotrinhVerilogHDL_v19
testbenches Verilog l mt module c cha th hin ca MUT, p dng cc d liu kim tra vo
n v quan st u ra. Bi v testbenches l mt chng trnh Verilog nn n c th chy trn
nhiu mi trng m phng khc nhau. Mt module v testbenches tng ng ca n t mt m
hnh m phng trong MUT c kim tra vi cc d liu u vo ging nhau th s ging
nhau bt chp mi trng m phng.
9.1.1
Page 278
GiaotrinhVerilogHDL_v19
initial begin
#20 b=4b1011;
#20 b=4b1110;
#20 b=4b1110;
#80 oe=1b0;
#20 $finish;
end
always #23 f = f + 1;
endmodule
Cc bin tng ng vi cc u vo v u ra ca MUT c khai bo trong testbench. Cc
bin kt ni ti u vo c khai bo l reg, cc bin kt ni vi u ra c khai bo l wire.
Th hin ca alu_4bit s lin kt vi cc reg v wire cc b ti cc cng ca MUT.
Bin lin kt vi u vo ca module alu_4bit phi c khai bo gi tr ban u. p
dng ca d liu ti u vo d liu b v u ra cho php oe c khai bo trong cu lnh
initial. Trong 60ns u, sau mi 20ns mt gi tr mi c gn cho b. Khi initial sau i
80ns, v hiu ha alu bng cch t oe = 0. V sau i 20ns hon thnh m phng, i
20ns l s thay i u vo cui cng nh hng ti u ra.
p dng d liu ti u vo chc nng f ca alu_4bit trong khi lnh always. Bt u vi
gi tr ban u bng 0, f tng ln 1 sau mi 23ns. Cu lnh $finish trong khi initial kt thc ti
v tr 160ns, ti thi im ny tt c cc khi th tc ang hot ng dng li v qu trnh m
phng chm dt. Hnh 9.1 hin th kt qu qu trnh m phng module alu_4bit
[Type text]
Page 279
GiaotrinhVerilogHDL_v19
9.1.2
Page 280
GiaotrinhVerilogHDL_v19
thc ti 63ns. Thi gian nh vy l s la chn bao gm t nht mt cnh ln ca ng h,
v vy s ng b ca u vo rst c th khi to trong thanh ghi misr. u vo d liu d_in
bt u l x, v trong khi l 4b1000 khi rst l 1.
Ngoi cc khi khi to, module test_misr cn bao gm 2 khi always to d liu
cho d_in v clk. Clock cho ra mt tn hiu tun hon vi chu k 11x2=22ns. u vo misrd_in
gn mt gi tr mi sau mi chu k 37ns. gim trng hp cc u vo thay i cng
mt lc, chng ta s dng s nguyn t cho thi gian ca u vo mch tun t.
Hnh 9.2 m t mt kt qu ca testbench:
[Type text]
Page 281
GiaotrinhVerilogHDL_v19
9.2.1
9.2.2
iu khin m phng
Page 282
GiaotrinhVerilogHDL_v19
initial #189 $stop;
endmodule
Mc d, cu trc Verilog c s dng khc nhau, d liu v clock p dng cho MUT
trong testbench cng ging vi testbench bn trn. Tuy nhin, nu trnh m phng cho testbench
trc khng c ngt, hoc im dng, n s chy mi mi. Trong testbench ny gii quyt vn
ny bng cch thm vo mt khi khc khi initial s dng qu trnh m phng sau 189ns.
Cc task iu khin trnh m phng l $stop v $finish.
Thi gian u tin theo chu trnh ca mt khi th tc c tip cn nh mt task, trnh
m phng s dng li hoc hon thnh. Mt task $stop th c th ni li, nhng mt task
$finish th khng th ni li.
Mt testbench khc c m t nh sau:
V d 9.8
reg x=0, reset=1, clock=0;
wire z;
moore_detector MUT ( x, reset, clock, z );
initial begin
#24 reset=1b0;
module test_moore_detector;
#165 $finish;
end
always #5 clock=~clock;
always #7 x=~x;
endmodule
Trong testbench ny tch hp trong khi initial c tn hiu reset tch cc thp v tn hin iu
khin thi gian trong mt khi initial. Thi gian iu chnh chm dt qu trnh m phng ti
thi gian 189ns ging nh testbench bn trn.
9.2.3
Page 283
GiaotrinhVerilogHDL_v19
V d 9.9
module test_moore_detector;
reg x=0, reset=1, clock=0;
wire z;
moore_detector MUT ( x, reset, clock, z );
initial #24 reset=1b0;
initial repeat(13) #5 clock=~clock;
initial repeat(10) #7 x=$random;
endmodule
Testbench ny s dng $radom to d liu ngu nhin cho u vo x ca mch, cu
lnh repeat trong khi initial to ra tn hin clock 13 ln sau mi 5ns, v x nhn d liu ngu
nhin 13 ln sau mi 7ns. Thay v dng mt b tnh quyt nh d liu m bo tnh quyt
nh cc trng thi, th d liu ngu nhin c s dng y. Chin lt ny lm cho n d
dng hn to d liu, nhng phn tch u ra ca mch s kh khn hn, do u vo khng
on trc. Trong cc mch ln, d liu ngu nhin c s dng nhiu cho u vo d liu
hn tn hiu iu khin. Testbench trong phn ny s dng sau 70ns.
9.2.4
Cung cp d liu ng b
Trong cc v d trc testbench cho MUT s dng thi gian c lp cho clock v d liu.
Trong trng hp nhiu b d liu c p dng, vic ng b ha d liu vi ng h h
thng tr nn kh khn. Hn na, vic thay i tn s clock s yu cu thay i thi gian ca
tt c cc d liu u vo ca module ang kim tra.
Testbench sau c vit cho module moore_detector, s dng mt cu lnh iu khin
s ng b gia d liu p dng vo X vi clock to ra trong testbench. Tn hiu clock ny to
ra trong cu lnh initial s dng cu trc repeat. Mt cu lnh initial khc c s dng to
d liu ngu nhin cho X. Nh ta thy trong cu lnh initial ny, vng lp mi mi s lp i lp
li cu lnh ny s dng y. Vng lp ny i cho ti cnh ln ca clock, v sau khi cnh
ln ca clock 3ns, mt d liu ngu nhin c to ra cho X. Trng thi d liu sau cnh ln ca
clock s s dng bi moore_detector trn u cnh tip theo ca clock. K thut ny ca d
liu m bo rng s thay i d liu v clock khng trng nhau.
V d 9.10
module test_moore_detector;
[Type text]
Page 284
GiaotrinhVerilogHDL_v19
reg x=0, reset=1, clock=0;
wire z;
moore_detector MUT ( x, reset, clock, z );
initial #24 reset=0;
initial repeat(13) #5 clock=~clock;
initial forever @(posedge clock) #3 x=$random;
endmodule
C tr hon 3ns c s dng y lm n c th s dng testbench ging nhau trong m
phng sau khi tng hp thit k tt nh m t hnh vi trong cc phn trn.Trong qu trnh m
phng sau khi tng hp, m hnh cc thnh phn s thc s tr hon nh cc gi tr c s
dng, tr hon trong testbench cho php truyn tn hiu kim tra hon thnh trc khi p dng
tn hiu khim tra mi.
9.2.5
Tng tc testbench
Trong qu trnh to testbench tip theo chng ta s dng mt my trng thi khc. l
my trng thi moore pht hin chui 1101 vi trng thi bt u (start) v reset (rst) iu khin
u vo. Nt start l 0 trong khi tm kim chui 1101, my trng thi s reset v trng thi khi
u. Nh ta thy trong mch sau c 5 trng thi, v u ra ca n ln 1 khi n bt u trng thi
e.
V d 9.11
module moore_detector (input x, start, rst, clk, output z );
parameter a=0, b=1, c=2, d=3, e=4;
reg [2:0] current;
always @( posedge clk )
if ( rst ) current <= a;
else if ( ~start ) current <= a;
else case ( current )
a : current <= x ? b : a ;
b: current <= x ? c : a ;
c : current <= x ? c : d ;
d : current <= x ? e : a ;
e : current <= x ? c : a ;
[Type text]
Page 285
GiaotrinhVerilogHDL_v19
default: current <= a;
endcase
assign z = (current==e);
endmodule
Testbench cho my trng thi l mt testbench tng tc mt.
module test_moore_detector;
reg x=0, start, reset=1, clock=0;
wire z;
moore_detector MUT ( x, start, reset, clock, z );
initial begin
#24 reset=1b0; start=1b1;
wait(z==1b1);
#11 start=1b0;
#13 start=1b1;
repeat(3) begin
#11 start=1b0;
#13 start=1b1;
wait(z==1b1);
end
#50 $stop;
end
always #5 clock=~clock;
always #7 x=$random;
endmodule
Trong khi initial, testbench giao tip vi MUT. u vo X v clock c to bi 2 khi
always. Mt tn hiu c chu k lin tc c to ra cho clock v mt d liu c chu k ngu
nhin c gn cho x.
u tin, gi tr 0 v 1 c t cho reset v start a my trng thi vo trng thi bt
u. Theo sau , mt cu lnh wait i z ln 1 nh l kt qu p ng ca MUT ti gi tr
x v clock. Sau s kin ny, bin start c gn bng 0 v sau l 1 sau 13ns khi ng
li my. Theo sau vng kch hot u tin ny, mt cu lnh repeat lp li tin trnh bt u my
trng thi v x ln 1 ba ln na. Sau 50ns testbench dng qu trnh m phng bng mt task
[Type text]
Page 286
GiaotrinhVerilogHDL_v19
$stop.
9.2.6
end
endmodule
Sau khi t my trng thi vo trng thi running, testbench i 13ln hon thnh
xung clock trc khi n t li u vo start v hon thnh m phng. Nh ta thy, khi always
ng thi vi khi running lin tc to xung clock 5ns. Cng ng thi vi cc khi ny
[Type text]
Page 287
GiaotrinhVerilogHDL_v19
l mt khi always khc to d liu ngu nhin cho t, v s dng t tr hon lnh gn ngu
nhin cho x. C khi ny to d liu cho u vo x cho n khi cu lnh $finish trong khi
running c thc hin.
[Type text]
Page 288
GiaotrinhVerilogHDL_v19
9.4 K thut chn (assertion) dng kim tra thit k
Khng ging nh m phng vi mt testbench hoc con ngi gii thch kt qu,
trong k thut chn kim tra kt qu chng trnh gim st chu trch nhim pht hnh
mt thng bo nu c mt iu g xy ra khng nh mong i. Trong Verilog, cc trnh gim
st l cc module, v chng c khi to trong mt thit k kim tra cc thuc tnh ca thit
k. Th hin ca mt module chn (assertion module) khng c xem nh l mt module phn
cng. Thay vo , loi th hin ny ging nh mt th tc lun lun hot ng v lin tc
kim tra cc s kin trong module thit k.
Thit lp hin ti ca mt trnh gim st chn (assertion monitor) c sn trong mt th vin
c bit n nh l mt th vin kim tra m (OVL). Ngi thit k c th pht trin cc ci
t ring ca h vo trong module chn. Nhng g tn ti trong trnh gim st kim tra gi tr
ca tn hiu, quan h ca mt vi tn hiu vi cc ci khc, s tun t ca cc s kin, v cc
m hnh d kin trn vector hoc nhm tn hiu. s dng k thut chn, ngi thit k
bin dch OVL v th vin c sn trong thit k cn kim tra.
Khi mt thit k c pht trin, k thut chn s thay th cc im cn thit trong thit k
kim tra cc chc nng cn thit. Khi thit k c m phng nh mt thnh phn n
chun, hoc trong mt cu trc phn cp ca mt thit k ln, trnh gim st kim tra tn
hiu cho cc gi tr ngoi l. Nu tn hiu khng c gi tr ngoi l bng s gim st, trnh gim
st chn vo s hin th mt thng ip v thi gian s khc bit (vi phm ca cc thuc tnh)
xy ra. Thng thng, thng ip xut hin trong vng bo co ca m phng, bn dch hoc
khung hin th iu khin (console).
9.4.1
Cch tm ni chn mt trnh gim st sao cho c li nht s c tho lun trong phn
ny.
K lut thit k: Khi mt ngi thit k t mt ni chn trong thit k, ngi thit k
cn phi t yu cu bn thn mnh xem xt cn thn thit k v trch xut cc thuc tnh cn
thit.
Kh nng quan st: Chn thm cc trnh gim st vo cc im cn gim st ca thit k lm
sao cho d quan st nht.
Qu trnh kim tra chnh thc sn sng: Cc chng trnh chn tng ng vi cc thuc tnh
c s dng trong cng c kim tra chnh thc. C chn vo mt trnh gim vo mt thit
k, sn sng cho n kim tra bng cng c kim tra thit k chnh thc.
[Type text]
Page 289
GiaotrinhVerilogHDL_v19
Thc thi ch thch: Trnh gim st chn vo c th xem nh l ch thch ch thch mt
vi tnh nng hoc hnh vi ca thit k. Cc ch thch ny to ra mt thng ip khi
hnh vi ca chng c gii thch l vi phm.
T thit k kn: Mt thit k vi k thut chn gim st m t thit k v cc th tc
kim tra n trong mt module Verilog.
9.4.2
Page 290
GiaotrinhVerilogHDL_v19
i s ng khc. Cc i s ng ny l cc cng ca module v cng c xem nh l cc
cng ca assertion module.
9.4.3
assert_always
C php s dng k thut chn gim st c m t nh sau:
C php 9-2
assert_always
#( severity_level, property_type, msg, coverage_level )
instance_name ( clk, reset_n, test_expr )
Lnh ny s lin tc chn kim tra test_expr chc chn n lun lun ng trn mi cnh
ca clock. Nu biu thc kim tra sai, mt thng ip tng ng s c hin th.
V d 9.13
module BCD_Counter (input rst, clk, outputreg [3:0] cnt);
always @(posedge clk) begin
if (rst || cnt >= 10) cnt = 0;
else cnt = cnt + 1;
end
assert_always #(1, 0, Err: Non BCD Count, 0)
AA1 (clk, 1b1, (cnt >= 0) && (cnt <= 9));
endmodule
Testbench kim tra module:
module BCD_Counter_Tester;
reg r, c;
wire [3:0] count;
BCD_Counter UUT (r, c, count);
initial begin
r = 0; c = 0;
end
initial repeat (200) #17 c= ~c;
initial repeat (03) #807 r= ~r;
endmodule
assert_change
[Type text]
Page 291
GiaotrinhVerilogHDL_v19
Lnh chn ny gim st kim tra trong mt s chu k ng h sau s kin bt u, biu thc
kim tra s thay i, c php
c s dng nh sau:
C php 9-3
assert_change
#( severity_level, width, num_cks,
action_on_new_start, property_type,
msg, coverage_level )
instance_name ( clk, reset_n, start_event,
test_expr )
assert_one_hot
K thut chn gim st ny dng kim tra ch mt bit trong n bit ca biu thc kim
tra l 1 trong khi trnh gim st vn ang hot ng, c php nh sau:
C php 9-4
assert_one_hot
#( severity_level, width, property_type,
msg, coverage_level )
instance_name ( clk, reset_n, test_expr )
assert_cycle_sequence
K thut chn ny dng kim tra my trng thi, c php nh sau:
C php 9-5
assert_cycle_sequence
#( severity_level, num_cks, necessary_condition,
property_type,
msg, coverage_level )
instance_name ( clk, reset_n, event_sequence )
[Type text]
Page 292
9.5 Bi tp
1. Ti sao phi kim tra thit k? C my cch kim tra thit k, nu c th?
2. Phn bit hai hnh thc kim tra thit k s dng testbench v verification?
3. Cc k thut to mt testbench hiu qu ?
4. Vit testbench cho mch t hp v mch tun t c g khc nhau?
5. Th no l chn kim tra thit k (assertion verification)? Li ch ca k thut ny?
6. Th vin mi OVL bao gm nhng cu lnh chn c bn no v cc s dng chng?