打印
[Actel FPGA]

verilog中reg和wire类型的区别和用法

[复制链接]
2527|9
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
即时生效|  楼主 | 2012-1-18 12:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
reg相当于存储单元,wire相当于物理连线
Verilog 中变量的物理数据分为线型和寄存器型。这两种类型的变量在定义时要设置位宽,缺省为1位。变量的每一位可以是0,1,X,Z。其中x代表一个未被预置初始状态的变量或者是由于由两个或多个驱动装置试图将之设定为不同的值而引起的冲突型线型变量。z代表高阻状态或浮空量。
线型数据包括wire,wand,wor等几种类型在被一个以上激励源驱动时,不同的线型数据有各自决定其最终值的分辨办法。
两者的区别是:即存器型数据保持最后一次的赋值,而线型数据需要持续的驱动
输入端口可以由net/reg驱动,但输入端口只能是net;输出端口可以使net/reg类型,输出端口只能驱动net;若输出端口在过程块中赋值则为reg型,若在过程块外赋值则为net型
用关键词inout声明一个双向端口, inout端口不能声明为寄存器类型,只能是net类型。

***************************************************************************************************************************************************
    wire表示直通,即只要输入有变化,输出马上无条件地反映;reg表示一定要有触发,输出才会反映输入。
        不指定就默认为1位wire类型。专门指定出wire类型,可能是多位或为使程序易读。wire只能被assign连续赋值,reg只能在initial和always中赋值。wire使用在连续赋值语句中,而reg使用在过程赋值语句中。
        在连续赋值语句中,表达式右侧的计算结果可以立即更新表达式的左侧。在理解上,相当于一个逻辑之后直接连了一条线,这个逻辑对应于表达式的右侧,而这条线就对应于wire。在过程赋值语句中,表达式右侧的计算结果在某种条件的触发下放到一个变量当中,而这个变量可以声明成reg类型的。根据触发条件的不同,过程赋值语句可以建模不同的硬件结构:如果这个条件是时钟的上升沿或下降沿,那么这个硬件模型就是一个触发器;如果这个条件是某一信号的高电平或低电平,那么这个硬件模型就是一个锁存器;如果这个条件是赋值语句右侧任意操作数的变化,那么这个硬件模型就是一个组合逻辑。
        输入端口可以由wire/reg驱动,但输入端口只能是wire;输出端口可以使wire/reg类型,输出端口只能驱动wire;若输出端口在过程块中赋值则为reg型,若在过程块外赋值则为net型。用关键词inout声明一个双向端口, inout端口不能声明为reg类型,只能是wire类型;输入和双向端口不能声明为寄存器类型。
       简单来说硬件描述语言有两种用途:1、仿真,2、综合。
对于wire和reg,也要从这两个角度来考虑。
*********************************************************************************
从仿真的角度来说,HDL语言面对的是编译器(如Modelsim等),相当于软件思路。
这时:
wire对应于连续赋值,如assign
reg对应于过程赋值,如always,initial

*********************************************************************************
从综合的角度来说,HDL语言面对的是综合器(如DC等),要从电路的角度来考虑。
这时:
1、wire型的变量综合出来一般是一根导线;
2、reg变量在always块中有两种情况:
(1)、always后的敏感表中是(a or b or c)形式的,也就是不带时钟边沿的,综合出来还是组合逻辑
(2)、always后的敏感表中是(posedge clk)形式的,也就是带边沿的,综合出来一般是时序逻辑,会包含触发器(Flip-Flop)

在设计中,输入信号一般来说你是不知道上一级是寄存器输出还是组合逻辑输出,那么对于本级来说就是一根导线,也就是wire型。而输出信号则由你自己来决定是寄存器输出还是组合逻辑输出,wire型、reg型都可以。但一般的,整个设计的外部输出(即最顶层模块的输出),要求是寄存器输出,较稳定、扇出能力也较好。

相关帖子

