当前位置:网站首页>【牛客网刷题系列 之 Verilog快速入门】~ 位拆分与运算
【牛客网刷题系列 之 Verilog快速入门】~ 位拆分与运算
2022-06-30 15:44:00 【AI很不错呦】
本章目录:
0. 插眼
1. VL5 位拆分与运算
1.1 题目描述
现在输入了一个压缩的16位数据,其实际上包含了四个数据[3:0][7:4][11:8][15:12],
现在请按照sel选择输出四个数据的相加结果,并输出valid_out信号(在不输出时候拉低)
0: 不输出且只有此时的输入有效
1:输出[3:0]+[7:4]
2:输出[3:0]+[11:8]
3:输出[3:0]+[15:12]
1.1.1 信号示意图

1.1.2 波形示意图

1.1.3 输入描述
输入信号 d, clk, rst
类型 wire
在testbench中,clk为周期5ns的时钟,rst为低电平复位
1.1.4 输出描述
输出信号 validout out
类型 wire
这里需要注意的是,官方给的类型是reg,当你提交的时候傻眼了,哈哈哈!!!
1.2 解题思路
和上一个题思路很像,就是保存下来一个数,对它进行相应的操作。
1.3 代码实现
module data_cal(
input clk,
input rst,
input [15:0]d,
input [1:0]sel,
output [4:0]out,
output validout
);
//*************code***********//
reg [15:0] d_reg;
reg [4:0] out_reg;
reg validout_reg;
always @ (posedge clk or negedge rst) begin
if(!rst) begin
out_reg <= 5'd0;
validout_reg <= 1'b0;
end
else begin
case (sel)
2'b00 : begin
validout_reg <= 1'b0;
d_reg <= d;
out_reg <= 5'd0;
end
2'b01 : begin
validout_reg <= 1'b1;
out_reg <= d_reg[3:0] + d_reg[7:4];
end
2'b10 : begin
validout_reg <= 1'b1;
out_reg <= d_reg[3:0] + d_reg[11:8];
end
2'b11 : begin
validout_reg <= 1'b1;
out_reg <= d_reg[3:0] + d_reg[15:12];
end
default : begin
validout_reg <= 1'b0;
out_reg <= 5'd0;
end
endcase
end
end
assign validout = validout_reg;
assign out = out_reg;
//*************code***********//
endmodule
1.4 测试文件
module data_cal_tb();
2 reg clk=0;
3 reg rst;
4 reg [15:0]d;
5 reg [1:0]sel;
6 wire [4:0]out;
7 wire validout;
8
9 always #5 clk = ~clk; // Create clock with period=10
10
11 initial begin
12 #10 rst <= 0;
13 // repeat(10) @(posedge clk);
14 #50 rst <= 1;
15 #210 $finish;
16 end
17
18 data_cal dut(
19 .clk(clk),
20 .rst(rst),
21 .d(d),
22 .sel(sel),
23 .out(out),
24 .validout(validout)
25 );
26
27 initial begin
28 d <= 16'h0000;
29 @(posedge rst);
30 repeat(1) @(posedge clk);
31 d <= 16'b1000010000100001;
32 repeat(4) @(posedge clk);
33 d <= 16'b1000010000100011;
34 repeat(5) @(posedge clk);
35 d <= 16'b1000010000100111;
36 end
37
38 initial begin
39 sel <= 2'd0;
40 @(posedge rst);
41 repeat(2) @(posedge clk);
42 sel <= 2'd2;
43 repeat(3) @(posedge clk);
44 sel <= 2'd1;
45 repeat(3) @(posedge clk);
46 sel <= 2'd0;
47 repeat(1) @(posedge clk);
48 sel <= 2'd3;
49 end
50
51 initial begin
52 $fsdbDumpfile("tb.fsdb");
53 $fsdbDumpvars;
54 end
55 endmodule
1.5 仿真波形

这个题,rtl代码倒是不难,但这测试文件写的我贼难受,而且仿真还出现了一些小问题,如下:
停在这里不动了,最后查资料说是代码中有死锁,我一查看,时钟出问题了,这里再次强调,时钟一定要给初始值,否则一切都白扯。
重要的事情说三遍!!!
时钟要给初始值!!!
时钟要给初始值!!!
时钟要给初始值!!!
好了,这篇就先写到这里吧!!!
一件三联不迷路===>
边栏推荐
- 渲染引擎的资源加载优化
- Li Zexiang, a legendary Chinese professor, is making unicorns in batches
- topic: Privacy, Deception and Device Abuse
- Explain in detail the use of for loop, break and continue in go language
- Interesting research on mouse pointer interaction
- 招标公告:深圳市财政局数据库异地灾备项目
- Implementation of Devops in the core field of qunar, the Internet R & D Efficiency
- 【算法篇】四种链表总结完毕,顺手刷了两道面试题
- 【时序数据库InfluxDB】Windows环境下配置InfluxDB+数据可视化,以及使用 C#进行简单操作的代码实例
- 大学生研究生毕业找工作,该选择哪个方向?
猜你喜欢

Google play index table

备战数学建模35-时间序列预测模型
Mysql8 error: error 1410 (42000): you are not allowed to create a user with grant solution

'&lt;', Hexadecimal value 0x3c, is an invalid problem solving
MySQL开放远程连接权限的两种方法

【时序数据库InfluxDB】Windows环境下配置InfluxDB+数据可视化,以及使用 C#进行简单操作的代码实例

15年做糊21款硬件,谷歌到底栽在哪儿?

How cloudxr promotes the future development of XR

halcon变量窗口的图像变量不显示,重启软件和电脑都没用

Policy Center > Misrepresentation
随机推荐
CloudXR如何推动XR的未来发展
【算法篇】四种链表总结完毕,顺手刷了两道面试题
Bidding announcement: remote disaster recovery project of Shenzhen Finance Bureau database
大学生研究生毕业找工作,该选择哪个方向?
String common API
Unsupported major.minor version 52.0
招标公告:天津市住房公积金管理中心数据库一体机及数据库软件项目(预算645万)
Practical cases of data visualization (timeline rotation diagram, streamlit control year metabase visualization tutorial) 2.0
什么是XR扩展现实,XR云串流平台有哪些
Unsupported major. minor version 52.0
360 digital, ant group, etc. were selected as member units of the "business security promotion plan" of the Chinese Academy of Communications
Asp.NetCore利用缓存使用AOP方式防止重复提交
Interpretation of gaussdb's innovative features: partial result cache accelerates operators by caching intermediate results
如何得到股票开户的优惠活动?在线开户安全么?
Google play index table
ASP. Net core Middleware
Mysql8.0 method and steps for enabling remote connection permission
Interview experience of service end test engineer
The new tea drinks are "dead and alive", but the suppliers are "full of pots and bowls"?
MySQL开放远程连接权限的两种方法