Added Viz to yosys.js
authorClifford Wolf <clifford@clifford.at>
Sun, 15 Feb 2015 21:53:41 +0000 (22:53 +0100)
committerClifford Wolf <clifford@clifford.at>
Sun, 15 Feb 2015 21:53:41 +0000 (22:53 +0100)
.gitignore
Makefile
misc/yosys.html
passes/cmds/show.cc

index f48aeac5f4bc34430750e0126ad5e049694ea6b0..4334b3d84dc16ecf56da855e73f2d7ce33ad194a 100644 (file)
@@ -11,6 +11,7 @@
 /qtcreator.creator.user
 /Makefile.conf
 /abc
+/viz.js
 /yosys
 /yosys.exe
 /yosys.js
index c762bd38cd1a7e3935dfe11b7c996120ccbdaf58..a7035008cf6afe66bb64daf0e47f049a828c3f91 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -113,7 +113,11 @@ LDLIBS =
 EXE = .js
 
 TARGETS := $(filter-out yosys-config,$(TARGETS))
-EXTRA_TARGETS += yosys.html
+EXTRA_TARGETS += yosys.html viz.js
+
+viz.js:
+       wget -O viz.js.part https://github.com/mdaines/viz.js/releases/download/0.0.3/viz.js
+       mv viz.js.part viz.js
 
 yosys.html: misc/yosys.html
        $(P) cp misc/yosys.html yosys.html
index 741b88b45099e03fb54ce175b8e0327d5f162511..a5a8b65c9e99567bfba65b09f0845ec62780fbb4 100644 (file)
@@ -2,7 +2,9 @@
        <title>yosys.js example application</title>
 </head><body onload="document.getElementById('command').focus()">
        <h1>yosys.js example application</h1>
-       <div id="tabs"></div>
+       <table width="100%"><tr><td><div id="tabs"></div></td><td align="right"><tt>[ <span onclick="load_example()">load example</span> ]</tt></td></tr></table>
+       <iframe id="viz" style="display: none"><script type="text/javascript" src="viz.js"></script></iframe>
+       <svg id="svg" style="display: none; position: absolute; padding: 10px; width: 100%; height: 80%;"></svg>
        <div><textarea id="output" style="width: 100%" rows="30" cols="100"></textarea></div>
        <div id="wait" style="display: block"><br/><b><span id="waitmsg">Loading...</span></b></div>
        <div id="input" style="display: none"><form onsubmit="window.setTimeout(run_command); return false"><tt><span id="prompt">
@@ -42,6 +44,7 @@
 
                var current_file = "";
                var console_messages = "";
+               var svg_cache = { };
 
                function update_tabs() {
                        var f, html = "", flist = FS.readdir('.');
                        }
                        html += ' | <span onclick="open_file(prompt(\'Filename:\'))">new file</span> ]</tt>';
                        document.getElementById('tabs').innerHTML = html;
