在vivado中使用attribute

本文介绍了在Vivado中几种常用的attribute设置方法,包括强制并行处理case语句、忽略代码段进行综合及控制算术结构是否使用DSP48资源等。通过示例详细解释了这些attribute的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  之前最常用的一个attribute就是mark_debug了,语法如下:(*mark_debug="ture"*)。

 

  今天又学到几个新的,原文在这里:http://china.xilinx.com/support/answers/54357.html

 

  一、PARALLEL_CASE (Verilog Only)

  Parallel case is valid only for Verilog designs. This attribute forces a case statement to be built as a parallel multiplexer. This also prevents the case statement from being transformed into a prioritized if-elsif cascade.

  This attribute can only be controlled through the Verilog RTL.

  Example:

(* parallel_case *)
casex select
   4'b1xxx: res = data1;
   4'bx1xx: res = data2;
   4'bxx1x: res = data3;
   4'bxxx1: res = data4;
endcase

  二、TRANSLATE_OFF/TRANSLATE_ON

  TRANSLATE_OFF and TRANSLATE_ON instructs the Synthesis tool to ignore blocks of code. This can be useful to ignore source code that is not relevant for Synthesis, such as simulation code. 

  These attributes are given within a comment in RTL code. The comment should start with one of the following keywords:

  • synthesis
  • synopsys
  • pragma

  TRANSLATE_OFF starts the section of code to be ignored, and TRANSLATE_ON ends the section to be ignored. These attributes cannot be nested.

  Be careful with the types of code that are included between the translate statements. 

  If it is code that affects the behavior of the design, a simulator could use that code, and create a simulation mismatch.

  Verilog Example

// synthesis translate_off

...Code to be ignored...

// synthesis translate_on

  VHDL Example

-- synthesis translate_off

...Code to be ignored...

-- synthesis translate_on

  三、USE_DSP48

  The use_dsp48 attributes allows a user to control how the Synthesis tool deals with arithmetic structures. 

  By default, mults, mult-add, mult-sub, and mult-accumulate type structures go into DSP48 blocks. Adders, subtractors, and accumulators can also go into these blocks, but by default are implemented with the fabric instead of using DSP48 blocks. 

  If this attribute is not specified, the default behavior is for Vivado Synthesis to determine the correct behavior. 

  This attribute overrides the default behavior and forces these structures into DSP48 blocks, and is placed in the RTL on signals, architectures and components, entities and modules, with the following priority:

  1. Signals
  2. Architectures and components
  3. Modules and entities

  Accepted values for this attribute are "yes" and "no."

Verilog Example

(* use_dsp48 = "yes" *) module test(clk, in1, in2, out1);

VHDL Example

attribute use_dsp48 : string;

attribute use_dsp48 of P_reg : signal is "no";

 

重点说一下USE_DSP48,这句话可以放在模块的前面,也可以放在reg声明的前面,如下:
  一、
放到模块前面

(*use_dsp48="yes"*)module COUNTER(
    input           clk,
    input           rst,
    output  [7:0]   cnt
);
    
    reg [7:0]   cnt_tmp = 8'b0;
    
    always @(posedge clk,posedge rst)
        if(rst)
            cnt_tmp <= 8'b0;
        else
            cnt_tmp <= cnt_tmp + 1'b1;
            
    assign cnt = cnt_tmp;
    
endmodule

综合后的资源占用如下:

  

  二、放到寄存器声明前面

module COUNTER(
    input           clk,
    input           rst,
    output  [7:0]   cnt
);
    
    (*use_dsp48="yes"*)reg [7:0]   cnt_tmp = 8'b0;
    
    always @(posedge clk,posedge rst)
        if(rst)
            cnt_tmp <= 8'b0;
        else
            cnt_tmp <= cnt_tmp + 1'b1;
            
    assign cnt = cnt_tmp;
    
endmodule

综合后的资源占用如下,可以看到跟放到模块前面的资源使用情况是一样的。但这只是针对计数器这么一个简单的模块,如果你的模块中还有其它更复杂的逻辑,那么建议使用第二种方法,只对某些特定的逻辑使用DSP单元。

  

  三、对比不使用DSP

// (*use_dsp48="yes"*) 默认加法不使用DSP
module COUNTER(
    input           clk,
    input           rst,
    output  [7:0]   cnt
);
    
    reg [7:0]   cnt_tmp = 8'b0;
    
    always @(posedge clk,posedge rst)
        if(rst)
            cnt_tmp <= 8'b0;
        else
            cnt_tmp <= cnt_tmp + 1'b1;
            
    assign cnt = cnt_tmp;
    
endmodule

综合后的资源占用:

  

posted on 2016-08-21 18:15 christ0127 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/christsong/p/5793213.html

