# Papilio multiply
## Problem

The goal is to multiply two four bit numbers in hardware. The goal is to build a little bit of a CPU that was originally a separate chip called a math co-processor or ALU. The goal is to demonstrate it's operation on a papilio with a logic start mega wing. This project was inspired by Fig. 8.19 page 465, of Chapter 8 (Algorithmic State Machines) of the text book "Digital Principles and Design" by Donald D. Bivone First Edition copyright 2003.

## Conceive

This problem can be done with simple VHDL code. It can be done with Intellectual Property (IP) of the major FPGA vendors. Or it can be done from scratch using an Algorithmic State Machine (ASM) which is started through an OpCode which the CPU finds in it's instruction register, two 4 bit shift registers, a 4 bit adder, a flipflop and a counter.
The goal is to build an intuition/confidence that one is actually design part of a CPU. Specifically a CPU's shift registers, latch and adder are the shared resources used. The ASM circuit is actually using these shared resources to create a multiplication operation.
The goal is to write the VHDL with tests for each module, then put it all together with a VHDL test and finally implement in a papilio using 8 switches on the logic start mega wing to input two 4 bit numbers and display 8 bits of the results on the logic start LEDs.

## Design

This project depends upon a lot of little circuits working. The goal was to start with the Wikibook designs, and then add to them, or modify them.
In designing the circuit that controls the LEDs showing the 4 least significant bits (lsb), we first focused on building the state machine. We actually needed 3 inputs: one input signaling that the register has been cleared and received the multiplier(first state), one input signaling the shift, and one input holding the value of the least significant bit of the partial sum stored in the register.
To ensure that the state machine is working, we decided to light all 4 LEDs once the clear signal is on, and turn them off to simulate the shift. Then we decided to add the flashing desired. It turned out to be the most challenging part:
* We first added the modules we built from other projects. We customized the .coe file to match one LED. The LED will just turned off. We decided to test the file on its own, and despite enabling only one LED, all will lit up with solid light. The goal was to see if we could customize the .coe file to match the pattern for 0 or 1 being concatenated.
* Then, we decided to blink the LEDS. We tested the code to blink the LEDs by itself and it worked. Then we add it as a module. First, it turned the LED off. There was no blinking. We decided to make it a process but it seems we cannot have a 'rising_edge(clk)' twice when designing a circuit. You can only use 'if' in a process, so we added the counter in different parts of our main process with no success. The signal that was supposed to blink the LEDs is never assigned so the compiler will give a constant value of 0. Therefore, it just turns off the LEDs. We decided then to blink the LEDs from the module. It did not work either. We finally tried to have as bidirectional ports the LEDs in the module and in the main vhdl code, it showed the LEDs having multiple drivers.
You will find below the code to blink the LEDs (We have two different patterns to represent 0 or 1). We also have the ASM for the flash-led circuit. We are using switches to simulate our three inputs. The "concatenated" bit did not work out so well with the switch but we needed to test it somehow and it can be implemented.

## Operate

presentation

## Demo

* Presentation
* Video of how multiplying works when using the IEEE VHDL arith.all library.
* Here is the demo of the multiplication using the papilio. we start with everything at zero, the four left most switches control the two leftmost digits(A value from 0 to 15 in decimal is displayed), and the four rightmost switches control the two rightmost digits(A value from 0-15 is also displayed here), once the combination is entered, the joystick is pressed, and the result from the multiplication is shown.
on only 3 digits because the maximum number we can have is 225 when all the switches are up.

## Next Steps

Just one piece of this circuit has been built and then it was redesigned. The current design needs to be built.