【Verilog题目练习】HDLBits题目集合

DAY_1

2 Verilog Language

2.1 Basics

2.1.1 Simple Wire

在这里插入图片描述
题目说明:将输出 out 和输入 in 使用 assgin 连接
代码

module top_module( input in, output out );
	assign out = in;
endmodule

2.1.2 Four Wires

在这里插入图片描述
题目说明:将输出 w 和输入 a 连接,同理,输出 x 和 ==y ==和 输入 b 连接,输出 z 和 输入 c 连接。
代码

module top_module( 
    input a,b,c,
    output w,x,y,z );
    
    assign w = a;
    assign x = b;
    assign y = b;
    assign z = c;

endmodule

2.1.3 Inverter

在这里插入图片描述

题目说明:将输入 in 取反后与输出 out 连接
代码

module top_module( input in, output out );
	assign out = !in;
endmodule

2.1.4 AND gate

在这里插入图片描述
题目说明:将输入 ab 相与后与输出 out 连接
代码

module top_module( 
    input a, 
    input b, 
    output out );
    
    assign out = a & b;

endmodule

2.1.5 NOR gate

在这里插入图片描述
题目说明:将输入 ab 相或后,再取反,然后与输出 out 连接
代码

module top_module( 
    input a, 
    input b, 
    output out );
    
    assign out = !(a | b);

endmodule

2.1.6 XNOR gate

