Skip to main content

Factorial single-purpose processor

Short answer

The algorithm initializes F=1, I=2, then while I<=n multiplies F=F*I and increments I. Datapath includes registers N, I, F, multiplier, incrementer, comparator; controller is a 4-state FSM with input start and status le.

Design a single-purpose processor that calculates Factorial of an integer number 'n'. Start with a function computing the desired result, translate it into a state diagram and sketch a probable datapath.

Assumption: input n is an 8-bit unsigned integer; with a 16-bit result register the output is valid for 0 <= n <= 8 (8! = 40320). Use a wider output register for larger n.

Step 1: Function

 Factorial(n):
   F = 1; I = 2
   while (I <= n):
       F = F * I
       I = I + 1
   output F

Step 2: State diagram (FSMD)

 S0 --start=1 (N=n, F=1, I=2)--> S1 (test)
 S0 --start=0--> S0
 S1 --I<=N--> S2 (F=F*I, I=I+1) --> S1
 S1 --I>N---> S3 (out=F, done=1) --> S0
StateConditionActionsNext
S0 waitstart = 0done = 0S0
S0start = 1N = n, F = 1, I = 2S1
S1 testI <= NnoneS2
S1I > NnoneS3
S2 multiplyalwaysF = F * I, I = I + 1S1
S3 outputalwaysout = F, done = 1S0

Step 3: Datapath

  n -> [N] ------------> [ I <= N ] --> le (to controller)
                              ^
  [I] <- mux(2, I+1) ---------+
  [F] <- mux(1, F*I) <- [ x ] <- F, I
  F --> out

Components: registers N, I, F; a multiplier; an incrementer; a comparator (I <= N); two 2:1 muxes. Control signals: N_ld, I_sel, I_ld, F_sel, F_ld, out_en. Status: le.

Controller

4-state FSM (2-bit state register). Block diagram:

 start, status flags --> +-------------+   +-----------+
 (from datapath)         | Next-state  |-->| State     |
                         | logic       |   | register  |
                         +------^------+   +-----+-----+
                                |   current state |
                                +-----------------+
                                                  v
                                           +-------------+
                                           | Output logic|--> control
                                           +-------------+    signals

Trace n = 4: F = 1; I = 2 -> F = 2; I = 3 -> F = 6; I = 4 -> F = 24; I = 5 > 4 -> output 24. (n = 0 or 1 outputs 1.)

✓ Answer checked by Claude Opus 5.5 (high effort)

All Embedded System chapter 2 questions →