| 1 | import pickle |
| 2 | import os |
| 3 | import sys |
| 4 | import re |
| 5 | |
| 6 | gowinhome = os.getenv("GOWINHOME") |
| 7 | if not gowinhome: |
| 8 | raise Exception("GOWINHOME not set") |
| 9 | |
| 10 | device = os.getenv("DEVICE") |
| 11 | if not device: |
| 12 | raise Exception("DEVICE not set") |
| 13 | |
| 14 | with open(f"{device}.pickle", 'rb') as f: |
| 15 | db = pickle.load(f) |
| 16 | |
| 17 | locre = re.compile(r"R(\d+)C(\d+)_(\w+)") |
| 18 | |
| 19 | def route(row, col, gb): |
| 20 | loc = f"R{row}C{col}_{gb}" |
| 21 | if loc not in db.aliases: return |
| 22 | print(loc) |
| 23 | gbo = db.aliases[loc] |
| 24 | print(gbo) |
| 25 | lrow, lcol, pip = locre.match(gbo).groups() |
| 26 | lrow = int(lrow) |
| 27 | lcol = int(lcol) |
| 28 | gt = db.grid[lrow-1][lcol-1].pips[pip] |
| 29 | gt = list(gt.keys())[0] |
| 30 | loc = f"R{lrow}C{lcol}_{gt}" |
| 31 | print(loc) |
| 32 | tap = db.aliases[loc] |
| 33 | print(tap) |
| 34 | trow, tcol, pip = locre.match(tap).groups() |
| 35 | trow = int(trow) |
| 36 | tcol = int(tcol) |
| 37 | smux = db.grid[trow-1][tcol-1].clock_pips[pip] |
| 38 | spine = "R1C1_NONE" |
| 39 | for out in smux.keys(): |
| 40 | loc = f"R{trow}C{tcol}_{out}" |
| 41 | if loc in db.aliases: |
| 42 | spine = db.aliases[loc] |
| 43 | break |
| 44 | print(spine) |
| 45 | srow, scol, pip = locre.match(spine).groups() |
| 46 | srow = int(srow) |
| 47 | scol = int(scol) |
| 48 | cmux = db.grid[srow-1][scol-1].clock_pips[pip] |
| 49 | iob = [] |
| 50 | for out in cmux.keys(): |
| 51 | loc = f"R{srow}C{scol}_{out}" |
| 52 | if loc in db.aliases: |
| 53 | iob.append(db.aliases[loc]) |
| 54 | |
| 55 | print(iob[0]) |
| 56 | |
| 57 | errors = {} |
| 58 | for r in range(db.rows): |
| 59 | for c in range(db.cols): |
| 60 | for clk in range(8): |
| 61 | gb = f"GB{clk}0" |
| 62 | try: |
| 63 | route(r+1, c+1, gb) |
| 64 | except KeyError as e: |
| 65 | errors.setdefault((r, c), []).append(e.args[0]) |
| 66 | print("Key error", e) |
| 67 | |
| 68 | print("#"*(db.cols+2)) |
| 69 | for r in range(db.rows): |
| 70 | print('#', end='') |
| 71 | for c in range(db.cols): |
| 72 | if (r, c) in errors: |
| 73 | print(len(errors[r, c]), end='') |
| 74 | else: |
| 75 | print(' ', end='') |
| 76 | print("#") |
| 77 | print("#"*(db.cols+2)) |
| 78 | |
| 79 | print(errors) |