{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "d245e103",
      "metadata": {},
      "source": [
        "### License\n",
        "Copyright (C) 2021-2022, Xilinx, Inc.\n",
        "<br>\n",
        "Copyright (C) 2022-2024, Advanced Micro Devices, Inc.\n",
        "<p>\n",
        "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "you may not use this file except in compliance with the License.\n",
        "<p>\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\n",
        "distributed under the License is distributed on an \"AS IS\" BASIS, \n",
        "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "See the License for the specific language governing permissions and\n",
        "limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "id": "81fbda3b",
      "metadata": {},
      "source": [
        "# ChipScoPy ILA and VIO Example\n",
        "\n",
        "\n",
        "<img src=\"../img/api_overview.png\" width=\"500\" align=\"left\">"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e698f67a",
      "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 2024.1 installed and running\n",
        "- Xilinx cs_server 2024.1 installed and running\n",
        "- Python 3.8 or greater installed\n",
        "- ChipScoPy 2024.1 installed\n",
        "- Jupyter notebook support installed - Please do so, using the command `pip install chipscopy[jupyter]`"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "ab60225b",
      "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": "e2e79592",
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\n",
        "import os\n",
        "from chipscopy import get_design_files\n",
        "from chipscopy import create_session, report_versions, delete_session"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "5132ba75",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "HW_URL: TCP:localhost:3121\n",
            "CS_URL: TCP:localhost:3042\n",
            "PROGRAMMING_FILE: /scratch/chipscopy/venv/lib/python3.11/site-packages/chipscopy/examples/designs/vck190/production/chipscopy_ced/chipscopy_wrapper.pdi\n",
            "PROBES_FILE:/scratch/chipscopy/venv/lib/python3.11/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",
        "# specify hw and if programming is desired\n",
        "HW_PLATFORM = os.getenv(\"HW_PLATFORM\", \"vck190\")\n",
        "PROG_DEVICE = os.getenv(\"PROG_DEVICE\", True)\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(f\"{HW_PLATFORM}/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": "2a4adfbc",
      "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": "74f36cfc",
      "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": [
              "\n",
              "\n"
            ]
          },
          "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 │ 2024.1.1717606426    │     │\n",
              "│                            │ │ Timestamp │ Jun 05 2024-09:53:46 │     │\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 │ 1716424495               │ │\n",
              "│                            │ │   Version │ 2024.1                   │ │\n",
              "│                            │ │ Timestamp │ Wed May 22 18:34:55 2024 │ │\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 │ 1714182049           │ │\n",
              "│                            │ │       Version │ 2024.1.0             │ │\n",
              "│                            │ │     Timestamp │ Apr 26 2024-18:40:49 │ │\n",
              "│                            │ │       Package │ 2024.1.1714182049    │ │\n",
              "│                            │ │ Artifact type │ pyinstaller          │ │\n",
              "│                            │ └───────────────┴──────────────────────┘ │\n",
              "└────────────────────────────┴──────────────────────────────────────────┘\n",
              "</pre>\n"
            ],
            "text/plain": [
              "\u001b[3m                      ChipScoPy Version Information                      \u001b[0m\n",
              "┌────────────────────────────┬──────────────────────────────────────────┐\n",
              "│\u001b[1m \u001b[0m\u001b[1m                    Entity\u001b[0m\u001b[1m \u001b[0m│\u001b[1m \u001b[0m\u001b[1mVersion                                 \u001b[0m\u001b[1m \u001b[0m│\n",
              "├────────────────────────────┼──────────────────────────────────────────┤\n",
              "│                  ChipScoPy │ ┌───────────┬──────────────────────┐     │\n",
              "│                            │ │\u001b[1m \u001b[0m\u001b[1mAttribute\u001b[0m\u001b[1m \u001b[0m│\u001b[1m \u001b[0m\u001b[1mValue               \u001b[0m\u001b[1m \u001b[0m│     │\n",
              "│                            │ ├───────────┼──────────────────────┤     │\n",
              "│                            │ │     Build │ 2024.1.1717606426    │     │\n",
              "│                            │ │ Timestamp │ Jun 05 2024-09:53:46 │     │\n",
              "│                            │ └───────────┴──────────────────────┘     │\n",
              "│                            │                                          │\n",
              "│ hw_server @ localhost:3121 │ ┌───────────┬──────────────────────────┐ │\n",
              "│                            │ │\u001b[1m \u001b[0m\u001b[1mAttribute\u001b[0m\u001b[1m \u001b[0m│\u001b[1m \u001b[0m\u001b[1mValue                   \u001b[0m\u001b[1m \u001b[0m│ │\n",
              "│                            │ ├───────────┼──────────────────────────┤ │\n",
              "│                            │ │    Status │ Connected                │ │\n",
              "│                            │ │     Build │ 1716424495               │ │\n",
              "│                            │ │   Version │ 2024.1                   │ │\n",
              "│                            │ │ Timestamp │ Wed May 22 18:34:55 2024 │ │\n",
              "│                            │ └───────────┴──────────────────────────┘ │\n",
              "│                            │                                          │\n",
              "│ cs_server @ localhost:3042 │ ┌───────────────┬──────────────────────┐ │\n",
              "│                            │ │\u001b[1m \u001b[0m\u001b[1m    Attribute\u001b[0m\u001b[1m \u001b[0m│\u001b[1m \u001b[0m\u001b[1mValue               \u001b[0m\u001b[1m \u001b[0m│ │\n",
              "│                            │ ├───────────────┼──────────────────────┤ │\n",
              "│                            │ │        Status │ Connected            │ │\n",
              "│                            │ │         Build │ 1714182049           │ │\n",
              "│                            │ │       Version │ 2024.1.0             │ │\n",
              "│                            │ │     Timestamp │ Apr 26 2024-18:40:49 │ │\n",
              "│                            │ │       Package │ 2024.1.1714182049    │ │\n",
              "│                            │ │ Artifact type │ pyinstaller          │ │\n",
              "│                            │ └───────────────┴──────────────────────┘ │\n",
              "└────────────────────────────┴──────────────────────────────────────────┘\n"
            ]
          },
          "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": [
              "\n",
              "\n"
            ]
          },
          "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": "03079d15",
      "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": "6cfb61c7",
      "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",
              "<span style=\"font-weight: bold\">--&gt; INFO: </span>Programming device with: \n",
              "/scratch/chipscopy/venv/lib/python3.11/site-packages/chipscopy/examples/designs/vck190/production/chipscopy_ced/chi\n",
              "pscopy_wrapper.pdi\n",
              "    \n",
              "</pre>\n"
            ],
            "text/plain": [
              "\n",
              "\u001b[1m--> INFO: \u001b[0mProgramming device with: \n",
              "/scratch/chipscopy/venv/lib/python3.11/site-packages/chipscopy/examples/designs/vck190/production/chipscopy_ced/chi\n",
              "pscopy_wrapper.pdi\n",
              "    \n"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "4ff7460ead1d40b7ad7fad190cf86237",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "Output()"
            ]
          },
          "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\"></pre>\n"
            ],
            "text/plain": []
          },
          "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",
              "</pre>\n"
            ],
            "text/plain": [
              "\n"
            ]
          },
          "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": "9011ad99",
      "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": "7b4b20e9",
      "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": "af3f4849",
      "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": "894cf173",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "0 - 718A03DBCD265D969EA5C15840AEFD17   chipscopy_i/counters/vio_fast_counter_0\n",
            "1 - 22BC7ED6C4145AAB88C58EAEDFF2B04C   chipscopy_i/counters/vio_slow_counter_0\n",
            "2 - 021098F58FE659EFAD76657E635E53FE   chipscopy_i/noc_tg_bc/axis_vio_0\n",
            "3 - 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"
          ]
        }
      ],
      "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": "6a4abaf6",
      "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": "92702039",
      "metadata": {},
      "source": [
        "<img src=\"img/capture_data.png\" width=\"400\" align=\"left\">"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "id": "2bfa8050",
      "metadata": {},
      "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": "e4f0dd59",
      "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": "de4a797b",
      "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": "64e000e5",
      "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": "1e77fbd4",
      "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": "8d00c552",
      "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": "f48220fc",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Window:0  Window Sample:0   293732336  0x1181FFF0 \n",
            "Window:0  Window Sample:1   293732337  0x1181FFF1 \n",
            "Window:0  Window Sample:2   293732338  0x1181FFF2 \n",
            "Window:0  Window Sample:3   293732339  0x1181FFF3 \n",
            "Window:0  Window Sample:4   293732340  0x1181FFF4 \n",
            "Window:0  Window Sample:5   293732341  0x1181FFF5 \n",
            "Window:0  Window Sample:6   293732342  0x1181FFF6 \n",
            "Window:0  Window Sample:7   293732343  0x1181FFF7 \n",
            "Window:0  Window Sample:8   293732344  0x1181FFF8 \n",
            "Window:0  Window Sample:9   293732345  0x1181FFF9 \n",
            "Window:0  Window Sample:10   293732346  0x1181FFFA \n",
            "Window:0  Window Sample:11   293732347  0x1181FFFB \n",
            "Window:0  Window Sample:12   293732348  0x1181FFFC \n",
            "Window:0  Window Sample:13   293732349  0x1181FFFD \n",
            "Window:0  Window Sample:14   293732350  0x1181FFFE \n",
            "Window:0  Window Sample:15   293732351  0x1181FFFF \n",
            "Window:0  Window Sample:16   293732352  0x11820000 <-- Trigger\n",
            "Window:0  Window Sample:17   293732353  0x11820001 \n",
            "Window:0  Window Sample:18   293732354  0x11820002 \n",
            "Window:0  Window Sample:19   293732355  0x11820003 \n",
            "Window:0  Window Sample:20   293732356  0x11820004 \n",
            "Window:0  Window Sample:21   293732357  0x11820005 \n",
            "Window:0  Window Sample:22   293732358  0x11820006 \n",
            "Window:0  Window Sample:23   293732359  0x11820007 \n",
            "Window:0  Window Sample:24   293732360  0x11820008 \n",
            "Window:0  Window Sample:25   293732361  0x11820009 \n",
            "Window:0  Window Sample:26   293732362  0x1182000A \n",
            "Window:0  Window Sample:27   293732363  0x1182000B \n",
            "Window:0  Window Sample:28   293732364  0x1182000C \n",
            "Window:0  Window Sample:29   293732365  0x1182000D \n",
            "Window:0  Window Sample:30   293732366  0x1182000E \n",
            "Window:0  Window Sample:31   293732367  0x1182000F \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 = ila.waveform.get_data(\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": "897d701b",
      "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": "58fdd7e9",
      "metadata": {},
      "source": [
        "<img src=\"img/edge_trigger.png\" width=\"550\" align=\"left\">"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "id": "021be3c6",
      "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": "f09528ea",
      "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": "ee7b08c2",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Window:0  Window Sample:0   329007284  0x139C40B4 \n",
            "Window:0  Window Sample:1   329007285  0x139C40B5 \n",
            "Window:0  Window Sample:2   329007286  0x139C40B6 \n",
            "Window:0  Window Sample:3   329007287  0x139C40B7 \n",
            "Window:0  Window Sample:4   329007288  0x139C40B8 \n",
            "Window:0  Window Sample:5   329007289  0x139C40B9 \n",
            "Window:0  Window Sample:6   329007290  0x139C40BA \n",
            "Window:0  Window Sample:7   329007291  0x139C40BB \n",
            "Window:0  Window Sample:8   329007292  0x139C40BC \n",
            "Window:0  Window Sample:9   329007293  0x139C40BD \n",
            "Window:0  Window Sample:10   329007294  0x139C40BE \n",
            "Window:0  Window Sample:11   329007295  0x139C40BF \n",
            "Window:0  Window Sample:12   329007296  0x139C40C0 \n",
            "Window:0  Window Sample:13   329007297  0x139C40C1 \n",
            "Window:0  Window Sample:14   329007298  0x139C40C2 \n",
            "Window:0  Window Sample:15   329007299  0x139C40C3 \n",
            "Window:0  Window Sample:16   329007300  0x139C40C4 <-- Trigger\n",
            "Window:0  Window Sample:17   329007299  0x139C40C3 \n",
            "Window:0  Window Sample:18   329007298  0x139C40C2 \n",
            "Window:0  Window Sample:19   329007297  0x139C40C1 \n",
            "Window:0  Window Sample:20   329007296  0x139C40C0 \n",
            "Window:0  Window Sample:21   329007295  0x139C40BF \n",
            "Window:0  Window Sample:22   329007294  0x139C40BE \n",
            "Window:0  Window Sample:23   329007293  0x139C40BD \n",
            "Window:0  Window Sample:24   329007292  0x139C40BC \n",
            "Window:0  Window Sample:25   329007291  0x139C40BB \n",
            "Window:0  Window Sample:26   329007290  0x139C40BA \n",
            "Window:0  Window Sample:27   329007289  0x139C40B9 \n",
            "Window:0  Window Sample:28   329007288  0x139C40B8 \n",
            "Window:0  Window Sample:29   329007287  0x139C40B7 \n",
            "Window:0  Window Sample:30   329007286  0x139C40B6 \n",
            "Window:0  Window Sample:31   329007285  0x139C40B5 \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 = ila.waveform.get_data(\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": "f61d21e8",
      "metadata": {},
      "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": "d90ab86c",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "$date\n",
            "        2024-06-07 14:58:49\n",
            "$end\n",
            "$version\n",
            "        ChipScoPy Version 2024.1.1717606426\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",
            "b10011100111000100000010110100 )\n",
            "0*\n",
            "0+\n",
            "0,\n",
            "0-\n",
            "b111000110011100 .\n",
            "b11101011110010 /\n",
            "10\n",
            "#1\n",
            "b10011100111000100000010110101 )\n",
            "b111000111111000 .\n",
            "b11101000111111 /\n",
            "#2\n",
            "b10011100111000100000010110110 )\n",
            "b111001001010011 .\n",
            "b11100110001100 /\n",
            "#3\n",
            "b10011100111000100000010110111 )\n",
            "b111001010101101 .\n",
            "b11100011011000 /\n",
            "#4\n",
            "b10011100111000100000010111000 )\n",
            "b111001100000110 .\n",
            "b11100000100100 /\n",
            "#5\n",
            "b10011100111000100000010111001 )\n",
            "b111001101011110 .\n",
            "b11011101101111 /\n",
            "#6\n",
            "b10011100111000100000010111010 )\n",
            "b111001110110100 .\n",
            "b11011010111001 /\n",
            "#7\n",
            "b10011100111000100000010111011 )\n",
            "b111010000001010 .\n",
            "b11011000000011 /\n",
            "#8\n",
            "b10011100111000100000010111100 )\n",
            "b111010001011110 .\n",
            "b11010101001101 /\n",
            "#9\n",
            "b10011100111000100000010111101 )\n",
            "b111010010110001 .\n",
            "b11010010010110 /\n",
            "#10\n",
            "b10011100111000100000010111110 )\n",
            "b111010100000011 .\n",
            "b11001111011110 /\n",
            "#11\n",
            "b10011100111000100000010111111 )\n",
            "b111010101010100 .\n",
            "b11001100100110 /\n",
            "#12\n",
            "b10011100111000100000011000000 )\n",
            "b111010110100100 .\n",
            "b11001001101110 /\n",
            "#13\n",
            "b10011100111000100000011000001 )\n",
            "b111010111110010 .\n",
            "b11000110110101 /\n",
            "#14\n",
            "b10011100111000100000011000010 )\n",
            "b111011001000000 .\n",
            "b11000011111011 /\n",
            "#15\n",
            "b10011100111000100000011000011 )\n",
            "b111011010001100 .\n",
            "b11000001000001 /\n",
            "#16\n",
            "1!\n",
            "0&\n",
            "b10011100111000100000011000100 )\n",
            "b111011011010111 .\n",
            "b10111110000111 /\n",
            "#17\n",
            "0!\n",
            "b10011100111000100000011000011 )\n",
            "b111011100100010 .\n",
            "b10111011001100 /\n",
            "#18\n",
            "b10011100111000100000011000010 )\n",
            "b111011101101010 .\n",
            "b10111000010000 /\n",
            "#19\n",
            "b10011100111000100000011000001 )\n",
            "b111011100100010 .\n",
            "b10111011001100 /\n",
            "#20\n",
            "b10011100111000100000011000000 )\n",
            "b111011011010111 .\n",
            "b10111110000111 /\n",
            "#21\n",
            "b10011100111000100000010111111 )\n",
            "b111011010001100 .\n",
            "b11000001000001 /\n",
            "#22\n",
            "b10011100111000100000010111110 )\n",
            "b111011001000000 .\n",
            "b11000011111011 /\n",
            "#23\n",
            "b10011100111000100000010111101 )\n",
            "b111010111110010 .\n",
            "b11000110110101 /\n",
            "#24\n",
            "b10011100111000100000010111100 )\n",
            "b111010110100100 .\n",
            "b11001001101110 /\n",
            "#25\n",
            "b10011100111000100000010111011 )\n",
            "b111010101010100 .\n",
            "b11001100100110 /\n",
            "#26\n",
            "b10011100111000100000010111010 )\n",
            "b111010100000011 .\n",
            "b11001111011110 /\n",
            "#27\n",
            "b10011100111000100000010111001 )\n",
            "b111010010110001 .\n",
            "b11010010010110 /\n",
            "#28\n",
            "b10011100111000100000010111000 )\n",
            "b111010001011110 .\n",
            "b11010101001101 /\n",
            "#29\n",
            "b10011100111000100000010110111 )\n",
            "b111010000001010 .\n",
            "b11011000000011 /\n",
            "#30\n",
            "b10011100111000100000010110110 )\n",
            "b111001110110100 .\n",
            "b11011010111001 /\n",
            "#31\n",
            "b10011100111000100000010110101 )\n",
            "b111001101011110 .\n",
            "b11011101101111 /\n"
          ]
        }
      ],
      "source": [
        "if upload_successful:\n",
        "    ila.waveform.export_waveform(\"VCD\", sys.stdout)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "id": "8463f053",
      "metadata": {},
      "outputs": [],
      "source": [
        "## When done with testing, close the connection\n",
        "delete_session(session)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "venv",
      "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.11.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
