# Digital Logic With Source [#2] In this second tutorial we are going to look at building some useful logic functions (such as a function that will add two binary numbers for us and output the result). We are also going to look at logic implementation in the Hammer Editor.

## Hammer Implementation

Hammer provides us with two entities with which we can form the base logic functions as described in the introductory tutorial:

• logic_branch
• logic_branch_listener

A logic_branch will act as an input or output. So we can set it’s initial value to 0 or 1, and in it’s Outputs window we can make it affect another logic_branch (or even another entity). However, it is made much simpler when using a logic_branch_listener. With this entity, if you have say 2,3 or even 4 inputs, you set the entity to listen to all inputs, and then affect the output accordingly. So for the AND function we would set the listener to listen to the inputs. Then in it’s Outputs we would have the following:

• OnAllFalse | and2_gate_x | SetValueTest | 0
• OnMixed    | and2_gate_x | SetValueTest | 0
• OnAllTrue  | and2_gate_x | SetValueTest | 1

For the OR gate:

• OnAllFalse | or2_gate_x | SetValueTest | 0
• OnMixed    | or2_gate_x | SetValueTest | 1
• OnAllTrue  | or2_gate_x | SetValueTest | 1

For the inverter we simply have two logic branches, where the output is the complement of the input.

For the XOR:

• OnAllFalse | xor2_gate_x | SetValueTest | 0
• OnMixed    | xor2_gate_x | SetValueTest | 1
• OnAllTrue  | xor2_gate_x | SetValueTest | 0

For the logic_branch_listeners, “gatenamehere”_gate_x is the output of our functions. We end up having something like this for the AND gate in the Hammer Editor: I also added sprites to visually be able to tell when a bit is high or low. I will be using the following textures in my Hammer logic implementation (feel free to use them):   I did not bother to make a NAND or NOR gate texture. To do that I will simply stick an inverter behind the AND or OR gate in the Hammer Editor. Similarly, I will abstract the XOR gate from the two inverters, two AND gates and one OR gate into the one XOR gate so it is easier to distinguish in our circuits. It’s texture will be: After a quick compile we can see that they all work (open the spoilers to see a GIF of the gate in action):

(Hammer) AND gate (Hammer) OR gate (Hammer) NOT gate (INVERTER) (Hammer) NAND gate (Hammer) NOR gate (Hammer) XOR gate Now that we have the Hammer implementation in place, we are ready to start working on some real useful logic functions.

We know how to add two numbers together. But how should we go about implementing it into a logic function? Well, we should start off by implementing a truth table. Remember how binary addition works:    Here C1 denotes carry 1.  Thus the corresponding truth table will be: With inputs A and B and outputs S and C (sum and carry). Our corresponding logical expressions for the outputs would be:

Sum:  Carry: Which as a circuit would look like this: However there is a problem. With this half adder there is no way for us to add together an n bit wide binary number. Suppose we wanted to add together two 4 bit numbers 0100 and 0011. As described in the previous tutorial any carry has to be carried across to the next bit in order to calculate what it’s value will be. In effect it ripples across. Similarly if we placed 4 of these half adders next to each other, we have no way of providing the carry from one half adder to be an input for the next half adder. Therefore we need a third input, a carry in. Again with truth tables we can represent this: Extracting the Boolean logical expressions gives us:

### Sum Expression   To minimise: We can use DeMorgan’s theorem:      However we can’t just apply DeMorgan’s to one side of the equation and not the other, hence: So now we have: And if we substitute in a variable to denote the formula: We now have:   Then we can sub the semantic equivalent formula held by Y back in: ### Carry Expression Which we can then minimise:   Here we can see that Cin ANDed with A XOR B is part of the Cout expression. We can look at the expression for S and see that the XOR function is both associative and commutative so we can turn: into: Which we did do, this just explains why we did it. This makes it easier to distinguish and we can see that for both outputs we have the A XOR B contributing to its outcome. We can then construct a circuit for this addition function: We now have a 1 bit full adder constructed from two half adders. This means we can add together two 1 bit wide binary numbers (alongside a carry in) and get the correct sum and carry out. This is the desired behaviour as it means we can place the carry out to be the carry in of the next 1 bit calculation, as you do when calculating with n bit wide binary numbers on paper. Hence, to make an 8 bit full adder we just place them next to each other and feed the carry out of the previous 1 bit full adder to be the carry in input of the next 1 bit full adder. The circuit would be as follows for an 8 bit full adder (Click to maximise): Given that we have the logical function for a 1 bit full adder sorted out now, we can implement that into Hammer: We can then place 8 bit full adders together in Hammer to get an 8 bit full adder. Click on the following image to enlarge: It is probably really hard to make out, but with the Hammer implemented 8 bit full adder we are getting correct results. Above you can see the result 56 from adding A = 2 + 4 + 16 = 22, and B = 1 + 32 = 33, and Cin = 1. Hence, we get A + B + Cin giving us 56, which is correctly displayed as the sum at the top of the 8 bit adder.

What if we wanted to subtract two binary numbers? Again, as we previously covered in the introductory tutorial, binary subtraction is much the same, except that we simply add 2’s complemented numbers. We could make a separate subber logic function, but as we have found out, subtraction in binary still makes use of the addition operator. This means we can use our adder logic block, except with a few alterations to allow it to choose between addition and subtraction. We know the following:   