多路選擇器
? 在多路數據傳輸過程中,能夠將任意一路選出來的電路叫做數據選擇器,也稱多路選擇器。對于一個具有2^n個輸入和一個輸出的多路選擇器,有n個選擇變量,多路選擇器也是FPGA內部的一個基本資源,主要用于內部信號的選通。簡單的多路選擇器還可以通過級聯生成更大的多路選擇器。
譯碼器
? 譯碼是編碼的逆過程,在編碼時,每一種二級制都有特定的含義,都表示一個確定的信號。把代碼狀態的含義翻譯出來的過程叫做譯碼,實現該功能的電路叫做譯碼器。或者說,譯碼器是可以將輸入二進制代碼的狀態翻譯成輸出信號,以表示原來含義的電路。
? 譯碼器是一類? ? ? 多輸入多輸出? ?的組合邏輯電路器件,可以分為變量譯碼和顯示譯碼。
多路選擇器 if else
module mux_2_1
(input wire in1 ,input wire in2 ,input wire sel ,output reg out
);always@(*)beginif(sel == 1'b1)beginout = in1 ;endelsebeginout = in2 ;endendendmodule
?多路選擇器 case
module mux2_1
(input wire in1 ,input wire in2 ,input wire sel ,output reg out
);always@(*)begincase(sel)1'b1: out = in1 ;1'b0: out = in2 ;default: out = in1 ;endcaseendendmodule
多路選擇器? ? ?:;
????????
module mux2_1
(input wire in1 ,input wire in2 ,input wire sel ,output wire out
);assign out = (sel == 1'b1)?in1:in2;endmodule
譯碼器 if else?
module decode_3_8
(input wire in1 ,input wire in2 ,input wire in3 ,output reg [7:0] out );always@(*)beginif( {in1,in2,in3} == 3'b000 )beginout = 8'b0000_0001 ;endelse if( {in1,in2,in3} == 3'b001 )beginout = 8'b0000_0010 ;endelse if( {in1,in2,in3} == 3'b010 )beginout = 8'b0000_0100 ;endelse if( {in1,in2,in3} == 3'b011 )beginout = 8'b0000_1000 ;endelse if( {in1,in2,in3} == 3'b100 )beginout = 8'b0001_0000 ;endelse if( {in1,in2,in3} == 3'b101 )beginout = 8'b0010_0000 ;endelse if( {in1,in2,in3} == 3'b110 )beginout = 8'b0100_0000 ;endelse if( {in1,in2,in3} == 3'b111 )beginout = 8'b1000_0000 ;endelsebeginout = 8'b0000_0001 ;endendendmodule
譯碼器 case
module decode3_8
(input wire in1 ,input wire in2 ,input wire in3 ,output reg [7:0] out
);always@(*)
begin case({in1,in2,in3})3'b000 : out = 8'b0000_0001 ;3'b001 : out = 8'b0000_0010 ;3'b010 : out = 8'b0000_0100 ;3'b011 : out = 8'b0000_1000 ;3'b100 : out = 8'b0001_0000 ;3'b101 : out = 8'b0010_0000 ;3'b110 : out = 8'b0100_0000 ;3'b111 : out = 8'b1000_0000 ;default : out = 8'b0000_0001 ;endcase
endendmodule
仿真驗證
仿真文件編寫
`timescale 1ns/1nsmodule tb_decode3_8();reg in1 ;reg in2 ;reg in3 ;wire [7:0] out ;initialbeginin1 <= 1'b0 ;in2 <= 1'b0 ;in3 <= 1'b0 ;endalways #10 in1 <= {$random}%2 ;always #10 in2 <= {$random}%2 ;always #10 in3 <= {$random}%2 ;initialbegin$timeformat(-9.0,"ns",6) ;$monitor("@time %t , in1 = %b ,in2 = %b ,in3 = %b , out = %b ",$time,in1,in2,in3,out) ;enddecoder3_8 decoder3_8_inst
(.in1 (in1) ,.in2 (in2) ,.in3 (in3) ,.out (out)
);endmodule