diff options
Diffstat (limited to '')
| -rw-r--r-- | bin2bcd.sv | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/bin2bcd.sv b/bin2bcd.sv new file mode 100644 index 0000000..80dc633 --- /dev/null +++ b/bin2bcd.sv | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | module bin2bcd | ||
| 2 | #( BITS = 8 | ||
| 3 | , BASE = 10 | ||
| 4 | ) | ||
| 5 | ( input bit clk | ||
| 6 | , input bit reset | ||
| 7 | |||
| 8 | , output bit bin_ready | ||
| 9 | , input bit bin_valid `define bin_valid $past(bin_valid) | ||
| 10 | , input bit [BITS-1:0] bin_data `define bin_data $past(bin_data) | ||
| 11 | |||
| 12 | , input bit bcd_ready `define bcd_ready $past(bcd_ready) | ||
| 13 | , output bit bcd_valid | ||
| 14 | , output bit [DIGITS-1:0][BASE_BITS-1:0] bcd_data | ||
| 15 | ); | ||
| 16 | |||
| 17 | // FIXME I don't think this works for odd bases | ||
| 18 | |||
| 19 | localparam BASE_BITS = $clog2(BASE); | ||
| 20 | localparam SLACK = (1 << BASE_BITS) - BASE; | ||
| 21 | localparam DIGITS = $rtoi($ceil($ln(1 << BITS) / $ln(BASE))); | ||
| 22 | localparam CARRY_TEST = $rtoi($ceil($itor(BASE) / 2)); | ||
| 23 | localparam CARRY_ADD = $rtoi($ceil($itor(SLACK) / 2)); | ||
| 24 | |||
| 25 | `ifdef DEBUG_BIN2BCD | ||
| 26 | |||
| 27 | initial $display("%d BITS", BITS); | ||
| 28 | initial $display("%d BASE", BASE); | ||
| 29 | initial $display("%d BASE_BITS", BASE_BITS); | ||
| 30 | initial $display("%d SLACK", SLACK); | ||
| 31 | initial $display("%d DIGITS", DIGITS); | ||
| 32 | initial $display("%d CARRY_TEST", CARRY_TEST); | ||
| 33 | initial $display("%d CARRY_ADD", CARRY_ADD); | ||
| 34 | |||
| 35 | initial for(int i = 0; i < BASE; i = i + 1) begin | ||
| 36 | // verilator lint_off WIDTH | ||
| 37 | automatic bit [BASE_BITS-1:0] n = i; | ||
| 38 | automatic bit [BASE_BITS-1:0] a = n >= CARRY_TEST ? CARRY_ADD : 0; | ||
| 39 | automatic bit [BASE_BITS-1:0] s = n + a; | ||
| 40 | // verilator lint_on WIDTH | ||
| 41 | automatic bit c; | ||
| 42 | automatic bit [BASE_BITS-1:0] d; | ||
| 43 | {c, d} = {s, 1'b0}; | ||
| 44 | $display("\t\t(%x + %x => %x) * 2 => %x:%x", n, a, s, c, d); | ||
| 45 | end | ||
| 46 | |||
| 47 | `endif | ||
| 48 | |||
| 49 | bit bin_b_valid; | ||
| 50 | bit [BITS-1:0] bin_b_data; | ||
| 51 | |||
| 52 | bit [DIGITS-1:0][BASE_BITS-1:0] bcd;; | ||
| 53 | |||
| 54 | bit [$clog2(BITS):0] work; | ||
| 55 | |||
| 56 | always_ff @(posedge clk) begin | ||
| 57 | if (reset) begin | ||
| 58 | bin_ready = 0; | ||
| 59 | bcd_valid = 0; | ||
| 60 | bin_b_valid = 0; | ||
| 61 | end else begin | ||
| 62 | if (bin_ready && `bin_valid) begin | ||
| 63 | bin_b_valid = 1; | ||
| 64 | bin_b_data = `bin_data; | ||
| 65 | bcd = 0; | ||
| 66 | work = BITS; | ||
| 67 | for (int i = BITS; i > 0; i = i - 1) begin | ||
| 68 | if (bin_b_data[BITS-1]) break; | ||
| 69 | bin_b_data = { bin_b_data[BITS-2:0], 1'b0 }; | ||
| 70 | work = work - 1; | ||
| 71 | end | ||
| 72 | end | ||
| 73 | |||
| 74 | if (bin_b_valid && work != 0) begin | ||
| 75 | for (int i = 0; i < DIGITS; i = i + 1) | ||
| 76 | // verilator lint_off WIDTH | ||
| 77 | if (bcd[i] >= CARRY_TEST) bcd[i] = bcd[i] + CARRY_ADD; | ||
| 78 | // verilator lint_on WIDTH | ||
| 79 | for (int i = DIGITS - 1; i > 0; i = i - 1) | ||
| 80 | bcd[i] = { bcd[i][BASE_BITS-2:0], bcd[i-1][BASE_BITS-1] }; | ||
| 81 | bcd[0] = { bcd[0][BASE_BITS-2:0], bin_b_data[BITS-1] }; | ||
| 82 | bin_b_data = { bin_b_data[BITS-2:0], 1'b0 }; | ||
| 83 | |||
| 84 | work = work - 1; | ||
| 85 | end | ||
| 86 | |||
| 87 | if (`bcd_ready) bcd_valid = 0; | ||
| 88 | if (!bcd_valid && bin_b_valid && work == 0) begin | ||
| 89 | bcd_valid = 1; | ||
| 90 | bcd_data = bcd; | ||
| 91 | bin_b_valid = 0; | ||
| 92 | end | ||
| 93 | |||
| 94 | bin_ready = !bin_b_valid; | ||
| 95 | end | ||
| 96 | end | ||
| 97 | |||
| 98 | endmodule | ||
