Introduction To VHDL - 2014FS
Introduction To VHDL - 2014FS
Ying-Shieh Kung
Professor Sep. 2014 ~ Jan. 2015
2
Library and Data type
LIBRARY
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
USE ieee.std_logic_arith.ALL;
USE ieee.std_logic_unsigned.ALL;
Data type
BIT
BIT_VECTOR (3 downto 0)
STD_LOGIC
STD_LOGIC_VECTOR (3 downto 0)
INTEGER range 0 to 7
3
Data Types and Object Declarations
4
Data Types and Object Declarations
Examples of user-defined enumerated types
5
Data Types and Object Declarations
6
VHDL statement
Conditional Signal Assignment Statement
_label:
WITH _expression SELECT
_signal <= _expression WHEN _constant_value,
_expression WHEN _constant_value,
_expression WHEN _constant_value,
_expression WHEN _constant_value;
7
Example-1: decoder
A 2-Line to 4-Line decoder with active-LOW enable
Truth table
Logic circuit
8
VHDL for decoder
Using Selected Signal Assignment Statement Using Conditional Signal Assignment Statement
9
Example-2: truth table
Truth table
10
Control Flow Statements
_process_label: _process_label:
PROCESS (sensitivity list); PROCESS (sensitivity list);
BEGIN BEGIN
CASE _expression IS IF _expression THEN
WHEN _constant_value => _statement;
_statement; _statement;
_statement; ELSIF _expression THEN
WHEN _constant_value => _statement;
_statement; _statement;
_statement; ELSE
WHEN OTHERS => _statement;
_statement; _statement;
_statement; END IF;
END CASE; END PROCESS _process_label;
END PROCESS _process_label;
11
Control Flow Statements
12
Example-3: Multiplexer
- mux4case.vhd
-- 4-to-1 multiplexer
-- Directs one of four input signals (d0 to d3) to output,
d0
-- depending on status of select bits (s1, s0).
d1
d2
y
-- Define inputs and outputs
d3 ENTITY mux4case IS
PORT(
s1 s0 d0, d1, d2, d3 : IN BIT; -- data inputs
s : IN BIT_VECTOR (1 downto 0); -- select inputs
4 to 1 MUX y : OUT BIT);
END mux4case;
ARCHITECTURE mux4to1 OF mux4case IS
BEGIN
-- Monitor select inputs and execute if they change
PROCESS (s)
s1 s0 y BEGIN
CASE s IS
0 0 d0 WHEN "00“ => y <= d0;
WHEN "01“ => y <= d1;
0 1 d1 WHEN "10“ => y <= d2;
1 0 d2 WHEN "11“ => y <= d3;
WHEN others => y <= '0';
1 1 d3
END CASE;
END PROCESS;
Truth table for a 4 to 1 MUX END mux4to1;
13
Control Flow Statements
14
Example-4: One-bit adder
ENTITY full_add IS
PORT( a, b, c_in : IN BIT;
c_out, sum : OUT BIT); full_add
END full_add; a c_out
b sum
ARCHITECTURE adder OF full_add IS
BEGIN
c_in
c_out <= ((a xor b) and c_in) or (a and b);
sum <= (a xor b) xor c_in;
END adder;
15
Example-5: Four-bit adder
ENTITY add4gen IS add4gen
PORT( c0 : IN BIT;
full_add
a, b : IN BIT_VECTOR(4 downto 1); C(1)
c4 : OUT BIT; a(1) a c_out
b(1) b sum sum(1)
sum : OUT BIT_VECTOR(4 downto 1)); C(0)
c0 c_in
END add4gen;
VHDL code
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
use IEEE.std_logic_signed.all;
library lpm;
use lpm.lpm_components.all;
entity mu is port (
a,b :in std_logic_vector(3 downto 0);
y :out std_logic_vector(7 downto 0);
clk :in std_logic );
end mu; 17
17
architecture main of mu is
signal mula,mulb : std_logic_vector( 3 downto 0);
signal mulr : std_logic_vector( 7 downto 0);
signal CNT :STD_LOGIC_VECTOR(7 downto 0);
begin
u4:lpm_mult
generic map(LPM_WIDTHA=>4, LPM_WIDTHB=>4,
LPM_WIDTHS=>4, LPM_WIDTHP=>8,
LPM_REPRESENTATION=>"signed",
lpm_pipeline=>1)
port map (dataa =>mula, datab =>mulb, result=>mulr, clock=>clk);
process(clk)
begin
IF clk='1' and clk'event THEN
CNT<=CNT+1;
IF CNT=X"00" THEN
mula <= a;
mulb <= b;
ELSIF CNT=X"01" THEN
y <= mulr;
ELSIF CNT=X"02" THEN
CNT <= X"00";
end if;
end if; 18
end process;
end main; 18
Implementation of sum-of-product
computation
Example: Y a1 * x1 a2 * x2 a3 * x3
Parallel processing: a1
x1 x
+
three multipliers a2 One clock
x2 x + y
and execute time
two adders a3
x3 x
y1 A 11 A 12 A 13 x 1
y A A 22 A 23 x 2
2 21
y 3 A 31 A 32 A 33 x 3
Example:
23 4 2 1 40
7 9 15 2 70
10 11 1 3 35
20
20
Method –1 (Parallel Computation)
LIBRARY IEEE;
USE IEEE.std_logic_1164.all;
USE IEEE.std_logic_arith.all;
USE IEEE.std_logic_signed.all;
ENTITY matrix IS
port(CLK,CLK_40n :IN STD_LOGIC;
A11,A12,A13,A21,A22,A23 :IN STD_LOGIC_VECTOR(11 downto 0);
A31,A32,A33,X1,X2,X3 :IN STD_LOGIC_VECTOR(11 downto 0);
Y1,Y2,Y3 :OUT STD_LOGIC_VECTOR(23 downto 0)
);
END matrix;
y1 A 11 A 12 A 13 x 1
y A A 22 A 23 x 2
2 21 21
y 3 A 31 A 32 A 33 x 3
21
Simulation result
22
22
Method –2 ELSIF CNT=X"02" THEN
addb <= mulr;
mula <= A13;
(Sequential Computation) mulb <= X3;
ELSIF CNT=X"03" THEN
LIBRARY IEEE; adda <= addr;
USE IEEE.std_logic_1164.all; addb <= mulr;
USE IEEE.std_logic_arith.all; mula <= A21;
USE IEEE.std_logic_signed.all; mulb <= X1;
LIBRARY lpm; ELSIF CNT=X"04" THEN
USE lpm.LPM_COMPONENTS.ALL; ram1 <= addr;
ENTITY matrix IS adda <= mulr;
port(CLK,CLK_40n :IN STD_LOGIC; mula <= A22;
A11,A12,A13,A21,A22,A23 :IN STD_LOGIC_VECTOR(11 downto 0); mulb <= X2;
A31,A32,A33,X1,X2,X3 :IN STD_LOGIC_VECTOR(11 downto 0); ELSIF CNT=X"05" THEN
Y1,Y2,Y3 :OUT STD_LOGIC_VECTOR(23 downto 0) addb <= mulr;
); mula <= A23;
END matrix; mulb <= X3;
ARCHITECTURE matrix_arch OF matrix IS ELSIF CNT=X"06" THEN
SIGNAL mula,mulb :STD_LOGIC_VECTOR(11 downto 0); adda <= addr;
SIGNAL mulr :STD_LOGIC_VECTOR(23 downto 0); addb <= mulr;
SIGNAL adda,addb,addr,ram1,ram2 :STD_LOGIC_VECTOR(23 downto 0); mula <= A31;
SIGNAL CNT :STD_LOGIC_VECTOR(7 downto 0); mulb <= X1;
ELSIF CNT=X"07" THEN
BEGIN ram2 <= addr;
multiplier: lpm_mult adda <= mulr;
generic map(LPM_WIDTHA=>12,LPM_WIDTHB=>12,LPM_WIDTHS=>12,LPM_WI mula <= A32;
DTHP=>24,LPM_REPRESENTATION=>"signed",LPM_PIPELINE=>1) mulb <= X2;
port map(dataa=> mula,datab=> mulb,clock=> clk,result=> mulr); ELSIF CNT=X"08" THEN
adder: lpm_add_sub addb <= mulr;
generic map(lpm_width=>24,LPM_REPRESENTATION=>"signed",lpm_pipeline=>1) mula <= A33;
port map(dataa=>adda,datab=>addb,clock=> clk,result=>addr); mulb <= X3;
GEN:block ELSIF CNT=X"09" THEN
BEGIN adda <= addr;
PROCESS(CLK_40n) addb <= mulr;
BEGIN ELSIF CNT=X"0A" THEN
IF CLK_40n'EVENT and CLK_40n='1' THEN Y3 <= addr;
CNT<=CNT+1; Y1 <= ram1;
IF CNT=X"00" THEN Y2 <= ram2;
mula <= A11; y 1A 11 A 12 A13 x1 CNT <= X"00";
23
y A x
mulb <= X1; END IF;
A A END IF;
2
ELSIF CNT=X"01" THEN 2 21 22 23
adda <= mulr; END PROCESS;
mula <= A12; y 3
A 31 A 32 A 33
x 3 END BLOCK GEN;
END matrix_arch; 23
mulb <= X2;
Simulation result
24
11 clocks 24
Q-format and fraction operation
The bit number behind the decimal point at the binary
digital value is called the Q-format (or Q-value)
Actually, the decimal point does not exist in the binary system.
25
Multiplication technique for Fixed-point
Fractions Integers
Decimal Binary Decimal
Q3 -0.875 1001 -7 Q0
x Q3 x 0.375 0011 x 3 x Q0
26
Binary Fractions
To use fractions, it necessary to describe them as integers
Fractional 16-bit integer
+1 +32K ( 7FFFH )
+1/2 +16K ( 4000H )
0 0
-1/2 -16K ( C000H )
-1 -32K ( 8000H )
By multiplying a fraction by 32K(32768), a normalized fraction is created
Example 1 :
To represent 0.62 : 62 x 32768 / 100 = 20316
To represent 0.1405: 1405 x 32768 / 10000 = 4604
27
Binary / Fractions transformation
using Matlab
Let
A0 =0.1 ; 0.1*32768= 3277= X"0CCD";
A1 = 0.2001 ; 0.2001*32768= 6557= X"199D";
A2 =-0.4525 ; -0.4525*32768= -14828 = X"C614";
X0=0.2001 ; 0.2001*32768= 6557= X"199D";
X1=0.2001 ; 0.2001*32768= 6557= X"199D";
X2=0.1 ; 0.1*32768= 3277= X"0CCD";
29
LIBRARY IEEE;
USE IEEE.std_logic_1164.all;
USE IEEE.std_logic_arith.all;
USE IEEE.std_logic_signed.all;
LIBRARY lpm;
USE lpm.LPM_COMPONENTS.ALL;
ENTITY STEP IS
port (CLK,CLK_40n : IN STD_LOGIC;
X0,X1,X2 : IN STD_LOGIC_VECTOR(15 downto 0);
Y : OUT STD_LOGIC_VECTOR(15 downto 0)
);
END STEP;
BEGIN
mull: lpm_mult
generic map(LPM_WIDTHA=>16,LPM_WIDTHB=>16,LPM_WIDTHS=>16,LPM_WIDTHP=>32,
LPM_REPRESENTATION=>"signed",LPM_PIPELINE=>1)
port map(dataa=> mula,datab=> mulb,clock=> clk,result=> mulr);
adder1: lpm_add_sub
generic map(lpm_width=>16,LPM_REPRESENTATION=>"signed",lpm_pipeline=>1)
port map(dataa=>adda,datab=>addb,clock=> clk,result=>addr);
30
--Q15
A0 <= X"0CCD";-- 3277 (0.1*32768)
A1 <= X"199D";-- 6557 (0.2001*32768)
A2 <= X"C614";-- -14828 (-0.4525*32768)
GEN:block
BEGIN
PROCESS(CLK_40n)
BEGIN
IF CLK_40n'EVENT and CLK_40n='1' THEN
CNT<=CNT+1;
IF CNT=X"00" THEN
mula <= A0;
mulb <= X0;
ELSIF CNT=X"01" THEN
adda <= mulr(30 downto 15);
mula <= A1;
mulb <= X1;
ELSIF CNT=X"02" THEN
addb <= mulr(30 downto 15);
mula <= A2;
mulb <= X2;
ELSIF CNT=X"03" THEN
adda <= addr;
addb <= mulr(30 downto 15);
ELSIF CNT=X"04" THEN
Y <= addr;
CNT <= X"00";
END IF;
END IF;
END PROCESS;
END BLOCK GEN;
END STEP_arch; 31
Simulation results in Quartus II
32
Simulation results in ModelSim
X“01E4"
33
Case study for Computing SoP
Design a VHDL to compute
Y = A0*X0+A1*X1+A2*X2
34
Case-1 xxxxxxxxxxxxxxxx 16bit Q0
x xxxxxxxxxxxxxxxx 16bit Q0
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
15:0 16bit Q0
error
Case-2 x.xxxxxxxxxxxxxxx 16bit Q15
x x.xxxxxxxxxxxxxxx 16bit Q15
xx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
error 30:15 error
16bit Q15
36
Co-Simulation result for SoP with16bit Q0
fixdt(1,16,0)
O: unsigned
1: signed
Q format (Q0)
data length (16bits)
37
Case-2: VHDL code for SoP (Sum of Product) with16bit Q15
38
Co-Simulation result for SoP with16bit Q15
fixdt(1,16,15)
O: unsigned
1: signed
Q format (Q15)
data length (16bits)
function y = fcn(x1,x2,x3,a1,a2,a3) 39
y=a1*x1+a2*x2+a3*x3
Case-3: VHDL code for SoP (Sum of Product) with 24bit Q15
40
Co-Simulation result for SoP with 24bit Q15
fixdt(1,24,15)
O: unsigned
1: signed
Q format (Q15)
data length (24bits)
function y = fcn(x1,x2,x3,a1,a2,a3) 41
y=a1*x1+a2*x2+a3*x3
Homework-1
1. Write a VHDL code (16bitsQ15) to compute the
following polynomial equation.
Homework format :
x3 x5 x7
y sin( x) x (word file)
3! 5! 7!
1. Topic
and x=0.1, 0.2, 0.3, 0.4 2. Technique description
3. VHDL code
Hint: (in Matlab) signed 4. Simulation result
format hex 16 bits
Q15 5. Discussion
fi(1/(3*2),1,16,15)
fi(1/(5*4*3*2),1,16,15) Result : X”0111”
fi(1/(7*6*5*4*3*2),1,16,15)
42
Homework-1
x2 / 2
ye with 1 x 1 and 0.4 1
Due date: Oct. 28 (Monday) (Every student need to prepare a presentation in class)
Homework (ppt file) has to send to my_digital_learning.
43
Topic-2 : Matlab simulation
clear; x2 / 2
s=0.4; ye eu
ma=20; % order
u2 u3 um
xx=-1:0.01:1; 1 u ...
xi=length(xx) 2! 3! m!
for i=1:xi
x=-xx(i)^2/s^2; Good Bad
1 1.2
y1(i)=exp(x); 0.9
1
mi=1; 0.8
0.6
for m=1:ma 0.6
mi=mi*m;
0.5
end; 0.2
0
0.1
y2(i)=y; -0.2
0 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
end; -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
plot(xx,y1,xx,y2)
Transfer function response
with step input
plant
x(s) 2n
連續系統 y(s)
s 2 2n s 2n
Continuous system
n 10, 0.5
t samp 0.01s
plant
數位系統 x(n) a 0 a 1z 1 a 2 z 2
y(n)
1 b1z 1 b 2 z 2
Digital system
a0 = 0.002575,a1=0.00485,
a2=0.002575,b1=-1.895,b2=0.905
45
clear;clf;format long;
za=0.5; Step Response
1.4
wn=10;
num=wn^2; 1.2
den=[1,2*za*wn,wn^2]; 1
S1=tf(num,den)
D1=c2d(S1,0.01,'tustin') 0.8
Amplitude
step(S1) 0.6
Pause
0.4
% 0.2
numd=[0.002575,0.00485,0.002575];
0
dend=[1,-1.895,0.905]; 0 20 40 60 80 100 120
Time (samples)
Dstep(numd,dend)
Transfer response with step input
Digital system
1 Y ( z 1 ) a 0 a 1 z 1 a 2 z 2
G (z ) 1
X (z ) 1 b 1 z 1 b 2 z 2
The difference equation:
47
Transfer response with step input
LIBRARY IEEE;
USE IEEE.std_logic_1164.all;
Use IEEE.numeric_std.all;
USE IEEE.std_logic_arith.all;
USE IEEE.std_logic_signed.all;
LIBRARY lpm;
USE lpm.LPM_COMPONENTS.ALL;
ENTITY STEP IS
port(CLK,CLK_40n :IN STD_LOGIC:='0';
X0 :IN STD_LOGIC_VECTOR(15 downto 0):=(others =>'0');
Yn :BUFFER STD_LOGIC_VECTOR(15 downto 0):=(others =>'0'));
END STEP;
mull: lpm_mult
generic map(LPM_WIDTHA=>16,LPM_WIDTHB=>16,LPM_WIDTHS=>16,LPM_WIDTHP=>32,
LPM_REPRESENTATION=>"SIGNED",LPM_PIPELINE=>1)
port map(dataa=> mula,datab=> mulb,clock=> clk,result=> mulr);
adder1: lpm_add_sub
generic map(lpm_width=>16,LPM_REPRESENTATION=>"SIGNED",lpm_pipeline=>1)
port map(dataa=>adda,datab=>addb,clock=> clk,result=>addr);
--Q15
A0 <= X"002A";--42 (0.00128)
A1 <= X"0050";--80 (0.00244)
A2 <= X"002A";--42 (0.00128)
B1 <= X"7948";--31048 (0.94754)
B2 <= X"C614";---14828 (-0.4525) 48
Transfer response with step input
GEN:block ELSIF CNT=X"05" THEN
BEGIN adda <= addr;
PROCESS(CLK_40n) addb <= mulr(30 downto 15);
BEGIN
IF CLK_40n'EVENT and CLK_40n='1' THEN ELSIF CNT=X"06" THEN
CNT<=CNT+1; Yn <= (addr(15) & addr(13 downto 0) & '0');
ELSIF CNT=X"07" THEN
IF CNT=X"00" THEN Y1 <= Yn;
mula <= A0; Y2 <= Y1;
mulb <= X0; X2 <= X1;
ELSIF CNT=X"01" THEN X1 <= X0;
adda <= mulr(30 downto 15); ELSIF CNT=250000 THEN
mula <= A1; CNT <= X"00000";
mulb <= X1; END IF;
ELSIF CNT=X"02" THEN END IF;
addb <= mulr(30 downto 15); END PROCESS;
mula <= A2; END BLOCK GEN;
mulb <= X2; END STEP_arch;
ELSIF CNT=X"03" THEN
adda <= addr;
addb <= mulr(30 downto 15);
mula <= B1;
mulb <= Y1; mulr(30..0)
ELSIF CNT=X"04" THEN
A0
X + + + + Yn
adda <= addr; mulr(30..0) mulr(30..0) mulr(30..0) mulr(30..0) addr(15) &
addb <= mulr(30 downto 15); A1
X addr(13..0)
mula <= B2; X0 A2 B1 B2 & '0'
X X X
mulb <= Y2;
X1 Yn =>Y2;
1 X1 =>X2;
X2 Yn Y2 X0 =>x1;
After the bilinear transform with sampling time 0.01, its discrete transfer function is
y(n)/4 = 23871*y(n-1)-26092*y(n-2)+12673*y(n-3)-2305*y(n-4)+3*x(n)+11*x(n-1)
+17*x(n-2)+11*x(n-3)+3*x(n-4)
53
Scope-2 Scope-4
Scope-1 Scope
54
Homework format :
Due date: Dec. 2 (Monday)
(ppt file) (Every student need to prepare a presentation in class)
1. Topic
2. Technique description
Homework (ppt file) needs to be submitted from the
website of my_digital_learning.
3. VHDL code
4. Simulation result
5. Discussion
PI controller design
Parallel processing :
PI controller 16
e1
up(n) Ki 16 × ui
Kp mulr1(30..15)
ui 16 + 16
+ Yn
Xn(n) e(n) 1 + Yn(n)
+ Ki z ui(n) + e1 mulr1(30..15)
Xn 16 e
1
- 1 z -feed 16 + 16
16 × up
Kp
feed Sequential processing using
Finite State Machine (FSM)
PI controller :
u p (n) K p e(n) mulr1(30..15)
e1 ui
ui (n) ui (n - 1) K i e(n - 1)
X + + Yn
e1
Ki ui up
Yn(n) u p (n) ui (n) Xn e mulr1(30..15)
+ X
ui ( n 1 ) K p e( n ) K i e( n 1 )
-feed Kp
S0 S1 S 2 S3 S4 S5 S6 S 7 S8 S 9 S10
ARCHITECTURE PI_PLANT of PI IS
SIGNAL e1 : STD_LOGIC_VECTOR(15 downto 0):=(others=>'0');
SIGNAL mula,mulb : STD_LOGIC_VECTOR(15 downto 0):=(others=>'0');
SIGNAL mulr : STD_LOGIC_VECTOR(31 downto 0):=(others=>'0');
SIGNAL adda,addb,addr : STD_LOGIC_VECTOR(15 downto 0):=(others=>'0');
SIGNAL ui : STD_LOGIC_VECTOR(15 downto 0):=(others=>'0');
SIGNAL error : STD_LOGIC_VECTOR(15 downto 0):=(others=>'0');
SIGNAL CNT : STD_LOGIC_VECTOR(15 downto 0):=(others=>'0');
BEGIN
mul: lpm_mult
generic map(LPM_WIDTHA=>16,LPM_WIDTHB=>16,LPM_WIDTHS=>16,LPM_WIDTHP=>32,
LPM_REPRESENTATION=>"SIGNED",LPM_PIPELINE=>1)
port map(dataa=> mula,datab=> mulb,clock=> clk,result=> mulr);
adder: lpm_add_sub
generic map(lpm_width=>16,LPM_REPRESENTATION=>"SIGNED",lpm_pipeline=>1)
port map(dataa=>adda,datab=>addb,clock=> clk,result=>addr);
60
PROCESS(CLK)
BEGIN
IF CLK'EVENT and CLK='1' THEN
CNT<=CNT+1;
IF CNT=X"0000" THEN
mula <= e1;
mulb <= Ki;
ELSIF CNT=X"0002" THEN
adda <= mulr(30 downto 15);
addb <= ui;
ELSIF CNT=X"0004" THEN
ui <= addr;
adda <= Xn;
addb <= -feed;
ELSIF CNT=X"0006" THEN
e1 <= addr;
mula <= addr;
mulb <= Kp;
ELSIF CNT=X"0008" THEN
adda <= mulr(30 downto 15);
addb <= ui;
ELSIF CNT=X"000A" THEN
Yn <= addr;
ELSIF CNT=2500 THEN -- 100hz
test1 <= e1;
test2 <= mulr(30 downto 15);
test3 <= ui;
CNT <= X"0000";
END IF;
END IF;
END PROCESS;
END PI_PLANT;
61
Kp=0.7, Ki=0.04
Kp=0.7, Ki=0.03
62
library ieee;
VHDL for a ROM chip use ieee.std_logic_1164.all;
using logic gates
entity rom16x7 is
port( address : IN INTEGER range 0 to 15;
data : OUT std_logic_vector (6 downto 0));
end rom16x7;
63
VHDL for a ROM chip
using RAM in FPGA
LIBRARY IEEE;
USE IEEE.std_logic_1164.all;
LIBRARY lpm;
USE lpm.LPM_COMPONENTS.ALL;
USE IEEE.std_logic_arith.all;
USE IEEE.std_logic_signed.all;
ENTITY new_array IS
PORT(clk : in std_logic;
inaddress : in std_logic_vector(7 downto 0);
data : out std_logic_vector(7 downto 0));
end new_array;
Process(clk)
begin
if clk'event and clk='1' then
data <= data1;
end if;
end process;
end new_array_arch;
64
Simulation results
65
LIBRARY work;
Microcontroller design USE work.all; micro.vhd
In Chapter 14. LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
USE ieee.std_logic_arith.ALL;
USE ieee.std_logic_unsigned.ALL;
---------------------------------------------------------------------------------------------------
-Input and Output Define\
----------------------------------------------------------------------------------------------------
entity micro is
port(
start : in std_logic;
x_in : in signed(7 downto 0);
z_out : out signed(7 downto 0);
done : out std_logic;
clk : in std_logic );
end micro;
---------------------------------------------------------------------------------------------------
--------- External Function Define
---------------------------------------------------------------------------------------------------
ARCHITECTURE structural OF micro IS
component microdata IS
PORT( x_in : IN SIGNED(7 DOWNTO 0);
fld_a, fld_b, fld_c : IN UNSIGNED(2 DOWNTO 0);
alu_op : IN UNSIGNED(1 DOWNTO 0);
ldr_in, ldr_out, selr_in : IN STD_LOGIC;
ldrf : IN STD_LOGIC;
zero, neg, cy :: OUT STD_LOGIC;
z_out : OUT SIGNED(7 DOWNTO 0);
clk : IN STD_LOGIC;
control_con : in STD_LOGIC
);
END component; 66
component microctrl IS
PORT(
start,zero,neg,cy : IN STD_LOGIC;
fld_a,fld_b,fld_c : OUT UNSIGNED(2 DOWNTO 0);
alu_op : OUT UNSIGNED(1 DOWNTO 0);
ldr_in,ldr_out : OUT STD_LOGIC;
selr_in,ldrf,done : OUT STD_LOGIC;
clk : IN STD_LOGIC;
control_con : out STD_LOGIC
);
END component;
---------------------------------------------------------------------------------------------------
----- Parameter Define
---------------------------------------------------------------------------------------------------
-- system parameter --
signal fld_a, fld_b, fld_c : unsigned(2 downto 0);
signal alu_op : unsigned(1 downto 0);
signal zero, neg, cy : std_logic;
signal ldr_in, ldr_out : std_logic;
signal selr_in, ldrf : std_logic;
signal control_con : std_logic;
---------------------------------------------------------------------------------------------------
----- Main Program
---------------------------------------------------------------------------------------------------
begin
u1: microdata
port map(x_in, fld_a, fld_b, fld_c, alu_op, ldr_in, ldr_out, selr_in, ldrf, zero, neg, cy, z_out, clk, control_con);
u2: microctrl
port map(start, zero, neg, cy, fld_a, fld_b, fld_c, alu_op, ldr_in, ldr_out, selr_in, ldrf, done, clk, control_con);
end structural ;
67
--microdata -------------------------------------------------------------------------
LIBRARY ieee;
USE ieee.std_logic_1164.all; microdata.vhd
USE ieee.std_logic_arith.all;
-------------------------------------------------------------------------
ENTITY microdata IS
PORT( x_in : IN SIGNED(7 DOWNTO 0);
fld_a, fld_b, fld_c : IN UNSIGNED(2 DOWNTO 0);
alu_op : IN UNSIGNED(1 DOWNTO 0);
ldr_in, ldr_out, selr_in : IN STD_LOGIC;
ldrf : IN STD_LOGIC;
zero, neg, cy : OUT STD_LOGIC;
z_out : OUT SIGNED(7 DOWNTO 0);
clk : IN STD_LOGIC;
control_con : IN STD_LOGIC );
END microdata;
-------------------------------------------------------------------------
ARCHITECTURE behavioral OF microdata IS
TYPE reg_fileT IS ARRAY(0 TO 7) OF SIGNED(7 DOWNTO 0);
SIGNAL RF : reg_fileT;
SIGNAL r_in : SIGNED(7 DOWNTO 0);
BEGIN
PROCESS (clk)
VARIABLE A,B,C : SIGNED(7 DOWNTO 0);
VARIABLE alu_out : SIGNED(7 DOWNTO 0);
VARIABLE zzero,nneg,ccy : STD_LOGIC;
-------------------------------------------------------------------------
procedure alu (
zzero,nneg,ccy : out STD_LOGIC;
alu_out : out SIGNED(7 DOWNTO 0);
a,b : in SIGNED(7 DOWNTO 0);
alu_op : in UNSIGNED(1 DOWNTO 0)
) is
variable alu_out_reg : SIGNED(8 DOWNTO 0); 68
begin
case alu_op is
when "00" => alu_out_reg := ("0" & a) + ("0" & b);
alu_out := alu_out_reg(7 downto 0);
ccy := alu_out_reg(8);
when "01" => alu_out :=a-b;
-- when "10" => alu_out_reg :=("0" & a) xor ("0" & b);
when "11" => alu_out :=a+1;
when others => null;
end case;
end alu;
-------------------------------------------------------------------------
BEGIN
A := RF(CONV_INTEGER(fld_a));
B := RF(CONV_INTEGER(fld_b));
if control_con='0' then
alu(zzero,nneg,ccy,alu_out,A,B,alu_op);
zero <= zzero;
neg <= nneg;
cy <=ccy;
end if;
70
PROCESS (clk)
VARIABLE index : UNSIGNED(13 DOWNTO 0);
BEGIN
IF (clk'EVENT AND clk = '1') THEN
IF (mode = '0') THEN csar <= csar+1;
ELSE
CASE condition IS
WHEN "00" => IF (start = cond_val) THEN
index := uinstr(13 DOWNTO 0);
csar <= CONV_INTEGER(index);
ELSE csar <= csar+1;
END IF;
WHEN "01" => IF (zero = cond_val) THEN
index := uinstr(13 DOWNTO 0);
csar <= CONV_INTEGER(index);
ELSE csar <= csar+1;
END IF;
WHEN "10" => IF (neg = cond_val) THEN
index := uinstr(13 DOWNTO 0);
csar <= CONV_INTEGER(index);
ELSE csar <= csar+1;
END IF;
WHEN "11" => IF (cy = cond_val) THEN
index := uinstr(13 DOWNTO 0);
csar <= CONV_INTEGER(index);
ELSE csar <= csar+1;
END IF;
WHEN OTHERS => NULL;
END CASE;
END IF;
END IF;
END PROCESS;
-------------------------------------------------------------------------
71
-- Continued in the next figure.--
PROCESS(csar)
TYPE csarray IS ARRAY(0 TO cssize-1) OF UNSIGNED(17 DOWNTO 0);
VARIABLE cs : csarray
-- Here the microprogram as initial contents of ARRAY cs--
:= (0 => "001000000000100010",
1 => "100000000000000001",
2 => "011000000011110001",
3 => "000000000010100100",
4 => "001000000111100000",
5 => "000010010010100010",
6 => "111000000000001000",
7 => "011111000111100000",
8 => "000011011011100000",
9 => "111000000000000101",
10 => "000111000111101000",
11 => "111000000000000000");
-----------------------------------------------------------------------
BEGIN
uinstr <= cs(csar);
control_con <= uinstr(17);
CASE uinstr(17) IS
WHEN '0' => alu_op <= uinstr(16 DOWNTO 15);
fld_a <= uinstr(14 DOWNTO 12);
fld_b <= uinstr(11 DOWNTO 9);
fld_c <= uinstr( 8 DOWNTO 6);
ldrf <= uinstr(5); ldr_out <= uinstr(3);
ldr_in <= uinstr(4); selr_in <= uinstr(2);
IF (uinstr(1) = '1') THEN done <= '1'; END IF;
IF (uinstr(0) = '1') THEN done <= '0'; END IF;
WHEN '1' => ldrf <= '0'; ldr_out <= '0'; ldr_in <= '0';
WHEN OTHERS => NULL;
END CASE;
END PROCESS; 72
END behav_microprogr;
Final-homework ModelSim
software. ud(n)
K d (1 z 1 )
3. simulate it using ModelSim
and SimuLink environment.
Fig.1
Y (s) n2
Plant: G (s) 2
U (s) s 2 n s n2
with n 10, 0.5
U ( z 1 ) K i z 1
PID controller: 1
K 1
K d (1 z 1 )
1 z
p
E(z )
error: e(n) = x(n)-y(n) Input: x(n) is step command with 0.5 magnitude
73
For your
reference