当前位置:网站首页>verilog REG 寄存器、向量、整数、实数、时间寄存器
verilog REG 寄存器、向量、整数、实数、时间寄存器
2022-07-02 03:02:00 【嗒曦】
寄存器(reg)
寄存器(reg)用来表示存储单元,它会保持数据原有的值,直到被改写。声明举例如下:
实例
reg clk_temp;
reg flag1, flag2 ;
例如在 always 块中,寄存器可能被综合成边沿触发器,在组合逻辑中可能被综合成 wire 型变量。寄存器不需要驱动源,也不一定需要时钟信号。在仿真时,寄存器的值可在任意时刻通过赋值操作进行改写。例如:
实例
reg rstn ;
initial begin
rstn = 1'b0 ;
#100 ;
rstn = 1'b1 ;
end
向量
当位宽大于 1 时,wire 或 reg 即可声明为向量的形式。例如:
实例
reg [3:0] counter ; //声明4bit位宽的寄存器counter
wire [32-1:0] gpio_data; //声明32bit位宽的线型变量gpio_data
wire [8:2] addr ; //声明7bit位宽的线型变量addr,位宽范围为8:2
reg [0:31] data ; //声明32bit位宽的寄存器变量data, 最高有效位为0
对于上面的向量,我们可以指定某一位或若干相邻位,作为其他逻辑使用。例如:
实例
wire [9:0] data_low = data[0:9] ;
addr_temp[3:2] = addr[8:7] + 1'b1 ;
Verilog 支持可变的向量域选择,例如:
实例
reg [31:0] data1 ;
reg [7:0] byte1 [3:0];
integer j ;
[email protected]* begin
for (j=0; j<=3;j=j+1) begin
byte1[j] = data1[(j+1)*8-1 : j*8];
//把data1[7:0]…data1[31:24]依次赋值给byte1[0][7:0]…byte[3][7:0]
end
end
Verillog 还支持指定 bit 位后固定位宽的向量域选择访问。
- [bit+: width] : 从起始 bit 位开始递增,位宽为 width。
- [bit-: width] : 从起始 bit 位开始递减,位宽为 width。
实例
//下面 2 种赋值是等效的
A = data1[31-: 8] ;
A = data1[31:24] ;
//下面 2 种赋值是等效的
B = data1[0+ : 8] ;
B = data1[0:7] ;
对信号重新进行组合成新的向量时,需要借助大括号。例如:
实例
wire [31:0] temp1, temp2 ;
assign temp1 = {byte1[0][7:0], data1[31:8]}; //数据拼接
assign temp2 = {32{1'b0}}; //赋值32位的数值0
整数,实数,时间寄存器变量
整数,实数,时间等数据类型实际也属于寄存器类型。
整数(integer)
整数类型用关键字 integer 来声明。声明时不用指明位宽,位宽和编译器有关,一般为32 bit。reg 型变量为无符号数,而 integer 型变量为有符号数。例如:
实例
reg [31:0] data1 ;
reg [3:0] byte1 [7:0]; //数组变量,后续介绍
integer j ; //整型变量,用来辅助生成数字电路
[email protected]* begin
for (j=0; j<=3;j=j+1) begin
byte1[j] = data1[(j+1)*8-1 : j*8];
//把data1[7:0]…data1[31:24]依次赋值给byte1[0][7:0]…byte[3][7:0]
end
end
此例中,integer 信号 j 作为辅助信号,将 data1 的数据依次赋值给数组 byte1。综合后实际电路里并没有 j 这个信号,j 只是辅助生成相应的硬件电路。
实数(real)
实数用关键字 real 来声明,可用十进制或科学计数法来表示。实数声明不能带有范围,默认值为 0。如果将一个实数赋值给一个整数,则只有实数的整数部分会赋值给整数。例如:
实例
real data1 ;
integer temp ;
initial begin
data1 = 2e3 ;
data1 = 3.75 ;
end
initial begin
temp = data1 ; //temp 值的大小为3
end
时间(time)
Verilog 使用特殊的时间寄存器 time 型变量,对仿真时间进行保存。其宽度一般为 64 bit,通过调用系统函数 $time 获取当前仿真时间。例如:
实例
time current_time ;
initial begin
#100 ;
current_time = $time ; //current_time 的大小为 100
end
边栏推荐
- Jointly developed by nailing, the exclusive functions of glory tablet V7 series were officially launched
- How to create an instance of the control defined in SAP ui5 XML view at runtime?
- OSPF LSA message parsing (under update)
- Just a few simple steps - start playing wechat applet
- [staff] pitch representation (treble clef | C3 60 ~ B3 71 pitch representation | C4 72 pitch representation | C5 84 pitch representation)
- 2022 low voltage electrician test question simulation test question bank simulation test platform operation
- ZABBIX API creates hosts in batches according to the host information in Excel files
- 多线程查询,效率翻倍
- Force deduction daily question 540 A single element in an ordered array
- [learn C and fly] day 5 chapter 2 program in C language (Exercise 2)
猜你喜欢

2022-2028 global manual dental cleaning equipment industry research and trend analysis report

Query word weight, search word weight calculation

Baohong industry | four basic knowledge necessary for personal finance

About DNS

小米青年工程师,本来只是去打个酱油

Jointly developed by nailing, the exclusive functions of glory tablet V7 series were officially launched

使用 useDeferredValue 进行异步渲染

A list of job levels and salaries in common Internet companies. Those who have conditions must enter big factories. The salary is really high

【JVM】创建对象的流程详解

C shallow copy and deep copy
随机推荐
Formatting logic of SAP ui5 currency amount display
Mathematical calculation in real mode addressing
[staff] the direction of the symbol stem and the connecting line (the symbol stem faces | the symbol stem below the third line faces upward | the symbol stem above the third line faces downward | the
Face++ realizes face detection in the way of flow
Après le mariage
Multi threaded query, double efficiency
Start a business
How does proxy IP participate in the direct battle between web crawlers and anti crawlers
连通块模板及变式(共4题)
STM32__ 05 - PWM controlled DC motor
JDBC details
Baohong industry | what misunderstandings should we pay attention to when diversifying investment
OSPF LSA message parsing (under update)
About DNS
QT environment generates dump to solve abnormal crash
[JS reverse series] analysis of a customs publicity platform
多线程查询,效率翻倍
CVPR 2022 | Dalian Institute of technology proposes a self calibration lighting framework for low light level image enhancement of real scenes
Use the open source project [banner] to achieve the effect of rotating pictures (with dots)
【JVM】创建对象的流程详解