Verilog HDL| HDLBits练习

/*官网链接:https://hdlbits.01xz.net/wiki/Main_Page*/

一.getting started

(1)要求:输出永远高电平

module top_module( output one );

// Insert your code here
    assign one = 1`b0;

endmodule

(2)要求:输出永远为低电平

module top_module(
    output zero
);// Module body starts after semicolon

endmodule

二.Verilog language

1.Basic

(1)simple wire(单线)

要求:输出等于输入(如图)

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

(2)four wires(四线)

要求:实现三线输入后四线输出(如图)

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

(3)inverter(非门)

要求:输出是输入的逻辑非(如图)

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

(4)and gate(与门)

要求:实现与门功能(如图)

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

(5)nor gate(或门)

要求:实现或门功能(如图)

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

(6)xnor gate(异或门)

要求:实现异或门功能(如图)

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

(7)declaring wires(声明线)

1)要求:实现如图所示的功能(如图)

module top_module (
    input in,              // Declare an input wire named "in"
    output out             // Declare an output wire named "out"
);

    wire not_in;           // Declare a wire named "not_in"

    assign out = ~not_in;  // Assign a value to out (create a NOT gate).
    assign not_in = ~in;   // Assign a value to not_in (create another NOT gate).

endmodule   // End of module "top_module"

2)要求:实现如图所示的功能

module top_module(
    input a,
    input b,
    input c,
    input d,
    output out,
    output out_n   ); 
	wire ab = a&&b;
    wire cd = c&&d;
    assign out = ab||cd;
    assign out_n=~out;
endmodule

(8)7458 chip(7458 芯片)

要求:创建一个与 7458 芯片功能相同的模块(7458如图)

module top_module ( 
    input p1a, p1b, p1c, p1d, p1e, p1f,
    output p1y,
    input p2a, p2b, p2c, p2d,
    output p2y );
	wire p1abc = p1a&&p1b&&p1c;
    wire p1def = p1d&&p1e&&p1f;
    wire p2ab = p2a&&p2b;
    wire p2cd = p2c&&p2d;
    assign p1y = p1abc||p1def;
    assign p2y = p2ab||p2cd;

endmodule

2.vectors

(1)vectors0

要求:构建一个3位输入的电路,然后将输入输出,将输入拆分成单独的三个1位的输出(如图)

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 o2 = vec[2];
    assign o1 = vec[1];
    assign o0 = vec[0];
endmodule

(2)vectors in more detail

        声明

wire [7:0] w;         // 8-bit wire
reg  [4:1] x;         // 4-bit reg
output reg [0:0] y;   // 1-bit reg that is also an output port (this is still a vector)
input wire [3:-2] z;  // 6-bit wire input (negative ranges are allowed)
output [3:0] a;       // 4-bit output wire. Type is 'wire' unless specified otherwise.
wire [0:7] b;         // 8-bit wire where b[0] is the most-significant bit.

        隐形网络

wire [2:0] a, c;   // Two vectors
assign a = 3'b101;  // a = 101
assign b = a;       // b =   1  implicitly-created wire
assign c = b;       // c = 001  <-- bug
my_module i1 (d,e); // d and e are implicitly one-bit wide if not declared.
                    // This could be a bug if the port was intended to be a vector.

        打包与未打包数组

reg [7:0] mem [255:0];   // 256 unpacked elements, each of which is a 8-bit packed vector of reg.
reg mem2 [28:0];         // 29 unpacked elements, each of which is a 1-bit reg.

assign w = a;            //访问整个向量是使用向量名称完成的

w[3:0]      // Only the lower 4 bits of w
x[1]        // The lowest bit of x
x[1:1]      // ...also the lowest bit of x
z[-1:-2]    // Two lowest bits of z
b[3:0]      // Illegal. Vector part-select must match the direction of the declaration.
b[0:3]      // The *upper* 4 bits of b.
assign w[3:0] = b[0:3];    // Assign upper 4 bits of b to lower 4 bits of w. w[3]=b[0], w[2]=b[1], etc.

要求:建立一个组合电路,将输入的半个字节(16位,[15:0])分成较低的[7:0]和较高的[15:8]字节

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

(3)vectors part select

要求:32位向量可以看作包含4个字节(bit [31:24] ,[23:16]等)。建立一个能够逆转4字节字的字节顺序的电路

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

    assign out[31:24] = in[7:0];
    assign out[23:16] = in[15:8];
    assign out[15:8] = in[23:16];
    assign out[7:0] = in[31:24];
endmodule

(4)bitewise operators

要求:构建一个有两个3位输入的电路,计算两个向量的按位 or,两个向量的逻辑 or 和两个向量的逆(NOT)。把 b 的倒数放在 out _ not 的上半部分(即位[5:3]) ,把 a 的倒数放在下半部分(如图)

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[2]|b[2],a[1]|b[1],a[0]|b[0]};
    assign out_or_logical = a || b;
    assign out_not = {~b[2],~b[1],~b[0],~a[2],~a[1],~a[0]};
endmodule

查看模拟波形,了解按位或和逻辑或的不同之处。 

(5)four-input gates

构建一个具有四个输入的组合电路:in[3:0]。有3个输出:

out_and:4 输入与门的输出;out_or:4 输入或门的输出;out_xor:4 输入异或门的输出。

module top_module( 
    input [3:0] in,
    output out_and,
    output out_or,
    output out_xor
);
    assign out_and = in[3] & in[2] & in[1] & in[0];
    assign out_or = in[3] | in[2] | in[1] | in[0];
    assign out_xor = in[3] ^ in[2] ^ in[1] ^ in[0];
endmodule

(6)vector concatenation operator

(7)vectors reversal 1

(8)replication operator

(9)more replucation

  

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yatingliu2019

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值