VHDL STD_LOGIC_VECTOR 通配符值
我一直在尝试用 VHDL 代码为我在 Altera DE1 板上实现的简单 16 位处理器编写有限状态机。在有限状态机中,我有一个 CASE 语句来处理不同的 16 位指令,这些指令由 16 位 STD_LOGIC_VECTOR 引入 FSM。但是,我在有限状态机解码指令的解码状态中遇到了一些麻烦。其中一条指令是 ADD,它采用两个寄存器作为操作数,第三个作为目标寄存器。不过,我还有一条 ADD 指令,它采用一个寄存器和一个 5 位立即值作为操作数,以及第二个寄存器作为目标。我的问题是,在 CASE 语句中,我需要能够区分两个不同的 ADD 指令。因此,我认为如果我在 CASE 语句中使用“-”或“X”等通配符值,我将能够仅用两种情况来区分两者,而不是列出所有可能的情况寄存器/立即值组合。例如:
CASE IR IS --(IR stands for "Instruction Register")
WHEN "0001------0-----" => (Go to 3-register add);
WHEN "0001------1-----" => (Go to 2-register/immediate value add);
WHEN OTHERS => (Do whatever);
END CASE;
这并不是我仅有的两条说明,我只是将这两条说明放在一起以使这篇文章更短一些。当我编译并运行此代码时,处理器在进入“解码”状态时停止执行。另外,Quartus 给出了很多很多警告,比如“LC3FSM.vhd(37) 处的 VHDL 选择警告:忽略包含元值“0001------0-----””的选择” 我不知道如何实现这一目标。我真的不会也可能不需要定义每个 16 位组合,并且我希望有一种方法可以在 STD_LOGIC_VECTOR 中使用通配符来最大限度地减少我必须定义的组合数量。
有人知道如何做到这一点吗?
谢谢
I've been trying to write a Finite State Machine in VHDL code for a simple 16-bit processor I'm implementing on an Altera DE1 board. In the Finite State Machine, I have a CASE
statement that handles the different 16-bit instructions, which are brought into the FSM by a 16-bit STD_LOGIC_VECTOR. However, I'm having a little trouble in the decode state where the Finite State Machine decodes the instruction. One of the instructions is an ADD which takes two registers as operands and a third as the destination register. However, I also have an ADD instruction which takes a register and a 5-bit immediate value as operands and a second register for the destination. My problem is that in the CASE
statement, I need to be able to differentiate between the two different ADD instructions. So, I thought that if I use wildcard values like "-" or "X" in the CASE
statement, I would be able to differentiate between the two with just two cases instead of listing all of the possible register/immediate value combinations. For example:
CASE IR IS --(IR stands for "Instruction Register")
WHEN "0001------0-----" => (Go to 3-register add);
WHEN "0001------1-----" => (Go to 2-register/immediate value add);
WHEN OTHERS => (Do whatever);
END CASE;
These aren't the only two instructions I have, I just put these two to make this post a little shorter. When I compile and run this code, the processor stops executing when it gets to the "decode" state. Also, Quartus gives many, many warnings saying things like "VHDL choice warning at LC3FSM.vhd(37): ignored choice containing meta-value ""0001------0-----"""
I am at a loss as to how to go about accomplishing this. I REALLY do not and probably don't need to define every single 16-bit combination, and I hope there's a way to use wildcards in a STD_LOGIC_VECTOR to minimize the number of combinations I will have to define.
Does anybody know how to accomplish this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,这是不可能的。对于大多数用户来说,出乎意料的是,比较运算符
=
和case
比较执行文字比较。这是因为std_logic
类型只是一组字符,由于其他函数(例如and
和or) 已定义。
VHDL-2008 引入了一个新的 case 语句
case?
,它按您的预期执行 - 您需要告诉编译器在 VHDL 2008 模式下运行。此外,VHDL 2008 中有一个?=
运算符,它可以比较两个值,同时考虑-
。如果您遇到的编译器仍然不支持 VHDL 2008,请向供应商投诉。还有一个 std_match 函数允许您在较旧的 VHDL 版本中执行比较,但我不知道如何使 case 语句以这种方式工作。
That can't be done unfortunately. Rather unexpectedly for most users, the comparison operator
=
and thecase
comparison perform a literal comparison. This is because thestd_logic
type is just a set of characters, which happen to perform like logic values due to the way other functions (egand
andor
) are defined.VHDL-2008 introduces a new case statement
case?
which performs as you expect - you'll need to tell your compiler to operate in VHDL 2008 mode. In addition, there is a?=
operator in VHDL 2008 which compares two values, taking account of-
s.If you are lumbered with a compiler which still doesn't support VHDL 2008, complain to the supplier. There is also a
std_match
function allows you to perform comparisons in older VHDL revisions, but nothing that I am aware to make thecase
statement work that way.假设您不需要指令中的其他位,您可以通过使用预检查过程屏蔽其他位来解决此问题。 (或者只是确保在编写指令时重置其他位?)
这确实是一个黑客行为。
假设 IR 存储为变量
或者假设您的指令经过精心设计(前四位是命令字还是类似的内容?),您可以执行嵌套的 case 语句,并根据需要在这些子块中进行微分。
Assuming you don't need the other bits in the instruction you could hack your way around this by masking the other bits with a pre-check process. (Or just ensure the other bits are reset when you write the instruction?)
This really is a bit of a hack.
assuming IR is stored as a variable
Alternatively assuming your instruction is cleverly thought out (are the first four bits the command word or something along those lines?) you could do nested case statements and do the differentiation as needed in those sub blocks.