diff options
Diffstat (limited to 'bin2bcd.sv')
| -rw-r--r-- | bin2bcd.sv | 52 |
1 files changed, 14 insertions, 38 deletions
| @@ -15,37 +15,8 @@ module bin2bcd | |||
| 15 | , output bit [DIGITS-1:0][BASE_BITS-1:0] bcd_data | 15 | , output bit [DIGITS-1:0][BASE_BITS-1:0] bcd_data |
| 16 | ); | 16 | ); |
| 17 | 17 | ||
| 18 | // FIXME I don't think this works for odd bases | ||
| 19 | |||
| 20 | localparam BASE_BITS = $clog2(BASE); | 18 | localparam BASE_BITS = $clog2(BASE); |
| 21 | localparam SLACK = (1 << BASE_BITS) - BASE; | ||
| 22 | localparam DIGITS = $rtoi($ceil($ln(1 << BITS) / $ln(BASE))); | 19 | localparam DIGITS = $rtoi($ceil($ln(1 << BITS) / $ln(BASE))); |
| 23 | localparam CARRY_TEST = $rtoi($ceil($itor(BASE) / 2)); | ||
| 24 | localparam CARRY_ADD = $rtoi($ceil($itor(SLACK) / 2)); | ||
| 25 | |||
| 26 | `ifdef DEBUG_BIN2BCD | ||
| 27 | |||
| 28 | initial $display("%d BITS", BITS); | ||
| 29 | initial $display("%d BASE", BASE); | ||
| 30 | initial $display("%d BASE_BITS", BASE_BITS); | ||
| 31 | initial $display("%d SLACK", SLACK); | ||
| 32 | initial $display("%d DIGITS", DIGITS); | ||
| 33 | initial $display("%d CARRY_TEST", CARRY_TEST); | ||
| 34 | initial $display("%d CARRY_ADD", CARRY_ADD); | ||
| 35 | |||
| 36 | initial for(int i = 0; i < BASE; i = i + 1) begin | ||
| 37 | // verilator lint_off WIDTH | ||
| 38 | automatic bit [BASE_BITS-1:0] n = i; | ||
| 39 | automatic bit [BASE_BITS-1:0] a = n >= CARRY_TEST ? CARRY_ADD : 0; | ||
| 40 | automatic bit [BASE_BITS-1:0] s = n + a; | ||
| 41 | // verilator lint_on WIDTH | ||
| 42 | automatic bit c; | ||
| 43 | automatic bit [BASE_BITS-1:0] d; | ||
| 44 | {c, d} = {s, 1'b0}; | ||
| 45 | $display("\t\t(%x + %x => %x) * 2 => %x:%x", n, a, s, c, d); | ||
| 46 | end | ||
| 47 | |||
| 48 | `endif | ||
| 49 | 20 | ||
| 50 | bit bin_b_valid; | 21 | bit bin_b_valid; |
| 51 | bit [BITS-1:0] bin_b_data; | 22 | bit [BITS-1:0] bin_b_data; |
| @@ -65,7 +36,7 @@ always_ff @(posedge clk) begin | |||
| 65 | bin_b_data = `bin_data; | 36 | bin_b_data = `bin_data; |
| 66 | bcd = 0; | 37 | bcd = 0; |
| 67 | work = BITS; | 38 | work = BITS; |
| 68 | for (int i = 0; i < BITS && i < MAX_SKIP; i = i + 1) begin | 39 | for (int i = 0; i < BITS && i < MAX_SKIP; ++i) begin |
| 69 | if (bin_b_data[BITS-1]) break; | 40 | if (bin_b_data[BITS-1]) break; |
| 70 | bin_b_data = { bin_b_data[BITS-2:0], 1'b0 }; | 41 | bin_b_data = { bin_b_data[BITS-2:0], 1'b0 }; |
| 71 | work = work - 1; | 42 | work = work - 1; |
| @@ -73,14 +44,19 @@ always_ff @(posedge clk) begin | |||
| 73 | end | 44 | end |
| 74 | 45 | ||
| 75 | if (bin_b_valid && work != 0) begin | 46 | if (bin_b_valid && work != 0) begin |
| 76 | for (int i = 0; i < DIGITS; i = i + 1) | 47 | automatic bit carry = bin_b_data[BITS-1]; |
| 77 | // verilator lint_off WIDTH | 48 | for (int i = 0; i < DIGITS; ++i) begin |
| 78 | if (bcd[i] >= CARRY_TEST) bcd[i] = bcd[i] + CARRY_ADD; | 49 | {carry, bcd[i]} = {bcd[i], carry}; |
| 79 | // verilator lint_on WIDTH | 50 | if ({carry, bcd[i]} >= BASE) begin |
| 80 | for (int i = DIGITS - 1; i > 0; i = i - 1) | 51 | // verilator lint_off WIDTH |
| 81 | bcd[i] = { bcd[i][BASE_BITS-2:0], bcd[i-1][BASE_BITS-1] }; | 52 | bcd[i] = {carry, bcd[i]} - BASE; |
| 82 | bcd[0] = { bcd[0][BASE_BITS-2:0], bin_b_data[BITS-1] }; | 53 | // verilator lint_on WIDTH |
| 83 | bin_b_data = { bin_b_data[BITS-2:0], 1'b0 }; | 54 | carry = 1; |
| 55 | end | ||
| 56 | end | ||
| 57 | assert(!carry); | ||
| 58 | |||
| 59 | bin_b_data = bin_b_data << 1; | ||
| 84 | 60 | ||
| 85 | work = work - 1; | 61 | work = work - 1; |
| 86 | end | 62 | end |
