plc_pb_to_placement_tcl.py 2.72 KB
Newer Older
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
'''
Convert plc to place.tcl for macros
'''
import sys
import re
import numpy as np

plc_file = sys.argv[1]
pb_file = sys.argv[2]
place_tcl = sys.argv[3]

orientMap = {
    "N" : "R0",
    "S" : "R180",
    "W" : "R90",
    "E" : "R270",
    "FN" : "MY",
    "FS" : "MX",
    "FW" : "MX90",
    "FE" : "MY90"
}

class pb_object:
    def __init__(self, id):
        self.name = None
        self.node_id = id
        self.height = 0
        self.width = 0
        self.weight = 0
        self.x = -1
        self.x_offset = 0
        self.y = -1
        self.y_offset = 0
        self.m_name = None
        self.pb_type = None
        self.side = None
        self.orientation = None

fp = open(pb_file, "r")
lines = fp.readlines()

key1 = ['"height"', '"weight"', '"width"', '"x"', '"x_offset"', '"y"', '"y_offset"']
key2 = ['"macro_name"', '"orientation"', '"side"', '"type"']
node_list = []

node_id = 0
key = ""
keys = set()
for line in lines:
    words = line.split()
    if words[0] == 'node':
        node_list.append(pb_object(node_id))
        node_id += 1
    elif words[0] == 'name:':
        node_list[-1].name = words[1]
    elif words[0] == 'key:' :
        key = words[1]
        keys.add(key)
    elif words[0] == 'placeholder:' :
        if key == key2[0]:
            node_list[-1].m_name = words[1]
        elif key == key2[1]:
            node_list[-1].orientation = words[1]
        elif key == key2[2]:
            node_list[-1].side = words[1]
        elif key == key2[3]:
            node_list[-1].pb_type = words[1]
    elif words[0] == 'f:' :
        if key == key1[0]:
            node_list[-1].height = words[1]
        elif key == key1[1]:
            node_list[-1].weight = words[1]
        elif key == key1[2]:
            node_list[-1].width = words[1]
        elif key == key1[3]:
            node_list[-1].x = np.round(float(words[1]),6)
        elif key == key1[4]:
            node_list[-1].x_offset = words[1]
        elif key == key1[5]:
            node_list[-1].y = np.round(float(words[1]),6)
        elif key == key1[6]:
            node_list[-1].y_offset = words[1]
fp.close()

fp = open(plc_file, "r")
lines = fp.readlines()
fp_out = open(place_tcl, "w")
id_pattern = re.compile("[0-9]+")
for line in lines:
    words = line.split()
    if id_pattern.match(words[0]) and (len(words) == 5):
        idx = int(words[0])
        if node_list[idx].pb_type == '"MACRO"':
            x = words[1]
            y = words[2]
            orient = orientMap[words[3]]
            isFixed = words[4]
            macro_name = node_list[idx].name.replace('_[', '\[')
            macro_name = macro_name.replace('_]', '\]')
            fp_out.write(f"placeInstance {macro_name} {x}"\
                        f" {y} {orient} -fixed\n")
fp.close()
fp_out.close()