|
| 1 | +from __future__ import absolute_import |
| 2 | +from __future__ import print_function |
| 3 | +import veriloggen |
| 4 | +import resolver_div |
| 5 | + |
| 6 | +expected_verilog = """ |
| 7 | +module Divider # |
| 8 | +( |
| 9 | + parameter W_D = 32, |
| 10 | + parameter A_SIGNED = 1, |
| 11 | + parameter B_SIGNED = 1, |
| 12 | + parameter O_SIGNED = 1 |
| 13 | +) |
| 14 | +( |
| 15 | + input CLK, |
| 16 | + input RST, |
| 17 | + input [32-1:0] in_a, |
| 18 | + input [32-1:0] in_b, |
| 19 | + input update, |
| 20 | + input enable, |
| 21 | + output reg [32-1:0] rslt, |
| 22 | + output reg [32-1:0] mod, |
| 23 | + output reg valid |
| 24 | +); |
| 25 | +
|
| 26 | + localparam DEPTH = 33; |
| 27 | +
|
| 28 | + function [0:0] getsign; |
| 29 | + input [W_D-1:0] in; |
| 30 | + begin |
| 31 | + getsign = in[W_D - 1]; |
| 32 | + end |
| 33 | + endfunction |
| 34 | +
|
| 35 | +
|
| 36 | + function [0:0] is_positive; |
| 37 | + input [W_D-1:0] in; |
| 38 | + begin |
| 39 | + is_positive = getsign(in) == 0; |
| 40 | + end |
| 41 | + endfunction |
| 42 | +
|
| 43 | +
|
| 44 | + function [W_D-1:0] complement2; |
| 45 | + input [W_D-1:0] in; |
| 46 | + begin |
| 47 | + complement2 = ~in + { { W_D - 1{ 1'b0 } }, 1'b1 }; |
| 48 | + end |
| 49 | + endfunction |
| 50 | +
|
| 51 | +
|
| 52 | + function [W_D*2-1:0] complement2_2x; |
| 53 | + input [W_D*2-1:0] in; |
| 54 | + begin |
| 55 | + complement2_2x = ~in + { { W_D * 2 - 1{ 1'b0 } }, 1'b1 }; |
| 56 | + end |
| 57 | + endfunction |
| 58 | +
|
| 59 | +
|
| 60 | + function [W_D-1:0] absolute; |
| 61 | + input [W_D-1:0] in; |
| 62 | + begin |
| 63 | + if(getsign(in)) begin |
| 64 | + absolute = complement2(in); |
| 65 | + end else begin |
| 66 | + absolute = in; |
| 67 | + end |
| 68 | + end |
| 69 | + endfunction |
| 70 | +
|
| 71 | + wire [32-1:0] abs_in_a; |
| 72 | + wire [32-1:0] abs_in_b; |
| 73 | + assign abs_in_a = (1)? absolute(in_a) : in_a; |
| 74 | + assign abs_in_b = (1)? absolute(in_b) : in_b; |
| 75 | + genvar d; |
| 76 | +
|
| 77 | + generate for(d=0; d<DEPTH; d=d+1) begin : s_depth |
| 78 | + reg stage_valid; |
| 79 | + reg in_a_positive; |
| 80 | + reg in_b_positive; |
| 81 | + reg [W_D*2-1:0] dividend; |
| 82 | + reg [W_D*2-1:0] divisor; |
| 83 | + reg [W_D*2-1:0] stage_rslt; |
| 84 | + wire [W_D*2-1:0] sub_value; |
| 85 | + wire is_large; |
| 86 | + assign sub_value = dividend - divisor; |
| 87 | + assign is_large = !sub_value[W_D * 2 - 1]; |
| 88 | + if(d == 0) begin |
| 89 | +
|
| 90 | + always @(posedge CLK) begin |
| 91 | + if(RST) begin |
| 92 | + stage_valid <= 0; |
| 93 | + in_a_positive <= 0; |
| 94 | + in_b_positive <= 0; |
| 95 | + end else begin |
| 96 | + if(update) begin |
| 97 | + stage_valid <= enable; |
| 98 | + in_a_positive <= is_positive(in_a); |
| 99 | + in_b_positive <= is_positive(in_b); |
| 100 | + end |
| 101 | + end |
| 102 | + end |
| 103 | +
|
| 104 | + end else begin |
| 105 | +
|
| 106 | + always @(posedge CLK) begin |
| 107 | + if(RST) begin |
| 108 | + stage_valid <= 0; |
| 109 | + in_a_positive <= 0; |
| 110 | + in_b_positive <= 0; |
| 111 | + end else begin |
| 112 | + if(update) begin |
| 113 | + stage_valid <= s_depth[(d - 1)].stage_valid; |
| 114 | + in_a_positive <= s_depth[(d - 1)].in_a_positive; |
| 115 | + in_b_positive <= s_depth[(d - 1)].in_b_positive; |
| 116 | + end |
| 117 | + end |
| 118 | + end |
| 119 | +
|
| 120 | + end |
| 121 | + if(d == 0) begin |
| 122 | +
|
| 123 | + always @(posedge CLK) begin |
| 124 | + if(update) begin |
| 125 | + dividend <= abs_in_a; |
| 126 | + divisor <= abs_in_b << W_D - 1; |
| 127 | + stage_rslt <= 0; |
| 128 | + end |
| 129 | + end |
| 130 | +
|
| 131 | + end else begin |
| 132 | +
|
| 133 | + always @(posedge CLK) begin |
| 134 | + if(update) begin |
| 135 | + dividend <= (s_depth[(d - 1)].is_large)? s_depth[(d - 1)].sub_value : s_depth[(d - 1)].dividend; |
| 136 | + divisor <= s_depth[(d - 1)].divisor >> 1; |
| 137 | + stage_rslt <= { s_depth[(d - 1)].stage_rslt, s_depth[(d - 1)].is_large }; |
| 138 | + end |
| 139 | + end |
| 140 | +
|
| 141 | + end |
| 142 | + end |
| 143 | + endgenerate |
| 144 | +
|
| 145 | +
|
| 146 | + always @(posedge CLK) begin |
| 147 | + if(RST) begin |
| 148 | + valid <= 0; |
| 149 | + end else begin |
| 150 | + if(update) begin |
| 151 | + valid <= s_depth[32].stage_valid; |
| 152 | + end |
| 153 | + end |
| 154 | + end |
| 155 | +
|
| 156 | +
|
| 157 | + generate if(O_SIGNED) begin |
| 158 | +
|
| 159 | + always @(posedge CLK) begin |
| 160 | + if(update) begin |
| 161 | + rslt <= (s_depth[(DEPTH - 1)].in_a_positive && s_depth[(DEPTH - 1)].in_b_positive)? s_depth[(DEPTH - 1)].stage_rslt : |
| 162 | + (!s_depth[(DEPTH - 1)].in_a_positive && s_depth[(DEPTH - 1)].in_b_positive)? complement2_2x(s_depth[(DEPTH - 1)].stage_rslt) : |
| 163 | + (s_depth[(DEPTH - 1)].in_a_positive && !s_depth[(DEPTH - 1)].in_b_positive)? complement2_2x(s_depth[(DEPTH - 1)].stage_rslt) : |
| 164 | + (!s_depth[(DEPTH - 1)].in_a_positive && !s_depth[(DEPTH - 1)].in_b_positive)? s_depth[(DEPTH - 1)].stage_rslt : 'hx; |
| 165 | + mod <= (s_depth[(DEPTH - 1)].in_a_positive && s_depth[(DEPTH - 1)].in_b_positive)? s_depth[(DEPTH - 1)].dividend[W_D-1:0] : |
| 166 | + (!s_depth[(DEPTH - 1)].in_a_positive && s_depth[(DEPTH - 1)].in_b_positive)? complement2_2x(s_depth[(DEPTH - 1)].dividend[W_D-1:0]) : |
| 167 | + (s_depth[(DEPTH - 1)].in_a_positive && !s_depth[(DEPTH - 1)].in_b_positive)? s_depth[(DEPTH - 1)].dividend[W_D-1:0] : |
| 168 | + (!s_depth[(DEPTH - 1)].in_a_positive && !s_depth[(DEPTH - 1)].in_b_positive)? complement2_2x(s_depth[(DEPTH - 1)].dividend[W_D-1:0]) : 'hx; |
| 169 | + end |
| 170 | + end |
| 171 | +
|
| 172 | + end else begin |
| 173 | +
|
| 174 | + always @(posedge CLK) begin |
| 175 | + if(update) begin |
| 176 | + rslt <= s_depth[(DEPTH - 1)].stage_rslt; |
| 177 | + mod <= s_depth[(DEPTH - 1)].dividend[W_D-1:0]; |
| 178 | + end |
| 179 | + end |
| 180 | +
|
| 181 | + end |
| 182 | + endgenerate |
| 183 | +
|
| 184 | +
|
| 185 | +endmodule |
| 186 | +""" |
| 187 | + |
| 188 | + |
| 189 | +def test(): |
| 190 | + veriloggen.reset() |
| 191 | + test_module = resolver_div.mkOrig() |
| 192 | + code = test_module.to_verilog() |
| 193 | + |
| 194 | + from pyverilog.vparser.parser import VerilogParser |
| 195 | + from pyverilog.ast_code_generator.codegen import ASTCodeGenerator |
| 196 | + parser = VerilogParser() |
| 197 | + expected_ast = parser.parse(expected_verilog) |
| 198 | + codegen = ASTCodeGenerator() |
| 199 | + expected_code = codegen.visit(expected_ast) |
| 200 | + |
| 201 | + assert(expected_code == code) |
0 commit comments