当前位置:网站首页>牛客网 Verilog 在线编程题库解答(VL1~VL10)
牛客网 Verilog 在线编程题库解答(VL1~VL10)
2022-08-09 17:28:00 【洋洋2020】
目录
VL1 四选一多路器
`timescale 1ns/1ns
module mux4_1(
input [1:0] d1,d2,d3,d0,
input [1:0] sel,
output[1:0] mux_out
);
reg [1:0] mux_out;
[email protected](*) begin
case(sel)
2'b00: mux_out <= d3;
2'b01: mux_out <= d2;
2'b10: mux_out <= d1;
2'b11: mux_out <= d0;
default: mux_out <= d0;
endcase;
end
endmodule
VL2 异步复位的串联 T 触发器
`timescale 1ns/1ns
module Tff_2 (
input wire data, clk, rst,
output reg q
);
reg q0;
[email protected](negedge rst or posedge clk) begin
if(~rst) begin
q0 <= 1'b0;
q <= 1'b0;
end
else begin
q0 <= data ^ q0;
q <= q0 ^ q;
end
end
endmodule
VL3 奇偶校验
`timescale 1ns/1ns
module odd_sel(
input [31:0] bus,
input sel,
output check
);
reg check_odd;
reg check_even;
[email protected](*) begin
check_odd <= ^bus;
check_even <= ~(^bus);
end
assign check = (sel)? check_odd: check_even;
endmodule
VL4 移位运算与乘法
`timescale 1ns/1ns
module multi_sel(
input [7:0]d,
input clk,
input rst,
output reg input_grant,
output reg [10:0]out
);
reg [10:0] d_m1, d_m2, d_m4, d_m8;
reg [1:0] pulse_cnt;
[email protected](negedge rst or posedge clk) begin
if(!rst)
pulse_cnt <= 2'd0;
else begin
pulse_cnt <= pulse_cnt + 1;
end
end
[email protected](negedge rst or posedge clk) begin
if(!rst) begin
d_m1 <= 11'd0;
d_m2 <= 11'd0;
d_m4 <= 11'd0;
d_m8 <= 11'd0;
end
else begin
if(pulse_cnt == 0) begin
d_m1 <= {3'd0,d};
d_m2 <= {2'd0,d,1'd0};
d_m4 <= {1'd0,d,2'd0};
d_m8 <= {d,3'd0};
end
end
end
[email protected](negedge rst or posedge clk) begin
if (!rst) begin
out <= 11'd0;
input_grant <= 1'b0;
end
else begin
case(pulse_cnt)
2'b00: begin
out <= {3'd0,d};
input_grant <= 1'b1;
end
2'b01: begin
out <= d_m2 + d_m1;
input_grant <= 1'b0;
end
2'b10: begin
out <= d_m4 + d_m2 + d_m1;
input_grant <= 1'b0;
end
2'b11: begin
out <= d_m8;
input_grant <= 1'b0;
end
endcase;
end
end
endmodule
VL5 位拆分与运算
`timescale 1ns/1ns
module data_cal(
input clk,
input rst,
input [15:0]d,
input [1:0]sel,
output [4:0]out,
output validout
);
reg [15:0] d_buf;
reg [4:0] out;
[email protected](negedge rst or posedge clk) begin
if (!rst)
d_buf <= 16'd0;
else
if(sel == 2'b00)
d_buf <= d;
end
[email protected](*) begin
case(sel)
2'b01: out <= d_buf[3:0] + d_buf[7:4];
2'b10: out <= d_buf[3:0] + d_buf[11:8];
2'b11: out <= d_buf[3:0] + d_buf[15:12];
default: out <= 16'd0;
endcase;
end
assign validout = (sel == 2'b00)? 1'b0:1'b1;
endmodule
VL6 多功能数据处理器
`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
);
[email protected](negedge rst_n or posedge clk) begin
if(~rst_n)
c <= 0;
else
case(select)
2'b00: c <= a;
2'b01: c <= b;
2'b10: c <= a+b;
2'b11: c <= a-b;
default: c <= 0;
endcase;
end
endmodule
VL7 求两个数的差值
`timescale 1ns/1ns
module data_minus(
input clk,
input rst_n,
input [7:0]a,
input [7:0]b,
output reg [8:0]c
);
[email protected](negedge rst_n or posedge clk) begin
if(~rst_n)
c <= 0;
else
c <= (a > b)? a-b: b-a;
end
endmodule
VL8 使用 generate...for 语句简化代码
`timescale 1ns/1ns
module gen_for_module(
input [7:0] data_in,
output [7:0] data_out
);
reg [7:0] data_out;
genvar i;
generate
for(i=0; i<8; i=i+1) begin: data_out_assign
[email protected](data_in[7-i]) begin
data_out[i] <= data_in[7-i];
end
end
endgenerate
endmodule
VL9 使用子模块实现三输入数的大小比较
`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,tmp2;
sub_mod sub_mod_inst0 (
.clk (clk ),
.rst_n(rst_n),
.inx (a ),
.iny (b ),
.zout (tmp1 )
);
sub_mod sub_mod_inst1 (
.clk (clk ),
.rst_n(rst_n),
.inx (a ),
.iny (c ),
.zout (tmp2 )
);
sub_mod sub_mod_inst2 (
.clk (clk ),
.rst_n(rst_n),
.inx (tmp1 ),
.iny (tmp2 ),
.zout (d )
);
endmodule
module sub_mod(
input clk,
input rst_n,
input [7:0] inx,
input [7:0] iny,
output [7:0] zout
);
reg [7:0] zout;
[email protected](negedge rst_n or posedge clk) begin
if(~rst_n)
zout <= 0;
else
zout <= (inx < iny)? inx: iny;
end
endmodule
VL10 使用函数实现数据大小端转换
`timescale 1ns/1ns
module function_mod(
input clk,
input rst_n,
input [3:0]a,
input [3:0]b,
output [3:0]c,
output [3:0]d
);
reg [3:0] c;
reg [3:0] d;
function [3:0] reverse;
input [3:0] din;
integer i;
begin
for(i = 0;i < 4;i=i+1)
reverse[i] = din[3-i];
end
endfunction
[email protected](*) begin
if(~rst_n) begin
c <= 0;
d <= 0;
end
else begin
c <= reverse(a);
d <= reverse(b);
end
end
endmodule
边栏推荐
- Jenkins deploys services to remote servers using pipelines
- 【工业数字化大讲堂 第二十一期】企业数字化能碳AI管控平台,特邀技术中心总经理 王勇老师分享,8月11日(周四)下午4点
- Logic unauthorized and horizontal and vertical unauthorized payment tampering, verification code bypass, interface
- LeetCode笔记:Weekly Contest 305
- 论文解读:Deep-4MCW2V:基于序列的预测指标,以鉴定大肠杆菌中的N4-甲基环胞嘧啶位点
- d中简单禁止垃集
- 读大学有用吗?
- 日本著名设计师三宅一生去世:产品曾被国人高价抢 乔布斯也是粉丝
- Uniapp 应用未读角标插件 Ba-Shortcut-Badge
- 华为发布「国产Copilot内核」PanGu-Coder,而且真的能用中文哦!
猜你喜欢
The senior told me that the MySQL of the big factory is connected through SSH
loadrunner脚本--参数化
史上最全架构师知识图谱(纯干货)
IMX6ULL—Assembly LED Lights
The most complete architect knowledge map in history
Can't install the Vmware virtual machine on the Ark Kai server?
JVM:(八)运行时数据区之方法区
动态RDLC报表(七)
华为发布「国产Copilot内核」PanGu-Coder,而且真的能用中文哦!
十七、一起学习Lua 错误处理
随机推荐
win10 uwp 模拟网页输入
Ark Standalone/Administrator Special Item Command Codes
fastdfs-client使用
读大学有用吗?
苦日子快走开
loadrunner脚本--参数化
Detailed explanation of JVM memory model and structure (five model diagrams)
LeetCode笔记:Biweekly Contest 84
One-key login principle of local number
毕昇编译器优化:Lazy Code Motion
商业智能BI行业分析思维框架:铅酸蓄电池行业(一)
C程序设计-第四版
华为发布「国产Copilot内核」PanGu-Coder,而且真的能用中文哦!
动态RDLC报表(二)
搭建Zabbix监控系统
阿里云张新涛:支持沉浸式体验应用快速落地,阿里云云XR平台发布
The strongest distributed lock tool: Redisson
ThreadLocal 夺命 11 连问,万字长文深度解析
ARM 汇编基础
How tall is the B+ tree of the MySQL index?