- 2071 Magh · 8 marks
Dual-purpose processor: factorial and prime
Short answer
The processor computes factorial by iterative multiplication (F=1, I=2..n) and checks primality by trial division with repeated subtraction. Datapath shares registers N, I, R, F, multiplier, subtractor; controller is a 10-state FSM with status flags le, lt2, ige, rge, rz.
Show the algorithm, FSMD, FSM, data-path, and controller design for a dual-purpose processor that can calculate the factorial of a number and also check if that number is a prime number.
Assumption: one 8-bit input n is read; the processor produces both outputs, FACT = n! and PRIME (1 if n is prime). Registers I and R are shared by both functions.
Algorithm
read n
F = 1
for I = 2 to n: F = F * I // factorial
FACT = F
if (n < 2) PRIME = 0
else:
I = 2
while (I < n):
R = n; while (R >= I) R = R - I
if (R == 0) { PRIME = 0; stop }
I = I + 1
PRIME = 1
output FACT, PRIME
FSMD
| State | Condition | Actions | Next |
|---|---|---|---|
| S0 | start = 0 | wait | S0 |
| S0 | start = 1 | N = n; F = 1; I = 2 | S1 |
| S1 | I <= N | none | S2 |
| S1 | I > N | FACT = F | S3 |
| S2 | always | F = F * I; I = I + 1 | S1 |
| S3 | N < 2 | none | S9 |
| S3 | N >= 2 | I = 2 | S4 |
| S4 | I >= N | none | S8 |
| S4 | I < N | R = N | S5 |
| S5 | R >= I | none | S6 |
| S5 | R < I | none | S7 |
| S6 | always | R = R - I | S5 |
| S7 | R = 0 | none | S9 |
| S7 | R != 0 | I = I + 1 | S4 |
| S8 prime | always | PRIME = 1, done = 1 | S0 |
| S9 not prime | always | PRIME = 0, done = 1 | S0 |
Note: n = 0 gives 0! = 1 and n = 1 gives 1.
FSM (control signals per state)
| State | Signals |
|---|---|
| S0 | N_ld, F_set1, I_set2 (on start) |
| S1 | none (compare I <= N) |
| S2 | F_ld (multiplier), I_inc |
| S3 | FACT_ld (compare N < 2) |
| S4 | R_ld (R = N) when I < N |
| S5 | none |
| S6 | R_sub |
| S7 | I_inc when R != 0 |
| S8 | PRIME = 1, done |
| S9 | PRIME = 0, done |
Datapath
n -> [N] --> comparators: I<=N, N<2, I>=N
[F] <- mux(1, F*I) <- [ * multiplier ] <- F, I
[I] <- mux(2, I+1) <- incrementer
[R] <- mux(N, R-I) <- subtractor ; compare R>=I ; zero detect R==0
[FACT] <- F ; [PRIME] flag
Components: registers N, F (16 or 32 bits for the factorial), I, R, FACT, PRIME flag; one multiplier, one incrementer, one subtractor; comparators (I <= N, N < 2, I >= N, R >= I) and a zero detector; four multiplexers.
Controller
Block diagram:
start, status flags --> +-------------+ +-----------+
(from datapath) | Next-state |-->| State |
| logic | | register |
+------^------+ +-----+-----+
| current state |
+-----------------+
v
+-------------+
| Output logic|--> control
+-------------+ signals
Inputs: start, status flags (le, lt2, ige, rge, rz). Outputs: loads, mux selects, done. 10 states (S0-S9): 4-bit binary state register.
Related questions
- Dual-purpose processor: median and variance · Embedded System, chapter 2
- Prime number checking processor · Embedded System, chapter 2
- Factorial single-purpose processor · Embedded System, chapter 2
- Design LCM single-purpose processor · Embedded System, chapter 2
- Single-purpose processor for power · Embedded System, chapter 2 · asked 2×
- Fibonacci single-purpose processor · Embedded System, chapter 2 · asked 2×
- Dual processor modes in centralized SPC · Telecommunications, chapter 4 · asked 4×
- nth Fibonacci term prime check · Computer Programming, chapter 6
- SPC and dual processor architecture · Telecommunications, chapter 4
- Prime number program: procedural and OOP · Object Oriented Programming, chapter 1