File: extract_tilegrid.py

package info (click to toggle)
prjtrellis 1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 83,000 kB
  • sloc: cpp: 20,813; python: 16,246; sh: 375; makefile: 262; asm: 80; ansic: 58
file content (76 lines) | stat: -rwxr-xr-x 2,614 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
"""
This script reads the output from Lattice's bstool in "test" mode, which should be invoked thus:

```
bstool -t bitstream.bit > bitstream.test
```

and from it obtains a list of tiles with the following information:
    - Tile name (with position encoded in the name)
    - Tile type
    - Frame and bit offset
    - Bitstream size in bits ("rows") and frames ("cols")
    - Sites contained within the tile, and their nominal location
and creates a JSON file as output
"""

import sys, re
import json, argparse

tile_re = re.compile(
    r'^Tile\s+([A-Z0-9a-z_/]+)\s+\((\d+), (\d+)\)\s+bitmap offset\s+\((\d+), (\d+)\)\s+\<([A-Z0-9a-z_/]+)>\s*$')

site_re = re.compile(
    r'^\s+([A-Z0-9_]+) \((-?\d+), (-?\d+)\)')

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('-m', action="store_true",
                    help="Use MachXO2 family layout")
parser.add_argument('infile', type=argparse.FileType('r'),
                    help="input file from bstool")
parser.add_argument('outfile', type=argparse.FileType('w'),
                    help="output JSON file")

def main(argv):
    args = parser.parse_args(argv[1:])
    tiles = {}
    current_tile = None
    for line in args.infile:
        tile_m = tile_re.match(line)
        if tile_m:
            name = tile_m.group(6)
            if args.m:
                current_tile = {
                    "type": tile_m.group(1),
                    "start_bit": int(tile_m.group(5)),
                    "start_frame": int(tile_m.group(4)),
                    "rows": int(tile_m.group(3)),
                    "cols": int(tile_m.group(2)),
                    "sites": []
                }
            else:
                current_tile = {
                    "type": tile_m.group(1),
                    "start_bit": int(tile_m.group(4)),
                    "start_frame": int(tile_m.group(5)),
                    "rows": int(tile_m.group(2)),
                    "cols": int(tile_m.group(3)),
                    "sites": []
                }
            identifier = name + ":" + tile_m.group(1)
            assert identifier not in tiles
            tiles[identifier] = current_tile
        else:
            site_m = site_re.match(line)
            if site_m and current_tile is not None:
                current_tile["sites"].append({
                    "name": site_m.group(1),
                    "pos_row": site_m.group(2),
                    "pos_col": site_m.group(3)
                })
    json.dump(tiles, args.outfile, sort_keys=True, indent=4)


if __name__ == "__main__":
    main(sys.argv)