Skip to content

Instantly share code, notes, and snippets.

@kishida
Created February 12, 2013 15:45
Show Gist options
  • Save kishida/4770794 to your computer and use it in GitHub Desktop.
Save kishida/4770794 to your computer and use it in GitHub Desktop.
固定小数点数BCD計算
//表示
wire [9:0] vtemp = airtemp < 10'h14 ? 10'h00 : (airtemp - 10'h14);
reg [7:0] bcd;
reg [7:0] bcdf2;
wire [7:0] bcdfadd = bcd + bcdf2;
wire [3:0] bcdfL = bcdfadd[3:0] < 4'd10 ? bcdfadd[3:0] : (bcdfadd[3:0] - 4'd10);
wire [3:0] bcdfH = bcdfadd[3:0] < 4'd10 ? bcdfadd[7:4] : (bcdfadd[7:4] + 4'd1);
reg [7:0] bcd1;
reg [7:0] bcd2;
wire [7:0]bcdadd = bcd1 + bcd2;
wire [3:0]bcdL = bcdadd[3:0] < 4'd10 ? bcdadd[3:0] : (bcdadd[3:0] - 4'd10);
wire [3:0]bcdH = bcdadd[3:0] < 4'd10 ? bcdadd[7:4] : (bcdadd[7:4] + 4'd1);
always @(airtemp) begin
case(vtemp[1:0])
2'd0: bcd <= 8'h00;
2'd1: bcd <= 8'h25;
2'd2: bcd <= 8'h50;
2'd3: bcd <= 8'h75;
default: bcd <= 8'hff;
endcase
case(airtemp16[3:0])
4'h0: bcdf2 <= 8'h00;
4'h1: bcdf2 <= 8'h02;
4'h2: bcdf2 <= 8'h03;
4'h3: bcdf2 <= 8'h05;
4'h4: bcdf2 <= 8'h06;
4'h5: bcdf2 <= 8'h08;
4'h6: bcdf2 <= 8'h09;
4'h7: bcdf2 <= 8'h11;
4'h8: bcdf2 <= 8'h13;
4'h9: bcdf2 <= 8'h14;
4'ha: bcdf2 <= 8'h16;
4'hb: bcdf2 <= 8'h17;
4'hc: bcdf2 <= 8'h19;
4'hd: bcdf2 <= 8'h20;
4'he: bcdf2 <= 8'h22;
4'hf: bcdf2 <= 8'h23;
default: bcdf2 <= 8'hff;
endcase
case(vtemp[5:2])
4'h0:bcd1 <= 8'h00;
4'h1:bcd1 <= 8'h01;
4'h2:bcd1 <= 8'h02;
4'h3:bcd1 <= 8'h03;
4'h4:bcd1 <= 8'h04;
4'h5:bcd1 <= 8'h05;
4'h6:bcd1 <= 8'h06;
4'h7:bcd1 <= 8'h07;
4'h8:bcd1 <= 8'h08;
4'h9:bcd1 <= 8'h09;
4'ha:bcd1 <= 8'h10;
4'hb:bcd1 <= 8'h11;
4'hc:bcd1 <= 8'h12;
4'hd:bcd1 <= 8'h13;
4'he:bcd1 <= 8'h14;
4'hf:bcd1 <= 8'h15;
default:bcd1 <= 8'hff;
endcase
case(vtemp[9:6])
4'h0:bcd2 <= 8'h00;
4'h1:bcd2 <= 8'h16;
4'h2:bcd2 <= 8'h32;
4'h3:bcd2 <= 8'h48;
4'h4:bcd2 <= 8'h64;
4'h5:bcd2 <= 8'h80;
4'h6:bcd2 <= 8'h96;
default:bcd2 <= 8'hff;
endcase
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment