Verilog if语句总是返回false
我的Verilog if语句总是返回false,verilog,Verilog,我的if/else语句不起作用,需要帮助。不管我进入什么地方,它都不起作用。它只将1赋值给操作数2。if/else语句有什么错 module TestMod; // the "main" thing parameter STDIN = 32'h8000_0000; // I/O address of keyboard input channel reg [7:0] str [1:3]; // typing in 2 chars at a ti
if/else
语句不起作用,需要帮助。不管我进入什么地方,它都不起作用。它只将1赋值给操作数2
。if/else
语句有什么错
module TestMod; // the "main" thing
parameter STDIN = 32'h8000_0000; // I/O address of keyboard input channel
reg [7:0] str [1:3]; // typing in 2 chars at a time (decimal # and Enter key)
reg [4:0] X, Y; // 5-bit X, Y to sum
wire [4:0] S; // 5-bit Sum to see as result
wire C5; // like to know this as well from result of Adder
reg operand, operand2; //operand
wire E; //exception overflow inidicator
AddSub addOrSub(X, Y, S, C5, E, operand2);
initial begin
$display("Enter X (two digit 00~15 (since max is 01111)):");
...
$display("Enter Y (two digit 00~15 (since max is 01111)):");
...
$display("Enter + or -");
operand = $fgetc(STDIN);
if (operand == "+") begin
operand2 = 0;
end
else begin
operand2 = 1;
end
#1; // wait until Adder getsthem processed
$display("operand", operand);
$display("X =",X, " (%b",X, ") Y =",Y, " (%b",Y,")", "C0=",operand2);
$display("Result =", S," (%b",S, ") C5 = ",C5);
end
endmodule
根据IEEE标准1800-2017第21.3.4.1节,一次读取一个字符,
$fgetc
:
从文件中读取一个字节
一个字节是8位。如果用户在STDIN上输入+
,则$fgetc
返回字符串+
,该字符串在数字上下文中由ASCII值43表示
您将操作数
声明为reg
,这是一个1位值。因此,操作数
只能取值为0或1。如果$fgetc
返回字符串+
,即43(或'b0010_1011),操作数
仅获取LSB,即1
(操作数==“+”)
将1与43进行比较,这是false,然后执行else
代码
正如您在注释中指出的,您需要将操作数声明为8位宽:
reg [7:0] operand;
所以操作数2不等于1或0?即使我在字符串中输入“+”,操作数2也总是等于1。我的意思是将其作为操作数=$fgetc(STDIN)。如果我将状态切换为If(操作数=“-”),则开始操作数2=1;结束或开始操作数2=0;end then Operator 2始终为0,反之亦然,我的操作数需要reg[7:0]才能正常工作这很有意义:在C中,我不会使用该代码,因为返回键未被处理,可能会妨碍您。在这里,它可能被添加到操作数中。当您尝试在循环中运行代码时,它仍然可能会产生干扰!