HDLBits自学笔记3:Verilog language.Modules Hierarchy

这篇具有很好参考价值的文章主要介绍了HDLBits自学笔记3:Verilog language.Modules Hierarchy。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Modules

在顶层模块中实例化模块mod_a,其端口描述:

module mod_a ( input in1, input in2, output out );
HDLBits自学笔记3:Verilog language.Modules Hierarchy,HDLBits,fpga开发

module top_module ( input a, input b, output out );
    // 按信号名称连线
    mod_a u1(
        .in1(a),
        .in2(b),
        .out(out)
    );
    // 按信号位置连线
    // mod_a u2(a, b, out);
endmodule

Connection ports by position

在顶层模块中按信号位置实例化模块mod_a,其端口描述:

module mod_a ( output, output, input, input, input, input );
HDLBits自学笔记3:Verilog language.Modules Hierarchy,HDLBits,fpga开发

module top_module ( 
    input a, 
    input b, 
    input c,
    input d,
    output out1,
    output out2
);
    mod_a u_1(out1, out2, a, b, c, d);
endmodule

Connecting ports by name

在顶层模块中按信号名实例化mod_a,其端口描述:

module mod_a ( output out1, output out2, input in1, input in2, input in3, input in4);
HDLBits自学笔记3:Verilog language.Modules Hierarchy,HDLBits,fpga开发

module top_module ( 
    input a, 
    input b, 
    input c,
    input d,
    output out1,
    output out2
);
    mod_a u1(
        .in1 (a),
        .in2 (b),
        .in3 (c),
        .in4 (d),
        .out1(out1),
        .out2(out2)
    );
endmodule

Three modules

提供D触发器模块my_dff,实例化3个D触发器,链式连接它们实现一个3bit移位寄存器

D触发器my_dff的端口描述:module my_dff ( input clk, input d, output q );

HDLBits自学笔记3:Verilog language.Modules Hierarchy,HDLBits,fpga开发

module top_module ( input clk, input d, output q );
    wire q1, q2;
    my_dff u1(clk, d, q1);
    my_dff u2(clk, q1, q2);
    my_dff u3(clk, q2 ,q);
endmodule

Modules and vectors

提供8位D触发器my_dff8,实例化3个8位D触发器,链式连接它们,实现长度为3的8位移位寄存器,并实现一个4选1多路选择器,选择输出打0、1、2、3拍的输入信号

my_dff8的端口描述:my_dff8 ( input clk, input [7:0] d, output [7:0] q );

HDLBits自学笔记3:Verilog language.Modules Hierarchy,HDLBits,fpga开发

module top_module ( 
    input clk, 
    input [7:0] d, 
    input [1:0] sel, 
    output reg [7:0] q 
);
    wire [7:0] q1, q2, q3;
    my_dff8 u1(clk, d, q1);
    my_dff8 u2(clk, q1, q2);
    my_dff8 u3(clk, q2, q3);
    
    always @(*) begin
        case(sel)
            2'b00: q = d;
            2'b01: q = q1;
            2'b10: q = q2;
            2'b11: q = q3;
        endcase
    end
endmodule

Adder 1

提供16位加法器模块add16,实例化2个该模块,实现32位加法器,一个计算低16位,一个计算高16位

add16的端口描述:module add16 ( input[15:0] a, input[15:0] b, input cin, output[15:0] sum, output cout );

HDLBits自学笔记3:Verilog language.Modules Hierarchy,HDLBits,fpga开发

