编码器Verilog代码中的意外输出
我一直在尝试在Verilog中编码编码。代码正在编译没有错误,但输出是出乎意料的。输出不会随输入而变化。我正在附加代码和输出的图片。
我已经在每个模块中使用值0初始化。但是OUT的价值不会在案例
语句中更新。这就是为什么输出始终为零的原因。
我试图使用$ display
关键字检查模块中变量“ in”的值,但没有显示任何内容。我想编码器模块内部的输入变量“ in”不是从测试模块中获取输入值。我不知道为什么会发生这种情况。
module ENCODER_2_1(
output reg out,
input [1:0] in,
input enable);
initial begin
out = 1'b0;
if (enable) begin
case (in)
2'b00 : out = 1'b0;
2'b10 : out = 1'b1;
default : out = 8'b00000000;
endcase
end
end
endmodule
module ENCODER_4_2(
output reg [1:0] out,
input [3:0] in,
input enable);
initial begin
out = 2'b00;
if (enable) begin
case (in)
4'b0000 : out = 2'b00;
4'b0001 : out = 2'b01;
4'b0010 : out = 2'b10;
4'b0100 : out = 2'b11;
default : out = 8'b00000000;
endcase
end
end
endmodule
module ENCODER_8_3(
output reg [2:0] out,
input [7:0] in,
input enable);
initial begin
out = 3'b000;
if (enable) begin
case (in)
8'b00000000 : out = 3'b000;
8'b00000001 : out = 3'b001;
8'b00000010 : out = 3'b010;
8'b00000100 : out = 3'b011;
8'b00001000 : out = 3'b100;
8'b00010000 : out = 3'b101;
8'b00100000 : out = 3'b110;
8'b01000000 : out = 3'b111;
default : out = 8'b00000000;
endcase
end
end
endmodule
module ENCODER_16_4(
output reg [3:0] out,
input [15:0] in,
input enable);
initial begin
out = 4'b0000;
if (enable) begin
case (in)
16'h0000 : out = 4'b0000;
16'h0001 : out = 4'b0001;
16'h0002 : out = 4'b0010;
16'h0004 : out = 4'b0011;
16'h0008 : out = 4'b0100;
16'h0010 : out = 4'b0101;
16'h0020 : out = 4'b0110;
16'h0040 : out = 4'b0111;
16'h0080 : out = 4'b1000;
16'h0100 : out = 4'b1001;
16'h0200 : out = 4'b1010;
16'h0400 : out = 4'b1011;
16'h0800 : out = 4'b1100;
16'h1000 : out = 4'b1101;
16'h2000 : out = 4'b1110;
16'h4000 : out = 4'b1111;
default : out = 8'b00000000;
endcase
end
end
endmodule
//To run the test uncomment this block
module test();
wire out1;
wire [1:0] out2;
wire [2:0] out3;
wire [3:0] out4;
reg [1:0] in2;
reg [3:0] in4;
reg [7:0] in8;
reg [15:0] in16;
reg enable;
ENCODER_2_1 enc_2_1(out1, in2, enable);
ENCODER_4_2 enc_4_2(out2, in4, enable);
ENCODER_8_3 enc_8_3(out3, in8, enable);
ENCODER_16_4 enc_16_4(out4, in16, enable);
initial begin
enable = 1;
#1 $display("\n2:1 Encoder");
$monitor("input = %b | output = %b",in2, out1);
in2 = 2'b00;
#1 in2 = 2'b10;
#1 $display("\n4:2 Encoder");
$monitor("input = %b | output = %b",in4, out2);
in4 = 4'b0000;
#1 in4 = 4'b0001;
#1 in4 = 4'b0010;
#1 in4 = 4'b0100;
#1 $display("\n8:3 Encoder");
$monitor("input = %b | output = %b",in8, out3);
in8 = 8'b00000000;
#1 in8 = 8'b00000001;
#1 in8 = 8'b00000010;
#1 in8 = 8'b00000100;
#1 in8 = 8'b00001000;
#1 in8 = 8'b00010000;
#1 in8 = 8'b00100000;
#1 in8 = 8'b01000000;
#1 $display("\n16:4 Encoder");
$monitor("input = %h | output = %b",in16, out4);
in16 = 16'h0000;
#1 in16 = 16'h0001;
#1 in16 = 16'h0002;
#1 in16 = 16'h0004;
#1 in16 = 16'h0008;
#1 in16 = 16'h0010;
#1 in16 = 16'h0020;
#1 in16 = 16'h0040;
#1 in16 = 16'h0080;
#1 in16 = 16'h0100;
#1 in16 = 16'h0200;
#1 in16 = 16'h0400;
#1 in16 = 16'h0800;
#1 in16 = 16'h1000;
#1 in16 = 16'h2000;
#1 in16 = 16'h4000;
end
endmodule
输出如下:
为什么输出不改变?
I have been trying to code encoder in Verilog. The code is compiling without error but the output are unexpected. The output is not changing with input. I am attaching the code and the pic of the output.
I have initialized out with value 0 in each module. But the value of out is not getting updated in the case
statement. That's why the output is always zero.
I tried to check value of the variable 'in' in the modules using the $display
keyword, but it isn't showing anything. I guess the input variable 'in' inside the encoder modules is not taking the input values from the test module. I don't know why is this happening.
module ENCODER_2_1(
output reg out,
input [1:0] in,
input enable);
initial begin
out = 1'b0;
if (enable) begin
case (in)
2'b00 : out = 1'b0;
2'b10 : out = 1'b1;
default : out = 8'b00000000;
endcase
end
end
endmodule
module ENCODER_4_2(
output reg [1:0] out,
input [3:0] in,
input enable);
initial begin
out = 2'b00;
if (enable) begin
case (in)
4'b0000 : out = 2'b00;
4'b0001 : out = 2'b01;
4'b0010 : out = 2'b10;
4'b0100 : out = 2'b11;
default : out = 8'b00000000;
endcase
end
end
endmodule
module ENCODER_8_3(
output reg [2:0] out,
input [7:0] in,
input enable);
initial begin
out = 3'b000;
if (enable) begin
case (in)
8'b00000000 : out = 3'b000;
8'b00000001 : out = 3'b001;
8'b00000010 : out = 3'b010;
8'b00000100 : out = 3'b011;
8'b00001000 : out = 3'b100;
8'b00010000 : out = 3'b101;
8'b00100000 : out = 3'b110;
8'b01000000 : out = 3'b111;
default : out = 8'b00000000;
endcase
end
end
endmodule
module ENCODER_16_4(
output reg [3:0] out,
input [15:0] in,
input enable);
initial begin
out = 4'b0000;
if (enable) begin
case (in)
16'h0000 : out = 4'b0000;
16'h0001 : out = 4'b0001;
16'h0002 : out = 4'b0010;
16'h0004 : out = 4'b0011;
16'h0008 : out = 4'b0100;
16'h0010 : out = 4'b0101;
16'h0020 : out = 4'b0110;
16'h0040 : out = 4'b0111;
16'h0080 : out = 4'b1000;
16'h0100 : out = 4'b1001;
16'h0200 : out = 4'b1010;
16'h0400 : out = 4'b1011;
16'h0800 : out = 4'b1100;
16'h1000 : out = 4'b1101;
16'h2000 : out = 4'b1110;
16'h4000 : out = 4'b1111;
default : out = 8'b00000000;
endcase
end
end
endmodule
//To run the test uncomment this block
module test();
wire out1;
wire [1:0] out2;
wire [2:0] out3;
wire [3:0] out4;
reg [1:0] in2;
reg [3:0] in4;
reg [7:0] in8;
reg [15:0] in16;
reg enable;
ENCODER_2_1 enc_2_1(out1, in2, enable);
ENCODER_4_2 enc_4_2(out2, in4, enable);
ENCODER_8_3 enc_8_3(out3, in8, enable);
ENCODER_16_4 enc_16_4(out4, in16, enable);
initial begin
enable = 1;
#1 $display("\n2:1 Encoder");
$monitor("input = %b | output = %b",in2, out1);
in2 = 2'b00;
#1 in2 = 2'b10;
#1 $display("\n4:2 Encoder");
$monitor("input = %b | output = %b",in4, out2);
in4 = 4'b0000;
#1 in4 = 4'b0001;
#1 in4 = 4'b0010;
#1 in4 = 4'b0100;
#1 $display("\n8:3 Encoder");
$monitor("input = %b | output = %b",in8, out3);
in8 = 8'b00000000;
#1 in8 = 8'b00000001;
#1 in8 = 8'b00000010;
#1 in8 = 8'b00000100;
#1 in8 = 8'b00001000;
#1 in8 = 8'b00010000;
#1 in8 = 8'b00100000;
#1 in8 = 8'b01000000;
#1 $display("\n16:4 Encoder");
$monitor("input = %h | output = %b",in16, out4);
in16 = 16'h0000;
#1 in16 = 16'h0001;
#1 in16 = 16'h0002;
#1 in16 = 16'h0004;
#1 in16 = 16'h0008;
#1 in16 = 16'h0010;
#1 in16 = 16'h0020;
#1 in16 = 16'h0040;
#1 in16 = 16'h0080;
#1 in16 = 16'h0100;
#1 in16 = 16'h0200;
#1 in16 = 16'h0400;
#1 in16 = 16'h0800;
#1 in16 = 16'h1000;
#1 in16 = 16'h2000;
#1 in16 = 16'h4000;
end
endmodule
The output is given below:
Why isn't the output changing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在所有
ENCODER
模块中,将:更改为:
ENCODER 模块中的代码仅在时间 0 执行一次,因为代码位于
initial
块中。您希望每次输入发生变化时模块输出都会更新。这是通过always @*
来完成的。如果您在模拟器中启用 SystemVerilog 功能,您可以使用
我看到输出变化:
In all your
ENCODER
modules, change:to:
The code in your ENCODER modules only executes once at time 0 because the code is in an
initial
block. You want the module outputs to be updated every time any input changes. This is accomplished withalways @*
.If you enable SystemVerilog features in your simulator, you could use
I see the output changing: