Verilog学习笔记二(多路选择器)

一、always语句块实现二选一逻辑

  1. always语句块里赋值的变量需要是reg型
  2. reg型变量赋值用带箭头的等号
  3. ^符号为异或,&为与

 verilog代码:

//2023/3/28 lzp
//二选一逻辑设计
`timescale 1ns/10ps
module select_gate(a, b, sel, y);
input a, b, sel;
output y;

//assign y=sel ? (a^b) : (a&b);

reg y;

always@(a or b or sel) begin
    if(sel == 1) begin
        y<=a^b;
    end
    else begin
        y<=a&b;
    end
end

endmodule

//test_bench
module select_gate_tb;
reg A, B, SEL;
wire Y;

select_gate select_gate(.a(A), .b(B), .sel(SEL), .y(Y));

initial begin
    A<=0;
    B<=1;
    SEL<=1;
    #2;
    SEL<=0;
    #2;
    $stop;
end
endmodule

 波形图:

 异名例化:

例化相关博客参考:最强Verilog例化说明_奇点FPGA的博客-CSDN博客

 二、case语句实现多路选择

 verilog代码:

//使用case实现的多路选择器
module muti_select_gate(a, b, sel, y);
input a, b;
input[1:0] sel;
output y;

reg y;

always@(a or b or sel) begin
    case(sel)
    2'b00: begin y<=a&b; end
    2'b01: begin y<=a|b; end
    2'b10: begin y<=a^b; end
    2'b11: begin y<=~(a^b); end
    endcase
end

endmodule

//test_bench of muti_select_gate
module muti_select_gate_tb;
reg A, B;
reg[1:0] SEL;
wire Y;

muti_select_gate muti_select_gate(.a(A), .b(B), .sel(SEL), .y(Y));

initial begin
    A<=0;
    B<=1;
    SEL<=2'b00;
    #2;
    SEL<=2'b01;
    #2;
    SEL<=2'b10;
    #2;
    SEL<=2'b11;
    #2;
    $stop;
end
endmodule

波形图:

 

 absel[3:2]指absel的高两位

 每过10ns absel自增1

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值