Added YosysJS.create_worker()
authorClifford Wolf <clifford@clifford.at>
Sun, 28 Jun 2015 15:47:58 +0000 (17:47 +0200)
committerClifford Wolf <clifford@clifford.at>
Sun, 28 Jun 2015 15:47:58 +0000 (17:47 +0200)
misc/yosysjs/demo02.html
misc/yosysjs/yosysjs.js
misc/yosysjs/yosyswrk.js [new file with mode: 0644]

index d36e223c971d1f22cf109253fe03c17b9d779005..a6c4eb96b5423d0961bcbe64cf76113919d00a55 100644 (file)
@@ -7,7 +7,6 @@
                padding:15px; text-align:center;"><span id="popupmsg">Loading...</span></div>
        </div>
        <h1>YosysJS Example Application #02</h1>
-       <iframe id="ys" style="width: 800px; height: 100px;"></iframe><p/>
        <textarea id="code" style="width: 800px; height: 300px;">
 // borrowed with some modifications from
 // http://www.ee.ed.ac.uk/~gerard/Teach/Verilog/manual/Example/lrgeEx2/cooley.html
@@ -56,47 +55,41 @@ endmodule
                        document.getElementById('popupmsg').textContent = 'Please wait..';
                }
                function synth1() {
-                       function work() {
-                               ys.write_file("input.v", document.getElementById('code').value);
-                               ys.run('design -reset; read_verilog input.v; show -stretch');
-                               YosysJS.dot_into_svg(ys.read_file('show.dot'), 'svg');
-                               document.getElementById('popup').style.visibility = 'hidden';
-                       }
                        document.getElementById('popup').style.visibility = 'visible';
-                       window.setTimeout(work, 100);
+                       ys.write_file("input.v", document.getElementById('code').value);
+                       ys.run('design -reset; read_verilog input.v; show -stretch');
+                       ys.read_file('show.dot', (function(text){
+                               YosysJS.dot_into_svg(text, 'svg');
+                               document.getElementById('popup').style.visibility = 'hidden';
+                       }));
                }
                function synth2() {
-                       function work() {
-                               ys.write_file("input.v", document.getElementById('code').value);
-                               ys.run('design -reset; read_verilog input.v; proc; opt_clean; show -stretch');
-                               YosysJS.dot_into_svg(ys.read_file('show.dot'), 'svg');
-                               document.getElementById('popup').style.visibility = 'hidden';
-                       }
                        document.getElementById('popup').style.visibility = 'visible';
-                       window.setTimeout(work, 100);
+                       ys.write_file("input.v", document.getElementById('code').value);
+                       ys.run('design -reset; read_verilog input.v; proc; opt_clean; show -stretch');
+                       ys.read_file('show.dot', (function(text){
+                               YosysJS.dot_into_svg(text, 'svg');
+                               document.getElementById('popup').style.visibility = 'hidden';
+                       }));
                }
                function synth3() {
-                       function work() {
-                               ys.write_file("input.v", document.getElementById('code').value);
-                               ys.run('design -reset; read_verilog input.v; synth -run coarse; show -stretch');
-                               YosysJS.dot_into_svg(ys.read_file('show.dot'), 'svg');
-                               document.getElementById('popup').style.visibility = 'hidden';
-                       }
                        document.getElementById('popup').style.visibility = 'visible';
-                       window.setTimeout(work, 100);
+                       ys.write_file("input.v", document.getElementById('code').value);
+                       ys.run('design -reset; read_verilog input.v; synth -run coarse; show -stretch');
+                       ys.read_file('show.dot', (function(text){
+                               YosysJS.dot_into_svg(text, 'svg');
+                               document.getElementById('popup').style.visibility = 'hidden';
+                       }));
                }
                function synth4() {
-                       function work() {
-                               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');
-                               YosysJS.dot_into_svg(ys.read_file('show.dot'), 'svg');
-                               document.getElementById('popup').style.visibility = 'hidden';
-                       }
                        document.getElementById('popup').style.visibility = 'visible';
-                       window.setTimeout(work, 100);
+                       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.read_file('show.dot', (function(text){
+                               YosysJS.dot_into_svg(text, 'svg');
+                               document.getElementById('popup').style.visibility = 'hidden';
+                       }));
                }
-               var ys = YosysJS.create('ys', on_ys_ready);
-               ys.verbose = true;
-               ys.echo = true;
+               var ys = YosysJS.create_worker(on_ys_ready);
        </script>
 </body></html>
