FPGA-牛客网-Verilog快速入门2

一、4位数值比较器电路

在进行多位数值比较的时候,首先将不同数值的高位进行比较,若高位有差异,则直接比较出数值的大小;如果数值的高位相同,则进行后面一位的比较,依次类推,可比较出不同数值的大小。

`timescale 1ns/1ns

module comparator_4(
	input		[3:0]       A   	,
	input	   [3:0]		B   	,
 
 	output	 wire		Y2    , //A>B
	output   wire        Y1    , //A=B
    output   wire        Y0      //A<B
);


    assign Y2=((A[3]>B[3]) || ((A[3]==B[3])&&(A[2]>B[2]))||((A[3]==B[3])&&(A[2]==B[2])&&(A[1]>B[1])||(A[3]==B[3])&&(A[2]==B[2])&&(A[1]==B[1])&&(A[0]>B[0])));
    assign Y1=((A[3]==B[3])&&(A[2]==B[2])&&(A[1]==B[1])&&(A[0]==B[0]));
    assign Y0=(~Y1)&&(~Y2);

endmodule

二、4bit超前进位加法器电路

待写...

三、优先编码器电路①

该代码中使用了casez

在case语句中,关于case、casez、casex三个不同的语法,case进行全等匹配,casez忽略?或z对应的位进行匹配,casex忽略x、?、z对应的位进行匹配

`timescale 1ns/1ns

module encoder_0(
   input      [8:0]         I_n   ,
   
   output reg [3:0]         Y_n   
);
//注意高低位
always@(*)begin
    casez(I_n)
        9'b1_1111_1111:Y_n=4'b1111;
        9'b0_????_????:Y_n=4'b0110;  //其中?,被视为高阻状态,casez中视为不必考虑的状态
        9'b1_0???_????:Y_n=4'b0111;
        9'b1_10??_????:Y_n=4'b1000;
        9'b1_110?_????:Y_n=4'b1001;
        9'b1_1110_????:Y_n=4'b1010;
        9'b1_1111_0???:Y_n=4'b1011;
        9'b1_1111_10??:Y_n=4'b1100;
        9'b1_1111_110?:Y_n=4'b1101;
        9'b1_1111_1110:Y_n=4'b1110;
        default:Y_n=4'b0000;
    endcase
end
endmodule

四、用优先编码器①实现键盘编码电路

`timescale 1ns/1ns
module encoder_0(
   input      [8:0]         I_n   ,
   
   output reg [3:0]         Y_n   
);

always @(*)begin
   casex(I_n)
      9'b111111111 : Y_n = 4'b1111;
      9'b0xxxxxxxx : Y_n = 4'b0110;
      9'b10xxxxxxx : Y_n = 4'b0111;
      9'b110xxxxxx : Y_n = 4'b1000;
      9'b1110xxxxx : Y_n = 4'b1001;
      9'b11110xxxx : Y_n = 4'b1010;
      9'b111110xxx : Y_n = 4'b1011;
      9'b1111110xx : Y_n = 4'b1100;
      9'b11111110x : Y_n = 4'b1101;
      9'b111111110 : Y_n = 4'b1110;
      default      : Y_n = 4'b1111;
   endcase    
end 
     
endmodule

module key_encoder(
      input      [9:0]         S_n   ,         
 
      output wire[3:0]         L     ,
      output wire              GS
);

wire   [3:0]         Y_n;
encoder_0 u0(
   .I_n   (S_n[9:1]),
        
   .Y_n   (Y_n)
);
 
assign L = ~Y_n;
 
assign GS = ~(S_n[0] & Y_n[0] & Y_n[1] & Y_n[2] & Y_n[3]);

endmodule

五、优先编码器Ⅰ

该部分使用了casez以及拼位操作,最终在将拼位操作中,所对应的数值进行拆分

例:{Y_temp, GS_temp, EO_temp} = 5'b111_1_0中,Y_temp为111、GS_temp为1、EO_temp为0

`timescale 1ns/1ns

