2008FallMT2 SOLUTIONS
2008FallMT2 SOLUTIONS
BILKENT UNIVERSITY
Department of Electrical and Electronics Engineering
EEE102 Introduction to Digital Circuit Design
Midterm Exam II - SOLUTIONS
Surname: ________________________________
Name: ________________________________
ID-Number: ________________________________
Signature: ________________________________
Duration is 120 minutes. Solve all 5 questions. Show all your work.
Q1 (12 points)
Q2 (22 points)
Q3 (22 points)
Q4 (22 points)
Q5 (22 points)
Total
Question 1-(12 pts):
(a) (4 pts) Draw the block diagram of a synchronous finite-state machine, and explain
the difference between Mealy and Moore machines. Make sure to label all of the blocks
properly.
The outputs for Mealy machines depend on the current state as well as on the input
directly. For Moore mchines, the outputs depend on the current state only.
(b) (4 pts) What is the fundamental difference between a flipflop and a latch?
(c) (4 pts) Draw the gate-level circuit diagram of an S-R latch and explain how it works.
Solution:
States: Power ON 0,0
S0: Power ON state (Also not enough
S0
information, or 0 after 0)
S1: Only one 1, or 1 after 0.
S2: Only two 1s, or 1 after 1. 1,0 0,0
0,0
S3: 110 received.
S2 1 0 S2
S3 1 1
Q1 Q0 X Q1* Q0*
0 0 0 0 0
0 0 1 0 1
0 1 0 0 0
0 1 1 1 0
1 0 0 1 1
1 0 1 1 0
1 1 0 0 0
1 1 1 0 1
D1: D0:
Q1Q0 Q1 Q1Q0 Q1
X X
0 0 0 1 0 0 0 1
0 1 0 1 1 0 1 0
Q0 Q0
Output table:
Q1 Q0 X Z
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 1
Z=Q1Q0X
X
+5v
D1 PR Q1
Q1Q0’+ Q1’Q0X D Q
CLK Q Z
CLR
P
+5v
+5v
Q1Q0’X’+ RESET
D0 PR Q0 IC
Q1’Q0’ D Q P
X+Q1Q0X’
clock CLK
CLR
Another solution:
State encoding:
Q1 Q0
S0 0 0
S1 0 1
S2 1 1
S3 1 0
D1: D0:
Q1Q0 Q1 Q1Q0 Q1
X X
0 0 1 0 0 0 0 0
0 1 1 0 1 1 1 1
Q0 Q0
D1 = Q1Q0+ Q0X D0 = X
Output table:
Q1 Q0 X Z
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 0
1 0 0 0
1 0 1 1
1 1 0 0
1 1 1 0
Z=Q1Q0’X
X Single- S
bit
Y Full
Adder
Cin Cout
Solution:
a) Full Adder:
Truth Table is
Cin X Y S Cout
0 0 0 0 0
0 0 1 1 0
0 1 0 1 0
0 1 1 0 1
1 0 0 1 0
1 0 1 0 1
1 1 0 0 1
1 1 1 1 1
Y\ CinX 00 01 11 10
0
0 0 1 0
1
0 1 1 1
Cout
VHDL code:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity fulladder is
Port (X, Y, Cin: in BIT;
Cout, S: out BIT);
end fulladder;
X X 4-bit S SumDiff
Y Y
Ripple
Adder/
Subtractor
CONTROL C O Overflow
Solution:
b) Ripple Adder/Subtractor:
To perform addition of 2 4-bit numbers, we use 4 full adders and set the carry-in of the
rightmost full adder to 0 (CONTROL = 0). There is overflow if c4 ≠ c3 . That is,
OVERFLOW = 1 if c4 ≠ c3 and OVERFLOW = 0 if c4 = c3 .
For subtraction, we know that X-Y = X + (-Y) = X+Y’+1. Thus we complement Y and add 1
through setting c0 to 1 (CONTROL = 1).
Y3 Y2 Y1 Y0
X3 X2 X1 X0
X Y X Y X Y X Y
S3 S2 S1 S0
OVERFLOW
VHDL code:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity RippleAdder_Subtractor is
Port(X: in BIT_VECTOR (3 downto 0);
Y: in BIT_VECTOR (3 downto 0);
CONTROL: in BIT;
SumDiff: out BIT_VECTOR (3 downto 0);
Overflow: out BIT);
end RippleAdder_Subtractor;
LABEL1: fulladder
port map(X(0), Y(0), C(0), C(1), SumDiff(0));
LABEL2: fulladder
port map(X(1), Y(1), C(1), C(2), SumDiff(1));
LABEL3: fulladder
port map(X(2), Y(2), C(2), C(3), SumDiff(2));;
LABEL4: fulladder
port map(X(3), Y(3), C(3), C(4), SumDiff(3));;
end Behavioral;
Question 4-(22 pts):
Implement F = (A+B)C’
a) (4 pts) using a generic 8-to-1 multiplexer and minimum number of additional simple
gates,
A B C (A+B)C’ 1
0 0 0 0
0 0 1 0 E
0 1 0 1 0 D0
0 1 1 0 0 D1
1 0 0 1
1 0 1 0 1 D2
1 1 0 1 0 D3
1 1 1 0 Y
1 D4
0 D5
1 D6
0 D7
S2 S1 S0
A B C
b) (4 pts) using a generic 4-to-1 multiplexer and minimum number of additional simple
gates,
1
E
B D0
0 D1
1 D2 Y
0 D3
S1 S0
A C
c) (4 pts) using a generic 2-to-1 multiplexer and minimum number of additional simple
gates,
1
A E
B D0
Y
0 D1
S0
C
Question 4-(22 pts) - Continued:
d) (4 pts) using one 74XX138 decoder and minimum number of additional simple gates.
(note that 74XX138 is a 3-to-8 binary decoder with active low outputs and with three
enables, one active high and two active low),
74X138
1 G1 Y0
0 G2A Y1
0 G2B Y2
Y3
A S2 Y4 F
B S1 Y5
C S0 Y6
Y7
e) (6 pts) using two 2-to-4 generic decoders and minimum number of additional simple
gates.
E Y0
Y1
B S1 Y2
C S0 Y3
F
A
E Y0
Y1
B S1 Y2
C S0 Y3
Question 5-(22 pts):
The following VHDL code is given:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity top is
Port ( b : in BIT;
a : in BIT_VECTOR (9 downto 0);
x : out STD_LOGIC_VECTOR (3 downto 0);
g : out BIT);
end top;
component devre
Port ( a : in BIT_VECTOR (9 downto 0);
d : out STD_LOGIC_VECTOR (3 downto 0);
g : out BIT);
end component;
signal y: STD_LOGIC_VECTOR (3 downto 0);
begin
devre1: devre port map (a,y,g);
x<="ZZZZ" when b='1' else y;
end Behavioral;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity devre is
Port ( a : in BIT_VECTOR (9 downto 0);
d : out STD_LOGIC_VECTOR (3 downto 0);
g : out BIT);
end devre;
begin
d<="0000" when (a="1111111111") else
"1001" when a(9)='0' else
"1000" when a(8)='0' else
"0111" when a(7)='0' else
"0110" when a(6)='0' else
"0101" when a(5)='0' else
"0100" when a(4)='0' else
"0011" when a(3)='0' else
"0010" when a(2)='0' else
"0001" when a(1)='0' else
"0000" when a(0)='0';
g<='0' when (a="1111111111") else '1';
end Behavioral;
a(8)
a(7)
x(3)
x(2)
x(1)
x(0)
c) What is the function of the entity “devre”? How can you name it?
What is the function of the entity “top”? How can you name it?
What is the function of the signal “b”? How can you name it?
What is the function of the signal “g”? How can you name it?
Solution:
“devre” is a priority encoder with active low inputs giving highest priority to
a(9).
“top” is the same thing but with tri-state encoding outputs and active low enable.
“b” is the is active low enable of “top” . If b=1 then the output of “top” becomes
high Z.
“g” is the active high got-something signal meaning that at least one of the inputs
are low.
Appendix: Some VHDL Templates 18-12-2008
ENTITY DECLARATION
entity entity_name is
generic ( constant_names : constant type;
constant_names : constant type;
…
constant_names : constant type);
port ( signal_names : mode signal_type;
signal_names : mode signal_type;
…
signal_names : mode signal_type);
end entity_name;
ARCHITECTURE DEFINITIONS
architecture architecture-name of entity-name is
type declarations
signal declarations
constant declarations
function definitions
procedure definitions
component declarations
begin
concurrent statement
...
concurrent statement
end architecture-name;
COMPONENT DECLARATION
component component_name
port ( signal_names : mode signal type;
signal_names : mode signal type;
…
signal_names : mode signal type);
end component;
COMPONENT INSTANTIATION
label: component_name port map (signal1, signal2, …,signaln);
or,
label: component_name port map (port1 =>signal1, port2 =>signal2, …, portn =>signaln);
Note that conditional concurrent assignment statement and with-select statement cannot be used in a process
statement. Instead, in a process, one can use the the sequential conditional assignment statements if and case.
BEVAVIORAL TYPE STATEMENTS:
process statement
process(signal_name, signal_name, …, signal_name)
type_declarations
variable declarations
constant declarations
begin
sequential-statement
…
sequential-statement
end process;
Simple sequential assignment statement
signal_name <= expression;
Simple variable assignment statement
variable_name := expression;
if statement in its general form
if boolean_ expression then sequential_statements
elsif boolean_ expression then sequential_statements
…
elsif boolean_ expression then sequential_statements
else sequential_statements
end if;
Note that you may not use the else and/or the elsif.
case-when statement
case expression is
when choices => sequential_statements
…
when choices => sequential_statements
end case;
loop statement
loop
sequential_statement
…
sequential_statement
end loop;
for-loop statement
for identifier in range loop
sequential_statement
…
sequential_statement
end loop;
while statement
while boolean_expression loop
sequential_statement
…
sequential_statement
end loop;
Note that the if, case, loop, for, and while statements are called sequential statements and they can only be used
in a process statement. Also note that each process is one concurrent statement.
If the “ieee.std_logic_arith.all” and “ieee.std_logic_unsigned.all” packages are included then + and – operators
for addition and subtraction can be used for UNSIGNED binary, SIGNED binary, and STD_LOGIC_VECTOR
types.
Concatenation operator & is used as follows: If A and B are 2 bit numbers then A&B is a four bit number with A
being more significant.