976 lines
42 KiB
Python
976 lines
42 KiB
Python
# -----------------------------------------------------------------------------
|
|
# Title : VHDL register definition package generator
|
|
# Project : Generic FW tools
|
|
# -----------------------------------------------------------------------------
|
|
# File : gen_hdl.py
|
|
# Author : Simon Southwell
|
|
# Company : Global Inkjet Systems
|
|
# Created : 2017-12-11
|
|
# Last update: 2017-12-13
|
|
# Platform :
|
|
# Standard : Python 3.x
|
|
# -----------------------------------------------------------------------------
|
|
# Description:
|
|
# Definition of class to read JSON register definitions and generate a VHDL
|
|
# entity/architecture implementations of memory mapped registers. Inherits
|
|
# MemMapperBase for methods to read JSON files and produce a model.
|
|
# -----------------------------------------------------------------------------
|
|
# Copyright (c) 2017 Global Inkjet Systems
|
|
# -----------------------------------------------------------------------------
|
|
|
|
import os
|
|
import sys
|
|
import argparse
|
|
import time
|
|
|
|
# Import base class to locate and read JSON files, and present a model
|
|
# to derived child class.
|
|
from memMapperBase import MemMapperBase, attr, regtype
|
|
from gen_pkg import MemMapperPkg
|
|
|
|
# ---------------------------------------------------------
|
|
# Class to generate a VHDL file with some useful register
|
|
# constants in it, from the set of JSON definitions for a
|
|
# project.
|
|
# ---------------------------------------------------------
|
|
|
|
class MemMapperHdl(MemMapperBase) :
|
|
|
|
# Define 'constant' values for the class
|
|
outputNameWidth = 30
|
|
tabSpaces = 2
|
|
|
|
# ---------------------------------------------------------
|
|
# Constructor
|
|
# ---------------------------------------------------------
|
|
def __init__(self, project='UNKNOWN') :
|
|
|
|
print('MemMapperHdl')
|
|
|
|
self.project = project
|
|
|
|
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
|
|
|
|
# ---------------------------------------------------------
|
|
# Create a string with uniquified name from register and
|
|
# field names
|
|
# ---------------------------------------------------------
|
|
@staticmethod
|
|
def __uniquifyName(regName, fieldName) :
|
|
|
|
if fieldName == 'NoFields':
|
|
sigNameStr = regName
|
|
|
|
# If a sub-field, append the field name to the signal to uniquify it
|
|
else:
|
|
sigNameStr = regName + '_' + fieldName
|
|
|
|
return sigNameStr
|
|
|
|
# ---------------------------------------------------------
|
|
# Create a when statement for an address
|
|
# ---------------------------------------------------------
|
|
@staticmethod
|
|
def __createWhenStr(bus, regName, addrRange=''):
|
|
|
|
addrName = MemMapperHdl.createPkgTag(bus, regName)
|
|
|
|
# If no address range given...
|
|
if addrRange == '' :
|
|
opStr = '\n when ' + addrName + ' =>\n'
|
|
# Address range specified
|
|
else :
|
|
opStr = '\n when ' + addrName + ' to (' + addrName + ' + std_logic_vector(to_unsigned(' + addrRange + ', 32))) =>\n'
|
|
return opStr
|
|
|
|
# ---------------------------------------------------------
|
|
# Create a pulse assignment
|
|
# ---------------------------------------------------------
|
|
@ staticmethod
|
|
def __createPulseAssignment(sigNameStr, colWidth, isRwpRead=False):
|
|
|
|
if isRwpRead :
|
|
pulseSigName = sigNameStr + '_read_pulse'
|
|
else :
|
|
pulseSigName = sigNameStr + '_pulse'
|
|
if len(pulseSigName) < colWidth:
|
|
pulseSigName += ' ' * (colWidth - len(pulseSigName))
|
|
return ' ' * 5 * MemMapperHdl.tabSpaces + pulseSigName + ' <= \'1\';\n'
|
|
|
|
# ---------------------------------------------------------
|
|
# Parse the model, sorting all registers/fields into
|
|
# register type categories with associated bus, field, bit
|
|
# range and reset value
|
|
# ---------------------------------------------------------
|
|
@staticmethod
|
|
def __categoriseModel(jsonDict) :
|
|
|
|
# Initialise a dictionary with an entry for each supported register type, and an associated
|
|
# (empty) list, to be populated with data for each register/field in a tuple, added to the
|
|
# list with its associated type
|
|
typeDict = {}
|
|
for rType in regtype.typeList :
|
|
typeDict[rType] = []
|
|
|
|
# Iterate through each bus dictionary
|
|
for jsonBusDict in jsonDict :
|
|
|
|
# Get the bus name from the dictionary
|
|
bus = jsonBusDict[attr.BUS]
|
|
|
|
if attr.PROCESS_REGS in jsonBusDict:
|
|
if jsonBusDict[attr.PROCESS_REGS] != 'enabled':
|
|
continue
|
|
|
|
# Only process this IP if it has a "registers" entry
|
|
if attr.REGISTERS in jsonBusDict :
|
|
|
|
regDict = jsonBusDict[attr.REGISTERS]
|
|
regKeys = regDict.keys()
|
|
|
|
# Loop through all the registers
|
|
for regName in regKeys :
|
|
|
|
if attr.ADDR_RANGE in regDict[regName] :
|
|
addrRange = regDict[regName][attr.ADDR_RANGE]
|
|
else :
|
|
addrRange = ''
|
|
|
|
# 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)
|
|
|
|
# Don't process reserved fields (but still need above bit calculations to skip them)
|
|
if fields[fieldName][attr.TYPE] != regtype.RESERVED :
|
|
|
|
# Construct a tuple of the entry's data for adding to the appropriate
|
|
# list in the dictionary of types.
|
|
regTuple = (bus, regName, fieldName,
|
|
bitStart + fieldLen - 1, bitStart,
|
|
fields[fieldName][attr.RESET], addrRange)
|
|
|
|
# Add the field data tuples to the dictionary, depending on type
|
|
typeDict[fields[fieldName][attr.TYPE]].append(regTuple)
|
|
|
|
# Increment the bit start position to be the bit after current field
|
|
bitStart += fieldLen
|
|
else :
|
|
# When no sub-fields, add the straight register name (with no suffix), and mark
|
|
# the field name as 'NoFields' in the tuple of the register's data.
|
|
width = int(regDict[regName][attr.WIDTH], 0)
|
|
regTuple = (bus, regName, 'NoFields', width-1, 0, regDict[regName][attr.RESET], addrRange)
|
|
|
|
# Add the register data tuples to the dictionary, depending on type
|
|
typeDict[regDict[regName][attr.TYPE]].append(regTuple)
|
|
|
|
# Return the dictionary of types with their populated lists
|
|
return typeDict
|
|
|
|
# ---------------------------------------------------------
|
|
# Output a formatted port declaration
|
|
# ---------------------------------------------------------
|
|
@staticmethod
|
|
def __opFormattedDeclaration(wfp, opStr, up, lo, opPort = True, prefixStr = ' ' , outStr = 'out ', inStr = 'in ',
|
|
colWidth=None) :
|
|
|
|
# If no colWidth parameter defined, use default
|
|
if colWidth is None:
|
|
colWidth = MemMapperHdl.outputNameWidth
|
|
|
|
# Select the direction string depending on flag
|
|
if opPort :
|
|
dirStr = outStr
|
|
else :
|
|
dirStr = inStr
|
|
|
|
# Pad out the output to fit the specified column width
|
|
if len(opStr) < colWidth:
|
|
opStr += ' ' * (colWidth - len(opStr))
|
|
|
|
# For single bit declaration construct with std_logic
|
|
if up == lo:
|
|
opStr += ' : ' + dirStr + 'std_logic;'
|
|
# For multi-bit declaration construct with std_logic_vector
|
|
else:
|
|
opStr += ' : ' + dirStr + 'std_logic_vector (' + str(up - lo) + ' downto 0);'
|
|
|
|
# Output the string
|
|
wfp.write(prefixStr + opStr + '\n')
|
|
|
|
# ---------------------------------------------------------
|
|
# Output generics (if any needed)
|
|
# ---------------------------------------------------------
|
|
@staticmethod
|
|
def __opGenerics(wfp, catDict, colWidth = None) :
|
|
|
|
# If no colWidth parameter defined, use default
|
|
if colWidth is None:
|
|
colWidth = MemMapperHdl.outputNameWidth
|
|
|
|
# Initialise lists maintained over main loop
|
|
seenRegList = []
|
|
|
|
# Add all the write types categories to the category list
|
|
catDictAll = []
|
|
for regType in MemMapperHdl.validTypes:
|
|
catDictAll += catDict[regType]
|
|
|
|
# Loop over all the categories
|
|
for wreg in catDictAll :
|
|
|
|
# Get the elements of the tuple into meaningful names
|
|
bus, regName, fieldName, upBit, loBit, resetStr, addrRange = wreg
|
|
|
|
# If this is a new register, do one-off processing
|
|
if regName not in seenRegList :
|
|
|
|
if addrRange != '' :
|
|
if len(addrRange) < colWidth:
|
|
addrRange += ' ' * (colWidth - len(addrRange))
|
|
wfp.write(' ' + addrRange + ' : integer range 0 to 65536 := 0;\n')
|
|
|
|
# ---------------------------------------------------------
|
|
# Output port declarations for the given type and direction
|
|
# ---------------------------------------------------------
|
|
def __opPortDeclaration(self, wfp, catDict, regType, typeIsPulse=False, dirIsOp=True,
|
|
colWidth=None) :
|
|
|
|
# If no colWidth parameter defined, use default
|
|
if colWidth is None:
|
|
colWidth = MemMapperHdl.outputNameWidth
|
|
|
|
# Initialise lists maintained over main loop
|
|
seenRegList = []
|
|
|
|
# Loop over all the writeable categories
|
|
for wreg in catDict[regType]:
|
|
|
|
# Get the elements of the tuple into meaningful names
|
|
bus, regName, fieldName, upBit, loBit, resetStr, addrRange = wreg
|
|
|
|
# If this is the first time we've processed this register, output the pulse
|
|
# signal declaration, and add to list of seen registers. This ensures just one pulse
|
|
# per register, even if multiple sub-fields
|
|
if typeIsPulse and regName not in seenRegList:
|
|
seenRegList.append(regName)
|
|
opStr = regName + '_pulse'
|
|
self.__opFormattedDeclaration(wfp, opStr, 0, 0, True, ' ' * 2 * MemMapperHdl.tabSpaces,
|
|
'out ', 'in ', colWidth)
|
|
if regType == regtype.READ_WRITE_PULSE :
|
|
opStr = regName + '_read_pulse'
|
|
self.__opFormattedDeclaration(wfp, opStr, 0, 0, True, ' ' * 2 * MemMapperHdl.tabSpaces,
|
|
'out ', 'in ', colWidth)
|
|
|
|
# Get a unique name for this port
|
|
opStr = MemMapperHdl.__uniquifyName(regName, fieldName)
|
|
|
|
# Output the port declaration for the output port
|
|
self.__opFormattedDeclaration(wfp, opStr, upBit, loBit, dirIsOp, ' ' * 2 * MemMapperHdl.tabSpaces,
|
|
'out ', 'in ', colWidth)
|
|
|
|
# A write pulse register will also need an input port declaration
|
|
if typeIsPulse and dirIsOp:
|
|
self.__opFormattedDeclaration(wfp, opStr + '_in', upBit, loBit, False, ' ' * 2 * MemMapperHdl.tabSpaces,
|
|
'out ', 'in ', colWidth)
|
|
|
|
# ---------------------------------------------------------
|
|
# Output signal declarations for internal write regs
|
|
# ---------------------------------------------------------
|
|
|
|
def __opSignalDeclaration(self, wfp, catDict, prefixStr = 'signal ', suffixStr = '',
|
|
colWidth=None) :
|
|
|
|
# If no colWidth parameter defined, use default
|
|
if colWidth is None:
|
|
colWidth = MemMapperHdl.outputNameWidth
|
|
|
|
# Loop over all the writeable categories
|
|
for wreg in catDict[regtype.WRITE] + catDict[regtype.WRITE_CLR0]:
|
|
|
|
# Get the elements of the tuple into meaningful names
|
|
bus, regName, fieldName, upBit, loBit, resetStr, addrRange = wreg
|
|
|
|
# Get a unique name for this port
|
|
opStr = MemMapperHdl.__uniquifyName(regName, fieldName)
|
|
|
|
# Add any specified suffix to the signal name
|
|
opStr += suffixStr
|
|
|
|
# Output the signal declaration
|
|
self.__opFormattedDeclaration(wfp, opStr, upBit, loBit, True, prefixStr, '', '', colWidth)
|
|
|
|
# ---------------------------------------------------------
|
|
# Output a signals assignment to various specifications,
|
|
# including a reset to 0
|
|
# ---------------------------------------------------------
|
|
@staticmethod
|
|
def __opSignalAssignment(wfp, catDict, regType, modifierStr = '\'0\'', prefixStr = ' ', isPulse=False, isReg=False,
|
|
colWidth = None, isRwpRead=False) :
|
|
# If no colWidth parameter defined, use default
|
|
if colWidth is None :
|
|
colWidth = MemMapperHdl.outputNameWidth
|
|
|
|
# Initialise lists maintained over main loop
|
|
seenRegList = []
|
|
|
|
# Loop over all the categories
|
|
for wreg in catDict[regType]:
|
|
|
|
# Get the elements of the tuple into meaningful names
|
|
bus, regName, fieldName, upBit, loBit, resetStr, addrRange = wreg
|
|
|
|
# Get a unique name for this signal
|
|
if not isPulse :
|
|
sigNameStr = MemMapperHdl.__uniquifyName(regName, fieldName)
|
|
else :
|
|
# Only process pulse signals once
|
|
if regName not in seenRegList :
|
|
seenRegList.append(regName)
|
|
# For pulse signals, only need one per register
|
|
sigNameStr = regName
|
|
else :
|
|
continue
|
|
|
|
# Remember this unmodified signal name
|
|
sigRootName = sigNameStr
|
|
|
|
# Add a suffix if a pulse or an internal register
|
|
if isPulse :
|
|
if isRwpRead :
|
|
sigNameStr += '_read'
|
|
sigNameStr += '_pulse'
|
|
elif isReg :
|
|
sigNameStr += '_reg'
|
|
|
|
# Create the first part of the assignment, padding the LHS signal to be specified width
|
|
opStr = sigNameStr
|
|
if len(opStr) < colWidth :
|
|
opStr += ' ' * (colWidth - len(opStr))
|
|
opStr = prefixStr + opStr + ' <= '
|
|
|
|
# Add the RHS signal assignment depending on the modifierStr argument
|
|
if modifierStr == '_reg' :
|
|
# For a register, simply add the modifier
|
|
opStr += sigNameStr + modifierStr
|
|
elif modifierStr == 'next_' :
|
|
opStr += modifierStr + sigRootName
|
|
elif modifierStr == 'reset' or modifierStr == 'reset0':
|
|
if modifierStr == 'reset0' :
|
|
resetVal = 0
|
|
else :
|
|
resetVal = int(resetStr, 0)
|
|
width = upBit - loBit + 1
|
|
resetStr = MemMapperBase.valToStdLogic(resetVal, width)
|
|
opStr += resetStr
|
|
elif modifierStr == 'avs':
|
|
if upBit == loBit :
|
|
opStr += 'avs_writedata(' + str(upBit) + ')'
|
|
else :
|
|
opStr += 'avs_writedata(' + str(upBit) + ' downto ' + str(loBit) + ')'
|
|
elif modifierStr == 'w0default' :
|
|
if (upBit - loBit) == 0 :
|
|
opStr += '\'0\''
|
|
else :
|
|
opStr += '(others => \'0\')'
|
|
|
|
else :
|
|
opStr += modifierStr
|
|
|
|
opStr += ';\n'
|
|
wfp.write(opStr)
|
|
|
|
# ---------------------------------------------------------
|
|
# Method to output write register cases entries
|
|
# ---------------------------------------------------------
|
|
@staticmethod
|
|
def __outputWriteCaseEntry(wfp, catDict, colWidth = None):
|
|
|
|
# If no colWidth parameter defined, use default
|
|
if colWidth is None :
|
|
colWidth = MemMapperHdl.outputNameWidth
|
|
|
|
# Initialise lists maintained over main loop
|
|
seenRegList = []
|
|
|
|
# Add all the write types categories to the category list
|
|
catDictWrites = []
|
|
for regType in MemMapperHdl.writeTypes :
|
|
catDictWrites += catDict[regType]
|
|
|
|
# make sure the values are grouped by bus and register by sorting the list (don't care order after this).
|
|
catDictWrites.sort()
|
|
|
|
# Loop over all the writeable categories
|
|
for wreg in catDictWrites :
|
|
|
|
# Get the elements of the tuple into meaningful names
|
|
bus, regName, fieldName, upBit, loBit, resetStr, addrRange = wreg
|
|
|
|
# Set flags to sub-catagorise this particular entry
|
|
isPulse = wreg in catDict[regtype.WRITE_PULSE] or wreg in catDict[regtype.READ_WRITE_PULSE]
|
|
|
|
# Create a unique signal name
|
|
sigNameStr = MemMapperHdl.__uniquifyName(regName, fieldName)
|
|
|
|
# If this is a new register, do one-off processing
|
|
if regName not in seenRegList :
|
|
|
|
# Add this register to the 'seen' list
|
|
seenRegList.append(regName)
|
|
|
|
# Create an address tag and output the 'when' statement for this address
|
|
whenStr = MemMapperHdl.__createWhenStr(bus, regName)
|
|
wfp.write(whenStr)
|
|
|
|
# For pulsed write register type, need to create an assignment to the set
|
|
# the pulse signal to 1
|
|
if isPulse :
|
|
opStr = MemMapperHdl.__createPulseAssignment(regName, colWidth)
|
|
wfp.write(opStr)
|
|
|
|
# If not a pulse type, then the assigned signal is the next value signal, so prepend 'next_'
|
|
if not isPulse :
|
|
sigNameStr = 'next_' + sigNameStr
|
|
|
|
# Pad the signal name to be the column width
|
|
if len(sigNameStr) < colWidth :
|
|
sigNameStr += ' ' * (colWidth - len(sigNameStr))
|
|
|
|
# Indentations
|
|
opStr = ' ' * 5 * MemMapperHdl.tabSpaces
|
|
|
|
# Add the write data signal from the bus to the output string
|
|
opStr += sigNameStr + ' <= avs_writedata('
|
|
|
|
# If only a bit wide, add an indexed reference to the write data signal
|
|
if upBit == loBit :
|
|
opStr += str(upBit)
|
|
# For multi-bit , append a bit slice
|
|
else :
|
|
opStr += str(upBit) + ' downto ' + str(loBit)
|
|
|
|
# Finish off the output string and output
|
|
opStr += ');\n'
|
|
wfp.write(opStr)
|
|
|
|
# ---------------------------------------------------------
|
|
# Method to output read register cases entries (for both
|
|
# read, write and constant types
|
|
# ---------------------------------------------------------
|
|
@staticmethod
|
|
def __outputReadCaseEntry(wfp, catDict, colWidth=None) :
|
|
|
|
# If not colWidth parameter defined, use default
|
|
if colWidth is None:
|
|
colWidth = MemMapperHdl.outputNameWidth
|
|
|
|
# Initialise lists maintained over main loop
|
|
seenRegList = []
|
|
catDictReads = []
|
|
|
|
# Add all the read types catagories to the category dictionary
|
|
# (unless in the 'noReadType' list)
|
|
for regType in MemMapperHdl.readTypes:
|
|
if regType not in MemMapperHdl.noReadTypes :
|
|
catDictReads += catDict[regType]
|
|
|
|
# Add all the write types categories to the category dictionary
|
|
# (unless in the 'noReadType' list)
|
|
for regType in MemMapperHdl.writeTypes:
|
|
if regType not in MemMapperHdl.noReadTypes:
|
|
catDictReads += catDict[regType]
|
|
|
|
# make sure the values are grouped by bus and register by sorting the list (don't care order after this).
|
|
catDictReads.sort()
|
|
|
|
# Loop over all the readable categories
|
|
for rreg in catDictReads :
|
|
|
|
# Get the elements of the tuple into meaningful names
|
|
bus, regName, fieldName, upBit, loBit, resetStr, addrRange = rreg
|
|
|
|
# Set flags to sub-categorise this particular entry
|
|
isRwPulse = rreg in catDict[regtype.READ_WRITE_PULSE]
|
|
isWrPulse = rreg in catDict[regtype.WRITE_PULSE] or isRwPulse
|
|
isRdPulse = rreg in catDict[regtype.READ_PULSE] or isRwPulse
|
|
isRead = rreg in catDict[regtype.READ]
|
|
isConst = rreg in catDict[regtype.CONSTANT]
|
|
|
|
# Create a unique signal name
|
|
sigNameStr = MemMapperHdl.__uniquifyName(regName, fieldName)
|
|
|
|
# If this is a new register, do one-off processing
|
|
if regName not in seenRegList :
|
|
|
|
# Add this register to the 'seen' list
|
|
seenRegList.append(regName)
|
|
|
|
# Create an address tag and output the 'when' statement for this address
|
|
whenStr = MemMapperHdl.__createWhenStr(bus, regName)
|
|
wfp.write(whenStr)
|
|
|
|
# If a read pulse register, add assignment to set pulse output to 1
|
|
if isRdPulse :
|
|
opStr = MemMapperHdl.__createPulseAssignment(regName, colWidth, isRwPulse)
|
|
wfp.write(opStr)
|
|
|
|
# Write pulse register reads come from the associated 'in' port, so append '_in' to signal name
|
|
if isWrPulse :
|
|
sigNameStr += '_in'
|
|
|
|
# Read pulse registers reads come from the input port (no suffix)
|
|
elif isRdPulse or isRead:
|
|
sigNameStr += ''
|
|
|
|
# Constants are just the reset values
|
|
elif isConst :
|
|
sigNameStr = MemMapperHdl.valToStdLogic(int(resetStr, 0), upBit - loBit + 1)
|
|
|
|
# For all other type, read from the internal registers (append '_reg' to signal name)
|
|
else :
|
|
sigNameStr += '_reg'
|
|
|
|
# Start creating the next slave read port signal definition
|
|
avsNameStr = 'next_avs_readdata('
|
|
|
|
# If multi-bit, add a 'x downto y' element
|
|
if upBit != loBit :
|
|
avsNameStr += str(upBit) + ' downto ' + str(loBit) + ')'
|
|
# If a single bit, add a bit index
|
|
else :
|
|
avsNameStr += str(upBit) + ')'
|
|
|
|
# Pad the avs signal to be the column width
|
|
if len(avsNameStr) < colWidth :
|
|
avsNameStr += ' ' * (colWidth - len(avsNameStr))
|
|
|
|
# Construct the entire assignment line and output
|
|
opStr = ' ' * 5 * MemMapperHdl.tabSpaces + avsNameStr + ' <= ' + sigNameStr + ';'
|
|
wfp.write(opStr + '\n')
|
|
|
|
# ---------------------------------------------------------
|
|
# ---------------------------------------------------------
|
|
|
|
def __processSubBlocks(self, jsonBusDict, srcDir, bus, ip, sep, opSuffix='') :
|
|
|
|
colWidth = MemMapperHdl.outputNameWidth
|
|
|
|
# Open up a file for writing the decoder VHDL
|
|
writeFileName = srcDir + sep + ip + '_' + bus + '_decode' + opSuffix + '.vhd'
|
|
wfp = open(writeFileName, 'w')
|
|
|
|
# Open template file for reading
|
|
scriptDir = os.path.dirname(sys.argv[0])
|
|
if scriptDir == '' :
|
|
scriptDir = '.'
|
|
templateFname = scriptDir + sep + 'templates' + sep + 'template_decode.vhd'
|
|
tfp = open(templateFname, 'r')
|
|
|
|
# Extract the sub-block dictionary, and rememebr the number of entries for later
|
|
subBlockDict = jsonBusDict[attr.SUB_BLOCKS]
|
|
sbLen = len(subBlockDict)
|
|
|
|
# Extract the addres high and low bits (as integers) and calculate the bit width for decode
|
|
addrHiBit = int(jsonBusDict[attr.ADDR_HI_BIT], 0)
|
|
addrLoBit = int(jsonBusDict[attr.ADDR_LO_BIT], 0)
|
|
addrWidth = addrHiBit - addrLoBit + 1
|
|
|
|
# Initialise a list in which we will keep a list of the ports we process
|
|
portNameList = []
|
|
|
|
# Read in each line of the template, looking for tags
|
|
for line in tfp:
|
|
|
|
# If the line has no tags, send straight to output, unmodified
|
|
if '%%' not in line:
|
|
wfp.write(line)
|
|
|
|
# When a tag present...
|
|
else:
|
|
# Split the line around the tag
|
|
tagLineSplit = line.split('%%')
|
|
|
|
# The tag is the second entry of the split list
|
|
tagStr = tagLineSplit[1]
|
|
|
|
# Decode the tag, and generate the appropriate output accordingly
|
|
if tagStr == 'IP_DESCRIPTION':
|
|
wfp.write(tagLineSplit[0] + ip.upper() + ' Block\n')
|
|
|
|
elif tagStr == 'PROJECT':
|
|
wfp.write(tagLineSplit[0] + self.project + '\n')
|
|
|
|
elif tagStr == 'OP_FILENAME':
|
|
wfp.write(tagLineSplit[0] + os.path.basename(writeFileName) + '\n')
|
|
|
|
elif tagStr == 'LONG_DESCRIPTION':
|
|
wfp.write('-- ' + jsonBusDict[attr.DESCRIPTION] + '\n')
|
|
|
|
elif tagStr == 'DATE':
|
|
wfp.write(tagLineSplit[0] + time.strftime("%Y-%m-%d") + '\n')
|
|
|
|
elif tagStr == 'DECODE_BLOCK':
|
|
wfp.write(tagLineSplit[0] + ip + '_' + bus + '_decode' + tagLineSplit[2])
|
|
|
|
elif tagStr == 'ADDR_DECODE_HI_BIT' :
|
|
wfp.write(' ADDR_DECODE_HI_BIT : natural range 1 to 32 := ' + str(addrHiBit) + ';\n')
|
|
|
|
elif tagStr == 'ADDR_DECODE_LO_BIT' :
|
|
wfp.write(' ADDR_DECODE_LO_BIT : natural range 1 to 32 := ' + str(addrLoBit) + '\n')
|
|
|
|
elif tagStr == 'STROBE_PORT_LIST' :
|
|
|
|
# Loop through all the entries in the sub-block list and generate the port list
|
|
# for read, write and readdata ports
|
|
for subBlockEntry in subBlockDict :
|
|
|
|
# Remove any key uniquifying tag
|
|
subBlock = subBlockEntry.split(':')[0]
|
|
|
|
# Get the dictionary associated with this sub-block and construct a port name
|
|
# from the sub-block's name, and the instance parameter
|
|
sbDict = subBlockDict[subBlockEntry]
|
|
if sbDict[attr.INSTANCE] == '' :
|
|
instance = ''
|
|
else :
|
|
instance = '_' + sbDict[attr.INSTANCE]
|
|
portName = subBlock + instance
|
|
|
|
# Append the port name to the list, for later
|
|
portNameList.append(portName)
|
|
|
|
# Output the write pulse port definition, aligned
|
|
portName = portName + '_write'
|
|
if len(portName) < colWidth :
|
|
portName += ' ' * (colWidth-len(portName))
|
|
wfp.write(' ' + portName + ': out std_logic;\n')
|
|
|
|
# Output the read pulse port definition
|
|
portName = subBlock + instance + '_read'
|
|
if len(portName) < colWidth:
|
|
portName += ' ' * (colWidth - len(portName))
|
|
wfp.write(' ' + portName + ': out std_logic;\n')
|
|
|
|
# Output the readdata port definition
|
|
portName = subBlock + instance + '_readdata'
|
|
if len(portName) < colWidth:
|
|
portName += ' ' * (colWidth - len(portName))
|
|
wfp.write(' ' + portName + ': in std_logic_vector(31 downto 0);\n')
|
|
|
|
elif tagStr == 'READDATA_BUS_LIST':
|
|
|
|
# Set a count to count down as we loop (to be abe to flag the last entry
|
|
sbCount = sbLen
|
|
|
|
# Write out the signal assignment statement to or all the read busses together
|
|
wfp.write(' avs_readdata <= ')
|
|
|
|
# Loop thorugh the sub-blocks to add their readdata busses to the assignment
|
|
for subBlockEntry in subBlockDict :
|
|
|
|
# Remove any key uniquifying tag
|
|
subBlock = subBlockEntry.split(':')[0]
|
|
|
|
# Construct and output the particular sub-block's readdata signal
|
|
sbDict = subBlockDict[subBlockEntry]
|
|
if sbDict[attr.INSTANCE] == '' :
|
|
instance = ''
|
|
else :
|
|
instance = '_' + sbDict[attr.INSTANCE]
|
|
sbCount -= 1
|
|
wfp.write(subBlock + instance + '_readdata')
|
|
|
|
# Follow the readdata signal name by either an 'or', or ';' if last entry
|
|
if sbCount == 0 :
|
|
wfp.write(';\n')
|
|
else :
|
|
wfp.write(' or ')
|
|
|
|
elif tagStr == 'DEFAULT_STROBE_OUTPUTS':
|
|
|
|
# Loop through the previously constructed port names, to output default value assignments
|
|
for strobe in portNameList :
|
|
|
|
# Construct write port signal name, and assign to '0'
|
|
portName = strobe + '_write'
|
|
if len(portName) < colWidth :
|
|
portName += ' ' * (colWidth - len(portName))
|
|
wfp.write(' ' + portName + ' <= \'0\';\n')
|
|
|
|
# Construct write port signal name, and assign to '0'
|
|
portName = strobe + '_read'
|
|
if len(portName) < colWidth:
|
|
portName += ' ' * (colWidth - len(portName))
|
|
wfp.write(' ' + portName + ' <= \'0\';\n')
|
|
|
|
elif tagStr == 'DECODE_CASE_STATEMENTS' :
|
|
|
|
# Loop through the sub-blocks and construct the main decode case statement entries
|
|
for subBlockEntry in subBlockDict :
|
|
|
|
# Remove any key uniquifying tag
|
|
subBlock = subBlockEntry.split(':')[0]
|
|
|
|
# Get theparticular sub-block data dictionary, and extract the instance and
|
|
# (as an integer) offset values adjusted for given start bit
|
|
sbDict = subBlockDict[subBlockEntry]
|
|
if sbDict[attr.INSTANCE] == '' :
|
|
instance = ''
|
|
else :
|
|
instance = '_' + sbDict[attr.INSTANCE]
|
|
offset = int(sbDict[attr.OFFSET], 0) >> addrLoBit
|
|
|
|
# Output the 'when' statement, with adjusted offset value
|
|
wfp.write(' when std_logic_vector(to_unsigned(' + str(offset) + ', ' + str(addrWidth) + ')) =>\n')
|
|
|
|
# Output the port's write strobe assignment to the bus write strobe port
|
|
portName = subBlock + instance + '_write'
|
|
if len(portName) < colWidth-4 :
|
|
portName += ' ' * (colWidth-4 - len(portName))
|
|
wfp.write(' ' + portName + ' <= avs_write;\n')
|
|
|
|
# Output the port's read strobe assignment to the bus read strobe port
|
|
portName = subBlock + instance + '_read'
|
|
if len(portName) < colWidth-4:
|
|
portName += ' ' * (colWidth-4 - len(portName))
|
|
wfp.write(' ' + portName + ' <= avs_read;\n\n')
|
|
|
|
# Close the open files
|
|
wfp.close()
|
|
tfp.close()
|
|
|
|
# ---------------------------------------------------------
|
|
# 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)
|
|
|
|
# Parse the model and sort the registers into categories
|
|
catDict = self.__categoriseModel(jsonDict)
|
|
|
|
# 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
|
|
|
|
# Check destination directory exists, and create it if not
|
|
if not os.path.exists(srcDir) :
|
|
os.makedirs(srcDir)
|
|
|
|
for jsonBusDict in jsonDict :
|
|
|
|
# Get the bus name from the dictionary
|
|
bus = jsonBusDict[attr.BUS]
|
|
|
|
# If a hierarchy block, skip
|
|
if attr.SUB_BLOCKS in jsonBusDict:
|
|
self.__processSubBlocks(jsonBusDict, srcDir, bus, ip, sep, opSuffix)
|
|
|
|
if attr.REGISTERS not in jsonBusDict :
|
|
continue
|
|
|
|
if attr.PROCESS_REGS in jsonBusDict :
|
|
if jsonBusDict[attr.PROCESS_REGS] != 'enabled' :
|
|
continue
|
|
|
|
writeFileName = srcDir + sep + ip + '_' + bus + '_regs' + opSuffix + '.vhd'
|
|
wfp = open(writeFileName, 'w')
|
|
|
|
# Open template file for reading
|
|
scriptDir = os.path.dirname(sys.argv[0])
|
|
if scriptDir == '' :
|
|
scriptDir = '.'
|
|
templateFname = scriptDir + sep + 'templates' + sep + 'template.vhd'
|
|
tfp = open(templateFname, 'r')
|
|
|
|
# Read in each line of the template, looking for tags
|
|
for line in tfp :
|
|
|
|
# If the line has no tags, send straight to output, unmodified
|
|
if '%%' not in line :
|
|
wfp.write(line)
|
|
|
|
# When a tag present...
|
|
else :
|
|
# Split the line around the tag
|
|
tagLineSplit = line.split('%%')
|
|
|
|
# The tag is the second entry of the split list
|
|
tagStr = tagLineSplit[1]
|
|
|
|
# Decode the tag, and generate the appropriate output accordingly
|
|
|
|
if tagStr == 'IP_DESCRIPTION' :
|
|
wfp.write(tagLineSplit[0] + ip.upper() + ' Block\n')
|
|
|
|
elif tagStr == 'PROJECT' :
|
|
wfp.write(tagLineSplit[0] + self.project + '\n')
|
|
|
|
elif tagStr == 'OP_FILENAME' :
|
|
wfp.write(tagLineSplit[0] + os.path.basename(writeFileName) + '\n')
|
|
|
|
elif tagStr == 'LONG_DESCRIPTION' :
|
|
wfp.write('-- ' + jsonBusDict[attr.DESCRIPTION] + '\n')
|
|
|
|
elif tagStr == 'DATE' :
|
|
wfp.write(tagLineSplit[0] + time.strftime("%Y-%m-%d") + '\n')
|
|
|
|
elif tagStr == 'IP_PKG' :
|
|
wfp.write(tagLineSplit[0] + ip + '_pkg.all;\n')
|
|
|
|
elif tagStr == 'REG_BLOCK' :
|
|
wfp.write(tagLineSplit[0] + ip + '_' + bus + '_regs' + tagLineSplit[2])
|
|
|
|
elif tagStr == 'GENERICS' :
|
|
self.__opGenerics(wfp, catDict)
|
|
|
|
elif tagStr == 'PORTLIST' :
|
|
|
|
# output internal write registers/fields
|
|
self.__opPortDeclaration(wfp, catDict, regtype.WRITE, False, True)
|
|
|
|
# output internal write0 registers/fields
|
|
self.__opPortDeclaration(wfp, catDict, regtype.WRITE_CLR0, False, True)
|
|
|
|
# output write pulse registers/fields and pulse signal (per register), and input read values
|
|
self.__opPortDeclaration(wfp, catDict, regtype.WRITE_PULSE, True, True)
|
|
|
|
# input read registers/fields
|
|
self.__opPortDeclaration(wfp, catDict, regtype.READ, False, False)
|
|
|
|
# input read pulse registers/fields, and output pulse (per register)
|
|
self.__opPortDeclaration(wfp, catDict, regtype.READ_PULSE, True, False)
|
|
|
|
# Output read/write pulse registers/fields, and output pulse (per register)
|
|
self.__opPortDeclaration(wfp, catDict, regtype.READ_WRITE_PULSE, True, True)
|
|
|
|
# Output signal declarations for internal write registers, both
|
|
# register and next signals
|
|
elif tagStr == 'INTERNAL_WRITE_REG_SIGS' :
|
|
|
|
self.__opSignalDeclaration(wfp, catDict, 'signal ', '_reg')
|
|
self.__opSignalDeclaration(wfp, catDict, 'signal next_', '', MemMapperHdl.outputNameWidth-5)
|
|
|
|
elif tagStr == 'INTERNAL_WRITE_REG_EXPORTS' :
|
|
# Export write regs to ports
|
|
self.__opSignalAssignment(wfp, catDict, regtype.WRITE, '_reg')
|
|
|
|
# Export write0 regs to ports
|
|
self.__opSignalAssignment(wfp, catDict, regtype.WRITE_CLR0, '_reg')
|
|
|
|
# Export write pulse ports from avs_writedata bus
|
|
wfp.write('\n -- Export AVS write bus to write pulse register ports\n')
|
|
self.__opSignalAssignment(wfp, catDict, regtype.WRITE_PULSE, 'avs')
|
|
|
|
elif tagStr == 'INTERNAL_WRITE_REG_NEXT_DEFAULTS' :
|
|
self.__opSignalAssignment(wfp, catDict, regtype.WRITE, '_reg', ' next_')
|
|
|
|
elif tagStr == 'WRITE_CLR0_NEXT_DEFAULTS' :
|
|
self.__opSignalAssignment(wfp, catDict, regtype.WRITE_CLR0, 'w0default', ' next_')
|
|
|
|
elif tagStr == 'WRITE_PULSE_OUTPUTS_DEFAULT_0' :
|
|
self.__opSignalAssignment(wfp, catDict, regtype.WRITE_PULSE, '\'0\'', ' ',
|
|
True, False, MemMapperHdl.outputNameWidth+5)
|
|
self.__opSignalAssignment(wfp, catDict, regtype.READ_PULSE, '\'0\'', ' ',
|
|
True, False, MemMapperHdl.outputNameWidth+5)
|
|
self.__opSignalAssignment(wfp, catDict, regtype.READ_WRITE_PULSE, '\'0\'', ' ',
|
|
True, False, MemMapperHdl.outputNameWidth + 5, False)
|
|
self.__opSignalAssignment(wfp, catDict, regtype.READ_WRITE_PULSE, '\'0\'', ' ',
|
|
True, False, MemMapperHdl.outputNameWidth + 5, True)
|
|
|
|
elif tagStr == 'INTERNAL_WRITE_REG_RESETS' :
|
|
self.__opSignalAssignment(wfp, catDict, regtype.WRITE, 'reset', ' ', False, True,
|
|
MemMapperHdl.outputNameWidth + 5)
|
|
self.__opSignalAssignment(wfp, catDict, regtype.WRITE_CLR0, 'reset0', ' ',
|
|
False,
|
|
True,
|
|
MemMapperHdl.outputNameWidth + 5)
|
|
|
|
elif tagStr == 'INTERNAL_WRITE_REG_UPDATES' :
|
|
self.__opSignalAssignment(wfp, catDict, regtype.WRITE, 'next_', ' ', False,
|
|
True,
|
|
MemMapperHdl.outputNameWidth+5)
|
|
self.__opSignalAssignment(wfp, catDict, regtype.WRITE_CLR0, 'next_', ' ', False,
|
|
True,
|
|
MemMapperHdl.outputNameWidth + 5)
|
|
|
|
elif tagStr == 'WRITE_REG_CASE_STATEMENTS' :
|
|
self.__outputWriteCaseEntry(wfp, catDict)
|
|
|
|
elif tagStr == 'READ_CASE_STATEMENTS' :
|
|
self.__outputReadCaseEntry(wfp, catDict, MemMapperHdl.outputNameWidth + 5)
|
|
|
|
# Close the open files
|
|
wfp.close()
|
|
tfp.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
|
|
MemMapperHdl.processCmdLine(parser)
|
|
|
|
# Specific command line options added here
|
|
parser.add_argument('-p', '--project_name', dest='projName', default='UNKNOWN', action='store',
|
|
help='Set a project name for header')
|
|
|
|
return parser.parse_args()
|
|
|
|
# ###############################################################
|
|
# Only run if not imported
|
|
#
|
|
if __name__ == '__main__' :
|
|
|
|
# Process the command line options
|
|
cmdArgs = processCmdLine()
|
|
|
|
memMapHdl = MemMapperHdl(cmdArgs.projName)
|
|
|
|
# Run the program for VHDL RTL generation
|
|
memMapHdl.run(cmdArgs.topBlock, cmdArgs.busList, cmdArgs.repoDir, cmdArgs.opDir, cmdArgs.opFileSuffix, 'hdl', cmdArgs.jsonFile, cmdArgs.buildDir, cmdArgs.qsysSuffix)
|
|
del memMapHdl
|
|
|
|
memMapPkg = MemMapperPkg()
|
|
|
|
# Run the program for the package generation
|
|
memMapPkg.run(cmdArgs.topBlock, cmdArgs.busList, cmdArgs.repoDir, cmdArgs.opDir, cmdArgs.opFileSuffix, 'pkg', cmdArgs.jsonFile, cmdArgs.buildDir, cmdArgs.qsysSuffix)
|
|
del memMapPkg
|