247和-9是相同的位模式,所以算術是正確的。有符號與無符號是位模式的解釋。
注意:0-1只有255,8位數字,您已將它們定義爲256位數字。
以下示例應該有助於說明,我們使用$signed
和$unsigned
關鍵字,這些關鍵字會更改十進制表示形式的顯示方式,但基本二進制形式不會更改。
module tb;
parameter n=8;
logic [n-1:0] x;
logic y;
initial begin
x = 0;
$display("%1d", x-9);
$display("%1b", x-9);
$display("");
$display("%1d", $unsigned(x-9));
$display("%1b", $unsigned(x-9));
$display("");
$display("%1d", $signed(x-9));
$display("%1b", $signed(x-9));
$display("");
$finish;
end
endmodule
,輸出:
4294967287
11111111111111111111111111110111
4294967287
11111111111111111111111111110111
-9
11111111111111111111111111110111
爲了您的例子中,你只需要使用$unsigned
:
module tb;
parameter n=8;
logic [n-1:0] x;
logic y;
initial begin
x = 0;
if(0 >= $unsigned(x-9)) begin
y = 1;
end
else begin
y = 0;
end
$display("y: %b", y);
$finish;
end
endmodule
顯示,在基數變化無符號,小數給出不同的結果,我同意。但是我陷入了一個特殊的情況。 參數n = 256; input [n-1:0] x; 輸出y; 初始開始 x = 0; (0> = unsigned'(x-9)) y = 1;其他 y = 0; 結束 在這裏,我期待Y爲0,因爲我的'if'邏輯是「0小於0-9,即247」但是變爲1.我認爲我遵循錯誤的編碼方法。你能幫我解決這個問題嗎? – 2014-11-21 08:22:11
@VinayakHiremath我已經添加了一個例子,向您展示如何爲您的條件使用'$ unsigned'。如果這仍然導致問題添加評論和更新答案與最新信息。 – Morgan 2014-11-21 08:36:03