Default code for generic circuits is set.

This commit is contained in:
hneemann 2019-07-06 14:13:33 +02:00
parent 52968d013d
commit 55c5ec171a
8 changed files with 156 additions and 46 deletions

View File

@ -18,11 +18,15 @@ import de.neemann.digital.draw.model.ModelCreator;
import de.neemann.digital.draw.model.NetList; import de.neemann.digital.draw.model.NetList;
import de.neemann.digital.hdl.hgs.*; import de.neemann.digital.hdl.hgs.*;
import de.neemann.digital.hdl.hgs.function.Function; import de.neemann.digital.hdl.hgs.function.Function;
import de.neemann.digital.hdl.hgs.refs.Reference;
import de.neemann.digital.hdl.hgs.refs.ReferenceToStruct;
import de.neemann.digital.hdl.hgs.refs.ReferenceToVar;
import de.neemann.digital.lang.Lang; import de.neemann.digital.lang.Lang;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.TreeSet;
/** /**
* The description of a nested element. * The description of a nested element.
@ -36,6 +40,7 @@ public final class ElementTypeDescriptionCustom extends ElementTypeDescription {
private String description; private String description;
private NetList netList; private NetList netList;
private boolean isCustom = true; private boolean isCustom = true;
private String declarationDefault;
/** /**
* Creates a new element * Creates a new element
@ -53,7 +58,7 @@ public final class ElementTypeDescriptionCustom extends ElementTypeDescription {
addAttribute(Keys.ROTATE); addAttribute(Keys.ROTATE);
addAttribute(Keys.LABEL); addAttribute(Keys.LABEL);
addAttribute(Keys.SHAPE_TYPE); addAttribute(Keys.SHAPE_TYPE);
if (circuit.getAttributes().get(Keys.IS_GENERIC)) if (isGeneric())
addAttribute(Keys.GENERIC); addAttribute(Keys.GENERIC);
} }
@ -118,7 +123,7 @@ public final class ElementTypeDescriptionCustom extends ElementTypeDescription {
if (depth > MAX_DEPTH) if (depth > MAX_DEPTH)
throw new NodeException(Lang.get("err_recursiveNestingAt_N0", circuit.getOrigin())); throw new NodeException(Lang.get("err_recursiveNestingAt_N0", circuit.getOrigin()));
if (circuit.getAttributes().get(Keys.IS_GENERIC)) { if (isGeneric()) {
try { try {
Context args; Context args;
if (containingVisualElement != null) { if (containingVisualElement != null) {
@ -201,4 +206,51 @@ public final class ElementTypeDescriptionCustom extends ElementTypeDescription {
isCustom = false; isCustom = false;
return this; return this;
} }
/**
* @return the generics field default value
*/
public String getDeclarationDefault() {
if (declarationDefault == null)
declarationDefault = createDeclarationDefault();
return declarationDefault;
}
private String createDeclarationDefault() {
TreeSet<String> nameSet = new TreeSet<>();
for (VisualElement ve : circuit.getElements()) {
String gen = ve.getElementAttributes().get(Keys.GENERIC).trim();
try {
if (!gen.isEmpty()) {
Parser p = new Parser(gen);
p.enableRefReadCollection();
p.parse(false);
for (Reference r : p.getRefsRead()) {
if (r instanceof ReferenceToStruct) {
ReferenceToStruct st = (ReferenceToStruct) r;
if (st.getParent() instanceof ReferenceToVar) {
ReferenceToVar var = (ReferenceToVar) st.getParent();
if (var.getName().equals("args")) {
nameSet.add(st.getName());
}
}
}
}
}
} catch (ParserException | IOException e) {
return "";
}
}
StringBuilder sb = new StringBuilder();
for (String name : nameSet)
sb.append(name).append(" := ;\n");
return sb.toString();
}
/**
* @return true if the circuit is generic
*/
public boolean isGeneric() {
return circuit.getAttributes().get(Keys.IS_GENERIC);
}
} }

View File

