HDLBits从零开始——第1题到第10题答案

文章提供了一系列Verilog代码示例,用于构建各种基本逻辑门,包括非门、与门、或非门和同或门。此外,还展示了如何使用中间导线连接多个门,并模拟7458芯片的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

第1题:Getting Started

第2题:Output Zero

第3题:Simple wire

第4题:Four wires

第5题:Inverter

第6题:AND gate

第7题:NOR gate

第8题:XNOR gate

第9题:Declaring wires

第10题:7458 chip


第1题:Getting Started

构建一个没有输入和一个输出的电路,该输出应始终驱动 1(或逻辑高电平)。

module top_module
( 
    output one 
);

// Insert your code here
    assign one = 1'b1;

endmodule

第2题:Output Zero

构建一个没有输入和一个输出常数 0 的输出的电路。

module top_module
(
    output zero
);

// Module body starts after semicolon
    assign  zero = 1'b0;

endmodule

第3题:Simple wire

构建一个具有一个输入和一个输出的模块。

module top_module
(   
    input   wire    in, 
    output  wire    out 
);

    assign  out =   in;

endmodule

第4题:Four wires

构建一个具有 3 个输入和 4 个输出的模块。

module top_module
( 
    input a,b,c,
    output w,x,y,z 
);

    assign  w = a;
    assign  x = b;
    assign  y = b;
    assign  z = c;

endmodule

第5题:Inverter

构建实现一个非门的模块。

module top_module
(   
    input in, 
    output out 
);
    
    assign  out = ~in;

endmodule

第6题:AND gate

构建实现一个与门的模块。

module top_module
( 
    input a, 
    input b, 
    output out 
);
    
    assign  out = a & b;  

endmodule

第7题:NOR gate

构建实现一个或非门的模块,或非门是输出反相的或门。 当用 Verilog 编写时,NOR 函数需要两个运算符。

module top_module
( 
    input a, 
    input b, 
    output out 
);

    //或非:先或后非,注意和缩位或非'~|'的使用区别
    //错误:out = a ~| b;
    assign  out = ~(a | b); 

endmodule

第8题:XNOR gate

构建实现 一个同或门的模块。

module top_module
( 
    input a, 
    input b, 
    output out 
);

    //同或:相同为1
    assign  out = ~(a ^ b); 

endmodule

第9题:Declaring wires

学习构建两条中间导线将 AND 和 OR 门连接在一起。

`default_nettype none
module top_module
(
    input a,
    input b,
    input c,
    input d,
    output out,
    output out_n   
); 

    /*-----方案1-----*/
    // wire    ab_out;
    // wire    cd_out;
    // wire    mid_out;
    
    // assign  ab_out = a & b;
    // assign  cd_out = c & d;
    // assign  mid_out = ab_out | cd_out;
    
    // assign  out = mid_out;
    // assign  out_n = ~mid_out;
    
    /*-----方案2-----*/
    wire    mid_out;
    
    assign  mid_out = (a & b) | (c & d);
    
    assign  out = mid_out;
    assign  out_n = ~mid_out;

endmodule

第10题:7458 chip

构建一个与 7458 芯片具有相同功能的模块,它有 10 个输入和 2 个输出, 可以选择使用一条语句来驱动每条输出线,或者选择声明(四条)线用作中间信号,其中每条内部线由一个与门的输出驱动。 如需额外练习,请同时尝试这两种方法。

module top_module 
( 
    input p1a, p1b, p1c, p1d, p1e, p1f,
    output p1y,
    input p2a, p2b, p2c, p2d,
    output p2y 
);
    
    /*-----方案1-----*/
    // wire    p2ab;
    // wire    p2cd;
    // wire    p1abc;
    // wire    p1def;
    
    // assign  p2ab = p2a & p2b;
    // assign  p2cd = p2c & p2d;
    // assign  p1abc = p1a & p1b & p1c;
    // assign  p1def = p1d & p1e & p1f;
    
    // assign  p1y = p1abc | p1def;
    // assign  p2y = p2ab | p2cd;
    
    /*-----方案2-----*/
    assign  p1y = (p1a & p1b & p1c) | (p1d & p1e & p1f);
    assign  p2y = (p2a & p2b) | (p2c & p2d);

endmodule
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值