-                       if (current_file == "") {
-                               document.getElementById('output').readOnly = true;
+                       if (current_file == "" || /\.dot$/.test(current_file)) {
+                               var element = document.getElementById('output');
+                               element.readOnly = true;
+                               element.scrollTop = element.scrollHeight; // focus on bottom
                                document.getElementById('command').focus();
                        } else {
                                document.getElementById('output').readOnly = false;
@@ -75,7 +80,7 @@
                {
                        if (current_file == "")
                                console_messages = document.getElementById('output').value;
-                       else
+                       else if (!/\.dot$/.test(current_file))
                                FS.writeFile(current_file, document.getElementById('output').value, {encoding: 'utf8'});
 
                        if (filename == "") {
                                }
                        }
 
+                       if (/\.dot$/.test(filename)) {
+                               dot = document.getElementById('output').value;
+                               if (!(dot in svg_cache)) {
+                                       el = document.getElementById('viz');
+                                       svg_cache[dot] = el.contentWindow.Viz(dot, "svg");
+                               }
+                               document.getElementById('svg').innerHTML = svg_cache[dot];
+                               document.getElementById('svg').style.display = 'block';
+                               document.getElementById('output').value = '';
+                       } else {
+                               document.getElementById('svg').innerHTML = '';
+                               document.getElementById('svg').style.display = 'none';
+                       }
+
                        current_file = filename;
                        update_tabs()
                }
 
                function startup() {
+                       el = document.getElementById('viz');
+                       el.contentWindow.document.open();
+                       el.contentWindow.document.write('<script type="text/javascript" src="viz.js"></' + 'script>');
+                       el.contentWindow.document.close();
+
                        document.getElementById('wait').style.display = 'none';
                        document.getElementById('input').style.display = 'block';
                        document.getElementById('waitmsg').innerText = 'Waiting for yosys.js...';
                        document.getElementById('prompt').innerText = yosys_prompt();
-                       FS.mkdir('/work')
-                       FS.chdir('/work')
+
+                       try { FS.mkdir('/work'); } catch (e) { }
+                       FS.chdir('/work');
+
                        update_tabs();
                        console.log('yosys.js loaded.');
                }
 
+               function load_example() {
+                       open_file('')
+
+                       var txt = "";
+                       txt += "// a simple yosys.js example. run \"script example.ys\".\n";
+                       txt += "\n";
+                       txt += "module example(input clk, input rst, input inc, output reg [3:0] cnt);\n";
+                       txt += "  always @(posedge clk) begin\n";
+                       txt += "    if (rst)\n";
+                       txt += "      cnt <= 0;\n";
+                       txt += "    else if (inc)\n";
+                       txt += "      cnt <= cnt + 1;\n";
+                       txt += "  end\n";
+                       txt += "endmodule\n";
+                       txt += "\n";
+                       FS.writeFile('example.v', txt, {encoding: 'utf8'});
+
+                       var txt = "";
+                       txt += "# a simple yosys.js example. run \"script example.ys\".\n";
+                       txt += "\n";
+                       txt += "design -reset\n";
+                       txt += "read_verilog example.v\n";
+                       txt += "proc\n";
+                       txt += "opt\n";
+                       txt += "show\n";
+                       txt += "\n";
+                       FS.writeFile('example.ys', txt, {encoding: 'utf8'});
+
+                       open_file('example.ys')
+                       document.getElementById('command').focus();
+               }
+
                function yosys_command(cmd) {
                        Module.ccall('run', '', ['string'], [cmd])
                }
                        var cmd = document.getElementById('command').value;
                        document.getElementById('command').value = '';
 
+                       var show_dot_before = "";
+                       try { show_dot_before = FS.readFile('show.dot', { encoding: 'utf8' }); } catch (e) { }
+
                        open_file('');
                        Module.print(yosys_prompt() + cmd);
 
                                        Module.print('Caught JavaScript exception. (see JavaScript console for details.)');
                                        console.log(e);
                                }
+
                                document.getElementById('wait').style.display = 'none';
                                document.getElementById('input').style.display = 'block';
                                document.getElementById('prompt').innerText = yosys_prompt();
+
+                               var show_dot_after = "";
+                               try { show_dot_after = FS.readFile('show.dot', { encoding: 'utf8' }); } catch (e) { }
+
+                               if (show_dot_before != show_dot_after)
+                                       open_file('show.dot');
+
                                update_tabs();
                        }
 
index 63da29b94a365f6449c415fdee1761969cb4f27a..4d6095e55a08af64634514baf49da30cc02214c3 100644 (file)
@@ -650,9 +650,14 @@ struct ShowPass : public Pass {
                std::vector<std::pair<std::string, RTLIL::Selection>> color_selections;
                std::vector<std::pair<std::string, RTLIL::Selection>> label_selections;
 
+#ifdef EMSCRIPTEN
+               std::string format = "dot";
+               std::string prefix = "show";
+#else
                std::string format;
-               std::string viewer_exe;
                std::string prefix = stringf("%s/.yosys_show", getenv("HOME") ? getenv("HOME") : ".");
+#endif
+               std::string viewer_exe;
                std::vector<std::string> libfiles;
                std::vector<RTLIL::Design*> libs;
                uint32_t colorSeed = 0;