verilog - How to convert two digit BCD into binary? -
i want make calculator based on fpga board(spartan 3). have following code
module bcd_converter( input [7:0] r, output reg [3:0] hundreds, output reg [3:0] tens, output reg [3:0] ones ); integer i; @ (r) begin hundreds = 4'd0; tens = 4'd0; ones = 4'd0; for(i=7;i>=0;i = i-1) begin if(hundreds >= 5) hundreds = hundreds + 3; if(tens >= 5) tens = tens + 3; if(ones >= 5) ones = ones + 3; hundreds = hundreds<<1; hundreds[0] = tens[3]; tens = tens << 1; tens[0] = ones[3]; ones = ones<<1; ones[0] = r[i]; end end endmodule
however, code provided conversion binary bcd. looking way reverse algorithm. there better way of converting bcd binary?
you decode each bcd digit binary separate case statements, , add results. example, hundreds case statement this:
case(hundreds) 4'd0 : hundreds_bin = 9'd0; 4'd1 : hundreds_bin = 9'd100; 4'd2 : hundreds_bin = 9'd200; 4'd3 : hundreds_bin = 9'd300; //..etc etc
Comments
Post a Comment