HDLBits练习(三)Verilog Language_Vectors

Vector0

要求:给定一个3位宽的输入向量,分别实现将该向量直接输出和将该向量的每一位输出。

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

Vector1

要求:将16位输入向量拆分成高8位和低8位分别输出。

使用索引拆分即可。

`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 );

    assign out_hi = in[15:8];
    assign out_lo = in[7:0];

endmodule

Vector2

要求:32位输入包含4个字节(byte),将这4个byte颠倒顺序进行输出。

使用位拼接符进行输出。

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

Vectorgates

要求:给定两个3位输入,进行位或、逻辑或、取反拼接(高3位为b取反,低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 | b;
    assign out_or_logical = a || b;
    assign out_not = {~b,~a};

endmodule

Gates4

要求:给定一个4位输出,实现按位与、或、异或运算。

使用按位运算符进行输出。

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

    assign out_and = & in;
    assign out_or = | in;
    assign out_xor = ^ in;

endmodule

Vector3

要求:给定6个5位输入,将它们按下图所示进行拼接,最低2位补充1,输出4个8位输出。

 使用位拼接符。

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

    assign {w, x, y, z} = {a, b, c, d, e, f, 2'b11};

endmodule

Vectorr

要求:给定一个8位输入,将其顺序颠倒后输出。

因为8位输入较短,直接进行位拼接即可,若输入位宽较长,也可使用for循环进行输出。

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

Vector4

要求:给定一个8位输入,将其最高位重复24次后与该输入进行位拼接。

使用复制运算符将输入的最高位复制24次:{24{in[7]}}

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

    assign out = {{24{in[7]}},in};

endmodule

Vector5

要求:给定5个1位输入,要求按下图方式拼接后进行运算。

 创建两个25位的中间变量,使用复制运算符很容易实现拼接。

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

    wire    [24:0]  one,two;
    
    assign one = {{5{a}},{5{b}},{5{c}},{5{d}},{5{e}},};
    assign two = {5{a,b,c,d,e}};
    
    assign out = one ~^ two;


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值