#!/usr/bin/env python3
"""
This is an utility script that allows to generate EOS S3 IOMUX configuration
either from data in JSON format or from the given EBLIF netlist plus PCF
constraints of the FPGA design.
"""
import argparse
import csv
import json
import re
import sys

from lib.parse_pcf import parse_simple_pcf
from eblif import parse_blif

# =============================================================================

# Known IOB types and VPR cells that can be placed at their sites
IOB_TYPES = {
    "CLOCK": ["PB-CLOCK", ],
    "BIDIR": ["PB-BIDIR", ],
    "SDIOMUX": ["PB-SDIOMUX", ],
}

# Default configuration of the IOMUX pad
PAD_DEFAULT = {
    "func_sel": 0,
    "ctrl_sel": 0,
    "mode": "none",
    "pull": "none",
    "drive": 2,
    "slew": "slow",
    "schmitt": 0
}

# Base address of the FBIO_SEL registers
FBIOSEL_BASE = 0x40004D80

# Base address of the IOMUX registers
IOMUX_BASE = 0x40004C00

# =============================================================================


def generate_iomux_register_content(config):
    """
    Generates a content of IOMUX registers according to the given config.
    """
    iomux_regs = {}

    # Generate content of the IOMUX_PAD_O_CTRL register for each pad
    for pad, pad_cfg in config["pads"].items():
        pad = int(pad)
        reg = 0

        # Patch default settings with settings read from the config file
        pad_cfg = dict(PAD_DEFAULT, **pad_cfg)

        func_sel = pad_cfg["func_sel"]
        assert func_sel in [0, 1], func_sel
        reg |= func_sel

        ctrl_sel = pad_cfg["ctrl_sel"]