当前位置:网站首页>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
边栏推荐
- V-model of custom component
- What are the common proxy servers and what are the differences?
- Actual battle of financial risk control - under Feature Engineering
- 2022-2028 global wood vacuum coating machine industry research and trend analysis report
- [learn C and fly] 3day Chapter 2 program in C language (exercise 2.3 calculate piecewise functions)
- tarjan2
- Qualcomm platform WiFi -- Native crash caused by WiFi
- STM32__05—PWM控制直流电机
- [staff] diacritical mark (ascending sign | descending sign B | double ascending sign x | double descending sign BB)
- 2022-2028 global soft capsule manufacturing machine industry research and trend analysis report
猜你喜欢
Baohong industry | 6 financial management models at different stages of life
Golang configure export goprivate to pull private library code
[question 008: what is UV in unity?]
What kind of good and cost-effective Bluetooth sports headset to buy
SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding
小米青年工程师,本来只是去打个酱油
Which brand of running headphones is good? How many professional running headphones are recommended
连通块模板及变式(共4题)
[JS reverse series] analysis of a customs publicity platform
[learn C and fly] 4day Chapter 2 program in C language (exercise 2.5 generate power table and factorial table
随机推荐
浅谈线程池相关配置
2022安全员-C证考试题及模拟考试
A list of job levels and salaries in common Internet companies. Those who have conditions must enter big factories. The salary is really high
Qualcomm platform WiFi -- Native crash caused by WiFi
Mathematics in Sinorgchem: computational geometry
How to create an instance of the control defined in SAP ui5 XML view at runtime?
Which kind of sports headphones is easier to use? The most recommended sports headphones
32, 64, 128 bit system
[question 008: what is UV in unity?]
Batch detect whether there is CDN in URL - high accuracy
JVM interview
Leetcode question brushing (10) - sequential question brushing 46 to 50
QT使用sqllite
MongoDB非關系型數據庫
超图iServer rest服务之feature查询
Principle of computer composition - interview questions for postgraduate entrance examination (review outline, key points and reference)
Start a business
Query word weight, search word weight calculation
2022-2028 global wood vacuum coating machine industry research and trend analysis report
Render header usage of El table