CSIE 1000 Introduction to Computers National Taiwan University
Fall 2014 Department of CSIE
Midterm Exam
November 11, 2014 (total: 105 points)
• Use * to represent the irrelevant values and the values not specified in the question.
• Feel free to use circuits introduced in the class. The following are some circuits that you may need. Feel free to change the orientations of the gates, the positions of inputs and outputs and the number of bits. On the other hand, for any circuit that was not introduced in the class, you have to implement it before using.
S 1-bit
HA C
X
Y
k k
k
+/-
add/sub
k k
k : :
k-wide n-to-1 MUX n
log2n
k-bit REG
k k
R data W
data W
D
7-segment display decoder
4 7
1-bit half adder k-bit add/sub k-wide n-to-1 k-bit register 7-segment display
1. (8%) What are the 8-bit 2’s complement representations of the following decimal numbers? Please give both their binary and hexadecimal representations.
a. 45 b. -28
2. (8%) The INC circuit adds one to the input. The 4-bit INC circuit accepts a 4-bit input X and outputs a 4-bit Y so that Y = X + 1 (please ignore the overflow issue). Please design a 4-bit INC circuit using only 1-bit half adders.
2- bit comparator
X Y
2 2
X>Y X=Y X<Y
G E L
4-bit comparator
X Y
4 4
X>Y X=Y X<Y
G E L
op
2DC
2 4
Din
8 4
D1
D0
(a) 2-bit comparator (b) 4-bit comparator (c) 2-digit decimal counter Figure 1: The comparators and the 2-digit decimal counter.
3. (18%) Figure 1(a) shows a 2-bit comparator which has two 2-bit unsigned-integer inputs, X = X1X0and Y = Y1Y0, and three output bits G, E, L for the conditions of X > Y , X = Y and X < Y , respectively.
(a) Write down the truth table for the three boolean functions G, E and L. (6%) (b) Write down their SOP forms. (6%) (c) Use K-map to simplify their expressions if possible. (6%) No need to draw their circuit diagrams for this question.
4. (8%) Design a 4-bit comparator as shown in Figure 1(b). Hint: Use the 2-bit comparator from the last question. Note that even if you do not answer the above question or your design is wrong, you can still use 2-bit comparators as black boxes in your design.
1
5. (12%) Design a circuit to implement the following pseudo code. Here, the variables A, B, X, Y, Z are registers containing 4-bit unsigned integers. Hint: in addition to registers, you will need a 4-bit comparator and some multiplexers. Similarly, you can just use 4-bit comparators as black boxes in this question no matter whether you answer the above question correctly.
if (A<B) then Z := X+A
else if (A==B) then Z := X+B
else
Y := A+B
6. (12%) In Hack ALU, the following configurations of inputs are used for x + 1, y − x and x|y. Explain why they work.
zx nx zy ny f no out
0 1 1 1 1 1 x + 1
0 0 0 1 1 1 y − x
0 1 0 1 0 1 x|y
7. (12%) Draw the required datapath on the datatpath sheet for (a) instruction fetch and the set of instructions {”jump register”, ”jump and link” }; and (b) the set of instructions {”sub”, ”load”, ”store indirect”, ”branch zero”} (without instruction fetch). Only draw the necessary part but not the whole TOY datapath. Add multiplexers only if necessary. Remember to sign your name and return the datatpath sheet with your answer sheet.
8. (12%) Refer to the TOY architecture (Figure 3; note that the numbering could be different from the lec- ture), please specify the operations of M U XP C, M U XM EM, M U XREGR, M U XALU, M U XREGW, W RIT EREG, W RIT EM EM and ALU OP during the execution stage for the instructions ”add”, ”load indirect” and ”jump register”. As an example, for ”jump and link”, they would be M U XP C = 0, M U XM EM =
∗, M U XREGR= ∗, M U XALU = 1, M U XREGW = 01, W RIT EREG= 1, W RIT EM EM = 0, ALU OP =
∗. (For ALUOP, you only need to specify 3-bit ALUcontrolas specified in Figure 3.)
9. (15%) Binary-coded decimal (BCD) is a binary encoding of decimal numbers where each decimal digit is represented by 4 bits. For example, the bit string 0100 1001 will be interpreted as 49 in decimal. In this question, you are asked to design a 2-digit decimal counter (2DC) which will count between 00 to 99 as shown in Figure 1(c). It has two inputs: a 2-bit op and an 8-bit Din(representing a 2-digit BCD). You can assume that the input Dinis always valid. 2DC has two 4-bit outputs, D1and D0, each for a decimal digit.
They can be connected to two 7-segment displays as the outputs. You can use the 7-segment display decoder in homework #1 to control the displays. The 2DC counter has the following operations. Hint: implement 2DC using two 4-bit registers, one for a decimal digit.
op[1] op[0] operation semantics
0 0 reset count = 0
0 1 set count= Din
1 0 inc INC(count);
1 1 dec DEC(count);
INC(count) ≡ if ( count < 99) count++;
DEC(count) ≡ if ( count > 0) count−−;
2
0: halt
#
1: add 2: subtract 3: and 4: xor 5: shift left 6: shift right 7: load addr
exit(0)
R[d] ← R[s] + R[t]
R[d] ← R[s] - R[t]
R[d] ← R[s] & R[t]
R[d] ← R[s] ^ R[t]
R[d] ← R[s] << R[t]
R[d] ← R[s] >> R[t]
R[d] ← addr 8: load
9: store A: load indirect B: store indirect C: branch zero D: branch positive E: jump register F: jump and link
R[d] ← mem[addr]
mem[addr] ← R[d]
R[d] ← mem[R[t]]
mem[R[t]] ← R[d]
if (R[d] == 0) pc ← addr if (R[d] > 0) pc ← addr pc ← R[t]
R[d] ← pc; pc ← addr
13 12 11 10
15 14 9 8 7 6 5 46 3 2 1 0
opcode dest d addr
opcode dest d source s source t
Format 2 Format 1
Operation Pseudocode
1 Fmt
1 1 1 1 1 1 2 2 2 1 1 2 2
2
Register 0 always 0.
Loads from mem[FF]
from stdin.
Stores to mem[FF] to stdout.
1
Figure 2: TOY reference card.
PC
Registers
W W Data
A Data B Data W Addr A Addr B Addr
Registers
W W Data
A Data B Data W Addr A Addr B Addr +
1
Memory
W W Data Addr
R Data Memory
W W Data Addr
R Data
IR op d s t IR op d s t
Cond Eval Cond Eval
A L U A L U 2
5 ALU_OP MUX MEM
MUX PC 1
0 1
1 0 10 01 00
1 0 0
MUX ALU
MUX REGR MUX REGW
CLOCK MEM CLOCK REG
WRITE REG WRITE MEM
WRITE IR
WRITE PC
ALU_OP
000 add/sub 001 and 010 xor 011 shift 100 copy input 2
Figure 3: TOY architecture.
3