//adapted from an online source // module bin2bcd( input [13:0] number, //binary input, 14 bits convert to 4 digits of decimal output logic [3:0] thousands, //outputs in bcd output logic [3:0] hundreds, output logic [3:0] tens, output logic [3:0] ones ); // Internal variable for storing bits logic [29:0] shift; integer i; always_comb begin // Clear previous number and store new number in shift register shift[29:14] = '0; shift[13:0] = number; // loop fourteen times, one for each input bit for (i=0; i<14; i=i+1) begin if (shift[17:14] >= 5) shift[17:14] = shift[17:14] + 2'h3; if (shift[21:18] >= 5) shift[21:18] = shift[21:18] + 2'h3; if (shift[25:22] >= 5) shift[25:22] = shift[25:22] + 2'h3; if (shift[29:26] >= 5) shift[29:26] = shift[29:26] + 2'h3; // Shift entire register left once shift = shift << 1; end // Push decimal numbers to output thousands = shift[29:26]; hundreds = shift[25:22]; tens = shift[21:18]; ones = shift[17:14]; end endmodule