From a89b407eb0399dc4130ec6149eda63bb5cfab627 Mon Sep 17 00:00:00 2001 From: Erik Kooistra <kooistra@astron.nl> Date: Thu, 23 Feb 2017 16:36:42 +0000 Subject: [PATCH] Added command line parser and __main__. Added class description docstrings --- tools/oneclick/prestudy/YAML/peripheral.py | 127 +++++++++++++++++---- 1 file changed, 102 insertions(+), 25 deletions(-) diff --git a/tools/oneclick/prestudy/YAML/peripheral.py b/tools/oneclick/prestudy/YAML/peripheral.py index 5948d3ac6f..f6506e9db2 100644 --- a/tools/oneclick/prestudy/YAML/peripheral.py +++ b/tools/oneclick/prestudy/YAML/peripheral.py @@ -29,11 +29,14 @@ import os import copy import yaml +import argparse import common as cm field_modes = ["RO", "WO", "RW", "WP", "RR"] class Field: + """ A field defines data at certain address or an array of addresses + """ def __init__(self, name, width, mode, offset, default, descr, repeat=None): self.name = name self.width = width @@ -47,7 +50,10 @@ class Field: self.descr = descr self.repeat = repeat + class Register: + """ A register consists of Fields + """ def __init__(self, name, fields = [], nof_inst = 1): self.name = name self.fields = fields @@ -64,13 +70,19 @@ class Register: def setNof_inst(self, nof_inst): self.nof_inst = nof_inst + class RAM(Field): + """ A RAM is a Field that is repeated depth times + """ def __init__(self, name, width, mode, depth, nof_inst, descr): Field.__init__(self, name, width, mode, 0x0, 0, descr) self.depth = depth self.nof_inst = nof_inst + class FIFO(Field): + """ A FIFO is a specific set of Fields + """ def __init__(self, name, width, mode, default, depth): Field.__init__(self, name, width, mode, 0, default) fifo_fields = [] @@ -81,45 +93,67 @@ class FIFO(Field): self.Register = Register("FIFO Register", fifo_fields) self.depth = depth + class Parameter: + """ A Parameter defines a name and value + """ def __init__(self, name, value): self.name = name self.value = value + class Peripheral: + """ A Peripheral consists of 1 or more MM slaves. The slave can be a + Register, RAM or FIFO. + + The Peripheral has parameters to configure the MM slaves. + + A Peripheral has two levels of supporting multiple instances: + 1) At system level a peripheral can be used more than once. The individual + peripherals are then distinghuised by defining a unique label per + instance. + 2) At peripheral level internally in the peripheral if the peripheral + support an array of instances. The size of the array is then specified + via nof_inst. Dependent on the specific definition of the peripheral, + the nof_inst then either replicates some fields in a register or it + replicates some or all MM slaves nof_inst times. + + The Peripheral evaluates the nof_inst and parameters to set the + dimensions of the MM slaves. + """ def __init__(self, name, nof_inst, descr, label = ""): self.name = name + self.postfix = "" + self.label = label # the label is used to make it unique in case multiple instances are required in a system self.nof_inst = nof_inst self.descr = descr - self.label = label # the label is used to make it unique in case multiple instances are required in a system - self.postfix = "" + self.parameters = {} self.registers = [] self.rams = [] self.fifos = [] - self.parameters = {} - def addRegister(self, name, fields): - self.registers.append(Register(name, fields, self.nof_inst)) + def setName(self, name): + self.name = name - def addRAM(self, name, width, mode, depth, descr): - self.rams.append(RAM(name, width, mode, depth, self.nof_inst, descr)) + def setNof_inst(self, nof_inst): + self.nof_inst = nof_inst + + def setLabel(self, label): + self.label = label def addParameter(self, name, value): self.parameters.append(Parameter(name, value)) - def setLabel(self, label): - self.label = label + def addRegister(self, name, fields): + self.registers.append(Register(name, fields, self.nof_inst)) - def setName(self, name): - self.name = name + def addRAM(self, name, width, mode, depth, descr): + self.rams.append(RAM(name, width, mode, depth, self.nof_inst, descr)) - def setNof_inst(self, nof_inst): - self.nof_inst = nof_inst - def evalRAM(self): """Evaluate the parameters and the nof_inst of the peripheral in order to define the real depth and width of the RAM. - For instance: depth = c_nof_weights*c_nof_signal_paths - witdh = c_weights_w*c_nof_complex """ + For example: depth = c_nof_weights*c_nof_signal_paths + witdh = c_weights_w*c_nof_complex """ for parameter in self.parameters: exec("%s = %d" % (parameter, self.parameters[parameter])) # Here the parameters of the peripheral are promoted to real python variables for ram in self.rams: @@ -165,8 +199,10 @@ class Peripheral: self.evalRAM() self.evalRegister() + class PeripheralLibrary: - + """ List of all information for peripheral config files in the root dir + """ def __init__(self, rootDir, fileName='peripheral.yaml'): """Store the dictionaries from all fileName files in rootDir.""" self.rootDir = rootDir @@ -243,12 +279,14 @@ class PeripheralLibrary: return return_peripheral -class System(Peripheral): + +class System: + """ A System consist of a set of one or more Peripherals. + """ def __init__(self, filePathName=None): - Peripheral.__init__(self, None, 1, None) self.filePathName = filePathName self.rootDir = os.environ['RADIOHDL'] + "/tools/oneclick/prestudy/YAML" - self.peri_lib = PeripheralLibrary( self.rootDir, "peripheral.yaml") + self.peri_lib = PeripheralLibrary(self.rootDir, "peripheral.yaml") self.peripherals = [] if filePathName==None: @@ -324,16 +362,55 @@ class System(Peripheral): for param in peripheral.parameters: print " " + param + " " + str(peripheral.parameters[param]) - print "" + print "" + +class SystemParseArgs: + """ Parse command line arguments + """ + def __init__(self): + # Parse command line arguments + argparser = argparse.ArgumentParser(description='System and peripheral config command line parser arguments') + argparser.add_argument('-p','--peripheral', default=None, required=False, help='peripheral names separated by commas') + argparser.add_argument('-s','--system', default=None, required=False, help='system names separated by commas') + argparser.add_argument('-r','--run', required=False, action='store_true', default=False, help='run command') + argparser.add_argument('-v','--verbosity', required=False, type=int, default=0, help='verbosity >= 0 for more info') + args = vars(argparser.parse_args()) + + # Keep the argparser for external access of e.g. print_help + self.argparser = argparser + + # Keep arguments in class record + self.peripheral_names = [] + if args['peripheral']!=None: + self.peripheral_names = args['peripheral'].split(',') + + self.system_names = [] + if args['system']!=None: + self.system_names = args['system'].split(',') + + self.run = args['run'] + + self.verbosity = args['verbosity'] - - +if __name__ == '__main__': + # Mode + # 0 = Read YAML info from one system configuration file + # 1 = Read YAML info from all system and peripheral configuration files in the root directory + mode = 0 - - + # Parse command line arguments + system_args = SystemParseArgs() + + # Read the peripheral configuration files from the root in the current directory and the sub directories + #hdl = HdlConfig(toolRootDir=os.path.expandvars('$RADIOHDL/tools/oneclick/prestudy/YAML'), libFileName='hdllib.cfg', toolFileName=cli_args.toolFileName) + if mode==0: + rootDir = os.path.expandvars('$RADIOHDL/tools/oneclick/prestudy/YAML') + peri_lib = PeripheralLibrary(rootDir, "peripheral.yaml") + system = System(rootDir + "/system.yaml") + system.display_system() -- GitLab