在这里插入图片描述
题目说明:将输入 ab 相异或后,再取反,然后与输出 out 连接(即ab同或
代码

module top_module( 
    input a, 
    input b, 
    output out );
    
    assign out = !(a ^ b);

endmodule

2.1.7 Declaring wires

在这里插入图片描述
题目说明:定义3个wire,前2个wire分别和 a bc d得到的信号连接,第3个wire和前2个wire相或后得到的信号连接,然后与输出 out 连接
代码

module top_module(
    input a,
    input b,
    input c,
    input d,
    output out,
    output out_n   ); 
    
    wire e,f,g;
    
    assign e = a & b;
    assign f = c & d;
    assign g = e | f;
    assign out = g;
    assign out_n = !g;

endmodule

2.1.8 7458 chip

在这里插入图片描述
题目说明:将多个输入input和多个输出output,按照图中所示的逻辑门,进行连接,不作详细说明
代码

module top_module ( 
    input p1a, p1b, p1c, p1d, p1e, p1f,
    output p1y,
    input p2a, p2b, p2c, p2d,
    output p2y );
	
    wire p2e,p2f;
    wire p1g,p1h;
    
    assign p2e = p2a & p2b;
    assign p2f = p2c & p2d;
    assign p2y = p2e | p2f;
    
    assign p1g = p1a & p1b & p1c;
    assign p1h = p1d & p1e & p1f;
    assign p1y = p1g | p1h;

endmodule

2.2 Vectors

2.2.1 Vectors

在这里插入图片描述
题目说明:熟悉向量,outv和vec连接,o0, o1,o2分别的vec的第0位,1位,第2位连接。
代码

module top_module ( 
    input wire [2:0] vec,
    output wire [2:0] outv,
    output wire o2,
    output wire o1,
    output wire o0  ); // Module body starts after module declaration
    
    assign outv = vec;
    assign o0 = vec[0];
    assign o1 = vec[1];
    assign o2 = vec[2];

endmodule

2.2.2 Vectors in more detail

Build a combinational circuit that splits an input half-word (16 bits, [15:0] ) into lower [7:0] and upper [15:8] bytes.

`default_nettype none // Disable implicit nets. Reduces some types of bugs.
module top_module(
input wire [15:0] in,
output wire [7:0] out_hi,
output wire [7:0] out_lo );

题目说明:将in的高8位与out_hi连接,低8位与out_lo连接
代码

module top_module( 
    input wire [15:0] in,
    output wire [7:0] out_hi,
    output wire [7:0] out_lo );
    
    assign out_hi = in[15:8];
    assign out_lo = in[7:0];

endmodule

2.2.3 Vector part select

A 32-bit vector can be viewed as containing 4 bytes (bits [31:24], [23:16], etc.). Build a circuit that will reverse the byte ordering of the 4-byte word.

AaaaaaaaBbbbbbbbCcccccccDddddddd => DdddddddCcccccccBbbbbbbbAaaaaaaa

题目说明:改变32位数据的顺序,将低8位提到高8位,以此类推进行倒序。
代码

module top_module( 
    input [31:0] in,
    output [31:0] out );//

    // 使用位拼接符
    assign out = {
   in[7:0], in[15:8], in[23:16], in[31:24]};

endmodule

2.2.4 Bitwise operators

在这里插入图片描述
题目说明:分别对输入a, b 进行按位或逻辑或以及按位取反,并输出
代码

module top_module( 
    input [2:0] a,
    input [2:0] b,
    output [2:0] out_or_bitwise,
    output out_or_logical,
    output [5:0] out_not
);

    assign out_or_bitwise = a | b;	//按位或
    assign out_or_logical = a || b;	//逻辑或
    //不能使用 !
    //应使用 ~ 进行按位取反,!是逻辑取反
    assign out_not[2:0] = ~a;		
    assign out_not[5:3] = ~b;
	
	/*示例
		a = 010
		b = 101
		
		a | b = {a[0] ^ b[0], a[1] ^ b[1], a[2] ^ b[2]}
		故结果为 a | b = 111

		a || b
			因为 a != 0, b != 0, 所以 a || b = 1 
	*/
			  		
endmodule

2.2.5 Four-input gates

Build a combinational circuit with four inputs, in[3:0].

There are 3 outputs:
out_and: output of a 4-input AND gate.
out_or: output of a 4-input OR gate.
out_xor: output of a 4-input XOR gate.

题目说明:进行一元操作符的练习
代码

module top_module( 
    input [3:0] in,
    output out_and,
    output out_or,
    output out_xor
);

    assign out_and = ∈	// &in = in[0] & in[1] & in[2] & in[3]
    assign out_or = |in;	// |in = in[0] | in[1] | in[2] | in[3]
    assign out_xor = ^in;	// ^in = in[0] ^ in[1] ^ in[2] ^ in[3]
    
endmodule

DAY_2

2.2.6 Vector concatenation operator

在这里插入图片描述
题目说明:将6个5bit的输入abcdef和11,运用位拼接符变成4个8位的输出
代码

module top_module (
    input [4:0] a, b, c, d, e, f,
    output [7:0] w, x, y, z );//

    // assign { ... } = { ... };
    // 作者提示我们使用位拼接符
    
    assign w = {
   a[4:0], b[4:2]};
    assign x = {
   b[1:0], c, d[4]};
    assign y = {
   d[3:0], e[4:1]};
    assign z = {
   e[0], f, 2'b11};

endmodule

2.2.7 Vector reversal 1

Given an 8-bit input vector [7:0], reverse its bit ordering.

题目说明:将一个8位的输入倒序输出
代码

module top_module( 
    input [7:0] in,
    output [7:0] out
);
	//将输入变为倒序有很多种方法,因为上一题作者刚刚考了位拼接符,我倾向于此处作者依然考察的是位拼接符
    assign out = {
   in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]};
    
endmodule

2.2.7 Replication operator

在这里插入图片描述
题目说明:一个有符号的8bit输入,要将他转换成有符号的32bit输出,作者在这个题目介绍了多位相同的合并写法
代码

module top_module (
    input [7:0] in,
    output [31:0] out );

    // assign out = { replicate-sign-bit , the-input };
    assign out = in[7] ? {
   {
   24{
   1'b1}}, in} : {
   {24{1'b0}}, in};

endmodule

2.2.8 More replication

在这里插入图片描述
题目说明:提供5个1bit的输入abcde,利用这5个输入组成两个向量,分别是aaaaabbbbbcccccdddddeeeee和abcdeabcdeabcdeabcdeabcde,然后对这两个向量进行按位同或,使用多位合并的描述方式。
代码

module top_module (
    input a, b, c, d, e,
    output [24:0] out );//

    // The output is XNOR of two vectors created by 
    // concatenating and replicating the five inputs.
    // assign out = ~{ ... } ^ { ... };
    assign out = ~{
   {
   5{
   a}}, {
   5{
   b}}, {
   5{
   c}}, {
   5{
   d}}, {
   5{
   e}}} ^ {
   {
   5{
   a, b, c, d, e}}};

endmodule

DAY_3

2.3 Modules Hierarchy

2.3.1 Modules

在这里插入图片描述
题目说明:这道题作者要我们对模块进行例化,将a与in1连接,b与in2连接,out与out连接
代码

module top_module ( input a, input b, output out );

    mod_a u_mod_a(
        .in1(a),
        .in2(b),
        .out(out)
    );
    
endmodule

PS:作者在本题给出了两种例化风格,一种输入输出的定义顺序按顺序例化,一种按照加括号的端口名对应来进行例化(博主代码所采用的的就是这种方式),个人建议使用第二种方式,更加的直观。

2.3.2 Connecting ports by position

在这里插入图片描述
题目说明:将top_module的输入和输出总计6个端口进行模块例化与mod_a模块连接,这道题作者要求我们使用按照位置例化的方法,那我们就按作者说的做。但是在自己写时,博主只建议使用端口名例化。
代码

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

    mod_a u_mod_a(out1, out2, a, b, c, d);
    
endmodule

2.3.3 Connecting ports by name

在这里插入图片描述
题目说明:同上一题,不过这次使用端口名进行例化。
代码

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

    mod_a u_mod_a(
        .in1(a),
        .in2(b),
        .in3(c),
        .in4(d),
        .out1(out1),
        .out2(out2)
    );
    
endmodule

2.3.4 Three modules

在这里插入图片描述
题目说明:我们发现这题是考查模块之间的连接,定义两个wire或连接两个中间信号即可
代码

module top_module ( input clk, input d, output q );

    wire q1,q2;
    
    my_dff u1_my_dff(
        .clk(clk),
        .d(d),
        .
  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值