277 lines
11 KiB
Python
277 lines
11 KiB
Python
# -----------------------------------------------------------------------------
|
|
# 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')
|
|
self.__useByteAddrs = False
|
|
MemMapperBase.__init__(self)
|
|
|
|
# ---------------------------------------------------------
|
|
# Configure the byte address output mode
|
|
# ---------------------------------------------------------
|
|
def setByteAddrMode(self, mode):
|
|
self.__useByteAddrs = mode
|
|
|
|
# ---------------------------------------------------------
|
|
# Overload virtual function with specific output generation,
|
|
# and generate a C/C++ header file with some useful
|
|
# constants in it.
|
|
# ---------------------------------------------------------
|
|
def genOutput(self, ip, opDir):
|
|
|
|
colWidth = 45
|
|
|
|
# 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.
|
|
if opDir == '':
|
|
srcDir = sep.join(jsonPath.split(sep)[:-2]) + sep + 'src'
|
|
else:
|
|
srcDir = opDir
|
|
|
|
# Initialise an external function declaration list
|
|
externalFuncList = []
|
|
|
|
# Check destination directory exists, and create it if not
|
|
if not os.path.exists(srcDir) :
|
|
os.makedirs(srcDir)
|
|
|
|
if self.__useByteAddrs :
|
|
offsetTypeStr = 'byte'
|
|
else :
|
|
offsetTypeStr = 'word'
|
|
|
|
# Open up the file for the header information
|
|
writeFileName = srcDir + sep + ip + '.h'
|
|
wfp = open(writeFileName, 'w')
|
|
wfp.write('// Block name: ' + jsonDict[0][attr.IP_NAME] + ', using ' + offsetTypeStr + ' offsets\n')
|
|
|
|
# Open up the file for the field access functions
|
|
funcsFilename = srcDir + sep + ip + '.cpp'
|
|
ffp = open(funcsFilename, 'w')
|
|
ffp.write('#include <stdint.h>\n')
|
|
ffp.write('#include <' + ip + '.h>\n\n')
|
|
|
|
# If the IP block is listed in sub-block offsets dictionary, extract these offsets
|
|
if ip in self.subBlockOffsetsDict:
|
|
# Loop through the busses, to check for matches
|
|
for jsonBusDict in jsonDict:
|
|
|
|
# Get the stored data from the dictionary entry
|
|
for sbEntry in self.subBlockOffsetsDict[ip]:
|
|
offsetStr, bus, parentIp = sbEntry
|
|
|
|
# If the bus is a match, print the offset information
|
|
if bus == jsonBusDict[attr.BUS]:
|
|
offsetInt = int(offsetStr, 0)
|
|
if self.__useByteAddrs:
|
|
offsetInt *= 4
|
|
wfp.write('// Offset in ' + parentIp + ' for bus ' + bus + ' : 0x' + format(offsetInt, '08x') + '\n')
|
|
|
|
wfp.write('\n')
|
|
|
|
wfp.write('#include <stdint.h>\n\n')
|
|
wfp.write('#ifndef __' + ip.upper() + '_H__\n')
|
|
wfp.write('#define __' + ip.upper() + '_H__\n\n')
|
|
|
|
# Get a camel case version of ip name
|
|
# ipNameCamel = self._underScore2CamelCase(ip)
|
|
|
|
# iterate through each bus dictionary
|
|
for jsonBusDict in jsonDict :
|
|
|
|
# Get the bus name from the dictionary
|
|
bus = jsonBusDict[attr.BUS]
|
|
|
|
# Process sub-block offsets, if any exist
|
|
if attr.SUB_BLOCKS in jsonBusDict:
|
|
# Extract the sub-block dictionary from the model
|
|
sbDict = jsonBusDict[attr.SUB_BLOCKS]
|
|
|
|
# Loop through each sub-block entry and process
|
|
for subBlockEntry in sbDict :
|
|
|
|
# Remove any key uniquifying tag
|
|
subBlock = subBlockEntry.split(':')[0]
|
|
|
|
# Get the particular sub-block's information
|
|
sbInstDict = sbDict[subBlockEntry]
|
|
|
|
# Create an instance string from the bis, ip, sub-block's name and instance suffix,
|
|
# and pad for alignment
|
|
if sbInstDict[attr.INSTANCE] == '' :
|
|
instSuffix = ''
|
|
else :
|
|
instSuffix = '_' + sbInstDict[attr.INSTANCE].upper()
|
|
instStr = bus.upper() + '_' + ip.upper() + '_' + subBlock.upper() + instSuffix
|
|
if len(instStr) < colWidth:
|
|
instStr += ' ' * (colWidth - len(instStr))
|
|
|
|
# Extract the address offset value (converting to byte offset if requested), and
|
|
# create a hex string format for it.
|
|
addrVal = int(sbInstDict[attr.OFFSET], 0)
|
|
if self.__useByteAddrs:
|
|
addrVal *= 4
|
|
addrStr = '0x' + format(addrVal, '08x')
|
|
|
|
# Write out a definition for the address offset
|
|
wfp.write('#define ' + instStr + ' ' + addrStr + '\n')
|
|
|
|
# If a pure hierarchy block, skip register processing
|
|
if attr.REGISTERS not in jsonBusDict :
|
|
wfp.write('\n')
|
|
continue
|
|
|
|
regDict = jsonBusDict[attr.REGISTERS]
|
|
regKeys = regDict.keys()
|
|
|
|
for regName in regKeys:
|
|
|
|
# wfp.write('static const uint32 ')
|
|
wfp.write('#define ')
|
|
# regNameCamel = self._underScore2CamelCase(regName)
|
|
|
|
addrVal = int(regDict[regName][attr.ADDRESS], 0)
|
|
if self.__useByteAddrs :
|
|
addrVal *= 4
|
|
addrStr = '0x' + format(addrVal, '08x')
|
|
|
|
# addrNameStr = ipNameCamel + regNameCamel + bus.capitalize()
|
|
addrNameStr = bus.upper() + '_' + ip.upper() + '_' + regName.upper()
|
|
|
|
if len(addrNameStr) < colWidth :
|
|
addrNameStr += ' ' * (colWidth - len(addrNameStr))
|
|
|
|
wfp.write(addrNameStr + ' ')
|
|
# wfp.write(' = ' + addrStr + ';\n')
|
|
wfp.write(addrStr + '\n')
|
|
|
|
# If there are fields for this register, process them here
|
|
if attr.FIELDS in regDict[regName]:
|
|
|
|
# Reset a count to accumulate bit lengths for bit offsets
|
|
bitCount = 0
|
|
|
|
# Extract the fields section from the model
|
|
fields = regDict[regName][attr.FIELDS]
|
|
|
|
# Loop through each field entry
|
|
for fieldName in fields :
|
|
|
|
# Get the specified bit length ofr this field as an integer
|
|
bitLen = int(fields[fieldName][attr.BIT_LEN], 0)
|
|
|
|
# Create the macro names for the field and the field length
|
|
fieldNameStr = bus.upper() + '_' + ip.upper() + '_' + regName.upper() + '_' + fieldName.upper()
|
|
fieldStr = fieldNameStr
|
|
fieldLenStr = fieldStr + '_WIDTH'
|
|
fieldMskStr = fieldStr + '_MASK'
|
|
|
|
# Pad out the macro names to the column width
|
|
if len(fieldStr) < colWidth:
|
|
fieldStr += ' ' * (colWidth - len(fieldStr))
|
|
if len(fieldLenStr) < colWidth:
|
|
fieldLenStr += ' ' * (colWidth - len(fieldLenStr))
|
|
if len(fieldMskStr) < colWidth:
|
|
fieldMskStr += ' ' * (colWidth - len(fieldMskStr))
|
|
|
|
# If not a reserved field, print out the field's bit offset, and width
|
|
if fields[fieldName][attr.TYPE] != regtype.RESERVED :
|
|
|
|
wfp.write('#define ' + fieldStr + ' ' + str(bitCount) + '\n')
|
|
wfp.write('#define ' + fieldLenStr + ' ' + str(bitLen) + '\n')
|
|
mask = (1 << bitLen) - 1
|
|
mask = mask << bitCount
|
|
maskStr = '0x' + format(mask, '08x')
|
|
wfp.write('#define ' + fieldMskStr + ' ' + maskStr + '\n')
|
|
|
|
funcDeclaration = 'const uint32_t ' + \
|
|
MemMapperBase._underScore2CamelCase(fieldNameStr) + \
|
|
'(uint32_t regVal)'
|
|
|
|
externalFuncList.append('extern ' + funcDeclaration + ';')
|
|
|
|
ffp.write(funcDeclaration + '\n{\n')
|
|
ffp.write(' return ((regVal & ' + fieldNameStr + '_MASK) >> ' + fieldNameStr + ');\n}\n\n')
|
|
|
|
# Add the current field's bit length the but count to get the bit offset for the next field
|
|
bitCount += bitLen
|
|
|
|
wfp.write('\n')
|
|
|
|
for externals in externalFuncList :
|
|
wfp.write(externals + '\n')
|
|
wfp.write('\n')
|
|
|
|
wfp.write('#endif\n')
|
|
wfp.close()
|
|
ffp.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)
|
|
|
|
# Specific command line options added here
|
|
parser.add_argument('-B', '--byte_addrs', dest='byteAddrs', default=False, action='store_true',
|
|
help='Output uses byte addresses (default word)')
|
|
|
|
return parser.parse_args()
|
|
|
|
# ###############################################################
|
|
# Only run if not imported (to test)
|
|
#
|
|
if __name__ == '__main__':
|
|
|
|
memMap = MemMapperCpp()
|
|
|
|
# Process the command line options
|
|
cmdArgs = processCmdLine()
|
|
|
|
# Do initialisation for text object specific configurations
|
|
memMap.setByteAddrMode(cmdArgs.byteAddrs)
|
|
|
|
# Run the program for C++ definitions generation
|
|
memMap.run(cmdArgs.topBlock, cmdArgs.busList, cmdArgs.repoDir)
|
|
del memMap
|