Digital Systems Design Using VHDL 3rd Edition Roth Solutions Manual Download
Digital Systems Design Using VHDL 3rd Edition Roth Solutions Manual Download
Chapter 8 Solutions
8.1 function vec2int (vec5: bit_vector(4 downto 0))
return integer is
variable temp: integer := 0;
begin
for i in 4 downto 0 loop
if vec5(i) = '1' then temp := temp + 1; end if;
if i /= 0 then temp := temp*2; end if;
end loop;
return temp;
end vec2int;
8.3 Assuming the array has a range 0 to N – 1, and integer arrays are defined with:
type IArr is array (natural range <>) of integer;
8.5 • VHDL functions can only return one value; VHDL procedures can return multiple values
• VHDL function calls replace an expression; VHDL procedure calls are statements
• VHDL procedures can modify the values of their parameters; VHDL functions cannot
• VHDL procedures can have parameters of class variable; VHDL functions cannot
188
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
8.6 function BSHIFT(DATA: bit_vector(7 downto 0); N: integer) return bit_vector
is
variable DATAOUT: bit_vector(7 downto 0);
begin
DATAOUT(7 downto N) := DATA(7-N downto 0);
for i in N-1 downto 0 loop
DATAOUT(i) := '0';
end loop;
return DATAOUT;
end BSHIFT;
189
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
8.11 library IEEE;
use IEEE.numeric_bit.all;
entity addBCD4test is
port(CLK: in bit);
end addBCD4test;
190
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
8.12
Time B C
0 0000 0000
5 0110 0000 call P1
5+∆ 1100 0110
5 + 2∆ 1100 1100
6 1100 1100 call P1
6+∆ 1001 1100 call P1 (process restarted)
6 + 2∆ 0011 1001
6 + 3∆ 0011 0011
7 0011 0011 call P1
7+∆ 0110 0011 call P1 (process restarted)
7 + 2∆ 1100 0110
7 + 3∆ 1100 1100
191
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
8.16 type matrix is array (natural range <>, natural range <>) of integer;
procedure addM (signal A, B: in matrix; signal C: inout matrix) is
variable A1: matrix(A'length(1)-1 downto 0, A'length(2)-1 downto 0) :=
A;
variable B1: matrix(B'length(1)-1 downto 0, B'length(2)-1 downto 0) :=
B;
variable C1: matrix(C'length(1)-1 downto 0, C'length(2)-1 downto 0) :=
C;
begin
assert A1'length(1) = B1'length(1) and A1'length(2) = B1'length(2)
report "Matrices are not the same size"
severity error;
for i in A1'range(1) loop
for j in A1'range(2) loop
C1(i,j) := A1(i,j) + B1(i,j);
end loop;
end loop;
C <= C1;
end addM;
entity P8_14 is
port(A, B: in bit := '1';
C, D: inout bit);
end P8_14;
192
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
begin
-- 8-14a Solution
process
begin
wait until A'event;
C <= transport not A after 5 ns;
wait for 3 ns;
if B'stable(3 ns) then D <= not D; end if;
end process;
-- 8-14b Solution
process(A, B)
begin
if A'event and A = '1' then -- A has changed
assert B'stable(2 ns) -- False if B has changed w/in past 2ns
report "B not stable for the 2ns before A'event."
severity error;
end if;
if B'event then -- B has changed
assert A'stable(1 ns) -- False if A has changed w/in past 1ns
report "B not stable for the 1ns following A'event."
severity error;
if B = '1' then -- B changed to high
assert (B'delayed'last_event >= 10 ns)
report "B was low for less than 10ns."
severity error;
end if;
end if;
end process;
end P8_14;
193
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
8.21
Time 0 2 4 6 8 10
S(0) Z 0 0 0 Z Z
S(1) Z Z Z Z Z 1
S(2) Z Z 1 0 0 0
R Z 0 X 0 0 X
8.22
entity decoder8 is
port(Addr: in bit_vector;
Check: in X01Z_vector(5 downto 0);
Sel: out bit);
end decoder8;
194
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
8.24 library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
entity OctDFF is
port(D, OE_b, CLK: in std_logic;
Q: out std_logic);
end OctDFF;
Check1: process
begin
if falling_edge(CLK) then
assert (CLK'delayed'last_event >= tw)
report "Clock pulse width violation."
severity error;
end if;
wait until rising_edge(CLK);
assert (D'stable(tsu))
report "Setup time violation."
severity error;
wait for th;
assert (D'stable(th))
report "Hold time violation."
severity error;
end process;
Check2: process
begin
wait until falling_edge(CLK);
assert (CLK'delayed'last_event >= tw)
report "Clock pulse width violation."
severity error;
end process;
end OctDFF;
begin
if (A'length /= B'length) then
report "Vector lengths are different!"
severity ERROR;
return FALSE;
else
195
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
for i in A'range loop
if ((A(i) = '0' or A(i) = '1' or A(i) = '-') nand
(B(i) = '0' or B(i) = '1' or B(i) = '-')) then
report "Illegal bit value"
severity ERROR;
return FALSE;
else
if not (A(i) = B(i) or A(i) = '-' or B(i) = '-') then
return FALSE; end if;
end if;
end loop;
end if;
return TRUE;
end MatchVec;
8.26 (a)
(c)
Time S(0) S(1) S(2) C
0 H H H H
4 H H U U
5 H U U U
8 H U 0 U
10 H 1 0 X
14 H 1 Z 1
15 H 0 Z 0
20 H Z Z H
21 Z Z Z Z
8.27
'X' '0' '1' 'L' 'H'
'X' 'X' 'X' 'X' 'X' 'X'
'0' 'X' '0' 'X' '0' '0'
'1' 'X' 'X' '1' '1' '1'
'L' 'X' '0' '1' 'L' 'X'
'H' 'X' '0' '1' 'X' 'H'
196
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
for i in 0 to vec'length-1 loop
inv_vec(i) := not vector(i);
end loop;
return inv_vec;
end function;
8.29
8.30 (a)
entity RAM_test is
port(CLK: in bit;
Pass, Fail: out bit);
end RAM_test;
197
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
IO: inout unsigned(7 downto 0));
end component;
198
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
8.32 (a)
(b)
entity Chex_test is
port(CLK: in bit;
Pass, Fail: out bit);
end Chex_test;
199
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
signal state, nextstate: integer range 0 to 4 := 0;
signal K, inc, RP, Eq, setRP: std_logic := '0';
signal WEb: std_logic := '1';
signal CNTR: unsigned(7 downto 0) := "00000000";
signal RAMbus, DATA: unsigned(7 downto 0);
signal evenbit, oddbit: std_logic;
begin
K <= '1' when CNTR = "11111111" else '0';
oddbit <= CNTR(0) xor RP; -- odd bit generator
evenbit <= not oddbit; -- even bit generator
DATA <= oddbit & evenbit & oddbit & evenbit &
oddbit & evenbit & oddbit & evenbit;
Eq <= '1' when Data = RAMbus else '0'; -- comparator
RAMbus <= DATA when WEb = '0' else "ZZZZZZZZ"; -- buffer to RAM IO
RAM: RAM6116 port map ('0', WEb, '0', CNTR, RAMbus);
process(state, K, Eq, RP)
begin
WEb <= '1'; inc <= '0'; Fail <= '0'; Pass <= '0'; setRP <= '0';
case state is
when 0 => WEb <= '0'; nextstate <= 1;
when 1 => inc <= '1';
if K = '0' then nextstate <= 0;
else nextstate <= 2; end if;
when 2 => inc <= '1';
if Eq = '0' then nextstate <= 3;
elsif K = '0' then nextstate <= 2;
elsif RP = '0' then nextstate <= 0; setRP <= '1';
else nextstate <= 4; end if;
when 3 => Fail <= '1';
when 4 => Pass <= '1';
end case;
end process;
process(CLK)
begin
if CLK = '1' and CLK'event then
state <= nextstate;
if setRP = '1' then RP <= '1'; end if;
if inc = '1' then
if CNTR = "11111111" then CNTR <= "00000000";
else CNTR <= CNTR + 1; end if;
end if;
end if;
end process;
end Chex_test;
200
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
if Q = '0' then Q <= '1' after Tplh; Qp <= '0' after Tphl;
else Q <= '0' after Tphl; Qp <= '1' after Tplh; end if;
else change <= '0'; end if;
end if;
if CLK'event then
assert CLK'delayed'last_event >= Tck
report "Minimum clock pulse width violation. "
severity warning;
end if;
if change = '1' and T'event then
assert CLK'last_event >= Th
report "Hold time violation."
severity warning;
end if;
end process;
end behavior;
entity DFF74LS175 is
generic(constant tplh: time := 30 ns; -- max values by TTL book
constant tphl: time := 30 ns;
constant tsu: time := 20 ns;
constant th: time := 5 ns;
constant tcmin: time := 33 ns); -- 30 MHz clock
port(D, CLR, CLK: in std_logic;
Q, QN: out std_logic);
end DFF74LS175;
Check: process
variable LastRiseTime: time := 0 ns;
begin
wait until rising_edge(CLK);
assert(NOW - LastRiseTime >= tcmin)
report "Clock period violation."
severity error;
LastRiseTime := NOW;
assert(D'stable(tsu))
report "Setup time violation."
severity error;
201
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
wait for th;
assert(D'stable(th))
report "Hold time violation."
severity error;
end process;
end DFF74LS175;
entity testDFF is
end testDFF;
begin
CLK <= not CLK after halfperiod;
DFF1: DFF74LS175 port map (D, CLR, CLK, Q, QN);
DFF2: DFF74LS175 port map (D, CLR, CLK2, Q2, QN2);
process
begin
CLR <= '1';
-- shift 1,0,1 into the flip-flip (test normal shifting)
wait until rising_edge(CLK);
wait for th + 1 ns;
D <= '1'; -- shift 1 in
wait until rising_edge(CLK);
wait for tplh;
assert (Q='1' and QN='0')
report "Wrong value latched."
severity error;
wait until rising_edge(CLK);
wait for th + 1 ns;
D <= '0'; -- shift 0 in
wait until rising_edge(CLK);
wait for tphl;
assert (Q='0' and QN='1')
report "Wrong value latched."
severity error;
wait until rising_edge(CLK);
wait for th + 1 ns;
D <= '1'; -- shift 1 in
wait until rising_edge(CLK);
wait for tplh;
assert (Q='1' and QN='0')
report "Wrong value latched."
severity error;
202
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
-- Test CLR
CLR <= '0';
wait for 5 ns;
CLR <= '1';
if tphl > tplh then
wait for tphl; else wait for tplh; end if;
assert (Q='0' and QN='1')
report "Clear error."
severity error;
entity P8_31 is
port(abus: in bit_vector(14 downto 0);
203
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
dbus: inout std_logic_vector(31 downto 0);
sel, wr: in bit);
end P8_31;
entity P8_32 is
generic(N: integer := 4);
port(SI, Sh, CLK: in bit;
Q: inout bit_vector(N downto 1);
SO: out bit);
end P8_32;
8.38
(a) entity shift_reg is
generic(N: positive := 4; Lshift: Boolean := true);-- generic parameters
used
port(D: in bit_vector(N downto 1);
Qout: out bit_vector(N downto 1);
CLK, Ld, Sh : in bit);
end shift_reg;
204
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
architecture SRN of shift_reg is
signal Q, shifter: bit_vector(N downto 1);
begin
Qout <= Q;
genLS: if Lshift generate -- conditional generate of left shift register
shifter <= Q(N-1 downto 1) & '0';
end generate;
genRS: if not Lshift generate -- conditional generate of right shift
register
shifter <= '0' & Q(N downto 2);
end generate;
process(CLK)
begin
if CLK'event and CLK = '1' then
if LD = '1' then Q <= D;
elsif Sh = '1' then Q <= shifter;
end if;
end if;
end process;
end SRN;
entity and2 is
port(A1, A2: in bit; Z: out bit);
end and2;
205
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
port(A1, A2: in bit; Z: out bit);
end component;
begin
N_Ands: for i in N downto 1 generate
begin
And2x: and2 port map(A(i), B, C(i));
end generate;
end internal;
entity BLACKCIRCLE is
port(Gxy, Pxy: out bit;
Gx, Gy, Px, Py: in bit);
end BLACKCIRCLE;
entity SEMICIRCLE is
port(Gimin1, Pimin1, C0: in bit;
Ci: out bit);
end SEMICIRCLE;
entity SQUARE is
port(Pi, Ci: in bit;
Si: out bit);
end SQUARE;
entity KOGGESTONE is
port(A, B: IN bit_vector(7 downto 0);
Cin: IN bit;
SUM: OUT bit_vector(7 downto 0);
Cout: OUT bit);
end KOGGESTONE;
206
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
architecture koggestone of KOGGESTONE is
component WHITECIRCLE is
port(Ai, Bi: in bit; Gi, Pi: out bit);
end component;
component BLACKCIRCLE is
port(Gxy, Pxy: out bit; Gx, Gy, Px, Py: in bit);
end component;
component SEMICIRCLE is
port(Gimin1, Pimin1, C0: in bit; Ci: out bit);
end component;
component SQUARE is
port(Pi, Ci: in bit; Si: out bit);
end component;
signal g0, g1, g2, g3, p0, p1, p2, p3, c: bit_vector(7 downto 0);
signal c_intermediate: bit_vector(7 downto 0);
begin
207
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Gen5: for i in 0 to 7 generate
begin
Stage4: SEMICIRCLE port map(Gimin1=>g3(i), Pimin1=>p3(i), C0=>Cin,
Ci=>c_intermediate(i));
end generate;
end generate;
end koggestone;
entity BLACKCIRCLE is
port(Gxy, Pxy: out bit;
Gx, Gy, Px, Py: in bit);
end BLACKCIRCLE;
entity SEMICIRCLE is
port(Gimin1, Pimin1, C0: in bit;
Ci: out bit);
end SEMICIRCLE;
entity SQUARE is
port(Pi, Ci: in bit;
Si: out bit);
end SQUARE;
entity BRENTKUNG is
port(A, B: IN bit_vector(7 downto 0);
Cin: IN bit;
SUM: OUT bit_vector(7 downto 0);
208
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Cout: OUT bit);
end BRENTKUNG;
component WHITECIRCLE is
port(Ai, Bi: in bit; Gi, Pi: out bit);
end component;
component BLACKCIRCLE is
port(Gxy, Pxy: out bit; Gx, Gy, Px, Py: in bit);
end component;
component SEMICIRCLE is
port(Gimin1, Pimin1, C0: in bit; Ci: out bit);
end component;
component SQUARE is
port(Pi, Ci: in bit; Si: out bit);
end component;
signal g0, g1, g2, g3, p0, p1, p2, p3, c: bit_vector(7 downto 0);
signal c_intermediate: bit_vector(7 downto 0);
begin
209
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
end generate;
end brent_kung;
entity BLACKCIRCLE is
port(Gxy, Pxy: out bit;
Gx, Gy, Px, Py: in bit);
end BLACKCIRCLE;
210
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
entity SEMICIRCLE is
port(Gimin1, Pimin1, C0: in bit;
Ci: out bit);
end SEMICIRCLE;
entity SQUARE is
port(Pi, Ci: in bit;
Si: out bit);
end SQUARE;
entity KOGGESTONE is
port(A, B: IN bit_vector(7 downto 0);
Cin: IN bit;
SUM: OUT bit_vector(7 downto 0);
Cout: OUT bit);
end KOGGESTONE;
component WHITECIRCLE is
port(Ai, Bi: in bit; Gi, Pi: out bit);
end component;
component BLACKCIRCLE is
port(Gxy, Pxy: out bit; Gx, Gy, Px, Py: in bit);
end component;
component SEMICIRCLE is
port(Gimin1, Pimin1, C0: in bit; Ci: out bit);
end component;
component SQUARE is
port(Pi, Ci: in bit; Si: out bit);
end component;
signal g0, g1, g2, g3, p0, p1, p2, p3, c: bit_vector(7 downto 0);
signal c_intermediate: bit_vector(7 downto 0);
begin
211
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
g2(1) <= g1(1);
p2(0) <= p1(0);
p2(1) <= p1(1);
end generate;
end koggestone;
entity KOGGESTONE64 is
generic(k: integer := 2);
port(Ak, Bk: IN bit_vector(8*k-1 downto 0);
Cin: IN bit;
SUMk: OUT bit_vector(8*k-1 downto 0);
Cout: OUT bit);
end KOGGESTONE64;
212
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Gen: for i in 0 to k-1 generate
begin
kStage: KOGGESTONE port map(A=>Ak(8*i+7 downto 8*i),B=>Bk(8*i+7
downto 8*i),Cin=>cin_intermediate(i),SUM=>SUMk(8*i+7 downto
8*i),Cout=>cout_intermediate(i));
cin_intermediate(i+1) <= cout_intermediate(i);
end generate;
Cout <= cout_intermediate(k-1);
end koggestone64;
entity BLACKCIRCLE is
port(Gxy, Pxy: out bit;
Gx, Gy, Px, Py: in bit);
end BLACKCIRCLE;
entity SEMICIRCLE is
port(Gimin1, Pimin1, C0: in bit;
Ci: out bit);
end SEMICIRCLE;
entity SQUARE is
port(Pi, Ci: in bit;
Si: out bit);
end SQUARE;
entity BRENTKUNG is
port(A, B: IN bit_vector(7 downto 0);
Cin: IN bit;
SUM: OUT bit_vector(7 downto 0);
Cout: OUT bit);
end BRENTKUNG;
213
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
component WHITECIRCLE is
port(Ai, Bi: in bit; Gi, Pi: out bit);
end component;
component BLACKCIRCLE is
port(Gxy, Pxy: out bit; Gx, Gy, Px, Py: in bit);
end component;
component SEMICIRCLE is
port(Gimin1, Pimin1, C0: in bit; Ci: out bit);
end component;
component SQUARE is
port(Pi, Ci: in bit; Si: out bit);
end component;
signal g0, g1, g2, g3, p0, p1, p2, p3, c: bit_vector(7 downto 0);
signal c_intermediate: bit_vector(7 downto 0);
begin
214
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
p3(2) <= p2(2);
p3(3) <= p2(3);
p3(4) <= p2(4);
p3(5) <= p2(5);
p3(6) <= p2(6);
g3(0) <= g2(0);
g3(1) <= g2(1);
g3(2) <= g2(2);
g3(3) <= g2(3);
g3(4) <= g2(4);
g3(5) <= g2(5);
g3(6) <= g2(6);
end brent_kung;
entity BRENTKUNG64 is
generic(k: integer := 2);
port(Ak, Bk: IN bit_vector(8*k-1 downto 0);
Cin: IN bit;
SUMk: OUT bit_vector(8*k-1 downto 0);
Cout: OUT bit);
end BRENTKUNG64;
begin
cin_intermediate(0) <= Cin;
Gen: for i in 0 to k-1 generate
begin
kStage: BRENTKUNG port map(A=>Ak(8*i+7 downto 8*i),B=>Bk(8*i+7 downto
8*i),Cin=>cin_intermediate(i),SUM=>SUMk(8*i+7
downto 8*i),Cout=>cout_intermediate(i));
cin_intermediate(i+1) <= cout_intermediate(i);
end generate;
215
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Cout <= cout_intermediate(k-1);
end brentkung64;
component BRENTKUNG is
port(A, B: IN bit_vector(7 downto 0);
Cin: IN bit;
SUM: OUT bit_vector(7 downto 0);
Cout: OUT bit);
end component;
begin
if k = 1 generate
GenKoggeStone: KOGGESTONE(A=>A,B=>B,Cin=>Cin,SUM=>SUM,Cout=>Cout);
if k = 0 generate
GenBrentKung: BRENTKUNG(A=>A,B=>B,Cin=>Cin,SUM=>SUM,Cout=>Cout);
end architecture;
entity HA is
port(A, B: in bit; S, Co: out bit);
end HA;
architecture internal of HA is
begin
Co <= A and B;
S <= A xor B;
end internal;
entity FA is
port(A, B, Ci: in bit; S, Co: out bit);
end FA;
architecture internal of FA is
begin
S <= A xor B xor Ci;
Co <= (A and B) or (A and Ci) or (B and Ci);
end internal;
216
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
entity P8_34 is
port(X, Y: in bit_vector(3 downto 0);
P: out bit_vector(7 downto 0));
end P8_34;
217
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
8.46 use std.textio.all;
entity P8_35 is
end P8_35;
218
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
219
© 2018 Cengage Learning®. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.