Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CS120B Homework 2: Factorial Calculator FSM Design, Assignments of Computer Science

Instructions for designing a finite state machine (fsm) to calculate the factorial of a given number using an 8-bit datapath. The design process includes writing high-level pseudo-code, converting it to control words, drawing the state diagram, deriving the next-state table, and implementing the excitation and output equations. The fsm uses d flip-flops and has inputs for the number (n) and control signals.

Typology: Assignments

2009/2010

Uploaded on 03/28/2010

koofers-user-5rv
koofers-user-5rv ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS120B โ€“ Homework #2
Given August 12, 2002. Due August 19, 2002 at the beginning of class.
Use the datapath and functional operations shown in Figure 1 below to implement the
following algorithm:
Input a number n and output the factorial of n, i.e. n! Assume that n is small and no
overflow error results from the calculations.
Use D flip-flops for the FSM.
You need to do the following:
a) Write the high-level pseudo-code to implement the algorithm. (2)
b) Convert the pseudo-code to control words. (2)
c) Draw the state-diagram for the control words. (2)
d) Derive the next-state/implementation table. (2)
e) Derive the excitation equations. (2)
f) Derive the output equations. (2)
g) Draw the complete FSM circuit. (2)
h) Draw the circuit(s) that generates signals from the datapath for the FSM. (2)
15
Input
mux
01
WE
WA
4
x
8
RF
RAE
RAA RBE
RBA
14
13-12
11
10-9
8
7-6
Clk
5
4
3
0
IE
OE
ALU
ALU
2
ALU
1
ALU
0
AB
Shifter
SH
1
SH
0
2
1
(a)
Figure 1. 8-bit datapath with register file: (a) circuit; (b) ALU operations; (c) Shifter operations.
ALU2 ALU1 ALU0Operation
0 0 0 Pass through A
0 0 1 A AND B
0 1 0 A OR B
0 1 1 NOT A
1 0 0 A + B
1 0 1 A โ€“ B
1 1 0 A + 1
1 1 1 A โ€“ 1
(b)
SH1 SH0Operation
0 0 Pass through
0 1 Shift left
1 0 Shift right
1 1 Rotate right
(c)
pf3
pf4
pf5

Partial preview of the text

Download CS120B Homework 2: Factorial Calculator FSM Design and more Assignments Computer Science in PDF only on Docsity!

CS120B โ€“ Homework

Given August 12, 2002. Due August 19, 2002 at the beginning of class.

Use the datapath and functional operations shown in Figure 1 below to implement the following algorithm:

Input a number n and output the factorial of n , i.e. n! Assume that n is small and no overflow error results from the calculations.

Use D flip-flops for the FSM.

You need to do the following: a) Write the high-level pseudo-code to implement the algorithm. (2) b) Convert the pseudo-code to control words. (2) c) Draw the state-diagram for the control words. (2) d) Derive the next-state/implementation table. (2) e) Derive the excitation equations. (2) f) Derive the output equations. (2) g) Draw the complete FSM circuit. (2) h) Draw the circuit(s) that generates signals from the datapath for the FSM. (2)

15

Input

mux

1 0

WE WA^4 x^8 RAE RF RAA

RBE RBA

14 13- 11 10-

8 7-

Clk

5 4 3

0

IE

OE

ALU

ALU 2 ALU 1 ALU 0

A B

SH SH (^1) Shifter 0

2 1

(a)

Figure 1. 8-bit datapath with register file: (a) circuit; (b) ALU operations; (c) Shifter operations.

ALU 2 ALU 1 ALU 0 Operation 0 0 0 Pass through A 0 0 1 A AND B 0 1 0 A OR B 0 1 1 NOT A 1 0 0 A + B 1 0 1 A โ€“ B 1 1 0 A + 1 1 1 1 A โ€“ 1

(b)

SH 1 SH 0 Operation 0 0 Pass through 0 1 Shift left 1 0 Shift right 1 1 Rotate right

(c)

Answer

a)

  1. cin >> n;
  2. fac = 1; while (n > 1){ //fac = fac * n;
  3. count = n;
  4. add = fac; while (count > 1){
  5. fac = fac + add;
  6. count = count - 1; }
  7. n = n - 1; }
  8. cout << fac << endl;

b) 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 State IE WE WA RAE RAA RBE RBA ALU SH OE Comment

(^1 1 1 00 0) ร—ร— 0 ร—ร— ร—ร—ร— ร—ร— 0 input n 2 0 1 01 0 00 0 00 101 00 0 fac = 0 (^3 0 1 01 1 01 0) ร—ร— 110 00 0 fac ++ (^4 0 1 10 1 00 0) ร—ร— 000 00 0 count = n (^5 0 1 11 1 01 0) ร—ร— 000 00 0 add = fac 6 0 1 01 1 01 1 11 100 00 0 fac = fac + add (^7 0 1 10 1 10 0) ร—ร— 111 00 0 count = count โ€“ 1 (^8 0 1 00 1 00 0) ร—ร— 111 00 0 n = n โ€“ 1 (^9 0 0) ร—ร— 1 01 0 ร—ร— 000 00 1 output fac

c)

d) We can use one signal for both ( n > 1) and ( count > 1) since the comparator taps into the same point in the datapath and when that signal is needed, the value for the corresponding variable is available at that point.

Next State Current State Start , ( n_count >1) Q 3 Q 2 Q 1 Q 0 00 01 10 11 0000 0000 0000 0001 0001 0001 0010 0010 0010 0010 0010 0011 0011 0011 0011 0011 0100 0100 0100 0100 0100 0101 0101 0101 0101 0101 0110 0110 0110 0110 0110 1000 0111 1000 0111 0111 0110 0110 0110 0110 1000 1001 0100 1001 0100 1001 0000 0000 0000 0000

e) Note that the equations are not necessarily minimized. There can be other solutions.

D 3 = Q 3 'Q 2 Q 1 Q 0 ' Start' ( n_count >1) ' + Q 3 'Q 2 Q 1 Q 0 ' Start ( n_count >1) ' + Q 3 Q 2 'Q 1 'Q 0 ' Start' ( _n- count >1) ' + Q 3 Q 2 'Q 1 'Q 0 ' Start ( n_count >1) ' = Q 3 'Q 2 Q 1 Q 0 ' ( n_count >1) ' + Q 3 Q 2 'Q 1 'Q 0 ' ( n_count >1) '

D 2 = Q 3 'Q 2 Q 1 ' + Q 3 'Q 2 ( n_count >1) + Q 3 'Q 2 'Q 1 Q 0 + Q 3 'Q 2 Q 1 Q 0 = Q 3 'Q 2 Q 1 ' + Q 3 'Q 2 ( n_count >1) + Q 3 'Q 1 Q 0

D 1 = Q 3 'Q 2 'Q 1 'Q 0 + Q 3 'Q 2 'Q 1 Q 0 ' + Q 3 'Q 2 Q 1 'Q 0 + Q 3 'Q 2 Q 1 Q 0 + Q 3 'Q 2 Q 1 Q 0 ' ( n_count >1) = Q 3 'Q 2 'Q 1 'Q 0 + Q 3 'Q 2 'Q 1 Q 0 ' + Q 3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