module encoder_83(
   input      [7:0]       I   ,
   input                  EI  ,
   
   output wire [2:0]      Y   ,
   output wire            GS  ,
   output wire            EO    
);

reg [2:0]Y_temp;
reg      GS_temp;
reg      EO_temp;

always@(*)begin
if(~EI)begin
    Y_temp<=3'b0;
    GS_temp<=1'b0;
    EO_temp<=1'b0;
end
else begin
    casez({EI,I})
    9'b1_0000_0000: {Y_temp,GS_temp,EO_temp}<=5'b000_01;
    9'b1_1???_????: {Y_temp, GS_temp, EO_temp} = 5'b111_1_0;
    9'b1_01??_????: {Y_temp, GS_temp, EO_temp} = 5'b110_1_0;
    9'b1_001?_????: {Y_temp, GS_temp, EO_temp} = 5'b101_1_0;
    9'b1_0001_????: {Y_temp, GS_temp, EO_temp} = 5'b100_1_0;
    9'b1_0000_1???: {Y_temp, GS_temp, EO_temp} = 5'b011_1_0;
    9'b1_0000_01??: {Y_temp, GS_temp, EO_temp} = 5'b010_1_0;
    9'b1_0000_001?: {Y_temp, GS_temp, EO_temp} = 5'b001_1_0;
    9'b1_0000_0001: {Y_temp, GS_temp, EO_temp} = 5'b000_1_0;
    default:begin
        {Y_temp, GS_temp, EO_temp} = 5'b000_0_0;
    end
    endcase
end
end
assign  Y = Y_temp;
assign  GS = GS_temp;
assign  EO = EO_temp;


endmodule

六、使用8线-3线优先编码器Ⅰ实现16线-4线优先编码器

使用8-3编码器实现16-4编码器需要用到两片8-3编码器进行串联,如图所示为串联后的16-4编码器

 在进行例化的时候,一定要注意高位在前,低位在后;在连线的时候需要注意各个端口的输入与输出

`timescale 1ns/1ns
module encoder_83(
   input      [7:0]       I   ,
   input                  EI  ,
   
   output wire [2:0]      Y   ,
   output wire            GS  ,
   output wire            EO    
);
assign Y[2] = EI & (I[7] | I[6] | I[5] | I[4]);
assign Y[1] = EI & (I[7] | I[6] | ~I[5]&~I[4]&I[3] | ~I[5]&~I[4]&I[2]);
assign Y[0] = EI & (I[7] | ~I[6]&I[5] | ~I[6]&~I[4]&I[3] | ~I[6]&~I[4]&~I[2]&I[1]);

assign EO = EI&~I[7]&~I[6]&~I[5]&~I[4]&~I[3]&~I[2]&~I[1]&~I[0];

assign GS = EI&(I[7] | I[6] | I[5] | I[4] | I[3] | I[2] | I[1] | I[0]);
//assign GS = EI&(| I);
         
endmodule

module encoder_164(
   input      [15:0]      A   ,
   input                  EI  ,
   
   output wire [3:0]      L   ,
   output wire            GS  ,
   output wire            EO    
);

wire       EO1 ;
wire [2:0] Y0  ;
wire [2:0] Y1  ;
wire       GS0 ;
wire       GS1 ;

encoder_83 inst_encoder83_1(
        .I   (A[7:0]),
        .EI  (EO1),
        .Y   (Y0),
        .GS  (GS0),
        .EO  (EO)  
);

encoder_83 inst_encoder83_2(
        .I   (A[15:8]),
        .EI  (EI),
        .Y   (Y1),
        .GS  (GS1),
        .EO  (EO1)  
);

assign  L[3] = GS1;
assign  GS = GS0 | GS1;
assign  L[2] = Y0[2]|Y1[2];
assign  L[1] = Y0[1]|Y1[1];
assign  L[0] = Y0[0]|Y1[0];


endmodule

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值