@@ -1281,55 +1281,52 @@ def test_blif_with_clock_passing(self):
1281
1281
"""
1282
1282
1283
1283
1284
- verilog_test_bench = """\
1285
- module tb();
1286
- reg clk;
1287
- reg[1:0] a100;
1288
- reg[3:0] w1;
1289
- reg[2:0] w12;
1290
- wire[1:0] out1;
1291
- wire[8:0] out10;
1292
-
1293
- integer tb_iter;
1294
- toplevel block(.clk(clk), .a100(a100), .w1(w1), .w12(w12), .out1(out1), .out10(out10));
1295
-
1296
- always
1297
- #5 clk = ~clk;
1298
-
1299
- initial begin
1300
- $dumpfile ("waveform.vcd");
1301
- $dumpvars;
1284
+ verilog_custom_reset = """\
1285
+ // Generated automatically via PyRTL
1286
+ // As one initial test of synthesis, map to FPGA with:
1287
+ // yosys -p "synth_xilinx -top toplevel" thisfile.v
1302
1288
1303
- clk = 0;
1304
- block.r1 = 2;
1305
- block.r2 = 3;
1306
- block.tmp13 = 0;
1307
- for (tb_iter = 0; tb_iter < 32; tb_iter++) begin block.mem_0[tb_iter] = 0; end
1308
- block.mem_0[2] = 9;
1309
- block.mem_0[9] = 12;
1310
- a100 = 2'd0;
1311
- w1 = 4'd0;
1312
- w12 = 3'd0;
1289
+ module toplevel(clk, rst);
1290
+ input clk;
1291
+ input rst;
1313
1292
1314
- #10
1315
- a100 = 2'd1;
1316
- w1 = 4'd4;
1317
- w12 = 3'd1;
1293
+ reg[3:0] r;
1318
1294
1319
- #10
1320
- a100 = 2'd3;
1321
- w1 = 4'd2;
1322
- w12 = 3'd7;
1295
+ wire const_0_1;
1296
+ wire const_1_0;
1297
+ wire const_2_0;
1298
+ wire const_3_0;
1299
+ wire[2:0] tmp0;
1300
+ wire[3:0] tmp1;
1301
+ wire[4:0] tmp2;
1302
+ wire[3:0] tmp3;
1303
+ wire[4:0] tmp4;
1304
+ wire[4:0] tmp5;
1305
+ wire[3:0] tmp6;
1323
1306
1324
- #10
1325
- a100 = 2'd2;
1326
- w1 = 4'd3;
1327
- w12 = 3'd4;
1307
+ // Combinational
1308
+ assign const_0_1 = 1;
1309
+ assign const_1_0 = 0;
1310
+ assign const_2_0 = 0;
1311
+ assign const_3_0 = 0;
1312
+ assign tmp0 = {const_1_0, const_1_0, const_1_0};
1313
+ assign tmp1 = {tmp0, const_0_1};
1314
+ assign tmp2 = r + tmp1;
1315
+ assign tmp3 = {const_3_0, const_3_0, const_3_0, const_3_0};
1316
+ assign tmp4 = {tmp3, const_2_0};
1317
+ assign tmp5 = rst ? tmp4 : tmp2;
1318
+ assign tmp6 = {tmp5[3], tmp5[2], tmp5[1], tmp5[0]};
1328
1319
1329
- #10
1330
- $finish;
1320
+ // Registers
1321
+ always @(posedge clk)
1322
+ begin
1323
+ begin
1324
+ r <= tmp6;
1325
+ end
1331
1326
end
1327
+
1332
1328
endmodule
1329
+
1333
1330
"""
1334
1331
1335
1332
@@ -1452,10 +1449,156 @@ def test_error_invalid_add_reset(self):
1452
1449
with self .assertRaisesRegex (pyrtl .PyrtlError , "Invalid add_reset option" ):
1453
1450
pyrtl .output_to_verilog (buffer , add_reset = 'foobar' )
1454
1451
1452
+ def test_error_existing_reset_wire (self ):
1453
+ buffer = io .StringIO ()
1454
+ _rst = pyrtl .Input (1 , 'rst' )
1455
+ with self .assertRaisesRegex (pyrtl .PyrtlError , "Found a user-defined wire named 'rst'." ):
1456
+ pyrtl .output_to_verilog (buffer )
1457
+
1458
+ def test_existing_reset_wire_without_add_reset (self ):
1459
+ buffer = io .StringIO ()
1460
+ rst = pyrtl .Input (1 , 'rst' )
1461
+ r = pyrtl .Register (4 , 'r' )
1462
+ r .next <<= pyrtl .select (rst , 0 , r + 1 )
1463
+ pyrtl .output_to_verilog (buffer , add_reset = False )
1464
+ self .assertEqual (buffer .getvalue (), verilog_custom_reset )
1465
+
1466
+
1467
+ verilog_testbench = """\
1468
+ module tb();
1469
+ reg clk;
1470
+ reg rst;
1471
+ reg[1:0] a100;
1472
+ reg[3:0] w1;
1473
+ reg[2:0] w12;
1474
+ wire[1:0] out1;
1475
+ wire[8:0] out10;
1476
+
1477
+ integer tb_iter;
1478
+ toplevel block(.clk(clk), .rst(rst), .a100(a100), .w1(w1), .w12(w12), .out1(out1), .out10(out10));
1479
+
1480
+ always
1481
+ #5 clk = ~clk;
1482
+
1483
+ initial begin
1484
+ $dumpfile ("waveform.vcd");
1485
+ $dumpvars;
1486
+
1487
+ clk = 0;
1488
+ rst = 0;
1489
+ block.r1 = 2;
1490
+ block.r2 = 3;
1491
+ block.tmp0 = 0;
1492
+ for (tb_iter = 0; tb_iter < 32; tb_iter++) begin block.mem_0[tb_iter] = 0; end
1493
+ block.mem_0[2] = 9;
1494
+ block.mem_0[9] = 12;
1495
+ a100 = 2'd0;
1496
+ w1 = 4'd0;
1497
+ w12 = 3'd0;
1498
+
1499
+ #10
1500
+ a100 = 2'd1;
1501
+ w1 = 4'd4;
1502
+ w12 = 3'd1;
1503
+
1504
+ #10
1505
+ a100 = 2'd3;
1506
+ w1 = 4'd2;
1507
+ w12 = 3'd7;
1508
+
1509
+ #10
1510
+ a100 = 2'd2;
1511
+ w1 = 4'd3;
1512
+ w12 = 3'd4;
1513
+
1514
+ #10
1515
+ $finish;
1516
+ end
1517
+ endmodule
1518
+ """ # noqa
1519
+
1520
+ verilog_testbench_no_reset = """\
1521
+ module tb();
1522
+ reg clk;
1523
+ reg[1:0] a100;
1524
+ reg[3:0] w1;
1525
+ reg[2:0] w12;
1526
+ wire[1:0] out1;
1527
+ wire[8:0] out10;
1528
+
1529
+ integer tb_iter;
1530
+ toplevel block(.clk(clk), .a100(a100), .w1(w1), .w12(w12), .out1(out1), .out10(out10));
1531
+
1532
+ always
1533
+ #5 clk = ~clk;
1534
+
1535
+ initial begin
1536
+ $dumpfile ("waveform.vcd");
1537
+ $dumpvars;
1538
+
1539
+ clk = 0;
1540
+ block.r1 = 2;
1541
+ block.r2 = 3;
1542
+ block.tmp0 = 0;
1543
+ for (tb_iter = 0; tb_iter < 32; tb_iter++) begin block.mem_0[tb_iter] = 0; end
1544
+ block.mem_0[2] = 9;
1545
+ block.mem_0[9] = 12;
1546
+ a100 = 2'd0;
1547
+ w1 = 4'd0;
1548
+ w12 = 3'd0;
1549
+
1550
+ #10
1551
+ a100 = 2'd1;
1552
+ w1 = 4'd4;
1553
+ w12 = 3'd1;
1554
+
1555
+ #10
1556
+ a100 = 2'd3;
1557
+ w1 = 4'd2;
1558
+ w12 = 3'd7;
1559
+
1560
+ #10
1561
+ a100 = 2'd2;
1562
+ w1 = 4'd3;
1563
+ w12 = 3'd4;
1564
+
1565
+ #10
1566
+ $finish;
1567
+ end
1568
+ endmodule
1569
+ """
1570
+
1571
+ verilog_testbench_custom_reset = """\
1572
+ module tb();
1573
+ reg clk;
1574
+ reg rst;
1575
+
1576
+ integer tb_iter;
1577
+ toplevel block(.clk(clk), .rst(rst));
1578
+
1579
+ always
1580
+ #5 clk = ~clk;
1581
+
1582
+ initial begin
1583
+ $dumpfile ("waveform.vcd");
1584
+ $dumpvars;
1585
+
1586
+ clk = 0;
1587
+ block.r = 0;
1588
+ $finish;
1589
+ end
1590
+ endmodule
1591
+ """
1592
+
1455
1593
1456
1594
class TestOutputTestbench (unittest .TestCase ):
1457
1595
def setUp (self ):
1458
1596
pyrtl .reset_working_block ()
1597
+ # To compare textual consistency, need to make
1598
+ # sure we're starting at the same index for all
1599
+ # automatically created names.
1600
+ pyrtl .wire ._reset_wire_indexers ()
1601
+ pyrtl .memory ._reset_memory_indexer ()
1459
1602
1460
1603
def test_verilog_testbench_does_not_throw_error (self ):
1461
1604
zero = pyrtl .Input (1 , 'zero' )
@@ -1470,7 +1613,7 @@ def test_verilog_testbench_does_not_throw_error(self):
1470
1613
with io .StringIO () as tbfile :
1471
1614
pyrtl .output_verilog_testbench (tbfile , sim_trace )
1472
1615
1473
- def test_verilog_testbench_consistency (self ):
1616
+ def create_design (self ):
1474
1617
# Various wire names so we can verify they are printed
1475
1618
# in deterministic order each time
1476
1619
i1 , i2 , i3 = pyrtl .input_list ('w1/4 w12/3 a100/2' )
@@ -1499,9 +1642,38 @@ def test_verilog_testbench_consistency(self):
1499
1642
'w12' : [0 , 1 , 7 , 4 ],
1500
1643
'a100' : [0 , 1 , 3 , 2 ],
1501
1644
})
1645
+ return sim_trace
1646
+
1647
+ def test_verilog_testbench_consistency (self ):
1648
+ sim_trace = self .create_design ()
1502
1649
with io .StringIO () as tbfile :
1503
1650
pyrtl .output_verilog_testbench (tbfile , sim_trace )
1504
- self .assertEqual (tbfile .getvalue (), verilog_test_bench )
1651
+ self .assertEqual (tbfile .getvalue (), verilog_testbench )
1652
+
1653
+ def test_verilog_testbench_no_reset_consistency (self ):
1654
+ sim_trace = self .create_design ()
1655
+ with io .StringIO () as tbfile :
1656
+ pyrtl .output_verilog_testbench (tbfile , sim_trace , add_reset = False )
1657
+ self .assertEqual (tbfile .getvalue (), verilog_testbench_no_reset )
1658
+
1659
+ def test_error_verilog_testbench_invalid_add_reset (self ):
1660
+ tbfile = io .StringIO ()
1661
+ with self .assertRaisesRegex (pyrtl .PyrtlError , "Invalid add_reset option" ):
1662
+ pyrtl .output_verilog_testbench (tbfile , add_reset = 'foobar' )
1663
+
1664
+ def test_error_verilog_testbench_existing_reset_wire (self ):
1665
+ tbfile = io .StringIO ()
1666
+ _rst = pyrtl .Input (1 , 'rst' )
1667
+ with self .assertRaisesRegex (pyrtl .PyrtlError , "Found a user-defined wire named 'rst'." ):
1668
+ pyrtl .output_verilog_testbench (tbfile )
1669
+
1670
+ def test_verilog_testbench_existing_reset_wire_without_add_reset (self ):
1671
+ buffer = io .StringIO ()
1672
+ rst = pyrtl .Input (1 , 'rst' )
1673
+ r = pyrtl .Register (4 , 'r' )
1674
+ r .next <<= pyrtl .select (rst , 0 , r + 1 )
1675
+ pyrtl .output_verilog_testbench (buffer , add_reset = False )
1676
+ self .assertEqual (buffer .getvalue (), verilog_testbench_custom_reset )
1505
1677
1506
1678
1507
1679
firrtl_output_concat_test = """\
0 commit comments