index 2c78451d2843e47e70475f5a8e92ca0e8596027e..65ed30493adb83fe7b3c6ff8ddc58a32c2e10c22 100644 (file)
@@ -172,7 +172,7 @@ var YosysJS = new function() {
                        try {
                                mod.ccall('run', '', ['string'], [cmd]);
                        } catch (e) {
-                               ys.errmsg = mod.ccall('errmsg', 'string', [], []);;
+                               ys.errmsg = mod.ccall('errmsg', 'string', [], []);
                        }
                        return ys.print_buffer;
                }
@@ -220,4 +220,80 @@ var YosysJS = new function() {
 
                return ys;
        }
+
+       this.create_worker = function(on_ready) {
+               var ys = new Object();
+               ys.YosysJS = this;
+               ys.worker = new Worker('yosyswrk.js');
+               ys.callback_idx = 1;
+               ys.callback_cache = {};
+
+               ys.callback_cache[0] = on_ready;
+               on_ready = null;
+
+               ys.worker.onmessage = function(e) {
+                       var response = e.data[0];
+                       var callback = ys.callback_cache[response.idx];
+                       delete ys.callback_cache[response.idx];
+                       if (callback) callback.apply(null, response.args);
+               }
+
+               ys.run = function(cmd, callback) {
+                       var request = {
+                               "idx": ys.callback_idx,
+                               "mode": "run",
+                               "cmd": cmd
+                       };
+
+                       ys.callback_cache[ys.callback_idx++] = callback;
+                       ys.worker.postMessage([request]);
+               }
+
+               ys.read_file = function(filename, callback) {
+                       var request = {
+                               "idx": ys.callback_idx,
+                               "mode": "read_file",
+                               "filename": filename
+                       };
+
+                       ys.callback_cache[ys.callback_idx++] = callback;
+                       ys.worker.postMessage([request]);
+               }
+
+               ys.write_file = function(filename, text, callback) {
+                       var request = {
+                               "idx": ys.callback_idx,
+                               "mode": "write_file",
+                               "filename": filename,
+                               "text": text
+                       };
+
+                       ys.callback_cache[ys.callback_idx++] = callback;
+                       ys.worker.postMessage([request]);
+               }
+
+               ys.read_dir = function(dirname, callback) {
+                       var request = {
+                               "idx": ys.callback_idx,
+                               "mode": "read_dir",
+                               "dirname": dirname
+                       };
+
+                       ys.callback_cache[ys.callback_idx++] = callback;
+                       ys.worker.postMessage([request]);
+               }
+
+               ys.remove_file = function(filename, callback) {
+                       var request = {
+                               "idx": ys.callback_idx,
+                               "mode": "remove_file",
+                               "filename": filename
+                       };
+
+                       ys.callback_cache[ys.callback_idx++] = callback;
+                       ys.worker.postMessage([request]);
+               }
+
+               return ys;
+       }
 }
diff --git a/misc/yosysjs/yosyswrk.js b/misc/yosysjs/yosyswrk.js
new file mode 100644 (file)
index 0000000..1d77b3d
--- /dev/null
@@ -0,0 +1,43 @@
+importScripts('yosys.js');
+
+onmessage = function(e) {
+       var request = e.data[0];
+       var response = { "idx": request.idx, "args": [] };
+
+       if (request.mode == "run") {
+               try {
+                       Module.ccall('run', '', ['string'], [request.cmd]);
+                       response.args.push("");
+               } catch (e) {
+                       response.args.push(mod.ccall('errmsg', 'string', [], []));
+               }
+       }
+
+       if (request.mode == "read_file") {
+               try {
+                       response.args.push(FS.readFile(request.filename, {encoding: 'utf8'}));
+               } catch (e) { }
+       }
+
+       if (request.mode == "write_file") {
+               try {
+                       FS.writeFile(request.filename, request.text, {encoding: 'utf8'});
+               } catch (e) { }
+       }
+
+       if (request.mode == "read_dir") {
+               try {
+                       response.args.push(FS.readdir(request.dirname));
+               } catch (e) { }
+       }
+
+       if (request.mode == "remove_file") {
+               try {
+                       FS.unlink(request.filename);
+               } catch (e) { }
+       }
+
+       postMessage([response]);
+}
+
+postMessage([{ "idx": 0, "args": [] }]);