{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "automatic-budget",
   "metadata": {},
   "source": [
    "<link rel=\"preconnect\" href=\"https://fonts.gstatic.com\">\n",
    "<link href=\"https://fonts.googleapis.com/css2?family=Fira+Code&display=swap\" rel=\"stylesheet\">\n",
    "\n",
    "### License\n",
    "\n",
    "<p style=\"font-family: 'Fira Code', monospace; font-size: 1.2rem\">\n",
    "Copyright 2021 Xilinx, Inc.<br><br>\n",
    "Licensed under the Apache License, Version 2.0 (the \"License\");<br>\n",
    "you may not use this file except in compliance with the License.<br><br>\n",
    "You may obtain a copy of the License at <a href=\"http://www.apache.org/licenses/LICENSE-2.0\"?>http://www.apache.org/licenses/LICENSE-2.0</a><br><br>\n",
    "Unless required by applicable law or agreed to in writing, software<br>\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,<br>\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.<br>\n",
    "See the License for the specific language governing permissions and<br>\n",
    "limitations under the License.<br>\n",
    "</p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "maritime-shock",
   "metadata": {},
   "source": [
    "# ChipScoPy ILA and VIO Example\n",
    "\n",
    "\n",
    "<img src=\"../img/api_overview.png\" width=\"500\" align=\"left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "solid-rebel",
   "metadata": {},
   "source": [
    "## Description\n",
    "This example demonstrates how to program and communicate with ILA (Integrated Logic Analyzer) and\n",
    "VIO (Virtual IO) cores using the ChipScoPy Python API.\n",
    "\n",
    "\n",
    "## Requirements\n",
    "- Local or remote Xilinx Versal board, such as a VCK190\n",
    "- Xilinx hw_server 2022.1 installed and running\n",
    "- Xilinx cs_server 2022.1 installed and running\n",
    "- Python 3.8 or greater installed\n",
    "- ChipScoPy 2022.1 installed\n",
    "- Jupyter notebook support installed - Please do so, using the command `pip install chipscopy[jupyter]`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "noted-characterization",
   "metadata": {},
   "source": [
    "## 1 - Initialization: Imports and File Paths\n",
    "\n",
    "After this step,\n",
    "- Required functions and classes are imported\n",
    "- URL paths are set correctly\n",
    "- File paths to example files are set correctly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "amino-plaintiff",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "from chipscopy import get_design_files\n",
    "from chipscopy import create_session, report_versions\n",
    "from chipscopy.api.ila import get_waveform_data, export_waveform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "indonesian-relations",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HW_URL: TCP:localhost:3121\n",
      "CS_URL: TCP:localhost:3042\n",
      "PROGRAMMING_FILE: C:\\wrk\\chipscopy\\chipscopy-venv\\Lib\\site-packages\\chipscopy\\examples\\designs\\vck190\\production\\chipscopy_ced\\chipscopy_wrapper.pdi\n",
      "PROBES_FILE:C:\\wrk\\chipscopy\\chipscopy-venv\\Lib\\site-packages\\chipscopy\\examples\\designs\\vck190\\production\\chipscopy_ced\\chipscopy_wrapper.ltx\n"
     ]
    }
   ],
   "source": [
    "# Make sure to start the hw_server and cs_server prior to running.\n",
    "# Specify locations of the running hw_server and cs_server below.\n",
    "# The default is localhost - but can be other locations on the network.\n",
    "CS_URL = os.getenv(\"CS_SERVER_URL\", \"TCP:localhost:3042\")\n",
    "HW_URL = os.getenv(\"HW_SERVER_URL\", \"TCP:localhost:3121\")\n",
    "\n",
    "# The get_design_files() function tries to find the PDI and LTX files. In non-standard\n",
    "# configurations, you can put the path for PROGRAMMING_FILE and PROBES_FILE below.\n",
    "design_files = get_design_files(\"vck190/production/chipscopy_ced\")\n",
    "\n",
    "PROGRAMMING_FILE = design_files.programming_file\n",
    "PROBES_FILE = design_files.probes_file\n",
    "\n",
    "print(f\"HW_URL: {HW_URL}\")\n",
    "print(f\"CS_URL: {CS_URL}\")\n",
    "print(f\"PROGRAMMING_FILE: {PROGRAMMING_FILE}\")\n",
    "print(f\"PROBES_FILE:{PROBES_FILE}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "norwegian-wagner",
   "metadata": {},
   "source": [
    "## 2 - Create a session and connect to the hw_server and cs_server\n",
    "\n",
    "The session is a container that keeps track of devices and debug cores.\n",
    "After this step,\n",
    "- Session is initialized and connected to server(s)\n",
    "- Versions are detected and reported to stdout"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "front-conflict",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "<rich.jupyter.JupyterRenderable at 0x18ec63cdcd0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-style: italic\">                         ChipScoPy Version Information                          </span>\n",
       "┌────────────────────────────┬─────────────────────────────────────────────────┐\n",
       "│<span style=\"font-weight: bold\">                     Entity </span>│<span style=\"font-weight: bold\"> Version                                         </span>│\n",
       "├────────────────────────────┼─────────────────────────────────────────────────┤\n",
       "│                  ChipScoPy │ ┌───────────┬──────────────────────┐            │\n",
       "│                            │ │<span style=\"font-weight: bold\"> Attribute </span>│<span style=\"font-weight: bold\"> Value                </span>│            │\n",
       "│                            │ ├───────────┼──────────────────────┤            │\n",
       "│                            │ │     Build │ 2022.1.1654231490    │            │\n",
       "│                            │ │ Timestamp │ Jun 02 2022-22:44:50 │            │\n",
       "│                            │ └───────────┴──────────────────────┘            │\n",
       "│                            │                                                 │\n",
       "│ hw_server @ localhost:3121 │ ┌───────────┬─────────────────────────────────┐ │\n",
       "│                            │ │<span style=\"font-weight: bold\"> Attribute </span>│<span style=\"font-weight: bold\"> Value                           </span>│ │\n",
       "│                            │ ├───────────┼─────────────────────────────────┤ │\n",
       "│                            │ │    Status │ Connected                       │ │\n",
       "│                            │ │     Build │ 1641456835                      │ │\n",
       "│                            │ │   Version │ 2022.1                          │ │\n",
       "│                            │ │ Timestamp │ Thu 06 Jan 2022 08:13:55 AM UTC │ │\n",
       "│                            │ └───────────┴─────────────────────────────────┘ │\n",
       "│                            │                                                 │\n",
       "│ cs_server @ localhost:3042 │ ┌───────────────┬──────────────────────┐        │\n",
       "│                            │ │<span style=\"font-weight: bold\">     Attribute </span>│<span style=\"font-weight: bold\"> Value                </span>│        │\n",
       "│                            │ ├───────────────┼──────────────────────┤        │\n",
       "│                            │ │        Status │ Connected            │        │\n",
       "│                            │ │         Build │ 1649790728           │        │\n",
       "│                            │ │       Version │ 2022.1.0             │        │\n",
       "│                            │ │     Timestamp │ Apr 12 2022-13:12:08 │        │\n",
       "│                            │ │       Package │ 2022.1.1649790728    │        │\n",
       "│                            │ │ Artifact type │ pyinstaller          │        │\n",
       "│                            │ └───────────────┴──────────────────────┘        │\n",
       "└────────────────────────────┴─────────────────────────────────────────────────┘\n",
       "</pre>\n"
      ],
      "text/plain": [
       "<rich.jupyter.JupyterRenderable at 0x18ec627d0d0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "<rich.jupyter.JupyterRenderable at 0x18ec627d0d0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "session = create_session(cs_server_url=CS_URL, hw_server_url=HW_URL)\n",
    "report_versions(session)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "after-region",
   "metadata": {},
   "source": [
    "## 3 - Program the device with the example design\n",
    "\n",
    "After this step,\n",
    "- Device is programmed with the example programming file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "chemical-hands",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">\n",
       "<span style=\"font-weight: bold\">--&gt; INFO: </span>Programming device with: C:\\wrk\\chipscopy\\chipscopy-venv\\Lib\\site-packages\\chipscop\n",
       "y\\examples\\designs\\vck190\\production\\chipscopy_ced\\chipscopy_wrapper.pdi\n",
       "    \n",
       "</pre>\n"
      ],
      "text/plain": [
       "<rich.jupyter.JupyterRenderable at 0x18ec644cb50>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2de37ac03d6648bca17c75724feaf3de",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Typical case - one device on the board - get it.\n",
    "device = session.devices.filter_by(family=\"versal\").get()\n",
    "device.program(PROGRAMMING_FILE)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "mineral-antarctica",
   "metadata": {},
   "source": [
    "## 4 - Discover Debug Cores\n",
    "\n",
    "Debug core discovery initializes the chipscope server debug cores. This brings debug cores in the chipscope server online.\n",
    "\n",
    "After this step,\n",
    "\n",
    "- The cs_server is initialized and ready for use\n",
    "- ILA and VIO core instances in the device are reported"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "sapphire-constraint",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Debug cores setup and ready for use.\n"
     ]
    }
   ],
   "source": [
    "device.discover_and_setup_cores(ltx_file=PROBES_FILE)\n",
    "print(f\"Debug cores setup and ready for use.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "careful-wagon",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 - D62FB110F1A25D439EB15D9149F450CE   chipscopy_i/counters/ila_fast_counter_0\n",
      "1 - 9125D1429AD25223AECDD656C2856755   chipscopy_i/counters/ila_slow_counter_0\n",
      "2 - C53C0D3572B655B6A8A13A86686D262D   chipscopy_i/noc_tg_bc/noc_bc_axis_ila_0\n"
     ]
    }
   ],
   "source": [
    "# Print out the ILA core instance UUIDs and instance names\n",
    "ila_cores = device.ila_cores\n",
    "for index, ila_core in enumerate(ila_cores):\n",
    "    print(f\"{index} - {ila_core.core_info.uuid}   {ila_core.name}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "falling-gamma",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 - 0E76DEFA789D564080CFA5332A88DB12   chipscopy_i/noc_tg_bc/noc_tg/inst/u_top_axi_mst/u_tg_top/gen_aximm_reg_space.u_reg_space/gen_vio_status_monitor.u_vio_status_monitor\n",
      "1 - 718A03DBCD265D969EA5C15840AEFD17   chipscopy_i/counters/vio_fast_counter_0\n",
      "2 - 22BC7ED6C4145AAB88C58EAEDFF2B04C   chipscopy_i/counters/vio_slow_counter_0\n",
      "3 - 021098F58FE659EFAD76657E635E53FE   chipscopy_i/noc_tg_bc/axis_vio_0\n"
     ]
    }
   ],
   "source": [
    "# Print out the VIO core instance UUIDs and instance names\n",
    "vio_cores = device.vio_cores\n",
    "for index, vio_core in enumerate(vio_cores):\n",
    "    print(f\"{index} - {vio_core.core_info.uuid}   {vio_core.name}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "political-institute",
   "metadata": {},
   "source": [
    "## 5 - VIO Control and ILA Capture\n",
    "\n",
    "ILA and VIO are two important building blocks for debugging applications in hardware.\n",
    "This example design design shows how to control IP using a VIO core and capture results with ILA.\n",
    "\n",
    "In this Design,\n",
    "- A VIO core controls the counter (reset, up/down, ce, load)\n",
    "- An ILA core captures the counter values\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "outside-cause",
   "metadata": {},
   "source": [
    "<img src=\"img/capture_data.png\" width=\"400\" align=\"left\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "killing-carbon",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using ILA: 9125D1429AD25223AECDD656C2856755  chipscopy_i/counters/ila_slow_counter_0\n",
      "Using VIO: 22BC7ED6C4145AAB88C58EAEDFF2B04C  chipscopy_i/counters/vio_slow_counter_0\n"
     ]
    }
   ],
   "source": [
    "# Grab the two cores we are interested in for the demonstration\n",
    "# As shown above, a counter is connected to the ILA core.\n",
    "# The VIO core controls the counter.\n",
    "\n",
    "ila = device.ila_cores.get(name=\"chipscopy_i/counters/ila_slow_counter_0\")\n",
    "vio = device.vio_cores.get(name=\"chipscopy_i/counters/vio_slow_counter_0\")\n",
    "\n",
    "print(f\"Using ILA: {ila.core_info.uuid}  {ila.name}\")\n",
    "print(f\"Using VIO: {vio.core_info.uuid}  {vio.name}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "virtual-jumping",
   "metadata": {},
   "source": [
    "### 5a - Configure the counter using VIO output probes\n",
    "\n",
    "<img src=\"img/vio_control_counter.png\" width=\"300\" align=\"left\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "surrounded-plenty",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "VIO Port <---> Probe mapping:\n",
      "probe_in0 <-- chipscopy_i/counters/slow_counter_0_Q\n",
      "probe_out0 --> chipscopy_i/counters/slow_counter_0_CE\n",
      "probe_out1 --> chipscopy_i/counters/slow_counter_0_SCLR\n",
      "probe_out2 --> chipscopy_i/counters/slow_counter_0_UP\n",
      "probe_out3 --> chipscopy_i/counters/slow_counter_0_LOAD\n",
      "probe_out4 --> chipscopy_i/counters/slow_counter_0_L\n"
     ]
    }
   ],
   "source": [
    "# Print all the VIO port and probe names. This is convenient to know which probes are connected to\n",
    "# VIO ports. Also verifies probe names to pass to other functions.\n",
    "\n",
    "print(\"VIO Port <---> Probe mapping:\")\n",
    "for probe in vio.probes:\n",
    "    if probe.direction == \"in\":\n",
    "        print(f\"{probe.port_name} <-- {probe.probe_name}\")\n",
    "    else:\n",
    "        print(f\"{probe.port_name} --> {probe.probe_name}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "industrial-museum",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Counter is now free-running and counting up\n"
     ]
    }
   ],
   "source": [
    "# Set up the VIO core to enable counting up from 0\n",
    "#\n",
    "vio.reset_vio()\n",
    "vio.write_probes(\n",
    "    {\n",
    "        \"chipscopy_i/counters/slow_counter_0_SCLR\": 0,\n",
    "        \"chipscopy_i/counters/slow_counter_0_L\": 0x00000000,\n",
    "        \"chipscopy_i/counters/slow_counter_0_LOAD\": 0,\n",
    "        \"chipscopy_i/counters/slow_counter_0_UP\": 1,\n",
    "        \"chipscopy_i/counters/slow_counter_0_CE\": 1,\n",
    "    }\n",
    ")\n",
    "print(\"Counter is now free-running and counting up\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "technical-series",
   "metadata": {},
   "source": [
    "### 5b - Capture and display free-running counter using the ILA core\n",
    "\n",
    "<img src=\"img/free_running_counter.png\" width=\"350\" align=\"left\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "hispanic-tribe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ILA is running - looking for trigger\n"
     ]
    }
   ],
   "source": [
    "# Trigger ILA on the free running counter. Trigger set to the first time we see 0s in low 16-bits.\n",
    "# This will show the counter is free running, and counting up\n",
    "\n",
    "ila.reset_probes()\n",
    "ila.set_probe_trigger_value(\"chipscopy_i/counters/slow_counter_0_Q_1\", [\"==\", \"0xXXXX_0000\"])\n",
    "ila.run_basic_trigger(window_count=1, window_size=32, trigger_position=16)\n",
    "print(\"ILA is running - looking for trigger\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "empty-member",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Window:0  Window Sample:0   350224368  0x14DFFFF0 \n",
      "Window:0  Window Sample:1   350224369  0x14DFFFF1 \n",
      "Window:0  Window Sample:2   350224370  0x14DFFFF2 \n",
      "Window:0  Window Sample:3   350224371  0x14DFFFF3 \n",
      "Window:0  Window Sample:4   350224372  0x14DFFFF4 \n",
      "Window:0  Window Sample:5   350224373  0x14DFFFF5 \n",
      "Window:0  Window Sample:6   350224374  0x14DFFFF6 \n",
      "Window:0  Window Sample:7   350224375  0x14DFFFF7 \n",
      "Window:0  Window Sample:8   350224376  0x14DFFFF8 \n",
      "Window:0  Window Sample:9   350224377  0x14DFFFF9 \n",
      "Window:0  Window Sample:10   350224378  0x14DFFFFA \n",
      "Window:0  Window Sample:11   350224379  0x14DFFFFB \n",
      "Window:0  Window Sample:12   350224380  0x14DFFFFC \n",
      "Window:0  Window Sample:13   350224381  0x14DFFFFD \n",
      "Window:0  Window Sample:14   350224382  0x14DFFFFE \n",
      "Window:0  Window Sample:15   350224383  0x14DFFFFF \n",
      "Window:0  Window Sample:16   350224384  0x14E00000 <-- Trigger\n",
      "Window:0  Window Sample:17   350224385  0x14E00001 \n",
      "Window:0  Window Sample:18   350224386  0x14E00002 \n",
      "Window:0  Window Sample:19   350224387  0x14E00003 \n",
      "Window:0  Window Sample:20   350224388  0x14E00004 \n",
      "Window:0  Window Sample:21   350224389  0x14E00005 \n",
      "Window:0  Window Sample:22   350224390  0x14E00006 \n",
      "Window:0  Window Sample:23   350224391  0x14E00007 \n",
      "Window:0  Window Sample:24   350224392  0x14E00008 \n",
      "Window:0  Window Sample:25   350224393  0x14E00009 \n",
      "Window:0  Window Sample:26   350224394  0x14E0000A \n",
      "Window:0  Window Sample:27   350224395  0x14E0000B \n",
      "Window:0  Window Sample:28   350224396  0x14E0000C \n",
      "Window:0  Window Sample:29   350224397  0x14E0000D \n",
      "Window:0  Window Sample:30   350224398  0x14E0000E \n",
      "Window:0  Window Sample:31   350224399  0x14E0000F \n"
     ]
    }
   ],
   "source": [
    "# Wait for the ILA trigger with upload.\n",
    "# Then print the captured ILA samples and mark the trigger position.\n",
    "\n",
    "ila.wait_till_done(max_wait_minutes=0.1)\n",
    "upload_successful = ila.upload()\n",
    "if upload_successful:\n",
    "    samples = get_waveform_data(\n",
    "        ila.waveform,\n",
    "        [\"chipscopy_i/counters/slow_counter_0_Q_1\"],\n",
    "        include_trigger=True,\n",
    "        include_sample_info=True,\n",
    "    )\n",
    "    for trigger, sample_index, window_index, window_sample_index, value in zip(*samples.values()):\n",
    "        trigger = \"<-- Trigger\" if trigger else \"\"\n",
    "        print(\n",
    "            f\"Window:{window_index}  Window Sample:{window_sample_index}  {value:10}  0x{value:08X} {trigger}\"\n",
    "        )\n",
    "else:\n",
    "    print(\"Failed to upload ILA data from core\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "logical-marine",
   "metadata": {},
   "source": [
    "### 5c - Trigger ILA using VIO Up/Down virtual switch\n",
    "\n",
    "This step demonstrates how VIO and ILA can be combined to form powerful debug building blocks.\n",
    "\n",
    "ILA is set to trigger when UP/DOWN counter signal edge rises or falls.\n",
    "VIO drives the UP/DOWN counter control signal to 0 causing the counter to count down.\n",
    "The signal transition causes ILA to trigger and capture data.\n",
    "\n",
    "After this step,\n",
    "- VIO drives counter to count from UP to DOWN\n",
    "- ILA triggers on the UP to DOWN signal transition\n",
    "- Waveform uploaded with the up/down trigger sample in the center of captured data\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "demonstrated-updating",
   "metadata": {},
   "source": [
    "<img src=\"img/edge_trigger.png\" width=\"550\" align=\"left\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "nonprofit-quantity",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ILA is running - looking for trigger\n"
     ]
    }
   ],
   "source": [
    "# Set ILA core to capture on a transition of the UP/DOWN toggle switch\n",
    "# Once transition happens, trigger in the middle of the buffer.\n",
    "\n",
    "ila.reset_probes()\n",
    "ila.set_probe_trigger_value(\"chipscopy_i/counters/slow_counter_0_UP_1\", [\"==\", \"B\"])\n",
    "ila.run_basic_trigger(window_count=1, window_size=32, trigger_position=16)\n",
    "\n",
    "print(\"ILA is running - looking for trigger\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "streaming-dealing",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "VIO changed up/down counter to count down\n"
     ]
    }
   ],
   "source": [
    "# VIO: Turn counter up/down switch to DOWN position.\n",
    "# This will cause the running ILA to trigger on the transition edge from up to down.\n",
    "\n",
    "vio.write_probes({\"chipscopy_i/counters/slow_counter_0_UP\": 0})\n",
    "\n",
    "print(\"VIO changed up/down counter to count down\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "heavy-meeting",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Window:0  Window Sample:0   396069065  0x179B88C9 \n",
      "Window:0  Window Sample:1   396069066  0x179B88CA \n",
      "Window:0  Window Sample:2   396069067  0x179B88CB \n",
      "Window:0  Window Sample:3   396069068  0x179B88CC \n",
      "Window:0  Window Sample:4   396069069  0x179B88CD \n",
      "Window:0  Window Sample:5   396069070  0x179B88CE \n",
      "Window:0  Window Sample:6   396069071  0x179B88CF \n",
      "Window:0  Window Sample:7   396069072  0x179B88D0 \n",
      "Window:0  Window Sample:8   396069073  0x179B88D1 \n",
      "Window:0  Window Sample:9   396069074  0x179B88D2 \n",
      "Window:0  Window Sample:10   396069075  0x179B88D3 \n",
      "Window:0  Window Sample:11   396069076  0x179B88D4 \n",
      "Window:0  Window Sample:12   396069077  0x179B88D5 \n",
      "Window:0  Window Sample:13   396069078  0x179B88D6 \n",
      "Window:0  Window Sample:14   396069079  0x179B88D7 \n",
      "Window:0  Window Sample:15   396069080  0x179B88D8 \n",
      "Window:0  Window Sample:16   396069081  0x179B88D9 <-- Trigger\n",
      "Window:0  Window Sample:17   396069080  0x179B88D8 \n",
      "Window:0  Window Sample:18   396069079  0x179B88D7 \n",
      "Window:0  Window Sample:19   396069078  0x179B88D6 \n",
      "Window:0  Window Sample:20   396069077  0x179B88D5 \n",
      "Window:0  Window Sample:21   396069076  0x179B88D4 \n",
      "Window:0  Window Sample:22   396069075  0x179B88D3 \n",
      "Window:0  Window Sample:23   396069074  0x179B88D2 \n",
      "Window:0  Window Sample:24   396069073  0x179B88D1 \n",
      "Window:0  Window Sample:25   396069072  0x179B88D0 \n",
      "Window:0  Window Sample:26   396069071  0x179B88CF \n",
      "Window:0  Window Sample:27   396069070  0x179B88CE \n",
      "Window:0  Window Sample:28   396069069  0x179B88CD \n",
      "Window:0  Window Sample:29   396069068  0x179B88CC \n",
      "Window:0  Window Sample:30   396069067  0x179B88CB \n",
      "Window:0  Window Sample:31   396069066  0x179B88CA \n"
     ]
    }
   ],
   "source": [
    "# Print the captured ILA samples and mark the trigger position.\n",
    "# Note that counter counts down after the trigger mark.\n",
    "\n",
    "ila.wait_till_done(max_wait_minutes=0.1)\n",
    "upload_successful = ila.upload()\n",
    "if upload_successful:\n",
    "    samples = get_waveform_data(\n",
    "        ila.waveform,\n",
    "        [\"chipscopy_i/counters/slow_counter_0_Q_1\"],\n",
    "        include_trigger=True,\n",
    "        include_sample_info=True,\n",
    "    )\n",
    "    for trigger, sample_index, window_index, window_sample_index, value in zip(*samples.values()):\n",
    "        trigger = \"<-- Trigger\" if trigger else \"\"\n",
    "        print(\n",
    "            f\"Window:{window_index}  Window Sample:{window_sample_index}  {value:10}  0x{value:08X} {trigger}\"\n",
    "        )\n",
    "else:\n",
    "    print(\"Failed to upload ILA data from core\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "better-saturn",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 6 - Waveform Export - VCD (or CSV)\n",
    "\n",
    " Demonstrate how to export waveform data to a VCD file for visualizing waveform in other tools.\n",
    "\n",
    " Export includes complete waveform with probe, _TRIGGER, and _WINDOW.\n",
    "\n",
    " - For CSV export, substitute \"CSV\" for \"VCD\" argument.\n",
    " - To export to a file, substitute the filename for 'sys.stdout'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "altered-glance",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "$date\n",
      "        2022-06-06 20:38:08\n",
      "$end\n",
      "$version\n",
      "        ChipScoPy Version 2022.1.1654231490\n",
      "$end\n",
      "$timescale\n",
      "        1ps\n",
      "$end\n",
      "$scope module dut $end\n",
      "$var reg 1 $ chipscopy_i/counters/slow_counter_0_CE_1  $end\n",
      "$var reg 1 % chipscopy_i/counters/slow_counter_0_SCLR_1  $end\n",
      "$var reg 1 & chipscopy_i/counters/slow_counter_0_UP_1  $end\n",
      "$var reg 1 ' chipscopy_i/counters/slow_counter_0_LOAD_1  $end\n",
      "$var reg 32 ( chipscopy_i/counters/slow_counter_0_L_1 [31:0] $end\n",
      "$var reg 32 ) chipscopy_i/counters/slow_counter_0_Q_1 [31:0] $end\n",
      "$var reg 1 * chipscopy_i/counters/ila_slow_counter_0_TRIG_OUT_trig  $end\n",
      "$var reg 1 + chipscopy_i/counters/ila_fast_counter_0_TRIG_IN_ack  $end\n",
      "$var reg 1 , chipscopy_i/counters/ila_fast_counter_0_TRIG_OUT_trig  $end\n",
      "$var reg 1 - chipscopy_i/counters/ila_slow_counter_0_TRIG_IN_ack  $end\n",
      "$var reg 16 . chipscopy_i/counters/slow_sine_Dout [15:0] $end\n",
      "$var reg 16 / chipscopy_i/counters/slow_cosine_Dout [15:0] $end\n",
      "$var reg 1 0 chipscopy_i/counters/locked  $end\n",
      "$var reg 1 ! _TRIGGER $end\n",
      "$var reg 1 \" _WINDOW $end\n",
      "$upscope $end\n",
      "$enddefinitions $end\n",
      "#0\n",
      "0!\n",
      "0\"\n",
      "1$\n",
      "0%\n",
      "1&\n",
      "0'\n",
      "b0 (\n",
      "b10111100110111000100011001001 )\n",
      "0*\n",
      "0+\n",
      "0,\n",
      "0-\n",
      "b111100000111110 .\n",
      "b10101111011100 /\n",
      "10\n",
      "#1\n",
      "b10111100110111000100011001010 )\n",
      "b111100010000011 .\n",
      "b10101100011111 /\n",
      "#2\n",
      "b10111100110111000100011001011 )\n",
      "b111100011000110 .\n",
      "b10101001100001 /\n",
      "#3\n",
      "b10111100110111000100011001100 )\n",
      "b111100100001000 .\n",
      "b10100110100011 /\n",
      "#4\n",
      "b10111100110111000100011001101 )\n",
      "b111100101001001 .\n",
      "b10100011100101 /\n",
      "#5\n",
      "b10111100110111000100011001110 )\n",
      "b111100110001000 .\n",
      "b10100000100110 /\n",
      "#6\n",
      "b10111100110111000100011001111 )\n",
      "b111100111000111 .\n",
      "b10011101100111 /\n",
      "#7\n",
      "b10111100110111000100011010000 )\n",
      "b111101000000100 .\n",
      "b10011010101000 /\n",
      "#8\n",
      "b10111100110111000100011010001 )\n",
      "b111101001000000 .\n",
      "b10010111101000 /\n",
      "#9\n",
      "b10111100110111000100011010010 )\n",
      "b111101001111011 .\n",
      "b10010100100111 /\n",
      "#10\n",
      "b10111100110111000100011010011 )\n",
      "b111101010110101 .\n",
      "b10010001100111 /\n",
      "#11\n",
      "b10111100110111000100011010100 )\n",
      "b111101011101101 .\n",
      "b10001110100110 /\n",
      "#12\n",
      "b10111100110111000100011010101 )\n",
      "b111101100100101 .\n",
      "b10001011100101 /\n",
      "#13\n",
      "b10111100110111000100011010110 )\n",
      "b111101101011011 .\n",
      "b10001000100011 /\n",
      "#14\n",
      "b10111100110111000100011010111 )\n",
      "b111101110010000 .\n",
      "b10000101100001 /\n",
      "#15\n",
      "b10111100110111000100011011000 )\n",
      "b111101111000100 .\n",
      "b10000010011111 /\n",
      "#16\n",
      "1!\n",
      "0&\n",
      "b10111100110111000100011011001 )\n",
      "b111101111110111 .\n",
      "b1111111011100 /\n",
      "#17\n",
      "0!\n",
      "b10111100110111000100011011000 )\n",
      "b111110000101000 .\n",
      "b1111100011001 /\n",
      "#18\n",
      "b10111100110111000100011010111 )\n",
      "b111110001011000 .\n",
      "b1111001010110 /\n",
      "#19\n",
      "b10111100110111000100011010110 )\n",
      "b111110000101000 .\n",
      "b1111100011001 /\n",
      "#20\n",
      "b10111100110111000100011010101 )\n",
      "b111101111110111 .\n",
      "b1111111011100 /\n",
      "#21\n",
      "b10111100110111000100011010100 )\n",
      "b111101111000100 .\n",
      "b10000010011111 /\n",
      "#22\n",
      "b10111100110111000100011010011 )\n",
      "b111101110010000 .\n",
      "b10000101100001 /\n",
      "#23\n",
      "b10111100110111000100011010010 )\n",
      "b111101101011011 .\n",
      "b10001000100011 /\n",
      "#24\n",
      "b10111100110111000100011010001 )\n",
      "b111101100100101 .\n",
      "b10001011100101 /\n",
      "#25\n",
      "b10111100110111000100011010000 )\n",
      "b111101011101101 .\n",
      "b10001110100110 /\n",
      "#26\n",
      "b10111100110111000100011001111 )\n",
      "b111101010110101 .\n",
      "b10010001100111 /\n",
      "#27\n",
      "b10111100110111000100011001110 )\n",
      "b111101001111011 .\n",
      "b10010100100111 /\n",
      "#28\n",
      "b10111100110111000100011001101 )\n",
      "b111101001000000 .\n",
      "b10010111101000 /\n",
      "#29\n",
      "b10111100110111000100011001100 )\n",
      "b111101000000100 .\n",
      "b10011010101000 /\n",
      "#30\n",
      "b10111100110111000100011001011 )\n",
      "b111100111000111 .\n",
      "b10011101100111 /\n",
      "#31\n",
      "b10111100110111000100011001010 )\n",
      "b111100110001000 .\n",
      "b10100000100110 /\n"
     ]
    }
   ],
   "source": [
    "if upload_successful:\n",
    "    export_waveform(ila.waveform, \"VCD\", sys.stdout)"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
