2013-01-22 158 views
1

這是一小段Verilog代碼。我希望它會返回三個相同的結果,所有8位表示爲-1。Verilog中的表達符號

module trivial; 

    reg we; 
    reg [7:0] c; 

    initial 
     begin 
     c = 8'd3; 
     we = 1'b1; 
     $display ("res(we) = %d", (we ? (-$signed(c))/8'sd2 : 8'd0)); 
     $display ("res(1) = %d", (1'b1 ? (-$signed(c))/8'sd2 : 8'd0)); 
     $display ("res = %d", (-$signed(c))/8'sd2); 
     end 

endmodule 

簡言之,我有標準(1364-2001)的版本在4.1.5節說,除法輪朝向零,所以-3/2 = -1。它還在第4.5節中說,操作符僅取決於操作數(編輯:但僅限於「自定義表達式」;事實證明有必要讀取關於符號的部分以及寬度爲的部分)。因此,具有劃分的子表達式應該不受其所使用的上下文的影響,對於涉及$ signed的子表達式也是如此。所以結果應該都一樣嗎?

三個不同的模擬器不同意我。只有兩個人彼此認同。明顯的原因是使用未簽名的部門而不是我期望的簽名部門。 (-3 = 253和253/2 = 126.5)

有人可以告訴我,如果任何模擬器是正確的,爲什麼? (見下文)我顯然一定會錯過一些東西,但請問?非常感謝。 編輯:見上面我失蹤的內容。我現在認爲在Icarus中存在一個錯誤,另外兩個模擬器是正確的

注意:三元選擇中未使用的值似乎沒有任何區別,無論是有符號還是無符號。 編輯:這是不正確,也許我忘了試之前修改測試保存有符號數的ModelSim的

Altera版:

$ vsim work.trivial -do 'run -all' 
Reading C:/altera/12.1/modelsim_ase/tcl/vsim/pref.tcl 

# 10.1b 

# vsim -do {run -all} work.trivial 
# Loading work.trivial 
# run -all 
# res(we) = 126 
# res(1) = 126 
# res = -1 

GPL Cver

GPLCVER_2.12a of 05/16/07 (Cygwin32). 
Copyright (c) 1991-2007 Pragmatic C Software Corp. 
    All Rights reserved. Licensed under the GNU General Public License (GPL). 
    See the 'COPYING' file for details. NO WARRANTY provided. 
Today is Mon Jan 21 18:49:05 2013. 
Compiling source file "trivial.v" 
Highest level modules: 
trivial 

res(we) = 126 
res(1) = 126 
res = -1 

伊卡洛斯的Verilog 0.9。 6

$ iverilog.exe trivial.v && vvp a.out 
res(we) = 126 
res(1) = -1 
res = -1 

回答

1

NCSIM給出:

res(we) = 126 
res(1) = 126 
res  = -1 

但是,如果所有輸入多路複用器被簽名,我得到:

$display ("res(we) = %d", (we ? (-$signed(c))/8'sd2 : 8'sd0)); //last argument now signed 
$display ("res(1) = %d", (1'b1 ? (-$signed(c))/8'sd2 : 8'sd0)); 
$display ("res  = %d",   (-$signed(c))/8'sd2); 

res(we) = -1 
res(1) = -1 
res  = -1 

記住,如果我們做一個無符號數算術作爲無符號做任何運算,利用位選擇時,同樣的情況:

reg signed [7:0] c; 
c = c[7:0] + 7'sd1; //<-- this is unsigned 

在多路複用器是一個單行表達式的一部分的示例中,我相信這在邏輯上被壓平進行優化,因此,有符號/無符號的所有參數被取入考慮。

+0

非常感謝您的答覆。 – user1999655