summaryrefslogtreecommitdiff
path: root/bin2bcd.sv
diff options
context:
space:
mode:
authorJulian Blake Kongslie2021-03-23 19:01:29 -0700
committerJulian Blake Kongslie2021-03-23 19:03:14 -0700
commit0f95f3efbe4879bcf8b24aafda1c10dd95e9dd40 (patch)
treeb30ab627b29a881a2448bd28e6e02e4f8581ceba /bin2bcd.sv
parentClean up the preshifts on bin2bcd and ntoa. (diff)
downloadtoycpu-0f95f3efbe4879bcf8b24aafda1c10dd95e9dd40.tar.xz
Replace dibble dabble algorithm with variant that tests after the add.
This makes it really easy to support odd bases.
Diffstat (limited to '')
-rw-r--r--bin2bcd.sv52
1 files changed, 14 insertions, 38 deletions
diff --git a/bin2bcd.sv b/bin2bcd.sv
index 266860c..8e618c6 100644
--- a/bin2bcd.sv
+++ b/bin2bcd.sv
@@ -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
20localparam BASE_BITS = $clog2(BASE); 18localparam BASE_BITS = $clog2(BASE);
21localparam SLACK = (1 << BASE_BITS) - BASE;
22localparam DIGITS = $rtoi($ceil($ln(1 << BITS) / $ln(BASE))); 19localparam DIGITS = $rtoi($ceil($ln(1 << BITS) / $ln(BASE)));
23localparam CARRY_TEST = $rtoi($ceil($itor(BASE) / 2));
24localparam CARRY_ADD = $rtoi($ceil($itor(SLACK) / 2));
25
26`ifdef DEBUG_BIN2BCD
27
28initial $display("%d BITS", BITS);
29initial $display("%d BASE", BASE);
30initial $display("%d BASE_BITS", BASE_BITS);
31initial $display("%d SLACK", SLACK);
32initial $display("%d DIGITS", DIGITS);
33initial $display("%d CARRY_TEST", CARRY_TEST);
34initial $display("%d CARRY_ADD", CARRY_ADD);
35
36initial 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);
46end
47
48`endif
49 20
50bit bin_b_valid; 21bit bin_b_valid;
51bit [BITS-1:0] bin_b_data; 22bit [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