在赋值语句中使用长嵌套的 if-else 是一种不好的做法吗? [英] Is a bad practice to use long nested if-else in assign statement?

查看:44
本文介绍了在赋值语句中使用长嵌套的 if-else 是一种不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有时在 verilog 中使用长赋值语句,它嵌套了 if-else 循环.

I sometimes use long assign statement in verilog which has nested if-else loop.

示例

assign a = (b) ? '1 : ((c&d) ? '0 : ((f&h) ? '1 : '0));

另一种方法是使用 always_comb 逻辑块.然而,上述方法可以节省时间并且易于快速编码.

Another way to do this is to use an always_comb logic block. However the above approach saves time and easy to code up quickly.

推荐答案

格式设置

嵌套条件连续赋值没有任何问题,但有一些方法可以使其更具可读性:

Formatting

There is nothing wrong with a nested conditional continuous assignment, but there are ways to make it more readable:

assign a = (b)    ? '1
         : (c&d)  ? '0
         : (f&h)  ? '1
                  : '0;

然而,这仍然是一个如果....如果是使用 always 和 ""if...else if...else if...else" 结构的代码,上面的代码可能更容易阅读(在合成相同的代码时).

However, this is still an "if...else if...else if...else" structure, and a question you should ask yourself is what is this code meant to do and how it would 'read'. The above may be easier to read (while synthesizing same code) if it is code using an always and ""if...else if...else if...else" structure.

这是一个干净使用嵌套条件连续赋值的例子:

Here is an example of a clean use of the nested conditional continuous assignment:

assign a = (state == STATE1) ? '1
         : (state == STATE2) ? '0
         : (state == STATE3) ? '1 
           /* default */     : '0;

可读性

请考虑,您显示的表单可能会节省代码原始输入的时间,但让您的代码可读性更高.无论是您自己,还是其他设计师,在一年或更长时间后查看代码都会欣赏到一种形式,可以让他们快速掌握逻辑在做什么.

Readability

Do consider, that your shown form may save time in the original typing of the code, but there is much higher value in having your code readable. Be it yourself, or another designer, looking at the code a year or more later will appreciate a form that allows them to quickly grasp what the logic is doing.

通过使用支持自动扩展片段(或缩写)的编辑器,可以在不损失可读性的情况下加快编码速度.我使用带有缩写的 vim 来真正加快所有块结构的输入速度,以及允许我垂直对齐给定字符(如="或(") 或选择中的字符串)的对齐脚本.

Coding can be sped up without loss of readability by using an editor that supports auto-expanding snippets (or abbreviations). I use vim with abbreviations that really speed up all block structure entry, and alignment scripts that allow me to vertically align given character (like "=" or "(") or string in selection.

这篇关于在赋值语句中使用长嵌套的 if-else 是一种不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