{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 4 : Combinational And Sequential Logic Circuits"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example4_1,pg 488"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# find output and carry\n",
      "# it is a half-adder circuit with the output 'a' and carry 'c' given by the boolean equations\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "b1   = 1                       #input-1\n",
      "b2   = 1                       #input-2\n",
      "\n",
      "#Calculations\n",
      "a=(b1 and (not b2))+((not b1) and b2)  #sum\n",
      "c=(b1 and b2)                   #carry\n",
      "\n",
      "#Result\n",
      "print(\"sum:\")\n",
      "print(\"a = %d\\n\"%a)\n",
      "print(\"carry:\")\n",
      "print(\"c = %.f\"%c) "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "sum:\n",
        "a = 0\n",
        "\n",
        "carry:\n",
        "c = 1\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example4_2,pg 489"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# find difference and borrow\n",
      "#the circuit is that of a half subtractor\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "b1   = 1                       #input-1, case-1\n",
      "B1   = 0                       #input-2  case-1\n",
      "b2   = 1                       #input-1, case 2\n",
      "B2   = 1                       #input-2, case 2\n",
      "\n",
      "\n",
      "\n",
      "#Calculations\n",
      "#case-1\n",
      "d1=(b1 and (not B1))+(B1 and (not b1)) #difference\n",
      "r1=(b1 and (not B1))                    #borrow\n",
      "\n",
      "#case-2\n",
      "d2=(b2 and (not B2))+(B2 and (not b2))\n",
      "r2=(b2 and (not B2))\n",
      "\n",
      "#Result\n",
      "print(\"difference case-1:\")\n",
      "print(\"d1 = %d\"%d1)\n",
      "print(\"borrow case-1:\")\n",
      "print(\"r1 = %.f\\n\"%r1)\n",
      "print(\"difference case-2:\")\n",
      "print(\"d2 = %.f\"%d2)\n",
      "print(\"borrow case-2:\")\n",
      "print(\"r2 = %.f\\n\"%r2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "difference case-1:\n",
        "d1 = 1\n",
        "borrow case-1:\n",
        "r1 = 1\n",
        "\n",
        "difference case-2:\n",
        "d2 = 0\n",
        "borrow case-2:\n",
        "r2 = 0\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example4_3,pg 489"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# find final output\n",
      "\n",
      "import math \n",
      "#Variable declaration\n",
      "b=1                    #input-1\n",
      "B=0                    #input-2\n",
      "\n",
      "#Calculations\n",
      "y=(not((b or B)) or (b and B))\n",
      "\n",
      "#Result\n",
      "print(\"final output:\")\n",
      "print(\"y = %d\"%y)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "final output:\n",
        "y = 0\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example4_4_a,pg 489"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# decoder output\n",
      "\n",
      "import math\n",
      "#VAriable declaration\n",
      "#initial conditions\n",
      "b  = 0                # input \n",
      "Bi = 0                # initial value\n",
      "Bf = 1                # final value\n",
      "\n",
      "#initial state of outputs\n",
      "\n",
      "y1i=not(b or Bi)\n",
      "\n",
      "y2i=not(b or (not Bi))\n",
      "\n",
      "y3i=not(Bi or (not(b)))\n",
      "\n",
      "y4i=not((not(Bi)) or (not(b)))\n",
      "\n",
      "#final state of outputs\n",
      "\n",
      "y1f=not(b or Bf)\n",
      "\n",
      "y2f=not(Bf or (not(b)))\n",
      "\n",
      "y3f=not(b or (not(Bf)))\n",
      "\n",
      "y4f=not((not(Bf)) or (not(b)))\n",
      "\n",
      "print(\"first: \")\n",
      "print(\"y1 = %.f \"%y1i)\n",
      "print(\"y2 = %.f \"%y2i)\n",
      "print(\"y3 = %.f \"%y3i)\n",
      "print(\"y4 = %.f\\n\"%y4i)\n",
      "print(\"next: \")\n",
      "print(\"y1 = %.f \"%y1f)\n",
      "print(\"y2 = %.f \"%y2f)\n",
      "print(\"y3 = %.f \"%y3f)\n",
      "print(\"y4 = %.f\"%y4f)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "first: \n",
        "y1 = 1 \n",
        "y2 = 0 \n",
        "y3 = 0 \n",
        "y4 = 0\n",
        "\n",
        "next: \n",
        "y1 = 0 \n",
        "y2 = 0 \n",
        "y3 = 1 \n",
        "y4 = 0\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example4_4_b,pg 489"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# decoder output\n",
      "\n",
      "import math\n",
      "#VAriable declaration\n",
      "#initial conditions\n",
      "b  = 1                # input \n",
      "Bi = 0                # initial value\n",
      "Bf = 1                # final value\n",
      "\n",
      "#initial state of outputs\n",
      "\n",
      "y1i=not(b or Bi)\n",
      "\n",
      "y2i=not(Bi or (not(b)))\n",
      "\n",
      "y3i=not(b or (not Bi))\n",
      "\n",
      "y4i=not((not(Bi)) or (not(b)))\n",
      "\n",
      "#final state of outputs\n",
      "\n",
      "y1f=not(b or Bf)\n",
      "\n",
      "y2f=not(b or (not(Bf)))\n",
      "\n",
      "y3f=not(Bf or (not(b)))\n",
      "\n",
      "y4f=not((not(Bf)) or (not(b)))\n",
      "\n",
      "print(\"first: \")\n",
      "print(\"y1=%.f \"%y1i)\n",
      "print(\"y2=%.f \"%y2i)\n",
      "print(\"y3=%.f \"%y3i)\n",
      "print(\"y4=%.f\\n\"%y4i)\n",
      "print(\"next: \")\n",
      "print(\"y1=%.f \"%y1f)\n",
      "print(\"y2=%.f \"%y2f)\n",
      "print(\"y3=%.f \"%y3f)\n",
      "print(\"y4=%.f\"%y4f)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "first: \n",
        "y1=0 \n",
        "y2=1 \n",
        "y3=0 \n",
        "y4=0\n",
        "\n",
        "next: \n",
        "y1=0 \n",
        "y2=0 \n",
        "y3=0 \n",
        "y4=1\n"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example4_5,pg 489"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# convert 8421 to 2421 code\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "#if A8,B8,C8,D8 is the binary in 8421 code, for 12 this would be 1100(DCBA)\n",
      "#in 8421-code\n",
      "A8 = 0\n",
      "B8 = 0\n",
      "C8 = 1\n",
      "D8 = 1\n",
      "\n",
      "#Calculations\n",
      "#in 2421-code\n",
      "D2 = D8\n",
      "C2 =(C8 or D8)\n",
      "B2 =(B8 or D8)\n",
      "A2=A8\n",
      "\n",
      "#Result\n",
      "print(\"2421-code for 12(1100) is:\")\n",
      "print(\"%d%d%d%d \"%(D2,C2,B2,A2))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "2421-code for 12(1100) is:\n",
        "1110 \n"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example4_6,pg 490"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Xcess 3 code\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "add = \"0011\"                 #binary-3 to be added\n",
      "x   = \"0010\"                 #binary-2\n",
      "y   = \"0100\"                 #binary-4\n",
      "z   = \"0111\"                 #binary-7\n",
      "\n",
      "#Calculations\n",
      "x   = int(x,2)\n",
      "add = int(add,2)\n",
      "XS31=x+add\n",
      "XS31=bin(XS31)[2:]\n",
      "y=int(y,2)\n",
      "XS32=y+add\n",
      "XS32=bin(XS32)[2:]\n",
      "z=int(z,2)\n",
      "XS33=z+add\n",
      "XS33=bin(XS33)[2:]\n",
      "\n",
      "#Result\n",
      "print(\"XS-3 for 2: %s\"%XS31)\n",
      "print(\"XS-3 for 4: %s\"%XS32)\n",
      "print(\"XS-3 for 7: %s\"%XS33)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "XS-3 for 2: 101\n",
        "XS-3 for 4: 111\n",
        "XS-3 for 7: 1010\n"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example4_7,pg 490"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# 8 to 1 MUX\n",
      "\n",
      "# one can see from relations of AND gate outputs in terms of address bits and input bit \n",
      "# that there are possibilities depending on which input is low\n",
      "# if I7=0, by making all address bits s1,s2,s3 as 1 one can have the conditions satisfied\n",
      "\n",
      "#Result\n",
      "print(\"This requires all the outputs from AND gates should be low\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "This requires all the outputs from AND gates should be low\n"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example4_8,pg 490"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# truth table of RS flip flop\n",
      "\n",
      "import math\n",
      "#Variable declaration\n",
      "#enter binary 1-bit values only\n",
      "\n",
      "S  = input(\"Enter value of S\\n\")\n",
      "R  = input(\"Enter value of R\\n\")\n",
      "Qn = input(\"Enter previous value of Q\\n\")\n",
      "En = input(\"Enter enable value\\n\")\n",
      "print(\"RS flip-flop truth table:\")\n",
      "if (En==0):\n",
      "    op=Qn\n",
      "    print(\"op = %d\"%op)\n",
      "elif(( S==0) and (R==0)): \n",
      "    op=Qn\n",
      "    print(\"op = %d\"%op)\n",
      "elif ((S==0) and (R==1)): \n",
      "     op=0\n",
      "     print(\"op = %d\"%op)\n",
      "elif((S==1) and (R==0)): \n",
      "     op=1\n",
      "     print(\"op = %d\"%op)\n",
      "elif ((S==1) and (R==1)): \n",
      "     print(\"output not determinable\")\n",
      "\n",
      "print(\"the relations are:\")\n",
      "print(\"Qn=(R+Qn*)*\")              #Q* = not(Q)\n",
      "print(\"Qn*=(S+Qn)*\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": "*"
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example4_9,pg 491"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# JK fiip flop\n",
      "\n",
      "# Q=(Q*+Q.K)* and Q*=(Q+Q*.J)*\n",
      "# with J=K=0 Q=Q and Q*=Q*\n",
      "# Q* is not(Q)\n",
      "\n",
      "#Result\n",
      "print(\"operational equations:\\n\")\n",
      "print(\"Q=(Q*+Q.K)* and Q*=(Q+Q*.J)*\\n\")\n",
      "print(\"holds good where Q and Q* should be given appropriate values\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "operational equations:\n",
        "\n",
        "Q=(Q*+Q.K)* and Q*=(Q+Q*.J)*\n",
        "\n",
        "holds good where Q and Q* should be given appropriate values\n"
       ]
      }
     ],
     "prompt_number": 26
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example4_10,pg 491"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# 3 bit binary counter\n",
      "\n",
      "print(\"It remains positive from the falling edge of pulse-2, then falling edge of 4th, 6th and 8th pulses and so on....\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "It remains positive from the falling edge of pulse-2, then falling edge of 4th, 6th and 8th pulses and so on....\n"
       ]
      }
     ],
     "prompt_number": 27
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example4_11,pg 491"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# 6 modulo counter\n",
      "\n",
      "print(\"All modulo counters are basically scalars.A 6-modulo counter is a 6-scaler\")\n",
      "print(\"so that after 6-input pulses the content of counter becomes 000(reset)\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "All modulo counters are basically scalars.A 6-modulo counter is a 6-scaler\n",
        "so that after 6-input pulses the content of counter becomes 000(reset)\n"
       ]
      }
     ],
     "prompt_number": 30
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example4_12,pg 491"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# 3 bit 5 modulo counter \n",
      "\n",
      "print(\"Normal count would be 2^3=8, while 5-modulo counter would limit it to 5, so that illegitimate states are 8-5=3\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Normal count would be 2^3=8, while 5-modulo counter would limit it to 5, so that illegitimate states are 8-5=3\n"
       ]
      }
     ],
     "prompt_number": 32
    }
   ],
   "metadata": {}
  }
 ]
}