# ----------------------------------------------------------------------------- # Title : C++ register definition package generator # Project : Generic FW tools # ----------------------------------------------------------------------------- # File : gen_cpp.py # Author : Simon Southwell # Company : Global Inkjet Systems # Created : 2017-12-04 # Last update: 2017-12-11 # Platform : # Standard : Python 3.x # ----------------------------------------------------------------------------- # Description: # Definition of class to read JSON register definitions and generate a set # of C++ declarations with constant definitions for address offsets and # field start bits and masks for bit-slicing the sub-fields. Inherits # MemMapperBase for methods to read JSON files and produce a model. # ----------------------------------------------------------------------------- # Copyright (c) 2017 Global Inkjet Systems # ----------------------------------------------------------------------------- import os import argparse # Import base class to locate and read JSON files, and present a model # to derived child class. import memMapperBase from memMapperBase import MemMapperBase, attr, regtype # --------------------------------------------------------- # --------------------------------------------------------- class MemMapperCpp(MemMapperBase): def __init__(self): print('MemMapperCpp') MemMapperBase.__init__(self) # Overload virtual function with specific output generation, # and generate a C/C++ header file with some useful constants in it. def genOutput(self, ip): # Fetch the JSON path and the dictionary with the parsed data jsonPath, jsonDict = self._getModel(ip) # What's the path separator? Usually '\\' for windows, else '/' sep = os.sep # Generate specific output. Assumes src/ exists (or will be created) in level above JSON file location. srcDir = sep.join(jsonPath.split(sep)[:-2]) + sep + 'src' # Check destination directory exists, and create it if not if not os.path.exists(srcDir) : os.makedirs(srcDir) writeFileName = srcDir + sep + ip + '.h' wfp = open(writeFileName, 'w') wfp.write('// Block name: ' + jsonDict[0][attr.IP_NAME] + '\n') # Get a camel case version of ip name ipNameCamel = self._underScore2CamelCase(ip) # iterate through each bus dictionary for jsonBusDict in jsonDict : # If a hierarchy block, skip if attr.SUB_BLOCKS in jsonBusDict: return # Get the bus name from th dictionary bus = jsonBusDict[attr.BUS] regDict = jsonBusDict[attr.REGISTERS] regKeys = regDict.keys() for regName in regKeys: wfp.write('static const uint32 ') regNameCamel = self._underScore2CamelCase(regName) addrVal = int(regDict[regName][attr.ADDRESS], 0) addrStr = '0x' + format(addrVal, '08x') addrNameStr = ipNameCamel + regNameCamel + bus.capitalize() colWidth = 30 if len(addrNameStr) < colWidth : addrNameStr += ' ' * (colWidth - len(addrNameStr)) wfp.write(addrNameStr) wfp.write(' = ' + addrStr + ';\n') wfp.close() # -------------------------------------------------------------- # Parse the command line arguments # def processCmdLine(): # Create a parser object parser = argparse.ArgumentParser(description='Process command line options.') # Process common command line options with inherited static base class method MemMapperCpp.processCmdLine(parser) return parser.parse_args() # ############################################################### # Only run if not imported (to test) # if __name__ == '__main__': memMap = MemMapperCpp() # Process the command line options cmdArgs = processCmdLine() # Run the program for C++ definitions generation memMap.run(cmdArgs.topBlock, cmdArgs.busList) del memMap