Verilog:HDLBits刷题-组合逻辑-基本门电路

前言

这是我第二次刷HDLBits的题,第一次是刚接触FPGA时,为了快速入门Verilog,第一次刷题跟着B站视频刷的,但是现在发现没有很大的用处,个人感觉还是有一点Verilog基础后,再来刷HDLBits会好一点,虽然很多人说这上面的题都很简单,但是还是值得刷一遍,里面几乎涵盖了Verilog的所有常用语法,并且还可以尝试用不同方法解同一道题。

代码

以下是我写的每道题的代码和思路


module top_module (
    input in,
    output out);

    assign out = in;

endmodule


// 输出接地
module top_module (
    output out);

    assign out = 1'd0;

endmodule


// 或非NOR,学会看图
module top_module (
    input in1,
    input in2,
    output out);

    assign out = ~(in1|in2);

endmodule


module top_module (
    input in1,
    input in2,
    output out);
    assign out = in1 & (~in2);

endmodule


// 异或、同或
module top_module (
    input in1,
    input in2,
    input in3,
    output out);

    wire out1;

    assign out1 = ~(in1^in2);
    assign out = out1 ^in3;

endmodule

// XOR 、 NOR  :X是异;N是非
module top_module( 
    input a, b,
    output out_and,
    output out_or,
    output out_xor,
    output out_nand,
    output out_nor,
    output out_xnor,
    output out_anotb
);

assign out_and = a & b ; 
assign out_or = a | b;
assign out_xor = a ^ b;
assign out_nand = ~(a&b);
assign out_nor = ~(a | b);
assign out_xnor = ~(a^b);
assign out_anotb = a &(~b);

endmodule

// 7421芯片
module top_module ( 
    input p1a, p1b, p1c, p1d,
    output p1y,
    input p2a, p2b, p2c, p2d,
    output p2y );

    assign p1y = ~(p1a & p1b & p1c & p1d);
    assign p2y = ~(p2a & p2b & p2c & p2d);

endmodule


// 真值表:需要化简
module top_module( 
    input x3,
    input x2,
    input x1,  // three inputs
    output f   // one output
);

assign f = (!x3 &  x2) | (x1 & x3);

endmodule


module top_module ( input [1:0] A, input [1:0] B, output z ); 
    assign z = (A==B)?1'b1:1'b0;
endmodule


module top_module (input x, input y, output z);
    assign z = (x^y) & x;
endmodule


// 看波形图写Verilog
module top_module ( input x, input y, output z );
// 方法一:
    // assign z = (!x & !y)|(x&y);
// 方法二:
    assign z = (x==y)?1'b1:1'b0;
endmodule


//====================  12  ======================
// 多模块例化

module top_module (input x, input y, output z);

wire z_a_1;
wire z_a_2;

wire z_b_1;
wire z_b_2;

wire z_a1b1;
wire z_a2b2;

assign z_a1b1 = z_a_1 | z_b_1;
assign z_a2b2 = z_a_2 & z_b_2;

assign z = z_a1b1 ^ z_a2b2;


mt2015_q4a mt2015_q4a_inst_1(
    .x(x), 
    .y(y), 
    .z(z_a_1));
mt2015_q4a mt2015_q4a_inst_2(
    .x(x), 
    .y(y), 
    .z(z_a_2));

mt2015_q4b mt2015_q4b_inst_1(
    .x(x), 
    .y(y), 
    .z(z_b_1));
mt2015_q4b mt2015_q4b_inst_2(
    .x(x), 
    .y(y), 
    .z(z_b_2));


endmodule

module  mt2015_q4a (input x, input y, output z);
    assign z = (x^y) & x;
endmodule

module mt2015_q4b ( input x, input y, output z );
// 方法一:
    // assign z = (!x & !y)|(x&y);
// 方法二:
    assign z = (x==y)?1'b1:1'b0;
endmodule


//====================  13  ======================
// 手机响铃、震动,下面2道题都是通过,描述写代码
module top_module (
    input ring,
    input vibrate_mode,
    output ringer,       // Make sound
    output motor         // Vibrate
);
//assign ringer = ring ;报错,这里的非不能省

assign ringer = ring & !vibrate_mode;
assign motor = ring & vibrate_mode;

endmodule


module top_module (
    input too_cold,
    input too_hot,
    input mode,
    input fan_on,
    output heater,
    output aircon,
    output fan
); 

assign heater = mode & too_cold;
assign aircon = !mode & too_hot;
assign fan = heater | aircon | fan_on;

endmodule


module top_module( 
    input [2:0] in,
    output [1:0] out );
// 不能这样写,这样就会??仿真看看
// reg count;
// integer  i;
// always @(*) begin
//     count = 1'b0;
//     for(i = 0;i<3;i++)begin
//         if(in[i])
//             count = count + 1'b1;
//     end

// end

// assign out = count;

assign out = in[0] + in[1] +in[2];
endmodule

// 为下一题做铺垫
module top_module( 
    input [3:0] in,
    output [2:0] out_both,
    output [3:1] out_any,
    output [3:0] out_different );

    assign out_both[0] = in[0]&in[1];
    assign out_both[1] = in[1]&in[2];
    assign out_both[2] = in[2]&in[3];

    assign out_any[1] = in[0]| in[1];
    assign out_any[2] = in[1]| in[2];
    assign out_any[3] = in[2]| in[3];

    assign out_different[0] = in[0] ^ in[1];
    assign out_different[1] = in[1] ^ in[2];
    assign out_different[2] = in[2] ^ in[3];
    assign out_different[3] = in[0] ^ in[3];

endmodule

// 在异步FIFO中,将二进制转换成格雷码时用到类似思想
module top_module( 
    input [99:0] in,
    output [98:0] out_both,
    output [99:1] out_any,
    output [99:0] out_different );

    assign out_both = in[99:1] & in[98:0];
    assign out_any = in[99:1] | in[98:0];
    assign out_different = {in[0],in[99:1]}^in;


endmodule





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值