{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Boolean Generator\n",
    "This notebook will show how to use the boolean generator to generate a boolean combinational function. The function that is implemented is a 2-input XOR."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 1: Download the `logictools` overlay"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pynq.overlays.logictools import LogicToolsOverlay\n",
    "\n",
    "logictools_olay = LogicToolsOverlay('logictools.bit')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 2: Specify the boolean function of a 2-input XOR \n",
    "The logic is applied to the on-board pushbuttons and LED, pushbuttons **PB0** and **PB3** are set as inputs and LED **LD2** is set as an output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "function = ['LD2 = PB3 ^ PB0']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 3: Instantiate and setup of the boolean generator object. \n",
    "The logic function defined in the previous step is setup using the `setup()` method "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "boolean_generator = logictools_olay.boolean_generator\n",
    "boolean_generator.setup(function)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "__Find the On-board pushbuttons and LEDs__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](images/boolean_generator.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 4: Run the boolean generator verify operation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "boolean_generator.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Verify the operation of the XOR function\n",
    "\n",
    "| PB0 | PB3 | LD2 |\n",
    "|:---:|:---:|:---:|\n",
    "|  0  |  0  |  0  |\n",
    "|  0  |  1  |  1  |\n",
    "|  1  |  0  |  1  |\n",
    "|  1  |  1  |  0  |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 5: Stop the boolean generator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "boolean_generator.stop()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 6: Re-run the entire boolean function generation in a single cell\n",
    "**Note**: The boolean expression format can be `list` or `dict`. We had used a `list` in the example above. We will now use a `dict`.  \n",
    "<font color=\"DodgerBlue\">**Alternative format:**</font> \n",
    "```python\n",
    "function = {'XOR_gate': 'LD2 = PB3 ^ PB0'}\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pynq.overlays.logictools import LogicToolsOverlay\n",
    "\n",
    "logictools_olay = LogicToolsOverlay('logictools.bit')\n",
    "boolean_generator = logictools_olay.boolean_generator\n",
    "\n",
    "function = {'XOR_gate': 'LD2 = PB3 ^ PB0'}\n",
    "\n",
    "boolean_generator.setup(function)\n",
    "boolean_generator.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "__Stop the boolean generator__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "boolean_generator.stop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 1,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
