数字电路-编码器,译码器,数据选择器,数值比较器Verilog实现

一、编码器

互斥编码器:就是任何时刻只允许一个输入信号有效,否则将产生错误编码。

优先编码器:允许输入信号同时有效,输出是对优先级别高的输入信号编码。

以8-3编码器为例:

8-3编码器:

module encoder_func(
                    //input
                    i_in,
                    //output
                    a_out
                    
                    );
     
input     [7:0]       i_in;
output    [2:0]       a_out;

reg       [2:0]       a_out;

always(*)begin
      case(i_in)
      8'b0000_0001:   a_out = 3'b000;
     8'b0000_0010:   a_out = 3'b001;
     8'b0000_0100:   a_out = 3'b010;
     8'b0000_1000:   a_out = 3'b011;
     8'b0001_0000:   a_out = 3'b100;
     8'b0010_0000:   a_out = 3'b101;
     8'b0100_0000:   a_out = 3'b110;
     8'b1000_0000:   a_out = 3'b111;
      default:        a_out = 3'b000;
   endcase
end

endmodule

 8-3优先编码器

module    prio_encoder_func(
                            //input
                            i_in,
                            //output
                            a_out
                            );
       
input          [7:0]         i_in;
output         [2:0]         a_out;

reg            [2:0]         a_out;

always @(*)begin
       if(i_in[7]) begin
           a_out = 3'b000;
    end
    else if(i_in[6]) begin
           a_out = 3'b001;
    end
    else if(i_in[5]) begin
           a_out = 3'b010;
    end
    else if(i_in[4]) begin
              a_out = 3'b011;
    end   
    else if(i_in[3]) begin
           a_out = 3'b100;
    end
    else if(i_in[2]) begin
           a_out = 3'b101;
    end
    else if(i_in[1]) begin
           a_out = 3'b110;
    end
    else if(i_in[0]) begin
              a_out = 3'b111;
       end  
    else begin
           a_out = 3'b000;
    end

end


endmodule

二、译码器

译码则是编码的逆过程,是把每一组代码的含义翻译出来的过程,完成译码功能的逻辑电路就被称为译码器。

module    decoder_func(
                       //input
        i_in,
        //output
        a_out
                       );
       
input        [2:0]        i_in;
output       [7:0]        a_out;

reg          [7:0]        a_out;

always @(*)begin
       case(i_in)
    3'b000:       a_out = 8'b0000_0001;
    3'b001:       a_out = 8'b0000_0010;
    3'b010:       a_out = 8'b0000_0100;
    3'b011:       a_out = 8'b0000_1000;
    3'b100:       a_out = 8'b0001_0000;
    3'b101:       a_out = 8'b0010_0000;
    3'b110:       a_out = 8'b0100_0000;
    3'b111:       a_out = 8'b1000_0000;
    default:      a_out = 8'b0000_0000;
       endcase

end

endmodule

三、数据选择器

所谓数据选择器就是从多个输入的逻辑信号中选择一个逻辑信号输出,实现数据选择功能的电路便成为数据选择器,也称为多路复用器。

module  data_selector_func(
                           //input
         a_in,
         d0,
         d1,
         d2,
         d3,
         d4,
         d5,
         d6,
         d7,
         //output
         b_out
                           );
        
input          [2:0]           a_in;
input                          d0;
input                          d1;
input                          d2;
input                          d3;
input                          d4;
input                          d5;
input                          d6;
input                          d7;

output                         b_out;

reg                            b_out;

always @(*)begin
       case(a_in)
    3'b000:        b_out = d0;
    3'b001:        b_out = d1;
    3'b010:        b_out = d2;
    3'b011:        b_out = d3;
    3'b100:        b_out = d4;
    3'b101:        b_out = d5;
    3'b110:        b_out = d6;
    3'b111:        b_out = d7;
       default:       b_out = 1'b0;
    endcase
end

endmodule

四、数值比较器

所谓数值比较器就是对两个数a/b 进行比较,以判断其大小的逻辑电路。

module num_compara_func(
                                             //input
                                            a_in,
                                            b_in,
                                           //output
                                           c_out
                                           );
      
input              a_in;
input              b_in;

output    [2:0]    c_out;

reg       [2:0]    c_out;

always @(*)begin
       if(a_in > b_in)begin
           c_out = 3'b001;
    end
    else if(a_in < b_in)begin
           c_out = 3'b010;
    end
    else begin
           c_out = 3'b100;
    end
end

endmodule



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值