From 28e6cf8b6c6ed4d2868d8156cb6e23fa71b8a105 Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Thu, 22 Mar 2018 11:12:41 +0000 Subject: [PATCH] convert to classes (or functions) --- src/actual_pinmux.py | 97 +++++++++++++------------- src/parse.py | 146 +++++++++++++++++++++------------------- src/pinmux_generator.py | 42 ++++++------ 3 files changed, 145 insertions(+), 140 deletions(-) diff --git a/src/actual_pinmux.py b/src/actual_pinmux.py index 98380e2..ec0e144 100644 --- a/src/actual_pinmux.py +++ b/src/actual_pinmux.py @@ -1,4 +1,3 @@ -from parse import * from string import digits try: from string import maketrans @@ -38,63 +37,63 @@ dedicated_wire = ''' endrule ''' # ============================================================ -pinmux = ''' ''' digits = maketrans('0123456789', ' '*10) # delete space later - def cn(idx): return "cell%s_mux" % str(idx) +def init(p): + p.pinmux = ' ' + global dedicated_wire + for cell in p.muxed_cells: + p.pinmux += " %s_out=" % cn(cell[0]) + i = 0 + while(i < len(cell) - 1): + p.pinmux += "wr%s" % cn(cell[0]) + \ + "==" + str(i) + "?" + cell[i + 1] + "_io:\n\t\t\t" + if(i + 2 == len(cell) - 1): + p.pinmux += cell[i + 2] + "_io" + i = i + 2 + else: + i = i + 1 + p.pinmux += ";\n" + # ======================================================== # -for cell in muxed_cells: - pinmux = pinmux + " %s_out=" % cn(cell[0]) - i = 0 - while(i < len(cell) - 1): - pinmux = pinmux + "wr%s" % cn(cell[0]) + \ - "==" + str(i) + "?" + cell[i + 1] + "_io:\n\t\t\t" - if(i + 2 == len(cell) - 1): - pinmux = pinmux + cell[i + 2] + "_io" - i = i + 2 - else: - i = i + 1 - pinmux = pinmux + ";\n" - # ======================================================== # + # check each cell if "peripheral input/inout" then assign its wire + # Here we check the direction of each signal in the dictionary. + # We choose to keep the dictionary within the code and not user-input + # since the interfaces are always standard and cannot change from + # user-to-user. Plus this also reduces human-error as well :) + for i in range(0, len(cell) - 1): + temp = cell[i + 1].translate(digits) + temp = temp.replace(' ', '') + x = dictionary.get(temp) + if(x is None): + print( + "ERROR: The signal : " + + str(cell[i + 1]) + + " of pinmap.txt isn't present in the current dictionary.\ + \nUpdate dictionary or fix-typo.") + exit(1) + if(x == "input"): + p.pinmux += \ + mux_wire.format(cell[0], i, "wr" + cell[i + 1]) + "\n" + elif(x == "inout"): + p.pinmux += \ + mux_wire.format(cell[0], i, "wr" + cell[i + 1] + + "_in") + "\n" + # ============================================================ # - # check each cell if "peripheral input/inout" then assign its wire - # Here we check the direction of each signal in the dictionary. - # We choose to keep the dictionary within the code and not user-input - # since the interfaces are always standard and cannot change from - # user-to-user. Plus this also reduces human-error as well :) - for i in range(0, len(cell) - 1): - temp = cell[i + 1].translate(digits) - temp = temp.replace(' ', '') + # ================== Logic for dedicated pins ========= # + for cell in p.dedicated_cells: + p.pinmux += " %s" % cn(cell[0]) + \ + "_out=" + cell[1] + "_io;\n" + temp = cell[1].translate(digits) x = dictionary.get(temp) - if(x is None): - print( - "ERROR: The signal : " + - str(cell[i + 1]) + - " of pinmap.txt isn't present in the current dictionary.\ - \nUpdate dictionary or fix-typo.") - exit(1) if(x == "input"): pinmux = pinmux + \ - mux_wire.format(cell[0], i, "wr" + cell[i + 1]) + "\n" + dedicated_wire.format(cell[0], "wr" + cell[1]) + "\n" elif(x == "inout"): pinmux = pinmux + \ - mux_wire.format(cell[0], i, "wr" + cell[i + 1] + - "_in") + "\n" -# ============================================================ # - -# ================== Logic for dedicated pins ========= # -for cell in dedicated_cells: - pinmux = pinmux + " %s" % cn(cell[0]) + \ - "_out=" + cell[1] + "_io;\n" - temp = cell[1].translate(digits) - x = dictionary.get(temp) - if(x == "input"): - pinmux = pinmux + \ - dedicated_wire.format(cell[0], "wr" + cell[1]) + "\n" - elif(x == "inout"): - pinmux = pinmux + \ - dedicated_wire.format(cell[0], "wr" + cell[1] + "_in") + "\n" -# =======================================================# + dedicated_wire.format(cell[0], "wr" + cell[1] + "_in") + "\n" + # =======================================================# diff --git a/src/parse.py b/src/parse.py index d280d0a..b9acbd6 100644 --- a/src/parse.py +++ b/src/parse.py @@ -1,84 +1,88 @@ import math -# == Parameters == # -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 -# ================ # - -# 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) - def missing_numbers(num_list): original_list = [x for x in range(num_list[0], num_list[-1] + 1)] num_list = set(num_list) return (list(num_list ^ set(original_list))) +class Parse(object): + # == Parameters == # + 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 + # ================ # + + # 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): + pinnumbers.append(int(line1[0])) + if(len(line1) == 2): # dedicated + dedicated_cells.append(line1) + if(len(line1) > 2): + muxed_cells.append(line1) + pinnumbers = sorted(pinnumbers) -# == 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): - pinnumbers.append(int(line1[0])) - if(len(line1) == 2): # dedicated - dedicated_cells.append(line1) - if(len(line1) > 2): - 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) -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-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: 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 + # ============================================================== # -# 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))) + # ============================================ # -# == 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))) -# ============================================ # +if __name__ == '__main__': + p = Parse() + print p.N_IO diff --git a/src/pinmux_generator.py b/src/pinmux_generator.py index d7b4d13..f3e00cd 100644 --- a/src/pinmux_generator.py +++ b/src/pinmux_generator.py @@ -24,11 +24,13 @@ import math # project module imports from interface_decl import ifaces, mux_interface, io_interface from wire_def import muxwire, generic_io -from parse import N_IO, ADDR_WIDTH, DATA_WIDTH -from parse import upper_offset, lower_offset -from actual_pinmux import muxed_cells, pinmux +from parse import Parse +from actual_pinmux import init from bus_transactors import axi4_lite +p = Parse() +init(p) + if not os.path.exists("bsv_src"): os.makedirs("bsv_src") @@ -77,7 +79,7 @@ with open("./bsv_src/pinmux.bsv", "w") as bsv_file: // of muxes happening per IO. For now we have a generalized width // where each IO will have the same number of muxes.''') - for cell in muxed_cells: + for cell in p.muxed_cells: bsv_file.write(mux_interface.ifacefmt(cell[0], int(math.log(len(cell) - 1, 2)))) @@ -88,7 +90,7 @@ with open("./bsv_src/pinmux.bsv", "w") as bsv_file: // declare the interface to the IO cells. // Each IO cell will have 8 input field (output from pin mux // and on output field (input to pinmux)''') - for i in range(0, N_IO): + for i in range(0, p.N_IO): bsv_file.write('''\n // interface for IO CEll-{0}''') bsv_file.write(io_interface.ifacefmt(i)) # ============================================================== @@ -116,13 +118,13 @@ with open("./bsv_src/pinmux.bsv", "w") as bsv_file: // the followins wires capture the pin-mux selection // values for each mux assigned to a CELL ''') - for cell in muxed_cells: + for cell in p.muxed_cells: bsv_file.write(muxwire.format( cell[0], int(math.log(len(cell) - 1, 2)))) bsv_file.write( '''\n // following wires capture the values sent to the IO Cell''') - for i in range(0, N_IO): + for i in range(0, p.N_IO): bsv_file.write(generic_io.format(i)) ifaces.wirefmt(bsv_file) @@ -133,7 +135,7 @@ with open("./bsv_src/pinmux.bsv", "w") as bsv_file: bsv_file.write(''' /*====== This where the muxing starts for each io-cell======*/ ''') - bsv_file.write(pinmux) + bsv_file.write(p.pinmux) bsv_file.write(''' /*============================================================*/ ''') @@ -142,14 +144,14 @@ with open("./bsv_src/pinmux.bsv", "w") as bsv_file: bsv_file.write(''' interface mux_lines = interface MuxSelectionLines ''') - for cell in muxed_cells: + for cell in p.muxed_cells: bsv_file.write(mux_interface.ifacedef(cell[0], int(math.log(len(cell) - 1, 2)))) bsv_file.write(''' endinterface; interface peripheral_side = interface PeripheralSide ''') - for i in range(0, N_IO): + for i in range(0, p.N_IO): bsv_file.write(io_interface.ifacedef(i)) ifaces.ifacedef(bsv_file) bsv_file.write(footer) @@ -171,9 +173,9 @@ package PinTop; Ifc_pinmux pinmux <-mkpinmux; // declare the registers which will be used to mux the IOs -'''.format(ADDR_WIDTH, DATA_WIDTH)) +'''.format(p.ADDR_WIDTH, p.DATA_WIDTH)) - for cell in muxed_cells: + for cell in p.muxed_cells: bsv_file.write(''' Reg#(Bit#({0})) rg_muxio_{1} <-mkReg(0);'''.format( int(math.log(len(cell) - 1, 2)), cell[0])) @@ -183,7 +185,7 @@ package PinTop; // pin-mux module rule connect_selection_registers;''') - for cell in muxed_cells: + for cell in p.muxed_cells: bsv_file.write(''' pinmux.mux_lines.cell{0}_mux(rg_muxio_{0});'''.format(cell[0])) @@ -192,10 +194,10 @@ package PinTop; // method definitions for the write user interface method ActionValue#(Bool) write(Bit#({2}) addr, Bit#({3}) data); Bool err=False; - case (addr[{0}:{1}])'''.format(upper_offset, lower_offset, - ADDR_WIDTH, DATA_WIDTH)) + case (addr[{0}:{1}])'''.format(p.upper_offset, p.lower_offset, + p.ADDR_WIDTH, p.DATA_WIDTH)) index = 0 - for cell in muxed_cells: + for cell in p.muxed_cells: bsv_file.write(''' {0}: rg_muxio_{1}<=truncate(data);'''.format(index, cell[0])) index = index + 1 @@ -211,10 +213,10 @@ package PinTop; method Tuple2#(Bool,Bit#({3})) read(Bit#({2}) addr); Bool err=False; Bit#(32) data=0; - case (addr[{0}:{1}])'''.format(upper_offset, lower_offset, - ADDR_WIDTH, DATA_WIDTH)) + case (addr[{0}:{1}])'''.format(p.upper_offset, p.lower_offset, + p.ADDR_WIDTH, p.DATA_WIDTH)) index = 0 - for cell in muxed_cells: + for cell in p.muxed_cells: bsv_file.write(''' {0}: data=zeroExtend(rg_muxio_{1});'''.format(index, cell[0])) index = index + 1 @@ -231,5 +233,5 @@ endpackage # ######## Generate bus transactors ################ with open('bsv_src/bus.bsv', 'w') as bsv_file: - bsv_file.write(axi4_lite.format(ADDR_WIDTH, DATA_WIDTH)) + bsv_file.write(axi4_lite.format(p.ADDR_WIDTH, p.DATA_WIDTH)) # ################################################## -- 2.30.2