微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > FPGA,CPLD和ASIC > 想实现输入8位二进制数,若有4位1则灯亮。

想实现输入8位二进制数,若有4位1则灯亮。

时间:10-02 整理:3721RD 点击:
我想实现FSM+datapath控制,但是不知为什么只要8位二进制数第一位为1,灯就亮了。
代码如下:

  1. module fsm(
  2.     input clk,
  3.     input rst,
  4.     input n,
  5.     input n_0,
  6.     output reg sel,
  7.     output reg  load_n,
  8.     output reg  load_count
  9.     );
  10.     parameter [2:0]         s0=3'b000,        s1=3'b001,        s2=3'b010,        s3=3'b011, s4=3'b100;
  11.         reg [1:0] current_state, next_state;
  12.         always @(posedge clk or negedge rst)
  13.                 if(~rst)        current_state<=s0;
  14.                 else        current_state<=next_state;
  15.         always @(posedge clk or negedge rst)
  16.                 if (~rst) begin sel <=1'b0; load_n<=1'b0; load_count<=1'b0; end
  17.                 else begin case (next_state)
  18.                                         s0:begin sel<=1'b1; load_n<=1'b1; load_count<=1'b0;end
  19.                                         s1:begin sel<=1'b0; load_n<=1'b0; load_count<=1'b0;end
  20.                                         s2:begin sel<=1'b0; load_n<=1'b1; load_count<=1'b1;end
  21.                                         s3:begin sel<=1'b0; load_n<=1'b1; load_count<=1'b0;end
  22.                                         s4:begin sel<=1'b0; load_n<=1'b0; load_count<=1'b0;end
  23.                                         endcase
  24.                         end
  25.         always @(current_state or n or n_0)
  26.                 begin case (current_state)
  27.                         s0: next_state=s1;
  28.                         s1: if(~(n==8'b00000000)&&(n_0==1'b1)) next_state=s2;
  29.                                 else if (~(n==8'b00000000)&&(n_0==1'b0)) next_state=s3;
  30.                                 else next_state=s4;
  31.                         s2: next_state=s1;
  32.                         s3: next_state=s1;
  33.                         s4: next_state=s4;
  34.                         endcase
  35.                 end
  36. endmodule

复制代码

  1. module datapath(
  2.     input sel,
  3.     input load_n,
  4.     input clk,
  5.         input rst,
  6.     input load_count,
  7.         input [7:0] m,
  8.     output [7:0]n,
  9.     output n_0,
  10.     output z
  11.     );
  12.         wire [7:0] A,C;
  13.         reg [7:0] N;
  14.         reg [2:0] COUNT=3'b000;
  15.         wire [3:0] D;
  16.         assign A=sel?m:C;
  17.         always @(posedge clk or negedge rst)
  18.                 if(~rst) N<=8'b00000000;
  19.                 else if (load_n==1'b1) N<=A;
  20.         assign n=N;
  21.         assign n_0=N[0];
  22.         assign C=N>>1;
  23.         always @(posedge clk or negedge rst)
  24.                 if(~rst) COUNT<=4'b0000;
  25.                 else if (load_count==1'b1) COUNT<=D;
  26.         assign D=COUNT+1;
  27.         assign z=(COUNT==3'b100);
  28.        
  29.        
  30. endmodule

复制代码

  1. module top(
  2.     input clk,
  3.     output light,
  4.     input [7:0] m,
  5.     input rst
  6.     );
  7.     wire n_1,n_0_1,load_n_1,load_count_1,sel_1,z_1;
  8.    
  9.    
  10.     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));
  11.     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));
  12.     assign light=z_1;
  13. endmodule

复制代码

各位大神能帮我看看哪里错了吗



   试着看了10分钟,没有任何注释,很多变量名也就一个字母。状态机没有说明,也没有任何仿真波形图,实在是太难看了,放弃了,吃饭上班去也。建议你准备一下这些东西,否则一般人很少有长时间的空闲帮你看这个的。

自己仿真一下,不是很容易找到问题吗?
看别人的代码,一般都比较费劲。

这个和序列检测器很像,使用状态机应该就可以了

恩,谢谢大家,最后发现问题了。我刚学数设,还有许多东西不懂,多向前辈们学习。

数设是什么鬼?



    parameter 中定义的s0等状态的位宽是3,而current_state和next_state的位宽是2

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top