2 - 组合逻辑代码设计和仿真

2 - 组合逻辑代码设计和仿真

用问号冒号语句实现二选一:

image-20211117154659020

用 always 语句块实现组合逻辑:

image-20211117154720487

用 if-else 实现二选一:
//2021.11.17 lyw
//2 choose 1 logic design
`timescale 1ns/10ps
module fn_sw (
            a,
            b,
            sel,
            y
);
input       a;
input       b;
input       sel;
output      y;

//use 'assign' to implement combinatorial logic
//assign      y=sel?(a^b):(a&b);

//use 'always' to implement combinatorial logic
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

//------testbench of fn_sw-----
module fn_sw_tb;
reg         a,b,sel;
wire        y;
fn_sw fn_sw (
            .a(a),
            .b(b),
            .sel(sel),
            .y(y)
);

initial begin
            a=0;b=0;sel=0;
    #10     a=0;b=0;sel=1;
    #10     a=0;b=1;sel=0;
    #10     a=0;b=1;sel=1;
    #10     a=1;b=0;sel=0;
    #10     a=1;b=0;sel=1;
    #10     a=1;b=1;sel=0;
    #10     a=1;b=1;sel=1;
    #10     $stop;
end

endmodule
二选一仿真波形:

image-20211117153738701

四选一组合逻辑实现:

image-20211117160715418

四选一代码:

关键:用 case 语句实现多路选择,以及用 always # 语句遍历逻辑值。

//2021.11.17 lyw
//4 choose 1
`timescale 1ns/10ps
module fn_sw_4 (
                a,
                b,
                sel,
                y
);
    input       a;
    input       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

//-----testbench of fn_sw_4---------
module fn_sw_4_tb;
reg[3:0]        absel;
wire            yy;
fn_sw_4 fn_sw_4 (
                .a(absel[0]),
                .b(absel[1]),
                .sel(absel[3:2]),
                .y(yy)
);
initial begin
            absel<=0;
    #200    $stop;
end

always #10 absel<=absel+1;

endmodule
四选一仿真波形:

image-20211117171358234


PS:

学习内容总结自网络,主讲教师为北京交通大学李金城老师。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值