Skip to content

Commit 23ccd54

Browse files
author
Oron Port
committed
fix loop iterator printing in vhdl and also add test for verilog
1 parent 78cdcb1 commit 23ccd54

File tree

3 files changed

+95
-4
lines changed

3 files changed

+95
-4
lines changed

compiler/stages/src/main/scala/dfhdl/compiler/stages/vhdl/VHDLOwnerPrinter.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,10 @@ protected trait VHDLOwnerPrinter extends AbstractOwnerPrinter:
131131
val dfValDcls =
132132
designMembers.view
133133
.flatMap {
134+
case _ @IteratorDcl() => None
134135
case p: DFVal.Dcl if p.isVar => Some(p)
135136
case _: DesignParam => None
136-
case c @ DclConst() =>
137+
case c @ DclConst() =>
137138
c.dfType match
138139
case DFInt32 => None
139140
case _ => Some(c)
@@ -214,7 +215,7 @@ protected trait VHDLOwnerPrinter extends AbstractOwnerPrinter:
214215
val dcl = csDFMembers(dcls).emptyOr(v => s"\n${v.hindent}")
215216
val named = pb.meta.nameOpt.map(n => s"$n : ").getOrElse("")
216217
val senList = pb.sensitivity match
217-
case Sensitivity.All => " (all)"
218+
case Sensitivity.All => " (all)"
218219
case Sensitivity.List(refs) =>
219220
if (refs.isEmpty) "" else s" ${refs.map(_.refCodeString).mkStringBrackets}"
220221
s"${named}process$senList$dcl\nbegin\n${body.hindent}\nend process;"

compiler/stages/src/test/scala/StagesSpec/PrintVHDLCodeSpec.scala

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class PrintVHDLCodeSpec extends StageSpec:
2020
val id1_y = SInt(16) <> VAR
2121
val id2_x = SInt(16) <> VAR
2222
val id2_y = SInt(16) <> VAR
23-
val id1 = new ID:
23+
val id1 = new ID:
2424
this.x <> id1_x
2525
this.y <> id1_y
2626
val id2 = new ID:
@@ -1306,4 +1306,55 @@ class PrintVHDLCodeSpec extends StageSpec:
13061306
|end Foo_arch;""".stripMargin
13071307
)
13081308
}
1309+
test("for loop with a register printing") {
1310+
class Foo(
1311+
val PORT_WIDTH: Int <> CONST = 5
1312+
) extends RTDesign:
1313+
val r = Bits(PORT_WIDTH) <> OUT.REG init all(0)
1314+
for (i <- 0 until PORT_WIDTH)
1315+
r(i).din := 1
1316+
for (i <- 0 until PORT_WIDTH)
1317+
if (r(PORT_WIDTH - 1 - i))
1318+
r(i).din := 0
1319+
end Foo
1320+
val top = (new Foo).getCompiledCodeString
1321+
assertNoDiff(
1322+
top,
1323+
"""|library ieee;
1324+
|use ieee.std_logic_1164.all;
1325+
|use ieee.numeric_std.all;
1326+
|use work.dfhdl_pkg.all;
1327+
|use work.Foo_pkg.all;
1328+
|
1329+
|entity Foo is
1330+
|generic (
1331+
| PORT_WIDTH : integer := 5
1332+
|);
1333+
|port (
1334+
| clk : in std_logic;
1335+
| rst : in std_logic;
1336+
| r : out std_logic_vector(PORT_WIDTH - 1 downto 0)
1337+
|);
1338+
|end Foo;
1339+
|
1340+
|architecture Foo_arch of Foo is
1341+
|begin
1342+
| process (clk)
1343+
| begin
1344+
| if rising_edge(clk) then
1345+
| if rst = '1' then r <= repeat("0", PORT_WIDTH);
1346+
| else
1347+
| for i in 0 to PORT_WIDTH-1 loop
1348+
| r(i) <= '1';
1349+
| end loop;
1350+
| for i in 0 to PORT_WIDTH-1 loop
1351+
| if r((PORT_WIDTH - 1) - i) then r(i) <= '0';
1352+
| end if;
1353+
| end loop;
1354+
| end if;
1355+
| end if;
1356+
| end process;
1357+
|end Foo_arch;""".stripMargin
1358+
)
1359+
}
13091360
end PrintVHDLCodeSpec

compiler/stages/src/test/scala/StagesSpec/PrintVerilogCodeSpec.scala

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class PrintVerilogCodeSpec extends StageSpec:
2222
val id1_y = SInt(16) <> VAR
2323
val id2_x = SInt(16) <> VAR
2424
val id2_y = SInt(16) <> VAR
25-
val id1 = new ID:
25+
val id1 = new ID:
2626
this.x <> id1_x
2727
this.y <> id1_y
2828
this.y2 <> OPEN
@@ -1221,4 +1221,43 @@ class PrintVerilogCodeSpec extends StageSpec:
12211221
|endmodule""".stripMargin
12221222
)
12231223
}
1224+
test("for loop with a register printing") {
1225+
class Foo(
1226+
val PORT_WIDTH: Int <> CONST = 5
1227+
) extends RTDesign:
1228+
val r = Bits(PORT_WIDTH) <> OUT.REG init all(0)
1229+
for (i <- 0 until PORT_WIDTH)
1230+
r(i).din := 1
1231+
for (i <- 0 until PORT_WIDTH)
1232+
if (r(PORT_WIDTH - 1 - i))
1233+
r(i).din := 0
1234+
end Foo
1235+
val top = (new Foo).getCompiledCodeString
1236+
assertNoDiff(
1237+
top,
1238+
"""|`default_nettype none
1239+
|`timescale 1ns/1ps
1240+
|`include "Foo_defs.svh"
1241+
|
1242+
|module Foo#(parameter int PORT_WIDTH = 5)(
1243+
| input wire logic clk,
1244+
| input wire logic rst,
1245+
| output logic [PORT_WIDTH - 1:0] r
1246+
|);
1247+
| `include "dfhdl_defs.svh"
1248+
| always_ff @(posedge clk)
1249+
| begin
1250+
| if (rst == 1'b1) r <= {PORT_WIDTH{1'b0}};
1251+
| else begin
1252+
| for (int i = 0; i < PORT_WIDTH; i = i + 1) begin
1253+
| r[i] <= 1'b1;
1254+
| end
1255+
| for (int i = 0; i < PORT_WIDTH; i = i + 1) begin
1256+
| if (r[(PORT_WIDTH - 1) - i]) r[i] <= 1'b0;
1257+
| end
1258+
| end
1259+
| end
1260+
|endmodule""".stripMargin
1261+
)
1262+
}
12241263
end PrintVerilogCodeSpec

0 commit comments

Comments
 (0)