想实现输入8位二进制数,若有4位1则灯亮。
时间:10-02
整理:3721RD
点击:
我想实现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
各位大神能帮我看看哪里错了吗
试着看了10分钟,没有任何注释,很多变量名也就一个字母。状态机没有说明,也没有任何仿真波形图,实在是太难看了,放弃了,吃饭上班去也。建议你准备一下这些东西,否则一般人很少有长时间的空闲帮你看这个的。
自己仿真一下,不是很容易找到问题吗?
看别人的代码,一般都比较费劲。
这个和序列检测器很像,使用状态机应该就可以了
恩,谢谢大家,最后发现问题了。我刚学数设,还有许多东西不懂,多向前辈们学习。
数设是什么鬼?
parameter 中定义的s0等状态的位宽是3,而current_state和next_state的位宽是2
