Skip to content

Commit bd716e8

Browse files
committed
Minor Verilog output improvements: Move lines with only "begin" to the end of previous line, remove an unnecessary begin/end block, put "end else begin" on the same line.
1 parent c4265e9 commit bd716e8

2 files changed

Lines changed: 50 additions & 78 deletions

File tree

pyrtl/importexport.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,10 +1184,9 @@ def _to_verilog_sequential(self, file: IO):
11841184

11851185
print(" // Register logic", file=file)
11861186
if self.add_reset == "asynchronous":
1187-
print(" always @(posedge clk or posedge rst)", file=file)
1187+
print(" always @(posedge clk or posedge rst) begin", file=file)
11881188
else:
1189-
print(" always @(posedge clk)", file=file)
1190-
print(" begin", file=file)
1189+
print(" always @(posedge clk) begin", file=file)
11911190
if self.add_reset:
11921191
print(" if (rst) begin", file=file)
11931192
for register in self._name_sorted(self.gate_graph.registers):
@@ -1197,18 +1196,19 @@ def _to_verilog_sequential(self, file: IO):
11971196
f"{register.bitwidth}'d{reset_value};",
11981197
file=file,
11991198
)
1200-
print(" end", file=file)
1201-
print(" else begin", file=file)
1199+
print(" end else begin", file=file)
1200+
indent = " "
12021201
else:
1203-
print(" begin", file=file)
1202+
indent = ""
12041203

12051204
for register in self._name_sorted(self.gate_graph.registers):
12061205
print(
1207-
f" {self._verilog_name(register.name)} <= "
1206+
f" {indent}{self._verilog_name(register.name)} <= "
12081207
f"{self._verilog_expr(register.args[0])};",
12091208
file=file,
12101209
)
1211-
print(" end", file=file)
1210+
if self.add_reset:
1211+
print(" end", file=file)
12121212
print(" end", file=file)
12131213
print(file=file)
12141214

@@ -1218,16 +1218,15 @@ def _to_verilog_memories(self, file: IO):
12181218
kind = "MemBlock"
12191219
if isinstance(memblock, RomBlock):
12201220
kind = "RomBlock"
1221-
print(f" // {kind} {memblock.name}", file=file)
1221+
print(f" // {kind} {memblock.name} logic", file=file)
12221222

12231223
# Find writes to ``memblock``.
12241224
write_gates = []
12251225
for write_gate in self._name_sorted(self.gate_graph.mem_writes):
12261226
if write_gate.op_param[1] is memblock:
12271227
write_gates.append(write_gate)
12281228
if write_gates:
1229-
print(" always @(posedge clk)", file=file)
1230-
print(" begin", file=file)
1229+
print(" always @(posedge clk) begin", file=file)
12311230
for write_gate in write_gates:
12321231
enable = write_gate.args[2]
12331232
verilog_enable = self._verilog_expr(write_gate.args[2])

tests/test_importexport.py

