Skip to main content

Dual-purpose processor: median and variance

Short answer

The processor inputs five numbers, sorts them with bubble sort, median is M[2]; mean = sum/5 by repeated subtraction; variance = Σ(xi-mean)²/5. Datapath includes register file, adder, subtractor, multiplier, counters; controller is a 17-state FSM.

Design a dual-purpose processor that calculates the median and variance of 5 numbers entered by the user, by showing the algorithm, FSMD, FSM, data-path and controller design.

Assumptions: five 8-bit unsigned integers are entered one at a time (each accompanied by an enter pulse). Median = middle value of the sorted list. Variance = Σ(xi - mean)² / 5 with integer arithmetic (mean and variance truncated). The processor sorts with bubble sort and divides by 5 using repeated subtraction.

Algorithm

 sum = 0
 for i = 0..4: wait(enter); M[i] = din; sum += din
 sort M[0..4] ascending (bubble sort)
 median = M[2]
 mean = sum / 5
 acc = 0
 for i = 0..4: d = M[i] - mean; acc += d*d
 variance = acc / 5
 output median, variance

FSMD

StateConditionActions (RTL)Next
S0start = 0waitS0
S0start = 1SUM = 0, I = 0S1
S1I < 5noneS2
S1I >= 5P = 0S4
S2enter = 0waitS2
S2enter = 1noneS3
S3alwaysM[I] = din, SUM = SUM + din, I = I + 1S1
S4alwaysP = 0 (pass count)S5
S5P < 4noneS6
S5P >= 4noneS10
S6alwaysJ = 0S7
S7J < 4 - PnoneS8
S7J >= 4 - PP = P + 1S5
S8M[J] > M[J+1]noneS9
S8elseJ = J + 1S7
S9alwaysswap M[J], M[J+1] (via T); J = J + 1S7
S10alwaysMED = M[2]; Q = 0; R = SUMS11
S11R >= 5R = R - 5; Q = Q + 1S11
S11R < 5MEAN = Q; I = 0; ACC = 0S12
S12I < 5noneS13
S12I >= 5Q = 0; R = ACCS15
S13alwaysD = M[I] - MEAN (signed)S14
S14alwaysACC = ACC + D x D; I = I + 1S12
S15R >= 5R = R - 5; Q = Q + 1S15
S15R < 5VAR = QS16
S16alwaysoutput MED, VAR; done = 1S0

FSM (control view, grouped)

 Input phase : S0 -> S1 -> S2 -> S3 -> S1 ... (5 numbers)
 Sort phase  : S4 -> S5 -> S6 -> S7 -> S8 -> (S9) -> S7 ... S5
 Mean phase  : S10 -> S11 (loop)
 Var. phase  : S12 -> S13 -> S14 -> S12 ... -> S15 (loop) -> S16 -> S0

17 states need a 5-bit state register (binary) or 17 flip-flops (one-hot).

Datapath

 din -> [ Register file M0..M4 (5x8) ] <- address mux (I, J, J+1, 2)
              | read A         | read B
              v                v
        [ comparator > ]   [ subtractor M[I]-MEAN ] -> [D]
              |                         |
          (gt to FSM)                [ multiplier D x D ] -> [ adder ] -> [ACC]
 [SUM] <- adder (SUM+din)
 [Q],[R] <- subtractor R-5, incrementer Q+1 ; comparator R>=5
 [I],[J],[P] counters/comparators (I<5, P<4, J<4-P)
 outputs: MED, VAR registers

Components: a 5x8 register file with two read ports and two write ports (so the swap in S9 fits in one state; with one write port, split S9 into two states); registers sized for the worst case:

  • SUM: 11 bits are enough (5 × 255 = 1275); 16 bits used.
  • D: 9-bit signed, since M[I] - MEAN ranges from -255 to +255.
  • ACC and R: 17 bits, since the sum of squared deviations can reach 78030 (e.g. inputs 0, 0, 0, 255, 255: 3 × 102² + 2 × 153² = 78030 > 65535).
  • Q and VAR: 16 bits, since ACC / 5 can reach 15606 (Q also gives the mean, which fits in 8 bits).
  • MEAN, T, MED: 8 bits; I, J, P: 3-bit counters.

Functional units: one adder, one subtractor, one 9x9 signed multiplier (product up to 65025), comparators and an incrementer.

Controller

FSM with inputs start, enter, gt, (I<5), (P<4), (J<4-P), (R>=5); outputs: register-file write/read selects, loads of all registers, ALU function selects and done. Block diagram:

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

Example: inputs 3, 1, 4, 1, 5 -> sum 14, sorted 1, 1, 3, 4, 5, median 3, mean 14/5 = 2 (integer), deviations of the sorted values -1, -1, 1, 2, 3, sum of squares 16, variance 16/5 = 3 (integer).

✓ Answer checked by Claude Opus 5.5 (high effort)

All Embedded System chapter 2 questions →