firmware_tools/logic_memory_mapper/gen_pkg.py
David Steele 55387dc256 HMBKYM16ST-38
Change any bit fields that are single bit from "subtype with range " to "constant".
Subtype with range is not used anywhere
2022-04-05 15:43:40 +01:00

256 lines
11 KiB
Python

# -----------------------------------------------------------------------------
# Title : VHDL register definition package generator
# Project : Generic FW tools
# -----------------------------------------------------------------------------
# File : gen_pkg.py
# Author : Simon Southwell
# Company : Global Inkjet Systems
# Created : 2017-12-04
# Last update: 2017-12-13
# Platform :
# Standard : Python 3.x
# -----------------------------------------------------------------------------
# Description:
# Definition of class to read JSON register definitions and generate a VHDL
# package with constant definitions for address offsets and subtypes
# 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 to generate a VHDL file with some useful register
# constants in it, from the set of JSON definitions for a
# project.
# ---------------------------------------------------------
class MemMapperPkg(MemMapperBase) :
outputNameWidth = 35
# ---------------------------------------------------------
# Constructor
# ---------------------------------------------------------
def __init__(self) :
print('MemMapperPkg')
MemMapperBase.__init__(self)
# ---------------------------------------------------------
# Returns a string that is the input string padded with
# spaces to fit the width
# ---------------------------------------------------------
@staticmethod
def __padString(rawStr, width) :
rtnStr = rawStr
rtnStrLen = len(rtnStr)
if rtnStrLen < width :
rtnStr += ' ' * (width - rtnStrLen)
return rtnStr
# ---------------------------------------------------------
# Overload base classes virtual function with specific
# output generation (VHDL package)
# ---------------------------------------------------------
def genOutput(self, ip, opDir='', opSuffix='') :
# 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 in the doc/ directory where the JSON file is located
if opDir == '':
srcDir = sep.join(jsonPath.split(sep)[:-2]) + sep + 'src'
else:
srcDir = opDir
# Check destination directory exists, and create it if not
if not os.path.exists(srcDir) :
os.makedirs(srcDir)
writeFileName = srcDir + sep + ip + '_pkg' + opSuffix + '.vhd'
wfp = open(writeFileName, 'w')
wfp.write('-- Block name: ' + jsonDict[0][attr.IP_NAME] + '\n')
wfp.write('-- And now for some register addresses...\n')
wfp.write('-- altera vhdl_input_version vhdl_2008\n')
wfp.write('library ieee;\n')
wfp.write('use ieee.std_logic_1164.all;\n')
wfp.write('use ieee.numeric_std.all;\n\n')
wfp.write('package ' + ip + '_pkg is\n\n')
# Iterate through each bus dictionary
for jsonBusDict in jsonDict :
# Get the bus name from the dictionary
bus = jsonBusDict[attr.BUS]
# If a hierarchy block, process
if attr.SUB_BLOCKS in jsonBusDict:
subBlockDict = jsonBusDict[attr.SUB_BLOCKS]
for sbEntryKey in subBlockDict :
sbEntry = sbEntryKey.split(':')[0]
offset = subBlockDict[sbEntryKey][attr.OFFSET]
if subBlockDict[sbEntryKey][attr.INSTANCE] == '':
instance = ''
else:
instance = '_' + subBlockDict[sbEntryKey][attr.INSTANCE]
constName = bus.upper() + '_' + sbEntry.upper() + instance.upper()
constNameVec = constName + '_ADDR'
constNameVec = MemMapperPkg.__padString(constNameVec, MemMapperPkg.outputNameWidth)
constNameInt = constName + '_ADDR_INT'
constNameInt = MemMapperPkg.__padString(constNameInt, MemMapperPkg.outputNameWidth)
wfp.write(' constant ' + constNameVec + ' : std_logic_vector(31 downto 0) := 32x"' + format(int(offset, 0), '08X') + '";\n')
wfp.write(' constant ' + constNameInt + ' : integer := 16#' + format(int(offset, 0), '08X') + '#;\n')
wfp.write('\n')
# If a hierarchy block, skip
if attr.REGISTERS not in jsonBusDict:
continue
# Determine the address decode width if specified with addr_width and/or hi and lo address bits,
# else default to 32
if attr.ADDR_WIDTH in jsonBusDict :
addrWidthInt = int(jsonBusDict[attr.ADDR_WIDTH], 0)
elif attr.ADDR_HI_BIT in jsonBusDict :
addrWidthInt = int(jsonBusDict[attr.ADDR_HI_BIT], 0) - int(jsonBusDict[attr.ADDR_LO_BIT], 0) + 1
else :
addrWidthInt = 32
# Only process the bus in the bus list
if bus in self.busList :
regDict = jsonBusDict[attr.REGISTERS]
regKeys = regDict.keys()
for regName in regKeys :
# An '_ADDR' suffix for the register address definition
regFullName = MemMapperBase.createPkgTag(bus, regName)
regFullUsName = regFullName + '_US'
regFullIntName = regFullName + '_INT'
# Pad the output string to a fixed value
regFullName = MemMapperPkg.__padString(regFullName, MemMapperPkg.outputNameWidth)
regFullUsName = MemMapperPkg.__padString(regFullUsName, MemMapperPkg.outputNameWidth)
regFullIntName = MemMapperPkg.__padString(regFullIntName, MemMapperPkg.outputNameWidth)
# Output the address declaration for this register
opStr = ' constant ' + regFullName + ' : std_logic_vector(' + str(addrWidthInt-1) + ' downto 0) := '
# Get the address value form the dictionary, and convert to an integer value
addrVal = int(str(regDict[regName][attr.ADDRESS]), 0)
# Generate a hex string for the value (without a leading '0x') and output
nibbles = (addrWidthInt + 3) >> 2
fmtStr = '0' + str(nibbles) + 'X'
addrValHex = format(addrVal, fmtStr)
opStr += str(addrWidthInt) + 'x\"' + addrValHex + '\";\n'
wfp.write(opStr)
opStr = ' constant ' + regFullUsName + ' : unsigned(' + str(addrWidthInt-1) + ' downto 0) := '
opStr += str(addrWidthInt) + 'x\"' + addrValHex + '\";\n'
wfp.write(opStr)
opStr = ' constant ' + regFullIntName + ' : integer := '
opStr += '16#' + format(addrVal, '08X') + '#;\n'
wfp.write(opStr)
# If there are sub-fields, process them here
if attr.FIELDS in regDict[regName] :
# Extract the fields dictionary from the register
fields = regDict[regName][attr.FIELDS]
# Keep a bit position count, initialised to 0
bitStart = 0
# Loop through the fields
for fieldName in fields :
# Extract the field bit length, and calculate the top and bottom bit positions
fieldLen = int(fields[fieldName][attr.BIT_LEN], 0)
bottomStr = str(bitStart)
topStr = str(bitStart + fieldLen - 1)
# Increment the bit start position to be the bit after current field
bitStart += fieldLen
# Don't process reserved fields (but still need above bit calculations to skip them)
if fields[fieldName][attr.TYPE] != regtype.RESERVED :
# Extract the field name and pad to a fixed width
fieldFullName = MemMapperBase.createPkgTag(bus, regName + '_' + fieldName, False)
# Pad the string to match the desired o/p width
fieldFullName = MemMapperPkg.__padString(fieldFullName, MemMapperPkg.outputNameWidth)
if fieldLen == 1:
# Output single bit as a constant
wfp.write(' constant ' + fieldFullName +
' : integer := ' + topStr + ';\n')
else:
# Output the bit-slice subtype declaration
wfp.write(' subtype ' + fieldFullName +
' is natural range ' + topStr + ' downto ' + bottomStr + ';\n')
# Put a blank line between busses
wfp.write('\n')
# Finish off the package declaration and close the output file
wfp.write('end package ' + ip + '_pkg;\n')
wfp.close()
# --------------------------------------------------------------
# Parse the command line arguments specific to the package
# generator
#
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
MemMapperPkg.processCmdLine(parser)
return parser.parse_args()
# ###############################################################
# Only run if not imported
#
if __name__ == '__main__' :
memMap = MemMapperPkg()
# Process the command line options
cmdArgs = processCmdLine()
# Run the program for VHDL package generation
memMap.run(cmdArgs.topBlock, cmdArgs.busList, cmdArgs.repoDir, cmdArgs.opDir, cmdArgs.opFileSuffix, cmdArgs.jsonFile, cmdArgs.buildDir, cmdArgs.qsysSuffix)
del memMap