BCD Encoder
The BCD (Binary-Coded Decimal) encoder is a combinatorial circuit that takes an N-bit binary input, i_bin, and converts it to a BCD value,
o_bcd, that is N + ceil{(N-4)/3} + 1 bits long. It implements the conversion using the
double dabble algorithm.
Parameters
Nnumber of bits in binary input
Ports
i_bininput binary valueo_bcdoutput BCD value
Source Code
bcd_encoder.sv
1`ifndef LIBSV_CODERS_BCD_ENCODER
2`define LIBSV_CODERS_BCD_ENCODER
3
4module bcd_encoder #(
5 parameter integer N /* verilator public_flat_rd */ = 8
6) (
7 input logic [ N-1:0] i_bin,
8 output logic [N+(N-4)/3:0] o_bcd
9);
10
11 integer i, j;
12
13 always_comb begin
14 o_bcd = {{(N - 4) / 3 + 1{1'b0}}, i_bin}; // initialize with input vector in lower bits
15 for (i = 0; i <= N - 4; i = i + 1) begin // iterate on structure depth
16 for (j = 0; j <= i / 3; j = j + 1) begin // iterate on structure width
17 if (o_bcd[N-i+4*j-:4] > 4) begin // if > 4
18 o_bcd[N-i+4*j-:4] = o_bcd[N-i+4*j-:4] + 4'd3; // add 3
19 end
20 end
21 end
22 end
23
24endmodule
25
26`endif /* LIBSV_CODERS_BCD_ENCODER */