From 5d78711ccc0bf22b810af7df055f9c741b28e383 Mon Sep 17 00:00:00 2001 From: Simon Southwell Date: Thu, 30 Nov 2017 16:27:26 +0000 Subject: [PATCH] Initial check-in of script to parse json register map data, as a base for development --- README.txt | 1 + logic_memory_mapper/gen_pkg.py | 211 +++++++++++++++++++++++++++++++++ 2 files changed, 212 insertions(+) create mode 100644 README.txt create mode 100644 logic_memory_mapper/gen_pkg.py diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..c673a13 --- /dev/null +++ b/README.txt @@ -0,0 +1 @@ +Repository for internally developed firmware tools \ No newline at end of file diff --git a/logic_memory_mapper/gen_pkg.py b/logic_memory_mapper/gen_pkg.py new file mode 100644 index 0000000..5826148 --- /dev/null +++ b/logic_memory_mapper/gen_pkg.py @@ -0,0 +1,211 @@ +import sys +import os +import re +import git +import json + +from os import listdir +from os.path import isfile, join, isdir +from itertools import chain, count + +gblRootDir = "" +gblDocDirs = [] +gblJsonPaths = {} +gblBuildDirs = [] +gblQsysPaths = {} +gblQsysInsts = {} +gblBusArray = ['csr', 'seq'] # Currently hard wired +gblIpArray = [] + +# Find the GIT root directory relative to where you are + +def getRootDir(path): + global gblRootDir + gitRepo = git.Repo(path, search_parent_directories=True) + gitRoot = gitRepo.git.rev_parse("--show-toplevel") + gblRootDir = gitRoot + +# Find all the 'doc' directories which is where the JSON files will be found. + +def getDocDirs(): + global gblRootDir + global gblDocDirs + gblDocDirs = [x[0] for x in os.walk(gblRootDir) if x[0].endswith("doc") ] + +# Find the directory for a particular JSON file. + +def getJsonPath(blockName): + global gblDocDirs + fileName = ".".join([blockName, "json"]) + found = 0 + for idx in gblDocDirs: + fileList = os.listdir(idx) + if fileName in fileList: + totalPath = "/".join([idx,fileName]) + found = 1 + return totalPath + break + if found == 0: + print "Could not find: "+fileName + +# Go through the IP list and find the directories for all the JSON files. + +def getAllJsonPaths(): + global gblDocDirs + global gblIpArray + global gblJsonPaths + for dir in gblDocDirs: + fileList = os.listdir(dir) + for ip in gblIpArray: + fileName = ip+".json" + if fileName in fileList: + gblJsonPaths[ip] = "/".join([dir,fileName]) + +# Extract the dictionary of information from the JSON file + +def getJsonDict(blockName): + global gblJsonPaths + jsonFileName = gblJsonPaths[blockName] + jsonFile = open(jsonFileName) + jsonStr = jsonFile.read() + jsonData = json.loads(jsonStr) + return jsonData[0] + +# Find all the 'build' directories which is where will find QSYS files. + +def getBuildDirs(): + global gblRootDir + global gblBuildDirs + gblBuildDirs = [x[0] for x in os.walk(gblRootDir) if x[0].endswith("build") ] + +# Find which directory a particular QSYS file live in + +def getQsysPath(blockName): + fileName = blockName+".qsys" # Create a Qsys filename from the block name + found = 0 + for idx in gblBuildDirs: + fileList = os.listdir(idx) # Search amongst all the directories... + if fileName in fileList: # ...that contain Qsys files + totalPath = "/".join([idx,fileName]) + found = 1 + return totalPath + break + if found == 0: + print "Could not find: "+fileName + +# Search a QSYS file for references to 'baseAddress' and extract associated IP and bus information. + +def getQsysInsts(topBlock): + global gblQsysPaths + lineArray = ['a', 'b', 'c', 'd'] + instArray = [] + readFileName = gblQsysPaths[topBlock+"_system"] + readFile = open(readFileName, "r") + for line in readFile: + if (("end=" in lineArray[2]) and ("baseAddress" in line)): + for bus in gblBusArray: + testStr = "."+bus+"\"" + if testStr in lineArray[2]: + lineList = re.split(r'[".]', lineArray[2]) + block = lineList[1] + wordList = block.split("_") + ip = "_".join(wordList[:-1]) + instName = wordList[-1] + numList = re.split(r'["x]', line) + base = numList[-2] + tempArray = [bus, ip, instName, base] + instArray.append(tempArray) + lineArray = lineArray[1:] + lineArray.append(line) + return instArray + +# Store the IP blocks into an array. + +def processQsysInsts(arg): + global gblIpArray + gblIpArray = [] + for idx in range(len(arg)): + if arg[idx][1] not in gblIpArray: + gblIpArray.append(arg[idx][1]) + +# Generate a VHDL file with some useful constants in it. + +def genVhdlPkg(arg): + jsonPath = getJsonPath(arg) + tempPath = "/".join(jsonPath.split("/")[:-2]) + vhdlPath = tempPath+"/src/"+arg+"_pkg.vhd" + tempDict = getJsonDict(arg) + writeFileName = vhdlPath + writeFile = open(writeFileName, "w") + writeFile.write("-- Block name: "+tempDict["ip_name"]+"\n") + writeFile.write("-- And now for some register addresses...\n") + writeFile.write("library ieee;\n") + writeFile.write("use ieee.std_logic_1164.all;\n\n") + writeFile.write("package "+arg+"_pkg is\n") + regDict = tempDict["registers"] + regKeys = regDict.keys() + for idx in regKeys: + writeFile.write(" constant CSR_"+idx.upper()+"_ADDR : std_logic_vector(31 downto 0) := x\"") + writeFile.write(str(regDict[idx]["address"])+"\";\n") + writeFile.write("end package "+arg+"_pkg;\n") + writeFile.close() + +# Generate a C/C++ header file with some useful constants in it. + +def genCppHeader(arg): + jsonPath = getJsonPath(arg) + tempPath = "/".join(jsonPath.split("/")[:-2]) + cppPath = tempPath+"/src/"+arg+".h" + tempDict = getJsonDict(arg) + writeFileName = cppPath + writeFile = open(writeFileName, "w") + writeFile.write("// Block name: "+tempDict["ip_name"]+"\n") + writeFile.write("// And now for some register addresses...\n") + regDict = tempDict["registers"] + regKeys = regDict.keys() + for idx in regKeys: + writeFile.write("static const uint32 ") + tmpArg = arg.lower() + tmpList = tmpArg.split("_") + #print "C: "+str(tmpList) + capList = [x.capitalize() for x in tmpList] + #print "D: "+str(capList) + tmpStr = "".join(capList) + writeFile.write(tmpStr+"Csr") + writeFile.write(" = 0x"+str(regDict[idx]["address"])+";\n") + writeFile.close() + +# + +def processJsonInsts(): + global gblIpArray + for ip in gblIpArray: + genVhdlPkg(ip) + genCppHeader(ip) + +# MAIN + +def main(topBlock): + global gblQsysInsts + global gblIpArray + global gblJsonPaths + + print "Hello again!" + + getRootDir("test.py") # Find the root of the repository + getDocDirs() # Find all the places where JSON files might be. + getBuildDirs() # Find all the places where QSYS files might be. + tmp = getJsonPath(topBlock) # Find where the top level JSON file is + gblJsonPaths[topBlock] = tmp # Place this into the JSON dictionary + tmpDict = getJsonDict(topBlock) # Get the dictionary entry out again + tmpQsys = tmpDict['qsys'] # Extract the top level Qsys system + tmpPath = getQsysPath(tmpQsys) # Get the path of this Qsys system + gblQsysPaths[tmpQsys] = tmpPath # Place this into the Qsys dictionary + gblQsysInsts = getQsysInsts(topBlock) # Find all the subblock information in the Qsys system + processQsysInsts(gblQsysInsts) # Put these IP blocks into an array + getAllJsonPaths() # Find all the related JSON files + processJsonInsts() # Generate VHDL files from this JSON information + +# Run the program + +main("top")