數(shù)字硬件建模SystemVerilog-歸約運算符(Reduction operators)
經(jīng)過幾周的更新,SV核心部分用戶自定義類型和包內(nèi)容已更新完畢,接下來就是RTL表達式和運算符。
馬上HDLBits-SystemVerilog版本也開始準(zhǔn)備了,基本這一部分完成后就開始更新~

介紹
歸約運算符對單個操作數(shù)的所有位執(zhí)行運算,并返回標(biāo)量(1位)結(jié)果。表5-9列出了歸約運算符。
表5-9:RTL建模的歸約運算符
歸約運算符包括一個NAND和一個NOR運算符,這是按位運算符所沒有的。歸約AND OR 和 XOR 運算符一次執(zhí)行一位操作,從最右邊的位(最低有效位)向最左邊的位(最高有效位)移動。歸約NAND、NOR和XNOR運算符首先分別執(zhí)行歸約AND、OR或XOR運算,然后反轉(zhuǎn)1位結(jié)果。
AND、NAND或NOR運算符是X-optimistic。對于歸約運算符,如果操作數(shù)中的任何位為0,結(jié)果將為1’b0。對于歸約NAND,如果操作數(shù)中的任何位為0,結(jié)果將為1’b1。類似地,對于歸約運算符,或者如果操作數(shù)中的任何位為l,結(jié)果將為1’b1。對于歸約NOR,如果操作數(shù)中的任何位為l,結(jié)果將是1’b0.歸約XOR和XNOR運算符是X-pessimistic。如果操作數(shù)的任何一位是X或Z,結(jié)果將是1’bx。表5-10顯示了幾個示例值的每個歸約運算符的結(jié)果。
表5-10:歸約操作的示例結(jié)果 
示例5-6說明了一個小型RTL模型,該模型利用歸約運算符檢查數(shù)據(jù)值的正確奇偶性,圖5-6顯示了該RTL模型綜合結(jié)果。
示例5-6:使用歸約運算符:使用異或的奇偶校驗
// //Book,"RTLModelingwithSystemVerilogforASICandFPGADesign" //byStuartSutherland // //Paritycheckerusingevenparity,registerederrorflag // //Copyright2016,StuartSutherland.Allrightsreserved. // //Version1.0 // // //User-definedtypedefinitions // `begin_keywords"1800-2012"http://useSystemVerilog-2012keywords packagedefinitions_pkg; typedefstruct{ logic[7:0]data; logicparity_bit; }data_t; endpackage:definitions_pkg `end_keywords // //Paritycheckerusingevenparity,registerederrorflag. //Thecombineddatavalueplusparitybitshouldalwayshave //anevennumberofbitssetto1 // `begin_keywords"1800-2012"http://useSystemVerilog-2012keywords moduleparity_checker importdefinitions_pkg::*; (inputdata_tdata_in,//9-bitstructureinput inputclk,//clockinput inputrstN,//active-lowasynchronousreset outputlogicerror//setifparityerrordetected ); timeunit1ns/1ns; always_ff@(posedgeclk,negedgerstN) if(!rstN)error<=?0; ???else???????error?<=?^{data_in.parity_bit,?data_in.data}; ?????//?reduction-XOR?returns?1?if?an?odd?number?of?bits?are ?????//?set?in?the?combined?data?and?parity_bit endmodule:?parity_checker `end_keywords
該文件的仿真文件如下:
//
//Book,"RTLModelingwithSystemVerilogforASICandFPGADesign"
//byStuartSutherland
//
//Testbench
//
//Copyright2016,StuartSutherland.Allrightsreserved.
//
//Version1.0
//
`begin_keywords"1800-2012"
moduletest
importdefinitions_pkg::*;
(outputlogicrstN,
outputdata_tdata_in,
inputlogicerror,
inputlogicclk
);
timeunit1ns/1ns;
//generatestimulus
initialbegin
$timeformat(-9,0,"ns",6);//nanoseconds,noprecision,6columns
rstN<=?0;????????????????????//?reset?DUT?(active?low)
????repeat(2)??@(negedge?clk)?;???//?hold?reset?for?2?clock?cycles
????rstN?=?1;?????????????????????//?remove?reset
????repeat?(10)?begin
??????@(negedge?clk)?;
??????data_in.data?=?$urandom();
??????data_in.parity_bit?=?$urandom()%2;??//?randomly?wrong?parity?value
??????@(negedge?clk)?check_results;
????end
????@(negedge?clk)?$finish;
??end
??//?verify?results
??task?check_results;
????$write("At?%t:?data=%b??parity_bit=%b:??",?$time,?data_in.data,?data_in.parity_bit);
????if?(^data_in.data?===?data_in.parity_bit)?begin:?good_data_in
??????$write("Good?data_in.?EXPECT:?error?=?0,?ACTUAL:?%b?",?error);
??????if?(error?===?1'b0)?$display("?OK");
??????else????????????????$display("?ERROR!");
????end:?good_data_in
????else?begin:?bad_data_in
??????$write("Bad?data_in.??EXPECT:?error?=?1,?ACTUAL:?%b?",?error);
??????if?(error?===?1'b1)?$display("?OK");
??????else????????????????$display("?ERROR!");
????end:?bad_data_in
??endtask
endmodule:?test
`end_keywords
`begin_keywords?"1800-2012"
module?top;
??timeunit?1ns/1ns;
??import?definitions_pkg::*;
??parameter?WIDTH?=?8;
??logic??clk,?rstN;
??data_t?data_in;
??logic??error;
??test???????????test?(.*);
??parity_checker?dut??(.*);
??initial?begin
????clk?<=?0;
????forever?#5?clk?=?~clk;
??end
endmodule:?top
`end_keywords
圖5-6:示例5-6的綜合結(jié)果:歸約異或(奇偶校驗)

審核編輯:湯梓紅
-
RTL
+關(guān)注
關(guān)注
1文章
393瀏覽量
62237 -
運算符
+關(guān)注
關(guān)注
0文章
173瀏覽量
11879
原文標(biāo)題:SystemVerilog-歸約運算符(Reduction operators)
文章出處:【微信號:Open_FPGA,微信公眾號:OpenFPGA】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
數(shù)字硬件建模SystemVerilog-按位運算符
關(guān)于數(shù)字硬件建模SystemVerilog
C語言程序設(shè)計--運算符與表達式
單片機C語言教程-運算符和表達式
基于運算符信息的數(shù)學(xué)表達式檢索技術(shù)
SystemVerilog-運算符/表達式規(guī)則
關(guān)于RTL表達式和運算符
RTL表達式和運算符
運算符/表達式規(guī)則
位邏輯運算符與表達式

RTL表達式和運算符
評論