HDLBits练习---Basic Gates

1.Exams/m2014 q4i

module top_module (
    output out);

    assign out = 1'b0;
    
endmodule

2.Exams/m2014 q4e

module top_module (
    input in1,
    input in2,
    output out);

    nor (out , in1 , in2);
    
endmodule

3.Exams/m2014 q4f

module top_module (
    input in1,
    input in2,
    output out);

    assign out = in1 & (~in2);
    
endmodule

4.Exams/m2014 q4g

module top_module (
    input in1,
    input in2,
    input in3,
    output out);

    wire a,c;
    xor (a , in1 , in2);  //xor异或
    assign c = ~a;
    xor (out , c , in3);
    
endmodule

5.Gates

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
);
    
    and (out_and , a , b);
    or (out_or , a , b);
    xor (out_xor , a , b);   //异或
    nand (out_nand , a , b);  //与非门
    nor (out_nor , a , b);  //或非门
    xnor (out_xnor , a , b);  //同或
    assign out_anotb = a & (~b);

endmodule

6.7420

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

    nand (p1y , p1a, p1b, p1c, p1d );
    nand (p2y , p2a, p2b, p2c, p2d );

endmodule

7.Truthtable1

实现下列真值表:

module top_module( 
    input x3,
    input x2,
    input x1,  // three inputs
    output f   // one output
);
    
    assign f = (~x3 & x2) + (x3 & x1);

endmodule

8.Mt2015 eq2

创建一个具有两个2位输入 A[1:0]和 B[1:0]的电路,并产生一个输出 z。如果 A = B,则 z 的值应为1,否则 z 应为0。

module top_module ( input [1:0] A, input [1:0] B, output z ); 

    assign z = (A == B) ? 1 : 0;
    
endmodule

9.Mt2015 q4a

Module A 应该实现函数 z = (x^y)& x。

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

    assign z = (x ^ y) & x;
    
endmodule

10.Mt2015 q4b

同或门

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

    xnor (z , x , y);  //根据波形图写出表达式看出是同或门
    
endmodule

11.Mt2015 q4

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

    wire z1,z2,z3,z4,z5,z6;
    A IA1 (.x(x),.y(y),.z(z1));
    B IB1 (.x(x),.y(y),.z(z2));
    A IA2 (.x(x),.y(y),.z(z3));
    B IB2 (.x(x),.y(y),.z(z4));
    or (z5 , z1 , z2);
    and (z6 , z3 , z4);
    xor (z , z5 , z6);  //异或门
endmodule
    
module A (input x, input y, output z);
    
    assign z = (x ^ y) & x;
    
endmodule

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

    xnor (z , x , y);  //根据波形图写出表达式看出是同或门
    
endmodule

12.Ringer

假设您正在设计一个电路来控制手机的铃声和振动电机,每当电话需要从来电振铃时,您的电路必须打开振铃器或电机,但不能同时打开两者。如果手机处于振动模式,请打开电机。否则,请打开铃声。

module top_module (
    input ring,
    input vibrate_mode,
    output ringer,       // Make sound
    output motor         // Vibrate
);

    assign ringer = ((ring == 1) & (vibrate_mode == 0)) ? 1 : 0;
    assign motor = ((ring == 1) & (vibrate_mode == 1)) ? 1 :0;
    
endmodule

13.Thermostat

加热/冷却恒温器控制加热器(冬季)和空调(夏季),实施一个电路,该电路将根据需要打开和关闭加热器、空调和鼓风机风扇。
这恒温器可以处于以下两种模式之一:加热和冷却。在加热模式下,当太冷时打开加热器,但不要使用空调。在冷却模式下,当空调过热时打开空调,但不要打开加热器。当加热器或空调打开时,还要打开风扇以使空气循环。此外,即使加热器和空调关闭,用户也可以要求风扇打开。
解题思路:根据题意找到输入输出,并列出输入对应的输出。

module top_module (
    input too_cold,
    input too_hot,
    input mode,
    input fan_on,
    output heater,
    output aircon,
    output fan
); 
    
    assign heater = (mode == 1 && too_cold == 1) ? 1 : 0;
    assign aircon = (mode == 0 && too_hot == 1) ? 1 : 0;
    assign fan = (fan_on == 1 || heater == 1 || aircon ==1) ? 1 : 0;

endmodule

14.Popcount3

计算3位输入向量中“1”的数量。

module top_module( 
    input [2:0] in,
    output [1:0] out );
    
    int i;
    always @ (*) begin
        out = 0;
        for (i=0;i<3;i=i+1) begin
            if (in[i]==1) begin
               out = out + 1;    //计数器
            end
            else out = out;
        end
    end
    
endmodule

15.Gatesv

module top_module( 
    input [3:0] in,
    output [2:0] out_both,
    output [3:1] out_any,
    output [3:0] out_different );
    
    int i;
    always @(*) begin
        for (i=0;i<3;i=i+1) begin
            if ((in[i] && in[i+1]) == 1) 
                out_both[i] = 1;
            else out_both[i] = 0;
        end
        for (i=1;i<4;i=i+1) begin
            if ((in[i] || in[i-1]) == 1) 
                out_any[i] = 1;
            else out_any[i] = 0;
        end
    end
    assign out_different = {in[3]^in[0] , in[3]^in[2] , in[1]^in[2] , in[0]^in[1]};  //异或^,不同取1,相同取0
    
endmodule


endmodule

16.Gatesv100

module top_module( 
    input [99:0] in,
    output [98:0] out_both,
    output [99:1] out_any,
    output [99:0] out_different );

    int i;
    always @(*) begin
        for (i=0;i<99;i=i+1) begin
            if ((in[i] && in[i+1]) == 1) 
                out_both[i] = 1;
            else out_both[i] = 0;
        end
        for (i=1;i<100;i=i+1) begin
            if ((in[i] || in[i-1]) == 1) 
                out_any[i] = 1;
            else out_any[i] = 0;
        end
    end
    assign out_different = in[99:0] ^ {in[0] , in[99:1]};  //即是{0,99,98,……,1}{99,98,……,1,0}按位与
    
endmodule

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值