HDLBits之Verilog学习记录 Day6

1 Replication operator

连接操作符允许我们将短小的向量连接在一起构成更宽的向量。很方便,但有的时候需要将多个重复的向量连接在一起,诸如 assign a = {b,b,b,b,b,b}; 这样的语句写多了是非常让人忧愁的。而重复操作符语法就可以在这种情况下帮到你,允许你将一个向量重复多次,并将它们连接在一起,语法是这样:{ 重复次数 { 向量 } }。

注:
1 重复次数必须是一个常量.
2 两组大括号都是必需的.
示例:

{5{1'b1}}           // 5'b11111 (or 5'd31 or 5'h1f)
{2{a,b,c}}          // The same as {a,b,c,a,b,c}
{3'd5, {2{3'd6}}}   // 9'b101_110_110. It's a concatenation of 101 with
                    // the second vector, which is two copies of 3'b110.

作业:看到复制运算符的一个常见地方是将较小的数字符号扩展为较大的数字,同时保留其有符号值。这是通过将较小数字的符号位(最高有效位)复制到左侧来完成的。例如,将4’b 0 101 (5)符号扩展为 8 位的结果为8’b 0000 0101 (5),而将4’b 1 101 (-3)符号扩展为 8 位的结果为8’b 1111 1101 (-3)。

现在要求你构建一个电路,将一个 8bit 有符号数扩展为 32bit 数,这需要将符号位的 24 个副本(即,复制位 [7] 24 次)和 8 位数字本身串联起来。

module top_module (
    input [7:0] in,
    output [31:0] out );//
    assign out = {{24{in[7]}},in};

    // assign out = { replicate-sign-bit , the-input };

endmodule

2 More Replication

作业:将 5 个 1bit 信号分别组成下图中两个 25 bit 信号,输出向量为这两个 25bit 向量的逐位操作的结果。如果两个待比较信号某比特相同,则结果向量对应的该比特位 1 。
HDLBits之Verilog学习记录 Day6_第1张图片

现在要求你构建一个电路,将一个 8bit 有符号数扩展为 32bit 数。

module top_module (
    input a, b, c, d, e,
    output [24:0] out );//
    assign out = ~({{5{a}},{5{b}},{5{c}},{5{d}},{5{e}}}^{{5{a,b,c,d,e}}});
    // The output is XNOR of two vectors created by 
    // concatenating and replicating the five inputs.
    // assign out = ~{ ... } ^ { ... };

endmodule

3 Modules

到目前为止,您已经熟悉了 a module,它是一个通过输入和输出端口与其外部交互的电路。更大、更复杂的电路是通过将更小的模块和连接在一起的其他部分(例如assign语句和always块)组成更大的模块来构建的。这形成了一个层次结构,因为模块可以包含其他模块的实例。

下图显示了一个非常简单的带有子模块的电路。在这个练习中,创建一个实例模块mod_a,模块的三个引脚(连接in1,in2和out)到顶层模块的三个端口(电线a,b和out)。该模块mod_a是为您提供的——您必须实例化它。

模块例化的基本语法 :模块名 实例名(定义连接 port 的信号);

比如 mod_a instance1 ( wa, wb, wc ); 例化了一个 mod_a 模块,将例化的实例命名为 instance1 。括号中是模块端口的连接。

连接模块时,只有模块上的端口很重要。您不需要知道模块内的代码。模块的代码mod_a如下所示:

module mod_a ( input in1, input in2, output out );
    // Module body
endmodule

模块的层次结构是通过在另一个模块中实例化一个模块来创建的,只要使用的所有模块都属于同一个项目(因此编译器知道在哪里可以找到该模块)。一个模块的代码没有写在另一个模块的主体中​​(不同模块的代码没有嵌套)。

您可以通过端口名称或端口位置将信号连接到模块。如需额外练习,请尝试两种方法。
HDLBits之Verilog学习记录 Day6_第2张图片

将信号连接到端口

有两种常用的方法将信号连接到端口:按位置或按名称。

1 按端口顺序,mod_a instance1 ( wa, wb, wc ); wa, wb, wc 分别连接到模块的 第一个端口(in1),第二个端口(in2)以及第三个端口(out)。这里所谓的端口顺序指的是模块端口的定义顺序。这种方式的弊端在于,一旦端口列表发生改变,所有模块实例化中的端口连接都需要改变。

2 按端口名称,mod_a instance2 ( .out(wc), .in1(wa), .in2(wb) ); 在这种方式中根据端口名称指定外部信号的连接。这样一来就和端口声明的顺序完全没有关系。一旦模块出现改动,只要修改相应的部分即可。实际上,一般都使用这种方式来进行模块实例化。

作业:实现以下电路
HDLBits之Verilog学习记录 Day6_第3张图片

module top_module ( input a, input b, output out );
    // mod_a instance1(.in1(a),.in2(b),.out(out)); //按名称
    mod_a instance1(a,b,out); // 按顺序连接顶层Module的三个端口

endmodule

HDLBits之Verilog学习记录 Day6_第4张图片

4 Connecting ports by position(Module pos)

作业:这个问题类似于上一个(模块)。您将获得一个名为的模块mod_a,该模块按该顺序具有 2 个输出和 4 个输入。您必须按位置将 6 个端口连接到顶级模块的端口out1, out2, a, b, c, 和d,和, 。

您将获得以下模块:

module mod_a ( output, output, input, input, input, input );

HDLBits之Verilog学习记录 Day6_第5张图片

module top_module ( 
    input a, 
    input b, 
    input c,
    input d,
    output out1,
    output out2
);
    mod_a instance1(out1,out2,a,b,c,d); //不清楚具体的端口名,只能按照顺序进行连接
endmodule

5 Connecting ports by name(Module name)

作业:这个问题类似于模块. 您将获得一个名为的模块mod_a,该模块按某种顺序具有 2 个输出和 4 个输入。您必须按名称将 6 个端口连接到顶级模块的端口:
HDLBits之Verilog学习记录 Day6_第6张图片
您将获得以下模块:

module mod_a ( output out1, output out2, input in1, input in2, input in3, input in4);

HDLBits之Verilog学习记录 Day6_第7张图片

module top_module ( 
    input a, 
    input b, 
    input c,
    input d,
    output out1,
    output out2
);
    mod_a instance1(.in1(a),.in2(b),.in3(c),.in4(d),.out1(out1),.out2(out2));

endmodule

你可能感兴趣的:(Verilog,fpga,Verilog)