As you probably know, spreadsheet programs like Microsoft EXCEL can perform logic operations as well as normal math. I recently started experimenting with using EXCEL to model simple digital logic circuits. I was amazed at how easy it was! In only a couple of hours I was designing and testing really complex stuff.
So in this article, we will start out with a simple 1 bit full adder circuit, and then, through a series of EXCEL files, we will quickly move on to more complex designs, ending with an ALU or Arithmetic Logic Unit - the heart of a computer CPU,
Our ALU is only 4 bits wide, but it's so easy to build this stuff in EXCEL, thanks to "copy" and "paste", I could literally convert it to a serious 16 bit ALU in only another couple of hours!
Let's Get StartedThere are a couple of different approaches to doing logic design in EXCEL. You can just use 0's and 1's and regular math with if statements. For example, A = 1 - B is a way to express A = NOT(B). Or if (A + B > 0, 1, 0) is a way to express A OR B.
The other way is to use actual logic operations, like C = OR(A, B) or A=NOT(B), and express all inputs and output as TRUE or FALSE. I have chosen this second method - while it is slightly more typing, it is also slightly clearer. (If you don't like True and False, I will show you how to convert everything back to 0s and 1s a little later.)
I am doing another thing in an attempt to make everything visually clear. Since all logic cells are either True or False, I am using EXCEL's conditional formatting, which can be set up from the Home Screen. All True (1) cells are colored green, while all False (0) cells are colored red.
I am using a relatively old version of EXCEL (2010). If you are using a newer version, you may not have to deal with these issues:
- My version doesn't have EOR (exclusive OR) - newer versions do. So in place of EOR, I have to type OR(AND(A, NOT(B)), AND(B, NOT(A))).
- EXCEL treats a completely blank cell as a False and colors it red, so I have to put something in every cell on the page other than blank. A single space works or ="" works to keep the cells empty and white (not red).
Logic designers are quick to point out that all digital logic can be constructed from NAND gates. We are going to do something similar. We will build all our logic from the AND, OR, and NOT operators in EXCEL.
There are still a couple of other things I should mention before we actually start building stuff.
- All the EXCEL files referenced are contained in a single ZIP file. They gradually get more complicated, and they build on each other. For example, we start with a one bit adder, get it working, then build a 4 bit adder. then a 4 bit, one of 4 multiplexer, then an 4 bit, one of 8 multiplexer, etc. You will need to experiment with these EXCEL files as we go to see how everything actually works.
- Everything we will build here is more or less static logic - stuff that can be described with a truth table. We aren't using clocked operations and we are not building circuits with feedback, where A depends of B, but B depends on A. A digital flip flop is a good example. These things are possible in EXCEL, but require extra steps. I may pursue them in a future posting.
- As we move to more complicated circuits, we need to focus on inputs and outputs. There are always some cells in the middle that are performing important steps, but they are not inputs or outputs. What I am doing with these in EXCEL is moving them into a big pile, away from the inputs and outputs, just to get them out of the way. For lack of a better name, I will refer to this pile as the MLP or Middle Logic Pile. In EXCEL, we can just pile this stuff up any place, but if we were actually laying out this logic in an integrated circuit, how the stuff in the MLP is arranged would be very critical to how compact and how fast the resulting circuit is.
A full adder is a simple logic circuit that adds two bits together, and also knows how to deal with carry in and carry out. The digital circuit for it looks like this:
And its truth table looks like this:
Now let's look at the EXCEL file - it is "adder1.xlsx". It is laid out on the speadsheet exactly like the logic circuit above. The inputs A, B, and Cin are shown in column C, the Sum output in G4 and the Cout in H10. By changing the inputs, you can verify that the logic in the EXCEL files matches the truth table above.
The nice thing about EXCEL is that once you have something that works, it is easy to replicate it using "copy" and "paste". In this case, we need to use "paste formulas". The new EXCEL file is "adder4.xlsx". I have taken the 1 bit adder and made 3 copies. I then moved the 4 bit A and B inputs to the top of the page, put the 4 bit sum just below them, set the carry in bits equal to the carry out for the less significant bit just below it. You can see below that A=7, B=2, and the Sum is 9, with Cin and Cout both false.
Once we've moved the inputs and outputs up the the top of the page, the cells remaining below line 10 are what we earlier called the MLP. They are necessary to make the logic work, but can be moved anywhere that they are out of the way.
You can experiment with our 4 bit adder by changing the inputs and watching how the output changes.
4 Bit 1 of 4 MultiplexerA multiplexer selects from multiple inputs, and puts one of them on the output. For this project, we are eventually going to design and create an 8 bit, 1 of eight multiplexer that we will use in our ALU. However, to start, we will go slightly simpler and do a 1 of 4 multiplexer. Let's just go directly to EXCEL and see how it works. The file is mult4.xlsx.
Let's look at how the multiplexer works for bit 0. There are 4 possible inputs in column C on lines 7-10. In column E, each can be selected by the S0 and S1. The selected input is then passed on by the AND in column F, and to the output by the OR in column G. That process is then repeated for 3 more bits by copying the formulas on lines 7-10. You can select different S0 and S1 and see how the outputs change.
4 Bit 1 in 8 MultiplexerWe started with the 1 in 4 multiplexer because it is easy to see how it works when laid out that way. In this next file, mult8.xlsx, we add another select bit, double the number of inputs, and rearrange things, making our multiplexer more user friendly, but somewhat harder to understand.
We actually copied our 1 in 4 multiplexer, and then we use the new select bit, S2, to choose between the two 1 in 4's. That occurs in the block of cells in columns J through M in the MLP. We have also rearranged the inputs and the output into 4 bit numbers. You can see in the image above, S0, S1, and S2 are all true, selecting input 7, and it is what appears at the output. You can change S0, S1, and S2 and watch how the other inputs are selected.
You can also see that once we have arranged the inputs and output, we have a lot of other stuff at the bottom. Again, this is the MLP - logic we need, but can be put anywhere.
Arithmetic Logic UnitWe aren't quite ready to build our ALU yet, but we can discuss what it is and what else we need to build one.
The ALU is where a computer CPU does its math and logic operations. It’s like this magic box where the CPU does all its calculations. But really it’s not that magic. It takes the numbers in two registers (one or both being the CPU’s accumulators), performs all it’s potential operations in parallel using simple logic circuits, and then uses a multiplexer to select the desired result and post it in the output register. It's surprisingly simple when you see how it works!
So we are going to build a simple 4 bit ALU. It will be able to Add, Subtract, And, Or, and EOR two inputs A and B. It will also be able to Not, Inc (increment), Dec (decrement) the content of A. These are our 8 possible outputs. We will use S0, S1, and S2 on our multiplexer as the "opcode" to select the operation we want our ALU to perform.
We are only missing a couple of necessary logic circuits at this point. We already have our 4 bit full adder. We also need a 4 bit full subtractor, and we also need INC, and DEC. AND, OR, EOR and NOT are also needed, but they are all simple single cell operations.
4 Bit Full SubtractorThis circuit is very similar to the full adder, but we have to deal with borrow instead of carry. Here is the circuit for a 1 bit full subtractor:
And here is its truth table:
The EXCEL file for the 1 bit full subtractor is subtr1.xlsx. It is similar to the 1 bit full adder. You can try it and verify that it follows the truth table.
The 4 bit full subtractor is in file subtr4.xlsx. Again, it is similar to the 4 bit full adder. The borrows are linked and it is rearranged so that there are two 4 bit inputs, followed by a 4 bit output, with borrow in and borrow out. You can experiment with it to see how it subtracts.
In the 4 bit subtraction above, you can see 4 in register A and we subtract 7 in register B, which yields a difference of -3, which in this case is expressed as a difference of 13 after borrowing 16 (i.e. borrow out is True).
Increment and DecrementAt first I was thinking I would use my adder and subtractor to accomplish these tasks, but we really want dedicated logic for each of these operations and it turns out INC and DEC are very easy to implement as stand-alone operations. They basically use half-adders and half-subtractors.
Here is our Incrementor circuit:
And here is our Decrementor:
Both incrementor and decrementor are implemented in EXCEL file IncDec.xlsx. You can try them out and experiment there. I did not implement carry in or carry out with either of these functions. That might be useful, but normally, we use Inc and Dec within the range that our number can handle, e.g. count down to 0.
By now, we don't need a lot of explanation. We have built those two circuits, then put the input and two outputs up at the top, with the middle logic at the bottom, ready for the MLP.
Building the ALUSo now we are ready to proceed in building our ALU. So we don' throw everything at you all at once, I have 3 EXCEL files that work up to the final result.
The first is ALU_start.xlsx. Our multiplexer can select one of 8 4 bit results to sent out as the result. So our 3 bit multiplexer select (or Op Code) can choose:
- 0 ADDc
- 1 SUBc
- 2 AND
- 3 OR
- 4 EOR
- 5 NOT
- 6 INC
- 7 DEC
We also already have in place our two inputs, which are now labelled Accum and RegB. Up there with inputs and outputs, we also have carry in and carry out for our Adder. The add is already working, as is AND, OR, and NOT. Our MLP is continuing to grow down at the bottom of the sheet.
Even at this early stage, you can try out different inputs and get results by selecting the different OpCodes, at least for the functions that are already there.
In the next EXCEL file, ALU_step2.xlsx, we have installed the rest of the operations: Subtract, EOR, INC, and DEC. We do this by copying these new functions onto the spreadsheet, linking them into the existing inputs and outputs, and dumping everything else into the MLP. We have added borrow in and borrow out to our inputs and outputs, but not much else has changed except for the size of the MLP.
Our little 4 bit ALU is now fully functional, You can put whatever you want into the input registers, select the desired OpCode, and see the desired result. This is very similar to how real CPUs perform their math and logic operations!
We have one more EXCEL file, ALU_final.xlsx. In this file, I have switched all the inputs and output back to 1's and 0's, in case all those True and False cells have been driving you crazy. We can force EXCEL to express Boolean logic as 1's and 0's by placing two minus signs (--) in front of each logic expression.
I have also highlighted inputs in RED and outputs in BLUE, and have a place to input the op code in decimal (0-7). I also show the inputs and outputs in decimal, but the actual inputs are the binaries, in case you want to experiment. You can see in this animation how to experiment with the finished ALU:
And here are some other sample outputs from our final ALU for different OpCodes:
I was really pleasantly surprised to see how quick and easy is was to design digital circuits using EXCEL It took me far less time to create all these circuits than it did to write this article. And in case you are wondering, I have almost no experience in digital circuit design. This was a great learning opportunity for me.
I have also created a second article: Modeling Flip-Flops and Counters in EXCEL. These are more difficult to model in EXCEL, because they usually require control of the timing of how binary states propagate through the circuit. I had some limited success here, if you want to check it out.
Comments
Please log in or sign up to comment.