当前位置:网站首页>【牛客网刷题系列 之 Verilog快速入门】~ 多功能数据处理器、求两个数的差值、使用generate…for语句简化代码、使用子模块实现三输入数的大小比较
【牛客网刷题系列 之 Verilog快速入门】~ 多功能数据处理器、求两个数的差值、使用generate…for语句简化代码、使用子模块实现三输入数的大小比较
2022-07-01 14:18:00 【AI很不错呦】
1. VL6 多功能数据处理器
题目来源:牛客网
1.1 题目描述
根据指示信号select的不同,对输入信号a,b实现不同的运算。输入信号a,b为8bit有符号数,当select信号为0,输出a;当select信号为1,输出b;当select信号为2,输出a+b;当select信号为3,输出a-b.
1.1.1 信号示意图

1.1.2 波形示意图
无
1.1.3 输入描述
clk:系统时钟
rst_n:复位信号,低电平有效
a,b:8bit位宽的有符号数
select:2bit位宽的无符号数
1.1.4 输出描述
c:9bit位宽的有符号数
1.2 解题思路
通过case语句完成多分支的运算,根据select不同值进行区分即可。
1.3 代码实现
`timescale 1ns/1ns
module data_select(
input clk,
input rst_n,
input signed[7:0]a,
input signed[7:0]b,
input [1:0]select,
output reg signed [8:0]c
);
always @ (posedge clk or negedge rst_n) begin
if(!rst_n) begin
c <= 9'd0;
end
else begin
case (select)
2'b00 : begin
c <= {
a[7], a};
end
2'b01 : begin
c <= {
b[7], b};
end
2'b10 : begin
c <= {
a[7], a} + {
b[7], b};
end
2'b11 : begin
c <= {
a[7], a} - {
b[7], b};
end
default : begin
c <= 9'd0;
end
endcase
end
end
endmodule
1.4 测试文件
待更。。。
1.5 仿真波形
待更。。。
=========================================================================
2. VL7 求两个数的差值
题目来源:牛客网
2.1 题目描述
根据输入信号a,b的大小关系,求解两个数的差值:输入信号a,b为8bit位宽的无符号数。如果a>b,则输出a-b,如果a≤b,则输出b-a。
2.1.1 信号示意图

2.1.2 波形示意图
无
2.1.3 输入描述
clk:系统时钟
rst_n:复位信号,低电平有效
a,b:8bit位宽的无符号数
2.1.4 输出描述
c:8bit位宽的无符号数
2.2 解题思路
和第一个题比较类似,只不过这个题目只有两种情况,直接用if语句块就解决了。
2.3 代码实现
`timescale 1ns/1ns
module data_minus(
input clk,
input rst_n,
input [7:0]a,
input [7:0]b,
output reg [8:0]c
);
always @ (posedge clk or negedge rst_n) begin
if(!rst_n) begin
c <= 9'd0;
end
else begin
if(a > b) begin
c <= a - b;
end
else begin
c <= b - a;
end
end
end
endmodule
2.4 测试文件
待更。。。
2.5 仿真波形
待更。。。
=========================================================================
3. VL8 使用generate…for语句简化代码
3.0 前言
题目来源:牛客网
3.0.1 知识点
考察generate…for…的用法,老办法,话不多说,插眼,传送门
3.1 题目描述
在某个module中包含了很多相似的连续赋值语句,请使用generata…for语句编写代码,替代该语句,要求不能改变原module的功能。
使用Verilog HDL实现以上功能并编写testbench验证。
module template_module(
input [7:0] data_in,
output [7:0] data_out
);
assign data_out [0] = data_in [7];
assign data_out [1] = data_in [6];
assign data_out [2] = data_in [5];
assign data_out [3] = data_in [4];
assign data_out [4] = data_in [3];
assign data_out [5] = data_in [2];
assign data_out [6] = data_in [1];
assign data_out [7] = data_in [0];
endmodule
3.1.1 信号示意图
无。
3.1.2 波形示意图
无
3.1.3 输入描述
data_in:8bit位宽的无符号数
3.1.4 输出描述
data_out:8bit位宽的无符号数
3.2 解题思路
该题基本语法考察。看前言部分传送门即可。
3.3 代码实现
`timescale 1ns/1ns
module gen_for_module(
input [7:0] data_in,
output [7:0] data_out
);
genvar i;
generate
begin : practice
for(i = 0; i < 8; i = i+1)
assign data_out[i] = data_in[7-i];
end
endgenerate
endmodule
3.4 测试文件
待更。。。
3.5 仿真波形
待更。。。
=========================================================================
4. VL9 使用子模块实现三输入数的大小比较
4.0 前言
题目来源:牛客网
4.0.1 知识点
模块之间的相互调用。
4.1 题目描述
在数字芯片设计中,通常把完成特定功能且相对独立的代码编写成子模块,在需要的时候再在主模块中例化使用,以提高代码的可复用性和设计的层次性,方便后续的修改。
请编写一个子模块,将输入两个8bit位宽的变量data_a,data_b,并输出data_a,data_b之中较小的数。并在主模块中例化,实现输出三个8bit输入信号的最小值的功能。
4.1.1 信号示意图

4.1.2 波形示意图
无
4.1.3 输入描述
clk:系统时钟
rst_n:异步复位信号,低电平有效
a,b,c:8bit位宽的无符号数
4.1.4 输出描述
d:8bit位宽的无符号数,表示a,b,c中的最小值
4.2 解题思路
该题模块调用倒是不难,就是分别写一下模块(一般情况下,不同模块是要写在不同的文件中的,但写在一个文件中也是可以的)。调用之前,需要先例化该模块!!!
但这个题比较容易出错的地方在于,子模块用时序逻辑和组合逻辑写,在主模块中处理方式是不一样的,我们细细看一下:
- 如果你子模块用的是时序逻辑写的话,你主模块必须实例化三次,如果你想实例化两次比较3个输入abc的话,你就会发生其中2个信号是在同一周期T下的,而另外那个信号却在周期T+1下,造成比较混乱。
- 如果你子模块用的是组合逻辑写的话,你主模块可以只实例化2次子模块,但是你得完成打两拍的动作等待一下,最后一起输出。
4.3 代码实现
4.3.1 子模块用的是时序逻辑写的
`timescale 1ns/1ns
module main_mod(
input clk,
input rst_n,
input [7:0]a,
input [7:0]b,
input [7:0]c,
output [7:0]d
);
wire [7:0] ab_min_reg;
wire [7:0] ac_min_reg;
sub_mod u1(
.clk(clk),
.rst_n(rst_n),
.a(a),
.b(b),
.c(ab_min_reg)
);
sub_mod u2(
.clk(clk),
.rst_n(rst_n),
.a(a),
.b(c),
.c(ac_min_reg)
);
sub_mod u3(
.clk(clk),
.rst_n(rst_n),
.a(ab_min_reg),
.b(ac_min_reg),
.c(d)
);
endmodule
//子模块(时序逻辑实现)
module sub_mod(
input clk,
input rst_n,
input [7:0]a,
input [7:0]b,
output reg [7:0]c
);
always @ (posedge clk or negedge rst_n) begin
if(!rst_n) begin
c <= 8'd0;
end
else begin
if(a >= b) begin
c <= b;
end
else begin
c <= a;
end
end
end
endmodule
4.3.2 子模块用的是组合逻辑写的
`timescale 1ns/1ns
module main_mod(
input clk,
input rst_n,
input [7:0]a,
input [7:0]b,
input [7:0]c,
output [7:0]d
);
wire [7:0] tmp1; // a b 的最小值
child_mod U0(
.a ( a ),
.b ( b ),
.d ( tmp1 )
);
wire [7:0] tmp2; // a c 的最小值
child_mod U1(
.a ( tmp1 ),
.b ( c ),
.d ( tmp2 )
);
reg [7:0] d_reg;
reg [7:0] d_reg2;
always @ (posedge clk&nbs***bsp;negedge rst_n)
begin
if( ~rst_n ) begin
d_reg <= 8'b0;
d_reg2 <= 8'b0;
end
else begin
d_reg <= tmp2;
d_reg2 <= d_reg;
end
end
assign d = d_reg2;
endmodule
module child_mod(
input [7:0]a,
input [7:0]b,
output [7:0]d
);
assign d = (a>b) ? b : a;
endmodule
4.4 测试文件
待更。。。
4.5 仿真波形
待更。。。
声明
本人所有系列的文章,仅供学习,不可商用,如有侵权,请告知,立删!!!
本人主要是记录学习过程,以供自己回头复习,再就是提供给后人参考,不喜勿喷!!!
如果觉得对你有用的话,记得收藏+评论!!!
边栏推荐
- Fiori applications are shared through the enhancement of adaptation project
- 开源实习经验分享:openEuler软件包加固测试
- [Jianzhi offer] 54 The k-th node of binary search tree
- 用对场景,事半功倍!TDengine 的窗口查询功能及使用场景全介绍
- Play with grpc - communication between different programming languages
- Etcd summary mechanism and usage scenarios
- 当主程架构游戏的时候,防止到处调用减少耦合性,怎么开放接口给其他人调用呢?
- 力扣解法汇总241-为运算表达式设计优先级
- Research Report on the development trend and competitive strategy of the global electromagnetic flowmeter industry
- The integration of computing and Internet enables the transformation of the industry, and the mobile cloud lights up a new roadmap for the future of digital intelligence
猜你喜欢
随机推荐
sqlilabs less9
Introduction to distributed transactions (Seata)
[sword finger offer] 55 - I. depth of binary tree
Halo effect - who says that those with light on their heads are heroes
Six years of technology iteration, challenges and exploration of Alibaba's globalization and compliance
Tdengine connector goes online Google Data Studio app store
sqlilabs less-11~12
程序设计的基本概念
用栈实现队列、用队列实现栈(C语言_leetcode_232+225)
Realize queue with stack and stack with queue (C language \leetcode\u 232+225)
Use the right scene, get twice the result with half the effort! Full introduction to the window query function and usage scenarios of tdengine
Provincial election + noi Part XI others
C language course design topic
sqlilabs less-8
那个很努力的学生,高考失败了……别慌!你还有一次逆袭机会!
如何看待国企纷纷卸载微软Office改用金山WPS?
MySQL log
C 语言基础
phpcms实现订单直接支付宝支付功能
A new book by teacher Zhang Yujin of Tsinghua University: 2D vision system and image technology (five copies will be sent at the end of the article)









