|
本帖最后由 orange-208 于 2012-9-21 17:24 编辑
如题
module BINtoBCD
(
CLK, Clear, Enable_1, Data_Bin,
Hex_4, Hex_3, Hex_2, Hex_1, Hex_e, BCD_over
);
input CLK, Clear, Enable_1;
input[31:0]Data_Bin;
output BCD_over;
output [3:0]Hex_4, Hex_3, Hex_2, Hex_1, Hex_e;
reg [3:0]Hex_4, Hex_3, Hex_2, Hex_1, Hex_e;
/*******************************************************/
function [3:0]Correct;
input[3:0]Decade;
begin
Correct =( Decade >= 4'd5 ) ? ( Decade + 4'd3 ) : Decade;
end
endfunction
/*******************************************************/
integer Cnt = 33;
reg BCD_over;
reg [39:0]BCD;
reg [31:0]Bin_input;
reg [3:0]BCD_10_I, BCD_9_I, BCD_8_I, BCD_7_I, BCD_6_I,
BCD_5_I, BCD_4_I, BCD_3_I, BCD_2_I, BCD_1_I;
always @ ( posedge CLK or posedge Clear )
begin
if( Clear )
begin
Cnt = 33;
BCD = 40'd0;
BCD_over = 1'b0;
Bin_input = 32'd0;
BCD_10_I = 4'b0000;
BCD_9_I = 4'b0000;
BCD_8_I = 4'b0000;
BCD_7_I = 4'b0000;
BCD_6_I = 4'b0000;
BCD_5_I = 4'b0000;
BCD_4_I = 4'b0000;
BCD_3_I = 4'b0000;
BCD_2_I = 4'b0000;
BCD_1_I = 4'b0000;
end
else
begin
if( Enable_1 )
begin
if( Cnt == 33 )
begin
Bin_input = Data_Bin;
Cnt = Cnt - 1'b1;
end
else if( Cnt == 0 )
begin
BCD_10_I = BCD[39:36];
BCD_9_I = BCD[35:32];
BCD_8_I = BCD[31:28];
BCD_7_I = BCD[27:24];
BCD_6_I = BCD[23:20];
BCD_5_I = BCD[19:16];
BCD_4_I = BCD[15:12];
BCD_3_I = BCD[11:8];
BCD_2_I = BCD[7:4];
BCD_1_I = BCD[3:0];
//----------------------------------------
if( BCD_10_I != 4'd0 )
begin
Hex_4 = BCD_10_I;
Hex_3 = BCD_9_I;
Hex_2 = BCD_8_I;
Hex_1 = ( BCD_6_I >= 4'd5 ) ? BCD_7_I + 1'b1 : BCD_7_I;
Hex_e = 4'd9 + 4'd2;
end
else if( BCD_9_I != 4'd0 )
begin
Hex_4 = BCD_9_I;
Hex_3 = BCD_8_I;
Hex_2 = BCD_7_I;
Hex_1 = ( BCD_5_I >= 4'd5 ) ? BCD_6_I + 1'b1 : BCD_6_I;
Hex_e = 4'd8 + 4'd2;
end
else if( BCD_8_I != 4'd0 )
begin
Hex_4 = BCD_8_I;
Hex_3 = BCD_7_I;
Hex_2 = BCD_6_I;
Hex_1 = ( BCD_4_I >= 4'd5 ) ? BCD_5_I + 1'b1 : BCD_5_I;
Hex_e = 4'd7 + 4'd2;
end
else if( BCD_7_I != 4'd0 )
begin
Hex_4 = BCD_7_I;
Hex_3 = BCD_6_I;
Hex_2 = BCD_5_I;
Hex_1 = ( BCD_3_I >= 4'd5 ) ? BCD_4_I + 1'b1 : BCD_4_I;
Hex_e = 4'd6 + 4'd2;
end
else if( BCD_6_I != 4'd0)
begin
Hex_4 = BCD_6_I;
Hex_3 = BCD_5_I;
Hex_2 = BCD_4_I;
Hex_1 = ( BCD_2_I >= 4'd5 ) ? BCD_3_I + 1'b1 : BCD_3_I;
Hex_e = 4'd5 + 4'd2;
end
else if( BCD_5_I != 4'd0 )
begin
Hex_4 = BCD_5_I;
Hex_3 = BCD_4_I;
Hex_2 = BCD_3_I;
Hex_1 = ( BCD_1_I >= 4'd5 ) ? BCD_2_I + 1'b1 : BCD_2_I;
Hex_e = 4'd4 + 4'd2;
end
else if( BCD_4_I != 4'd0 )
begin
Hex_4 = BCD_4_I;
Hex_3 = BCD_3_I;
Hex_2 = BCD_2_I;
Hex_1 = BCD_1_I;
Hex_e = 4'd3 + 4'd2;
end
else if( BCD_3_I != 4'd0 )
begin
Hex_4 = 4'd0;
Hex_3 = BCD_3_I;
Hex_2 = BCD_2_I;
Hex_1 = BCD_1_I;
Hex_e = 4'd2 + 4'd2;
end
else if( BCD_2_I != 4'd0 )
begin
Hex_4 = 4'd0;
Hex_3 = 4'd0;
Hex_2 = BCD_2_I;
Hex_1 = BCD_1_I;
Hex_e = 4'd1 + 4'd2;
end
else if( BCD_1_I != 4'd0 )
begin
Hex_4 = 4'd0;
Hex_3 = 4'd0;
Hex_2 = 4'd0;
Hex_1 = BCD_1_I;
Hex_e = 4'd0 + 4'd2;
end
else
begin
Hex_4 = 4'd0;
Hex_3 = 4'd0;
Hex_2 = 4'd0;
Hex_1 = 4'd0;
Hex_e = 4'd0;
end
//----------------------------------------
BCD_over = 1'b1;
end
else
begin
BCD[3:0] = Correct( BCD[3:0] );
BCD[7:4] = Correct( BCD[7:4] );
BCD[11:8] = Correct( BCD[11:8] );
BCD[15:12] = Correct( BCD[15:12] );
BCD[19:16] = Correct( BCD[19:16] );
BCD[23:20] = Correct( BCD[23:20] );
BCD[27:24] = Correct( BCD[27:24] );
BCD[31:28] = Correct( BCD[31:28] );
BCD[35:32] = Correct( BCD[35:32] );
BCD[39:36] = Correct( BCD[39:36] );
Cnt = Cnt - 1'b1;
BCD = ( BCD << 1 );
BCD[0] = Bin_input [31];
Bin_input = Bin_input << 1;
end
end
end
end |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|