start code-gen for mux cells
[pinmux.git] / src / parse.py
index 6f4d8c8a8bf052cf6d9b68a266a49664d05e9957..2dffeb33b75974257d62c7cd414de90426942428 100644 (file)
@@ -1,4 +1,5 @@
 import math
+import os.path
 
 
 def missing_numbers(num_list):
@@ -12,83 +13,116 @@ class Parse(object):
     N_MUX = 1          # number of selection lines for the mux per io
     N_IO = 0
     N_MUX_IO = 0
-    Addressing = 'WORD'
-    ADDR_WIDTH = 32
-    DATA_WIDTH = 32
+    ADDR_WIDTH = 64  # TODO parameterise
+    PADDR_WIDTH = 32  # TODO parameterise
+    DATA_WIDTH = 64  # TODO parameterise
     # ================ #
 
-    # Generating the number of bits for memory map #
-    lower_offset = 0
-    if Addressing == 'BYTE':
-        lower_offset = 0
-    elif Addressing == 'HWORD':
-        lower_offset = 1
-    elif Addressing == 'WORD':
-        lower_offset = 2
-    elif Addressing == 'DWORD':
-        lower_offset = 3
-    else:
-        print('ERROR: Addressing should be one of: BYTE, HWORD, WORD, DWORD')
-        exit(1)
-
-    # == capture the number of IO cells required == #
-    pinmapfile = open('pinmap.txt', 'r')
-    max_io = 0
-    muxed_cells = []
-    dedicated_cells = []
-    pinnumbers = []
-
-    for lineno, line in enumerate(pinmapfile):
-        line1 = line.split()
-        if len(line1) <= 1:
-            continue
-        pinnumbers.append(int(line1[0]))
-        if len(line1) == 2:  # dedicated
-            dedicated_cells.append(line1)
-        else:
-            muxed_cells.append(line1)
-
-    pinnumbers = sorted(pinnumbers)
-    upper_offset = lower_offset + int(math.log(len(muxed_cells), 2))
-
-    # ============================================= #
-    # ======= Multiple checks to see if the user has not screwed ======#
-    missing_pins = missing_numbers(pinnumbers)
-
-    # Check-1: ensure that no pin is present in both muxed and dedicated pins
-    for muxcell in muxed_cells:
-        for dedcel in dedicated_cells:
-            if dedcel[1] in muxcell:
-                print("ERROR: " + str(dedcel[1]) + " present \
-                                      in dedicated & muxed lists")
-                exit(1)
-
-    # Check-2: if pin numbering is consistent:
-    if missing_pins:
-        print("ERROR: Following pins have no assignment: " +
-              str(missing_numbers(pinnumbers)))
-        exit(1)
-    unique = set(pinnumbers)
-    duplicate = False
-    for each in unique:
-        count = pinnumbers.count(each)
-        if count > 1:
-            print("ERROR: Multiple assignment for pin: " + str(each))
-            duplicate = True
-    if duplicate:
-        exit(1)
-
-    # Check-2: confirm if N_* matches the instances in the pinmap
-    # ============================================================== #
-
-    # == user info after parsin ================= #
-    N_IO = len(dedicated_cells) + len(muxed_cells)
-    print("Max number of IO: " + str(N_IO))
-    print("Muxed IOs: " + str(len(muxed_cells)))
-    print("Dedicated IOs: " + str(len(dedicated_cells)))
-    # ============================================ #
+    def __init__(self, pth=None, verify=True):
+
+        max_io = 0
+        self.muxed_cells = []
+        self.muxed_cells_width = []
+        self.muxed_cells_bank = []
+        self.dedicated_cells = []
+        self.pinnumbers = []
+        self.bankwidths = {} 
+        self.banksize = {}
+        self.bankstart = {}
+
+        fname = 'pinspec.txt'
+        if pth:
+            fname = os.path.join(pth, fname)
+        with open(fname) as bankwidths:
+            for lineno, line in enumerate(bankwidths):
+                line1 = line[:-1].split('\t')
+                self.bankwidths[line1[0]] = int(line1[3])
+                self.banksize[line1[0]] = int(line1[2])
+                self.bankstart[line1[0]] = int(line1[1])
+            
+        # == capture the number of IO cells required == #
+        fname = 'pinmap.txt'
+        if pth:
+            fname = os.path.join(pth, fname)
+        with open(fname) as pinmapfile:
+            for lineno, line in enumerate(pinmapfile):
+                line1 = line[:-1].split('\t')
+                if len(line1) <= 3:
+                    continue
+                self.pinnumbers.append(int(line1[0]))
+                self.muxed_cells_bank.append(line1[1])
+                self.muxed_cells_width.append(int(line1[2]))
+                # XXX TODO: dedicated pins in separate file
+                #if len(line1) == 2:  # dedicated
+                #    self.dedicated_cells.append(line1)
+                #else:
+                for i in range(3, len(line1)):
+                    # XXX HORRIBLE HACK!!
+                    if line1[i].startswith('pwm'):
+                        line1[i] = 'pwm%s_out' % line1[i][4:]
+                line1 = [line1[0]] + line1[3:]
+                print "line", line1
+                self.muxed_cells.append(line1)
+
+        self.pinnumbers = sorted(self.pinnumbers)
+
+        if verify:
+            self.do_checks()
+
+        #self.cell_bitwidth = self.get_cell_bit_widths()
+
+        # == user info after parsing ================= #
+        self.N_IO = len(self.dedicated_cells) + len(self.muxed_cells)
+        print("Max number of IO: " + str(self.N_IO))
+        #print("Muxer bit width: " + str(self.cell_bitwidth))
+        print("Muxed IOs: " + str(len(self.muxed_cells)))
+        print("Dedicated IOs: " + str(len(self.dedicated_cells)))
+        #sys.exit(0)
+
+    def do_checks(self):
+        """ Multiple checks to see if the user has not screwed up
+        """
+        missing_pins = missing_numbers(self.pinnumbers)
+
+        # Check-1: ensure no pin is present in both muxed and dedicated pins
+        for muxcell in self.muxed_cells:
+            for dedcel in self.dedicated_cells:
+                if dedcel[1] in muxcell:
+                    print("ERROR: " + str(dedcel[1]) + " present \
+                                          in dedicated & muxed lists")
+                    exit(1)
+
+        # Check-2: if pin numbering is consistent:
+        if missing_pins:
+            print("ERROR: Following pins have no assignment: " +
+                  str(missing_numbers(self.pinnumbers)))
+            exit(1)
+        unique = set(self.pinnumbers)
+        duplicate = False
+        for each in unique:
+            count = self.pinnumbers.count(each)
+            if count > 1:
+                print("ERROR: Multiple assignment for pin: " + str(each))
+                duplicate = True
+        if duplicate:
+            exit(1)
+
+        # Check-3: confirm if N_* matches the instances in the pinmap
+        # ============================================================== #
+
+        # TODO
+
+    def get_max_cell_bitwidth(self):
+        max_num_cells = 0
+        for cell in self.muxed_cells:
+            print cell
+            max_num_cells = max(len(cell) - 1, max_num_cells)
+        return int(math.log(max_num_cells + 1, 2))
+
+    def get_muxwidth(self, cellnum):
+        return self.muxed_cells_width[int(cellnum)]
 
 
 if __name__ == '__main__':
     p = Parse()
-    print p.N_IO
+    print (p.N_IO)