tilk / digitaljs

Teaching-focused digital circuit simulator
BSD 2-Clause "Simplified" License
633 stars 44 forks source link

Detect and disallow invalid connections in JSON #10

Open tilk opened 4 years ago

tilk commented 4 years ago

If the input JSON has a connection between different-sized ports, DigitalJS will happily connect them, with disastrous results. For example, yosys2digitaljs currently generates such a bad JSON for:

module test2(input [1:0] i, output o);
  assign o = i;
endmodule
module test(output o);
  test2 x(0'b1, o);
endmodule
ShreyaPrasad1209 commented 4 years ago

Hi, can you explain how is JSON converted into digital circuits using yosys2digitaljs? A simple workflow describing which files are being used in the process would be great.

tilk commented 4 years ago

I don't really understand your question. Yosys2digitaljs takes Yosys-genereated JSON, and generates DigitalJS-compatible JSON. Yosys's JSON format describes connections using numbered, single bit nets, while DigitalJS's format describes connections of whole ports. The complex part is converting the single bit connections to whole port connections, adding adapter elements (bit extensions, bit vector splitting and concatenation, etc.) as needed. The code is currently a little bit messy, it's here: https://github.com/tilk/yosys2digitaljs/blob/master/index.js

ShreyaPrasad1209 commented 4 years ago

I saw the mention of JointJS and BackboneJS being used. Would you please help me with their functionality also. Also, You mentioned, "Yosys's JSON format describes connections using numbered, single bit nets, while DigitalJS's format describes connections of whole ports." This is pretty vague as I wasn't able to understand "DigitalJS's JSON" part. Can you help me with an example?

ialokim commented 4 years ago

JointJS and BackboneJS

JointJS is used to render the circuit as a graph in an SVG and JointJS already builds on top of BackboneJS. It also uses jQuery and lodash.

DigitalJS's JSON

Please refer to the README's section on the input format. Some examples to get you started are available under examples, e.g. a (relatively simple) fulladder.

ialokim commented 4 years ago

How would you like digitaljs to react in such a case? I would propose to show a warning/error (e.g. using a red shadow) on the affected wire (or port?), perhaps with a tooltip shown on mouseover stating the problem, and only allow circuit.start() in case there are no errors in the whole graph.

This would then also be another step towards #2 by handling invalid connections introduced by changing bit lengths after initialization.

tilk commented 4 years ago

Your proposition is fine. The information about the circuit correctness should also be accessible by code, for example by a property on HeadlessCircuit. Also, I worry about complexity of the solution. It should be simple (so that it's easy to see it's correct), and should not impact the performance for error-free circuits.