{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "753808fe",
      "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": "dff6228e",
      "metadata": {},
      "source": [
        "# ChipScoPy VIO Example\n",
        "\n",
        "\n",
        "<img src=\"../img/api_overview.png\" width=\"500\" align=\"left\">"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "41b2e3d9",
      "metadata": {},
      "source": [
        "## Description\n",
        "This example demonstrates how to program and communicate with\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": "cb3fd39d",
      "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": "c674d7cc",
      "metadata": {},
      "outputs": [],
      "source": [
        "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": "5b20c359",
      "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').lower() in ('true', '1', 't')\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": "99f17f12",
      "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": "4db380c4",
      "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": "88ad49d8",
      "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": "4023ef7a",
      "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": "945f561d3d7c4d688e415a2f00ee8616",
              "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",
        "if PROG_DEVICE:\n",
        "    device.program(PROGRAMMING_FILE)\n",
        "else:\n",
        "    print(\"skipping programming\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "15300d8e",
      "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",
        "- Debug cores in the cs_server may be accessed"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "id": "848dda1c",
      "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": "markdown",
      "id": "b2f65b18",
      "metadata": {},
      "source": [
        "## 5 - Using The VIO Core\n",
        "\n",
        "The following cells demonstrate how to perform various operations with the VIO core.\n",
        "These are meant to be useful code snippets that can be copy/pasted for your own application."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "49c08519",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "       UUID                              INSTANCE NAME\n",
            "VIO-0  718A03DBCD265D969EA5C15840AEFD17  chipscopy_i/counters/vio_fast_counter_0\n",
            "VIO-1  22BC7ED6C4145AAB88C58EAEDFF2B04C  chipscopy_i/counters/vio_slow_counter_0\n",
            "VIO-2  021098F58FE659EFAD76657E635E53FE  chipscopy_i/noc_tg_bc/axis_vio_0\n",
            "VIO-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": [
        "# Enumerate all VIO cores in the device.\n",
        "# Every VIO core has properties including a UUID and instance name.\n",
        "#\n",
        "# Print out the VIO core instance UUIDs and instance names\n",
        "# It is normal for this call to be slower the first time, and faster subsequent iterations.\n",
        "\n",
        "vio_cores = device.vio_cores\n",
        "print(\"       UUID                              INSTANCE NAME\")\n",
        "for index, vio_core in enumerate(vio_cores):\n",
        "    print(f\"VIO-{index}  {vio_core.core_info.uuid}  {vio_core.name}\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "92568472",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "vio_by_instance_name and vio_by_uuid match!\n"
          ]
        }
      ],
      "source": [
        "# You can get a VIO core by instance name or uuid\n",
        "vio_by_instance_name = device.vio_cores.get(name=\"chipscopy_i/counters/vio_slow_counter_0\")\n",
        "the_vio_uuid = vio_by_instance_name.uuid\n",
        "\n",
        "# Grab the same VIO by UUID and ensure it is the same core\n",
        "vio_by_uuid = device.vio_cores.get(uuid = the_vio_uuid)\n",
        "assert(vio_by_instance_name == vio_by_uuid)\n",
        "\n",
        "print(\"vio_by_instance_name and vio_by_uuid match!\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "id": "2f5a41e4",
      "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": [
        "# The VIO API knows the mapping between logical probes and ports on the VIO core.\n",
        "# The code below prints the probe to port mapping.\n",
        "\n",
        "vio = device.vio_cores.get(name=\"chipscopy_i/counters/vio_slow_counter_0\")\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": 9,
      "id": "53d5e578",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Wrote 0x12345678 to chipscopy_i/counters/slow_counter_0_L\n",
            "Wrote 0x11223344 to probe_out4\n"
          ]
        }
      ],
      "source": [
        "# Writing values\n",
        "# Values may be written to the ouput ports or logical named probes.\n",
        "\n",
        "vio = device.vio_cores.get(name=\"chipscopy_i/counters/vio_slow_counter_0\")\n",
        "\n",
        "# Writing values to a logical named probes:\n",
        "vio.write_probes({\n",
        "    \"chipscopy_i/counters/slow_counter_0_L\": 0x12345678\n",
        "})\n",
        "print(\"Wrote 0x12345678 to chipscopy_i/counters/slow_counter_0_L\")\n",
        "\n",
        "# Writing the value to the same VIO port:\n",
        "vio.write_ports({\n",
        "    \"probe_out4\": 0x11223344\n",
        "})\n",
        "print(\"Wrote 0x11223344 to probe_out4\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "id": "8efcf745",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Counter Value: 303267458, Activity: NNNRFBBBBBBBBBBBBBBBBBBBBBBBBBBB\n",
            "Counter Value: 309890220, Activity: NNNNNNNNNRBBBBBBBBBBBBBBBBBBBBBB\n",
            "Counter Value: 311499850, Activity: NNNNNNNNRFFBBBBBBBBBBBBBBBBBBBBB\n"
          ]
        }
      ],
      "source": [
        "# Reading VIO probe values\n",
        "# Probes are the logical names mapped to VIO ports in the LTX file.\n",
        "\n",
        "vio = device.vio_cores.get(name=\"chipscopy_i/counters/vio_slow_counter_0\")\n",
        "vio.reset_vio()\n",
        "\n",
        "# Reading all probes at once and extracting one of interest returns a dictionary of probe data.\n",
        "#   value is returned as an integer\n",
        "#   activity is a string, one acivity per bit: N=None, R=Rising, F=Falling, B=Both\n",
        "all_probe_info = vio.read_probes()\n",
        "value = all_probe_info[\"chipscopy_i/counters/slow_counter_0_Q\"][\"value\"]\n",
        "activity = all_probe_info[\"chipscopy_i/counters/slow_counter_0_Q\"][\"activity\"]\n",
        "print(f\"Counter Value: {value}, Activity: {activity}\")\n",
        "\n",
        "# Reading from a single named logical probe\n",
        "# For convenience, you can ask specifically for one or more probes to reduce the data size.\n",
        "# The returned dictionary format is the same.\n",
        "one_probe_info = vio.read_probes(\"chipscopy_i/counters/slow_counter_0_Q\")\n",
        "value = one_probe_info[\"chipscopy_i/counters/slow_counter_0_Q\"][\"value\"]\n",
        "activity = one_probe_info[\"chipscopy_i/counters/slow_counter_0_Q\"][\"activity\"]\n",
        "print(f\"Counter Value: {value}, Activity: {activity}\")\n",
        "\n",
        "# Reading the same value directly from the VIO port \"probe_in0\" - mapped to the same counter\n",
        "port_info = vio.read_ports(\"probe_in0\")\n",
        "value = port_info[\"probe_in0\"][\"value\"]\n",
        "activity = port_info[\"probe_in0\"][\"activity\"]\n",
        "print(f\"Counter Value: {value}, Activity: {activity}\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "id": "e52cff84",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Reading probe values"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "id": "00b31274",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "VIO core chipscopy_i/counters/vio_slow_counter_0 reset to initial values.\n"
          ]
        }
      ],
      "source": [
        "# Resetting the VIO core resets all output values to the their default.\n",
        "# Default values were optionally set during implementation as a property on the VIO IP.\n",
        "\n",
        "vio = device.vio_cores.get(name=\"chipscopy_i/counters/vio_slow_counter_0\")\n",
        "vio.reset_vio()\n",
        "print(f\"VIO core {vio} reset to initial values.\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "id": "de7703ed",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{   'core_info': {   'core_major_ver': 1,\n",
            "                     'core_minor_ver': 0,\n",
            "                     'core_type': 2,\n",
            "                     'drv_ver': 3,\n",
            "                     'tool_major_ver': 226,\n",
            "                     'tool_minor_ver': 0,\n",
            "                     'uuid': '22BC7ED6C4145AAB88C58EAEDFF2B04C'},\n",
            "    'input_ports': {'probe_in0': 32},\n",
            "    'instance': 'chipscopy_i/counters/vio_slow_counter_0',\n",
            "    'name': 'chipscopy_i/counters/vio_slow_counter_0',\n",
            "    'output_ports': {   'probe_out0': 1,\n",
            "                        'probe_out1': 1,\n",
            "                        'probe_out2': 1,\n",
            "                        'probe_out3': 1,\n",
            "                        'probe_out4': 32},\n",
            "    'probes': [   {   'bus_left_index': 31,\n",
            "                      'bus_right_index': 0,\n",
            "                      'direction': 'in',\n",
            "                      'is_bus': True,\n",
            "                      'port_bit_offset': 0,\n",
            "                      'port_index': 0,\n",
            "                      'probe_name': 'chipscopy_i/counters/slow_counter_0_Q'},\n",
            "                  {   'bus_left_index': 0,\n",
            "                      'bus_right_index': 0,\n",
            "                      'direction': 'out',\n",
            "                      'is_bus': False,\n",
            "                      'port_bit_offset': 0,\n",
            "                      'port_index': 0,\n",
            "                      'probe_name': 'chipscopy_i/counters/slow_counter_0_CE'},\n",
            "                  {   'bus_left_index': 0,\n",
            "                      'bus_right_index': 0,\n",
            "                      'direction': 'out',\n",
            "                      'is_bus': False,\n",
            "                      'port_bit_offset': 0,\n",
            "                      'port_index': 1,\n",
            "                      'probe_name': 'chipscopy_i/counters/slow_counter_0_SCLR'},\n",
            "                  {   'bus_left_index': 0,\n",
            "                      'bus_right_index': 0,\n",
            "                      'direction': 'out',\n",
            "                      'is_bus': False,\n",
            "                      'port_bit_offset': 0,\n",
            "                      'port_index': 2,\n",
            "                      'probe_name': 'chipscopy_i/counters/slow_counter_0_UP'},\n",
            "                  {   'bus_left_index': 0,\n",
            "                      'bus_right_index': 0,\n",
            "                      'direction': 'out',\n",
            "                      'is_bus': False,\n",
            "                      'port_bit_offset': 0,\n",
            "                      'port_index': 3,\n",
            "                      'probe_name': 'chipscopy_i/counters/slow_counter_0_LOAD'},\n",
            "                  {   'bus_left_index': 31,\n",
            "                      'bus_right_index': 0,\n",
            "                      'direction': 'out',\n",
            "                      'is_bus': True,\n",
            "                      'port_bit_offset': 0,\n",
            "                      'port_index': 4,\n",
            "                      'probe_name': 'chipscopy_i/counters/slow_counter_0_L'}],\n",
            "    'type': 'CoreType.AXIS_VIO'}\n"
          ]
        }
      ],
      "source": [
        "# You can access low level VIO properties as a dictionary or in json. \n",
        "# This gives easy python access to probe and port information.\n",
        "\n",
        "import pprint\n",
        "pp = pprint.PrettyPrinter(indent = 4)\n",
        "\n",
        "vio = device.vio_cores.get(name=\"chipscopy_i/counters/vio_slow_counter_0\")\n",
        "\n",
        "vio_dict = vio.to_dict()\n",
        "pp.pprint(vio_dict)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "id": "00164470",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{\n",
            "    \"core_info\": {\n",
            "        \"core_type\": 2,\n",
            "        \"drv_ver\": 3,\n",
            "        \"core_major_ver\": 1,\n",
            "        \"core_minor_ver\": 0,\n",
            "        \"tool_major_ver\": 226,\n",
            "        \"tool_minor_ver\": 0,\n",
            "        \"uuid\": \"22BC7ED6C4145AAB88C58EAEDFF2B04C\"\n",
            "    },\n",
            "    \"input_ports\": {\n",
            "        \"probe_in0\": 32\n",
            "    },\n",
            "    \"instance\": \"chipscopy_i/counters/vio_slow_counter_0\",\n",
            "    \"name\": \"chipscopy_i/counters/vio_slow_counter_0\",\n",
            "    \"output_ports\": {\n",
            "        \"probe_out0\": 1,\n",
            "        \"probe_out1\": 1,\n",
            "        \"probe_out2\": 1,\n",
            "        \"probe_out3\": 1,\n",
            "        \"probe_out4\": 32\n",
            "    },\n",
            "    \"probes\": [\n",
            "        {\n",
            "            \"probe_name\": \"chipscopy_i/counters/slow_counter_0_Q\",\n",
            "            \"direction\": \"in\",\n",
            "            \"is_bus\": true,\n",
            "            \"bus_left_index\": 31,\n",
            "            \"bus_right_index\": 0,\n",
            "            \"port_index\": 0,\n",
            "            \"port_bit_offset\": 0\n",
            "        },\n",
            "        {\n",
            "            \"probe_name\": \"chipscopy_i/counters/slow_counter_0_CE\",\n",
            "            \"direction\": \"out\",\n",
            "            \"is_bus\": false,\n",
            "            \"bus_left_index\": 0,\n",
            "            \"bus_right_index\": 0,\n",
            "            \"port_index\": 0,\n",
            "            \"port_bit_offset\": 0\n",
            "        },\n",
            "        {\n",
            "            \"probe_name\": \"chipscopy_i/counters/slow_counter_0_SCLR\",\n",
            "            \"direction\": \"out\",\n",
            "            \"is_bus\": false,\n",
            "            \"bus_left_index\": 0,\n",
            "            \"bus_right_index\": 0,\n",
            "            \"port_index\": 1,\n",
            "            \"port_bit_offset\": 0\n",
            "        },\n",
            "        {\n",
            "            \"probe_name\": \"chipscopy_i/counters/slow_counter_0_UP\",\n",
            "            \"direction\": \"out\",\n",
            "            \"is_bus\": false,\n",
            "            \"bus_left_index\": 0,\n",
            "            \"bus_right_index\": 0,\n",
            "            \"port_index\": 2,\n",
            "            \"port_bit_offset\": 0\n",
            "        },\n",
            "        {\n",
            "            \"probe_name\": \"chipscopy_i/counters/slow_counter_0_LOAD\",\n",
            "            \"direction\": \"out\",\n",
            "            \"is_bus\": false,\n",
            "            \"bus_left_index\": 0,\n",
            "            \"bus_right_index\": 0,\n",
            "            \"port_index\": 3,\n",
            "            \"port_bit_offset\": 0\n",
            "        },\n",
            "        {\n",
            "            \"probe_name\": \"chipscopy_i/counters/slow_counter_0_L\",\n",
            "            \"direction\": \"out\",\n",
            "            \"is_bus\": true,\n",
            "            \"bus_left_index\": 31,\n",
            "            \"bus_right_index\": 0,\n",
            "            \"port_index\": 4,\n",
            "            \"port_bit_offset\": 0\n",
            "        }\n",
            "    ],\n",
            "    \"type\": \"CoreType.AXIS_VIO\"\n",
            "}\n"
          ]
        }
      ],
      "source": [
        "# The VIO properties can conveniently be accessed as JSON as well.\n",
        "# This is convenient when interfacing with other languages.\n",
        "vio = device.vio_cores.get(name=\"chipscopy_i/counters/vio_slow_counter_0\")\n",
        "vio_json = vio.to_json()\n",
        "print(vio_json)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "id": "8eba587f",
      "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
}
