Skip to content

Commit

Permalink
fixes to cheader output
Browse files Browse the repository at this point in the history
  • Loading branch information
sdnellen committed Jul 20, 2023
1 parent 4265b58 commit a2787b0
Show file tree
Hide file tree
Showing 37 changed files with 19,286 additions and 621 deletions.
3 changes: 1 addition & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
"type": "java",
"name": "Ordt",
"request": "launch",
"mainClass": "Ordt",
"mainClass": "ordt.extract.Ordt",
"projectName": "ordt"
}

]
}
19 changes: 19 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "java (build)",
"paths": [
"${workspace}"
],
"isFullBuild": true,
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [],
"label": "java (build): Build Workspace",
"detail": "$(tools) Build all the Java projects in workspace."
}
]
}
206 changes: 91 additions & 115 deletions src/ordt/output/cheader/CHeaderBuilder.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
package ordt.output.cheader;

import ordt.extract.RegNumber;
import ordt.output.FieldProperties;
import ordt.output.OutputBuilder;
import ordt.output.common.OutputLine;
import ordt.parameters.ExtParameters;

import java.io.BufferedWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class CHeaderBuilder extends OutputBuilder {
private List<OutputLine> memoryMapOutputList = new ArrayList<OutputLine>();
private List<OutputLine> bitfieldOutputList = new ArrayList<OutputLine>();
private List<OutputLine> commonOutputList = new ArrayList<OutputLine>();
private List<OutputLine> explicitFunctionOutputList = new ArrayList<OutputLine>();
private int indentLvl = 0;
private final int noIndent = 0;

/*******************************************************************************************************************
* User configurable parameters
******************************************************************************************************************/

/*******************************************************************************************************************
* Internal variables
******************************************************************************************************************/

public CHeaderBuilder(ordt.extract.RegModelIntf model) {
setBaseBuilderID(); // set unique ID of this instance
this.model = model;
Expand All @@ -37,21 +27,72 @@ public CHeaderBuilder(ordt.extract.RegModelIntf model) {
model.getRoot().generateOutput(null, this); // generate output structures recursively starting at model root
}

public class MemoryMapEntry {
public RegNumber address;
public String regName;
public MemoryMapEntry(RegNumber address, String regName) {
this.address = address;
this.regName = regName;
}
}
private List<MemoryMapEntry> memoryMapEntryList = new ArrayList<MemoryMapEntry>();

String explicitFunctionString =
"/*\n" +
" * bits.h\n" +
" *\n" +
" * Struct and function declarations for dealing with bit assignment.\n" +
" */\n" +
"\n" +
"#ifndef _BITS_H\n" +
"#define _BITS_H\n" +
"\n" +
"#define BITS_PER_LONG 32\n" +
"\n" +
"// ## allows token concatenation\n" +
"//X = 1 and Y = 10 would return 110\n" +
"#define __AC(X,Y)\t(X##Y)\n" +
"#define _AC(X,Y)\t__AC(X,Y)\n" +
"\n" +
"#define _UL(x)\t\t(_AC(x, UL))\n" +
"#define UL(x)\t\t(_UL(x))\n" +
"\n" +
"#define BIT(nr) (1UL << (nr))\n" +
"// BIT defines a bit mask for the specified bit number from 0 to whatever fits into an unsigned long\n" +
"// so BIT(10) should evaluate to decimal 1024 (which is binary 1 left shifted by 10 bits)\n" +
"\n" +
"#define GENMASK_INPUT_CHECK(h, l) 0\n" +
"\n" +
"// h is high index, l is low index in a bitfield\n" +
"// __GENMASK returns 32 bit number with 1s in the h-to-l field\n" +
"// if h = 4 and l = 1, __GENMASK would return 00000000000000000000000000011110\n" +
"#define __GENMASK(h, l) \\\n" +
"\t(((~UL(0)) - (UL(1) << (l)) + 1) & \\\n" +
"\t (~UL(0) >> (BITS_PER_LONG - 1 - (h))))\n" +
"\n" +
"#define GENMASK(h, l) \\\n" +
"\t(GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))\n" +
"\n" +
"#endif /* _BITS_H */\n";

/*******************************************************************************************************************
* Builder override methods
******************************************************************************************************************/

@Override
public void addField() {
while (fieldList.size() > 0){
FieldProperties field = fieldList.remove();
int lowIndex = field.getLowIndex();
int highIndex = field.getLowIndex() + field.getFieldWidth() - 1;

if (lowIndex == highIndex) {
String bitIndices = String.format("%d", lowIndex);
bitfieldOutputList.add(new OutputLine(noIndent, String.format("#define %s BIT(%s)", field.getTextName(), bitIndices)));
}
if (lowIndex != highIndex) {
String bitIndices = String.format("%d, %d", highIndex, lowIndex);
bitfieldOutputList.add(new OutputLine(noIndent, String.format("#define %s GENMASK(%s)", field.getTextName(), bitIndices)));
}
int lowIndex = fieldProperties.getLowIndex();
int highIndex = fieldProperties.getLowIndex() + fieldProperties.getFieldWidth() - 1;
String fieldName = fieldProperties.getBaseName().toUpperCase();
String textNameComment = (fieldProperties.getTextName() == null) ? "" : " /* " + fieldProperties.getTextName() + " */";

if (lowIndex == highIndex) {
String bitIndices = String.format("%d", lowIndex);
bitfieldOutputList.add(new OutputLine(noIndent, String.format("#define %s BIT(%s)%s", fieldName, bitIndices, textNameComment)));
}
else {
String bitIndices = String.format("%d, %d", highIndex, lowIndex);
bitfieldOutputList.add(new OutputLine(noIndent, String.format("#define %s GENMASK(%s)%s", fieldName, bitIndices, textNameComment)));
}
}
@Override
Expand All @@ -60,14 +101,15 @@ public void addAliasField() {

@Override
public void addRegister() {
String regAddress = regProperties.getExtractInstance().getAddress().toString();
String regName = regProperties.getTextName();

if (ExtParameters.cheaderAddMemoryMap())
memoryMapOutputList.add(new OutputLine(indentLvl, String.format("%s = %s,", regName, regAddress)));

if (ExtParameters.cheaderAddBitfields())
bitfieldOutputList.add(new OutputLine(noIndent, String.format("\n/* %s registers */", regName)));
String regName = regProperties.getBaseName().toUpperCase();

if (ExtParameters.cheaderAddMemoryMap())
memoryMapEntryList.add(new MemoryMapEntry(regProperties.getBaseAddress(), regName));

if (ExtParameters.cheaderAddBitfields()) {
String textName = (regProperties.getTextName() == null) ? "" : " (" + regProperties.getTextName() + ")";
bitfieldOutputList.add(new OutputLine(noIndent, "\n/* " + regName + textName + " register fields */"));
}
}

@Override
Expand All @@ -83,109 +125,43 @@ public void finishRegSet() {

@Override
public void addRegMap() {
if (ExtParameters.cheaderAddMemoryMap())
addHeader();
}
@Override
public void finishRegMap() {
if (ExtParameters.cheaderAddMemoryMap())
endEnum();
}

@Override
public void write(BufferedWriter bw) {
bufferedWriter = bw;

// Comments about auto generated file with name and date
addComments();
for (OutputLine jsLine : commonOutputList){
writeStmt(jsLine.getIndent(), jsLine.getLine());
}
writeStmt(0, String.format("#ifndef __%s_REGISTER_MAP__", getAddressMapName().toUpperCase()));
writeStmt(0, String.format("#define __%s_REGISTER_MAP__\n", getAddressMapName().toUpperCase()));

// Explicitly declare all functions in the same header file
if (ExtParameters.cheaderExplicitFunctions()){
explicitFunctions();
for (OutputLine jsLine : explicitFunctionOutputList){
writeStmt(jsLine.getIndent(), jsLine.getLine());
}
}
else writeStmt(noIndent,"#include <bits.h>\n");
if (ExtParameters.cheaderExplicitFunctions())
writeStmt(noIndent, explicitFunctionString);
else writeStmt(noIndent, "#include <bits.h>\n");

// Write memory map (enum)
if (ExtParameters.cheaderAddMemoryMap())
// sherlock: memoryMapOutputList is array, OutputLine is datatype for temp variable jsLine
// sherlock: loop iterates over array and jsline becomes each element
for (OutputLine jsLine : memoryMapOutputList) {
writeStmt(jsLine.getIndent(), jsLine.getLine());
if (ExtParameters.cheaderAddMemoryMap()) {
writeStmt(0, String.format("/* %s_REGISTERS memory map */", getAddressMapName().toUpperCase()));
writeStmt(0, String.format("enum %s_REGS {", getAddressMapName().toUpperCase()));
Iterator<MemoryMapEntry> mapIter = memoryMapEntryList.iterator();
while (mapIter.hasNext()) {
MemoryMapEntry mapEntry = mapIter.next();
String suffix = (mapIter.hasNext())? "," : "";
writeStmt(1, mapEntry.regName + " = " + mapEntry.address.toString() + suffix);
}
writeStmt(0, "};");
}

// Write bitfields (#define)
if (ExtParameters.cheaderAddBitfields())
for (OutputLine jsLine : bitfieldOutputList) {
writeStmt(jsLine.getIndent(), jsLine.getLine());
}

endComments();
}

/*******************************************************************************************************************
* Builder specific methods
******************************************************************************************************************/
void addComments() {
commonOutputList.add(new OutputLine(indentLvl, String.format("#ifndef __%s_REGISTER_MAP__", getAddressMapName().toUpperCase())));
commonOutputList.add(new OutputLine(indentLvl, String.format("#define __%s_REGISTER_MAP__\n", getAddressMapName().toUpperCase())));
}

void addHeader() {
memoryMapOutputList.add(new OutputLine(indentLvl, String.format("/* %s_REGISTERS memory map */", getAddressMapName().toUpperCase())));
memoryMapOutputList.add(new OutputLine(indentLvl++, String.format("enum %s_REGS {", getAddressMapName().toUpperCase())));
}

void endEnum(){
memoryMapOutputList.add(new OutputLine(--indentLvl, "};"));
}

void endComments() {
commonOutputList.add(new OutputLine(indentLvl, "#endif"));
writeStmt(noIndent, "\n#endif");
}

void explicitFunctions(){
explicitFunctionOutputList.add(new OutputLine(indentLvl, String.format("/*\n" +
" * bits.h\n" +
" *\n" +
" * Struct and function declarations for dealing with bit assignment.\n" +
" */\n" +
"\n" +
"#ifndef _BITS_H\n" +
"#define _BITS_H\n" +
"\n" +
"#define BITS_PER_LONG 32\n" +
"\n" +
"// ## allows token concatenation\n" +
"//X = 1 and Y = 10 would return 110\n" +
"#define __AC(X,Y)\t(X##Y)\n" +
"#define _AC(X,Y)\t__AC(X,Y)\n" +
"\n" +
"#define _UL(x)\t\t(_AC(x, UL))\n" +
"#define UL(x)\t\t(_UL(x))\n" +
"\n" +
"#define BIT(nr) (1UL << (nr))\n" +
"// BIT defines a bit mask for the specified bit number from 0 to whatever fits into an unsigned long\n" +
"// so BIT(10) should evaluate to decimal 1024 (which is binary 1 left shifted by 10 bits)\n" +
"\n" +
"#define GENMASK_INPUT_CHECK(h, l) 0\n" +
"\n" +
"// h is high index, l is low index in a bitfield\n" +
"// __GENMASK returns 32 bit number with 1s in the h-to-l field\n" +
"// if h = 4 and l = 1, __GENMASK would return 00000000000000000000000000011110\n" +
"#define __GENMASK(h, l) \\\n" +
"\t(((~UL(0)) - (UL(1) << (l)) + 1) & \\\n" +
"\t (~UL(0) >> (BITS_PER_LONG - 1 - (h))))\n" +
"\n" +
"#define GENMASK(h, l) \\\n" +
"\t(GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))\n" +
"\n" +
"#endif /* _BITS_H */")));
}

}
Loading

0 comments on commit a2787b0

Please sign in to comment.