沙发
即时生效|  楼主 | 2012-1-18 12:53 | 只看该作者
**********************************************************************************************************************************************
Well I had this doubt when I was learning Verilog: What is the difference between reg and wire? Well I won't tell stories to explain this, rather I will give you some examples to show the difference.
   
    There is something else about wire which sometimes confuses. wire data types can be used for connecting the output port to the actual driver. Below is the code which when synthesized gives a AND gate as output, as we know a AND gate can drive a load.
view plaincopy to clipboardprint?
module wire_example( a, b, y);   
input a, b;   
output y;   

wire a, b, y;   
assign y = a & b;   

endmodule
module wire_example( a, b, y);
input a, b;
output y;

wire a, b, y;
assign y = a & b;

endmodule
SYNTHESIS OUTPUT

    What this implies is that wire is used for designing combinational logic, as we all know that this kind of logic can not store a value. As you can see from the example above, a wire can be assigned a value by an assign statement. Default data type is wire: this means that if you declare a variable without specifying reg or wire, it will be a 1-bit wide wire.
    Now, coming to reg data type, reg can store value and drive strength. Something that we need to know about reg is that it can be used for modeling both combinational and sequential logic. Reg data type can be driven from initial and always block.
Reg data type as Combinational element

view plaincopy to clipboardprint?
module reg_combo_example( a, b, y);   
input a, b;   
output y;   
      
reg   y;   
wire a, b;   
      
always @ ( a or b)   
begin   
     y = a & b;   
end   
     
endmodule
module reg_combo_example( a, b, y);
input a, b;
output y;
   
reg   y;
wire a, b;
   
always @ ( a or b)
begin
     y = a & b;
end

endmodule

使用特权

评论回复
板凳
即时生效|  楼主 | 2012-1-18 12:54 | 只看该作者
SYNTHESIS OUTPUT
   
This gives the same output as that of the assign statement, with the only difference that y is declared as reg. There are distinct advantages to have reg modeled as combinational element; reg type is useful when a "case" statement is required (refer to the Verilog section for more on this).
    To model a sequential element using reg, we need to have edge sensitive variables in the sensitivity list of the always block.
Reg data type as Sequential element
view plaincopy to clipboardprint?
module reg_seq_example( clk, reset, d, q);   
input clk, reset, d;   
output q;   
      
reg   q;   
wire clk, reset, d;   
     
always @ (posedge clk or posedge reset)   
if (reset) begin   
    q <= 1'b0;   
end else begin   
    q <= d;   
end   
     
endmodule
module reg_seq_example( clk, reset, d, q);
input clk, reset, d;
output q;
   
reg   q;
wire clk, reset, d;

always @ (posedge clk or posedge reset)
if (reset) begin
    q <= 1'b0;
end else begin
    q <= d;
end

endmodule
SYNTHESIS OUTPUT



    There is a difference in the way we assign to reg when modeling combinational logic: in this logic we use blocking assignments while modeling sequential logic we use nonblocking ones.

    From the college days we know that wire is something which connects two points, and thus does not have any driving strength. In the figure below, in_wire is a wire which connects the AND gate input to the driving source, clk_wire connects the clock to the flip-flop input, d_wire connects the AND gate output to the flip-flop D input

使用特权

评论回复
地板
giftyouth| | 2012-1-31 10:00 | 只看该作者
:funk:楼主上传的片片,木有使用权限

使用特权

评论回复
5
elecintop| | 2012-1-31 10:15 | 只看该作者
;P楼主辛苦了

使用特权

评论回复
6
lipei99| | 2012-1-31 16:35 | 只看该作者
:dizzy:看着有点晕。。。

使用特权

评论回复
7
jamie-ma| | 2012-2-2 16:54 | 只看该作者
楼主辛苦了,我耐心看看

使用特权

评论回复
8
xlhtracy| | 2012-2-3 15:37 | 只看该作者
楼主重发一下图片呗

使用特权

评论回复
9
liwsx| | 2012-2-5 21:56 | 只看该作者
楼主辛苦,额,感谢讲解

使用特权

评论回复
10
lirfv| | 2012-2-6 21:04 | 只看该作者
对啊,楼主把图片重新发下,方便大家看吗

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

685

帖子

3

粉丝