Eunchan Kim | 632c6f7 | 2019-09-30 11:11:51 -0700 | [diff] [blame] | 1 | # Copyright lowRISC contributors. |
| 2 | # Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | |
| 5 | import logging as log |
| 6 | from copy import deepcopy |
| 7 | from pathlib import Path |
| 8 | import hjson |
| 9 | |
| 10 | import re |
| 11 | |
| 12 | |
| 13 | def is_ipcfg(ip: Path) -> bool: # return bool |
| 14 | log.info("IP Path: %s" % repr(ip)) |
| 15 | ip_name = ip.parents[1].name |
| 16 | hjson_name = ip.name |
| 17 | |
| 18 | log.info("IP Name(%s) and HJSON name (%s)" % (ip_name, hjson_name)) |
| 19 | |
| 20 | if ip_name + ".hjson" == hjson_name or ip_name + "_reg.hjson" == hjson_name: |
| 21 | return True |
| 22 | return False |
| 23 | |
| 24 | |
| 25 | def search_ips(ip_path): # return list of config files |
Philipp Wagner | 14a3fee | 2019-11-21 10:07:02 +0000 | [diff] [blame] | 26 | # list the every Hjson file |
Tobias Wölfel | 4c5fbec | 2019-10-23 12:43:17 +0200 | [diff] [blame] | 27 | p = ip_path.glob('*/data/*.hjson') |
Eunchan Kim | 632c6f7 | 2019-09-30 11:11:51 -0700 | [diff] [blame] | 28 | |
Tobias Wölfel | 4c5fbec | 2019-10-23 12:43:17 +0200 | [diff] [blame] | 29 | # filter only ip_name/data/ip_name{_reg|''}.hjson |
Eunchan Kim | 632c6f7 | 2019-09-30 11:11:51 -0700 | [diff] [blame] | 30 | ips = [x for x in p if is_ipcfg(x)] |
| 31 | |
| 32 | log.info("Filtered-in IP files: %s" % repr(ips)) |
| 33 | return ips |
| 34 | |
| 35 | |
| 36 | def is_xbarcfg(xbar_obj): |
| 37 | if "type" in xbar_obj and xbar_obj["type"] == "xbar": |
| 38 | return True |
| 39 | |
| 40 | return False |
| 41 | |
| 42 | |
| 43 | def get_hjsonobj_xbars(xbar_path): |
Philipp Wagner | 14a3fee | 2019-11-21 10:07:02 +0000 | [diff] [blame] | 44 | """ Search crossbars Hjson files from given path. |
Eunchan Kim | 632c6f7 | 2019-09-30 11:11:51 -0700 | [diff] [blame] | 45 | |
Philipp Wagner | 14a3fee | 2019-11-21 10:07:02 +0000 | [diff] [blame] | 46 | Search every Hjson in the directory and check Hjson type. |
Eunchan Kim | 632c6f7 | 2019-09-30 11:11:51 -0700 | [diff] [blame] | 47 | It could be type: "top" or type: "xbar" |
| 48 | returns [(name, obj), ... ] |
| 49 | """ |
| 50 | p = xbar_path.glob('*.hjson') |
| 51 | try: |
| 52 | xbar_objs = [hjson.load(x.open('r'), use_decimal=True) for x in p] |
| 53 | except ValueError: |
| 54 | raise Systemexit(sys.exc_info()[1]) |
| 55 | |
| 56 | xbar_objs = [x for x in xbar_objs if is_xbarcfg(x)] |
| 57 | |
| 58 | return xbar_objs |
| 59 | |
| 60 | |
| 61 | def get_module_by_name(top, name): |
| 62 | """Search in top["module"] by name |
| 63 | """ |
| 64 | module = None |
| 65 | for m in top["module"]: |
| 66 | if m["name"] == name: |
| 67 | module = m |
| 68 | break |
| 69 | |
| 70 | return module |
| 71 | |
| 72 | |
| 73 | def get_signal_by_name(module, name): |
| 74 | """Return the signal struct with the type input/output/inout |
| 75 | """ |
| 76 | result = None |
| 77 | for s in module["available_input_list"] + module[ |
| 78 | "available_output_list"] + module["available_inout_list"]: |
| 79 | if s["name"] == name: |
| 80 | result = s |
| 81 | break |
| 82 | |
| 83 | return result |
| 84 | |
| 85 | |
| 86 | def add_prefix_to_signal(signal, prefix): |
| 87 | """Add prefix to module signal format { name: "sig_name", width: NN } |
| 88 | """ |
| 89 | result = deepcopy(signal) |
| 90 | |
| 91 | if not "name" in signal: |
| 92 | raise SystemExit("signal {} doesn't have name field".format(signal)) |
| 93 | |
| 94 | result["name"] = prefix + "_" + signal["name"] |
| 95 | |
| 96 | return result |
| 97 | |
| 98 | |
| 99 | def get_ms_name(name): |
| 100 | """Split module_name.signal_name to module_name , signal_name |
| 101 | """ |
| 102 | |
| 103 | tokens = name.split('.') |
| 104 | |
| 105 | if len(tokens) == 0: |
| 106 | raise SystemExit("This to be catched in validate.py") |
| 107 | |
| 108 | module = tokens[0] |
| 109 | signal = None |
| 110 | if len(tokens) == 2: |
| 111 | signal = tokens[1] |
| 112 | |
| 113 | return module, signal |
| 114 | |
| 115 | |
| 116 | def parse_pad_field(padstr): |
| 117 | """Parse PadName[NN...NN] or PadName[NN] or just PadName |
| 118 | """ |
| 119 | match = re.match(r'^([A-Za-z0-9_]+)(\[([0-9]+)(\.\.([0-9]+))?\]|)', padstr) |
| 120 | return match.group(1), match.group(3), match.group(5) |
| 121 | |
| 122 | |
| 123 | def get_pad_list(padstr): |
| 124 | pads = [] |
| 125 | |
| 126 | pad, first, last = parse_pad_field(padstr) |
| 127 | if first is None: |
| 128 | first = 0 |
| 129 | last = 0 |
| 130 | elif last is None: |
| 131 | last = first |
| 132 | first = int(first, 0) |
| 133 | last = int(last, 0) |
| 134 | width = first - last + 1 |
| 135 | |
| 136 | for p in range(first, last + 1): |
| 137 | pads.append({"name": pad, "index": p}) |
| 138 | |
| 139 | return pads |
Eunchan Kim | 436d224 | 2019-10-29 17:25:51 -0700 | [diff] [blame] | 140 | |
| 141 | |
| 142 | # Template functions |
| 143 | def ljust(x, width): |
| 144 | return "{:<{width}}".format(x, width=width) |
| 145 | |
| 146 | |
| 147 | def bitarray(d, width): |
| 148 | """Print Systemverilog bit array |
| 149 | |
| 150 | @param d the bit width of the signal |
| 151 | @param width max character width of the signal group |
| 152 | |
| 153 | For instance, if width is 4, the max d value in the signal group could be |
| 154 | 9999. If d is 2, then this function pads 3 spaces at the end of the bit |
| 155 | slice. |
| 156 | |
| 157 | "[1:0] " <- d:=2, width=4 |
| 158 | "[9999:0]" <- max d-1 value |
| 159 | |
| 160 | If d is 1, it means array slice isn't necessary. So it returns empty spaces |
| 161 | """ |
| 162 | |
| 163 | if d <= 0: |
| 164 | log.error("lib.bitarray: Given value {} is smaller than 1".format(d)) |
| 165 | raise ValueError |
| 166 | if d == 1: |
| 167 | return " " * (width + 4) # [x:0] needs 4 more space than char_width |
| 168 | |
| 169 | out = "[{}:0]".format(d - 1) |
| 170 | return out + (" " * (width - len(str(d)))) |
| 171 | |
| 172 | |
| 173 | def parameterize(text): |
| 174 | """Return the value wrapping with quote if not integer nor bits |
| 175 | """ |
| 176 | if re.match('(\d+\'[hdb]\s*[0-9a-f_A-F]+|[0-9]+)', text) == None: |
| 177 | return "\"{}\"".format(text) |
| 178 | |
| 179 | return text |