Improved YosysJS WebWorker API
authorClifford Wolf <clifford@clifford.at>
Sat, 4 Jul 2015 15:08:44 +0000 (17:08 +0200)
committerClifford Wolf <clifford@clifford.at>
Sat, 4 Jul 2015 15:08:44 +0000 (17:08 +0200)
misc/yosysjs/demo02.html
misc/yosysjs/yosysjs.js
misc/yosysjs/yosyswrk.js

index a6c4eb96b5423d0961bcbe64cf76113919d00a55..9191db98d42963f8ca830f3a121e523f847261e8 100644 (file)
@@ -54,42 +54,50 @@ endmodule
                        document.getElementById('popup').style.visibility = 'hidden';
                        document.getElementById('popupmsg').textContent = 'Please wait..';
                }
+               function handle_run_errors(logmsg, errmsg) {
+                       if (errmsg != "") {
+                               window.alert(errmsg);
+                               document.getElementById('popup').style.visibility = 'hidden';
+                       }
+               }
                function synth1() {
                        document.getElementById('popup').style.visibility = 'visible';
                        ys.write_file("input.v", document.getElementById('code').value);
-                       ys.run('design -reset; read_verilog input.v; show -stretch');
+                       ys.run('design -reset; read_verilog input.v; show -stretch', handle_run_errors);
                        ys.read_file('show.dot', (function(text){
-                               YosysJS.dot_into_svg(text, 'svg');
+                               console.log(ys.errmsg);
+                               if (ys.errmsg == "") YosysJS.dot_into_svg(text, 'svg');
                                document.getElementById('popup').style.visibility = 'hidden';
                        }));
                }
                function synth2() {
                        document.getElementById('popup').style.visibility = 'visible';
                        ys.write_file("input.v", document.getElementById('code').value);
-                       ys.run('design -reset; read_verilog input.v; proc; opt_clean; show -stretch');
+                       ys.run('design -reset; read_verilog input.v; proc; opt_clean; show -stretch', handle_run_errors);
                        ys.read_file('show.dot', (function(text){
-                               YosysJS.dot_into_svg(text, 'svg');
+                               if (ys.errmsg == "") YosysJS.dot_into_svg(text, 'svg');
                                document.getElementById('popup').style.visibility = 'hidden';
                        }));
                }
                function synth3() {
                        document.getElementById('popup').style.visibility = 'visible';
                        ys.write_file("input.v", document.getElementById('code').value);
-                       ys.run('design -reset; read_verilog input.v; synth -run coarse; show -stretch');
+                       ys.run('design -reset; read_verilog input.v; synth -run coarse; show -stretch', handle_run_errors);
                        ys.read_file('show.dot', (function(text){
-                               YosysJS.dot_into_svg(text, 'svg');
+                               if (ys.errmsg == "") YosysJS.dot_into_svg(text, 'svg');
                                document.getElementById('popup').style.visibility = 'hidden';
                        }));
                }
                function synth4() {
                        document.getElementById('popup').style.visibility = 'visible';
                        ys.write_file("input.v", document.getElementById('code').value);
-                       ys.run('design -reset; read_verilog input.v; synth -run coarse; synth -run fine; show -stretch');
+                       ys.run('design -reset; read_verilog input.v; synth -run coarse; synth -run fine; show -stretch', handle_run_errors);
                        ys.read_file('show.dot', (function(text){
-                               YosysJS.dot_into_svg(text, 'svg');
+                               if (ys.errmsg == "") YosysJS.dot_into_svg(text, 'svg');
                                document.getElementById('popup').style.visibility = 'hidden';
                        }));
                }
                var ys = YosysJS.create_worker(on_ys_ready);
+               ys.verbose(true);
        </script>
 </body></html>
index 9723386f9902133b39588f6d16d1c36353e80a02..eabc0ec5095ac93461522efbf56ec2a95a36f872 100644 (file)
@@ -227,6 +227,7 @@ var YosysJS = new function() {
                ys.worker = new Worker('yosyswrk.js');
                ys.callback_idx = 1;
                ys.callback_cache = {};
+               ys.errmsg = "";
 
                ys.callback_cache[0] = on_ready;
                on_ready = null;
@@ -235,6 +236,7 @@ var YosysJS = new function() {
                        var response = e.data[0];
                        var callback = ys.callback_cache[response.idx];
                        delete ys.callback_cache[response.idx];
+                       if ("errmsg" in response) ys.errmsg = response.errmsg;
                        if (callback) callback.apply(null, response.args);
                }
 
@@ -294,6 +296,17 @@ var YosysJS = new function() {
                        ys.worker.postMessage([request]);
                }
 
+               ys.verbose = function(value, callback) {
+                       var request = {
+                               "idx": ys.callback_idx,
+                               "mode": "verbose",
+                               "value": value
+                       };
+
+                       ys.callback_cache[ys.callback_idx++] = callback;
+                       ys.worker.postMessage([request]);
+               }
+
                return ys;
        }
 }
index 1d77b3d25ddc6288888f498fe5d550c17505f424..b6173439f15a4fd6f5700fdceffa1e4ef82245e4 100644 (file)
@@ -1,3 +1,13 @@
+var Module = {};
+var verbose_mode = false;
+var text_buffer = "";
+
+Module["printErr"] = Module["print"] = function(text) {
+       if (verbose_mode)
+               console.log(text);
+       text_buffer += text + "\n";
+}
+
 importScripts('yosys.js');
 
 onmessage = function(e) {
@@ -5,12 +15,16 @@ onmessage = function(e) {
        var response = { "idx": request.idx, "args": [] };
 
        if (request.mode == "run") {
+               response["errmsg"] = "";
                try {
+                       text_buffer = "";
                        Module.ccall('run', '', ['string'], [request.cmd]);
-                       response.args.push("");
                } catch (e) {
-                       response.args.push(mod.ccall('errmsg', 'string', [], []));
+                       response.errmsg = Module.ccall('errmsg', 'string', [], []);
                }
+               response.args.push(text_buffer);
+               response.args.push(response.errmsg);
+               text_buffer = "";
        }
 
        if (request.mode == "read_file") {
@@ -37,6 +51,12 @@ onmessage = function(e) {
                } catch (e) { }
        }
 
+       if (request.mode == "verbose") {
+               if (request.value)
+                       console.log(text_buffer);
+               verbose_mode = request.value;
+       }
+
        postMessage([response]);
 }