# Blog Posts

## I am unreasonably happy that my 1 bit ALU board works.

One thing about my “Introduction to Digital Computers” series I wanted to verify was that all the circuits designed in the series in fact works correctly.

And one aspect of that–which we introduced in the first part of the fourth video–was the complete ALU.

While I’ve taken the liberty to do some minor redesigns (so as to reduce the number of transistors–things like realizing we don’t need a separate XOR circuit if we already have A XOR B embedded in the adder circuit)–I’ve built a circuit that is entirely made of transistors and resistors (with a couple of status LEDs to show the outputs visually on the board) that actually is a full 1 bit slice of my 8-bit ALU.

And I’m unreasonably happy that it (the second iteration of the circuit board design) works flawlessly.

Of course I have 7 more blank boards and a whole bunch of resistors and transistors on order.

And when they arrive I plan to wire it all up to demonstrate that the principles introduced here in fact work as advertised.

## Introduction to Digital Computers 4: Computer Architecture

Okay, this took much longer than I wanted it to.

But finally the fourth video for our Introduction to Digital Computer series!

In this fourth video I start to tie the pieces together. We first finish the Arithmetic Logic Unit, then we tie it in with six registers: an accumulator, flag register, and four general purpose registers. Along the way we solve three problems: how do we create a three-state gate to drive our bus, how we handle the timing of read and write operations, and how we can build an accumulator that can read and write at the same time. We then show these circuits in action, using the control lines to sequence through a very simple operation.

Sign up for an account and let me know what you think!

## Showing the weather.

So here’s a quick little Arduino project which combines an Ethernet shield, a display shield and an Arduino-compatible Adafruit Metro to display the current weather and forecast.

The goal of this project was to pull weather data from a remote server (in this case, the DarkSky API) to display the current temperature, weather and a 7 day forecast.

Now of course you can’t simply just plug in the Ethernet shield and display shield to have them both work together. While both uses the SPI system to communicate, the Ethernet shield uses pins 10 and 4 on the Adafruit Metro for the select line, while the Display shield uses pins 10, 9 and 4.

This means the first thing we must do is rearrange the pins so we don’t have a conflict. The easiest way to do this is to use a prototype shield.

Since the top sheild will be the display, we can use our prototype shield to shift pin 8 to pin 10 and pin 5 to pin 4. We cut the connector pins and use jumper wires to make it so that pin 8 on the Arduino connects to pin 10 on the shield, and pin 5 to pin 4. A quick modification of the Arduino GFX library will allow us to run both the Ethernet and display shields at the same time.

This allows us to assemble our stack.

The second problem we have with this setup is that the Ethernet shield does not connect to HTTPS connections as it doesn’t support SSL/TLS. This can be solved by using a WiFi shield which has SSL/TLS built-in.

I solved it for my setup by using a short Java program uploaded to a remove server which performs the HTTPS request and proxies it through to an HTTP request.

In Java, the servlet code simply takes a request, forwards the request to the DarkSky API server using SSL, and returns the results:

```/*  WeatherForwardServlet.java
*
*/

package com.chaosinmotion.wforward.server;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author woody
*
*/
public class WeatherForwardServlet extends HttpServlet
{
static long throttle = 10000;       /* 10 seconds */
private static final long serialVersionUID = 1L;

static Object syncObject = new Object();
static long lastCachedTime;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
ServletOutputStream out = resp.getOutputStream();

/*
*  Determine if we're getting requests too fast.
*/

resp.setContentType("application/json");

synchronized(syncObject) {
long time = System.currentTimeMillis();
if (time - throttle < lastCachedTime) {
out.println("{ \"error\": \"throttle\", \"errorid\": -1 }");
return;
}
lastCachedTime = time;
}

/*
*  Pull the request and forward
*/

String uri = req.getRequestURI();
int index = uri.indexOf("api/");
String trim = uri.substring(index+4);

/*
*  Make request
*/

String request = "https://api.darksky.net/forecast/" + trim;
URL url = new URL(request);
URLConnection conn = url.openConnection();
conn.setDefaultUseCaches(false);
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);

InputStream is = conn.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while (-1 != (len = is.read(buffer))) {
baos.write(buffer, 0, len);
}
is.close();

out.write(baos.toByteArray());
}
}```

