# How are most ALUs built, and is it possible to 'build your own'?

I've been REALLY really trying to learn about the very low level of computers. I've been looking at a lot of homebrew pages and it's quite a lot to take in. I took classes in electronic engineering in college, but we didn't really go into super depth on the subject (it was computer science, so most of it was in fact algorithms and such).

Anyway, how are/were most ALUs built? I realize that's not the only part of a computer, but still it's an important part.

And could you sort of build your own ALU just using logic gates (for learning purposes)? I understand this probably sounds stupid to those more knowledgeable, but I'm just trying to understand. (Heck, even a simple adder would be a neat project.) If so, how would this be done? Are there any hardware examples? (I've looked around Google, but I can't find anything that kind of has a step-by-step guide explaining things).

-
You might be interested in this. –  Dean Jun 30 '11 at 16:05
Speaking from inside a CPU chip team, if you're interested in computer and CPU architecture then the canonical books are "Computer Organization and Design: The Hardware/Software Interface" followed by "Computer Architecture: A Quantitative Approach". These are referred to as "Patterson and Hennessy" and "Hennessy and Patterson", respectively. –  Ross Rogers Jun 30 '11 at 20:32
@Dean - Yeah, color coded wires are for sissies! :-) –  stevenvh Jul 1 '11 at 14:11
Obligatory: 32-bit ALU implemented in Minecraft –  Mike DeSimone Jul 5 '11 at 0:55
@Shubham: Once I saw that I knew I needed to stay away from Minecraft... I'd get nothing else done... O_o –  Mike DeSimone Aug 7 '11 at 23:59

You can build them completely from basic logic gates, and the result will be a nice piece of art :-).

The 74xx logic series also contains a 74LS181, a 4-bit slice ALU, which simplifies things drastically. Bit slice ALU's were used to build more complex ALUs (read: longer word lengths), but newer technologies have made this kind of ICs obsolete.
note: TTL (74xx) is just one technology used for logic gates. Rarely used anymore. It was followed by Low-Power Schottky: 74LSxx, strictly speaking also a form of TTL. Nowadays there are dozens of logic families, all based on high-speed CMOS (74HCxx, 74HCTxx, 74ACxx,...)

These days the proper way to create an ALU would be to do it in a CPLD or an FPGA. This gives you a lot of gates, and the HDL (Hardware Description Language) you use to design the ALU is a lot easier and less error-prone than trying to figure out how to make the connections yourself with the logic gates. VHDL and Verilog are the HDLs of the day.

An alternative method to create an ALU (not using logic gates, though), would be a single parallel EEPROM/Flash. You use inputs A and B and the operation as input (address) and get the result of the operation as output (data). All you have to do is compile the ROM's content, meaning that you have to write at every address what the result of the operation will be for the corresponding inputs A, B and operation. Word size will be limited by the largest ROM size you can find.

-

You can build your own ALU, but even old computers with discrete TTL chips used some integration for that. For example, look at the 74x181 chip. That's a 4 bit slice of a ALU, and was used in some TTL computers to implement the full ALU by using one of these chips for each 4 bits.

-
Sorry to sound ignorant, but how Exactly are TTL chips different from Logic gates? and was TTL used before or after logic gates? or are they made from logic gates :confused: –  Mercfh Jun 30 '11 at 16:26
@Sauron - I added a note on logic families to my answer –  stevenvh Jul 6 '11 at 7:43

I would start in the HDL world first. Write some verilog, use verilator or icarus verilog to simulate it. Write the code such that it resembles discrete and, or, and not gates, then if so inclined find some 74xx series parts (recycled?) and breadboard something. OR, there are a number of \$50 plus or minus CPLD and FPGA boards that can be had and you can put the alu in one of those coming up with some sort of interface on the outside to see that it is working. I would argue that the HDL education is the same you learn the basics of the adder, etc and muxing the inputs and output and operation. But you can do it in an easy to use and see environment before taking it to hardware. Very much like how folks do it today, design and simulate then deploy.

-
Absolutely. Simulate first. Then if you have to have hardware, an FPGA is most practical. Wiring up 74xx TTL for something this complex would be a huge project that shouldn't be tackled until a design is really thought out. –  Chris Stratton Jul 28 '11 at 8:07

