HDLBits答案(7)_Verilog多路选择器
Verilog多路選擇器
HDLBits鏈接
定義
多路選擇器(Multiplexer)簡稱多路器,它是一個多輸入、單輸出的組合邏輯電路,在數(shù)字系統(tǒng)中有著廣泛的應用。它可以根據(jù)地址碼(選擇碼)的不同,從多個輸入數(shù)據(jù)流中選取一個,讓其輸出到公共的輸出端。
部分練習題
題目描述1:實現(xiàn)一個位寬為1的2-1的多路選擇器。當sel=0,選擇a,當sel=1,選擇b。
Solution1:
module top_module( input a, b, sel,output out ); assign out = sel?b:a; endmodule題目描述2:實現(xiàn)一個位寬為100的2-1的多路選擇器。當sel=0,選擇a,當sel=1,選擇b。
Solution2:
module top_module( input [99:0] a, b,input sel,output [99:0] out );assign out = sel?b:a; endmodule題目描述3:創(chuàng)建一個位寬為16的9-1的多路選擇器。sel=0選擇a, sel=1選擇b,以此類推。對于未使用的sel值(sel=9~15),將所有輸出位設置為1。
Solution3:
module top_module( input [15:0] a, b, c, d, e, f, g, h, i,input [3:0] sel,output [15:0] out );always @(*) begincase(sel)4'd0: out = a;4'd1: out = b;4'd2: out = c;4'd3: out = d;4'd4: out = e;4'd5: out = f;4'd6: out = g;4'd7: out = h;4'd8: out = i;default:out = 16'hffff;endcaseendendmodule題目描述4:創(chuàng)建一個位寬為1的256-1的多路選擇器。256個輸入都被打包成一個256位的輸入向量。sel=0表示選擇in[0],sel=1表示選擇in[1],以此類推。
Solution4:
module top_module( input [255:0] in,input [7:0] sel,output out );assign out = in[sel]; endmodule題目描述5:創(chuàng)建一個位寬為4的256-1的多路選擇器。256個4位輸入都被打包成一個1024位的輸入向量。sel=0選擇in[3:0],sel=1選擇in[7:4],sel=2選擇in[11:8],以此類推。
Solution5:
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]}; endmodule[David說]:題5中不能使用assign out = in[sel*4+3:sel*4],此時會報錯,因為該等式不能證明選擇的這個位寬是個常數(shù)。所以我們繼續(xù)按bit進行選取然后進行拼接。
總結(jié)
- 學習了多路選擇器的相關(guān)知識。
- 學習了可以根據(jù)向量索引進行數(shù)據(jù)的選擇,要注意的是被選取數(shù)據(jù)的位寬是否為常數(shù)。
總結(jié)
以上是生活随笔為你收集整理的HDLBits答案(7)_Verilog多路选择器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: js 日期控件laydate使用
- 下一篇: 【excel技巧读书笔记001】清除打开
