简体   繁体   English

有模块时为什么要在verilog中使用函数

[英]Why use functions in verilog when there is module

Part 1 :第 1 部分

I was always told to use functions in Verilog to avoid code duplication.我总是被告知使用 Verilog 中的函数来避免代码重复。 But can't I do that with a module?但是我不能用模块来做到这一点吗? If my understanding is correct, all functions can be re-written as modules in Verilog except that modules cannot be instantiated from the inside of an always block.如果我的理解是正确的,除了不能从 always 块内部实例化模块之外,所有函数都可以在 Verilog 中重写为模块。 Except, in this case, I can always stick with modules.除了在这种情况下,我总是可以坚持使用模块。 Am I correct?我对么?

Part 2 :第 2 部分

If I am correct, why can't the Verilog compiler be written in such a way that the modules get the treatment of a function?如果我是对的,为什么 Verilog 编译器不能以模块得到函数处理的方式编写? I mean, why can't the compiler allow the programmer to instantiate a module inside n block and stop supporting functions?我的意思是,为什么编译器不能允许程序员在 n 块内实例化一个模块并停止支持函数?

  1. module != function.模块 != 功能。 Their usage in verilog is completely different.它们在 verilog 中的用法完全不同。

Functions are actually extended expressions and it is used in expressions.函数实际上是扩展表达式,它用于表达式中。 It can be used in rhs expression of the 'assign' statement or in expressions inside any procedural block.它可以用在'assign' 语句的rhs 表达式或任何程序块内的表达式中。

  • Functions cannot consume time.函数不能消耗时间。

  • Functions must return a value.函数必须返回一个值。


Modules are used to express hardware hierarchy and contain concurrent procedural blocks (which might contain functions).模块用于表示硬件层次结构并包含并发程序块(可能包含函数)。

  • Modules may consume time.模块可能会消耗时间。

  • Modules cannot return value.模块不能返回值。 (output ports are not return values) (输出端口不是返回值)


Potentially you can create a function which replaces internals of a single always block and write an equivalent module with an always block which returns function.潜在地,您可以创建一个函数来替换单个 always 块的内部结构,并使用返回函数的 always 块编写等效模块。 But this is it.但就是这样。

  1. You are not correct :).你不正确:)。 Verilog compiler cannot be written in such a way, because there is a verilog standard which every verilog compiler must follow. Verilog 编译器不能以这种方式编写,因为每个 verilog 编译器都必须遵循一个 verilog 标准。 Otherwise it will not be verilog.否则它不会是verilog。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM