import sys import textwrap import toml def group_blocks(registers): blocks = [] current_block = {} for reg in registers: reg["addr"] = int(reg["addr"], 16) registers = list(sorted(registers, key=lambda r: r["addr"])) base = registers[0]["addr"] for i, reg in enumerate(registers): # Check if this is the same block as before if registers[i-1]["addr"] + 1 != reg["addr"]: if i != 0: blocks.append(current_block) current_block = {"addr": reg["addr"], "regs": []} current_block["regs"].append(reg) blocks.append(current_block) return (base, blocks) def peripheral(filename): data = toml.load(open(filename)) base, blocks = group_blocks(data["reg"]) info = data["info"] print(f"""\ {info["name"]} {info["desc"]} {hex(base)} """) if "int" in data: for interrupt in data["int"]: print(f"""\ {interrupt["name"]} {interrupt["desc"]} {interrupt["vect"]} """) print("") for block in blocks: print(f"""\ {hex(block["addr"]-base)} {hex(len(block["regs"]))} registers """) print(f""" """) for block in blocks: for reg in block["regs"]: print(f"""\ {reg["name"]} {reg["desc"]} {hex(reg["addr"]-base)}""") if "cstm" in reg: print(textwrap.indent(reg["cstm"].strip(), " ")) if "safe" in reg and "fields" in reg: print(f'Warning: {info["name"]}[{reg["name"]}] has both "safe" and "fields[]"!', file=sys.stderr) if "safe" in reg: # The whole register is safe to write print("""\ 0 255 """) if "fields" in reg: print(""" """) for field in reg["fields"]: print(f"""\ {field["name"]} {field["desc"]} [{field["rnge"]}]""") if "cstm" in field: print(textwrap.indent(field["cstm"].strip(), " ")) # Write constraint if "safe" in field: rnge = [int(x) for x in field["rnge"].split(":")] bits = rnge[0] - rnge[1] + 1 maxi = 2**bits-1 print(f"""\ 0 {maxi} """) elif "valu" in field: print("""\ true """) if "valu" in field: print(""" """) for value in data["val"][field["valu"]]["values"]: print(f"""\ {value["name"]} {value["desc"]} {value["valu"]} """) print("""\ """) print(f"""\ """) print("""\ """) print("""\ """) print(f"""\ """) if __name__ == "__main__": import sys peripheral(sys.argv[1])