Lines changed: 40 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -852,106 +852,91 @@ def test_blif_nor_gate_correct(self):
852852
assign tmp79 = ((tmp60 + {(1'd0), tmp72}) + {{2 {1'd0}}, tmp76});
853853
854854
// Register logic
855-
always @(posedge clk)
856-
begin
855+
always @(posedge clk) begin
857856
if (rst) begin
858857
r <= 4'd0;
859858
s <= 4'd13;
860-
end
861-
else begin
859+
end else begin
862860
r <= (tmp66[3:0]);
863861
s <= (tmp70[3:0]);
864862
end
865863
end
866864
867-
// MemBlock z
868-
always @(posedge clk)
869-
begin
865+
// MemBlock z logic
866+
always @(posedge clk) begin
870867
z[2'd0] <= 4'd9;
871868
end
872869
873-
// MemBlock tmp0
874-
always @(posedge clk)
875-
begin
870+
// MemBlock tmp0 logic
871+
always @(posedge clk) begin
876872
tmp0[2'd0] <= a;
877873
tmp0[2'd1] <= (tmp14[3:0]);
878874
end
879875
assign tmp72 = tmp0[2'd0];
880876
881-
// MemBlock tmp1
882-
always @(posedge clk)
883-
begin
877+
// MemBlock tmp1 logic
878+
always @(posedge clk) begin
884879
tmp1[2'd0] <= a;
885880
tmp1[2'd1] <= (tmp18[3:0]);
886881
end
887882
assign tmp76 = tmp1[2'd0];
888883
889-
// MemBlock tmp2
890-
always @(posedge clk)
891-
begin
884+
// MemBlock tmp2 logic
885+
always @(posedge clk) begin
892886
tmp2[2'd0] <= a;
893887
tmp2[2'd1] <= (tmp22[3:0]);
894888
end
895889
896-
// MemBlock tmp3
897-
always @(posedge clk)
898-
begin
890+
// MemBlock tmp3 logic
891+
always @(posedge clk) begin
899892
tmp3[2'd0] <= a;
900893
tmp3[2'd1] <= (tmp26[3:0]);
901894
end
902895
903-
// MemBlock tmp4
904-
always @(posedge clk)
905-
begin
896+
// MemBlock tmp4 logic
897+
always @(posedge clk) begin
906898
tmp4[2'd0] <= a;
907899
tmp4[2'd1] <= (tmp30[3:0]);
908900
end
909901
910-
// MemBlock tmp5
911-
always @(posedge clk)
912-
begin
902+
// MemBlock tmp5 logic
903+
always @(posedge clk) begin
913904
tmp5[2'd0] <= a;
914905
tmp5[2'd1] <= (tmp34[3:0]);
915906
end
916907
917-
// MemBlock tmp6
918-
always @(posedge clk)
919-
begin
908+
// MemBlock tmp6 logic
909+
always @(posedge clk) begin
920910
tmp6[2'd0] <= a;
921911
tmp6[2'd1] <= (tmp38[3:0]);
922912
end
923913
924-
// MemBlock tmp7
925-
always @(posedge clk)
926-
begin
914+
// MemBlock tmp7 logic
915+
always @(posedge clk) begin
927916
tmp7[2'd0] <= a;
928917
tmp7[2'd1] <= (tmp42[3:0]);
929918
end
930919
931-
// MemBlock tmp8
932-
always @(posedge clk)
933-
begin
920+
// MemBlock tmp8 logic
921+
always @(posedge clk) begin
934922
tmp8[2'd0] <= a;
935923
tmp8[2'd1] <= (tmp46[3:0]);
936924
end
937925
938-
// MemBlock tmp9
939-
always @(posedge clk)
940-
begin
926+
// MemBlock tmp9 logic
927+
always @(posedge clk) begin
941928
tmp9[2'd0] <= a;
942929
tmp9[2'd1] <= (tmp50[3:0]);
943930
end
944931
945-
// MemBlock tmp10
946-
always @(posedge clk)
947-
begin
932+
// MemBlock tmp10 logic
933+
always @(posedge clk) begin
948934
tmp10[2'd0] <= a;
949935
tmp10[2'd1] <= (tmp54[3:0]);
950936
end
951937
952-
// MemBlock tmp11
953-
always @(posedge clk)
954-
begin
938+
// MemBlock tmp11 logic
939+
always @(posedge clk) begin
955940
tmp11[2'd0] <= a;
956941
tmp11[2'd1] <= (tmp58[3:0]);
957942
end
@@ -993,12 +978,11 @@ def test_blif_nor_gate_correct(self):
993978
// Combinational logic
994979
assign out1 = tmp0;
995980
996-
// RomBlock rom
981+
// RomBlock rom logic
997982
assign tmp0 = rom[in1];
998983
999-
// MemBlock mem
1000-
always @(posedge clk)
1001-
begin
984+
// MemBlock mem logic
985+
always @(posedge clk) begin
1002986
mem[tmp0] <= 8'd42;
1003987
end
1004988
@@ -1027,12 +1011,10 @@ def test_blif_nor_gate_correct(self):
10271011
assign tmp3 = (tmp0 + {{3 {1'd0}}, 1'd1});
10281012
10291013
// Register logic
1030-
always @(posedge clk)
1031-
begin
1014+
always @(posedge clk) begin
10321015
if (rst) begin
10331016
tmp0 <= 4'd2;
1034-
end
1035-
else begin
1017+
end else begin
10361018
tmp0 <= (tmp3[3:0]);
10371019
end
10381020
end
@@ -1062,12 +1044,10 @@ def test_blif_nor_gate_correct(self):
10621044
assign tmp3 = (tmp0 + {{3 {1'd0}}, 1'd1});
10631045
10641046
// Register logic
1065-
always @(posedge clk or posedge rst)
1066-
begin
1047+
always @(posedge clk or posedge rst) begin
10671048
if (rst) begin
10681049
tmp0 <= 4'd2;
1069-
end
1070-
else begin
1050+
end else begin
10711051
tmp0 <= (tmp3[3:0]);
10721052
end
10731053
end
@@ -1096,11 +1076,8 @@ def test_blif_nor_gate_correct(self):
10961076
assign tmp3 = (tmp0 + {{3 {1'd0}}, 1'd1});
10971077
10981078
// Register logic
1099-
always @(posedge clk)
1100-
begin
1101-
begin
1102-
tmp0 <= (tmp3[3:0]);
1103-
end
1079+
always @(posedge clk) begin
1080+
tmp0 <= (tmp3[3:0]);
11041081
end
11051082
11061083
endmodule
@@ -1126,11 +1103,8 @@ def test_blif_nor_gate_correct(self):
11261103
assign tmp5 = (rst ? {{4 {1'd0}}, 1'd0} : (r + {{3 {1'd0}}, 1'd1}));
11271104
11281105
// Register logic
1129-
always @(posedge clk)
1130-
begin
1131-
begin
1132-
r <= (tmp5[3:0]);
1133-
end
1106+
always @(posedge clk) begin
1107+
r <= (tmp5[3:0]);
11341108
end
11351109
11361110
endmodule
@@ -1370,8 +1344,7 @@ def test_bit_slice_inputs(self):
13701344
output [1:0] o;
13711345
foo f1(a, b, o);
13721346
1373-
always @(posedge clk)
1374-
begin
1347+
always @(posedge clk) begin
13751348
a <= ~a;
13761349
b <= ~b;
13771350
end

0 commit comments

Comments
 (0)