{
 "metadata": {
  "name": "",
  "signature": "sha256:4819bbe8a5a2d640fb7430b44d6f07599ae9c25e7c65861336ca306e42b147ba"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example for SRLatch"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from __future__ import print_function\n",
      "from BinPy.Sequential.sequential import SRLatch\n",
      "from BinPy.tools.clock import Clock\n",
      "from BinPy.Gates import Connector\n",
      "from BinPy.tools.oscilloscope import Oscilloscope"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "s = Connector(1)\n",
      "r = Connector(0)\n",
      "\n",
      "p = Connector(0)\n",
      "q = Connector(1)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Initialize the clock\n",
      "clock = Clock(1, 4)\n",
      "clock.start()\n",
      "# A clock of 1 hertz frequency\n",
      "clk_conn = clock.A\n",
      "\n",
      "enable = Connector(1)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Initialize the sr latch\n",
      "srff = SRLatch(s, r, enable, clk_conn)\n",
      "\n",
      "# To connect outputs use s.setOutputs(op1,op2)\n",
      "srff.setOutputs(A=p, B=q)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Initialize the oscilloscope\n",
      "\n",
      "o = Oscilloscope((clk_conn, 'CLK'), (s, 'S'), (\n",
      "    r, 'R'), (p, 'OUT'), (q, 'OUT!'), (enable, 'ENABLE'))\n",
      "o.start()\n",
      "o.setScale(0.015)  # Set scale by trial and error.\n",
      "o.setWidth(100)\n",
      "o.unhold()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\u001b[0m\n",
        "\u001b[0m\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print (\"SET STATE - S = 1, R = 0\")\n",
      "# Set State\n",
      "s.state = 1\n",
      "r.state = 0\n",
      "# The same thing can also be done by --> srff.setInputs(s = 1, r = 0)\n",
      "while True:\n",
      "    if clk_conn.state == 0:\n",
      "        # Falling edge will trigger the FF\n",
      "        srff.trigger()\n",
      "        break\n",
      "print (srff.state())\n",
      "# Sending a positive edge to srff\n",
      "while True:\n",
      "    if clk_conn.state == 1:\n",
      "        # Falling edge will trigger the FF\n",
      "        srff.trigger()\n",
      "        break"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "SET STATE - S = 1, R = 0\n",
        "[1, 0]\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print (\"RESET STATE - S = 0, R = 1\")\n",
      "# Reset State\n",
      "s.state = 0\n",
      "r.state = 1\n",
      "# The same thing can also be done by --> srff.setInputs(s = 1, r = 0)\n",
      "while True:\n",
      "    if clk_conn.state == 0:\n",
      "        # Falling edge will trigger the FF\n",
      "        srff.trigger()\n",
      "        break\n",
      "# Displaying the output using the connector instances\n",
      "print (\"[\", p(), \",\", q(), \"]\")\n",
      "\n",
      "# Sending a positive edge to srff\n",
      "while True:\n",
      "    if clk_conn.state == 1:\n",
      "        # Falling edge will trigger the FF\n",
      "        srff.trigger()\n",
      "        break"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "RESET STATE - S = 0, R = 1\n",
        "["
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " 0 , 1 ]\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print (\"INVALID STATE - S = 1, R = 1\")\n",
      "# Invalid state\n",
      "s.state = 1\n",
      "r.state = 1\n",
      "# The same thing can also be done by --> srff.setInputs(s = 1, r = 1)\n",
      "while True:\n",
      "    if clk_conn.state == 0:\n",
      "        # Falling edge will trigger the FF\n",
      "        srff.trigger()\n",
      "        break\n",
      "print (srff.state())\n",
      "\n",
      "# Sending a positive edge to srff\n",
      "while True:\n",
      "    if clk_conn.state == 1:\n",
      "        # Falling edge will trigger the FF\n",
      "        srff.trigger()\n",
      "        break"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "INVALID STATE - S = 1, R = 1\n",
        "ERROR: Invalid State - Resetting the Latch"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "[0, 1]\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print (\"2nd INVALID STATE - S = 0, R = 0\")\n",
      "# Invalid state\n",
      "s.state = 1\n",
      "r.state = 1\n",
      "# The same thing can also be done by --> srff.setInputs(s = 1, r = 1)\n",
      "while True:\n",
      "    if clk_conn.state == 0:\n",
      "        # Falling edge will trigger the FF\n",
      "        srff.trigger()\n",
      "        break\n",
      "print (srff.state())\n",
      "\n",
      "# Sending a positive edge to srff\n",
      "while True:\n",
      "    if clk_conn.state == 1:\n",
      "        # Falling edge will trigger the FF\n",
      "        srff.trigger()\n",
      "        break"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "2nd INVALID STATE - S = 0, R = 0\n",
        "ERROR: Invalid State - Resetting the Latch"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "[0, 1]\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Display the oscilloscope     \n",
      "o.display()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\u001b[0m===================================================================================================================\n",
        "BinPy - Oscilloscope\n",
        "===================================================================================================================\n",
        "                                                                              SCALE - X-AXIS : 1 UNIT WIDTH = 0.015\n",
        "===================================================================================================================\n",
        "          \u2502\n",
        "          \u2502\n",
        "          \u2502                 \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510        \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510              \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510                 \n",
        "     CLK  \u2502                 \u2502            \u2502        \u2502             \u2502              \u2502              \u2502                 \n",
        "          \u2500 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518            \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518             \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518              \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n",
        "          \u2502\n",
        "          \u2502\n",
        "          \u2502\n",
        "          \u2502\n",
        "          \u2502 \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510                    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510             \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510                 \n",
        "       S  \u2502 \u2502                \u2502                    \u2502              \u2502             \u2502              \u2502                 \n",
        "          \u2500 \u2518                \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518              \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518              \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n",
        "          \u2502\n",
        "          \u2502\n",
        "          \u2502\n",
        "          \u2502\n",
        "          \u2502                  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510   \n",
        "       R  \u2502                  \u2502                                                                              \u2502   \n",
        "          \u2500 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518                                                                              \u2514\u2500\u2500\u2500\n",
        "          \u2502\n",
        "          \u2502\n",
        "          \u2502\n",
        "          \u2502\n",
        "          \u2502 \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510                                                                      \n",
        "     OUT  \u2502 \u2502                            \u2502                                                                      \n",
        "          \u2500 \u2518                            \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n",
        "          \u2502\n",
        "          \u2502\n",
        "          \u2502\n",
        "          \u2502\n",
        "          \u2502                              \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510   \n",
        "    OUT!  \u2502                              \u2502                                                                  \u2502   \n",
        "          \u2500 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518                                                                  \u2514\u2500\u2500\u2500\n",
        "          \u2502\n",
        "          \u2502\n",
        "          \u2502\n",
        "          \u2502\n",
        "          \u2502 \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510   \n",
        "   ENABL  \u2502 \u2502                                                                                               \u2502   \n",
        "          \u2500 \u2518                                                                                               \u2514\u2500\u2500\u2500\n",
        "          \u2502\n",
        "          \u2502\n",
        "\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\u2502\n",
        "\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n",
        "\u001b[0m\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Kill the clock and the oscilloscope threads after use\n",
      "o.kill()\n",
      "clock.kill()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 11
    }
   ],
   "metadata": {}
  }
 ]
}