Verilog HDLBits 第八期:3.1.2 Multiplexer
目錄
?前言
3.1.2.1 2 to 1multiplexer(Mux2to1)
Solution:
3.1.2.2 2 to 1 bus multiplexer(Mux2to1v)
Solution:
3.1.2.3 9?to 1multiplexer(Mux9to1v)
Solution:
3.1.2.4 256 to 1multiplexer(Mux256to1)
Solution:
?3.1.2.5 256 to 1 4-bit multiplexer(Mux256to1v)
Solution:
?前言
HDLbits網站如下
Problem sets - HDLBits (01xz.net)
從本期開始我們繼續HDLbits第三章Circuits的學習,本期的內容是3.1.2Multiplexer
3.1.2.1 2 to 1multiplexer(Mux2to1)
創建一個一位寬的2選1數據選擇器。當sel=0時,選擇a;當sel=1時,選擇b
期望的答案長度:1行
Hint:三目運算符(cond?? iftrue?: iffalse)的可讀性更強
Solution:
module top_module (input a,input b,input sel,output out );assign out = (sel & b) | (~sel & a); // 數據選擇器由與門和或門表示// 三目運算符的可讀性更強,特別是面對向量時:assign out = sel ? b : a;endmodule3.1.2.2 2 to 1 bus multiplexer(Mux2to1v)
創建一個100位寬的2選1數據選擇器。當sel=0時,選擇a;當sel=1時,選擇b
期望的答案長度:1行
Hint:三目運算符(cond?? iftrue?: iffalse)的可讀性更強
Solution:
module top_module (input [99:0] a,input [99:0] b,input sel,output [99:0] out );assign out = sel ? b : a;// 下面這種形式不能工作:// assign out = (sel & b) | (~sel & a);endmodule本題中,sel是1位向量,而a、b皆是100位向量,使用assign out = (sel & b) | (~sel & a);
sel&b表示b[99:1]并沒有驅動邏輯,由時序圖可以看到,out=a
而換成assign out = ({100{sel}} & b) | ({100{~sel}} & a); 可以正常運行。
3.1.2.3 9?to 1multiplexer(Mux9to1v)
創建一個16位寬的9選1數據選擇器。當sel=0時,選擇a;當sel=1時,選擇b,以此類推。對于沒有用到的項(sel=9到15),所有輸出位都設為“1”
Hint:對于這么多選項,case語句可能會很有用
Solution:
module top_module (input [15:0] a,input [15:0] b,input [15:0] c,input [15:0] d,input [15:0] e,input [15:0] f,input [15:0] g,input [15:0] h,input [15:0] i,input [3:0] sel,output logic [15:0] out );// case語句只能在過程塊(always塊)中使用。這是一個組合電路,所以使用組合always塊always @(*) beginout = '1; // '1 是所有位都設為1的特殊文字語法 '0, 'x, 'z 也是有效的// 我習慣先聲明out的默認值,來代替case語句中的default項case (sel)4'h0: out = a;4'h1: out = b;4'h2: out = c;4'h3: out = d;4'h4: out = e;4'h5: out = f;4'h6: out = g;4'h7: out = h;4'h8: out = i;endcaseendendmodule3.1.2.4 256 to 1multiplexer(Mux256to1)
創建一個1位寬的256選1數據選擇器。256位輸入都打包成一個256位的輸入向量。sel=0,選擇in[0];sel=1,選擇in[1];sel=2,選擇in[2]
期望的答案長度:1行
Hint:
- 有了這么多選項,case 語句就沒那么有用了。
- 只要綜合器可以確定所選位的寬度是恒定的,則向量索引可以是可變的。特別是,使用變量索引從向量中選擇一位是可行的。
Solution:
module top_module (input [255:0] in,input [7:0] sel,output out );assign out = in[sel];endmodule?3.1.2.5 256 to 1 4-bit multiplexer(Mux256to1v)
創建一個4位寬的256選1數據選擇器。256個4bit輸入都打包成一個1024位的輸入向量。sel=0,選擇in[3:0];sel=1,選擇in[7:4];sel=2,選擇in[11:8]
期望的答案長度:1-5行
Hint:
- 有了這么多選項,case 語句就沒那么有用了。
- 只要綜合器可以確定所選位的寬度是恒定的,則向量索引可以是可變的。但他并不總是擅長于此。有error?"... is not a constant",意味著它不能證明選擇寬度是恒定的。特別是 in[ sel*4+3 : sel*4 ] 不起作用。
- 位切片(“索引向量部分選擇”,自 Verilog-2001 起)具有更緊湊的語法。
Solution:
module top_module (input [1023:0] in,input [7:0] sel,output [3:0] out );assign out = {in[sel*4+3], in[sel*4+2], in[sel*4+1], in[sel*4+0]};endmodule我們不能部分選擇多個位,但我們可以一次選擇一位、重復四次,然后將它們連接在一起。
或者,片選多個比特的方法有兩種:
- assign out = in[sel*4 +: 4];? //選擇從索引“sel*4”開始,然后選擇總寬度為 4 位的 (+:) 索引號遞增。
- assign out = in[sel*4+3 -: 4];?? ?// 選擇從索引 "sel*4+3" 開始,然后選擇總寬度為 4 位的 (-:) 索引號遞減。
- 請注意:寬度必須是恒定的(本題中是4)
還算比較簡單,同時學習了片選的新語法
總結
以上是生活随笔為你收集整理的Verilog HDLBits 第八期:3.1.2 Multiplexer的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4 轮拿下腾讯 Offer (附真题)
- 下一篇: 水文遥测终端(水文遥测终端机)遥测终端机