X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.logic.model.am2900%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fexamples%2FVerilogExporter.java;h=fb1fd2f402c14058254d3c78a0a5e35c0ad044e6;hb=590c4809a069b0d30529735bf58f0db5382f8a88;hp=1f9c72f02fe2c39c3e82bf780a5bfc33d4d1ca81;hpb=3e740982394864e95786d33405fe1cad2583cead;p=Mograsim.git diff --git a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/VerilogExporter.java b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/VerilogExporter.java index 1f9c72f0..fb1fd2f4 100644 --- a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/VerilogExporter.java +++ b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/VerilogExporter.java @@ -18,6 +18,7 @@ import java.util.Set; import java.util.stream.Collectors; import com.google.gson.JsonElement; +import com.google.gson.JsonNull; import net.mograsim.logic.model.am2900.Am2900Loader; import net.mograsim.logic.model.model.LogicModelModifiable; @@ -228,7 +229,8 @@ public class VerilogExporter { return combinedInterfacePinsPerComponentID.entrySet().stream().collect(Collectors.toMap(Entry::getKey, e -> { - List names = e.getValue().values().stream().distinct().collect(Collectors.toList()); + List names = e.getValue().values().stream().distinct().sorted().collect(Collectors.toList()); + System.out.println("Assuming following order for interface pins of " + e.getKey() + ": " + names); Map widthesPerName = Arrays.stream(componentsById.get(e.getKey()).interfacePins) .collect(Collectors.toMap(p -> p.name, p -> p.logicWidth)); List widthes = names.stream().map(widthesPerName::get).collect(Collectors.toList()); @@ -304,13 +306,14 @@ public class VerilogExporter private void appendInterface(StringBuilder result) { + result.append("input rst, input clk"); if (!sortedInterfacePinNames.isEmpty()) { Map logicWidthsPerInterfacePinName = Arrays.stream(componentJson.interfacePins) .collect(Collectors.toMap(p -> p.name, p -> p.logicWidth)); - result.append('\n'); for (int i = 0; i < sortedInterfacePinNames.size(); i++) { + result.append(",\n"); String interfacePinName = sortedInterfacePinNames.get(i); int logicWidth = logicWidthsPerInterfacePinName.get(interfacePinName); @@ -324,10 +327,8 @@ public class VerilogExporter appendLogicWidth(result, logicWidth); result.append(sanitizeVerilog(interfacePinName)); result.append("_res"); - if (i != sortedInterfacePinNames.size() - 1) - result.append(','); - result.append('\n'); } + result.append('\n'); } } @@ -394,7 +395,12 @@ public class VerilogExporter result.append("assign "); result.append(sanitizeVerilog(resultWireName)); - result.append(" = "); + result.append(" = rst ? "); + result.append(logicWidth * 2); + result.append("'b"); + for (int i = 0; i < logicWidth; i++) + result.append("10"); + result.append(" : "); result.append(sanitizeVerilog(lastWireName)); result.append(";\n"); } @@ -420,10 +426,15 @@ public class VerilogExporter } result.append(COMPONENT_PREFIX); - result.append(sanitizeVerilog(subcomponentID + subcomponentParams.params)); - result.append(" ("); + String paramsString = subcomponentParams.params == JsonNull.INSTANCE ? "" : subcomponentParams.params.toString(); + result.append(sanitizeVerilog(subcomponentID + paramsString)); + result.append(' '); + // abuse the pinIdentifierGenerator for making these unique + result.append(pinIdentifierGenerator.getPinID("comp", subcomponentName)); + result.append(" (rst, clk"); for (int i = 0; i < subcomponentInterfacePinNames.size(); i++) { + result.append(",\n "); String innerPinID = pinIdentifierGenerator.getPinID(subcomponentName, subcomponentInterfacePinNames.get(i)); String lastWireName; @@ -447,13 +458,13 @@ public class VerilogExporter result.append(sanitizeVerilog(nextWireName)); result.append(", "); result.append(sanitizeVerilog(resultWireName)); - if (i != subcomponentInterfacePinNames.size() - 1) - result.append(", \n "); } result.append(");\n\n"); } } + private static Map, Tuple2, List>> atomicComponentInterfaces = new HashMap<>(); + private Tuple2, List> getSubcomponentInterfacePinNamesAndWidths(String subcomponentID, JsonElement subcomponentParams) { @@ -461,13 +472,23 @@ public class VerilogExporter if (result != null) return result; + Tuple2 subcomponentKey = new Tuple2<>(subcomponentID, subcomponentParams); + + result = atomicComponentInterfaces.get(subcomponentKey); + if (result != null) + return result; + Map pins = IndirectModelComponentCreator .createComponent(new LogicModelModifiable(), subcomponentID, subcomponentParams).getPins(); List names = pins.keySet().stream().sorted().collect(Collectors.toList()); List widthes = pins.entrySet().stream().sorted(Comparator.comparing(e -> e.getKey())).map(Entry::getValue) .map(p -> p.logicWidth).collect(Collectors.toList()); - System.out.println("Assuming following order for interface pins of " + subcomponentID + ": " + names); - return new Tuple2<>(names, widthes); + System.out.println( + "Assuming following order for interface pins of " + subcomponentID + " with params " + subcomponentParams + ": " + names); + result = new Tuple2<>(names, widthes); + + atomicComponentInterfaces.put(subcomponentKey, result); + return result; } private static void appendLogicWidth(StringBuilder result, int logicWidth) @@ -532,6 +553,41 @@ public class VerilogExporter this.e1 = e1; this.e2 = e2; } + + @Override + public int hashCode() + { + final int prime = 31; + int result = 1; + result = prime * result + ((e1 == null) ? 0 : e1.hashCode()); + result = prime * result + ((e2 == null) ? 0 : e2.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) + { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Tuple2 other = (Tuple2) obj; + if (e1 == null) + { + if (other.e1 != null) + return false; + } else if (!e1.equals(other.e1)) + return false; + if (e2 == null) + { + if (other.e2 != null) + return false; + } else if (!e2.equals(other.e2)) + return false; + return true; + } } private static String sanitizeVerilog(String str)