HDLBits答案(3)_Verilog模块的例化与调用
HDLBits_Verilog模块的例化与调用
HDLBits链接
模块
只要使用的所有模块都属于同一个项目,就可以通过在模块内部实例化一个模块来创建模块的层次结构。一个模块的代码不能在另一个模块的主体中编写(不同模块的代码不是嵌套的)。
有两种常见的方式将wire信号连接到端口上,分别是按位置和按名称连接。
按位置:mod_a instance1 ( wa, wb, wc );
按名称:mod_a instance2 ( .out(wc), .in1(wa), .in2(wb) );
- By position:
mod_a instance1 ( wa, wb, wc );
题目描述:完成mod_a模块的调用
Solution:
module top_module ( input a, input b, output out );mod_a david(.out(out),.in1(a),.in2(b));
endmodule
[David说]:模块调用两种方式:按位置调(简洁但不稳,顺序不可乱),按名称调(繁琐但很稳,顺序可变)
按位置连接端口
题目描述:
已有一个名为mod_a的模块,它有2个输出和4个输入。您必须按位置将这6个端口连接到顶层模块的端口out1、out2、a、b、c和d,并按顺序进行连接。
给定如下的模块:
module mod_a ( output, output, input, input, input, input );
Solution:
module top_module ( input a, input b, input c,input d,output out1,output out2
);mod_a david(out1,out2,a,b,c,d);
endmodule
按名称连接端口
题目描述:按名称调模块mod_a
module top_module ( input a, input b, input c,input d,output out1,output out2
);mod_a(.out1(out1),.out2(out2),.in1(a),.in2(b),.in3(c),.in4(d));
endmodule
三个模块的调用
题目描述:
已有一个模块my_dff,其中有两个输入和一个输出(D触发器)。实例化三个D触发器然后将它们连接在一起,实现一个长度为3的移位寄存器。clk端口需要连接到所有my_dff实例。
已有模块: module my_dff ( input clk, input d, output q );
Solution:
module top_module ( input clk, input d, output q );wire temp1;wire temp2;my_dff block1(clk,d,temp1);my_dff block2(clk,temp1,temp2);my_dff block3(clk,temp2,q);
endmodule
[David说]:内部调用多个模块时,定义合理的wire信号连接内部的模块。
模块输入为向量
题目描述:
已有一个模块my_dff8,它具有两个输入和一个输出(实现一组8位的D触发器)。实例化其中的三个,然后将它们连接在一起,实现一个长度为3的8位宽移位寄存器。另外,构造一个4-1多路选择器,根据sel[1:0]选择输出值。本质上,sel选择的是延迟输入的周期。
已有模块: module my_dff8 ( input clk, input [7:0] d, output [7:0] q );
Solution:
module top_module ( input clk, input [7:0] d, input [1:0] sel, output [7:0] q
);wire [7:0] out1;wire [7:0] out2;wire [7:0] out3;my_dff8 block1(clk,d,out1);my_dff8 block2(clk,out1,out2);my_dff8 block3(clk,out2,out3);always @(*) begincase(sel)2'b00:q=d;2'b01:q=out1; 2'b10:q=out2; 2'b11:q=out3; endcaseend
endmodule
[David说]:wire与wire类型之间的连接直接用非阻塞赋值=连接即可。
加法器模块1
题目描述:用两个带进位的16bit加法器组成一个32bit加法器。
module add16 (input[15:0] a, input[15:0] b, input cin, output[15:0] sum, output cout);
Solution:
module top_module(input [31:0] a,input [31:0] b,output [31:0] sum
);wire [15:0] low_out;wire [15:0] high_out;wire temp_cout;wire cout;add16 low (a[15:0],b[15:0],0,low_out,temp_cout);add16 high(a[31:16],b[31:16],temp_cout,high_out,cout);assign sum={high_out,low_out};
endmodule
[David说]:注意采用流拼接简化代码,无用信号接口作为输出也可以不进行管脚信号的定义。
加法器模块2
题目描述:
自定义一个1bit的全加器,构成一个16bit全加器,然后用两个16bit全加器完成32bit加法模块的搭建,忽略进位。
1、定义一个1bit全加器。2、在顶层完成两个16bit全加器的调用。
Solution:
module top_module (input [31:0] a,input [31:0] b,output [31:0] sum
);wire temp_cout;add16 low (a[15:0],b[15:0],0,sum[15:0],temp_cout);add16 high(a[31:16],b[31:16],temp_cout,sum[31:16]);
endmodulemodule add1 ( input a, input b, input cin, output sum, output cout );assign {cout,sum} = a + b + cin;
endmodule
进位选择加法器
题目描述:
在这个练习中,已有和上一个练习相同的模块add16,本题将两个带进位的16位数字相加,并生成一个输出进位和16位的和。需构造一个16位2-1多路选择器。
module add16 ( input[15:0] a, input[15:0] b, input cin, output[15:0] sum, output cout );
[David说]:提前加法器进位的计算,用资源换速度。
Solution:
module top_module(input [31:0] a,input [31:0] b,output [31:0] sum
);wire [15:0] highout0;wire [15:0] highout1;wire cout;add16 low(a[15:0],b[15:0],0,sum[15:0],cout);add16 high0(a[31:16],b[31:16],0,highout0);add16 high1(a[31:16],b[31:16],1,highout1);assign sum[31:16]=cout? highout1:highout0;
endmodule
加法器→减法器
题目描述:利用sub信号和两个16bit的全加器,实现32bit的加减法运算。
对输入的sub信号进行判断,如果sub=0,则输出(a + b + 0);如果sub=1,则输出(a + ~b + 1)。
已有模块:
module add16 ( input[15:0] a, input[15:0] b, input cin, output[15:0]sum, output cout );
tip: Use a 32-bit wide XOR gate to invert the b
input whenever sub
is 1.
Solution:
module top_module(input [31:0] a,input [31:0] b,input sub,output [31:0] sum
);wire [31:0] b_processed;wire cout_temp;assign b_processed = b ^ {32{sub}};add16 lower(a[15:0],b_processed[15:0],sub,sum[15:0],cout_temp);add16 higher(a[31:16],b_processed[31:16],cout_temp,sum[31:16]);
endmodule
[David说]:巧用sub信号对输入b处理,妙!
总结:
学习了模块的例化与调用,深刻理解对着电路图写HDL这句话。
HDLBits答案(3)_Verilog模块的例化与调用相关推荐
- (64)Verilog HDL多模块重复例化
(64)Verilog HDL多模块重复例化 1.1 目录 1)目录 2)FPGA简介 3)Verilog HDL简介 4)Verilog HDL多模块重复例化 5)结语 1.2 FPGA简介 FPG ...
- (65)Verilog HDL多模块重复例化:generate for
(65)Verilog HDL多模块重复例化:generate for 1.1 目录 1)目录 2)FPGA简介 3)Verilog HDL简介 4)Verilog HDL多模块重复例化:genera ...
- (66)Verilog HDL模块参数化例化
(66)Verilog HDL模块参数化例化 1.1 目录 1)目录 2)FPGA简介 3)Verilog HDL简介 4)Verilog HDL模块参数化例化 5)结语 1.2 FPGA简介 FPG ...
- (67)Verilog HDL模块条件例化
(67)Verilog HDL模块条件例化 1.1 目录 1)目录 2)FPGA简介 3)Verilog HDL简介 4)Verilog HDL模块条件例化 5)结语 1.2 FPGA简介 FPGA( ...
- HDLBits答案(11)_Verilog计数器
Verilog计数器 HDLBits链接 前言 今天更新一个小节内容:计数器.计数器可以说是我们接触数字电路以后用的最频繁的模块之一了,无论是项目.应聘还是将来的工作,计数器都无处不在. 题库 题目描 ...
- HDLBits答案(18)_Verilog有限状态机(5)
Verilog有限状态机(5) HDLBits链接 前言 今天继续更新状态机小节的习题. 题库 题目描述1: 第一道题目比较容易,题目中的in信号包含了一个起始位(0),8个数据位和一个停止位(1), ...
- HDLBits答案(19)_Verilog有限状态机(6)
Verilog有限状态机(6) HDLBits链接 前言 今天继续更新状态机小节的习题. 题库 Fsm hdlc 同步帧检测涉及对数据的连续位流进行解码,以寻找指示帧(数据包)开始和结束的位模式. 6 ...
- HDLBits答案(13)_Verilog移位寄存器附加题
Verilog移位寄存器 HDLBits链接 前言 今天更新一节内容,该小节题目不多,共三道,但技巧性挺强. 题库 题目描述1:各单元的下一状态是此时当前单元相邻两位的异或. 在这个电路中,创建一个5 ...
- HDLBits答案(12)_Verilog移位寄存器
Verilog移位寄存器 HDLBits链接 前言 今天更新一节寄存器相关内容,其中涉及CRC校验的内容是用线性反馈移位寄存器搭建而成的. 题库 题目描述1: 构建一个4bit的移位寄存器(右移),含 ...
最新文章
- 耐能团队论文登上《自然·电子学》:集成忆阻器与CMOS以实现更好的AI
- 解决VS2015安装Android SDK 后文件不全及更新问题
- linux avahi-daemon进程 网络服务 简介
- C++11语言新特性-《C++标准库(第二版)》读书笔记
- Dataset之COCO数据集:COCO数据集的简介、下载、使用方法之详细攻略
- mysql innodb表损坏_MySQL数据库INNODB表损坏修复处理过程分享
- JAVA知识基础(八):继承
- 电磁波考试中可以用计算机吗,计算机考试试题库带答案(8页)-原创力文档
- 共享计算机脱机访问计算机,让Windows7脱机共享访问更安全 -电脑资料
- nurbs曲线拟合程序_基于NURBS曲线拟合的shx字体优化
- 佳博打印机ip地址修改软件_【动手实践】树莓派将有线打印机转为无线共享打印机 by xinlong...
- sql server 无法为该请求检索数据
- DEDE网站安全设置防挂马教程
- 如何下载Google Chromium源码。
- nginx openresty DNS resolver配置实例,通过配置resolver解决proxy_pass中使用变量参数,高性能负载均衡 NGINX Plus 中 RESTful API
- java 图片 大小_在JAVA中调整图片大小
- (*(volatile unsigned int *))详解
- 浏览器y轴滚动条占据宽度,导致出现x轴滚动条的解决方案
- 百度智能云 × 火星人丨厨电智能化,从动“手”到动“口”
- 3055. 字符频率
热门文章
- SAP customer engagement center Fiori界面登录后的处理
- bubble click event handling
- SAP S4HANA的product搜索的动态SQL语句的拼接原理
- 工作激发了我的热情,并不断激励着我” - SAP成都研究院Jerry Wang
- ABAP Text表的实现原理
- CM: How to get the complete url of a product attachment
- 使用代码创建SAP Sales area
- 使用ABAP memory inspector分析product 搜索内存占用
- One order datatype 命名规范
- 后台nodejs程序如何主动推送数据给浏览器