Compile into a Java servlet and upload to your favorite server and now requests are being forwarded and the results directly returned without modification.

The third interesting problem is this: when you make a request to the DarkSky API, you get back a result that is about 33K in size.

An Arduino Metro only has around 2K of RAM.

So how do we solve this problem?

We take a clue from the SAX parser for XML. The way the SAX parser works is that it scans the input stream, and makes appropriate subroutine calls based on the symbols currently parsed. The beauty of this system is that we can easily discard information we don’t care about, keeping only the information we’re interested in.

In this case, we create a new parser engine to parse our JSON response. the JsonParser class, which performs method calls if we see the start of an object, the end of one, the key/value pair, the start and end of an array, and values in the JSON stream.

This allows us to build a very simple parser which scans only for the information we care about: today’s temperature and wind directions, the pressure, the appropriate icon to display, and the first seven days of the long-term forecast. All of this is done in the DarkSkyParser class.

With that, it’s a simple matter of then getting the weather information from the DarkSky API, and displaying the results. All of this is handled in the main Weather entry point.

The complete sources for the Arduino project is on GitHub.

## Introduction to Digital Computers: an Update.

So here’s where the next video stands.

First, as I noted in a previous blog post, I’ve settled on an 8080-like MPU with 6 8-bit registers and an internal 8-bit bus.

Now to make an internal 8-bit bus work, you nee a tri-state gate, a logic circuit which can be used to disconnect our logic gate from a common bus. The idea here is that you may have six things that can write their output to a single set of lines (the bus), but only one thing actually writes out to the bus at the same time.

Which I now have a successful design for.

I’ve also figured out the timing issues for the accumulator. Basically the accumulator register is always tied to the output of our ALU–but because you’re reading the results into the accumulator at the same time you’re writing the previous value as the output. So ideally we want an accumulator which can latch the results internally when a clock signal changes state–but doesn’t actually output the results until the clock state changes back.

An interesting side effect is that if you want to copy a value into the accumulator, it is easier to simply write the results through the ALU. Meaning we need a control state on the ALU that simply copies the input across. (On the other hand, it means instructions like the 8080 instruction `SUB (IX+index)` relatively easy, since once the address is loaded, the memory value at address IX+index passes through the ALU on its way to the accumulator.)

And that means the test ALU board I just sent off for fabrication (and which will probably have a one-month turnaround, because you can have it fast or have it cheap–but you can’t have both fast and cheap) is now out of date.

Don’t worry, I’ll explain all of this in an upcoming video.

I do plan to build separate boards for the regular registers (the B/C/D/E and F registers), and for the A register (which does not have a write-enable line, but does contain multiple latches to shift values on both the clock signal going high and going low).

The only other circuit I need is to develop 90 degree out of phase signal to properly time the loading of all of the registers and the ALU.

Once I have that tested, I can start putting together the next video.

## Making Circuit Boards.

So one of my goals of my series “Introduction To Digital Computers” is to show the design of a computer that can be built entirely out of NPN transistors. The idea is that by showing the design of a digital computer from the absolute ground up–from something you can hold in your hands rather than from some theoretical abstraction–it makes the design of a digital computer a concrete thing. And hopefully that makes the entire process of designing a digital computer more “real” rather than some abstraction in some textbook somewhere.

But I’m beginning to realize that if I’m going to design something from NPN transistors, I need to build the circuits to make sure they work. After all, there are a lot of designs purportedly showing designing gates using NPN transistors that simply will not work.

So I’ve started relying more on MacSpice and iCircuit to verify my designs.

That doesn’t go far enough.

So the other day I’ve spent some time building the final ALU.

The circuit diagrams (from Eagle) were translated into two 100mm by 100mm boards (since the 61 transistors and countless resistors don’t fit on one board), one board which contains the logic for the ‘S’ output line (the sum/difference, and AND/OR/XOR/NOT logic) and one board which contains the logic for the ‘C’ output line (the carry/borrow/shift logic). Both schematics as PDF files can be downloaded here: BitALU.zip

