{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CS 201: Gates and Circuits\n", "\n", "

\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Gates and circuits I.

\n", "\n", "

\n", "Summary:\n", "\n", "- Introducing logic gates (for AND, OR, NOT, XOR, NAND, NOR), \n", "wires, and circuits.\n", "- Feedforward or combinational versus sequential (\"loopy\") circuits.\n", "- Computing the time (gate delays) of a combinational circuit\n", "- The\n", "role of parallelism in circuits.\n", "- Another useful gate: XOR.- Boolean bases: gates of types {AND, OR, NOT} are sufficient\n", "to compute any Boolean function, by the sum of products representation.\n", "\n", "

\n", "However, each of the following sets is also a complete basis\n", "for the Boolean functions: {AND, NOT}, {OR, NOT}, {NAND}, {NOR}.\n", "We left open the question of whether {AND, OR} or {XOR, NOT} is\n", "a complete Boolean basis.\n", "\n", "

\n", "Please also see the notes:\n", "Gates and circuits\n", "and\n", "Boolean bases.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "We looked at a circuit for the key/door/seatbelt alarm signal,\n", "derived from the Boolean expression a ≡ $k * (d' + b')$.\n", "Recall that this computes an alarm signal ($a$) as a function of\n", "sensor values for the key in the ignition ($k$), the door closed ($d$),\n", "and the seatbelt fastened ($b$).\n", "Drawings of the symbols for six types of gates and for the\n", "alarm circuit are in:\n", "Gates and circuits\n", "

\n", "In the alarm circuit, there are 4 gates (two NOT gates, one AND gate, and\n", "one OR gate.)\n", " We model the time for this circuit to compute its\n", "output as follows.\n", "Each gate takes some small but non-zero time to get\n", "the correct answer on its output wire after its inputs are\n", " changed, its delay.\n", "In reality, different gates will have different delays,\n", "but for our purposes we assume that each gate takes\n", "the same time, a unit we call 1 gate delay.\n", "If at a given time $t$ the inputs $k, d, b$ are correct and\n", "stable, then after 1 gate delay, the outputs of the two NOT\n", "gates will be correct and stable, (namely, $d'$ and $b'$.)\n", "Note that the two NOT gates operate in parallel -- neither\n", "has to wait for the other's output.\n", "After another 1 gate delay, the output of the OR gate is\n", "correct and stable (namely, $(d' + b')$).\n", "Note that we cannot assume that the inputs to the OR\n", "gate are correct and stable until the NOT gates have\n", "produced correct outputs.\n", "Finally, after 1 more gate delay, the output of the AND gate\n", "is correct and stable (namely, $k*(d' + b')$.)\n", "Thus, the delay for the whole circuit is $1+1+1 = 3$ gate\n", "delays.\n", "This is also the number of gates in the longest path\n", "of wires and gates from an input wire to an output wire,\n", " the depth of the circuit." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "Because any Boolean function can be expressed using AND, OR and NOT,\n", "we know we can build a circuit to compute any Boolean function using\n", "just AND, OR and NOT gates.\n", "However, we note that\n", "\n", "$$(x + y) \\equiv (x' * y')'$$\n", "\n", "that is, OR can be expressed using just AND and NOT.\n", "(This equivalence can be checked using a truth table, or\n", "by applying one of DeMorgan's laws and the law of double negation:\n", "\n", "$$(x' * y')' \\equiv (x')' + (y')' \\equiv x + y$$\n", "\n", "Thus, everywhere we have an OR gate, we can substitute a small\n", "circuit of 3 NOT gates and an AND gate.\n", "That is, we can build a circuit for any Boolean function using\n", "just AND and NOT gates; the set of gate types {AND, NOT} is a\n", "complete Boolean basis.\n", "By duality, we could replace every AND gate using 3 NOT gates and\n", "an OR gate using the dual equivalence:\n", "\n", "$$(x * y) \\equiv (x' + y')'$$\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Thus, the set of gate types {OR, NOT} is a complete Boolean basis.\n", "In fact, if we consider the NAND gate, which computes the NOT-AND function\n", "\n", "$$(x \\rm{NAND} y) \\equiv (x * y)'$$\n", "\n", "then we can build a circuit for any Boolean function just using NAND\n", "gates.\n", "The truth table for NAND is as follows.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n",
    "    x  y  |  (x NAND y)\n",
    "   --------------------\n",
    "    0  0  |      1\n",
    "    0  1  |      1\n",
    "    1  0  |      1\n",
    "    1  1  |      0\n",
    "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because (x NAND x) ≡ x', we can use a single NAND gate\n", "both of whose inputs are x as a replacement for a NOT gate.\n", "And because $(x NAND y)' \\equiv ((x * y)')' \\equiv (x * y)$,\n", "we can use a pair of NAND gates to replace each AND gate\n", "in a circuit.\n", "Because we previously showed that {AND, NOT} is a complete\n", "Boolean basis, we conclude that {NAND} is a complete Boolean basis.\n", "Dually, we consider the NOR function: (x NOR y) ≡ (x + y)' and\n", "can similarly show that {NOR} is a complete Boolean basis.\n", "(As a footnote: The \n", "Wikipedia article on logical NOR claims that\n", "\"The computer used in the spacecraft that first carried humans to the moon, \n", "the Apollo Guidance Computer, was constructed entirely using NOR gates \n", "with three inputs.\")\n", "\n", "\n", "

\n", "We left open the questions of whether {AND, OR} or {XOR, NOT}\n", "might be a complete Boolean basis." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Gates and circuits II.

\n", "\n", "

\n", "Summary:\n", "\n", "- A combinational circuit to compare two 4-bit numbers -- output\n", "1 if they are equal and 0 if they are unequal.\n", "- Comparing the time taken by different organizations of the tree\n", "of AND gates combining the results for individual bits.\n", "- A combinational circuit to add two 4-bit binary numbers, getting\n", "a 5-bit result: a half-adder, a full-adder, and combining them\n", "into a ripple-carry adder.\n", "- The time taken by the ripple-carry adder, and the distinction\n", "between linear time (BAD) and logarithmic time (GOOD) time for circuits.\n", "\n", "\n", "

\n", "Please see the notes:\n", "Equality circuit\n", "and\n", "Addition circuit.\n", "

\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "In this lecture we consider two combinational circuits,\n", "one to compare two 4-bit numbers for equality, and one to\n", "add two 4-bit numbers to get a 5-bit sum.\n", "In a computer, combinational circuits perform basic operations\n", "like adding, subtracting, multiplying or dividing numbers, comparing\n", "numbers and other quantities, performing shifts and logical operations,\n", "and the like.\n", "The other important type of circuit, sequential circuits, are used\n", "to implement memory, storing values before and after they are operated on.\n", "The next lecture will consider sequential circuits." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "To construct a circuit to compare two 4-bit quantities,\n", "$x1, x2, x3, x4$ and $y1, y2, y3, y4 $for equality, we\n", "first consider a circuit to compare two single bits for equality.\n", "The input is two bits, $x$ and $y$, and the output is one bit $z$,\n", "where $z = 1$ if and only if $x = y$.\n", "A truth table for this Boolean function is as follows.\n", "

\n",
    "    x  y  |  z\n",
    "   ------------\n",
    "    0  0  |  1\n",
    "    0  1  |  0\n",
    "    1  0  |  0\n",
    "    1  1  |  1\n",
    "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We could use the sum-of-products algorithm to write an\n", "expression for $z$, namely, $z = x'*y' + x*y$.\n", "We could also observe that the function in this table\n", "is just the negation of the XOR of $x$ and $y$, because\n", "
\n",
    "    x  y  |  (x XOR y)  (x XOR y)'\n",
    "   -------------------------------\n",
    "    0  0  |      0          1\n",
    "    0  1  |      1          0\n",
    "    1  0  |      1          0\n",
    "    1  1  |      0          1\n",
    "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Thus, we can also write $z = (x XOR y)'$.\n", "These two expressions lead to two different circuits\n", "for the given Boolean function.\n", "Using the expression $z = x'*y' + x*y$, we get\n", "a circuit with two NOT gates, two AND gates and an OR\n", "gate, with a gate delay of 3 in the longest path.\n", "Using the expression $z = (x XOR y)'$, we get\n", "a circuit with one XOR gate and one NOT gate, and\n", "a gate delay of 2 in the longest path.\n", "Of course, the second circuit assumes that gates\n", "computing the XOR operation are available.\n", "

\n", "Once we have a circuit available for computing whether two\n", "bits are equal, we draw a rectangle around it, and give it\n", " a label, say $EQ$, and use it as a subcircuit in our\n", "further circuit design. This kind of abstraction, like\n", "writing auxiliary procedures in Racket, helps to keep\n", "us from being overwhelmed in details as our designs\n", "get more complex." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given the design of $EQ$, we use four copies of it and three AND gates\n", "to compare two 4-bit numbers, a number $x$ represented by inputs\n", "$x1, x2, x3$, and $x4$, and a number $y$ represented by inputs\n", "$y1, y2, y3, y4$, and produces an output $z$, where $z = 1$\n", "if and only if $x1 = y1$ and $x2 = y2$ and $x3 = y3$ and $x4 = y4$,\n", "that is, the two 4-bit numbers $x$ and $y$ are equal.\n", "Please see the notes:\n", "Equality circuit\n", "

\n", "A circuit for 4-bit binary addition.\n", "Please see the notes:\n", "Addition circuit.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Gates and circuits III. How to build a computer memory

\n", "\n", "

\n", "

Summary:

\n", "
  • Using a selector to choose one of two inputs to be an output. (Read one bit of Memory)\n", "
  • Sequential circuits:\n", " \n", "

    \n", "\n", "

    \n", "Please also see the diagrams:\n", "NAND latch and D flipflop." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

    \n", "Previously we saw the design of a circuit to add two 4-bit numbers\n", "to produce a 5-bit sum. We could also design a circuit to subtract\n", "two 4-bit numbers to produce a 5-bit difference. (Exactly what that\n", "means will become clearer when we talk about number systems for \n", "computers.)\n", "Suppose we want to choose between the sum and difference of two\n", "4-bit numbers based on a Boolean value $s$.\n", "That is, we want a circuit with inputs $x3, x2, x1, x0$ (representing\n", "a binary number) and $y3, y2, y1, y0$ (representing a binary number)\n", "and $s$ (to choose between sum and difference), and outputs $z4, z3, z2, z1, z0$\n", "where if $s = 1$, then $z4, z3, z2, z1, z0$ is the sum of the two input numbers,\n", "and if $s = 0$, then $z4, z3, z2, z1, z0$ is the difference of the two input\n", "numbers.\n", "In a programming language this is just an if statement, for example,\n", " in Racket we would have something like (if (= s 1) (add x y) (subtract x y)).\n", "In hardware, we have a different solution: compute both the sum and\n", "difference, in separate circuits, in parallel, and then use s to\n", "select which values will appear on the output wires $z4, z3, z2, z1, z0$.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

    \n", "We can think about this one bit at a time.\n", "A 1-bit selector circuit has inputs $s$, $x$, and $y$, and one output $z$,\n", "where $z$ is equal to $x$ if $s = 0$ and $z$ is equal to $y$ if $s = 1$.\n", "If we construct a truth table for this Boolean function, we\n", "get the following.\n", "

    \n",
        "   s  x  y  |  sel(s,x,y)\n",
        "   ----------------------\n",
        "   0  0  0  |    0\n",
        "   0  0  1  |    0\n",
        "   0  1  0  |    1\n",
        "   0  1  1  |    1\n",
        "   1  0  0  |    0\n",
        "   1  0  1  |    1\n",
        "   1  1  0  |    0\n",
        "   1  1  1  |    1\n",
        "
    " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that in the top half of the table (where $s = 0$), the output just\n", "copies $x$, and in the bottom half of the table (where $s = 1$), the output\n", "just copies $y$.\n", "We could write out an expression with four terms using the sum of products\n", "algorithm, but there is a simpler equivalent expression, namely\n", "\n", "$$z = (s' * x) + (s * y)$$\n", "\n", "This can be verified by constructing the truth table for this\n", "expression, or by noting that when $s = 0, s' = 1$, so we have\n", "$z = (1 * x) + (0 * y)$, which simplifies to $z = x$, and when $s = 1$,\n", "$s' = 0,$ so we have $z = (0 * x) + (1 * y)$, which simplifies to $z = y$.\n", "A circuit based on this expression takes time 3 gate delays.\n", "\n", "

    \n", "This one-bit selector can be generalized to any number of bits.\n", "That is, if we have a selector input s and two n-bit inputs \n", "$a1, a2, ..., an$, and $b1, b2, ..., bn$,\n", "and an n-bit output $c1, c2, ..., cn$, we can arrange that \n", "if $s = 0$ then $c1 = a1, c2 = a2, ..., cn = an$ and if $s = 1$ then\n", "$c1 = b1, c2 = b2, ..., cn = bn$, by using n one-bit selectors\n", "with the same s input. The first one-bit selector has $a1$ and $b1$\n", "as its other inputs, and $c1$ as its output. The second one-bit\n", "selector has $a2$ and $b2$ as its other inputs, and $c2$ as its output,\n", "and so on.\n", "Each of these one-bit selectors operates in parallel with the others,\n", "so after just 3 gate delays the n-bit output $c1, c2, ..., cn$ is\n", "available.\n", "Thus, with a 5-bit selector we can solve our original problem of\n", "selecting the sum or difference of two 4-bit inputs." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

    \n", "

    Sequential circuits.

    \n", " \n", "Up to now we have considered only combinational circuits, which have\n", "no loops of wires and gates.\n", "In a combinational circuit, the eventual final outputs of the\n", "circuit are completely determined by the values of the circuit\n", "inputs.\n", "However, the outputs of a sequential circuit may depend on both\n", "the inputs and the past values of the wires of the circuit.\n", "We'll consider several examples of sequential circuits.\n", "The first is a single NOT gate whose input is its own output,\n", "indicated in the following diagram.\n", "
    \n",
        "\n",
        "\n",
        "    ---|NOT>---*----- z\n",
        "    |          |\n",
        "    ------------\n",
        "\n",
        "
    " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here the wire $z$ is both the output of the NOT gate and its input.\n", "We examine the behavior of this circuit assuming that $z$ is initially $0$.\n", "This is the input to the NOT gate, which one gate delay later outputs $z = 1$.\n", "After one further gate delay later, the output of the NOT gate is $z = 0$.\n", "Thus, the value of $z$ alternates between $0$ and $1$ at intervals of one gate\n", "delay, indefinitely.\n", "Graphing the value of $z$ as a function of time (measured in gate delays):" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
    \n",
        "\n",
        "  1      ====    ====    ====    ====   \n",
        "\n",
        "  0  ====    ====    ====    ====\n",
        "     0   1   2   3   4   5   6   7\n",
        "\n",
        "
    " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The output value of this circuit never stabilizes, but nonetheless,\n", "it could be useful as a clock marking off time in units of one gate\n", "delay." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Another sequential circuit (\"Garden of Eden\").\n", " \n", "We now consider a sequential circuit consisting of an OR gate\n", "whose output is fed back as one of the inputs to the OR gate.\n", "A picture of this situation follows.\n", "
    \n",
        "\n",
        "   x ----|\n",
        "         |OR>----+---- z\n",
        "      ---|       |\n",
        "      |          |\n",
        "      ------------           \n",
        "\n",
        "
    \n", "This is an OR gate with inputs $x$ and $z$ and output $z$.\n", "Some assignments of values to the wires $x$ and $z$ are stable\n", "in the sense that they are not different after one gate delay.\n", "To find these values, we consider the equation:\n", "
    \n",
        "    z = x OR z\n",
        "
    " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "This has three solutions:\n", "
    \n",
        "    x = 0 and z = 0\n",
        "    x = 1 and z = 1\n",
        "    x = 0 and z = 1\n",
        "
    \n", "The combination $x = 1$ and $z = 0$ is not stable, because after\n", "one gate delay we have $x = 1$ and $z = 1$.\n", "If we consider the three solutions as states of the circuit,\n", "and see how changes in the input $x$ can transition from one\n", "state to another, we have the following diagram.\n", "
    \n",
        "(x = 0, z = 0) ------->  (x = 1, z = 1) -------> (x = 0, z = 1)\n",
        "                x = 1          ^         x = 0         |\n",
        "                               |                       |\n",
        "                               -------------------------\n",
        "                                         x = 1\n",
        "
    " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We see that the state $x = 0$ and $z = 0$ is a so-called \"Garden of Eden\" state,\n", "because once we leave it (by setting the input $x$ to $1$), we cannot re-enter\n", "it.\n", "From the state $x = 1$ and $z = 1$ we can reach the state $x = 0$ and $z = 1$\n", "by setting the input $x$ to $0$, and from the state $x = 0$ and $z = 1$ we can\n", "reach the state $x = 1$ and $z = 1$ by setting $x$ to $1$, but the fact that\n", "$z = 1$ in both states means that $z$ will continue to be $1$.\n", "This circuit exhibits a kind of memory, recording whether the input $x$\n", "has ever been set to $1$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

    \n", "

    The NAND latch.

    \n", "Please also see the diagrams:\n", "NAND latch and D flipflop.\n", "The third kind of sequential circuit that we consider provides\n", "a more useful kind of memory.\n", "It consists of two NAND gates with the output of each one fed\n", "back to be an input of the other.\n", "A diagram follows.\n", "
    \n",
        "\n",
        "    x  ----|\n",
        "           |NAND>----*----- q\n",
        "        ---|        /\n",
        "        |          / \n",
        "        ----------/---\n",
        "                 /   |\n",
        "        --------/    |\n",
        "        |            |\n",
        "        ---|         |\n",
        "           |NAND>----*----- u\n",
        "    y  ----|\n",
        "\n",
        "
    " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "The inputs to the circuit are $x$ and $y$, and the outputs are $q$ and $u$.\n", "The output $q$ is an input with $y$ to a NAND gate whose output is $u$,\n", "and the output $u$ is an input with $x$ to a NAND gate whose output is $q$.\n", "The stable states of this circuit are those assignments of $0$ and $1$\n", "to the wires $x$, $y$, $q$, and $u$ that satisfy the following equations.\n", "
    \n",
        "   q  =  x NAND u\n",
        "   u  =  y NAND q\n",
        "
    \n", "This set of equations has 5 solutions, as follows.\n", "
    \n",
        "(1)   x = 0, y = 0, q = 1, u = 1\n",
        "(2)   x = 1, y = 0, q = 0, u = 1\n",
        "(3)   x = 0, y = 1, q = 1, u = 0\n",
        "(4)   x = 1, y = 1, q = 1, u = 0\n",
        "(5)   x = 1, y = 1, q = 0, u = 1\n",
        "
    \n", "\n", "Note that the values of the output wires $q$ and $u$ are not\n", "determined by the values of the input wires (see (4) and (5)).\n", "If we avoid state (1), we can use the other four states to\n", "remember one bit of information.\n", "Consider how we can move from one of these four states\n", "to another by changing the value on one input wire, either\n", "$x$ or $y$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
    \n",
        "                        (y=0)\n",
        "                      <--------   \n",
        "    (x=1,y=0,q=0,u=1) ---------> (x=1,y=1,q=0,u=1)\n",
        "                     ^  (y=1)    /\n",
        "                      \\         /\n",
        "                       \\       /\n",
        "                        \\     /\n",
        "                         \\   /\n",
        "                          \\ /\n",
        "                           X\n",
        "                          / \\\n",
        "                  (x=0)  /   \\  (y=0)\n",
        "                        /     \\\n",
        "                       /       \\\n",
        "                      /         \\\n",
        "                     /           \\\n",
        "                    V   (x=0)     \\\n",
        "                      <--------\n",
        "    (x=0,y=1,q=1,u=0) ---------> (x=1,y=1,q=1,u=0)\n",
        "                        (x=1)\n",
        "
    \n", "In the states on the left, the inputs $(x=1,y=0)$ or $(x=0,y=1)$\n", "determine the outputs. Then when $y$ (or $x$) is set to $1$,\n", "there is a transition to the corresponding state on the\n", "top, where the inputs are $x=1$ and $y=1$, but the state on top\n", "indicates that the preceding left state was $(x=1,y=0)$\n", "while the state on the bottom indicates that the preceding\n", "left state was $(x=0,y=1)$.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

    \n", "The NAND latch is the heart of a D-flip flop, which\n", "we'll see next lecture.\n", "

    \n", "\n", "\n", "

    Gates and circuits IV.

    \n", "\n", "\n", "

    \n", "

    Summary:

    \n", "

    \n", "