Trang
qua
trll1h
m6
phong. phcp
gan
thu
nhat xac
djnh
thaa
tac
gan
gia
tri '0'
cha
lin
hi9U S sau 10
ns
tfnh til Ihoi die:m
m6
phung
hi~n
thai.
Trang
khi d6,
phcp
gan
Ihu hai
se
d~1
cha
tin hi¢u S
gia
tr!
'I'
sau 5
ns
tfnh
tu
thai
diem
1\10
phong
hi¢n thoi. Nhlf v(ly. Irong
qua
trlnh rn6 phong,
dc
thaa
t,te thlfc
hi~n
phcp
gall
tht.'f
haL
se
de
len
de
than
lac
clla phep
gan
thl1
nhat va qmt
tdnh
tren se Wong
t.'rng
v6"i
vi¢c gall
eho
tin
hi~u
S
gia
trj
'I'
sau 5 ns tinh tlr
thai
die·m rna
phong
hi~n
thai.
Dc
chi r6 W
klul.c
bi¢1
giiJa
phep
g,il1
tin
hi~u
\'~I
phep
gan
bien. eh{lllg
ta
hay
xct
vi
til.l
dOl.1Il
chuong
trlnh sau,
signal S
1.
eLK: nIT ;
Main:
process
begin
SI <='1';
SI
<= '0';
wait until eLK'event
and
ClK
= 'J';
end process Main;
Trong
do,~n
clll.rang
tdnh
nay.
c;'le
diu
l~nh
gan
gill til
'I'
va
'0'
cho
tin
hi¢"ll
SI
dl.IC)"e
d(lt
tru6"c
cau
1~l1h
wait. Phcp
gan
Ihu
hai sc
I()(~i
bi) tlic
d¢ng
clla
phep
gan
thl1
nhat va
do
d6
tin hi¢u
SI
se
duqc
gan
gia
tr~
'0'
sau khi
qua
trlnh
mo
phung
th\.fc
hi¢n
l~nh
wait.
Trong
dOi.~n
chuang
trlnh tiep
thea
dU(1i
clay,
signal
S1,
S2:
integer;
Main: process
begin
SI
<=
5;
wait until CLK'event
and
ClK
='1';
SI
<=
Ill;
S2 <=
SI;
wait until CLK'cvent
and
eLK
= '1':
end process
Main;
thaa
tAc
gan
gili
let
'10'
cha
tin hi¢u
SI
duqc
dinh
tru6"e
\'6"i
thai glall Ire
delta
tfnh
Ilrthai
diem
ma
phong
hi¢n
t~li.
Trong
khi
do
phep
gan
gia
trj hi¢n
thai
eua
SI
( bang
'5'
)
cha
tin hi¢u S2
dut;1e
dinh
tru6"e
v6"i
tbai
gian
Ire
177
delta
tinh
tir
thai
di~m
mO'phong hi¢n
t<,l.i.
Nhu
v'~y.
sau
cau
I¢nh
wait
thu
hai,
cac
tin hi¢u SI va
S2
se
dm;)'c
gan gia tr! la '10' va
'5'
rn¢t
each
tuang
Lrl1g.
Di~u
do
co
nghia la tin hi¢u
S2
se
nh~n
duCJc
gia trt bang
'5'
- gia
tTj
eua
SI
twoc
khi
phep
gan
"SI
<=
10;"
duCJc
thlfc hi¢n.
Chung
ta xct them
m(>t
vi
dl;!
nua
voi
dO':ln
chuang
lrinh sau,
si~nal
S2:
integer;
Main:
process
begin
variable
VI:
interger;
VI
:~
10;
S2<~Vl;
wait
until
CLK'event
and
eLK
= 'I':
end
process
Main;
Trong
vi dy nay, kili phep gan
"VI
:=
10;"
duCJc
thy·c
hi¢l1.
gia tr, 10 se duqc
gan
cho
bien
VI
In¢t each tuc thai
t~i
thai di6m rna ph6ng hi¢n ".li. Sau d6
thao tac gan gia trj
cua
VI
eha
tin hi¢u S2 se
dL1CJc
djnh Ijch trlnh va sau khi
di.u I¢nh
wait
duCJc
tht!c hi¢n. tIn hi¢u
S2
se
duCJc
gan gia tr!
bang'
10'.
3.
Cau
I~nh
if
Ci1u
J¢nh
if
t<;1o
nen
phfUl
nh<.lnh
(rang khi tlwc hi¢n
chuang
trinh. Tuy
thea
ket
qua
cua
bicu thuc dieu
ki~n
ma
co
tl1l~:
ho~c
rn¢t
so
I¢nh
ho~e
kh6ng
col¢nh
nao
se
duCJc
thlfc hi¢n. Cilu I¢nh if
co
cau truc cu phap
nhu
saU:
178
if < dicill_ki¢n >
then
{<
ciiu)¢nlt_wclll_tl/
> J
!
elsif
< die'lf_
ki¢1I
>then
J
{<
Ciill_fel11!
_fudn
tit> J
[else!
<
call_I¢lJh_flldn_t~(
>
J]
end
if;
Trang
do, <
dill{
hell>
Ii'!.
bie:u
(huc
kitu
boolean.
Twng
ffi('ii nhanh eua loan
tir
if
co
th~
chua m(lt
hQ(~c
nhi6u cau l¢nh
tu,'ln
II!.
Dau tien
bi~u
'hue
di~u ki~n
(hrqe 'fnh to,in, neu
ke",
qu,\
ellO
gia trj
true,
cae
eflu
l~nh
Imin tl! narn sau
IU
khoa
then
sc
dueye
thl!e
hi~n
tue thai. Trong
truong hqp nguqe
l'.ti,
ne'u
bicu thue dieu
ki~n
sau
IU
kh(ia
elsif
eho gia Irj
true.
cae cau l¢nh
luGn
tl! sau
lU
khoa
then
tiep theo sc
dw~e
thlfC
hi¢n,
vv.
Drn.m
chuang trlnh du6i day su
d1,lng
cau l¢nh
if
d~
rna
t,-l
phan tu AND vbi
hai dau vao:
signal
Input
1,
Input2. Output: STD _LOGIC:
And_process: process ( Input
I,
Input2
);
begin
jf Input 1
= '0'
or
Input2 = '0'
then
Output
<=
'0';
elsif
Input I = 'X'
or
Inpul2 = 'X'
then
Output <= 'X';
else Output
<=
'1':
end
if
end
process;
4.
Call
l~nh
Case
Crill
l¢nh
case
duqe su
d~ng
trong truong
heyp
c6
tnQI
bitu
thue M kiem
,>o,it
nhicu
re
nhanh lrong
chu"0ng
tdnh
YHDL. Cae
l~nh
lUang
I1ng
voi
t11Qt
1rong cae il!a ehQn sc
dUQ'c
thl!c hi¢n
n2'u
bicu thue ki6m soli! co gi,j Irj
bill1g
gia
Ir!
luang ung eua
11,l'a
eh(:)I1.
Cau I¢nh
case
co
C<lu
true eli phap nhu' sau:
case
< hilll {llIrc >
is
when
<
h/il_ch()I1
>
=>
{<
rdll_h'lIh
IlIdn
fl(>!
{
when
< 1(/"il_ch()JI >
=>
{<
('tlll_'fnh
_WGII_fl/
>}
end
case;
179
Trong
d6,
< bit/II
~"lIrc
> sau khi tinh phai eo
ki~u:
nguyen,
kie:u
Ji~t
ke
hO{le
mang
m9t
ehicu
de
ky
tl,l
nhLl
BIT
~
VECfOR;
< ft/(l C/l(JII >
ho~e
III
nhung bicu th(rc
eo
djnh ( vi
dl,l
nhLl
hung
so
)
hO(lC
mOt
khoang
eo
dinh;
moi gill trj trong mien
xac
djnh eua <
bi("11
,Mer>
phJ.i
ctuqc phu boi
mOt
va
chi
ll1(lt
<
h.r(J~('h{Ji/
>;
<
h.t(I_("h~J/I
>
euai
eung eo the la
others.
Tu
kh6a
others
lUang (rng v6i
tAt
c,i eae trui:mg h9P eon
h~i
eua
<
h.ltl~ch~m
> trong mien xiie dinh ella hieu
th(re kicm soaL
D6i
voi
cae
< It/aJ!/(:m >, ngoll ngu
VHDL
dLl<l
ra
cac
gi6i
h,~n
sau:
180
<
fl/(J~ch{)//
> phili e6 ellng kieu voi <
bi£"JI~tllll"C
>.
m6i gia trj clla < hi/II ,llIr( >
ph"l.i
dUge
bicu dicn bang
1l19t
va
chi
mQt
<
l{la~cl/(:m
>.
neu khong e6
h!a
ehQn
others
thl
mQi
gia
tri
e6 the co
dUQ"c
ella
<
biell~tlll(c
>
phii
dUQ"e
phu bai tat c.i
dc
khii
nang
e6
the
C()
eua
<
h{a
chol1
>.
Vi
dl,l
Slr
dyng
cau l¢nh
case,
signal
S1:
integer
range
0 to 7 ;
signal
II,
12,
D : BIT ;
selcct~proeess:
process
( S
1,
II,
12,
13
)
begin
case
SI is
when
0 I 2
=>
OU
<=
'0';
when
1 =>
OU <= II ;
when
3 to 5
=>
OU <=
12;
when
others
=>
OU<=B;
end
case;
end
process
seJeecproecss;
5.
Cau
I~nh
rbng
null
Clu
l¢nh
rang
c6 eli rhap
nhu
SilU:
null;
Trang
ngon
ngU
VHDL, khi
chuang
trinh
InO
ph6ng
g~p
CflU
l¢nh null,
no se
he,
qua
l¢nh nay va
thlfC
hi¢n
l~nh
tiep sau.
Thong
thuong. cau I¢nh
null
dung
d~
chi
truang
hqp kh6ng thlfc
hi~n
I¢nh
InQt
eel.ch
luang
mLnh
khL
co
d.c
dicu
ki~n
tn\.
l<,li
gia
Ir!
true.
Do do,
C,lU
l¢nh null
thLfong
dW;K
dlmg
trong
dc
cau
l~nh
case dni vdj nhung
h.ra
chQn kh6ng
can
thao
taco
V'd1.:l.
variable
Sel :
integer
range
0 to 31;
variable
V :
integer
range
0
to
31;
ease Sel is
whenOtu
15=>
V
:~
SeI;
when
uthers
=>
nulL
end
case;
6.
Cae
I¢nh v6ng
I~p
L~nh
qp luop
chua
than vong
1~p
baa
g6m
day
de
c1u l¢nh
sc
dU,!e
Ihl,l'C
hi¢1l
kh6ng
ho~c
nhieu hin.
au
l~nh
loop
c()
quy
It
I<
.l
phap
nhu
SilU:
[< nhdn >:] l<
sU_dd_/(1p
>] loop
{ <
/i'Il/1
Wan
tlf
>}
I
. -
-'
{next [<
!l1J(11I
> ] [","hen < diell_
ki{'JI
>]; } I
I exit [<
111/(/11
>]
[when < die'lI_
ki{;/1
>];}
end
loop [l1l1all ];
<
1111(111
>:
nhii.n
ella vong
Ii).p
va
thuang
dUQ'e
dung
de xay
dl,l'r1g
nhiing \'ong
l~lP
16ng nhau. trong
1.16
mOL
vang
l~p
ducyc
ket
Ihue
b6'i
tu
khoa
end
loop;
181
<
sa.
dr)'
ItJp
>:
trong ngon ngu VHDL co m9t so
d~ng
vong
l~p
vai cae
sa
do
l~p
khae nhau
nhl1:
vang
l~p
v6i
sa
d6
l~p
for, vong
1~lp
while,
\'a
vong
l~p
khong ehua
sa
do
l~p.
Vai nhiJng vong
h}p
kh6ng chua <
S(I
do
l<lp
>,
cae
l¢nh trong day
l~nh
tu[in
1If
sc
du<,1c
thuc hi¢n cho toi khi
duq'c
ngit
bbi cau
l~nh
exit. Trong
ng6n ngu
VHDL
cau l¢nh next eung c6
th€::
duqc dung
de
thay
obi
tdnh
tt!
th~fC
hi~n
than ella vong
l~p
(giong
cau l¢nh
continue
trong ng6n ngu
C).
Vi
dl;!,
Count_down:
process
variable
Min,
Sec
:
integer
range
0 to 60;
begin
LI : loop
L2:
loop
exit L2
when
(
Sec
:::;
0 ) ;
wait until CLK'event
and
CLK =
'I
';
Sec
:=
Sec -
I;
end
loop L2;
exit
L 1
when
( Min:::; 0
);
Min:=
Min -1;
Sec := 60;
end
loop
Ll;
end
process
Count_down;
Yang
Il~p
chua < sd_dr)'_I(/p >
d~ng
for
la m¢t
di.wg
khac cua vong
b~p.
Yang
l(lp
for
la
du
I¢nh tuan
It.!
nam trong qua
tdnh
process, va cho phep
Ihan cua \'ong
l~p
tht!c hi¢n theo
so
luqng xae djnh cae Ian
l(lp.
V f
dl:\,
for
I
in
1 to
10
loop
Lsquare
(
I)
::::;
I *
I;
end
loop;
Bien dicu
khd::n
\'ong
l~p
1 kh6ng ctin thiet ph,ii khai hao tnrac
va
kh6ng the
dl1q'C
gan gia trj trong
th[\I1
vong
l~p.
Trang nhung tru0ng hqp de chi ra dmg
bien
l~p
nh~n
khoang lui. chung ta co the
si'r
dl;!ng
tlr
khoa
downto.
Vi
dL.1.
182
for I
in
X
downto
Y loop
Csquare ( I )
:~
I * I;
end
loop;
SCI
do
l~p
",'hile la
sa
do
l~p
trong do qua trlnh
l~p
dllQ'C
thlfe hien neu
bieu thuc dieu ki¢n
I~p
nh(tn gia tr!
true.
Vang
l~p
sc
dung
li,li
khi gi.i
tr!
ella
bitiu thuc dicu ki¢n
trO'
thanh false
hoi!.c
qua trinh
th,!C
hi¢n than vong
l~p
g~p
l¢nh exit. Cling wang
tl,C
nhu vong
l~p
for, cau l¢nh
next
cung co
th~
dllQ'c
dung M thay doi
tr~t
11,C
l~p.
VI
dl:l.
process
variable
A,
B.
C,
D:
integer;
begin
while ( ( A + B ) > ( C + D ) ) loop
A:~A-I;
c:~
C +
B;
next when
(B
<
10);
B:~
B - D;
end loop:
end
process:
86i
v6i cac vong
l~p,
trong cac nhanh chua phep gan tin hi¢u phai co
it
nhat
lTI(lt
du
l¢nh wait. Neu khong th6a man (lieu
ki~n
nay, qua trinh mll
ph6ng c6
the:
khac tli. Chung ta hay xct
vi
dl:l
dm.l11
cJ1llCfIlg
trlnh sau,
signal
S:
integer
range
0 to
10;
process
variable
I:
integer
ran~e
0 to
10;
begin
wait
until
( CLK'event
and
CLi\
=
'0'
);
I:~
0;
while ( I <
\0
) loop
S
<~
I;
1:~I+l;
end
loop;
end
process;
lR3