diff --git a/src/main/java/de/neemann/digital/hdl/verilog2/VerilogRenaming.java b/src/main/java/de/neemann/digital/hdl/verilog2/VerilogRenaming.java index 2cadf5230..0c61ef5cf 100644 --- a/src/main/java/de/neemann/digital/hdl/verilog2/VerilogRenaming.java +++ b/src/main/java/de/neemann/digital/hdl/verilog2/VerilogRenaming.java @@ -37,11 +37,17 @@ public class VerilogRenaming implements HDLModel.Renaming { public String checkName(String name) { if (isKeyword(name) || !isFirstCharValid(name)) // Escaped identifier, the space is part of the identifier. - return "\\" + name + " "; + return "\\" + replaceWhitespace(name) + " "; else return cleanName(name); } + private String replaceWhitespace(String name) { + return name + .replace(' ', '_') + .replace('\t', '_'); + } + private boolean isFirstCharValid(String name) { char c = name.charAt(0); @@ -69,6 +75,10 @@ public class VerilogRenaming implements HDLModel.Renaming { switch (c) { case '\\': break; + case '\t': + case ' ': + sb.append("_"); + break; case '/': case '!': case '~': diff --git a/src/test/java/de/neemann/digital/hdl/verilog2/VerilogGeneratorTest.java b/src/test/java/de/neemann/digital/hdl/verilog2/VerilogGeneratorTest.java index 27633d54a..797d287e5 100644 --- a/src/test/java/de/neemann/digital/hdl/verilog2/VerilogGeneratorTest.java +++ b/src/test/java/de/neemann/digital/hdl/verilog2/VerilogGeneratorTest.java @@ -14,7 +14,7 @@ public class VerilogGeneratorTest extends TestCase { public void testComb() throws Exception { ToBreakRunner br = new ToBreakRunner("dig/hdl/model2/comb.dig"); CodePrinterStr out = new CodePrinterStr(); - VerilogGenerator gen = new VerilogGenerator(br.getLibrary(), out).export(br.getCircuit()); + new VerilogGenerator(br.getLibrary(), out).export(br.getCircuit()); assertEquals("/*\n" + " * Generated by Digital. Don't modify this file!\n" @@ -77,7 +77,7 @@ public class VerilogGeneratorTest extends TestCase { public void testSplitter3() throws Exception { ToBreakRunner br = new ToBreakRunner("dig/hdl/model2/splitter3.dig"); CodePrinterStr out = new CodePrinterStr(); - VerilogGenerator gen = new VerilogGenerator(br.getLibrary(), out).export(br.getCircuit()); + new VerilogGenerator(br.getLibrary(), out).export(br.getCircuit()); assertEquals("/*\n" + " * Generated by Digital. Don't modify this file!\n" @@ -97,7 +97,7 @@ public class VerilogGeneratorTest extends TestCase { public void testSplitter2() throws Exception { ToBreakRunner br = new ToBreakRunner("dig/hdl/model2/splitter2.dig"); CodePrinterStr out = new CodePrinterStr(); - VerilogGenerator gen = new VerilogGenerator(br.getLibrary(), out).export(br.getCircuit()); + new VerilogGenerator(br.getLibrary(), out).export(br.getCircuit()); assertEquals("/*\n" + " * Generated by Digital. Don't modify this file!\n" @@ -121,7 +121,7 @@ public class VerilogGeneratorTest extends TestCase { public void testSplitter2I() throws Exception { ToBreakRunner br = new ToBreakRunner("dig/hdl/splitter2.dig"); CodePrinterStr out = new CodePrinterStr(); - VerilogGenerator gen = new VerilogGenerator(br.getLibrary(), out).export(br.getCircuit()); + new VerilogGenerator(br.getLibrary(), out).export(br.getCircuit()); assertEquals("/*\n" + " * Generated by Digital. Don't modify this file!\n" @@ -171,4 +171,35 @@ public class VerilogGeneratorTest extends TestCase { "endmodule\n", out.toString()); } + public void testNames() throws Exception { + ToBreakRunner br = new ToBreakRunner("dig/hdl_names/main.dig"); + CodePrinterStr out = new CodePrinterStr(); + new VerilogGenerator(br.getLibrary(), out).export(br.getCircuit()); + + assertEquals("/*\n" + + " * Generated by Digital. Don't modify this file!\n" + + " * Any changes will be lost if this file is regenerated.\n" + + " */\n" + + "\n" + + "module a_b (\n" + + " input A,\n" + + " input B,\n" + + " output Y\n" + + ");\n" + + " assign Y = (A & B);\n" + + "endmodule\n" + + "\n" + + "module main (\n" + + " input A,\n" + + " input B,\n" + + " output Y\n" + + ");\n" + + " a_b a_b_i0 (\n" + + " .A( A ),\n" + + " .B( B ),\n" + + " .Y( Y )\n" + + " );\n" + + "endmodule\n", out.toString()); + } + } \ No newline at end of file diff --git a/src/test/java/de/neemann/digital/hdl/vhdl2/VHDLGeneratorTest.java b/src/test/java/de/neemann/digital/hdl/vhdl2/VHDLGeneratorTest.java index cebf74a80..18add8fcd 100644 --- a/src/test/java/de/neemann/digital/hdl/vhdl2/VHDLGeneratorTest.java +++ b/src/test/java/de/neemann/digital/hdl/vhdl2/VHDLGeneratorTest.java @@ -14,7 +14,7 @@ public class VHDLGeneratorTest extends TestCase { public void testComb() throws Exception { ToBreakRunner br = new ToBreakRunner("dig/hdl/model2/comb.dig"); CodePrinterStr out = new CodePrinterStr(); - VHDLGenerator gen = new VHDLGenerator(br.getLibrary(), out).export(br.getCircuit()); + new VHDLGenerator(br.getLibrary(), out).export(br.getCircuit()); assertEquals("-- generated by Digital. Don't modify this file!\n" + "-- Any changes will be lost if this file is regenerated.\n" + @@ -85,7 +85,7 @@ public class VHDLGeneratorTest extends TestCase { public void testSplitter3() throws Exception { ToBreakRunner br = new ToBreakRunner("dig/hdl/model2/splitter3.dig"); CodePrinterStr out = new CodePrinterStr(); - VHDLGenerator gen = new VHDLGenerator(br.getLibrary(), out).export(br.getCircuit()); + new VHDLGenerator(br.getLibrary(), out).export(br.getCircuit()); assertEquals("-- generated by Digital. Don't modify this file!\n" + "-- Any changes will be lost if this file is regenerated.\n" + @@ -111,7 +111,7 @@ public class VHDLGeneratorTest extends TestCase { public void testSplitter2() throws Exception { ToBreakRunner br = new ToBreakRunner("dig/hdl/model2/splitter2.dig"); CodePrinterStr out = new CodePrinterStr(); - VHDLGenerator gen = new VHDLGenerator(br.getLibrary(), out).export(br.getCircuit()); + new VHDLGenerator(br.getLibrary(), out).export(br.getCircuit()); assertEquals("-- generated by Digital. Don't modify this file!\n" + "-- Any changes will be lost if this file is regenerated.\n" + @@ -141,7 +141,7 @@ public class VHDLGeneratorTest extends TestCase { public void testSplitter2I() throws Exception { ToBreakRunner br = new ToBreakRunner("dig/hdl/splitter2.dig"); CodePrinterStr out = new CodePrinterStr(); - VHDLGenerator gen = new VHDLGenerator(br.getLibrary(), out).export(br.getCircuit()); + new VHDLGenerator(br.getLibrary(), out).export(br.getCircuit()); assertEquals("-- generated by Digital. Don't modify this file!\n" + "-- Any changes will be lost if this file is regenerated.\n" + @@ -202,4 +202,49 @@ public class VHDLGeneratorTest extends TestCase { "end Behavioral;\n", out.toString()); } + public void testNames() throws Exception { + ToBreakRunner br = new ToBreakRunner("dig/hdl_names/main.dig"); + CodePrinterStr out = new CodePrinterStr(); + new VHDLGenerator(br.getLibrary(), out).export(br.getCircuit()); + + assertEquals("-- generated by Digital. Don't modify this file!\n" + + "-- Any changes will be lost if this file is regenerated.\n" + + "\n" + + "LIBRARY ieee;\n" + + "USE ieee.std_logic_1164.all;\n" + + "USE ieee.numeric_std.all;\n" + + "\n" + + "entity a_b is\n" + + " port (\n" + + " A: in std_logic;\n" + + " B: in std_logic;\n" + + " Y: out std_logic);\n" + + "end a_b;\n" + + "\n" + + "architecture Behavioral of a_b is\n" + + "begin\n" + + " Y <= (A AND B);\n" + + "end Behavioral;\n" + + "\n" + + "LIBRARY ieee;\n" + + "USE ieee.std_logic_1164.all;\n" + + "USE ieee.numeric_std.all;\n" + + "\n" + + "entity main is\n" + + " port (\n" + + " A: in std_logic;\n" + + " B: in std_logic;\n" + + " Y: out std_logic);\n" + + "end main;\n" + + "\n" + + "architecture Behavioral of main is\n" + + "begin\n" + + " gate0: entity work.a_b\n" + + " port map (\n" + + " A => A,\n" + + " B => B,\n" + + " Y => Y);\n" + + "end Behavioral;\n", out.toString()); + } + } \ No newline at end of file diff --git a/src/test/resources/dig/hdl_names/a b.dig b/src/test/resources/dig/hdl_names/a b.dig new file mode 100644 index 000000000..d777f0db3 --- /dev/null +++ b/src/test/resources/dig/hdl_names/a b.dig @@ -0,0 +1,57 @@ + + + 1 + + + + And + + + + + Out + + + Label + Y + + + + + + In + + + Label + A + + + + + + In + + + Label + B + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/dig/hdl_names/main.dig b/src/test/resources/dig/hdl_names/main.dig new file mode 100644 index 000000000..94c753ac9 --- /dev/null +++ b/src/test/resources/dig/hdl_names/main.dig @@ -0,0 +1,57 @@ + + + 1 + + + + a b.dig + + + + + Out + + + Label + Y + + + + + + In + + + Label + A + + + + + + In + + + Label + B + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file