Binary — To Bcd Verilog Code

bcd = temp; end endmodule For a truly scalable version, use a generate loop or a for loop that iterates over BCD digits:

always @(*) begin bcd_reg = 0; bin_reg = bin;

initial begin $monitor("Binary = %d (%b) → BCD = %b (%d %d %d)", binary, binary, bcd, bcd[11:8], bcd[7:4], bcd[3:0]); binary = 8'd0; #10; binary = 8'd5; #10; binary = 8'd42; #10; binary = 8'd99; #10; binary = 8'd170; #10; binary = 8'd255; #10; $finish; end endmodule

for (i = 0; i < BINARY_WIDTH; i = i + 1) begin // Shift left by 1: bring next binary bit into LSB of temp temp = temp[4*BCD_DIGITS-2:0], bin[BINARY_WIDTH-1]; bin = bin[BINARY_WIDTH-2:0], 1'b0;

always @(*) begin temp = 0; // Clear BCD accumulator bin = binary; // Local copy of input

// Check and correct each BCD digit // (using blocking statements inside loop) // Digit 0 (least significant BCD digit) if (temp[3:0] > 4) temp[3:0] = temp[3:0] + 3; // Digit 1 if (temp[7:4] > 4) temp[7:4] = temp[7:4] + 3; // Digit 2 (for 3-digit BCD) if (BCD_DIGITS > 2 && temp[11:8] > 4) temp[11:8] = temp[11:8] + 3; // Add more digits if needed end

Welcome Back!

Login to your account below

Retrieve your password

Please enter your username or email address to reset your password.

Add New Playlist

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
49
0
Join the conversationx
()
x