|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
这是个6位二进制转8位BCD码,5路输入输出,test是做测试用,检测到state==READ 时,状态不再变,并且不读数据。请各位指点一下,不胜感激~
//
//
module bin2bcd (IN1,IN2,IN3,IN4,IN5,
clk,
showsign_in,
bitsign_in,
IN1bcd_out,IN2bcd_out,IN3bcd_out,IN4bcd_out,IN5bcd_out,
showsign_out,
bitsign_out,
test);
input [5:0] IN1,IN2,IN3,IN4,IN5;
input clk;
input [4:0] showsign_in;
input bitsign_in;
output [7:0] IN1bcd_out,IN2bcd_out,IN3bcd_out,IN4bcd_out,IN5bcd_out,test;
output [4:0] showsign_out;
output bitsign_out;
reg [7:0] IN1bcd_out,IN2bcd_out,IN3bcd_out,IN4bcd_out,IN5bcd_out,IN1bcd,IN2bcd,IN3bcd,IN4bcd,IN5bcd;
reg [4:0] showsign_out,showsign_reg;
reg bitsign_out,bitsign_reg;
reg [5:0] IN1reg,IN2reg,IN3reg,IN4reg,IN5reg;
reg [2:0] state;
reg [2:0] dex;
reg count;
reg [7:0] testreg;
parameter IDLE =3'b111;
parameter READ =3'b101;
parameter YIWEI =3'b100;
parameter YUNSUAN =3'b110;
parameter JIEGUO =3'b000;
assign test = testreg;
always @(negedge clk) begin
case (state)
IDLE: begin state <= READ; dex <= 3'b000; testreg <= 8'b0000_0001; end
READ: begin
testreg <= 8'b0000_0011;
IN1reg <= IN1;
IN2reg <= IN2;
IN3reg <= IN3;
IN4reg <= IN4;
IN5reg <= IN5;
dex <= 3'b000;
IN1bcd <= 7'h00;
IN2bcd <= 7'h00;
IN3bcd <= 7'h00;
IN4bcd <= 7'h00;
IN5bcd <= 7'h00;
showsign_reg<= showsign_in;
bitsign_reg<= bitsign_in;
state <= YIWEI;
end
YIWEI:begin testreg <= {(|dex),7'b000_0111};
if(dex==3'd5) begin
IN1bcd <= (IN1bcd << 1'b1) + IN1reg[5];
IN2bcd <= (IN2bcd << 1'b1) + IN2reg[5];
IN3bcd <= (IN3bcd << 1'b1) + IN3reg[5];
IN4bcd <= (IN4bcd << 1'b1) + IN4reg[5];
IN5bcd <= (IN5bcd << 1'b1) + IN5reg[5];
state <= JIEGUO;
end
else begin
dex <= dex + 1'b1;
IN1reg <= IN1reg << 1'b1;
IN2reg <= IN2reg << 1'b1;
IN3reg <= IN3reg << 1'b1;
IN4reg <= IN4reg << 1'b1;
IN5reg <= IN5reg << 1'b1;
IN1bcd <= (IN1bcd << 1'b1) + IN1reg[5];
IN2bcd <= (IN2bcd << 1'b1) + IN2reg[5];
IN3bcd <= (IN3bcd << 1'b1) + IN3reg[5];
IN4bcd <= (IN4bcd << 1'b1) + IN4reg[5];
IN5bcd <= (IN5bcd << 1'b1) + IN5reg[5];
state <= YUNSUAN;
count <= 1'b0;
end
end
YUNSUAN:begin testreg <= {(|dex),7'b000_1111};
if(count) begin
IN1bcd <=IN1bcd + ((IN1bcd[7:4]>4'b100)?8'b0011_0000:8'b0000_0000);
IN2bcd <=IN2bcd + ((IN2bcd[7:4]>4'b100)?8'b0011_0000:8'b0000_0000);
IN3bcd <=IN3bcd + ((IN3bcd[7:4]>4'b100) ?8'b0011_0000:8'b0000_0000);
IN4bcd <=IN4bcd + ((IN4bcd[7:4]>4'b100) ?8'b0011_0000:8'b0000_0000);
IN5bcd <=IN5bcd + ((IN5bcd[7:4]>4'b100) ?8'b0011_0000:8'b0000_0000);
state <= YIWEI;
end
else begin
count <= 1'b1;
IN1bcd <=IN1bcd + ((IN1bcd[3:0]>4'b100)?4'b0011:4'b0000);
IN2bcd <=IN2bcd + ((IN2bcd[3:0]>4'b100)?4'b0011:4'b0000);
IN3bcd <=IN3bcd + ((IN3bcd[3:0]>4'b100) ?4'b0011:4'b0000);
IN4bcd <=IN4bcd + ((IN4bcd[3:0]>4'b100) ?4'b0011:4'b0000);
IN5bcd <=IN5bcd + ((IN5bcd[3:0]>4'b100) ?4'b0011:4'b0000);
end
end
JIEGUO:begin testreg <= 8'b0001_1111;
IN1bcd_out <= IN1bcd;
IN2bcd_out <= IN2bcd;
IN3bcd_out <= IN3bcd;
IN4bcd_out <= IN4bcd;
IN5bcd_out <= IN5bcd;
showsign_out <= showsign_reg;
bitsign_out <= bitsign_reg;
dex <= 3'b000;
state <= READ;
end
default: begin state <= IDLE; testreg <= 8'b1111_1111; end
endcase
end
endmodule |
|