For books I definitely second the "Patterson and Hennessy" books (IIRC there are 3, disguised as 3 editions, but in reality totally different books. if you are serious: get them all.)

If you want to experiment your way into basic ALU or even CPU design: start experimenting with a logic simulator. We used DigitalWorks for our classes, but I would not recommend it. Logisim ( http://sourceforge.net/projects/circuit/ ) looks promising. What you need to master is layering: build basic blocks like a full-adder, a selector, and an edge-triggered flip flop from gates, then build registers, ALU, sequencing logic from those building blocks, all the way up to a CPU with memory. It is realy not that hard to build let's say the equivalent of a PIC (14-bit core) CPU, attach a bunch of LEDs and program it to show a Kitt display.

After that it will be fun to do a 32-bit core, port GCC to it, realise it in an FPGA and run Linux on it. But you won't be the first...

-

It's not a stupid question at all. The Wikipedia page shows such a gate-level circuit for a 2-bit ALU. ALU ICs used to commonly available in 'slices' - typically 4-bits, which you could cascade to get larger bus widths (see bit-slicing).

The datasheet for the Texas 74181 4-bitslice ALU has a gate level schematic also.

-

You should look into The Elements of Computing Systems by Nisan & Schocken.

-

The story of CPUs is one of increasing amount of stuff per package.

The earliest CPUs always used serial ALUs built from a few relays or vacuum tubes. The first to buck this tradition was the 1947 Whirlwind.

The earliest transistorized CPUs built everything from individual transistors.

The Apollo Guidance Computer (AGM), perhaps the first computer built from integrated circuits, used only one kind of IC outside the memory: 3-input NOR gates. The ALU and every other part of the CPU was built entirely from a large number of NOR gate ICs. The (much faster) Cray 1 also used only one kind of IC outside the memory: another kind of NOR gate.

As people figured out how to cram more transistors on a chip, later CPUs used (relatively) fewer chips to implement an ALU.

An ALU can be built entirely from multiplexers ("Multiplexers: the tactical Nuke of Logic Design"), using many fewer chips than the NOR implementation.

Dieter Mueller posted a 8-bit ALU design that has more functionality than two 74181 chips -- the 74181 can't shift right -- built from even fewer chips: 14 complex TTL chips: two 74283 4-bit adders, some 4:1 mux, and some 2:1 mux.

Like many historically important commercial computers, many home-brew CPUs use some version of the 74181, the first "complete" ALU on a single chip.

Many of those CPUs built a 8-bit ALU or a 16-bit (or both) out of a few 74181 chips and a few 74182 chips -- each 74181 only handles 4-bit-wide operations. Homebrew machines typically use the simplest possible thing that will work -- the carry-out of one 74181 feeding in to the carry-in of the next, forming a ripple-carry adder. Commercial machines that use 74181 chips typically use a 74182 look ahead carry generator to make addition and subtraction significantly faster.

Today most ALUs are hidden away inside some chip -- a small part of a CPU, some other kind of ASIC, or a CPLD or FPGA.

Even after "single-chip computers" were available, occasionally someone will build a 74181-compatible ALU out of a GAL, or an ALU using only simpler logic gates, or even individual transistors or relays, for learning purposes.

People have done it, therefore it must be possible.

A step-by-step guide explaining ALU design and implementation sounds like a really good idea. Please help us write one at the Microprocess Design wikibook, perhaps the "ALU" or "Wire Wrap" sections.

-

Historically, some ALU work was done with discretes (yeah, transistors and such), and some with gates, and a lot with 4-bit 'slice' chips (TI's SN74181 was an early one, and Fairchild 29F01 had its day).
But TODAY, it's a matter of building the logic equations in a gate-array or PLA logic description language. There are commercially available prebuilt modules that will plop any chosen small logic unit into your design, all it takes is money to license the 'IP' (intellectual property).

-
When resistors were cheap relative to resistors, there were a number of tricks that could allow for the construction of ALUs with very low transistor counts (e.g. a full adder using two transistors per stage). Such circuits were power-hungry and not terribly fast, but when transistors were expensive they could save considerable cost. –  supercat Jul 6 '11 at 17:05