module top_module(
    input [31:0] a,
    input [31:0] b,
    output [31:0] sum
);
    wire cout;
    add16 u1(
        .a   (a[15:0]),
        .b   (b[15:0]),
        .cin (1'b0),
        .sum (sum[15:0]),
        .cout(cout)
    );
    add16 u2(
        .a   (a[31:16]),
        .b   (b[31:16]),
        .cin (cout),
        .sum (sum[31:16]),
        .cout()
    );
endmodule

Adder 2

编写1位全加器add1,系统会实例化16个1位全加器,实现16位加法器add16,在此基础上,实例化2个16位加法器,实现32位加法器:

HDLBits自学笔记3:Verilog language.Modules Hierarchy,HDLBits,fpga开发

module top_module (
    input [31:0] a,
    input [31:0] b,
    output [31:0] sum
);
    wire cout;
    add16 u1(
        .a   (a[15:0]),
        .b   (b[15:0]),
        .cin (1'b0),
        .sum (sum[15:0]),
        .cout(cout)
    );
    
    add16 u2(
        .a   (a[31:16]),
        .b   (b[31:16]),
        .cin (cout),
        .sum (sum[31:16]),
        .cout()
    );
endmodule

module add1 ( input a, input b, input cin,   output sum, output cout );
    assign cout = (a & b) | (b & cin) | (a & cin);
    assign sum = a ^ b ^ cin;
endmodule

Carry-select adder

行波进位加法器的缺点是延迟太大,每一个全加器都必须等待前一个全加器的进位产生,这导致加法器延迟大。

进位选择加法器是对此的一种改进,低16位加法器保持不变,高16位加法器实例化两个,一个假设低16位进位为0,一个假设为1,用2选1选择器选择使用哪个结果,如下图:

HDLBits自学笔记3:Verilog language.Modules Hierarchy,HDLBits,fpga开发

module top_module(
    input [31:0] a,
    input [31:0] b,
    output [31:0] sum
);
    wire low_cout;
    wire [15:0] high_sum0, high_sum1, high_sum;
    
    add16 low(
        .a    (a[15:0]),
        .b    (b[15:0]),
        .cin  (1'b0),
        .sum  (sum[15:0]),
        .cout (low_cout)
    );
    
    add16 high0(
        .a    (a[31:16]),
        .b    (b[31:16]),
        .cin  (1'b0),
        .sum  (high_sum0),
        .cout ()
    );
    
    add16 high1(
        .a    (a[31:16]),
        .b    (b[31:16]),
        .cin  (1'b1),
        .sum  (high_sum1),
        .cout ()
    );
    
    assign sum[31:16] = low_cout ? high_sum1 : high_sum0;
endmodule

Adder-subtractor

选择性的将加法器的一个输入反转,就能从加法器构建加减器,此时进位也为1,等同于反转后+1。一个加减器能够实现两种操作:(a + b + 0)(a + ~b + 1),提供16位加法器add16,建立32位加减器。

HDLBits自学笔记3:Verilog language.Modules Hierarchy,HDLBits,fpga开发文章来源地址https://www.toymoban.com/news/detail-516996.html

module top_module(
    input [31:0] a,
    input [31:0] b,
    input sub,
    output [31:0] sum
);
    wire cout;
    add16 u0(
        .a   (a[15:0]),
        .b   ({16{sub}} ^ b[15:0]),// 异或有选择反相性
        .cin (sub),
        .sum (sum[15:0]),
        .cout(cout)
    );
    
    add16 u1(
        .a   (a[31:16]),
        .b   ({16{sub}} ^ b[31:16]),
        .cin (cout),
        .sum (sum[31:16]),
        .cout()
    );
endmodule

到了这里,关于HDLBits自学笔记3:Verilog language.Modules Hierarchy的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【HDLBits 刷题 2】Verilog Language(2)Vectors 部分

    目录 写在前面 Vectors Vector0 Vector1 Vector2 Vectorgates Gates4 Vector3 Vectorr  Vector4 Vector5 来到了 Verilog 语法的矢量部分,这部分仍然比较简单,所以只给出题目、代码和仿真结果,其他不多赘述。 构建一个具有一个3位输入的电路,然后输出相同的矢量,并将其拆分为三个单独的1位输

    2024年02月07日
    浏览(27)
  • HDLBits_第1章_Verilog Language(已完结)

    目录 1. Verilog Language 1.1 Basics 1.1.1 Simple wire 1.1.2 Four wires 1.1.3 Inverter  1.1.4 AND gate 1.1.5 NOR gate 1.1.6 XNOR gate 1.1.7 Declaring wires 1.1.8 7458 chip 1.2 Vectors 1.2.1 Vectors 1.2.2 Vectors in more detail  1.2.3 Vector part select  1.2.4 Bitwise operators  1.2.5 Four-input gates 1.2.6 Vector concatenation operator 1.2.7 Vector reve

    2024年02月08日
    浏览(26)
  • FPGA自学笔记--串口通信发送多字节数据(verilog版)

            关于uart协议实现这部分大家可以参考我上一篇的博客。《FPGA自学笔记--串口通信实现(vivadoverilog版)》。在上一篇博客中,主要实现了将单字节的数据,我们其实就是用上一篇博客的模块来实现多字节数据的发送。         在真实的数据传输过程中,我们不

    2023年04月17日
    浏览(30)
  • Verilog刷题[hdlbits] :Module add

    You are given a module add16 that performs a 16-bit addition. Instantiate two of them to create a 32-bit adder. One add16 module computes the lower 16 bits of the addition result, while the second add16 module computes the upper 16 bits of the result, after receiving the carry-out from the first adder. Your 32-bit adder does not need to handle carry-in (assu

    2024年02月06日
    浏览(33)
  • verilog学习 | HDLBits:在线学习答案

    HDLBits 在提供 Verilog 基础语法教程的同时,还能够在线仿真 Verilog 模块。 以下是各单元解法答案。希望可以帮助您了解 Verilog 的工作原理。 HDLBits 在提供 Verilog 基础语法教程的同时,还能够在线仿真 Verilog 模块。 ⚠️ 注意:顶层的模块名称和端口名称 top_module 不能更改,否

    2024年02月16日
    浏览(25)
  • hdlbits系列verilog解答(always块if语句)-31

    if 语句通常创建一个 2 对 1 多路复用器,如果条件为 true,则选择一个输入,如果条件为 false,则选择另一个输入。 always @(*) begin if (condition) begin out = x; end else begin out = y; end end 这等效于使用带有条件运算符的连续赋值: assign out = condition ? (x : y); 使用if语句不当时会产生不想

    2024年02月06日
    浏览(28)
  • hdlbits系列verilog解答(8位宽移位寄存器)-24

    这项练习是module_shift移位寄存器的延伸。模块端口不是只有单个引脚,我们现在有以向量作为端口的模块,您将在其上附加线向量而不是普通线网数据。与 Verilog 中的其他位置一样,端口的向量长度不必与连接到它的导线匹配,但这会导致向量的零填充或截断。本练习不使用

    2024年02月08日
    浏览(22)
  • Verilog单边沿检测和双边沿检测的方法(HDLBits例题)

    1.单边沿检测 边沿检测是用来检测某一信号是否发送了从0至1或者从1至0的变化,有同步和异步之分。 同步边沿检测:是使用一个基准时钟,即在同一个时钟下来检测一个信号的上升沿或者下降沿。 异步边沿检测:是利用D触发器来实现边沿检测。 HDLBits例题: 单边沿检测例题

    2024年02月11日
    浏览(29)
  • verilog1 HDLbits:12 hour clock(12小时计时器)

    HDL bit -12 hour clock题目地址 翻译:创建一组适合用作 12 小时制的计数器。计数器由一个快速运行的 clk 计时,每当时钟增加(即每秒一次)时,ena 就会有一个脉冲。 reset 将时钟重置为凌晨 12:00。pm 为 0 表示 AM,1 表示 PM。 hh、mm 和 ss 是两个 BCD(二进制编码十进制)数字,分别表示

    2024年02月19日
    浏览(28)
  • HDLBits学习笔记——移位寄存器

    为了方便做笔记,从移位寄存器(Shift Registers)这章开始按章节做记录。 1.   4-bit Shift Registers 题目: Build a 4-bit shift register (right shift), with asynchronous reset, synchronous load, and enable. areset: Resets shift register to zero. load: Loads shift register with data[3:0] instead of shifting. ena: Shift right (q[3

    2023年04月08日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包