Professional Documents
Culture Documents
DesignWithFPGA K51
DesignWithFPGA K51
=
=
2
1 i
i
x sum
Task:
sum = 0
FOR i = 1 TO 2
sum = sum + x
i
ENDFOR
y = sum
Algorithm:
Processing
Control
Datapath construction rules:
each variable and constant corresponds to a register
each operator corresponds to a functional unit
connect outputs of registers to input of functional
units; when multiple outputs connect to the same input:
MUX or bus with tristate drivers
connect output of functional units to input
of registers; when multiple outputs connect to the same
input: MUX or bus with tristate drivers
2.4 Cu trc FSMD
Datapath design
DHBK 2007
231
sum = 0
FOR i = 1 TO 2
sum = sum + x
i
ENDFOR
y = sum
Algorithm:
Variables: sum
Reset
Load
Clk
Register
SUM
0
1
2
Wait
100
Add
Operators: add
x
i
Connections
Add2
010
Output
001
Add1
010
Start=1
y
0
Start
Output order:
Reset,Load,
Out
210
2.4 Cu trc FSMD
Datapath design
DHBK 2007
232
Task: count the number of 1s in a word
Data = Inport || OCnt = 0 || Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp || Data = Data >> 1
ENDWHILE
Outport = OCnt
Algorithm:
All instructions on a single line are executed concurrently:
maximum speed, but highest cost
Trading-off speed for area is explained in the section on
Synthesis techniques
All hardware components work in parallel. Implementing
hardware is hence not writing a sequential software
program and implementing this directly in hardware. Above
algorithm is a concurrent description!
2.4 Cu trc FSMD
Datapath design
DHBK 2007
233
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
0
1
2
3
4
5
Comp
x00000
Update
010100
Load
111x00
s=1
Temp
x00010
z=0
Out
x00001
z=1
s=0
s
Data
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
OCnt
R
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
Mask
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
Temp
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
<>0
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
AND
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
Add
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
>>1
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
1 0
Inport
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
zero
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
Outport
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
Wait
x01x00
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
Output order:
543210
DHBK 2007
234
Possible optimisations:
When the life time of 2 variables is non-overlapping, both can
be stored in the same register: register sharing
When two operations are not executed concurrently, they can
be assigned to the same functional unit: functional unit
sharing
When two connections are not used concurrently, they can be
shared: connection sharing
When two registers are not concurrently read from resp.
writen to, they can be combined into a single register file:
register port sharing
Operations that could be executed concurrently, may also be
executed sequentially, facilitating the four previous
optimisations
2.4 Cu trc FSMD
Datapath design
DHBK 2007
235
Temporary storage
External input
External output
Result switching network
Generic structure of the datapath:
Functional units
Operand switching network
2.4 Cu trc FSMD
Datapath design
DHBK 2007
236
Typical datapath:
R
L
C
S
1 0
WA
WE
RA
1
RE
1
RA
2
RE
2
R
L
COE RFOE
1
RFOE
2
ROE
AOE SOE
OOE
> = <
Counter
Register
File
2
3
Register
Comparator ALU Barrel
shifter
Outport
Inport
F
Sh
D
2.4 Cu trc FSMD
Datapath design
DHBK 2007
237
In the datapath of previous slide a few decisions have
been taken:
Only 1 i.o. 2 result busses ALU and Barrel shifter cannot
be used concurrently
Only 2 i.o. 4 operand busses e.g. Compare and ALU work
on the same set of data
9 registers with only 2 write ports and 3 read ports
Inport can only feed the register file
2.4 Cu trc FSMD
Datapath design
DHBK 2007
238
OOE SOE D SH0 F0
RF
OE2
RE2 RA0 SH1 SH2 AOE F1 F2 ROE L R RA1 RA2
Barrel
shifter
ALU
Register
Register File
Read Port 2
Instruction format
RF
OE1
RE1 RA0 RA1 WA1 R RA2 WE WA0 WA2 S COE C L
Register File
Read Port 1
Register
File
Write Port
Counter
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
18 19 20 21 22 23 24 25 26 27 28 29 30 31
32-bit instruction word
For reasons of simplicity, clarity and correctness, it is
possible to assign a mnemonic to a certain bit pattern
(e.g. ADD): assembly instruction
2.4 Cu trc FSMD
Datapath design
DHBK 2007
239
The size of the instruction word may be reduced, since
several operations cannot be executed concurrently
Either Register File Read Port 2, either Register Read Port
connects to the 1st Operand Bus (-1)
Either Register File Read Port 1, either Counter Read Port
connects to the 2nd Operand Bus (-1)
ALU & Shift cannot occur concurrently: 1 bit needed to
select the operator and 4 bits control the operator (-2)
When the ALU operator is active, its output may immediately
be placed on the result bus; idem for the Barrel shifter (-2)
For the counter the Count and Load operations are
exclusive (-1)
Additional limitations to concurrency may be
introduced at the cost of increased execution time
2.4 Cu trc FSMD
Datapath design
DHBK 2007
240
Design freedom
Type Fixed To be designed speed cost design
time
custom
proc.
fixed
algo
- - custom
DP
custom
Ctrl
|| ++ ||
soft IP fixed
algo
DP - DP
ext.
custom
Ctrl
| + |
ASIP algo
class
DP Ctrl DP
ext.
Ctrl
ext.
+ | +
Proc any
algo
DP Ctrl - -
++
-
|| ++
A compiler performs the same tasks as synthesis tools
(e.g. assign variables without overlapping life time to
the same register) but with less degrees of freedom,
since the hardware is fixed
2.4 Cu trc FSMD
Datapath design
DHBK 2007
241
The controller has been designed each time using the
design method for FSMs as discussed before
For a large number of states this is a tedious job
Next slides present alternative design methods, that lead
to a faster design process in several cases
2.4 Cu trc FSMD
Controller design
DHBK 2007
242
D
Clk
Q
S*=F(S,I)
Next
State
Combi-
nato-
rial
Logic
O=H(S,I)
Output
Combi-
nato-
rial
Logic
D
Clk
Q
D
Clk
Q
Standard FSM
2.4 Cu trc FSMD
Controller design
DHBK 2007
243
State
Reg
Next
state
logic
Out-
put
logic
CI
CI
SS
SS
Current
State
Next
State
Control
Input (CI)
Control
Output (CO)
Control
Signals (CS)
Status
Signals (SS)
Redrawn
Size State Reg:
log
2
n( for n states
for straightforward
and
minimum-bit-change;
n for n states for
one-hot
CS
CO
2.4 Cu trc FSMD
Controller design
DHBK 2007
244
State
Reg
Next
state
logic
Out-
put
logic
CI
CI
SS
SS
Current
State
Next
State
CS
CO
R
L
C
S
1 0
WA
WE
RA
1
RE
1
RA
2
RE
2
R
L
COE RFOE
1
RFOE
2
ROE
AOE SOE
OOE
> = <
Counter
Register
File
2
3
Register
Comparator ALU
Barrel
shifter
Outport
F
Sh
D
Critical path delay:
Find the longest combinatorial path from clock
to clock
RFOE
2
RFOE
1
State
Reg
Next
state
logic
Out-
put
logic
CI
CI
SS
SS
Current
State
Next
State
CS
CO
R
L
C
S
1 0
WA
WE
RA
1
RE
1
RA
2
RE
2
R
L
COE ROE
AOE SOE
OOE
> = <
Counter
Register
File
2
3
Register
Comparator ALU
Barrel
shifter
Outport
F
Sh
D
ClkOutStateReg + OutputLogic + AddressToOutRegFile +
BusDriver + BarrelShifter +BusDriver +Mux +
SetupInPortRegFile
2.4 Cu trc FSMD
Controller design
DHBK 2007
245
State
Reg
Next
state
logic
Out-
put
logic
CI
CI
SS
SS
Current
State
Next
State
CI
CO
CS SS
Modification 1
log
2
n
n
dec.
Properties:
* simple
design and small
next state and
output logic of
one-hot
* small number of
flip-flops of
straightforward
and minimum-
bit-change
One-hot
State
reg
CS
CO
2.4 Cu trc FSMD
Controller design
DHBK 2007
246
Modification 2
Often the state diagram shows an unconditional sequence of
states, but for a few exceptions
E.g.
Wait
100
Add2
010
Output
001
Add1
010
Start=1
0
2.4 Cu trc FSMD
Controller design
DHBK 2007
247
State
Reg
Next
state
logic
Out-
put
logic
CI
CI
SS
SS
Current
State
Next
State
CI CO
CS SS
MUX
INC
Next
State
Logic
Modification 2
CS
CO
2.4 Cu trc FSMD
Controller design
DHBK 2007
248
Advantage of modification 2:
The next state logic is very simple:
for unconditional next state: select the INC
only for conditional next state the hardware should generate the
next state
Implementation of the INC:
ripple carry chain of Half Adders
INC and State Reg together form a synchronous counter
2.4 Cu trc FSMD
Controller design
DHBK 2007
249
Modification 3
Often the state diagram contains a part that is repeated several
times subroutine
s0
s3
s4
s1
s2
s0
s1
s2
s3
s4
s5
s6
7 states
5 states
Only at run-time
it is known
which will be
the next
state following
the end of a
subroutine
stack
2.4 Cu trc FSMD
Controller design
DHBK 2007
250
State
Reg
Next
state
logic
Out-
put
logic
CI
CI
SS
SS
Current
State
Next
State
CI CO
CS SS
MUX
Stack
Modification 3
Push/
Pop
Return
State
Next
State
Logic
CS
CO
2.4 Cu trc FSMD
Controller design
DHBK 2007
251
Log
2
n
n
Dec
Next
state
logic
Out-
put
logic
CI
CI
SS
SS
Current
State
Next
State
CI CO
CS SS
MUX
Stack
Combination
Push/
Pop
State
Reg
INC
Assumption: Return state = Jump state + 1
CS
CO
2.4 Cu trc FSMD
Controller design
DHBK 2007
252
Implementation of the next state logic and the output
logic
Either construct via Karnaugh a minimal AND-OR
implementation
Either put the truth table in a ROM-table (this method is
called microprogrammed control)
2.4 Cu trc FSMD
Controller design
DHBK 2007
253
ROM
table
CI SS
Current
State
Next
State
CI CO
CS SS
MUX
Stack
ROM table
Push/
Pop
State
Reg
INC
CS
CO
2.4 Cu trc FSMD
Controller design
DHBK 2007
254
Be careful about timing!
Example:
ReadFromExternal(A);
|| sum := 0;
WHILE A <> 1
sum := sum + A;
|| ReadFromExternal(A);
Each iteration of the
WHILE loop (body, test
and decision) should be
executed in just one
clock cycle!!
A sum
LA LS
RS
Comp Add
C=1 when A<>1
Comp
A
C
No 3-state
drivers: each
bus only has
one source
2.4 Cu trc FSMD
Controller design
DHBK 2007
255
Can the controller be state based?
Example:
ReadFromExternal(A);
|| sum := 0;
WHILE A <> 1
sum := sum + A;
|| ReadFromExternal(A);
A sum
LA LS
RS
Comp Add
C=1 when A<>1
s0
LA=1
RS=1
LS=0
s1
LA=1
RS=0
LS=1
C=1
C=0
Animate sequence
A=5,2,1 sum=7
A=? Sum=?
LA LS
RS
Comp Add
s0
LA=1
RS=1
LS=0
s1
LA=1
RS=0
LS=1
C=1
C=0
5
?
?
C=?
A=? Sum=0
LA LS
RS
Comp Add
s0
LA=1
RS=1
LS=0
s1
LA=1
RS=0
LS=1
C=1
C=0
5
?
?
C=?
A=5 Sum=0
LA LS
RS
Comp Add
s0
LA=1
RS=1
LS=0
s1
LA=1
RS=0
LS=1
C=1
C=0
5
5
5
C=1
Reset is asynchronous
A=5 Sum=0
LA LS
RS
Comp Add
s0
LA=1
RS=1
LS=0
s1
LA=1
RS=0
LS=1
C=1
C=0
2
5
5
C=1
A=2 Sum=5
LA LS
RS
Comp Add
s0
LA=1
RS=1
LS=0
s1
LA=1
RS=0
LS=1
C=1
C=0
2
7
7
C=1
A=2 Sum=5
LA LS
RS
Comp Add
s0
LA=1
RS=1
LS=0
s1
LA=1
RS=0
LS=1
C=1
C=0
1
7
7
C=1
A=1 Sum=7
LA LS
RS
Comp Add
s0
LA=1
RS=1
LS=0
s1
LA=1
RS=0
LS=1
C=1
C=0
1
8
8
C=0
A=1 Sum=7
LA LS
RS
Comp Add
s0
LA=1
RS=1
LS=0
s1
LA=1
RS=0
LS=1
C=1
C=0
?
8
8
C=0
A=? Sum=8
LA LS
RS
Comp Add
s0
LA=1
RS=1
LS=0
s1
LA=1
RS=0
LS=1
C=1
C=0
?
?
?
C=?
One count too much
sum=8 i.o. 7
2.4 Cu trc FSMD
Controller design
DHBK 2007
256
Can the controller be input based?
Example:
ReadFromExternal(A);
|| sum := 0;
WHILE A <> 1
sum := sum + A;
|| ReadFromExternal(A);
Animate sequence
A=5,2,1 sum=7
Reset is asynchronous
A sum
LA LS
RS
Comp Add
C=1 when A<>1
s0
LA=1
RS=1
LS=0
s1
RS=0
C=1
LA=1
LS=1
C=0
LA=0
LS=0
A=? Sum=?
LA LS
RS
Comp Add
C=?
s0
LA=1
RS=1
LS=0
s1
RS=0
C=1
LA=1
LS=1
C=0
LA=0
LS=0
5 ?
?
A=? Sum=0
LA LS
RS
Comp Add
C=?
s0
LA=1
RS=1
LS=0
s1
RS=0
C=1
LA=1
LS=1
C=0
LA=0
LS=0
5 ?
?
A=5 Sum=0
LA
LS
RS
Comp Add
C=1
s0
LA=1
RS=1
LS=0
s1
RS=0
C=1
LA=1
LS=1
C=0
LA=0
LS=0
5 5
5
A=5 Sum=0
LA LS
RS
Comp Add
C=1
s0
LA=1
RS=1
LS=0
s1
RS=0
C=1
LA=1
LS=1
C=0
LA=0
LS=0
2 5
5
A=2 Sum=5
LA LS
RS
Comp Add
C=1
s0
LA=1
RS=1
LS=0
s1
RS=0
C=1
LA=1
LS=1
C=0
LA=0
LS=0
2 7
7
A=2 Sum=5
LA LS
RS
Comp Add
C=1
s0
LA=1
RS=1
LS=0
s1
RS=0
C=1
LA=1
LS=1
C=0
LA=0
LS=0
1 7
7
A=1 Sum=7
LA LS
RS
Comp Add
C=0
s0
LA=1
RS=1
LS=0
s1
RS=0
C=1
LA=1
LS=1
C=0
LA=0
LS=0
1 8
8
A=1 Sum=7
LA LS
RS
Comp Add
C=0
s0
LA=1
RS=1
LS=0
s1
RS=0
C=1
LA=1
LS=1
C=0
LA=0
LS=0
? 8
8
A=1 Sum=7
LA LS
RS
Comp Add
C=0
s0
LA=1
RS=1
LS=0
s1
RS=0
C=1
LA=1
LS=1
C=0
LA=0
LS=0
? 8
8
Result is correct.
Always check timing!
2.4 Cu trc FSMD
Controller design
DHBK 2007
257
2.4 Cu trc FSMD
State-action table
The specification of an FSMD could be done using the
traditional next state & output table
However, for large designs, this becomes not so
practical
Next slide shows the next state & output table for the
one counting application
Data = Inport; OCnt = 0; Mask = 1
WHILE Data <> 0 DO
Temp = Data AND Mask
OCnt = OCnt + Temp; Data = Data >> 1
ENDWHILE
Outport = OCnt
DHBK 2007
258
Next state and output table
Present
state
Next state
(Start, Status)
Data
path
output
Data path variables
00 01 10 11 Outport Data OCount Temp Mask
S
0
S
0
S
0
S
1
S
1
Z X X X X
S
1
S
2
S
2
S
2
S
2
Z Inport X X X
S
2
S
3
S
3
S
3
S
3
Z Data 0 X X
S
3
S
4
S
4
S
4
S
4
Z Data OCount X 1
S
4
S
5
S
5
S
5
S
5
Z Data OCount Data
AND
Mask
Mask
S
5
S
6
S
6
S
6
S
6
Z Data OCount
+Temp
X Mask
S
6
S
4
S
7
S
4
S
7
Z Data >>
1
OCount X Mask
S
7
S
0
S
0
S
0
S
0
Ocount Data Ocount X X
2.4 Cu trc FSMD
State-action table
DHBK 2007
259
The next state and output table do not offer a good
overview
often the next state is only dependent on a few of the inputs
often, the data path variables do not change
Hence, the same information as in the next state and
output table is presented in a more condensed form: the
state action table (See next slide)
2.4 Cu trc FSMD
State-action table
DHBK 2007
260
Present
state
Next state Control and data path
actions
Condition State Condition Actions
S
0
Start=0
Start=1
S
0
S
1
Output=Z
S
1
S
2
Data=Inport
S
2
S
3
Ocount=0
S
3
S
4
Mask=1
S
4
S
5
Temp=Data
AND Mask
S
5
S
6
Ocount=
Ocount+
Temp
S
6
Data <> 0
Data = 0
S
4
S
7
Data >> 1
S
7
S
0
Output=
OCount
2.4 Cu trc FSMD
State-action table
DHBK 2007
261
2.4 Cu trc FSMD
Algorithmic-state-machine chart
An algorithmic-state-machine chart (ASM chart) is an
alternative visualization method for the state action
table
It shows loops, conditions and next states in a way
which is easier to understand for a human being
Each row in the state action table translates to an ASM
block
ASM blocks are constructed out of three types of
elements: state boxes, decision boxes and condition
boxes
DHBK 2007
262
Unconditional
variable
assignment
State name State encoding
State box
Decision box
Condition
1 0
Condition box
Conditional
variable
assignment
2.4 Cu trc FSMD
Algorithmic-state-machine chart
DHBK 2007
263
Done = 0
s
0
Start = 0
0
Data = Inport
Example of an ASM block
1
2.4 Cu trc FSMD
Algorithmic-state-machine chart
DHBK 2007
264
An ASM block has to obey following rule:
each input combination should lead to exactly one next state
Example 1 of an invalid ASM block:
Cond1 Cond2
1 0 0 1
s
0
s
1
s
2
When Cond2=1
there are two
next states
2.4 Cu trc FSMD
Algorithmic-state-machine chart
DHBK 2007
265
Example 2 of an invalid ASM block:
Cond1
Cond2
0
1
s
0
s
1
s
2
0
1
When Cond1=0
and Cond2=0
there is no
next state
2.4 Cu trc FSMD
Algorithmic-state-machine chart
DHBK 2007
266
An ASM chart representing a state-based or Moore type
FSMD has no condition boxes, since all outputs only
depend on the state; all assignments to variables are
done in state boxes
An ASM chart representing an input-based or Mealy
type FSMD has state boxes as well as condition boxes;
variable assignments that only depend on the state are
done within the state boxes; variable assignments that
depend on input conditions are done in condition boxes
2.4 Cu trc FSMD
Algorithmic-state-machine chart
DHBK 2007
267
Start=1
s
0
0
Data=Inport
OCount=0
1
s
1
Data
LSB
s
2
Ocount=Ocount+1
1
s
3
0
Data=Data>>1
s
4
Data<>0
1
Output=OCount
0
s
5
State based (Moore)
DHBK 2007
268
Start=1
s
0
0
Data=Inport
OCount=0
1
s
1
Data
LSB
s
2
Output=OCount
0
s
3
Input based (Mealy)
Data<>0
1
Data=Date>>1
1
Ocount=Ocount+1
0
Only 4 states instead
of the 6 for a state
based approach
DHBK 2007
269
Chng 4. Ngn ng m t phn cng
VHDL
4.1 Gii thiu v VHDL
4.2 Tn hiu v cc kiu d liu
4.3 Cc php ton s hc v logic
4.4 Cc lnh song song v tun t
4.5 Cc cu trc tun t
4.6 Chng trnh con
DHBK 2007
270
4.1 Gii thiu v VHDL
VHDL = VHSIC Hardware Description Language
VHSIC = Very High Speed Integrated Circuit
L ngn ng lp trnh dng m t hot ng ca h
thng s
c quy nh trong chun IEEE 1076 t nm 1983
Cc ngn ng m t phn cng khc:
Verilog
Abel
DHBK 2007
271
library IEEE; -- Su dung thu vien chuan IEEE
use IEEE.STD_LOGIC_1164.ALL; --Su dung tat ca cac thanh phan trong goi STD_LOGIC_1164
entity hex2led is
Port ( HEX : in std_logic_vector(3 downto 0);
LED : out std_logic_vector(6 downto 0));
end hex2led;
-- Khai bao hoat dong cua hex2Led
architecture Behavioral of hex2led is
begin
with HEX SELect
LED<= "1111001" when "0001", --1
"0100100" when "0010", --2
"0110000" when "0011", --3
"0011001" when "0100", --4
"0010010" when "0101", --5
"0000010" when "0110", --6
"1111000" when "0111", --7
"0000000" when "1000", --8
"0010000" when "1001", --9
"0001000" when "1010", --A
"0000011" when "1011", --b
"1000110" when "1100", --C
"0100001" when "1101", --d
"0000110" when "1110", --E
"0001110" when "1111", --F
"1000000" when others; --0
end Behavioral;
DHBK 2007
272
4.1 Gii thiu v VHDL
V d 1
Thit k mch Test vi 3 u vo 8-bit (In1, In2, In3)
v hai u ra 1 bit (Out1, Out2). Out1=1 khi In1=In2
v Out2=1 khi In1 = In3
In1
In2
In3
Test
Out1
Out2
Compare
A
B
EQ
Compare
A
B
EQ
Test l mt khi gm 2 bn copy ca khi compare
DHBK 2007
273
A
B
EQ
Compare
4.1 Gii thiu v VHDL
V d 1
A[0]
B[0]
A[1]
B[1]
A[7]
B[7]
EQ
XNOR
AND
Thit k khi compare dng mch t hp
DHBK 2007
274
4.1 Gii thiu v VHDL
V d 1
Thit k khi compare dng VHDL
- -Eight bit comparator
entity Compare is
port( A,B: in bit_vector(0 to 7);
EQ: out bit);
end Compare;
architecture Behav1 of Compare is
begin
EQ <= 1 when (A=B) else 0;
end Behav1;
Entity xc nh giao din
vi bn ngoi ca khi cn thit k
u vo v ra c gi l port
Architecture miu t hot ng
v cu trc bn trong ca
khi cn thit k
Ch :
-Mt entity c th c nhiu architecture, mi architecture l mt
cch th hin khc nhau ca cng mt chc nng
- Cc Ports l vector c chiu: vo (in), ra (out), hoc c vo c ra (inout)
DHBK 2007
275
4.1 Gii thiu v VHDL
Component v Instantiation
Biu din Test bng VHDL
entity Test is
port( In1,In2,In3: in bit_vector(0 to 7);
Out1,Out2: out bit);
end Test;
architecture Struct1 of Test is
component Comparator is
port( X,Y: in bit_vector(0 to 7);
Z: out bit);
end component;
begin
Compare1: Comparator port map (X=>In1, Y=>In2, Z=>Out1);
Compare2: Comparator port map (X=>In1,Y=>In3,Z=>Out2);
end Struct1;
2 bn copy ca cng mt
component
Comparator
Ch :
- Hai bn comparator chy song song vi nhau !!!
- y l architecture miu t cu trc ca entity Test
DHBK 2007
276
4.1 Gii thiu v VHDL
Cu hnh (Configuration)
Khi mt entity c nhiu architectures, ta s x dng
architecture no?
Lm th no gn Components vi Entities?
-- Configuration information: architecture selection
-- and component-entity binding
configuration Build1 of Test is
for Struct1
for Compare1: Comparator use entity Compare(Behav1)
port map (A => X, B => Y, EQ => Z);
end for;
for others: Comparator use entity Compare(Behav1)
port map (A => X, B => Y, EQ => Z);
end for;
end for;
end Build1;
DHBK 2007
277
4.1 Gii thiu v VHDL
Khai bo Entity v Architecture
ENTITY:
entity Entity_name is
port(Signal_name: in Signal_type;
Signal_name: out Signal_type);
end Entity_name;
ARCHITECTURE:
architecture Architecture_name of Entity_name is
Khai bo cc tn hiu cc b;
Khai bo cc components;
begin
Cc cu lnh;
end Architecture_name;
DHBK 2007
278
4.1 Gii thiu v VHDL
Khai bo component
COMPONENT:
component Component_name is
port( Signal_name: in Signal_type;
Signal_name: out Signal_type);
end component;
Khai bo copy ca COMPONENT :
Instance_name: Component_name
port map (Signal_list);
Hoc cch th 2, copy trc tip:
Instance_name: Entity_name(Architecture_name)
port map (Signal_list);
DHBK 2007
279
4.1 Gii thiu v VHDL
Khai bo cu hnh
CONFIGURATION:
configuration Config_name of Entity_name is
for Architecture_name
for Instance_name: Component_name use entity
Entity_name(Architecture_name)
port map (Signal_list);
end for;
end for;
end Config_name;
DHBK 2007
280
4.1 Gii thiu v VHDL
V d 2
Biu din cng AND bng VHDL
A
B
C
Y
-- 3-input AND gate
entity AND3 is
port ( A,B,C: in bit;
Y: out bit);
end AND3;
architecture RTL of AND3 is
begin
Y <= 1 when ((A=1) and (B=1) and (C=1)) else 0;
end RTL;
DHBK 2007
281
4.1 Gii thiu v VHDL
V d 2
Biu din cng OR bng VHDL
A
B
C
Y
-- 3-input OR gate
entity OR3 is
port ( A,B,C: in bit;
Y: out bit);
end OR3;
architecture RTL of OR3 is
begin
Y <= 0 when ((A=0) and (B=0) and (C=0)) else 1;
end RTL;
DHBK 2007
282
4.1 Gii thiu v VHDL
V d 2
Biu din cng INV bng VHDL
-- INV gate
entity INV is
port ( A: in bit;
Y: out bit);
end INV;
architecture RTL of INV is
begin
Y <= 1 when (A=0) else 0;
end RTL;
A
Y
DHBK 2007
283
4.1 Gii thiu v VHDL
V d 3
Thit k b MUX 2-1 dng VHDL
A
S
B
Y
entity MUX21 is
port ( A,B,S: in bit;
Y: out bit);
end MUX21;
architecture Behav of MUX21 is
begin
Y <= A when (S=1) else B;
end Behav;
DHBK 2007
284
architecture Struct of MUX21 is
signal U,V,W : bit;
component AND2
port ( X,Y: in bit;
Z: out bit);
end component;
component OR2
port ( X,Y: in bit;
Z: out bit);
end component;
component INV
port ( X: in bit;
Z: out bit);
end component;
begin
Gate1: INV port map (X=>S,Z=>U);
Gate2: AND2 port map (X=>A,Y=>S,Z=>W);
Gate3: AND2 port map (X=>U,Y=>B,Z=>V);
Gate4: OR2 port map (X=>W,Y=>V,Z=>Y);
end Struct;
4.1 Gii thiu v VHDL
V d 3
A
S
B
Y
A
S
B
Y
U
V
W
DHBK 2007
285
4.1 Gii thiu v VHDL
V d 3
Gi s ta mun s dng cc cng AND, OR v INV
v d 2 trong MUX21
configuration Use3InputGates of MUX21 is
for Behav
end for;
for Struct
for Gate1:INV use entity INV(RTL)
port map (A=>X,Y=>Z);
end for;
for All:AND2 use entity AND3(RTL)
port map (A=>X,B=>Y,C=>1,Y=>Z);
end for;
for Gate4:OR2 use entity OR3(RTL)
port map (A=>X,B=>Y,C=>0,Y=>Z);
end for;
end for;
end Use3InputGates;
Entities
A
B
C
Y
A
Y
Components
X
Y
Z
X
Z
DHBK 2007
286
4.1 Gii thiu v VHDL
To Testbench
Testbench l entity dng m phng v kim tra thit
k
MUX21
A
B
Y
S
entity Testbench is
end Testbench;
Testbench khng c port
architecture BehavTest of Testbench is
Signal In1,In2,Select,Out : bit;
begin
mux21_copy: entity MUX21(Behav) port map (In1, In2, Select, Out);
Thu: process is
begin
In1<=0;In2<=1;Select<=0; wait for 20 ns;
Select<=1; wait for 20 ns;
In1<=1;In2<=0; wait for 20 ns;
...
end process;
end BehavTest;
DHBK 2007
287
4.1 Gii thiu v VHDL
S dng li
Thng thng, cc b phn ca thit k ny c th c
dng li trong cc thit k khc
Cc sn phm cng nghip thng bao gm 95% b
phn dng li v ch c 5% l phi thit k mi
VHDL khuyn khch dng li bng khi nim
Packages
Mt Package cha cc nh ngha v hng s, khai
bo component, cc kiu d liu ca ngi s dng, cc
chng trnh con vit bng VHDL
Package c ct trong Library: library thc cht
l mt th mc
DHBK 2007
288
4.1 Gii thiu v VHDL
S dng li
Package interface declaration:
package Package_name is
-- constants
-- user defined types
-- component declarations
-- sub programs
end Package_name;
How to use a package?
use Library_name.Package_name.all;
U1: entity Package_name.Entity_name(Architecture_name);
DHBK 2007
289
4.2 Tn hiu v cc kiu d liu:
Cc kiu d liu c nh ngha
package Standard is
type Bit is (0,1);
type Boolean is (False, True);
type Character is (--ASCII set);
type Integer is range implementation_defined;
type Real is range implementation_defined;
type Bit_vector is (--array of bits);
type String is (--array of characters);
type Time is range implementation_defined;
end Standard;
Bit, Boolean v Character l kiu d liu lit k
DHBK 2007
290
4.2 Tn hiu v cc kiu d liu:
Cc kiu d liu c nh ngha
V d v khai bo cc kiu s nguyn:
type Year is range 0 to 99;
type Memory_address is range 65535 downto 0;
V d v khai bo cc kiu s thc
type Probability is range 0.0 to 1.0;
type Input_level is range -5.0 to 5.0;
Khai bo bit_vector, gi tr c t trong du nhy kp
constant State1: bit_vector(4 downto 0) := 00100;
Kiu chui String l mt chui k t characters; gi tr ca n
c t trong du nhy kp
constant Error_message: string
:= Unknown error: ask your poor sysop for help;
MSB, bit 4
LSB
DHBK 2007
291
4.2 Tn hiu v cc kiu d liu:
Cc kiu d liu c nh ngha
ARCHITECTURE test OF test IS
BEGIN
PROCESS(X)
VARIABLE a : INTEGER;
BEGIN
a := 1; --Ok 1
a := -1; --Ok 2
a := 1.0; --error 3
END PROCESS;
END test;
ARCHITECTURE test OF test IS
SIGNAL a : REAL;
BEGIN
a <= 1.0; --Ok 1
a <= 1; --error 2
a <= -1.0E10; --Ok 3
a <= 1.5E-20; --Ok 4
a <= 5.3 ns; --error 5
END test;
DHBK 2007
292
4.2 Tn hiu v cc kiu d liu:
Cc kiu d liu c nh ngha
Kiu Time l kiu vt l:
type Time is range implementation_defined
units
fs;
ps = 1000 fs;
ns = 1000 ps;
us = 1000 ns;
ms = 1000 us;
sec = 1000 ms;
min = 60 sec;
hr = 60 min;
end units;
Primary unit:
resolution limit
Secondary units
c s dng rt nhiu trong chy m phng
wait for 20 ns;
constant Sample_period: time := 2 ms;
constant Clock_period: time := 50 ns;
DHBK 2007
293
4.2 Tn hiu v cc kiu d liu:
Cc kiu d liu ca ngi s dng
Ngi s dng c th nh ngha kiu vt l nh sau:
type Length is range 0 to 1E9
units
um;
mm = 1000 um;
m = 1000 mm;
km = 1000 m;
mil = 254 um;
inch = 1000 mil;
foot = 12 inch;
yard = 3 foot;
end units;
Primary unit:
resolution limit
Metric secondary units
Imperial secondary units
DHBK 2007
294
4.2 Tn hiu v cc kiu d liu:
Kiu lit k ca ngi s dng
Ngi s dng c th nh ngha ku lit k nh sau:
type FSM_states is (reset, wait, input, calculate, output);
constant reset: bit_vector := 10000;
constant wait: bit_vector := 01000;
constant input: bit_vector := 00100;
constant calculate: bit_vector := 00010;
constant output: bit_vector := 00001;
DHBK 2007
295
PACKAGE example IS
TYPE current IS RANGE 0 TO 1000000000
UNITS
na; --nano amps
ua = 1000 na; --micro amps
ma = 1000 ua; --milli amps
a = 1000 ma; --amps
END UNITS;
TYPE load_factor IS (small, med, big );
END example;
USE WORK.example.ALL;
ENTITY delay_calc IS
PORT ( out_current : OUT current;
load : IN load_factor;
delay : OUT time);
END delay_calc;
ARCHITECTURE delay_calc OF delay_calc IS
BEGIN
delay <= 10 ns WHEN (load = small) ELSE
delay <= 20 ns WHEN (load = med) ELSE
delay <= 30 ns WHEN (load = big) ELSE
delay <= 10 ns;
out_current <= 100 ua WHEN (load = small)ELSE
out_current <= 1 ma WHEN (load = med) ELSE
out_current <= 10 ma WHEN (load = big) ELSE
out_current <= 100 ua;
END delay_calc;
DHBK 2007
296
4.2 Tn hiu v cc kiu d liu:
Kiu mng array
type 1D_array is array (1 to 10) of integer;
type 2D_array is array (5 downto 0, 1 to 10) of real;
TYPE data_bus IS ARRAY(0 TO 31) OF BIT;
VARIABLE X: data_bus;
VARIABLE Y: BIT;
Y := X(0);
Y := X(15);
DHBK 2007
297
4.2 Tn hiu v cc kiu d liu:
Kiu bn ghi record
TYPE optype IS ( add, sub, mpy, div, jmp );
TYPE instruction IS
RECORD
opcode : optype;
src : INTEGER;
dst : INTEGER;
END RECORD;
PROCESS(X)
VARIABLE inst : instruction;
VARIABLE source, dest : INTEGER;
VARIABLE operator : optype;
BEGIN
source := inst.src; --Ok line 1
dest := inst.src; --Ok line 2
source := inst.opcode; --error line 3
operator := inst.opcode; --Ok line 4
inst.src := dest; --Ok line 5
inst.dst := dest; --Ok line 6
inst := (add, dest, 2); --Ok line 7
inst := (source); --error line 8
END PROCESS;
DHBK 2007
298
4.2 Tn hiu v cc kiu d liu:
Standard logic
library IEEE;
use IEEE.Std_logic_1164.All;
type std_logic is (
U, -- uninitialized e.g. after power-up
X, -- strongly driven unknown e.g. after setup violation
0, -- strongly driven logic zero
1, -- strongly driven logic one
Z, -- high impedance e.g. not driven at all
W, -- weakly driven unknown
L, -- weakly driven logic zero
H, -- weakly driven logic one
-); -- dont care
DHBK 2007
299
LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;
PACKAGE memory IS
CONSTANT width : INTEGER := 3;
CONSTANT memsize : INTEGER := 7;
TYPE data_out IS ARRAY(0 TO width) OF std_logic;
TYPE mem_data IS ARRAY(0 TO memsize) OF data_out;
END memory;
LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;
USE WORK.memory.ALL;
ENTITY rom IS
PORT( addr : IN INTEGER;
data : OUT data_out;
cs : IN std_logic);
END rom;
4.2 Tn hiu v cc kiu d liu:
Standard logic
DHBK 2007
300
ARCHITECTURE basic OF rom IS
CONSTANT z_state : data_out := (Z, Z, Z, Z);
CONSTANT x_state : data_out := (X, X, X, X);
CONSTANT rom_data : mem_data :=
( ( 0, 0, 0, 0),
( 0, 0, 0, 1),
( 0, 0, 1, 0),
( 0, 0, 1, 1),
( 0, 1, 0, 0),
( 0, 1, 0, 1),
( 0, 1, 1, 0),
( 0, 1, 1, 1) );
BEGIN
ASSERT addr <= memsize
REPORT addr out of range
SEVERITY ERROR;
data <= rom_data(addr) AFTER 10 ns WHEN cs = 1 ELSE
data <= z_state AFTER 20 ns WHEN cs = 0 ELSE
data <= x_state AFTER 10 ns;
END basic;
4.2 Tn hiu v cc kiu d liu:
Standard logic
DHBK 2007
301
4.2 Tn hiu v cc kiu d liu:
Gn tn hiu
Gn theo v tr, khng gn theo ch s ca mng
signal Down: std_logic_vector (3 downto 0);
signal Up: std_logic_vector (0 to 3);
Up <= Down;
Phng n no sau y l phng n ng?
Up(0)
Up(1)
Up(2)
Up(3)
Down(3)
Down(2)
Down(1)
Down(0)
OR
Up(0)
Up(1)
Up(2)
Up(3)
Down(0)
Down(1)
Down(2)
Down(3)
Tng ng theo v tr
DHBK 2007
302
4.2 Tn hiu v cc kiu d liu:
Gn tn hiu
C th gn bin cho mt phn ca mng
Ch chiu (to hoc downto) phi ging nhau trong cc
php khai bo
signal Bus: std_logic_vector (7 downto 0);
signal A: std_logic_vector (0 to 3);
Cu lnh no sau y ng?
Bus(0 to 3) <= A;
Bus <= A;
Bus(3 downto 0) <= A;
Bus(5 downto 4) <= A(0 to 1);
Chiu bus khng ging nh khai bo
Kch thc mng khc nhau
OK! Bus(3) is driven by A(0)
OK! Bus(5) is driven by A(0)
DHBK 2007
303
4.2 Tn hiu v cc kiu d liu:
Gn tn hiu
C th kt hp hai mng khc nhau
signal Byte_bus: std_logic_vector(7 downto 0);
signal Nibble_busA, Nibble_busB: std_logic_vector(3 downto 0);
Byte_bus <= Nibble_busA & Nibble_busB;
Byte_bus(7)
Byte_bus(6)
Byte_bus(5)
Byte_bus(4)
Byte_bus(3)
Byte_bus(2)
Byte_bus(1)
Byte_bus(0)
Nibble_busA(3)
Nibble_busA(2)
Nibble_busA(1)
Nibble_busA(0)
Nibble_busB(3)
Nibble_busB(2)
Nibble_busB(1)
Nibble_busB(0)
DHBK 2007
304
4.2 Tn hiu v cc kiu d liu:
Gn tn hiu
Not supported by all synthesis tools!!
signal X,Y,Z,T: std_logic_vector(3 downto 0);
signal A,B,C: std_logic;
X <= (A,B,C,C); -- correspondence by position
Y <= (3 => A, 1 downto 0 => C, 2 => B);
Z <= (3 => A, 2 => B, others => C);
T <= (others => 0); -- initialization irrespective of width of T
DHBK 2007
305
4.2 Tn hiu v cc kiu d liu:
Tham s dng chung (Generic)
entity General_mux is
generic (width : integer);
port ( Input : in std_logic_vector (width - 1 downto 0);
Select : in integer range 0 to width - 1;
Output : out std_logic);
end General_mux;
Dng truyn tham s t entity ti cc bn copy ca n
Cc bn copy c th c cc gi tr tham s khc nhau
DHBK 2007
306
4.2 Tn hiu v cc kiu d liu:
Tham s dng chung (Generic)
LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;
ENTITY test IS
GENERIC(rise, fall : TIME; load : INTEGER);
PORT ( ina, inb, inc, ind : IN std_logic;
out1, out2 : OUT std_logic);
END test;
ARCHITECTURE test_arch OF test IS
COMPONENT AND2
GENERIC(rise, fall : TIME; load : INTEGER);
PORT ( a, b : IN std_logic;
c : OUT std_logic);
END COMPONENT;
BEGIN
U1: AND2 GENERIC MAP(10 ns, 12 ns, 3 )
PORT MAP (ina, inb, out1 );
U2: AND2 GENERIC MAP(9 ns, 11 ns, 5 )
PORT MAP (inc, ind, out2 );
END test_arch;
DHBK 2007
307
4.2 Tn hiu v cc kiu d liu:
Tham s dng chung (Generic)
LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;
ENTITY test IS
GENERIC(rise, fall : TIME;
GENERIC(load : INTEGER);
PORT ( ina, inb, inc, ind : IN std_logic;
PORT ( out1, out2 : OUT std_logic);
END test;
ARCHITECTURE test_arch OF test IS
COMPONENT and2
GENERIC(rise, fall : TIME := 10 NS;
GENERIC(load : INTEGER := 0);
PORT ( a, b : IN std_logic;
PORT ( c : OUT std_logic);
END COMPONENT;
BEGIN
U1: and2 GENERIC MAP(10 ns, 12 ns, 3 )
PORT MAP (ina, inb, out1 );
U2: and2 PORT MAP (inc, ind, out2 );
END test_arch;
DHBK 2007
308
4.2 Tn hiu v cc kiu d liu:
Thuc tnh (attributes)
TYPE state IS (0 TO 7);
4 thuc tnh (attributes) c nh ngha sn
_ TLEFT, which returns the left bound of a type or subtype
_ TRIGHT, which returns the right bound of a type or subtype
_ THIGH, which returns the upper bound of a type or subtype
_ TLOW, which returns the lower bound of a type or subtype
PROCESS(x)
SUBTYPE smallreal IS REAL RANGE -1.0E6 TO 1.0E6;
VARIABLE q : real;
BEGIN
q := smallrealLEFT; -- use of left returns 1.0E6
END PROCESS;
DHBK 2007
309
4.2 Tn hiu v cc kiu d liu:
Thuc tnh (attributes)
PROCESS(a)
TYPE bit4 IS ARRAY(0 TO 3) of BIT;
TYPE bit_strange IS ARRAY(10 TO 20) OF BIT;
VARIABLE len1, len2 : INTEGER;
BEGIN
len1 := bit4LENGTH; -- returns 4
len2 := bit_strangeLENGTH; -- returns 11
END PROCESS;
Xem VHDL programming by example bit chi tit hn v thuc tnh
DHBK 2007
310
4.3 Cc php ton s hc v logic:
Cc php ton logic
Cc php ton logic gm: not, and, or, xor, nand, nor
Th t u tin:
not c thc hin u tin
Cc lnh cn li c cng mc u tin
Cc php ton logic c dng cho cc kiu d liu sau
: bit, bit_vector, boolean, std_logic, std_logic_vector,
std_ulogic, std_ulogic_vector
Cc php ton logic c th c dng vi mng:
Cc mng phi c cng kch thc
Cc phn t ca mng tng ng theo v tr
DHBK 2007
311
4.3 Cc php ton s hc v logic:
Cc php ton logic
library IEEE;
use IEEE.Std_Logic_1164.All;
entity Gate is
port( A,B,C: in std_logic;
Z: out std_logic);
end Gate;
architecture Logical of Gate is
begin
Z <= A and not(B or C);
end Logical;
DHBK 2007
312
4.3 Cc php ton s hc v logic:
Cc php ton logic
library IEEE;
use IEEE.Std_Logic_1164.All;
entity Gate is
generic(width : integer range 0 to 31);
port( A,B,C: in std_logic_vector(width-1 downto 0);
Z: out std_logic_vector(width-1 downto 0));
end Gate;
architecture Logical of Gate is
begin
Z <= A and not(B or C);
end Logical;
DHBK 2007
313
Cc php ton s hc v logic:
Cc php ton so snh
Cc php ton so snh gm: <, <=, =>, >, =, /=
Kt qu ca php so snh l kiu boolean
Hai ton hng phi c cng kiu d li
Php ton so snh c th c dng cho mng
Mng c th c kch thc khc nhau!
Cc phn t ca mng s c so snh tng bit, t tri sang
phi
Bit vector c th c so snh nu nh c cng di v khi
so snh gi tr ca bit vector s c i sang s nguyn
khng du so snh
DHBK 2007
314
Cc php ton so snh
library IEEE
use IEEE.Std_Logic_1164.All;
entity Compare is
port( A: in std_logic_vector(3 downto 0);
B: in std_logic_vector(0 to 4);
Z: out boolean);
end Compare;
architecture Relational of Compare is
begin
Z <= TRUE when A<B else FALSE;
end Relational;
entity Testbench
end entity Testbench;
architecture Build1 of Testbench is
signal A: std_logic_vector(3 downto 0) := 1110;
signal B: std_logic_vector(0 to 4) := 10111;
signal Z: boolean;
begin
DUT: entity Compare(Relational)
port map (A => A, B => B, Z => Z);
end Build1;
What is the
value of Z?
TRUE?
FALSE?
1110
is compared to
1011
by bit position
from left to
right;
in the 2nd
position
A(2) > B(1)
hence (A<B)
is FALSE
DHBK 2007
315
4.3 Cc php ton s hc v logic:
Cc php ton s hc
Cc php ton s hc gm: +, -, *, /, ** (exponential),
abs (absolute value), mod (modulus), rem (remainder)
Cc php ton ny dng cho kiu s nguyn v s thc
(tr mod and rem khng dng cho s thc), khng dng
cho kiu bit vector
C hai ton hng phi c cng kiu d liu v c th c
di gi tr khc nhau
Mt bin kiu vt l (v d: time) c th nhn vi mt
s nguyn v mt s thc, kt qu s vn l kiu vt l
DHBK 2007
316
4.3 Cc php ton s hc v logic:
Cc php ton s hc
entity Add is
port ( A,B: in integer range 0 to 7;
Z: out integer range 0 to 14);
end Add;
architecture Behav of Add is
begin
Z <= A + B;
end Behav;
DHBK 2007
317
4.4 Cc lnh song song v tun t :
Cc lnh song song
Tt c cc lnh trong architecture u c thc hin
ng thi (song song) ging nh trong phn cng, cc
cng logic hot ng ng thi
entity Concurrent is
port ( A,B,C,D: in std_logic;
Y,Z: out std_logic);
end Concurrent;
architecture Struct of Concurrent is
begin
NAND1: entity NAND2 port map (A,B,Y);
NAND2: entity NAND2 port map (C,D,Z);
end Struct;
A
B
C
D
Y
Z
Schematic:
Mch s hot ng th no nu nh khai bo NAND1 sau NAND2?
DHBK 2007
318
entity Concurrent is
port ( A,B,C,D: in std_logic;
Y,Z: out std_logic);
end Concurrent;
architecture Struct of Concurrent is
begin
NAND2: entity NAND2 port map (C,D,Z);
NAND1: entity NAND2 port map (A,B,Y);
end Struct;
A
B
C
D
Y
Z
Schematic:
Mch vn hot ng nh c!!!
4.4 Cc lnh song song v tun t :
Cc lnh song song
DHBK 2007
319
4.4 Cc lnh song song v tun t :
Cc lnh song song
A
B
D
Z
Schematic:
T1
entity Concurrent is
port ( A,B, D: in std_logic;
Z: out std_logic);
end Concurrent;
architecture Struct of Concurrent is
signal T1: std_logic;
begin
NAND2: entity NAND2 port map (T1,D,Z);
NAND1: entity NAND2 port map (A,B,T1);
end Struct;
DHBK 2007
320
4.4 Cc lnh song song v tun t :
Process
Nhiu khi phng trnh t hp trong mt cu lnh rt
phc tp, v d:
entity Complex is
port( A,B,C,D,E,F,G,H,I,J:
in std_logic;
Y,Z: out std_logic);
end Complex;
architecture Struct of Complex is
begin
Y <= ((A nand B) nand (C nand D))
when (S = 1) else
((E nand F) nand (G nand H));
Z <= I nand J;
end Struct;
A
B
C
D
E
F
G
H
S
Y
I
J
Z
DHBK 2007
321
4.4 Cc lnh song song v tun t :
Process
V vy ngi ta a ra khi nim process:
Mt process hot ng nh mt cu lnh v c thc hin
ng thi vi cc cu lnh khc
Bn trong mt process, cc lnh c thc hin tun t theo
th t t trn xung di. iu ny gip cho vic chia nh
mt lnh rt phc tp thnh cc lnh n gin hn.
truyn d liu gia cc lnh trong mt process chng ta
c th dng cc bin tm thi variables. Cc bin ny khng
nht thit phi l mt tn hiu vt l no.
process s c thc hin li mi khi c mt s kin (event)
xy ra i vi mt tn hiu (signal) no trong danh sch tn
hiu nhy cm (sensitivity list).
DHBK 2007
322
4.4 Cc lnh song song v tun t :
Process
C php khai bo Process:
Process_name: process (sensitivity_list) is
-- variable declarations;
begin
-- sequential commands
end process;
C php khai bo bin:
variable Variable_name: type;
C php gn gi cho mt bin:
Variable_name := expression;
Gn bin :=
Gn tn hiu <=
DHBK 2007
323
4.4 Cc lnh song song v tun t :
Process
entity Complex is
port( A,B,C,D,E,F,G,H,I,J:
in std_logic;
Y,Z: out std_logic);
end Complex;
architecture Struct of Complex is
begin
Y_process: process (A,B,C,D,E,F,G,H,S) is
variable T1,T2: std_logic;
begin
if (S=1) then
T1 := A nand B;
T2 := C nand D;
else
T1 := E nand F;
T2 := G nand H;
end if;
Y <= T1 nand T2;
end process;
Z <= I nand J;
end Struct;
A
B
C
D
E
F
G
H
S
Y
I
J
Z
T1 and T2 have no
physical meaning since
each refers to 2 different
physical wires
T1 T2
Sensitivity list
DHBK 2007
324
Xt v d sau:
Example: process (A,B,M) is
begin
Y <= A;
M <= B;
Z <= M;
end process;
1. Gi thit B c gi tr mi B
2. Process Example c thc hin. Cc tn hiu ra c ghi nh:
Y <= A; M <= B; Z <= M;
Gi tr M c !!! M ch nhn
gi tr mi cui process
3. Process Example tm dng
Y, M v Z nhn cc gi tr mi Y, M, Z.
4. V M trong sensitivity list, process Example li c thc hin
5. Process Example thc hin: Y <= A; M <= B; Z <= M;
6. Y, M and Z nhn gi tr mi Y, M, Z.
7. Khng c tn hiu no trong sensitivity list thay i, process dng
4.4 Cc lnh song song v tun t :
Process
DHBK 2007
325
4.5 Cc cu trc tun t
Cc cu trc tun t ch c s dng trong process v
chng trnh con!!!
Cc cu trc tun t bao gm: IF, CASE, FOR, NEXT,
EXIT, WAIT, ASSERT
IF statement:
if condition then
-- sequential statements
else
-- sequential statements
end if;
multiple IF statements:
if condition1 then
-- sequential statements
elsif condition2 then
-- sequential statements
elsif condition3 then
-- sequential statements
else
-- sequential statements
end if;
DHBK 2007
326
4.5 Cc cu trc tun t
case Expression is
when Value_1 =>
-- sequential statements
when Value_2 =>
-- sequential statements
-- etc.
end case;
Example: process (A,B,C,X) is
begin
case X is
when 0 to 4 =>
Z <= B;
when 5 =>
Z <= C;
when 7 | 9 =>
Z <= A;
when others =>
Z <= 0;
end case;
end process;
Yu cu:
1. Tt c cc trng hp
phi c xc nh
2. Gi tr value phi l hng s
v phi bit khi thit k
3. Gi tr phi c cng kiu vi
expression
DHBK 2007
327
4.5 Cc cu trc tun t
TYPE vectype IS ARRAY(0 TO 1) OF BIT;
VARIABLE bit_vec : vectype;
CASE bit_vec IS
WHEN 00 =>
RETURN 0;
WHEN 01 =>
RETURN 1;
WHEN 10 =>
RETURN 2;
WHEN 11 =>
RETURN 3;
END CASE;
DHBK 2007
328
4.5 Cc cu trc tun t
for I in 0 to 3 loop
-- sequential statements
end loop;
1. Khng c khai bo bin dng trong vng for v khng
c gn gi tr cho bin
Ch :
FOR i IN 1 to 10 LOOP
i_squared(i) := i * i;
END LOOP;
DHBK 2007
329
4.5 Cc cu trc tun t
entity General_mux is
generic (width : integer);
port ( Input : in std_logic_vector (width - 1 downto 0);
Select : in integer range 0 to width - 1;
Output : out std_logic);
end General_mux;
architecture Behav of General_mux is
begin
Selector: process (Input, Select) is
begin
for I in 0 to width-1 loop
if Select=I then
Output <= Input(I);
end if;
end loop;
end process;
end Behav;
DHBK 2007
330
4.5 Cc cu trc tun t
PROCESS(A, B)
CONSTANT max_limit : INTEGER := 255;
BEGIN
FOR i IN 0 TO max_limit LOOP
IF (done(i) = TRUE) THEN
NEXT;
ELSE
done(i) := TRUE;
END IF;
q(i) <= a(i) AND b(i);
END LOOP;
END PROCESS;
Cu trc NEXT c dng b qua vng lp hin ti nhy ti
vng lp tip theo
DHBK 2007
331
4.5 Cc cu trc tun t
Cu trc EXIT c dng thot ra khi vng lp for
PROCESS(a)
variable int_a : integer;
BEGIN
int_a := a;
FOR i IN 0 TO max_limit LOOP
IF (int_a <= 0) THEN -- less than or
EXIT; -- equal to
ELSE
int_a := int_a -1;
q(i) <= 3.1416 / REAL(int_a * i); -- signal
END IF; -- assign
END LOOP;
y <= q;
END PROCESS;
DHBK 2007
332
4.5 Cc cu trc tun t
PROCESS
BEGIN
WAIT UNTIL clock = 1 AND clockEVENT;
q <= d;
END PROCESS;
PROCESS
BEGIN
WAIT ON a; -- 1.
.
WAIT ON b; -- 2.
.
END PROCESS;
WAIT FOR 10 ns;
WAIT FOR ( a * ( b + c ));
DHBK 2007
333
4.5 Cc cu trc tun t
Bin (Variables)
Variable ch c s dng trong process hoc chng
trnh con
Gi tr ca variable c cp nht ngay lp tc trong
khi gi tr ca tn hiu signal s c cp nht khi
process kt thc
Bin v tn hiu c th c gn gi tr cho nhau
Bin ch c dng tm thi trong process, nu gi tr
ca n mun c truy cp ngoi process ta phi gn
gi tr cho mt tn hiu
DHBK 2007
334
LIBRARY IEEE;
USE IEEE.std_logic_1164ALL;
ENTITY mux IS
PORT (I0, I1, I2, I3, A, B : IN std_logic;
Q : OUT std_logic);
END mux;
ARCHITECTURE mux_behave OF mux IS
SIGNAL sel : INTEGER RANGE 0 TO 3;
BEGIN
B : PROCESS(A, B, I0, I1, I2, I3)
BEGIN
sel <= 0;
IF (A = 1) THEN sel <= sel + 1; END IF;
IF (B = 1) THEN sel <= sel + 2; END IF;
CASE sel IS
WHEN 0 =>
Q <= I0;
WHEN 1 =>
Q <= I1;
WHEN 2 =>
Q <= I2;
WHEN 3 =>
Q <= I3;
END CASE;
END PROCESS;
END mux_behave;
Tm ch sai trong on m !
4.5 Cc cu trc tun t
Bin (Variables)
DHBK 2007
335
4.5 Cc cu trc tun t
Biu din sn ln ca xung nhp
Tm chc nng ca entity What
entity What is
port (D,Clk: in std_logic;
Q: out std_logic);
end What;
architecture RTL of What is
begin
process (D, Clk) is
begin
if (Clk=1) then
Q <= D;
end if;
end process;
end RTL;
With a latch, not with a D-flip-flop!!
When a Clk-event occurs and Clk is low, nothing happens
When a Clk-event occurs and Clk is high, the D input is copied
to the Q output
When a D-event occurs and Clk is high, the D input is copied to
the Q output => hence a latch: when Clk is high, Q follows D
Since there is no ELSE part
the previous Q value has
to be remembered for the case
where Clk=0.
The synthesis tool will hence
infer a latch instead of just
combinatorial logic!!!
Beware of unintended latches
when ELSE parts are omitted
DHBK 2007
336
Cch 1: WAIT UNTIL
entity DFlipFlop is
port (D,Clk: in std_logic;
Q: out std_logic);
end DFlipFlop;
architecture RTL of DFlipFlop is
begin
process is
begin
wait until Clkevent and Clk=1;
Q <= D;
end process;
end RTL;
This is not synthesisable
4.5 Cc cu trc tun t
Biu din sn ln ca xung nhp
DHBK 2007
337
Cch 2: Sensitivity list
entity DFlipFlop is
port (D,Clk: in std_logic;
Q: out std_logic);
end entity DFlipFlop;
architecture RTL of DFlipFlop is
begin
process (D,Clk) is
begin
if (Clkevent and Clk=1) then
Q <= D;
end if;
end process;
end architecture RTL;
4.5 Cc cu trc tun t
Biu din sn ln ca xung nhp
DHBK 2007
338
4.5 Cc cu trc tun t
Biu din Finite State Machine
Wait
00
Up1
01
Up2
10
Up3
11
Down3
11
Down2
10
Down1
01
Start=0
Start=1
Up=0
Start=1
Up=1
Up Start
Next
state
logic
Out
put
logic
State
Reg
Reset
Output
NextState
CurrentState
DHBK 2007
339
Wait
00
Up1
01
Up2
10
Up3
11
Down3
11
Down2
10
Down1
01
Start=0
Start=1
Up=0
Start=1
Up=1
entity FSM is
port ( Start, Up, Reset, Clk: in std_logic;
Output: out std_logic_vector(0 to 1));
end FSM;
architecture Behav of FSM is
type FSM_States is (Wait,Up1,Up2,Up3,Down1,Down2,Down3);
signal CurrentState, NextState : FSM_States;
begin
OutputLogic:
process(CurrentState) is
end process;
NextStateLogic:
process(CurrentState,Start,Up) is
end process;
StateRegister:
process(NextState,Clk,Reset) is
end process;
end Behav;
4.5 Cc cu trc tun t
Biu din Finite State Machine
DHBK 2007
340
Wait
00
Up1
01
Up2
10
Up3
11
Down3
11
Down2
10
Down1
01
Start=0
Start=1
Up=0
Start=1
Up=1
OutputLogic:
process(CurrentState) is
begin
case CurrentState is
when Wait =>
Output <= 00;
when Up1|Down1 =>
Output <= 01;
when Up2|Down2 =>
Output <= 10;
when Up3|Down3 =>
Output <= 11;
end case;
end process;
4.5 Cc cu trc tun t
Biu din Finite State Machine
DHBK 2007
341
Wait
00
Up1
01
Up2
10
Up3
11
Down3
11
Down2
10
Down1
01
Start=0
Start=1
Up=0
Start=1
Up=1
NextStateLogic:
process(CurrentState,Start,Up) is
begin
case CurrentState is
when Wait =>
if (Start=0) then
NextState <= Wait;
elsif (Up=1) then
NextState <= Up1;
else
NextState <= Down3;
end if;
when Up1 =>
NextState <= Up2;
when Up2 =>
NextState <= Up3;
when Up3|Down1 =>
NextState <= Wait;
when Down3 =>
NextState <= Down2;
when Down2 =>
NextState <= Down1;
end case;
end process;
4.5 Cc cu trc tun t
Biu din Finite State Machine
DHBK 2007
342
Wait
00
Up1
01
Up2
10
Up3
11
Down3
11
Down2
10
Down1
01
Start=0
Start=1
Up=0
Start=1
Up=1
StateRegister:
process(NextState,Clk,Reset) is
begin
if Reset=1 then
CurrentState <= Wait;
elseif (Clkevent and Clk=1) then
CurrentState <= NextState;
end if;
end process;
4.5 Cc cu trc tun t
Biu din Finite State Machine
DHBK 2007
343
4.6 Chng trnh con
Gm 2 loi: th tc (procedure) v hm (function)
Th tc: c th tr v nhiu gi tr, c th c cc bin s
l cc tn hiu vo, ra hoc vo\ra
Hm: ch tr v mt gi tr v tt c cc bin s ch c
th l tn hiu vo
Th tc v hm c 2 loi: song song v tun t. Th tc
hoc hm song song c t ngoi process v th tc
hoc hm tun t ch c t trong process
Cc lnh trong chng trnh con c thc hin tun t
ging nh process
DHBK 2007
344
4.6 Chng trnh con
ENTITY convert IS
PORT(I1 : IN log8; --line 2
O1 : OUT INTEGER); --line 3
END convert;
ARCHITECTURE behave OF convert IS
FUNCTION vector_to_int(S : log8) --line 4
RETURN INTEGER is --line 5
VARIABLE result : INTEGER := 0; --line 6
BEGIN
FOR i IN 0 TO 7 LOOP --line 7
result := result * 2; --line 8
IF S(i) = 1 THEN --line 9
result := result + 1; --line 10
END IF;
END LOOP;
RETURN result; --line 11
END vector_to_int;
BEGIN
O1 <= vector_to_int(I1); --line 12
END behave;
DHBK 2007
345
4.6 Chng trnh con
V d khai bo th tc:
procedure increment_reg (variable reg : inout word_32; constant
incr : in integer := 1);
V d gi th tc:
increment_reg(index_reg, offset2); -- add value to index_reg
increment_reg(prog_counter); -- add 1 (default) to prog_counter