|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
module keyfull_test(key,i,sensor,clk,rst_n,rx_data);
input[3:0] key;//决定触发器被何种信号触发(PC串口,按键,传感器)
input[3:0] sensor;//传感器信号入口
input rst_n;
input[7:0] rx_data;
input clk;
output[3:0] i;
reg[3:0] i;
wire[3:0] rx_data_l;
reg[15:0] state,nextstate;
assign rx_data_l=rx_data[3:0];
//assign rx_data_l=7;
parameter s0=4'b0,
KEYE=4'b0001,RS485E=4'b0010,
SENSORE=4'b0100,
FIRE=4'b1000;
always @(posedge clk or negedge rst_n)
begin
if(!rst_n)
state<=s0; //复位
else
state<=nextstate;
end
always @(state or key)
begin
case(state)
s0:if(key==1)nextstate=KEYE;
else if(key==2)nextstate=RS485E;
else if(key==3)nextstate=SENSORE;
else nextstate=s0; //状态s0
KEYE:if(key==1)
nextstate=KEYE;
else if((key==4)||(key==5)||(key==6)||(key==7)||(key==8)||(key==9))
nextstate=FIRE;
else
nextstate=s0; //状态s4
RS485E:if(key==2)
nextstate=RS485E;
else
nextstate=s0; //状态s5
SENSORE:if(key==3)
nextstate=SENSORE;
else
nextstate=s0; //状态s6
FIRE:if((key==4)||(key==5)||(key==6)||(key==7)||(key==8)||(key==9)||(key==11)||(key==10))
nextstate=FIRE;
else
nextstate=s0;
default:state=s0;
endcase
end
always @(state or key or rst_n)
begin
if(!rst_n) begin
i=4'b0;
end
else begin
/* if (state == KEYE)
i=key-4'b0011;*/
if (state == RS485E)
i=rx_data_l;
else if (state == SENSORE)
i=sensor;
else if (state == FIRE)
i=key-4'b0011;
else begin
i=4'b0000;
end
end
end
endmodule
下面是testbench
`timescale 1 ps/ 1 ps
module keyfull_test_vlg_tst();
// constants
// general purpose registers
reg eachvec;
// test vector input registers
reg clk;
reg [3:0] key;
reg rst_n;
wire [7:0] rx_data;
wire [3:0] sensor;
// wires
wire [3:0] i;
assign sensor=4'b1010;
assign rx_data=8'b11001111;
// assign statements (if any)
keyfull_test i1 (
// port map - connection between master ports and signals/registers
.clk(clk),
.i(i),
.key(key),
.rst_n(rst_n),
.rx_data(rx_data),
.sensor(sensor)
);
initial
begin
clk=0;
rst_n=1;
#100 rst_n=0;
#500 rst_n=1;
#50 key=2;
#500000 key=3;
#500000 key=1;
#500000 key=6;
$display("Running testbench");
end
always
begin
@eachvec;
end
always
#10 clk=~clk;
endmodule
这个状态机应该来说没问题啊,怎么回事呢? |
|