如何在Verilog中编写2to4解码器的行为级别代码?

问题描述

我想使用Verilog中的for循环为2至4个解码器编写行为级代码。这是我尝试过的方法,但是我似乎总是将输出设为0:

module decoder2x4Beh(a,e,q);
input e;
input [1:0]a;
output reg [3:0]q;
integer int_a,i;
always @(a) int_a = a;
initial begin
if(e) begin
 for(i=0;i<4;i=i+1) begin
  if(int_a==i)begin
  q[i] = 1'b1;
  end
 end
end
else q=4'b0;
end
endmodule

这是我的测试台:

module testDecoder2x4();
reg e;
reg [1:0]a;
wire [3:0]q;
//decoder3x8 dec1(.d(d),.y(y));
//decoder2x4_df dec2(.a(a),.e(e),.q(q));
decoder2x4Beh dec3(.a(a),.q(q));
integer k;
initial
begin
{e,a} = 3'b0;
$monitor($time," enable %b input code = %b   output q3 %b q2 %b q1 %b q0 %b",a,q[3],q[2],q[1],q[0]);
for(k=0;k<16;k=k+1)
begin
#10 {e,a} = k;
end
end
initial
#120 $stop;
endmodule

解决方法

decoder2x4Beh中,更改:

initial begin

收件人:

always @* begin

intital块仅在时间0执行一次,但是您希望该块在任何输入信号发生变化时都执行。

这是我得到的输出,显示了q的变化:

               0 enable 0 input code = 00   output q3 0 q2 0 q1 0 q0 0
              20 enable 0 input code = 01   output q3 0 q2 0 q1 0 q0 0
              30 enable 0 input code = 10   output q3 0 q2 0 q1 0 q0 0
              40 enable 0 input code = 11   output q3 0 q2 0 q1 0 q0 0
              50 enable 1 input code = 00   output q3 1 q2 0 q1 0 q0 1
              60 enable 1 input code = 01   output q3 1 q2 0 q1 1 q0 1
              70 enable 1 input code = 10   output q3 1 q2 1 q1 1 q0 1
              80 enable 1 input code = 11   output q3 1 q2 1 q1 1 q0 1
              90 enable 0 input code = 00   output q3 0 q2 0 q1 0 q0 0
             100 enable 0 input code = 01   output q3 0 q2 0 q1 0 q0 0
             110 enable 0 input code = 10   output q3 0 q2 0 q1 0 q0 0
,

您遇到了一些问题:

  1. 在解码器2x4Beh中,您的动作代码仅在时间0执行一次,因为您放入了initial块。相反,它应该是always块的一部分。例如
always @* begin
   if(e) begin
      for(i=0;i<4;i=i+1) begin
         if(int_a==i)begin
            q[i] = 1'b1;
         end
      end
   end
   else 
     q=4'b0;
end
  1. {e,a} = k,只会为某些序列设置启用。我认为您应该在tb的开头提供一个重置,然后在模拟过程中声明“ e”。

  2. 最好使用always @*来避免敏感性列表不完整的问题。

  3. 您应该在设计中开始使用时钟。

  4. 好的缩进将有助于阅读程序。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...