And the reason for the 100mm by 100mm boards is that Seeed Studio has an amazingly cheap deal on 100mm by 100mm boards. (But make the board a millimeter above the given size, the price jumps from \$5/board to \$50/board.) I think the way they can offer this deal is that they’re manufacturing their own boards for their own products–and they are selling leftover space on those sheets.

Hopefully the design is correct; after assembling one board and verifying it works correctly I plan to run off a set of 10, populate 8 of them, and assemble a fully working 8-bit ALU in order to verify the design.

I also plan to verify the design of the register circuits from the prior video, and actually assemble them.

The target, in the end, is a computer which has 6 8-bit registers: an accumulator, a flags register, and four additional intermediate 8-bit registers B, C, D and E. This is similar to the 8080 MPU, though instead of an H/L register, the ultimate design will use a 16-bit IX register, a 16-bit PC register, a 16-bit stack pointer, and internal registers for processing microcode and latching intermediate results to and from main memory. (The design of the IX register will be limited only to those instructions necessary to maintain a call stack for C–which means we only really need instructions to push and pop IX from the stack, to copy SP to IX and back, and to access memory at a fixed 8-bit and 16-bit offset from IX. Of course this also implies that the B and C registers as well as the D and E registers can be combined to form a 16-bit address.)

But baby steps, baby steps.

So this means the next video may be delayed another few weeks so I can verify my designs.

## A note about the next Introduction To Digital Computers video.

I originally planned to try to get this video out by the end of October.

But I just returned from a two week trip to Spain and the south of France–and before I release a video which starts to tie together the registers developed in the third video with the arithmetic logic in the second video, I want to put together these circuits to test to make sure they in fact work.

And this will take some time.

So the next video may be delayed a week or two.

Sorry for any inconvenience.

## Introduction to Digital Computers 3: Flip Flops

It’s been about a month, and here’s the third video explaining how computers work from the ground up.

In this third video I discuss flip flops. Starting with two not gates hooked together I discuss how a bi-stable circuit operates, extend this using NOR gates to Set/Reset flip flops, build data flip flops, edge-triggered data flip flops and 8 bit registers which can store up to 8 digit base-two numbers. I also talk a little bit about signal delays through transistor circuits, setup and hold times, and I also briefly introduce edge-triggered Set/Reset flip flops, J/K flip flops and T flip flops, as well as how to use them to build a counter.

All of this, of course, literally comes from the ground up, since everything can be built using 2N3904 transistors, a few LEDs, 330&ohm; and 10k&ohm; resistors, and a few switches to wire it all up.

My hope is to release subsequent videos about once a month, give or take.

Sign up for an account and let me know what you think!

## Gear Tooth Geometry.

I feel like, with making the first two “Introduction to Digital Computers” videos I’ve gained a superpower. And I’ve used that power to help me visualize and solve another problem I was having, figuring out the shape of spur gears (and eventually, internal spur gears, the next major feature I’d like to add to the Kythera application).

Which led me to making this video.

As always, show notes give further details.

Let me know if you find any problems or have any questions.

And as always, thanks for watching.

## A second pass at my earth/moon orrery.

I’ve come to realize making something from scratch winds up building a bunch of prototypes as you both perfect the plans and figure out how to make the parts.

And in my case, while the plans may be great–my ability to machine parts is… shall we say questionable?

At any rate, I have a second pass at the Earth/Moon orrery.

A few observations about my design.

First, the gears don’t quite have the axle holes dead center. It’s not that I don’t own a set of mill center drills, which are useful for starting a hole at the exact spot you want it at (rather than trying to use a drill bit and getting a hole wherever the damned bit bends to), it’s that I forgot to use them when I made the center holes of the gears.

So I have to remember when cutting a gear to use the following steps, which I document here because someone else may find these useful. (I’d make a video, but I’d need to license the Benny Hill Yakety Sax music.)

1. Measure and cut the blank for the gear from 1/8th or 1/16th thick sheet metal. (I’m using aluminum because it’s cheaper than ruing hundreds of dollars of brass.) The blanks should be slightly bigger than (N+2)/32 inches (for 32P gears, which is the size I’m cutting) in diameter. (I made the mistake once of thinking “radius.” That wasn’t good.) The blank should be drawn around a divot in the center made with a hand punch.

