CS52 - Spring 2016 - Class 13

Example code in this lecture

   assign5-starter.py
   simple_circuit.py
   nand4.py
   2to1mux.py

Lecture notes

  • admin
       - assignment 5 out
          - should be able to finish it after today's class


  • look at assign5-starter.py again
       - two main classes
          - Node represents wires
             - keep track of their state (0 or 1)
             - keep track of what gates the go in to
          - Gate represents gates
             - keep track of what nodes are input
             - keep track of the output node
       - gates are constructed by:
          1) constructing the input *and* output nodes for the gate/circuit
          2) calling the constructor for the gate

       - computation then happens by:
          1) calling set_state on the input nodes (don't forget to save these when you construct the gate!)
          2) the set_state then calls "notify" of any gates that node is connected to
          3) notify calls evaluate to figure out what the value of the gate should be
          4) if the gate value has changed, then set_state is called on the output node (and the process continues)

  • flexible input gates vs. fixed input gates
       - The normal gate class takes as input a *list* of nodes for the input
       - It is also convenient sometimes to have gates that take a fixed set of input nodes (e.g. one or two input gates since these are very common)
       - The Gate1 and Gate2 classes define constructors that do just this!

  • constructing your own gates
       - most of the work is already done for you
       - to construct a generic multi-input gate
          1) create a new class that inherits from the Gate class
          2) define the evaluate method (to override the default Gate evaluate method)
          
          That's it!

       - look at the AndGate class in assign5-starter.py code

       - Defining fixed input types is easy once you have the generic version
          1) create a new class that inherits from the gate size you want (e.g. Gate2) and Gate
          2) for the body of the class, just put pass
             - we don't actually need any code in the body
             - pass just tells python that we realize this, but python doesn't allow for empty bodies syntactically

  • look at the simple_circuit.py code

  • look at the nand4.py code

  • what does the following circuit do (there's a better picture in the slides posted for today)?

       s ----- not --- AND1
       in0 -|--------- AND1 ---- OR
        ------ AND2 -------- OR --- OUT
       in1 ------- AND2

       (or written in boolean logic: (!s && in0) || (s && in1) )

       - This is called a multiplexer (in particular, a 2-to-1 multiplexer)
          - The s bit the select or control bit
             - if s is 0, then whatever value is in in0 is output
             - if s is 1, then whatever value is in in1 is output
          - the truth table is:

          in0 in1 s | out
          0 0 0 | 0
          0 0 1 | 0
        0 1 0 | 0
          0 1   1 | 1
          1 0 0 | 1
          1 0 1 | 0
          1 1 0 | 1
          1 1 1 | 1



  • look at 2to1mux.py code
       - requires that assign5-starter.py and and OrGate2 are defined