首页 > 其他分享 >HDLBits(二) 8.22

HDLBits(二) 8.22

时间:2022-08-22 11:47:23浏览次数:52  
标签:wire 8.22 HDLBits 信号 2.2 声明 assign 向量

2.Verilog语言

2.1 基础

2.1.7 声明导线

创建一个中间信号,用于简化整个电路模块的逻辑表达

语法:wire foo ;#foo为定义的wire name#

    wire       w1 , w2 ;    
    assign     w1 = a & b ; 
    assign     w2 = c & d ;    
    assign     out = w1 | w2 ;
    assign     out_n = ~out ;
    wire	and_1 = a & b;
    wire	and_2 = c & d;
    wire	or_1  = and_1 | and_2;
    assign	out   = or_1;
    assign	out_n = ~or_1;

2.1.8 7458

7458包含四个与门(AND)和两个或门(OR),总体上有十个输入和两个输出,具体电路如下图

    wire       and_1, and_2, and_3, and_4;
    assign     and_1 = p1a & p1b & p1c;
    assign     and_2 = p1f & p1e & p1d;
    assign     and_3 = p2a & p2b;
    assign     and_4 = p2c & p2d;
    assign     p1y = and_1 | and_2;
    assign     p2y = and_3 | and_4;

2.2 向量

2.2.1 向量

向量就是使用一个名称对相关信号进行分组,以便于操作

一般向量都用于导线信号,故一般将其视为位宽大于 1 的 wire 信号

格式:type [upper : lower] vector_name ;

eg:wire [7:0] w ;声明了一个 8 bit 位宽的信号,向量名为 w ,该向量等价于 8 个 1bit 位宽的 wire 信号

    assign outv = vec;
    assign o2 = vec[2];
    assign o1 = vec[1];
    assign o0 = vec[0];

2.2.2 更详细的矢量

2.2.2.1 隐式声明

若向量为模块的输入/输出,可在向量前声明(eg:input wire[3:0] z;)

Verilog区分向量的字节序,即 [3:0] ≠ [0:3]

变量的隐式声明:若将一个未定义声明的信号连接到模块的输入输出端口时,综合器会自动为你声明这个信号,但问题是,综合器只会声明为 1 bit wire 型信号

如下代码所示,b d e信号由于是隐式声明,位宽只有 1 bit,导致Warning的生成

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.

 可以通过添加`default_nettype none宏定义来关闭隐式声明功能,这样当你使用未声明变量时,会出现Error而不是Warning

2.2.2.2 unpacked数组,packed数组

向量的位宽一般写在向量名之前(eg: [3:0] ),这定义了数组的packed维度,于是在仿真中(硬件中有所不同),这个向量中的每一位信号都被压缩为一整块来进行操作

向量的长度一般写在向量名之后,这定义了数组的unpacked维度,这通常用于声明内存数组

简单得来说,定义在向量名之前的是向量的位宽,定义在向量名之后的维度可以理解为向量数组的长度,同 C 语言中的数组长度概念相同,一般用来对存储器建模

可以将数组等效为矩形,宽即为packed,长即为unpacked

eg:

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.

2.2.2.3 访问向量元素:部分选取

在 assign 赋值操作中,如果等号左右两侧信号的位宽不同,那么就会进行截断或者补零操作

左侧信号位宽大于右侧信号位宽,右值的低位赋予左值对应的低位,左值高位的部分赋零

左侧信号位宽小于右侧信号位宽,右值的低位赋予左值对应的低位,右值高位的部分直接被截断,即保留右值的低位

总体而言,就是以左边信号位宽为标准

使用 [ ] 可以对信号进行片选,选择信号中特定几位比特

w[3:0]      // w 中较低的四位
x[1]        // x 最低位
x[1:1]      // x 最低位
z[-1:-2]    // Z 最低两位
b[3:0]      // 如果 b 在声明时 声明为 wire [0:3] b;则不能使用 b [3:0]进行选择
b[0:3]      // b的高四位.
assign w[3:0] = b[0:3];    // 将 b 的高位赋予 w 的低位 w[3]=b[0], w[2]=b[1], etc.

 

标签:wire,8.22,HDLBits,信号,2.2,声明,assign,向量
From: https://www.cnblogs.com/LhTian/p/16612296.html

相关文章

  • 第七周总结(8.22)
    上周主要学习了hadoop相关的知识,因为是在虚拟机上安装的hadoop,所以还学习了linux的一些常用的命令,对于文件系统有了更深的理解。后面配置了hadoop环境以及hive仓库,并通过远......
  • HDLBits(一)(边刷边学)
    1开始1.1输出逻辑1直接assignone=1'b1,给outputone赋值1'b1,表示1bit数值,b=二进制,o=八进制,d=十进制,h=十六进制1.2输出逻辑0 可以直接提交,因为在Quartus中,输出端......
  • HDLBits答案——Verilog Language
    VerilogLanguage1Basics1.1Wiremoduletop_module(inputin,outputout);assignout=in;endmodule1.2Wire4moduletop_module(inputa,b,......
  • HDLBits答案——Getting started
    Gettingstarted1Steponemoduletop_module(outputone);//Insertyourcodehereassignone=1'b1;endmodule2Zeromoduletop_module(outputze......