<think>嗯,用户问的是关于VHDL中的attribute,我需要详细介绍一下。首先,我得回忆一下VHDL的属性有哪些类型,以及它们的常见用途。可能用户刚开始学VHDL,所以需要从基础讲起,但也不能太简单,要覆盖足够的信息。 首先,VHDL中的attribute分为预定义的和用户自定义的。预定义的属性有很多种,比如用于数组的'length、'range,还有信号的'event和'last_value。这些属性在代码中经常用来获取信号或变量的信息,比如时钟边沿检测。然后,用户自定义的属性可能需要用attribute语法来声明,不过可能用户更关心的是预定义的那些。 然后,我需要分门别类地介绍这些属性,比如值类属性、函数类属性、信号类属性,还有类型属性和范围属性。每个类别举几个例子,说明它们的用法和实际应用场景。比如,'event通常用在进程里检测时钟变化,而'length可以获取数组的长度,这在循环或者生成语句中很有用。 用户可能是在编写代码时遇到了具体问题,比如如何获取信号的上升沿,或者如何遍历数组的索引,这时候属性就能派上用场。所以实际例子很重要,比如用'event和rising_edge()函数的比较,或者用'range来生成多个实例。 另外,用户可能不知道如何自定义属性,这部分虽然不太常用,但也需要简要提及,说明如何声明和使用自定义属性,可能用于元数据或者综合指示。 需要确保语言通俗易懂,避免太专业的术语,或者如果用了术语就要解释清楚。比如,解释'stable属性时,可以说明它返回布尔值,表示信号是否有变化,但也要提醒在合成中可能不被支持,避免用户误用。 还要注意结构清晰,分点列出不同类型的属性,每个部分有标题,例子用代码块表示,这样用户看起来更清晰。可能需要检查一下每个属性的正确性,比如数组的'left和'right是否取决于定义时的方向,比如downto和to的区别。 最后,总结一下attribute的作用,强调它们如何增强代码的灵活性和可维护性,特别是在处理不同位宽或信号变化时,避免硬编码,使代码更通用。可能用户需要的是实际应用中的技巧,所以给出一些实用建议会更好,比如在时钟检测中使用'event的常见做法。 总之,我需要组织一个结构清晰、涵盖主要属性类型、有实际例子和注意事项的回答,帮助用户理解并正确应用VHDL中的attribute。</think>VHDL 中的 **Attribute(属性)** 是一种特殊的语法机制,用于获取或定义与对象(如信号、变量、类型、实体等)相关的附信息。属性可以增强代码的灵活性和可读性,尤其在描述硬件行为或优化综合结果时非常有用。 --- ### **1. 预定义属性(Predefined Attributes)** VHDL 提供了一系列预定义属性,主要分为以下几类: #### **(1) 值类属性(Value Attributes)** - **'left**, **'right**, **'low**, **'high** 用于获取标量类型(如整数、枚举类型)或数组类型的边界值。 ```vhdl type my_range is range 15 downto 0; signal value : my_range; ``` - `my_range'left` → 15 - `my_range'right` → 0 - `my_range'low` → 0 - `my_range'high` → 15 - **'length** 获取数组的长度。 ```vhdl signal arr : std_logic_vector(7 downto 0); ``` - `arr'length` → 8 --- #### **(2) 函数类属性(Function Attributes)** - **'event** 检测信号是否发生了事件(值变化)。常用于检测时钟边沿: ```vhdl if clk'event and clk = '1' then -- 上升沿检测 ``` - **'last_value** 返回信号变化前的上一个值。 ```vhdl if data'last_value = '0' and data = '1' then ``` - **'stable(t)** 检测信号在时间 `t` 内是否稳定(无变化)。 ```vhdl if sig'stable(10 ns) then ``` --- #### **(3) 信号类属性(Signal Attributes)** - **'delayed(t)** 返回信号延迟 `t` 时间后的版本。 ```vhdl signal delayed_sig : std_logic := sig'delayed(5 ns); ``` - **'transaction** 生成一个事件触发的信号(每次赋值时翻转)。常用于测试。 --- #### **(4) 类型与范围属性** - **'range** 获取数组的索引范围,常用于循环: ```vhdl for i in data'range loop ``` - **'reverse_range** 反向遍历数组索引范围: ```vhdl for i in data'reverse_range loop ``` --- ### **2. 用户自定义属性(User-Defined Attributes)** VHDL 允许用户定义自己的属性,通常用于添元数据或工具指令(如综合约束)。定义语法如下: ```vhdl attribute attribute_name : attribute_type; -- 声明属性类型 attribute attribute_name of object : class is value; -- 应用属性 ``` #### 示例:定义 FPGA 引脚约束 ```vhdl attribute pin_assignment : string; attribute pin_assignment of clk : signal is "A1"; -- 将 clk 信号绑定到引脚 A1 ``` --- ### **3. 实际应用场景** 1. **时钟边沿检测** ```vhdl process(clk) begin if rising_edge(clk) then -- 等效于 clk'event and clk='1' -- 同步逻辑 end if; end process; ``` 2. **动态生成硬件逻辑** ```vhdl for i in data'range generate instance : entity work.component port map (data(i)); end generate; ``` 3. **避免硬编码** 使用 `'length` 或 `'range` 替代固定数值,提高代码通用性: ```vhdl signal counter : integer range 0 to data'length := 0; ``` --- ### **4. 注意事项** - 部分属性(如 `'event`)不可综合,仅用于仿真。 - 自定义属性的支持程度取决于工具(如 Vivado、Quartus)。 通过合理使用属性,可以显著提升 VHDL 代码的可维护性和硬件描述能力。建议结合具体设计需求,查阅工具的属性支持文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值