【Verilog基础】5.选择器

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]
0003
0010
0101
0111
1002
1012
1102
1112

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

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值