Beruflich Dokumente
Kultur Dokumente
q2
d
rst
q
rst
q3
d
q
rst
qN
d
q
rst
clk
rst
Two generic solutions are presented next. In the first, a regular behavioral code is used, while a structural
code (using the COMPONENT construct) is presented in the second.
Solution 1: With a regular behavioral code.
------------------------------------------------LIBRARY ieee;
USE ieee.std_logic_1164.all;
------------------------------------------------ENTITY shift_register IS
GENERIC (N: INTEGER := 4); --number of stages
PORT (d, clk, rst: IN STD_LOGIC;
q: BUFFER STD_LOGIC_VECTOR(1 TO N));
END shift_register;
------------------------------------------------ARCHITECTURE shift_register OF shift_register IS
BEGIN
PROCESS (clk, rst)
BEGIN
IF (rst='1') THEN
q <= (OTHERS => '0');
Digital Electronics and Design with VHDL, Volnei A. Pedroni, ElsevierMorgan Kaufmann, 2008
ELSIF (clk'EVENT AND clk='1') THEN
q <= d & q(1 TO N-1);
END IF;
END PROCESS;
END shift_register;
-------------------------------------------------
Simulation results (from the circuit synthesized with either code above) are shown next. Observe that after
every positive clock edge the input data train advances one position. Observe also the operation of the reset
signal, which is asynchronous.
Digital Electronics and Design with VHDL, Volnei A. Pedroni, ElsevierMorgan Kaufmann, 2008
Parity
generator
b(7:0)
parity
Solution
A VHDL code for this problem is shown below. As before, the industry-standard data type (STD_LOGIC) was
employed to specify all input and output signals.
---------------------------------------------------LIBRARY ieee;
USE ieee.std_logic_1164.all;
---------------------------------------------------ENTITY parity_generator IS
PORT (a: IN STD_LOGIC_VECTOR(6 DOWNTO 0);
parity: IN STD_LOGIC;
b: OUT STD_LOGIC_VECTOR(7 DOWNTO 0));
END parity_generator;
---------------------------------------------------ARCHITECTURE parity_generator OF parity_generator IS
BEGIN
PROCESS (a, parity)
VARIABLE temp: STD_LOGIC;
BEGIN
temp := '0';
FOR i IN a'RANGE LOOP
temp := temp XOR a(i);
END LOOP;
b(7) <= parity XNOR temp;
b(6 DOWNTO 0) <= a(6 DOWNTO 0);
END PROCESS;
END parity_generator;
----------------------------------------------------
Simulation results, obtained from the circuit synthesized with the code above, are shown next. The first graph
is for odd parity (parity='0'), while the second is for even parity (parity='1')
Digital Electronics and Design with VHDL, Volnei A. Pedroni, ElsevierMorgan Kaufmann, 2008
Digital Electronics and Design with VHDL, Volnei A. Pedroni, ElsevierMorgan Kaufmann, 2008
2**(N1)1. However, as explained in Section 3.2 (and mentioned in the discussion above), this does not
affect the hardware. This fact can be observed in the simulation results from the two codes below, which are
exactly alike (only the interpretation of the results changes, because bitwise the plots are exactly the same).
Solution 1: For an unsigned system
--------------------------------------------------------------LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE ieee.std_logic_unsigned.all;
--------------------------------------------------------------ENTITY adder_sub IS
GENERIC (N: INTEGER := 8); --number of input bits
PORT (a, b: IN STD_LOGIC_VECTOR(N-1 DOWNTO 0);
sum, sub: OUT INTEGER RANGE 0 TO 2**N-1);
END adder_sub;
--------------------------------------------------------------ARCHITECTURE adder_sub OF adder_sub IS
BEGIN
sum <= CONV_INTEGER (a + b);
sub <= CONV_INTEGER (a - b);
END adder_sub;
---------------------------------------------------------------
Simulation results from Solution 1 are shown below. Note that, as expected, overflow can occur (for example,
120+150=270 27028=14).
Simulation results from Solution 2 are presented next. Note that these results are exactly the same as those
obtained in Solution 1.
Digital Electronics and Design with VHDL, Volnei A. Pedroni, ElsevierMorgan Kaufmann, 2008
PWM
duty
clk
y
duty
0
1
2
3
4
5
6
7
Duty cycle
0/7 (0%)
1/7 (14,3%)
2/7 (28,6%)
3/7 (42.9%)
4/7 (57.1%)
5/7 (71.4%)
6/7 (85.7%)
7/7 (100%)
Solution
A VHDL code for this problem is shown below. Note that to make it work for any PWM size, the number of
control bits (N) was entered using the GENERIC attribute.
--------------------------------------------------ENTITY pwm IS
GENERIC (N: INTEGER := 3); -- # of control bits
PORT (clk: IN STD_LOGIC;
duty: IN INTEGER RANGE 0 TO 2**N-1;
y: OUT STD_LOGIC);
END pwm;
--------------------------------------------------ARCHITECTURE pwm OF pwm IS
BEGIN
PROCESS (clk)
VARIABLE count: INTEGER RANGE 0 TO 2**N;
BEGIN
IF (clk'EVENT AND clk='1') THEN
count := count + 1;
IF (count=duty) THEN
y <= '1';
ELSIF (count=2**N) THEN
y <= '0';
count := 0;
END IF;
END IF;
END PROCESS;
END pwm;
---------------------------------------------------
Simulation results from the circuit inferred from the code above are shown next. In this simulation, N=3 bits
were used to represent duty, so it resembles exactly the case depicted in the previous figure. Waveforms for
duty=2 and duty=6 are depicted in the plots.
Digital Electronics and Design with VHDL, Volnei A. Pedroni, ElsevierMorgan Kaufmann, 2008
clk
clk
y
T
Solution
As in Section 23.2, the circuit can be divided into two sections, one producing the intermediate signal x (with
glitches) in the figure below, and the other the final signal, y (without glitches). Hence both FSMs can be
modeled with three states (A, B, C). The first circuit must produce x=1 when in state A, x=clk when in state
B, and again x=1 when in state C, while the second circuit must produce y=x when in A, y=1 when in B
(to eliminate the glitch), and again y=x when in C. The corresponding quasi-single-machine is depicted on
the right of the figure below.
Waveform
generator
B
clk
C
1
MUX2
A
1
clk
MUX1
clk
Glitch
remover
sel1
sel2
T
A
x
B
1
C
x
Comb.
logic
DFFs
FSM1
Comb.
logic
(FSM2)
DFFs
Digital Electronics and Design with VHDL, Volnei A. Pedroni, ElsevierMorgan Kaufmann, 2008
IF (clk'EVENT AND clk='0') THEN
pr_state1 <= nx_state1;
END IF;
END PROCESS;
----- Upper section of FSM1: ----PROCESS (pr_state1, clk)
BEGIN
CASE pr_state1 IS
WHEN A =>
sel1 <= 1;
nx_state1 <= B;
WHEN B =>
sel1 <= 0;
nx_state1 <= C;
WHEN C =>
sel1 <= 1;
nx_state1 <= A;
END CASE;
END PROCESS;
----- MUX1: ---------------------x <= clk WHEN sel1=0 ELSE '1';
----- Quasi-FSM2: ---------------PROCESS (clk)
BEGIN
IF (clk'EVENT AND clk='1') THEN
CASE pr_state1 IS
WHEN A =>
sel2 <= 0;
WHEN B =>
sel2 <= 1;
WHEN C =>
sel2 <= 0;
END CASE;
END IF;
END PROCESS;
----- MUX2: ---------------------y <= x WHEN sel2=0 ELSE '1';
END sig_generator;
----------------------------------------------
Simulation results are presented below. Note that, as expected, glitches do occur in x, but are eliminated in y.
Digital Electronics and Design with VHDL, Volnei A. Pedroni, ElsevierMorgan Kaufmann, 2008
Solution
The same input signal used in example 19.3 (see Figure 19.7) will be employed here (with the obvious
difference that now VHDL will be used to create it instead of a graphical tool). Such a signal (x) is shown in
the figure below, which also exhibits the expected outputs, y and y, where y represents the functionalanalysis output (no propagation delays types I and III testbenches), while y represents the timing-analysis
output (with propagation delays types II and IV testbenches).
x
00000000
00000001
160ns
00000010
320ns
11111100
480ns
01111111
640ns
11111111
800ns
00011000
960ns
y
y
Both files (design and test) needed for this simulation are presented below. Note that the design file is simply
a copy of that in Example 19.3. The test file, on the other hand, contains a stimulus-generation process
(responsible for creating x), plus two processes responsible for the automated analysis of the results (one
process creates the template, which is similar to the waveform y shown above, with a fixed propagation
delay of 10ns, while the other is responsible for comparing the template to the actual output, performed at
every 20ns).
------ Design file: ------------------------------LIBRARY ieee;
USE ieee.std_logic_1164.all;
--------------------------------------------------ENTITY parity_detector IS
GENERIC (N: INTEGER := 8); --number of bits
PORT (x: IN STD_LOGIC_VECTOR(N-1 DOWNTO 0);
y: OUT STD_LOGIC);
END parity_detector;
--------------------------------------------------ARCHITECTURE structural OF parity_detector IS
SIGNAL internal: STD_LOGIC_VECTOR(N-1 DOWNTO 0);
BEGIN
internal(0) <= x(0);
gen: FOR i IN 1 TO N-1 GENERATE
internal(i) <= internal(i-1) XOR x(i);
END GENERATE;
y <= internal(N-1);
END structural;
-------------------------------------------------------- Test file: --------------------------------LIBRARY ieee;
USE ieee.std_logic_1164.all;
--------------------------------------------------ENTITY test_parity_detector IS
GENERIC (N: INTEGER := 8); -- # of input bits
END test_parity_detector;
--------------------------------------------------ARCHITECTURE test OF test_parity_detector IS
COMPONENT parity_detector IS
PORT (x: IN STD_LOGIC_VECTOR(N-1 DOWNTO 0);
y: OUT STD_LOGIC);
END COMPONENT;
-----------------------------SIGNAL x: STD_LOGIC_VECTOR(N-1 DOWNTO 0) := "00000000";
SIGNAL y: STD_LOGIC;
SIGNAL template: STD_LOGIC := '0';
CONSTANT delay: TIME := 10 ns; --estimated propag. delay
------------------------------
Digital Electronics and Design with VHDL, Volnei A. Pedroni, ElsevierMorgan Kaufmann, 2008
BEGIN
dut: parity_detector PORT MAP (x=>x, y=>y);
----- Generation of x: ------PROCESS
BEGIN
WAIT FOR 160 ns;
x <= "00000001";
WAIT FOR 160 ns;
x <= "00000010";
WAIT FOR 160 ns;
x <= "11111100";
WAIT FOR 160 ns;
x <= "01111111";
WAIT FOR 160 ns;
x <= "11111111";
WAIT FOR 160 ns;
x <= "00011000";
WAIT;
END PROCESS;
----- generate template: ----PROCESS
BEGIN
WAIT FOR delay; --estimated propag. delay
WAIT FOR 160 ns;
template <= '1';
WAIT FOR 320 ns;
template <= '0';
WAIT FOR 160 ns;
template <= '1';
WAIT FOR 160 ns;
template <= '0';
WAIT;
END PROCESS;
----- verify output: --------PROCESS
BEGIN
WAIT FOR 20 ns;
ASSERT (y=template)
REPORT "Output differs from template!"
SEVERITY FAILURE;
END PROCESS;
END test;
---------------------------------------------------
In our design, the files above were saved with the names parity_detector.vhd (design file) and
test_parity_detector.vhd (test file). Therefore, the files created by the compiler (Quartus II in this example),
following the procedure of Appendix C, were parity_detector.vho (new design file) and
parity_detector_vhd.sdo (SDF file). These two files, along with test_parity_detector.vhd, are needed by
ModelSim to perform the type IV simulation.
Continuing with the procedure of Appendix C, combined with Appendix A, and using the EP2C35F672C6
Cyclone II device (DE2 board), an actual propagation delay around 8ns results. The resulting ModelSim plots
for this type IV simulation are shown below.
Digital Electronics and Design with VHDL, Volnei A. Pedroni, ElsevierMorgan Kaufmann, 2008
(3)
M4
(2)
M3
M6
(4) sel
M2
(10)
(4)
(5)
X2
M1
(a)
0
(9)
y (5)
(6)
M5
(3)
X1
(b)
X3
y (8)
sel 1
(7)
sel 0
(6)
a. Write a SPICE code to simulate the TG-based 2x1 multiplexer of figure (a). Assume that =0.2um and
adopt minimum size for the nMOS transistors and twice that for the pMOS ones (that is, (W/L)n=3/2 and
(W/L)p=6/2). Use either Level3 or BSIM3v3 models for the transistors (consult the MOSIS site at
www.mosis.org). Generate suitable signals to verify the multiplexers functionality.
b. Using .SUBCKT to instantiate the multiplexer of part (a) above, construct a SPICE code and test the
transient response and operation of the 4x1 multiplexer of figure (b).
Solution
A SPICE code for part (b) of this exercise is presented below (which indeed includes part (a)). The circuit
node numbers and transistor names are included in figure (a) and (b) above. As requested, the nMOS and
pMOS transistors sizes are (W/L)n=3/2 and (W/L)p=6/2), respectively, with =0.2um. The BSIM3v3
model (see pages 628-630) was employed to represent the MOSFET characteristics.
*----File containing the BSIM3v3 MOSFET model:-----.INC C:\ORCAD\MY_CIRCUITS\MOSFET_MODELS.CIR
*-----Defaults:------------------------------------.OPTIONS DEFL=0.4U DEFW=0.6U DEFAD=2P DEFAS=2P
*-----Subcircuit (2x1_multiplexer):----------------.SUBCKT multiplexer_2x1 1 2 3 4 5
M1 5 4 3 0 N
M2 5 6 3 1 P W=1.2U
M3 5 6 2 0 N
M4 5 4 2 1 P W=1.2U
M5 6 4 0 0 N
M6 6 4 1 1 P W=1.2U
.ENDS
*-----Final circuit (4x1_multiplexer):-------------X1 1 2 3 6 9 multiplexer_2x1
X2 1 4 5 6 10 multiplexer_2x1
X3 1 9 10 7 8 multiplexer_2x1
VDD 1 0 DC 3.3V
*-----Transient response:--------------------------Va 2 0 PULSE (0V 3.3V 25NS 0NS 0NS 25NS 275NS)
Vb 3 0 PULSE (0V 3.3V 100NS 0NS 0NS 25NS 200NS)
Vc 4 0 PULSE (0V 3.3V 175NS 0NS 0NS 25NS 125NS)
Vd 5 0 PULSE (0V 3.3V 250NS 0NS 0NS 25NS 50NS)
Vsel0 6 0 PULSE (0V 3.3V 75NS 0NS 0NS 75NS 150NS)
Vsel1 7 0 PULSE (0V 3.3V 150NS 0NS 0NS 150NS 300NS)
.TRAN 0.22NS 300NS
.PROBE V(2) V(3) V(4) V(5) V(6) V(7) V(8) V(9) V(10)
.END
*-------------------------------------------------
Digital Electronics and Design with VHDL, Volnei A. Pedroni, ElsevierMorgan Kaufmann, 2008
Simulation results obtained with PSpice 15.7 (see Appendix B) are presented below, where the following
signals are depicted:
V(2): Input voltage at node 2 (input a of the multiplexer);
V(3): Input voltage at node 3 (input b of the multiplexer);
V(4): Input voltage at node 4 (input c of the multiplexer);
V(5): Input voltage at node 5 (input d of the multiplexer);
V(6): Input voltage at node 6 (selection port sel0 of the multiplexer);
V(7): Input voltage at node 7 (selection port sel1 of the multiplexer);
V(9): Output voltage at node 9 (output of the first multiplexer);
V(10): Output voltage at node 10 (output of the second multiplexer);
V(8): Output voltage at node 8 (output of the last multiplexer).
Observe in the last plot, V(8), that the values obtained for the low-to-high (LH) and high-to-low (HL) inputto-output (IO) propagation delays are tpIO_LH 5ns and tpIO_HL 3ns.
Digital Electronics and Design with VHDL, Volnei A. Pedroni, ElsevierMorgan Kaufmann, 2008
5ns
3ns
Propagation delays from select-to-output (SO) are illustrated in the next set of plots. In this case, the final part
of the code was replaced with the following:
*-----Transient response:--------------------------Va 2 0 DC 0V
Vb 3 0 DC 3.3V
Vc 4 0 DC 0V
Vd 5 0 DC 0V
Vsel0 6 0 PULSE (0V 3.3V 50NS 0NS 0NS 50NS 100NS)
Vsel1 7 0 DC 0V
.TRAN 0.1NS 150NS
.PROBE V(2) V(3) V(4) V(5) V(6) V(7) V(8) V(9) V(10)
Consequently, all input signals are kept at fixed values, with a=c=d=0 and b=1. By switching sel0
between 0 and 1, the following propagation delays are observed: tpSO_LH 13ns and tpSO_HL 13ns.
13ns
13ns