1、二选一选择器:
module mux2to1(s,d0,d1,y);
input wire s ,d0,d1;
output wire y;
assign y= s ? d1:d0;
endmodule
2、四选一选择器:
module mux4to1(s,d,y);
input wire [1:0] s ;
input wire [3:0] d ;
output reg y;
always@(*) begin
case(s[1:0])
2'd0: y =d[0];
2'd1: y =d[1];
2'd2: y =d[2];
default:y = d[3];
endcase
end
endmodule
3、带有优先级的多路选择器:
带有优先级的多路选择器 | |
din [2:0] | dout [7:0] |
000 | 3 |
001 | 0 |
010 | 1 |
011 | 1 |
100 | 2 |
101 | 2 |
110 | 2 |
111 | 2 |
module mux(din,dout);
input wire [2:0] din;
output reg [7:0] dout;
always@(*) begin
if(din[2])
dout = 'd2;
else if(din[1])
dout = 'd1;
else if(din[0])
dout = 'd0;
else dout = 'd3;
end
endmodule
4、多路选择器:
尽可能使用case,casex和casez非必要不用。
module mux(din,dout);
input wire [2:0] din ;
output reg [7:0] dout;
always@(*) begin
case(din)
3'b000: dout=8'h01;
3'b001: dout=8'h02;
3'b010: dout=8'h04;
3'b011: dout=8'h08;
3'b100: dout=8'h10;
3'b101: dout=8'h20;
3'b110: dout=8'h40;
3'b111: dout=8'h80;
end
endmodule