//32位二进制数转BCD码(基本思想是逢十进1)
module BIN_BCD_4 (CLK, A,BWW,BQW,BAW,BSW, BW, BQ, BB, BS, BG);
input CLK;
input [31:0]A; //二进制输入数据
output [3:0]BWW,BQW,BAW,BSW,BW, BQ, BB, BS, BG;//BCD数据输出寄存器
reg [3:0]BWW,BQW,BAW,BSW,BW, BQ, BB, BS, BG;
integer I;
reg [35:0]TEMP;
reg [31:0]C;
always @ (posedge CLK)
begin
C=A;
TEMP=0;
for (I=1; I<32; I=I+1)
begin
{TEMP, C}={TEMP[34:0], C, 1'b0};//左移一位
if (TEMP[3:0]>4'b0100)
begin
TEMP[3:0]=TEMP[3:0]+3; // >4则加3
end
if (TEMP[7:4]>4'b0100)
begin
TEMP[7:4]=TEMP[7:4]+3;
en