@ -1023,10 +1023,19 @@ public class CircuitComponent extends JComponent implements ChangedListener, Lib
try { try {
ArrayList<Key> list = getAttributeList(element); ArrayList<Key> list = getAttributeList(element);
if (list.size() > 0) { if (list.size() > 0) {
ElementTypeDescription elementType = library.getElementType(element.getElementName());
if (elementType instanceof ElementTypeDescriptionCustom) {
ElementTypeDescriptionCustom customDescr = (ElementTypeDescriptionCustom) elementType;
if (customDescr.isGeneric()) {
if (element.getElementAttributes().get(Keys.GENERIC).isEmpty())
element.getElementAttributes().set(Keys.GENERIC, customDescr.getDeclarationDefault());
}
}
Point p = new Point(e.getX(), e.getY()); Point p = new Point(e.getX(), e.getY());
SwingUtilities.convertPointToScreen(p, CircuitComponent.this); SwingUtilities.convertPointToScreen(p, CircuitComponent.this);
AttributeDialog attributeDialog = new AttributeDialog(parent, p, list, element.getElementAttributes()).setVisualElement(element); AttributeDialog attributeDialog = new AttributeDialog(parent, p, list, element.getElementAttributes()).setVisualElement(element);
ElementTypeDescription elementType = library.getElementType(element.getElementName());
if (elementType instanceof ElementTypeDescriptionCustom) { if (elementType instanceof ElementTypeDescriptionCustom) {
attributeDialog.addButton(Lang.get("attr_openCircuitLabel"), new ToolTipAction(Lang.get("attr_openCircuit")) { attributeDialog.addButton(Lang.get("attr_openCircuitLabel"), new ToolTipAction(Lang.get("attr_openCircuit")) {
@Override @Override

View File

@ -5,6 +5,7 @@
*/ */
package de.neemann.digital.hdl.hgs; package de.neemann.digital.hdl.hgs;
import de.neemann.digital.builder.tt2.TT2Exporter;
import de.neemann.digital.core.Bits; import de.neemann.digital.core.Bits;
import de.neemann.digital.hdl.hgs.function.FirstClassFunction; import de.neemann.digital.hdl.hgs.function.FirstClassFunction;
import de.neemann.digital.hdl.hgs.function.FirstClassFunctionCall; import de.neemann.digital.hdl.hgs.function.FirstClassFunctionCall;
@ -55,6 +56,7 @@ public class Parser {
} }
private ArrayList<Reference> refRead;
private final Tokenizer tok; private final Tokenizer tok;
private Context staticContext; private Context staticContext;
@ -78,6 +80,20 @@ public class Parser {
staticContext = new Context(); staticContext = new Context();
} }
/**
* If called all read references are collected.
*/
public void enableRefReadCollection() {
refRead = new ArrayList<>();
}
/**
* @return returns the references read
*/
public ArrayList<Reference> getRefsRead() {
return refRead;
}
private Statement lino(Statement statement) { private Statement lino(Statement statement) {
if (statement instanceof StatementWithLine) if (statement instanceof StatementWithLine)
return statement; return statement;
@ -517,6 +533,8 @@ public class Parser {
case IDENT: case IDENT:
String name = tok.getIdent(); String name = tok.getIdent();
Reference r = parseReference(name); Reference r = parseReference(name);
if (refRead != null)
refRead.add(r);
return r::get; return r::get;
case NUMBER: case NUMBER:
long num = convToLong(tok.getIdent()); long num = convToLong(tok.getIdent());

View File

@ -53,4 +53,17 @@ public class ReferenceToStruct implements Reference {
return value; return value;
} }
/**
* @return the parent reference
*/
public Reference getParent() {
return parent;
}
/**
* @return the struct field name
*/
public String getName() {
return name;
}
} }

View File

@ -42,4 +42,11 @@ public class ReferenceToVar implements Reference {
public Object get(Context context) throws HGSEvalException { public Object get(Context context) throws HGSEvalException {
return context.getVar(name); return context.getVar(name);
} }
/**
* @return the var name
*/
public String getName() {
return name;
}
} }

View File

@ -15,7 +15,7 @@
<int>16</int> <int>16</int>
</entry> </entry>
</elementAttributes> </elementAttributes>
<pos x="460" y="260"/> <pos x="460" y="240"/>
</visualElement> </visualElement>
<visualElement> <visualElement>
<elementName>In</elementName> <elementName>In</elementName>
@ -80,8 +80,11 @@ end loop
# read data # read data
loop(n,1&lt;&lt;8) loop(n,1&lt;&lt;8)
C 0 (n) 0 (n+2) 0 0 (n) 0 (n+2)
end loop</dataString> end loop
0 0 (1&lt;&lt;8) 0 (2)
</dataString>
</testData> </testData>
</entry> </entry>
</elementAttributes> </elementAttributes>
@ -92,8 +95,8 @@ end loop</dataString>
<elementAttributes> <elementAttributes>
<entry> <entry>
<string>generic</string> <string>generic</string>
<string>bits:=int(16); <string>dataBits := 16;
addr:=8;</string> addrBits := 8;</string>
</entry> </entry>
</elementAttributes> </elementAttributes>
<pos x="520" y="260"/> <pos x="520" y="260"/>
@ -105,8 +108,8 @@ addr:=8;</string>
<p2 x="520" y="320"/> <p2 x="520" y="320"/>
</wire> </wire>
<wire> <wire>
<p1 x="460" y="260"/> <p1 x="460" y="240"/>
<p2 x="520" y="260"/> <p2 x="480" y="240"/>
</wire> </wire>
<wire> <wire>
<p1 x="460" y="340"/> <p1 x="460" y="340"/>
@ -116,6 +119,10 @@ addr:=8;</string>
<p1 x="500" y="340"/> <p1 x="500" y="340"/>
<p2 x="520" y="340"/> <p2 x="520" y="340"/>
</wire> </wire>
<wire>
<p1 x="480" y="260"/>
<p2 x="520" y="260"/>
</wire>
<wire> <wire>
<p1 x="480" y="280"/> <p1 x="480" y="280"/>
<p2 x="520" y="280"/> <p2 x="520" y="280"/>
@ -140,6 +147,10 @@ addr:=8;</string>
<p1 x="480" y="320"/> <p1 x="480" y="320"/>
<p2 x="480" y="340"/> <p2 x="480" y="340"/>
</wire> </wire>
<wire>
<p1 x="480" y="240"/>
<p2 x="480" y="260"/>
</wire>
<wire> <wire>
<p1 x="500" y="340"/> <p1 x="500" y="340"/>
<p2 x="500" y="380"/> <p2 x="500" y="380"/>

View File

@ -13,7 +13,7 @@
<elementAttributes> <elementAttributes>
<entry> <entry>
<string>generic</string> <string>generic</string>
<string>this.Bits=args.bits;</string> <string>this.Bits=int(args.dataBits);</string>
</entry> </entry>
</elementAttributes> </elementAttributes>
<pos x="560" y="140"/> <pos x="560" y="140"/>
@ -23,7 +23,7 @@
<elementAttributes> <elementAttributes>
<entry> <entry>
<string>generic</string> <string>generic</string>
<string>this.Bits=args.bits;</string> <string>this.Bits=int(args.dataBits);</string>
</entry> </entry>
</elementAttributes> </elementAttributes>
<pos x="560" y="260"/> <pos x="560" y="260"/>
@ -33,7 +33,7 @@
<elementAttributes> <elementAttributes>
<entry> <entry>
<string>generic</string> <string>generic</string>
<string>this.Bits=args.bits;</string> <string>this.Bits=int(args.dataBits);</string>
</entry> </entry>
</elementAttributes> </elementAttributes>
<pos x="660" y="160"/> <pos x="660" y="160"/>

View File

@ -68,7 +68,7 @@
<entry> <entry>
<string>generic</string> <string>generic</string>
<string>if (isPresent(args)) { <string>if (isPresent(args)) {
this.Bits=args.bits; this.Bits=int(args.dataBits);
}</string> }</string>
</entry> </entry>
</elementAttributes> </elementAttributes>
@ -106,12 +106,12 @@
<string>generic</string> <string>generic</string>
<string>if (isPresent(args)) { <string>if (isPresent(args)) {
if (args.addr&lt;2) { if (args.addrBits&lt;2) {
panic(&quot;at least two address bits are necessary!&quot;); panic(&quot;at least two address bits are necessary!&quot;);
} }
this.&apos;Input Splitting&apos;=&quot;&quot;+args.addr; this.&apos;Input Splitting&apos;=&quot;&quot;+args.addrBits;
this.&apos;Output Splitting&apos;=(args.addr-1)+&quot;,1&quot;; this.&apos;Output Splitting&apos;=(args.addrBits-1)+&quot;,1&quot;;
}</string> }</string>
</entry> </entry>
</elementAttributes> </elementAttributes>
@ -123,13 +123,13 @@
<entry> <entry>
<string>generic</string> <string>generic</string>
<string>if (isPresent(args)) { <string>if (isPresent(args)) {
export bits:=args.bits; export dataBits:=args.dataBits;
export addr:=args.addr-1; export addrBits:=args.addrBits-1;
if (args.addr&gt;2) { if (args.addrBits&gt;2) {
setCircuit(&quot;memNode.dig&quot;); setCircuit(&quot;memNode.dig&quot;);
} }
} else { } else {
export bits:=int(1); export dataBits:=int(1);
}</string> }</string>
</entry> </entry>
</elementAttributes> </elementAttributes>
@ -141,13 +141,13 @@
<entry> <entry>
<string>generic</string> <string>generic</string>
<string>if (isPresent(args)) { <string>if (isPresent(args)) {
export bits:=args.bits; export dataBits:=args.dataBits;
export addr:=args.addr-1; export addrBits:=args.addrBits-1;
if (args.addr&gt;2) { if (args.addrBits&gt;2) {
setCircuit(&quot;memNode.dig&quot;); setCircuit(&quot;memNode.dig&quot;);
} }
} else { } else {
export bits:=int(1); export dataBits:=int(1);
}</string> }</string>
</entry> </entry>
</elementAttributes> </elementAttributes>
@ -161,14 +161,14 @@
</wire> </wire>
<wire> <wire>
<p1 x="300" y="320"/> <p1 x="300" y="320"/>
<p2 x="360" y="320"/> <p2 x="340" y="320"/>
</wire> </wire>
<wire> <wire>
<p1 x="320" y="160"/> <p1 x="320" y="160"/>
<p2 x="520" y="160"/> <p2 x="520" y="160"/>
</wire> </wire>
<wire> <wire>
<p1 x="360" y="420"/> <p1 x="340" y="420"/>
<p2 x="640" y="420"/> <p2 x="640" y="420"/>
</wire> </wire>
<wire> <wire>
@ -185,10 +185,10 @@
</wire> </wire>
<wire> <wire>
<p1 x="240" y="200"/> <p1 x="240" y="200"/>
<p2 x="340" y="200"/> <p2 x="360" y="200"/>
</wire> </wire>
<wire> <wire>
<p1 x="340" y="200"/> <p1 x="360" y="200"/>
<p2 x="400" y="200"/> <p2 x="400" y="200"/>
</wire> </wire>
<wire> <wire>
@ -212,7 +212,7 @@
<p2 x="520" y="300"/> <p2 x="520" y="300"/>
</wire> </wire>
<wire> <wire>
<p1 x="360" y="240"/> <p1 x="340" y="240"/>
<p2 x="380" y="240"/> <p2 x="380" y="240"/>
</wire> </wire>
<wire> <wire>
@ -224,7 +224,7 @@
<p2 x="520" y="340"/> <p2 x="520" y="340"/>
</wire> </wire>
<wire> <wire>
<p1 x="340" y="340"/> <p1 x="360" y="340"/>
<p2 x="400" y="340"/> <p2 x="400" y="340"/>
</wire> </wire>
<wire> <wire>
@ -244,7 +244,7 @@
<p2 x="620" y="220"/> <p2 x="620" y="220"/>
</wire> </wire>
<wire> <wire>
<p1 x="360" y="380"/> <p1 x="340" y="380"/>
<p2 x="400" y="380"/> <p2 x="400" y="380"/>
</wire> </wire>
<wire> <wire>
@ -268,25 +268,25 @@
<p2 x="500" y="440"/> <p2 x="500" y="440"/>
</wire> </wire>
<wire> <wire>
<p1 x="340" y="200"/> <p1 x="340" y="240"/>
<p2 x="340" y="340"/> <p2 x="340" y="320"/>
</wire>
<wire>
<p1 x="340" y="380"/>
<p2 x="340" y="420"/>
</wire>
<wire>
<p1 x="340" y="320"/>
<p2 x="340" y="380"/>
</wire>
<wire>
<p1 x="360" y="200"/>
<p2 x="360" y="340"/>
</wire> </wire>
<wire> <wire>
<p1 x="600" y="220"/> <p1 x="600" y="220"/>
<p2 x="600" y="320"/> <p2 x="600" y="320"/>
</wire> </wire>
<wire>
<p1 x="360" y="240"/>
<p2 x="360" y="320"/>
</wire>
<wire>
<p1 x="360" y="380"/>
<p2 x="360" y="420"/>
</wire>
<wire>
<p1 x="360" y="320"/>
<p2 x="360" y="380"/>
</wire>
</wires> </wires>
<measurementOrdering/> <measurementOrdering/>
</circuit> </circuit>