|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
我想实现FSM+datapath控制,但是不知为什么只要8位二进制数第一位为1,灯就亮了。
代码如下:
- module fsm(
- input clk,
- input rst,
- input n,
- input n_0,
- output reg sel,
- output reg load_n,
- output reg load_count
- );
- parameter [2:0] s0=3'b000, s1=3'b001, s2=3'b010, s3=3'b011, s4=3'b100;
- reg [1:0] current_state, next_state;
- always @(posedge clk or negedge rst)
- if(~rst) current_state<=s0;
- else current_state<=next_state;
- always @(posedge clk or negedge rst)
- if (~rst) begin sel <=1'b0; load_n<=1'b0; load_count<=1'b0; end
- else begin case (next_state)
- s0:begin sel<=1'b1; load_n<=1'b1; load_count<=1'b0;end
- s1:begin sel<=1'b0; load_n<=1'b0; load_count<=1'b0;end
- s2:begin sel<=1'b0; load_n<=1'b1; load_count<=1'b1;end
- s3:begin sel<=1'b0; load_n<=1'b1; load_count<=1'b0;end
- s4:begin sel<=1'b0; load_n<=1'b0; load_count<=1'b0;end
- endcase
- end
- always @(current_state or n or n_0)
- begin case (current_state)
- s0: next_state=s1;
- s1: if(~(n==8'b00000000)&&(n_0==1'b1)) next_state=s2;
- else if (~(n==8'b00000000)&&(n_0==1'b0)) next_state=s3;
- else next_state=s4;
- s2: next_state=s1;
- s3: next_state=s1;
- s4: next_state=s4;
- endcase
- end
- endmodule
复制代码
- module datapath(
- input sel,
- input load_n,
- input clk,
- input rst,
- input load_count,
- input [7:0] m,
- output [7:0]n,
- output n_0,
- output z
- );
- wire [7:0] A,C;
- reg [7:0] N;
- reg [2:0] COUNT=3'b000;
- wire [3:0] D;
- assign A=sel?m:C;
- always @(posedge clk or negedge rst)
- if(~rst) N<=8'b00000000;
- else if (load_n==1'b1) N<=A;
- assign n=N;
- assign n_0=N[0];
- assign C=N>>1;
- always @(posedge clk or negedge rst)
- if(~rst) COUNT<=4'b0000;
- else if (load_count==1'b1) COUNT<=D;
- assign D=COUNT+1;
- assign z=(COUNT==3'b100);
-
-
- endmodule
复制代码
- module top(
- input clk,
- output light,
- input [7:0] m,
- input rst
- );
- wire n_1,n_0_1,load_n_1,load_count_1,sel_1,z_1;
-
-
- fsm fsm1(.clk(clk),.rst(rst),.n(n_1),.n_0(n_0_1),.sel(sel_1),.load_n(load_n_1),.load_count(load_count_1));
- datapath datapath1(.clk(clk),.rst(rst),.load_n(load_n_1),.load_count(load_count_1),.m(m),.sel(sel_1),.n(n_1),.n_0(n_0_1),.z(z_1));
- assign light=z_1;
- endmodule
复制代码 |
|