HDLBits答案(5)_Generate实例化模块
Verilog的更多功能
HDLBits鏈接
三目運算符(形式:condition ? if_true : if_false)
verilog中有跟c語言類似的三目運算符,這可以用于在一行中根據條件選擇兩個值中的一個,而不用在組合always塊中使用if-then。下面給出一些示例:
(0 ? 3 : 5) // This is 5 because the condition is false. (sel ? b : a) // A 2-to-1 multiplexer between a and b selected by sel.always @(posedge clk) // A T-flip-flop.q <= toggle ? ~q : q;always @(*) // State transition logic for a one-input FSMcase (state)A: next = w ? B : A;B: next = w ? A : B;endcaseassign out = ena ? q : 1'bz; // A tri-state buffer((sel[1:0] == 2'h0) ? a : // A 3-to-1 mux(sel[1:0] == 2'h1) ? b :c )題目描述:
給定4個無符號數,求最小值。無符號數可以用比較運算符(a <b)進行比較。
Solution:
module top_module (input [7:0] a, b, c, d,output [7:0] min);wire [7:0] mintemp1;wire [7:0] mintemp2;assign mintemp1 = (a<b)? a:b;assign mintemp2 = (c<mintemp1)?c:mintemp1;assign min = (d<mintemp2)?d:mintemp2;endmodule縮位運算符
縮位運算符可以對向量的各個位進行和、或和異或操作,產生1位的輸出:
& a[3:0] // AND: a[3]&a[2]&a[1]&a[0]. Equivalent to (a[3:0] == 4'hf) | b[3:0] // OR: b[3]|b[2]|b[1]|b[0]. Equivalent to (b[3:0] != 4'h0) ^ c[2:0] // XOR: c[2]^c[1]^c[0]我們可以通過翻轉上述縮位運算的輸出來獲得NAND、NOR和XNOR縮位運算的輸出。
題目描述1:奇偶校驗經常被用作傳輸數據時檢測錯誤的簡單方法。創建一個電路,為一個8位字節計算一個奇偶校驗位(它將在1個字節的基礎上增加1位)。我們將使用“偶數”奇偶校驗,其中奇偶校驗位只是所有8位數據位的異或。
Solution1:
module top_module (input [7:0] in,output parity); assign parity = ^ in; endmodule題目描述2:構建一個組合電路,包括100個輸入,in[99:0];
3個輸出:
- out_and:100個與門的輸出
- out_or:100個或門的輸出
- out_xor:100個異或門的輸出
Solution2:
module top_module( input [99:0] in,output out_and,output out_or,output out_xor );assign out_and = ∈assign out_or = |in;assign out_xor = ^in; endmodulefor循環的組合邏輯:向量順序翻轉
題目描述:
給定一個100位的輸入向量,翻轉它的位順序。
Solution:
module top_module( input [99:0] in,output [99:0] out );integer i;always @(*) beginfor(i=0;i<100;i++) beginout[i]=in[99-i];endend endmodulefor循環的組合邏輯:255bit的數1操作
題目描述:
“計數”電路對輸入向量中的1進行計數。為一個255位輸入向量建立一個“計數”電路。
Solution:
module top_module( input [254:0] in,output [7:0] out );integer i;integer temp;always @(*) beginout = 8'd0;for(i=0;i<255;i++) beginout = out + in[i];endend endmodule[David說]:注意,這里會產生鎖存器,做題可以,實際運用需斟酌。
鎖存器會對電路產生哪些影響呢?
所以,在ASIC設計中,除了CPU高速電路,或者RAM這種對面積很敏感的電路,一般不提倡用鎖存器。
循環生成語句
[David說]:Verilog中的generate語句常用于編寫可配置的、可綜合的RTL的設計結構。它可用于創建模塊的多個實例化,或者有條件的實例化代碼塊。
generate循環的語法與for循環語句的語法很相似。但是在使用時必須先在genvar聲明中聲明循環中使用的索引變量名,然后才能使用它。genvar聲明的索引變量被用作整數用來判斷generate循環。genvar聲明可以是generate結構的內部或外部區域,并且相同的循環索引變量可以在多個generate循環中,只要這些環不嵌套。genvar只有在建模的時候才會出現,在仿真時就已經消失了。
Verilog中generate循環中的generate塊可以命名也可以不命名。如果已命名,則會創建一個generate塊實例數組。如果未命名,則有些仿真工具會出現警告,因此,最好始終對它們進行命名。
題目描述1:
通過實例化100個全加法器,創建一個100位行波進位加法器。
Solution1:
module top_module( input [99:0] a, b,input cin,output [99:0] cout,output [99:0] sum );genvar i;generatefor(i=0;i<100;i++) begin:adderif(i==0)assign{cout[0],sum[0]}=a[0]+b[0]+cin;elseassign{cout[i],sum[i]}=a[i]+b[i]+cout[i-1];end endgenerate endmodule題目描述2:
為您提供了一個名為bcd_fadd的BCD一位加法器,它的輸入為兩個BCD數字和進位輸入信號,并生成求和輸出和進位輸出信號。
module bcd_fadd {input [3:0] a,input [3:0] b,input cin,output cout,output [3:0] sum );實例化bcd_fadd的100個副本,以創建一個100位的BCD行波進位加法器。
Solution2:
module top_module( input [399:0] a, b,input cin,output cout,output [399:0] sum );wire [99:0] cout_temp;genvar i;generatefor(i=0;i<100;i++) begin:bcd_faddif(i == 0)bcd_fadd bcd_inst(a[3:0],b[3:0],cin,cout_temp[0],sum[3:0]);elsebcd_fadd bcd_inst(a[4*i+3:4*i],b[4*i+3:4*i],cout_temp[i-1],cout_temp[i],sum[4*i+3:4*i]);endassign cout=cout_temp[99];endgenerate endmodule總結
本節設計大量的設計技巧,如for循環的巧用,批量進行例程的例化,這些可以在設計過程中簡化工作量。
總結
以上是生活随笔為你收集整理的HDLBits答案(5)_Generate实例化模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDLBits答案(4)_如何避免生成锁
- 下一篇: iOS之地理位置及定位系统 -- 入门