BCD Decoder

The BCD (Binary-Coded Decimal) decoder is a combinatorial circuit takes a N decimal digit BCD input and converts it to a binary output. The BCD input , i_bcd, is a 4*N bit wide input vector where each 4 bits represents one decimal digit. The output binary value, o_bin, is 3*N + floor{(N+2)/3} bits long. It implements the conversion using the reverse double dabble algorithm.

Parameters

  • N number of digits in BCD input

Ports

  • i_bcd input BCD value

  • o_bin output binary value

Source Code

bcd_decoder.sv
 1`ifndef LIBSV_CODERS_BCD_DECODER
 2`define LIBSV_CODERS_BCD_DECODER
 3
 4module bcd_decoder #(
 5    parameter integer N  /* verilator public_flat_rd */ = 3
 6) (
 7    input  logic [        4*N-1:0] i_bcd,
 8    output logic [3*N+(N+2)/3-1:0] o_bin
 9);
10
11    integer i, j;
12
13    logic [4*N-1:0] temp;  // create temp vector as wide as i_bcd to hold intermediate values
14    always_comb begin
15        temp = i_bcd;  // initialize with input vector
16        for (i = 0; i < 4 * (N - 1); i = i + 1) begin  // iterate on structure depth
17            for (j = 0; j < N - i / 4 - 1; j = j + 1) begin  // iterate on structure width
18                if (temp[4+i+4*j-:4] > 7) begin  // if > 7
19                    temp[4+i+4*j-:4] = temp[4+i+4*j-:4] - 4'd3;  // subtract three
20                end
21            end
22        end
23        o_bin = temp[3*N+(N+2)/3-1:0];  // truncate final result in temp vector to get o_bin
24    end
25
26endmodule
27
28`endif  /* LIBSV_CODERS_BCD_DECODER */