## Introduction: Mechanical CPU Clock

The Mechanical CPU Clock shows the basic building blocks of a CPU (ALU, buses,RAM,registers, and a Control Unit). It executes a set of instructions which will emulate a simple wall clock.
The inspiration for this project came from trying to teach my son and daughter about how a computer works (in reality, I was always fascinated with mechanical computers and clocks, but I had to give a better excuse to my wife for buying a laser cutter specifically for this project). After looking around the web at various mechanical computers, I could not find something that represented all the components of  CPU. However, I draw my inspiration from the following projects (and their derivatives):
DigicompII: http://digi-compii.com/
Ball Logic: http://brain.wireos.com/?p=2207

In the following sections I will attempt to explain how everything works as it is being built. I am not going to go deep into the subject of computer science and will purposely try to avoid some terms, so to not confuse the novice reader. If I do use any terms, I will try to explain them simply. However, there might be some needed background information that I will miss, so please do not hesitate to contact me about trying to explain the concepts in more details (I don't promise anything, but I will do my best). Even if you are not going to build the clock, going through the sections will help in the understanding of how the clock/CPU works (the build sections will go over the concepts of ALU,RAM,register,control unit and buses). Again, one of the motivations for this project was to get people to understand how a CPU (the heart of a computer) works.
More details about the clock can be found here: http://www.liorelazary.com/index.php?option=com_content&view=article&id=46:mechanical-cpu-clock&catid=10:clocks&Itemid=15

123D: http://www.123dapp.com/idw-3D-Model/Mechanical-CPU-Clock/604647

The time (hour) is read in binary (http://en.wikipedia.org/wiki/Binary_numeral_system) from the middle register (register A, look for the highlight in the video where register A is at). Note the position of the 4 flip-flops (the 4 upside-down red T levers). If a lever is pointing to the right, then the digit is 0, if its on the left then its a 1. Write down the digits from each of the 4 flip-flops, from top to bottom. Then rewrite the number from left to right by rotating 90 degrees to the right, so that the left most digit is from the bottom and the right digit is from the top. For example. If the clock has the flips-flops at this position:

flip-flop 1 pointing left == 1
flip-flop 2 pointing left == 1
flip-flop 3 pointing left == 1
flip-flop 4 pointing right == 0

which is rewritten to: 0111 which is a binary representation of 7 decimal. So the time is 7 o'clock.

To Transform from binary to decimal (the number system that humans are used to):

add 1 if the top dial is pointing to the left or  nothing if its pointing to the right
add 2 if the second dial is pointing to the left or nothing if its pointing to the right
add 4 if the third dial is pointing to the left or nothing if its pointing to the right
add 8 if the forth dial is pointing to the left  or nothing if its pointing to the right

So  the above example translates to: 1 + 2 + 4 + nothing = 7 decimal

Here is a simple conversion from binary to decimal. So if register A has

0000 is 0 but its 12 in the clock case
0001 is 1
0010 is 2
0011 is 3
0100 is 4
0101 is 5
0110 is 6
0111 is 7
1000 is 8
1001 is 9
1010 is 10
1011 is 11

In the video the clock goes through a full cycle, 0,1,2...11 and back to 0, so see if you can follow it.

The minutes can also be read from the position of the ball and the lever (a bit hard to see in the video). The numbers on the outside represent the minutes in HEX format. To convert the number to decimal (the number system we are used to) you take the first digit to the left multiply that by 16 and add the right digit.

Example: 32 in HEX = 3*16+2 = 50 in decimal

00 is 12, 05 is 5, 0A is 10, 0F is 15, 14 is 20, etc.

## Step 1: CPU Instructions

For a CPU to do any meaningful work, it needs to be told what to do. This comes in the form of instructions. For this wall clock I will only concentrate on the hours to make things simple. That is, we will increment a variable named Hours and check to see if its equal to 11 (we will use 0 base indexing for the hours, so 12 is represented as a 0). If the check is true, then we will reset the variable back to 0.  We do not want to code the above statement directly into the CPU, since we want it to be general purpose CPU (otherwise we are just making a clock). Therefore, we will implement a basic instruction set and write the clock code around these primitive instructions (the set of instructions to execute are known as an assembly language). Lastly, we will keep the hour variable in a Register, which is a term given for a special memory component used to hold data and operate on it.  We will also use a simple 1 unit (1 bit) memory address to act as a flag for the control unit, and name it DTD for personal reasons (stands for dedicated to Dani. A long personal story). Note that this CPU will be programed with the clock instructions, but it could be programed with any other code to do a number of other things.

Here are the basic instructions (assembly language) that we will use:
INCRMENT: Increment register A by adding 1 to it.
EQUAL: If register A is equal to a specific number, then skip the next instruction.
CLEAR:  set register A to 0, set DTD to false
SET_DTD: set DTD to true
CHECK_DTD: if DTD is true, then skip the next instruction.

Here is the assembly language code for the clock (register A will hold the hours).

1: CHECK_DTD
2: JUMP 5
3: CLEAR
4: JUMP 1
5: INCREMENT
6: EQUAL 11
7: JUMP 1
8: SET_DTD
9: JUMP 1

Line 1 checks to see if DTD is set to true (this is used to indicate if we need to reset the hour). If its true the we jump to line 3 and set the hour to 0 and the DTD to false. If DTD is false then we continue with the next instruction and jump to line 5. Line 5 increments the hour (register A), while line 6 check to see if it equal to 11. If register A is equal to 11, then we jump to line 8 and set the DTD to true and jump back to the beginning (line 1). Otherwise, we go to the beginning. If we run though this code once an hour, then by reading register A, we can tell what is the current hour.

## Step 2: Hardware Design : Flip-Flop

We will now implement the primitive CPU instructions in hardware and program the CPU. I will use Autodesk Inventor for this mainly because of its great dynamic simulation (www.autodesk.com/Inventor). It also allows for parametric design, which makes modeling a lot simpler.

We start by implementing register A and the ALU (Arithmetic/Logic unit). The ALU is a CPU component that performs all the mathematical operations. In our CPU, the ALU will simply perform a simple operation of adding a 1 to register A. This is also known as an adder (http://en.wikipedia.org/wiki/Adder_%28electronics%29)

Start by creating a 12” diameter disc and attach a Flip-Flop (http://en.wikipedia.org/wiki/Flip-flop_%28electronics%29) to it. A Flip-Flop is a device that alternates its state with a given input. For example, a politician might change his stance on a specific issue based on some event, and then will change it back based on another event. In that case, we say he is a flip-flop, and we might be able to build a computer out of him:) Modern CPUs utilize transistors for their flip-flops, however we will build a mechanical flip-flop that will change its state based on a ball drop.

## Step 3: Hardware Design : Register

Next, create a cut in the 12” disk encasing the envelope of the flip-flop, so that the flip-flop can move freely (parametric design makes this process very easy).

## Step 4: Hardware Design : Register

Since we want our clock to count up to 12, we will need to represent 11 (0,1,...,11) in our register. Our flip-flop can only have two states, so we can use a binary representation of 11 using 4 flip-flops (1011). We will place the least significant bit (LSB) on top, and the most significant bit at the bottom. We will also create another register (register B) which will hold a copy of register A, and will be used to reset register A to a given number (the CLEAR statement). This will be done by resetting register B and copying the data back to A. Register B will also be 4 bits wide. We can simply do this by making an array of the flip-flop cut.

## Step 5: Hardware Design : Register C

Next we will add another register (register C), which will also contain a copy of A. This register will be used for the EQUAL statement to check if register A is equal to 11. We will want to increment and check register A in the same clock cycle (the same ball drop) so we can keep the timing correct. One way to achieve this mechanically will be to place register C under register A, but this will make the clock very long. By examining how the ALU (adder) will work on register A, we can see that the when we change the least significant bit (LSB) to 1, we can escape the register and use the ball to perform the check in register C. Since we are only doing this on the LSB, we can just check the 3 most significant bits (MSB), since we already know that the LSB will be 1. Therefore, we can place register C under and to the left of register A. This can be thought of as code optimization.

## Step 6: Hardware Design : DTD

Next we will implement the DTD bit flag (the control unit) in the form of  two tracks. If the ball goes on track A, then DTD is false. If its on track B, then DTD is true.

## Step 7: Hardware Design : Programming

Programming the clock is achieved by routing the ball to specific locations. That is, we will “hard code” the program into the CPU. The ALU is going to be programmed as an adder, by allowing the ball to continue down register A whenever the flip-flops are at 0, and escaping register A whenever they are 1 (overflow condition). Lastly, we will program the CHECK statement to check for the number 11. As mention before, we are only going to check the 3 MSB (101) so the ball will end up on track B every time this is true, and on track A every time this is false.

## Step 8: Hardware Design : Buses

Lastly we will add the bus on our CPU, to connect our registers together. As one of the registers changes, it will copy the data to the rest of the registers.

## Step 9: Hardware Design : Code

Here is the final clock with all the appropriate labels. It works by executing the flowing code.

1: CHECK_DTD
2: JUMP 5
3: CLEAR
4: JUMP 1
5: INCREMENT
6: EQUAL 11
7: JUMP 1
8: SET_DTD
9: JUMP 1

Line 1 is at the top of the clock, implemented as a wall. When the ball is on track A, it will hit the wall and drop to register A (line 2). In register A, it will increment the register (line 5) and check to see if register A is equal to 11 (line 6). If the check is false (DTD false) then the ball will end up on track A and the ball gets lifted to the top again (line 7). However, if register A is equal to 11, then the ball will end up on track B (DTD true) and will be lifted up to the top again (line 9). Since the DTD is true this time, the ball will be on track B and pass the CHECK_DTD by moving over to register B. Register B will clear itself to 0 and copy the data via the bus to register A. If will then end up at the bottom on track A (DTD false) and move back up to the top (line 4).

## Step 10: Hardware Design : Simulation

Before building the clock, we can simulate the ball drop using the dynamic simulator in Inventor. Due to the complexity on the simulation (and my lack of a server farm), we can just simulate it step by step, using some known constraints to help things out. Here is a simulation of the final stage (when the hour turns 11 and the DTD flag is set and the ball goes on track B).

## Step 11: Hardware Design : Lifter

To lift the ball on track A or B we will use a lever with two magnets and a motor to rotate the lever once an hour. This will allow the ball to drop once every hour and increment register A (which will hold the current hour).

## Step 12: Hardware Design : Clock Face

Since we can also read out the current minutes by the position of the lever, we can place the minutes numbers on the clock face just like in a real clock. However, just for fun we will place the minutes in Hex representation instead of decimal.

## Step 13: Building the Clock

We start building the clock by laser cutting the parts out of acrylic.  You can use what ever color you like, but some colors show up better then others, so you should experiment.
The piano wire used is 0.5mm which I got from do-it-center as well as the screws (6-32). I got 2.5 inch screws and cut them to length as needed. Other then that, you only need the acrylic. I used 1/2" for the tracks, and 1/8" for the rest (12x12 sheets).

Here are the Bill of Materials and sources
Acrylic Sheets (all cut from 12"x12" acrylic sheets ordered from http://www.estreetplastics.com/)
1x Face 1/8" acrylic clear
1x Tracks 1/2"  acrylic black
1x Base 1/8" acrylic clear/white depending if you want to see the minute hand
1x BackPlate 1/8" acrylic black
4x T flip-flops 1/4" acrylic Red
4x L flip-flops 1/4" acrylic Red (or other color to show the different registers better)
3x I flip-flops 1/4" acrylic Red (or other color to show the different registers better)
1x lifter 1/4" acrylic red
27x 1/8" acrylic O spacer

Misc Hardware
4x Magcraft NSN0601 1/4-Inch by 1/10-Inch Rare Earth Disc Magnets (2 go into the lifter slot) (From http://www.amazon.com/Magcraft-NSN0601-10-Inch-Magnets-50-Count/dp/B000E63O08/ref=sr_1_1?s=hi&ie=UTF8&qid=1334715032&sr=1-1)
10mm +/- 1mm steel ball bearing (Do it center)
8x 6-32 screws 1.3"  (I cut mine to length from 2.5" screw) (from Do-it center)
11x 6-32 screws 0.70" (I cut mine to length from 2.5" screw) (from Do-it center)
19x 6-32 nuts  (from Do-it center)
1x 4' x 0.5mm steel piano wire (from Do-it center)
8x 6-32  lock nuts (for securing the whole clock) (from Do-it center).
2x 6-32 Countersunk Screws to secure the motor (from Do-it center)
1x 1RPH motor (I got mine here http://www.herbach.com/Merchant2/merchant.mv?Screen=PROD&Store_Code=HAR&Product_Code=H1-33&Category_Code=SYNCTIME).

Overview of the assembly process.

## Step 14: Building the Clock

The buses are made out of piano wire. Note the angle band at the top, which will be used to adjust the final length.

Attach the buss wire to the flip-flops by forming an L band at the bottom.

## Step 15: Building the Clock

Place the ball inside and attach the face to the backplate and tracks with screws.

## Step 16: Building the Clock

The motor used for this clock is a 1 RPH (1 revolution per hour) timing motor, which gets very hot. To dissipate some of the heat, we will base a case for it out of aluminum sheet. Another, more complicated solution, will be top use a stepper motor.

## Step 17: Final Clock

Here is the final clock mounted on a wall.

Participated in the
Make It Real Challenge