Professional Documents
Culture Documents
Hardware Modeling - 1 - 2
VHDL l g?
Mt t vit tt cho mt t vit tt khc, VHDL l t vit tt ca VHSIC Hardware Description Language
Hardware Modeling - 1 - 3
Chun IEEE 1076 rt hon chnh cho vic m hnh ho thit b, nhng n mi ch nh ngha cc tham s khi qut cho vic tng hp thit b
Kt qu: mt m hnh phn cng cho khng hn ph hp vi mt thit k mc cng logic c thng qua cc cng c v cc cng ngh ch (target) khc nhau
Hardware Modeling - 1 - 4
Chun ho VHDL
T chc IEEE chnh thc ph chun chp nhn ngn ng VHDL nh l mt chun ca h vo nm 1987, chun IEEE 1076
Sa i u tin c thc hin nm 1993, v VHDL-93 hin nay c coi l phin bn chnh thc ca ngn ng ny, hin nay bt u xut hin VHDL 200X
Tuy nhin, hu ht cc cng c (tool) u h tr phin bn u tin (VHDL-87) Cc b phn ca VHDL 200X c h tr bi mt s tool
VHDL-2X VHDL-93
VHDL-87
Hardware Modeling - 1 - 5
Language Subsets
Khng phi tt c cc cu trc VHDL u c th tng hp c. V d, wait for 10 ns l mt cu trc m hnh ho thng dng, nhng n khng tng ng vi v cng khng th to ra mt phn t mc cng logic
Hardware Modeling - 1 - 6
Behavioral
RTL
AND_OR2
DFF
Logic
Layout
CLB_ R5C5
CLB_ R5C6
Hardware Modeling - 1 - 7
Behavioral
Hardware Model
Sum <= A + B ;
RTL
Synthesizable Code
component Xlx_add2 port ( A: in bit ; B: in bit ; Sum: out bit ); end component ;
Logic
Layout
Hardware Modeling - 1 - 8
Hardware Modeling - 1 - 9
Nguyn tc phn on
Khi tin hnh phn on mt thit k trong VHDL, cn lu tm cn nhc mt s cc im chnh sau
Ph hp vi cu trc m ngun dng RTL Quan trng i vi cc rng buc v thi gian, Important for timing constraints, ti u ho cu trc mch Hu ht cc cng c u khng ti u thng qua bin gii hierarchy
Hardware Modeling - 1 - 10
SUM [3:0]
B[3:0]
C_in C_out
A B C_in
entity Full_Add
Sum
C_out
Macro
A B
entity Half_Add
Sum Carry
Leaf Cell
Hardware Modeling - 1 - 11
Khi s dng VHDL, c th thc hin cc bc kim tra thit k, bt u t Behavioral Simulation
Behavioral Simulation (Testbench driven)
VHDL modules
VITAL
Synthesis
Hardware Modeling - 1 - 12
Hardware Modeling - 1 - 13
Chip level Board level Std parts model Model bus operation Discrete event-driven Flexibility over strictly netlist-driven
Behavioral Module
MCU
FPGA
Memory
PLD
Hardware Modeling - 1 - 14
Kt lun
VHDL l mt ngn ng dng m hnh ho phn cng ca thit b Tng hp logic l mt tp con ca ton b ngn ng Cc vn v cng c v cng ngh c nh hng n vic tng hp logic ca mt thit k Cc m phng HDL c th bao gm cc d liu nh thi (back-annotated timing data)
Hardware Modeling - 1 - 15
Bi 2
Ngn ng VHDL
Hardware Modeling - 1 - 16
Hardware Modeling - 1 - 17
Primary
Secondary
Mi khi c thay i trong primary design unit, cn phi kim tra li secondary design unit. Nu khng, chng trnh s c li. Cc secondary unit khng th tn ti c lp -- tc l, phi ph thuc primary unit
Hardware Modeling - 1 - 18
Entity
Entity m t external interface ca thc th c thit k, cng cc thuc tnh lin quan vi interface
entity Half_Add is
Sum Carry
Hardware Modeling - 1 - 19
Architecture
Note: VHDL93 cho php s dng reserved word architecture sau reserved word end
Hardware Modeling - 1 - 20
Multiple Architecture
Chng c th biu din cc giai on khc nhau ca qu trnh thit k hoc cc cch tip cn khc nhau cho cng mt chc nng (optimization for speed verus area, etc.)
entity Cnt64 is ... end Cnt64 ; architecture BEH of Cnt64 is ... end BEH ; architecture RTL of Cnt64 is ... end RTL ; architecture XLX of Cnt64 is ... end XLX ;
entity Half_Add is ... end Half_Add ; architecture BEH of Half_Add is ... end BEH ; architecture RTL of Half_Add is ... end RTL ; architecture XLX of Half_Add is ... end XLX ;
Hardware Modeling - 1 - 21
Package
Mt package declaration c dng khai bo cc d liu dng cho ton b thit k, bao gm:
package My_Pack is
constant. . . ... function. . . ... component . . . ... subtype. . . end package My_pack ; library IEEE; use IEEE.std_logic_1164.all ; ... use work.My_Pack.all ;
entity . . .
Hardware Modeling - 1 - 22
Package Body
Mt package body l mt dependent unit ca mt package, n cha cc thng tin chi tit v cc i tng trong package
package My_Pack is
constant. . . declaration ... function bv_to_integer ( ... details component . . . ... subtype. . .
end My_Pack ;
function bv_to_integer (BV: bit_v.. return integer is variable begin for index in BV'range loop ....
... end My_Pack ;
Hardware Modeling - 1 - 23
Library
trong cc th vin
thit k (design unit) c kim chng Work v std l hai th vin dng c cho mi design
unit
library IEEE ; package std_logic_1164 is.. package std_logic_unsigned is.. package std_logic_arith is..
Hardware Modeling - 1 - 24
Library
Libraries v Packages comprise the VHDL Design Management structure Theo mt ngha no , n tng t nh directories v
subdirectories
library IEEE ; package std_logic_1164 is.. package std_logic_unsigned is.. package std_logic_arith is..
Hardware Modeling - 1 - 25
Khi to cc Library
Chun VHDL khng quy nh cc yu cu cht ch v cu trc ca library, do vy chng khng d c chuyn giao gia cc tool
library My_Lib
Hardware Modeling - 1 - 26
Khi to cc Library
library My_Lib
Hardware Modeling - 1 - 27
Work Library
y l th mc con (sub-directory) mc nh c dng lu gi tt c cc n v thit k c bin dch (compiled design units), tr khi c cc ch nh khc. Mi mt cng c m phng hoc tng hp u s to ra mt cu trc nh th
Hardware Modeling - 1 - 28
Work Library
Mt v d v ni dung ca work library
Design Unit entity entity entity package architecture architecture Identifier HALF_ADD DFF REG4 My_Counters. . . RTL STRUCTURAL
Hardware Modeling - 1 - 29
V d v Hierarchy : DFF
entity DFF is port (D, Clock : in std_logic ; Reset : in std_logic ; Q : out std_logic) ; end entity DFF ; architecture RTL of DFF is begin process (Clock, Reset) begin If (Reset = 1 ) then Q <= 0 ; elsif (Clockevent and Clock = 1) then Q <= D ; end if ; end process ; end architecture RTL ;
Clock
Reset
Hardware Modeling - 1 - 30
V d v Hierarchy : REG-4
entity REG_4 is port (D_in : in std_logic_vector (3 downto 0); Clk, Rst : in std_logic; Q_out : out std_logic_vector (3 downto 0)); end REG_4; architecture Structural of REG_4 is component DFF port ( D, Clock : in std_logic ; Reset : in std_logic; Q : out std_logic ) ; end component ; begin U3 : DFF port map (D_in(3), Clk, Rst, Q_out(3)); U2 : DFF port map (D_in(2), Clk, Rst, Q_out(2)); U1 : DFF port map (D_in(1), Clk, Rst, Q_out(1)); U0 : DFF port map (D_in(0), Clk, Rst, Q_out(0)); end Structural;
REG_4 D_in(3)
DFF U3
Q_out(3)
D_in(2)
DFF U2
Q_out(2)
D_in(1)
DFF U1
Q_out(1)
DFF U0
Q_out(0)
Hardware Modeling - 1 - 31
Lin kt Tn hiu
Lin kt theo v tr: Cc tn hiu mc cao c lit k theo ng trt t ca cc cng (port) mc thp trong component declaration U1: DFF port map (D_in, Clk, Rst, Q_out) ;
component DFF port (D, Clock : in std_logic ; Reset : in std_logic ; Q : out std_logic ) ; end component ;
Hardware Modeling - 1 - 32
Lin kt Tn hiu
Lin kt theo tn: Cc cng (ports) v tn hiu (signals) c lit k mt cch r rng, y , khng ph thuc trt t (strongly recommended) U1: DFF port map ( D =>D_in(1), Clock =>Clk, Reset =>Rst, Q =>Q_out(1)) ;
component DFF port (D, Clock : in std_logic ; Reset : in std_logic ; Q : out std_logic ) ; end component ;
Hardware Modeling - 1 - 33
Signal Declaration
Top
Sub_A I1 I2
A4 A1 A2 A3
Sub_B
Bus_1
B1 B3 B2 B4
Sig_1
O1 O2
begin U0 : Sub_A port map (I1, I2, Sig_1, Bus_1) ; U1 : Sub_B port map (Bus_1, Sig_1, O1, O2) ; end Structural ;
Hardware Modeling - 1 - 34
C th ho phn t
entity REG_4 is port (D_in : in std_logic_vector (3 downto 0) ; Clk, Rst : in std_logic ; Q_out : out std_logic_vector (3 downto 0)) ; end REG_4 ; architecture Xilinx_Struct of REG_4 is component FDC port (D : in std_logic ; Clock, Reset : in std_logic ; Q : out std_logic) ; end component ; begin U3 : FDC port map (D=>D_in(3), Clock=>Clk, Reset=>Rst, Q=> Q_out(3)) ; U2 : FDC port map (D=>D_in(2), Clock=>Clk, Reset=>Rst, Q=> Q_out(2)) ; U1 : FDC port map (D=>D_in(1), Clock=>Clk, Reset=>Rst, Q=> Q_out(1)) ; U0 : FDC port map (D=>D_in(0), Clock=>Clk, Reset=>Rst, Q=> Q_out(0)) ; end Xilinx_Struct ; D_in(0) Clk Rst
Component instantiation from target library may be helpful for chip level optimization ( i.e., Xilinx Virtex )
D_in(3)
FDC U3
Q_out(3)
D_in(2)
FDC U2
Q_out(2)
D_in(1)
FDC U1
Q_out(1)
FDC U0
Q_out(0)
Hardware Modeling - 1 - 35
S dng Generics
Generics l cc tham s c th cp nht ng (thay i gi tr) trong tng cu lnh c th ho phn t ca thit k (component instantiation)
entity My_Cntr is generic (Count_Width : integer := 8 ); port ( Data_In: in std_logic_vector (Count_Width -1 downto 0); Clk, Reset, Load, UpDn : in std_logic; Q_Out: out std_logic_vector (Count_Width -1 downto 0)); end entity My_Cntr;
Lab
Marker
Hardware Modeling - 1 - 36
Cp nht gi tr ca Generics
S dng mt generic map cng vi port map, khi c mt phn t c c th ho u trong thit k
library IEEE; use IEEE.std_logic_1164.all; use IEEE.std_logic_unsigned_all; use IEEE.std_logic_arith.all; entity MY_TOP_DESIGN is port ( DATA_BUS: in std_logic_vector (63 downto 0); CLOCK, RST, LD, CNTRL : in std_logic; DATA_Out: out std_logic_vector (63 downto 0)) ; end entity MY_TOP_DESIGN;
component My_Cntr generic ( Count_Width : integer := 8 ); port ( Data_In: in std_logic_vector (Count_Width -1 downto 0); Clk, Reset, Load, UpDn : in std_logic; Q_Out: out std_logic_vector (Count_Width -1 downto 0)); end component ; begin U0: My_Cntr generic map (Count_Width => 64) port map (DATA_BUS, CLOCK, RST, LD, CNTRL, DATA_OUT ) ; end architecture RTL;
Hardware Modeling - 1 - 37
Design unit c kim tra li c php. Sau khi hon tt, n s c lu gi work directory Cu trc hierarchy ca thit k c dn tri bt u t mc cao nht. ng vi mi sub-module c th ch c mt copy duy nht c to ra Dng m t netlist ca thit k c to ra hoc theo format chun cng nghip hoc theo mt vendor-specific format M hnh c m phng theo cc bc thi gian gin on. N c iu khin bi cc s kin cc tn hiu ng b qu trnh
Hardware Modeling - 1 - 38
...
architecture..
Analyze
Execute
Elaborate
Synthesize
Hardware Modeling - 1 - 39
Do mi quan h gia cc primary v secondary design unit cng nh kh nng c th c th ho cc module mc thp hn, nn qu trnh bin dch lun tun theo mt trt t nghgim ngt Cc entity phi c phn tch trc cc architecture tng ng vi chng Cc package cn phi c phn tch trc cc package body Mi mt module cn phi c phn tch trc khi n c tham tr bi cc module khc
Hardware Modeling - 1 - 40
Comments
Comments (ch thch) c tc dng lm cho m ngun tr nn d hiu hn, iu ny cng tng t nh cc ngn ng khc - - Comments bt u bng hai du gch ngang - - Chng ch tip tc cho n ht dng - - Mt comment trn nhiu dng cn phi dng - - hai du gch ngang trn tt c cc dng
A_OUT <= 1 ; - - Comments c th vit t y
Hardware Modeling - 1 - 41
Kt lun
VHDL bao gm cc primary v secondary design unit Trt t bin dch chng trnh VHDL c thc hin theo mt quy lut cht ch Tt c cc design unit c kim chng c lu trong mt th vin gi l work library C th s dng Generics tng tnh linh hot ca m ngun (source code)
Hardware Modeling - 1 - 42
Bi 3
Hardware Modeling - 1 - 43
Data Types
Data types l mt yu t quan trng trong VHDL (cng nh trong cc ngn ng khc)
Mi mt kiu d liu ch cho php nhn cc gi tr trong mt gii nht nh Mi i tng (signal, variable, constant, hoc port) cn phi c kiu d liu nht nh khi c khai bo (declared) Cc tn hiu lin kt vi nhau cn phi c cng kiu
Trong VHDL c nhiu kiu d liu cho php m t phn cng v kim tra li nhm m bo s tng thch tn hiu trong cc m hnh ln v phc tp
Cn tun th quy tc kim tra kiu trong cc m hnh c hnh vi (behavioral) v mc cng (gate-level)
Hardware Modeling - 1 - 44
Signals v Ports
Kiu d liu v rng bus cn phi ph hp trong cc lnh gn tn hiu (signal) v cng vo ra (port)
entity REG_4 is port (D_in1 : in std_logic_vector (3 downto 0); Cntrl : in std_logic_vector (1 downto 0); Clock, Reset : in std_logic; Q_out : out std_logic_vector (3 downto 0)); end entity REG_4; signal signal signal signal A : integer ; B : bit ; C : integer ; D : std_logic ;
Hardware Modeling - 1 - 45
Scalar
Single value object, defined indices, ordered Group objects, similar or different types Defines pointers to objects covered in Advanced VHDL course Sequence of objects of given type covered in Advanced VHDL course
Composite
Access
File
Hardware Modeling - 1 - 46
Bit Boolean Integer Real Physical Character Std_logic and std_ulogic Enumerated
Hardware Modeling - 1 - 47
Bit v Boolean
Va m hnh ho phn cng, nhng n khng cho php m t cc gi tr high-impedance, unknown, dont care, v.v...
architecture BEHAVE of MUX is signal A,B,Sel, Z : bit ; begin if Sel = 1 then Z <= A ; else Z <= B ; end if . . .
Thch hp cho vic m hnh ho mc khi nim l thuyt if Sel =1, if F >= G..
type boolean is (false, true) ;
both yield boolean result
Hardware Modeling - 1 - 48
Integer v Real
Cn phi quy nh gii gi tr cho cc kiu nguyn (integer), nu khng n s s dng gii gi tr mc nh vi s nguyn 32-bit signal B : integer range 15 downto 0 ;
Hardware Modeling - 1 - 49
Physical
Kiu vt l (Physical type) c dng nh lng cc i lng vt l nh l khi lng, di, thi gian Kiu vt l c nh ngha theo n v c s ca n
Bt k n v dn xut no cng l bi ca n v c s
type time is range 1 to 1000000 units Time l kiu vt l duy nht c nh ngha trong VHDL. N rt cn thit cho fs; vic m phng thi gian tr v cc tham ps = 1000 fs; s khc c lin quan thi gian. ns = 1000 ps; constant Tpd : time := 3ns ; us = 1000 ns; ... ms = 1000 us; . . .
Z <= A after Tpd ;
Hardware Modeling - 1 - 50
Std_logic v Std_ulogic
Std_logic c pht trin t h thng MVL (MultiValue Logic) v c dng m hnh ho phn cng mt cch chi tit hn so vi kiu bit
H tr cc mc gi tr khc nhau ca tn hiu: don't-care conditions, unknown, three-state c nh ngha trong gi IEEE std_logic_1164 U, -- Uninitialized X, -- Forcing Unknown 0, -- Forcing Zero Recall: type bit is 1, -- Forcing One Z, -- High Impedance limited to (0, 1). W, -- Weak Unknown L, -- Weak Zero H, -- Weak One - -- Dont Care );
type std_ulogic is (
Hardware Modeling - 1 - 51
S khc nhau ch thc hin mch (implementation) Ch u trong ulogic c ngha l unresolved
Nu ngi thit k s dng hai hay nhiu tn hiu iu khin mt ca ra chung th anh ta phi dng mt hm resolution (c trong gi ieee_std_1164) quyt nh trn thc t tn hiu no c a ti ca ra Std_ulogic khng c kh nng , nhng n to ra mt cng c ni ti kim tra cc li wired-oring do s sut
signal A,B,C,Res_Out : std_logic ; signal Out_1 : std_ulogic ; Out_1 <= A ; Out_1 <= B ; Out_1 <= C ; Res_Out <= A; Res_Out <= B; Res_Out <= C;
A B C
Hardware Modeling - 1 - 52
A B C
Out_1
Res_Out
Signal Resolution
Res_Out <= A when En0 = 1 else Z ; Res_Out <= B when En1 = 1 else Z ; Res_Out <= C when En2 = 1 else Z ;
En0
A
En1
B
En2
Res_Out
Hardware Modeling - 1 - 53
Enumerated (lit k)
Cc kiu lit k do ngi dng nh ngha s dng cc gi tr c th nhn bit mt cch d dng v thch hp vi hot ng ca m hnh Lm cho cc chng trnh d hiu hn nht l khi m t cc my trng thi (h logic dy) hoc cc h thng phc tp
...
signal STATE, NEXT_STATE : My_State ; ... case (STATE) is when LOAD => . . . if COND_A and COND_B then NEXT_STATE <= FETCH ; else NEXT_STATE <= STOR ;
Hardware Modeling - 1 - 54
Trong VHDL khng nh ngha u l LSB hoc MSB; do vy, khi bin dch gi tr ny khng c t ng hiu l 3 Ch : dng du trch kp (0011) cho cc i tng c kiu bit_vector, std_logic_vector hoc string, v dng du trch n (1) cho cc i tng c kiu bit, std_logic v character
Hardware Modeling - 1 - 55
Array
B_bus
Nu B_bus c kiu WORD, gi tr c th 3 2 1 0 c ca cc index position phn t l? Cn nu B_bus c kiu signal B_bus : DATA ; DATA? type DATA is array (3 downto 0) of integer range 0 to 9 ;
Hardware Modeling - 1 - 56
Php gn cc Array
Khi gn gi tr mt array cho mt array khc, th: 1. Cc array cn phi c cng kiu gi tr 2. Cc array phi c cng di 3. Php gn c thc hin theo v tr, t tri sang phi
signal My_BusA, My_BusB: bit_vector (3 downto 0) ; signal My_BusC : bit_vector (0 to 3) ;
My_BusB 3 2 1 0
My_BusC 0 1 2 3
Inadvertent bit-swap?
Hardware Modeling - 1 - 57
n gin cc php gn array v lm cho chng trnh d c hn - ngi thit k c th s dng cc gi tr c s 16 (hexadecimal) hoc 8 (octal)
Data_Word <= XA6F; Data_Word <= 101001101111 ; Data_Word <= O5157; Data_Word <= B1010_0110_1111 ;
Hardware Modeling - 1 - 58
Records
type OPCODE is record PARITY : bit; ADDRESS : std_logic_vector ( 0 to 3 ); DATA_BYTE : std_logic_vector ( 7 downto 0 ); NUM_VALUE : integer range 0 to 6; STOP_BITS : bit_vector (1 downto 0); end record ; ... signal TX_PACKET, RX_PACKET : OPCODE; Record l nhm cc phn t n c kiu ban u c th khc nhau.
...
PARITY ADDRESS DATA_BYTE NUM_VALUE STOP_BITS
TX _ PAC K ET
Hardware Modeling - 1 - 59
String
signal Warning1: string (1 to 30) := Unexpected Outputs Detected ;
--declared within the architecture
process ( A_sig , B_sig, C_sig ) process ( A_sig , B_sig, C_sig ) begin begin if (A_sig and B_sig ) /= 1 then if (A_sig and B_sig ) /= 1 then report Unexpected Outputs ; report Warning1; elsif ( A_sig and C_sig ) = 1 then elsif ( A_sig and C_sig ) = 1 then report I need a vacation ; report Warning2 & Problem Mod2; end if ; end if ; process ( A_sig , B_sig, C_sig ) end process ; end process ; begin assert (A_sig and B_sig ) /= 1 then report Warning1; severity note ; end if ; end process ;
Hardware Modeling - 1 - 60
Php gp cc Array
Php gp (Aggregate) l mt cng c tin li nhm cc kiu d liu (data type) c v hng v phc hp trong cc php gn
signal H_BYTE, L_BYTE: std_logic_vector ( 0 to 7); signal Q_Out : std_logic_vector (31 downto 0); signal A, B, C, D : std_logic; signal WORD : std_logic_vector (3 downto 0);
Only scalar data variables are allowed on the left-side aggregates. (A,B,C,D)<=WORD; WORD <= ( 2 => 1, 3 => D, others => 0 ) ; Q_Out <= ( others => 0 ) ; WORD <= ( A, B, C, D ) ; H_Byte <= ( 7|6|0 => 1, 2 to 5 => 0 ) ;
The total number of elements on both sides of any assignment must match, others can be used as a default assignment, regardless of the array size
Hardware Modeling - 1 - 61
Gp cc Record
Php gp (Aggregate) l mt cng c tin li nhm cc kiu d liu (data type) c v hng v phc hp trong cc php gn
type D_WORD is record UPPER : std_logic_vector (7 downto 0 ) ; LOWER : std_logic_vector (7 downto 0 ) ; end record ; signal DATA_WORD : D_WORD ; signal H_BYTE, L_BYTE: std_logic_vector (7 downto 0); signal TX_PACKET, RX_PACKET : OPCODE; --defined earlier DATA_WORD <= ( H_BYTE, L_BYTE) ; Only records can accept aggregate of arrays
TX_PACKET <= ( 1,0011,11101010,5,10 ) ; DATA_WORD <= ( LOWER => L_BYTE, UPPER=> H_BYTE) ; TX_PACKET. ADDRESS <= ( 0011 ) ; TX_PACKET. ADDRESS(2) <= 0 ; DATA_WORD <= ( LOWER | UPPER=> H_BYTE); DATA_WORD <= ( others => H_BYTE);
Hardware Modeling - 1 - 62
To lp cc Array 2-D
Khi cn m hnh ho cc cu trc b nh, ngi thit k phi to ra mt cu trc array 2 chiu (2-D structure)
0 1 2
Hardware Modeling - 1 - 63
type OPCODE is record PARITY : bit; ADDRESS : std_logic_vector ( 0 to 3 ); DATA_BYTE : std_logic_vector ( 7 downto 0 ); NUM_VALUE : integer range 0 to 6; STOP_BITS : bit_vector (1 downto 0); end record ; ... signal TX_PACKET, RX_PACKET : OPCODE;
My_Data ...
DATA_BYTE NUM_VALUE STOP_BITS
PARITY
ADDRESS
... ...
Hardware Modeling - 1 - 64
Vi hu ht cc ng dng ca memory, vector a ch Read/Write c chuyn thnh dng integer, tham tr ti mt phn t trong array 2-D - Hm bin i conv_integer c trong gi
ieee.std_logic_unsigned
type Mem_Array is array ( 0 to 3 ) of std_logic_vector ( 7 downto 0 ); signal My_Mem : Mem_Array ; signal R_Addr, W_Addr : std_logic_vector (1 downto 0 ) ;
7
0 1 2 3
0
My_Mem (conv_integer( W_Addr)) <= Data_In ; ... D_Out <= My_Mem (conv_integer (R_Addr));
Hardware Modeling - 1 - 65
Vi cc ng dng ROM, php kt tp (aggregate) l mt cng c tin ch kch hot mng array 2-D
type ROM_Array is array ( 0 to 3 ) of std_logic_vector ( 7 downto 0); constant My_ROM : ROM_Array := --continued below
7 0 1
0
constant My_ROM : ROM_Array := ( 0 => (others => 1) , 1 => 10100010, 2 => 00001111, 3 => 11110000 ) ;
2
3
Hardware Modeling - 1 - 66
Kiu c cng c th l mt kiu c nh ngha trong IEEE1076 hoc cng c th do ngi dng to ra subtype My_Int is integer range 0 to 255 ;
Label
Base Type
Constraint
subtype My_Small_Int is My_Int range 5 to 30 ; Cc kiu con (Subtypes) thng c dng pht hin li out-of-range trong kt qu m phng, v cng c th c tc dng ti u ho qu trnh tng hp logic
Hardware Modeling - 1 - 67
subtype Arith_Ops is My_State range Add to Mult ; Label Base Type Constraint
subtype My_OHE_State is std_logic_vector ( 3 downto 0 ) ; constant Init_St0 : My_OHE_State := 0001 ; constant Load_St1 : My_OHE_State := 0010 ; constant Jump_St2 : My_OHE_State := 0100 ; constant Stor_St3 : My_OHE_State := 1000 ;
Hardware Modeling - 1 - 68
Bi 4
Operators v Expressions
Hardware Modeling - 1 - 69
VHDL cha mt lng ln cc ton t (operator) dng cho vic m hnh ho phn cng Tuy nhin, mi ton t c s dng vi (nhng) kiu d liu (data type) nht nh
c bit, cn lu cc ton t s hc (arithmetic) khng dng c vi cc kiu d liu phc hp (bit_vector and std_logic_vector), v cc array khng c gi tr bng s tng minh
Hardware Modeling - 1 - 70
Operator Overloading
Operator overloading (chng hm) trong VHDL c ngha l mt ton t c th (v d, + ) c th c dng vi cc data type khc nhau
Cc ton t Logic
Cc logical operator c nh ngha vi cc ton hng c kiu (data type) bit, boolean, bit_vector v std_logic_vector
A Z B F Z <= A and B ; H Y G Y <= G or ( F and H ) ;
Hardware Modeling - 1 - 72
. . .
C_vec (0)
Quy tc s dng vi bin kiu Array 1. Cc array phi c cng kiu (type) 2. Cc array phi c cng kch thc 3. Php ton thc hin vi cc phn t cng v tr trong mi array, t tri sang phi
Hardware Modeling - 1 - 73
Cc ton t quan h
Cc ton t quan h c th dng vi hu ht cc data type Tt c cc ton t quan h u cho kt qu kiu (type) Boolean
= /= < <= > >= Equality Inequality Less than Less than or equal Greater than Greater than or equal
signal FLAG_BIT : boolean ; signal A, B : integer ;
Hardware Modeling - 1 - 74
if ( A_vec > B_vec ) then State <= Normal else State <= Code_Red d ny cho kt qu l false end if Cc vector c so snh t tri sang phi. Vic so snh c thc hin ln lt theo tng phn t
Hardware Modeling - 1 - 75
Cc ton t s hc
+ * / abs **
signal A_num, B_num : integer range 0 to 15 ; signal Z_num : integer range 0 to 31 ; Z_num <= ( A_num + B_num ) ; A_num
+
B_num
Z_num
Hardware Modeling - 1 - 76
thc hin cc php tnh s hc cn c cc function nh ngha sn trong IEEE hoc t cc hng cung cp phn mm thit k
Cc function nh vy thng c t trong cc gi gi l arithmetic packages. Mt s cng c (tool) dch sn cc gi ny v t th vin ca chng Cc gi std_logic_unsigned v std_logic_arith c xc lp trong th vin IEEE pht huy kh nng trao i m ngun gia cc cng c khc nhau
package STD_LOGIC_UNSIGNED is function + (A,B: std_logic_vector) return std_logic_vector ; function + (A: std_logic_vector, B: integer ) return std_logic_vector ; function + (A,B: std_logic_vector) return integer ; .... library IEEE ; use IEEE.std_logic_1164.all ; use IEEE.Std_Logic_Unsigned.all ; Ton t + l overloaded use IEEE.Std_Logic_Arith.all;
Hardware Modeling - 1 - 77
Array Arithmetic
Nu c cc function thch hp c th dng c trong module thit k (thng qua mnh use), th compiler s t ng truyn tham s cho tr li kt qu tnh t subprogram
Hardware Modeling - 1 - 78
Cc ton t Shift c nh ngha trong VHDL-93 v rt hay c s dng, nht l khi cn m t hot ng ca cc phn t trong cu trc my tnh
Mi ton t c hai ton hng, ton hng tri c kiu array one-dimension (1-D) v ton hng phi c kiu nguyn (integer) ch s bc cn dch (hoc quay). S m cng c dng ch chiu dch (hoc quay) ngc li
Kt qu ca cc php dch (hoc quay) c cng kiu v kch thc vi ton hng tri
Hardware Modeling - 1 - 79
sll - Logical left shift srl - Logical right shift sla - Arithmetic left shift sra - Arithmetic right shift rol - rotate left logical ror - rotate right logical
Hardware Modeling - 1 - 80
Shift Operator - Cc v d
V d: signal A_vec : bit_vector (7 downto 0) := 11000110; signal D_vec : bit_vector (7 downto 0); D_vec <= A_vec sll 2; D_vec <= A_vec sra 2; D_vec <= A_vec ror 3; D_vec <= A_vec srl 2; D_vec <= A_vec sra -2; 00011000 11110001 11011000 00110001 00011000
cho kq
Hardware Modeling - 1 - 81
Concatenation
Ton t ni (concatenation) & cho php kt ni cc bin kiu scalar v kiu array thnh kiu array ln hn
Z_vec <= A_vec & B_vec ; X_vec <= A_bit & B_bit & C_bit ; Y_vec <= B_vec & D_bit ;
Hardware Modeling - 1 - 82
Nhm cc ton t
Z <= A + B + C + D ; A B
Z <= ( A + B ) + ( C + D ) ; A B Z C D
+
C
+
D
+
+ +
Z
3 logic levels
2 logic levels
This is especially important when the target technology is LUT (Look-Up Table) based. Each added level of logic incurs additional block and routing delays
Hardware Modeling - 1 - 83
Cc Slice ca Array
signal A_vec, B_vec : std_logic_vector (7 downto 0) ; signal Z_vec : std_logic_vector (15 downto 0) ; signal A_bit, B_bit, C_bit, D_bit : std_logic ;
Z_vec (15 downto 8) <= A_vec ; B_vec <= Z_vec (12 downto 5) ; A_vec (1 downto 0) <= C_bit & D_bit ; ... Z_vec (5 downto 1) <= B_vec (1 to 5 ) ;
The direction (ascending or descending) of the slice must be consistent with the direction of the array as it was originally declared
Hardware Modeling - 1 - 84
Slice v Concatenation
V d, m hnh ho mt biu thc c iu kin vi cc phn t c chn khng lin nhau (9,8,3,2,1, v 0) t Status bus gm 16-bit
Gii m ton b bus c th lm tng s lng mch logic t hp v lm cho thit b lm vic chm hn)
signal Status_Bus : std_logic_vector (15 downto 0) ;
process ( Status_Bus ) begin case ( Status_Bus ) is when 1101111101010011 => < sequential statement(s) > ; when . . .
Inefficient !!
signal Int_Bus : std_logic_vector ( ... Int_Bus <= Status_Bus ( 9 downto 8 ) & Status_Bus ( 3 downto 0 ) ;
process ( Status_Bus ) begin case ( Status_Bus(9 downto 8) & Status_Bus (3 downto 0)) is when 110011 => < sequential statement(s) > ; process ( Int_Bus ) when . . . begin case ( Int_Bus ) is when 110011 => 5 downto 0 ) ; < sequential statement(s) > ; when . . .
Optimal !!
Hardware Modeling - 1 - 85
Bi 5
Hardware Modeling - 1 - 86
m hnh ho mt thc th phn cng mt cch c hiu qu, VHDL s dng c cc cu lnh concurrent (ng thi) ln sequential (tun t) Cc cu lnh c gi l concurrent khi chng c thc hin khng tu thuc vo v tr ca chng trong chng trnh Cc cu lnh c gi l sequential khi chng c thc hin mt cch tun t theo trnh t vit chng trong chng trnh, ging nh cc lnh trong cc phn mm thng thng
Hardware Modeling - 1 - 87
Cu trc ngn ng
architecture RTL of ENTITY_1 is . . . begin concurrent statements ; ... process begin sequential statements ; ... end process ; ... concurrent statements ; ... process begin sequential statements ; ... end process ; ... end architecture RTL ;
Hardware Modeling - 1 - 88
Process
architecture RTL of My_And2 is begin ... process (A, B) begin C <= A and B ; end process ; ... end architecture RTL;
Thng l do s thay i ca cc tn hiu trong process, hoc cc iu kin c th trong mt cu lnh wait
Hardware Modeling - 1 - 89
Cc Process l ng thi
Trong vic m hnh ho phn cng, quan im v concurrency (tnh ng thi) l rt cn thit
A B G1
G2
Process 3
G3
process (C,..) begin
Hardware Modeling - 1 - 90
Cc process concurrent, c lin kt bi cc tn hiu, thng c gi l VHDL Connectivity Model (m hnh kt ni) Process n Process n1
Sig1 Sig2
C<=A and B ...
C C
If C = 1 then ...
Process n3
process (...
Process n4
process n
Process n2
process (C,..) begin
Rst
Hardware Modeling - 1 - 91
architecture Behave of DFF is begin ... Reg1: process (Clock, Reset) begin if Reset = 1 then Q <= 0 ; elsif ( Clockevent and Clock = 1 ) then Q <= D ; end if ; end process; All statements within ... the process are handled sequentially, in order end Behave ;
Hardware Modeling - 1 - 92
Out1
A
architecture . . . begin Out1 <= A; Out1 <= B; ... end architecture ;
?
B
Out1
Hardware Modeling - 1 - 93
Tm dng cc Process
Vi on m RTL, cc tn hiu trong sensitivity list to nn mt iu kin wait mc nhcho process i vi cc m hnh hnh vi, ngi thit k thng dng cc cu lnh wait. Trong VHDL c bn dng lnh wait
wait on... wait until... wait for ... wait An event on given signal A specific condition A specified time amount Indefinite suspension wait on A, B ; wait until CLK = 1 ; wait for 10 ns ; wait;
Hardware Modeling - 1 - 94
Cc iu kin Wait
V d di y so snh cc iu kin wait t cc cu lnh c vit r rng (explicit) v iu kin wait n (implicit)
FYI: Another important consideration is that all processes are initialized before simulation, that means they are run until the first wait condition is met Given that fact, what is the value on C at simulation time zeroassuming use of std_logic for each of the examples above ?
Hardware Modeling - 1 - 95
Modeling Concurrency
Ti mt thi im bt k trong thi gian m phng, (1) tt c cc process c thc hin cho n khi b tm dng, (2) gi tr ca cc tn hiu c cp nht, (3) s thay i gi tr cc tn hiu lm cho cc process lin quan c thc hin
Thi gian gia cc cng on ny c gi l delta cycle Theo cch , cc process v cc lnh concurrent c coi nh xy ra ng thi ti mi im m phng
Concurrent Operations Delta cycles in-between Delta cycles in-between
...
D1 1001
D+1
D1
D+1
D+2
998
999
1000
Hardware Modeling - 1 - 96
Sp xp trnh t cc Event
C ch sp xp trnh t cc event l ht nhn ca mi trng m hnh ho hnh vi trong VHDL Mi hot ng (transaction) ca thc th c sp t mt thi im thch hp Bc thi gian ch tng ln khi khng cn transaction no khc thi im m phng
Hardware Modeling - 1 - 97
Scheduling Events
Delta cycles Simulation discrete time steps Delta cycles
...
ns
D1
D+1
D1
D+1
D+2
t+1
t+2
t+3
Transaction Queue
Out1 <= 1 ; Out2 <= 0 ; ... Int <= 1 after 1 ns; ... Data <= 0 after 2 ns; ...
t+3 Out1<= 1
Delta Cycles
Out2<= 0 ...
Discrete Time
Hardware Modeling - 1 - 98
Transactions
Hardware Modeling - 1 - 99
Events
Nu gi tr ca Z hoc F thc s c thay i sau lnh gn (transaction), ta ni c mt event xut hin tn hiu Mi mt php gn tn hiu u gy ra mt transaction, nhng khng phi tt c cc transaction u lm xut hin cc event tn hiu c gn Mt event mt tn hiu cho c th hot kch mt process ch khi tn hiu c tn trong sensitivity list ca proccess
Process 2
Z Z
If Z = 1 then ...
Process 3
process (Z,..) begin
Building Registers
process ( Clk) begin if (Clkevent and Clk = 1) then C <= A and B ; end if; end process;
Hu nh mi kt ni gia cc im trong mt thc th u c th c m hnh ho bng signal (tn hiu) hoc variable (bin) Tuy nhin, cn phn bit cch dng, cc kh nng v nhng hn ch ca tng i tng ny mc thc th, tt c cc port c khai bo thuc lp signal, chng c kh nng trao i gi tr vi cc module khc, v c th dng c cho mi process c khai bo trong cc architecture body l thuc Trong mi architecture body, tt c cc tn hiu c khai bo ch c tc dng cc b vi architecture , v ch c dng vi cc process c khai bo trong cng architecture Trong mi process, c th khai bo v s dng variable m phng cc mi lin kt cc b
Port l signal, c khai bo mc cao nht (entity) Trong mi architecture c th c cc signal cc b Trong mi process c th c cc variable cc b entity architecture process Variables
Signals
Input Ports Output Ports
Php gn vi Signal
VHDL phn bit rt r rng trong vic x l vi cc i tng thuc cc lp signal v variable Cc quy tc c bn l:
Tt c cc signal v variable c gn phi c cng kiu (type) Tt c cc signal v variable c gn phi c cng kch thc (size)
Signals c mode in c th c nhng khng c gn gi tr Signals c mode out c th c gn gi tr trong process, nhng khng c c
Php gn vi Signal
Internal_Cnt entity Count_1 is port (Clk, D : in bit ; Q : out integer range...); end Count_1;
Counter
architecture WRONG of Count_1 is begin process (Clk) begin If Clkevent and Clk =1 then Q <= Q + 1; end if ; end process ;
architecture RTL of Count_1 is signal Internal_Cnt : integer range ... ; begin process (Clk) begin If Clkevent and Clk =1 then Internal_Cnt <= Internal_Cnt + 1 ; end if ; end process ; Q <= Internal_Cnt ;
Using Variables
V phm vi ca mt variable ch gii hn trong process n c khai bo, ta khng th s dng n trao i gi tr vi cc module khc, nh cch s dng cc signal Bt k variable no c c trc khi gi tr ca n c xc nh, u s c suy din nh l mt register trong cu trc hardware nu nh n c dng vi mt process c clock
Tuy nhin, rt nn s dng cc variable c trong cc mch logic t hp trung gian Chng lm tng tc ca vic chy m phng v ch cn t delta cycles cho vic cp nht cc tn hiu ra
Clk
Clk
Alternate Solution
Internal_Cnt entity Count_1 is port (Clk, D : in std_logic ; Q : out std_logic_vector ...); end Count_1;
Counter
architecture WRONG of Count_1 is begin process (Clk) begin If Clkevent and Clk =1 then Q <= Q + 1; end if ; end process ; Will produce
compiler error
architecture RTL of Count_1 is begin process (Clk) variable Internal_Cnt : std_logic_vector.. begin If Clkevent and Clk =1 then Internal_Cnt := Internal_Cnt + 1 ; Q <= Internal_Cnt ; end if ; end process ;
Bi 6
Cu trc ngn ng
Cc lnh if/else,
architecture RTL of ENTITY_1 is . . . begin concurrent statements ; ... process begin case ( sel_a ) is when ... end case ; end process ; ... ... process begin if (sel_b = 00) then ... else. end if ; end process ; ... end architecture RTL ;
Cc cu lnh If/Else
Cu lnh if/else lm cho cc giao dch c thc hin da trn nhng iu kin nht nh. C ba dng lnh if/else c bn sau:
process begin if (boolean expression) then sequential statements; end if ; process begin if (boolean expression) then sequential statements ; else sequential statements ; end if ; process begin if (boolean expression 1) then sequential statements ; elsif (boolean expression 2) then sequential statements ; elsif (boolean expression 3) then sequential statements ; else sequential statements ; end if ;
V d v lnh If/Elsif
1. iu kin u tin tho mn s c thc hin ngay 2. Cc iu kin c th gi nhau 3. iu kin u tin ca lnh if/elsif c u tin cao nht
process (A, B, C, D, Sel) D begin If (Sel = 00) then C Z <= A ; elsif (Sel = 01) then Z <= B ; B elsif (Sel = 10) then Z <= C ; elsif (Sel = 11) then A Z <= D ; end if; end process ; Sufficient for std_logic? A B C D Sel Z
Cu lnh Case
Cu lnh case lm cho cc giao dch c thc hin tu thuc vo gi tr ca biu thc chn Lnh case c hai dng c bn:
process () begin case ( selector expression ) is when ... => sequential statements ; when ... => sequential statements ; when ... => sequential statements ; end case ; ... end process ; process (...) begin case ( selector expression ) is when ... => sequential statements ; ... when others => sequential statements ; end case ; ... end process ;
V d v lnh Case
Tt c cc gi tr c th c ca biu thc chn u phi c ch r (specified) trong cu lnh Cc iu kin khng c chng cho (gi) nhau Gii cc gi tr c m t phi hu hn (discrete) Lnh Case thch hp vi cc cu trc kiu LUT
Hu ht cc cng c tng hp mch process (A, B, C, D, Sel ) u to ra cu trc MUX t lnh case begin
A B C D Sel Z
Z Z Z Z
V d di y minh ho trng hp cc iu kin chng cho nhau. Khi ta ch c th s dng lnh if/elseif. Mun s dng lnh case phi chnh li cc iu kin
process (A, B, C, D, Sel) begin If (Sel <= 3) then Z <= A ; elsif (Sel <= 5) then Z <= B ; elsif (Sel <= 7) then Z <= C ; elsif (Sel <= 9) then Z <= D ; end if; end process ;
D C B Z
Gii hu hn cc gi tr
sequential statements ;
architecture ... begin Z <= A when Sel = 00 else B when Sel = 10 else C when Sel = 11 else X ; end architecture ;
Lnh gn tn hiu c la chn l mt dng lnh c tc dng tng ng vi lnh case Quy tc s dng hon ton ging vi lnh case:
(1) Tt c cc iu kin phi c lit k (2) Cc iu kin khng c chng cho nhau
architecture ... begin process ( A,B,C, SEL ) begin case (SEL) is when 00 => Z <= A ; when 10 => Z <= B ; when 11 => Z <= C ; when others => Z <= X ; end case ; end process ; end architecture ;
architecture... with SEL select Z <= A when 00 , B when 10 , C when 11 , X when others ; end architecture ;
Outline
If/else Statements Case Statement Concurrent Form of If/else and Case Statements Loop Statements Summary
Cc cu lnh Loop
Cc cu lnh loop c th c dng cho mi trng hp c vng lp Lnh loop c nhiu dng khc nhau, mi dng s dng mt phng php iu khin lp ring. Dng thng dng nht l for loop
Bin ch s index cho vng lp khng c khai bo process ( A, B_bus ) ring, khng th gn gi begin tr cho n, v khng c for i in 7 downto 0 loop s dng ngoi vng lp C_bus (i) <= A and B_bus (i) ; N c dng nh l end loop ; mt hng s Sau mi php lp, gi tr ca n c cp nht theo chiu hng ghi trong cu lnh t tri sang phi
process ( A, B_bus ) begin for i in 7 downto 0 loop C_bus (i) <= A and B_bus (i) ; end loop ;
Khi m phng, cc lnh loop to nn mt cng c linh hot m hnh ho hnh vi Cc lnh loop c bit cn hay c s dng trong cc chng trnh con (function v procedure)
. . .
C_bus (0)
A B_bus (0)