如何用 C++ 模拟数字电路(只有输入/输出,没有图形)
我需要在 C++ 中基于输入/输出创建数字电路的模拟。
输入以一系列元素(or、and、nand 等)和这些元素引脚之间的连接列表的形式给出(即元素 1 的输入引脚 1 连接到元素 2 的输出引脚)。
源被认为是只有一个输出引脚的元素,并且在输入文件的末尾,描述了源,例如它是周期性的,还是随机时刻的自定义 1 或 0。
探针位于电路的末端,它被视为只有一个输入引脚的元件,并且自然地在任何时间点检查信号的值是 0 还是 1。
所以我的问题是,如何创建代表电路的数据结构?它可以像一个元素的连接列表,还是每个元素都应该有指向其他元素的指针?如何在整个电路模型中传输信号?
I need to create a simulation of a digital circuit in C++ based on an input/output basis.
The input is given as a series of elements (or, and, nand, etc.) and as a list of connections between the pins of those elements (i.e. input pin 1 of element 1 is connected to output pin of element 2).
A source is considered an element with just an output pin, and at the end of the input file, the source is described, like is it periodic, or its custom 1 or 0 at random moments.
A probe is at the end of the circuit, and it's considered as an element with just an input pin, and checks if the value of the signal is 0 or 1 at any point in time, naturally.
So my question is, how do I create a data structure that represents a circuit? Can it be like a connected-list of elements, or should each element have pointers to other elements? And how do I transfer a signal throughout the circuit model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您所描述的是一种名为“基于流的编程”的编程范例。
如果您有兴趣,我开发了一个 C++ 面向对象的基于流程的编程库,名为 DSPatch (或“派遣”)。通用的面向对象的 API 允许您创建和路由几乎任何类型的可想象的流程链,从基本逻辑电路到成熟的电子仿真。它速度快,非常易于使用,并且免费供个人/专有使用。
What you've described is a programming paradigm called "Flow-Based Programming".
If you're interested, I've developed an C++ object-oriented flow-based programming library called DSPatch (or "dispatch"). The generic object-oriented API allows you to create and route almost any type of process chain imaginable, from basic logic circuits to full-blown electronics simulation. Its fast, very easy-to-use, and free for personal / proprietary use.
从组件的基类开始:
对特定逻辑实现的基组件进行子类化。
Start with a base class for your components:
Subclass the base component for specific logic imlpementations.
我想说你应该寻找类似图表的结构。您可以添加一个字段,指定其是否处于活动状态/打开状态,并将所有传入数据中继到活动的相邻节点。但这可能会更倾向于 AN 方向。
I'd say you should look for a graph like structure. You could add a field specifying if its active/turned on or not and relaying all incoming data to active neighbouring nodes. But this might go a bit more in the AN direction.
一定要把它存储在图表中。
对于简单的模拟,存储连接应该足够了,可以进行数据流模拟。
如果您正在做更复杂的事情,例如希望能够检测危险,您必须存储基本构建块的延迟和电线的长度。然后,您可以构建一个基于时间的系统,其中线路上的每个变化都会在设备输出线路上的特定(可能不完全确定)时间触发。
Definitely store it in a graph.
For simple simulation storing the connection should be enough, with a data flow simulation.
If you doing something more complex, for example want to be able to detect hazards, you have to store the delays of the basic building blocks and lengths of the wires. Then you can build a time-based system where each change on wires triggers at specific (maybe not-totally deterministic) time on the output wires of the devices.