## CS 202 - Homework 6

#### Due: Monday 4/9, at the beginning of class

The goal of this homework is to build a sequential circuit that implements a 4-bit multiplier. Details are included below and will also be discussed in class. You may work in groups of two, or by yourself if you prefer. If you choose to work in a pair, you should perform pair programming, that is, share a single computer and always work together. Each person must contribute equally.

### Detailed instructions

Save your circuit in a file hw6.circ. In your circuit, you may only use the following parts:

• Shift registers and JK-Flipflops (from the Memory library)
• Simple gates, possibly with some negated inputs
• Clock, pins, probes, splitters, tunnels, and constants (from the Wiring library)
• Buttons (from the Input/Output library)

The circuit is composed of the following parts:

• One 4-bit adder (use your circuit from HW 5 with "Sub" set to constant 0)
• One 5-bit and one 4-bit shift register
• Two 4-bit input pins for the input numbers A and B
• One 8-bit output pin for the product Y
• Unsigned decimal probes, attached to A, B, and Y
• Control circuitry, consisting of
• "start" and "step" buttons and a clock
• four JK Flipflops
• various AND and OR gates

#### Shift registers

Logisim's shift register unfortunately doesn't have labels on the pins. A 5-bit shift register (with Data Bits = 1, Number of Stages = 5, and Parallel Load = Yes) looks as follows:

Properly labeled, it would look like this:

This register stores 5 bits, which are displayed inside the rectangle and appear at the outputs q4..q0. The unlabeled output pin (let's call it "out") on the right edge is the same as q0. The register also has 5 data inputs d4..d0 at the top and the shift input "in" on the left. Finally, it has 3 control inputs ld, sh, clr, as well as a clock input. Upon receiving a clock tick it operates as follows:

• If ld=1, it loads the inputs d4..d0.
• If sh=1, each bit gets shifted one to the right, with "in" supplying the new leftmost bit.
• If clr=1, all bits are set to 0.

In your main circuit, you will need a 4-bit and a 5-bit shift register. If you want, you can add labels manually (or define larger subcircuits with labels), but it's also fine to use the register unlabeled.

#### Overall structure

The high-level structure of the multiplier circuit is shown below. The arrows in this picture represent multiple wires (4 or 5) in most cases. The left shift register has 5 bits: its leftmost input (d4) is connected to the carry output of the adder.

Two 4-bit pins will be used to enter the two numbers A and B to be multiplied. Let's call the 9 outputs of the shift registers q8..q0, left-to-right. We will ignore output q8. Outputs q7..q4 from the left shift register loop back into one set of inputs to the adder.

The two shift registers initially hold the numbers 0 and B, and in the end hold the 8-bit product Y = A*B. The following sequence is executed four times to compute the product:

1. If the right-most bit (q0) is set, add A3..A0 to q7..4
2. Shift everything one bit to the right, i.e., q8..1 -> q7..0

#### Control circuitry

We will use a synchronous design, and connect the clock inputs of all flipflops to a common clock signal "clk". The picture below shows a useful clock circuit constructed from a button, a clock, an OR gate, and a tunnel with label clk.

This circuit lets you step the clock manually (for debugging), or continuously (Simulate -> Ticks Enabled).

The whole multiplication sequence consists of 9 steps. The necessary control signals for each step are shown below (the letters A and B refer to the left and right shift registers, respectively):

```        step:  i   0  1  2  3  4  5  6  7    (done)

ldA    0   X  0  X  0  X  0  X  0     0
shA    0   0  1  0  1  0  1  0  1     0
clrA   1   0  0  0  0  0  0  0  0     0

ldB    1   0  0  0  0  0  0  0  0     0
shB    0   0  1  0  1  0  1  0  1     0
```

In this diagram, "i" is the initialization step, and steps 0..7 perform adding and shifting. "X" is 1 or 0 depending on whether or not A should be added to the current sum in the shift register. This depends on the rightmost bit, so X = q0. The two shift registers are connected so that bit 4 gets shifted into bit 3: inB = outA = q4. The other control inputs have constant values: inA = 0, clrB = 0.

To create the 9 states in sequence, we need a counter with at least 4 bits. We use four JK flipflops with outputs c0, c1, c2, c3. A clever encoding of states is to start counting at 7:

```        step:  i   0  1  2  3  4  5  6  7    (done)

c0   1   0  1  0  1  0  1  0  1     0
c1   1   0  0  1  1  0  0  1  1     0
c2   1   0  0  0  0  1  1  1  1     0
c3   0   1  1  1  1  1  1  1  1     0

7   8  9 10 11 12 13 14 15     0
```

To set everything in motion, we add a button to create a "start" signal, and use this signal to (asynchronously) set c0, c1, c2 (using input "1") and reset c3 (using input "0"). The "0" and "1" inputs are at the bottom edge of Logisim's JK flipflop; you can leave the other inputs unconnected.

The J and K inputs for flipflops 1-3 are the usual inputs for synchronous counters:

```        J1, K1 = c0
J2, K2 = c0 AND c1
J3, K3 = c0 AND c1 AND c2
```

For flipflop 0, however, we need to ensure that we stay in the "done" state (all flipflops = 0) once the product has been computed, and don't start over again. This can be achieved by setting

```        J0 = c3
K0 = 1
```

The only thing that remains to be done is to derive the equations for the control signals ldA, ldB, shA, shB, and clrA from c0, c1, c2, c3. This is easy and left to you :)

#### General remarks

• Think carefully about how to layout everything cleanly. Perhaps draw a design on paper first. Your circuit will be graded not only on functionality, but also on design and readability.

• Make good use of tunnels (named signals) to replace some wires. This will increase legibility a lot! For example, use the symbol "clk" for all your clock signals. Also, control signals (ldA, c0, ...) are good candidates for tunnels instead of wires. In cases where two signals are identical (e.g., shA and shB), you can use a single name (e.g., shift). Don't replace all your wires with tunnels, however. While it's often a good idea to use named signals for control signals, you should still use wires for most other signals, in order to visualize how the different components are connected.

• Create wire bundles for A3..0, B3..0, q7..0, and c3..0. Attach unsigned decimal probes to these bundles. Note that you can use splitters to branch off subsets of the wires in a bundle.

• Most importantly, make sure you understand how the circuit should operate. If you work in a team (hint: you should), test your understanding by explaining the circuit to your teammate. Walk through the different steps in the control sequence. For example, when you click the start button, clrA and ldB should go to 1. Then, when you create a clock tick by clicking the step button, 0 and B should get loaded into the two registers; also clrA and ldB should go back to 0, and ldA should now go to 1 if the rightmost bit of B was 1. On the next clock click, ldA should go back to 0, and shA and shB should go to 1, and so forth.

#### Electronic submission

Once your circuit works and you've tested it thoroughly, submit both the .circ file for your circuit, and a PDF file containing screen shots of the top level circuit and all subcircuits (if you have defined any - which is not required). Before taking screen shots, be sure that each circuit is labeled with your name(s) and the circuit's function. If you define subcircuits, make sure you label all pins.