2. Set up an arbor for the gear. Take a piece of aluminum rod close in diameter to the gear we’re cutting, mount and center on the lathe, and face the arbor. This causes the end of the arbor to be flat relative to the cutting surface we’re cutting on.

Note that all of this is taking place on the little Sherline, because one of the steps will completely screw up the center otherwise.

3. Attach the blank to the arbor. Using a center on the tail stock and a dash of superglue, superglue the gear blank to the arbor. Use the tail stock center to help center and push the gear blank on the arbor, and allow five to ten minutes for the glue to set.

4. Cut the gear blank. On the lathe, turn the blank to the desired diameter. For 32P gears, this is the Outer Diameter of the gear, and is (N+2)/32 inches in diameter for a gear with N teeth.

5. Using a mill center drill bit, start a center for the axle. This is the step I kept missing, and that caused my gears to be slightly off center. The end result is a tiny little divot at the exact center of the gear blank, which then can be used to drill a hole without having the drill bit wander too far off center.

6. Drill the center hole the desired diameter. Enough said. In the case of the orrery, I have to remember one of the 52-teeth gears has a center hole of 9mm. (At some point once I’ve perfected the plans, I’ll upload the instructions for making this device.)

7. Re-mount the gear blank on the mill. I assume the mill has been set up for cutting gears, with the appropriate cutter mounted on the cutting arbor, and the rotary table set up at a 90 degree angle. At this point you’ll need to then verify the gear cutter is centered in the gear to be cut.

8. Start cutting gears. What I’ve been doing–to great effect–is to center the gear across the cutter, so turning the Y axis knob in front slides the gear into the cutter. The moment I hear the tell-tale sign of the gear cutter starting to cut the gear, I note the depth on the knob (all my mills and lathes are marked in inches), rotate the X axis to get the gear out of the cutter, and turn the knob into the cutter 0.0625 inches. This is the desired depth of the teeth.

(We get 0.0625 inches from the observation that if the outer diameter of a gear grows by 2/32″ for a 32P gear, this means the inner diameter must shrink by a corresponding 2/32″ as well. This means the diameter difference between the outer and inner diameter differs by 4/32″–and the tooth depth, the delta of the radius–must be half that, or 2/32″. That is, the tooth depth is 2/32″ = 0.0625″. By listening for the cutting, my guess is that I’m cutting the teeth a hair wider and a hair deeper than needed. On the other hand, this works out fairly well, given that the final mechanism I built, pictured above, rotates pretty freely with center holes precisely at the locations my Kythera program predicted.)

Once the gear depth is set, lock the Y axis, and start cutting gears by turning the rotary table the desired amount and sliding the gear blank through the cutter using the X axis.

I don’t have any CNC motors attached to my mill, only to the rotary table–so this gets pretty boring pretty fast.

9. If all goes well, separate the blank from the arbor. The technique I’ve seen which works well is to use a blowtorch and heat up the gear blank until the superglue releases. Try not to do this on cement, because you can cause the imperfections in the cement to pop, throwing small bits of cement at your face.

The different gears that are supposed to be attached to each other, by the way, I simply superglued together. This is probably not a great long-term solution, but in the short term it works very well.

I think with a little better technique I can cut the gears and mount them and have a functioning orrery. But the last gear, the thing that rotates around showing the position of the moon–that leaves a whole lot to be desired.

Originally I had built a 14 tooth gear using 1/8th inch thick aluminum, and then cutting a separate component:

This I then superglued to the 14-pin tooth, and I cut the 6mm end with a 6mm thread, which I then screwed a cap on top that holds the pin that will eventually support the moon.

And this went… poorly.

The stupid part is that in retrospect this should have been cut as a pinion:

And the moon should have been mounted not by screwing the top piece on, but by using a grub screw.

Well, in a few days I’ll go back to cutting parts and seeing if I can’t machine something better. Meanwhile, version 2 of my orrery prototype.

## Announcing a new Quick Video: A Full Adder

When we used NPN transistors to build our logic gates, it implies they can be used for building bigger things. In this video I’ve built a full 1 bit adder from our second Introduction video, entirely from discrete transistors.

Build 8 of these–and you have an 8-bit adder.

The show notes include the complete circuit diagram.

So here’s the video.

And thanks for watching.