|
| 1 | +import led |
| 2 | + |
| 3 | +expected_verilog = """ |
| 4 | +module blinkled # |
| 5 | + ( |
| 6 | + parameter WIDTH = 8 |
| 7 | + ) |
| 8 | + ( |
| 9 | + input CLK, |
| 10 | + input RST, |
| 11 | + output reg [WIDTH-1:0] LED_0, |
| 12 | + output reg [WIDTH-1:0] LED_1, |
| 13 | + output reg [WIDTH-1:0] LED_2, |
| 14 | + output reg [WIDTH-1:0] LED_3 |
| 15 | + ); |
| 16 | + reg [32-1:0] count_0; |
| 17 | + reg [32-1:0] count_1; |
| 18 | + reg [32-1:0] count_2; |
| 19 | + reg [32-1:0] count_3; |
| 20 | + always @(posedge CLK) begin |
| 21 | + if(RST) begin |
| 22 | + count_0 <= 0; |
| 23 | + end else begin |
| 24 | + if(count_0 == 9) begin |
| 25 | + count_0 <= 0; |
| 26 | + end else begin |
| 27 | + count_0 <= count_0 + 1; |
| 28 | + end |
| 29 | + end |
| 30 | + end |
| 31 | + always @(posedge CLK) begin |
| 32 | + if(RST) begin |
| 33 | + LED_0 <= 0; |
| 34 | + end else begin |
| 35 | + if(count_0 == 9) begin |
| 36 | + LED_0 <= LED_0 + 1; |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | + always @(posedge CLK) begin |
| 41 | + if(RST) begin |
| 42 | + count_1 <= 0; |
| 43 | + end else begin |
| 44 | + if(count_1 == 19) begin |
| 45 | + count_1 <= 0; |
| 46 | + end else begin |
| 47 | + count_1 <= count_1 + 1; |
| 48 | + end |
| 49 | + end |
| 50 | + end |
| 51 | + always @(posedge CLK) begin |
| 52 | + if(RST) begin |
| 53 | + LED_1 <= 0; |
| 54 | + end else begin |
| 55 | + if(count_1 == 19) begin |
| 56 | + LED_1 <= LED_1 + 1; |
| 57 | + end |
| 58 | + end |
| 59 | + end |
| 60 | + always @(posedge CLK) begin |
| 61 | + if(RST) begin |
| 62 | + count_2 <= 0; |
| 63 | + end else begin |
| 64 | + if(count_2 == 29) begin |
| 65 | + count_2 <= 0; |
| 66 | + end else begin |
| 67 | + count_2 <= count_2 + 1; |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | + always @(posedge CLK) begin |
| 72 | + if(RST) begin |
| 73 | + LED_2 <= 0; |
| 74 | + end else begin |
| 75 | + if(count_2 == 29) begin |
| 76 | + LED_2 <= LED_2 + 1; |
| 77 | + end |
| 78 | + end |
| 79 | + end |
| 80 | + always @(posedge CLK) begin |
| 81 | + if(RST) begin |
| 82 | + count_3 <= 0; |
| 83 | + end else begin |
| 84 | + if(count_3 == 39) begin |
| 85 | + count_3 <= 0; |
| 86 | + end else begin |
| 87 | + count_3 <= count_3 + 1; |
| 88 | + end |
| 89 | + end |
| 90 | + end |
| 91 | + always @(posedge CLK) begin |
| 92 | + if(RST) begin |
| 93 | + LED_3 <= 0; |
| 94 | + end else begin |
| 95 | + if(count_3 == 39) begin |
| 96 | + LED_3 <= LED_3 + 1; |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | +endmodule |
| 101 | +""" |
| 102 | + |
| 103 | +def test_led(): |
| 104 | + led_module = led.mkLed() |
| 105 | + led_code = led_module.to_verilog() |
| 106 | + |
| 107 | + from pyverilog.vparser.parser import VerilogParser |
| 108 | + from pyverilog.ast_code_generator.codegen import ASTCodeGenerator |
| 109 | + parser = VerilogParser() |
| 110 | + expected_ast = parser.parse(expected_verilog) |
| 111 | + codegen = ASTCodeGenerator() |
| 112 | + expected_code = codegen.visit(expected_ast) |
| 113 | + |
| 114 | + assert(led_code == expected_code) |
0 commit comments