From e0e6d130cd083a8285ea2991629e0049023da234 Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Thu, 19 Feb 2015 13:36:54 +0100 Subject: [PATCH] YosysJS stuff --- Makefile | 12 +--- frontends/verilog/verilog_parser.y | 1 + kernel/driver.cc | 17 ++++- kernel/log.cc | 19 ++++-- kernel/log.h | 1 + misc/yosysjs/demo03.html | 103 +++++++++++++++++++++++++++++ misc/yosysjs/yosysjs.js | 19 +++++- 7 files changed, 156 insertions(+), 16 deletions(-) create mode 100644 misc/yosysjs/demo03.html diff --git a/Makefile b/Makefile index a301df2fc..405870b64 100644 --- a/Makefile +++ b/Makefile @@ -97,16 +97,10 @@ else ifeq ($(CONFIG),emcc) CXX = emcc CXXFLAGS := -std=c++11 $(filter-out -fPIC -ggdb,$(CXXFLAGS)) EMCCFLAGS := -Os -Wno-warn-absolute-paths -EMCCFLAGS += --memory-init-file 0 -s NO_EXIT_RUNTIME=1 -EMCCFLAGS += -s EXPORTED_FUNCTIONS="['_main','_run','_prompt']" -EMCCFLAGS += --embed-file share +EMCCFLAGS += --memory-init-file 0 --embed-file share -s NO_EXIT_RUNTIME=1 +EMCCFLAGS += -s EXPORTED_FUNCTIONS="['_main','_run','_prompt','_errmsg']" +EMCCFLAGS += -s DISABLE_EXCEPTION_CATCHING=0 -s ALLOW_MEMORY_GROWTH=1 # https://github.com/kripken/emscripten/blob/master/src/settings.js -# EMCCFLAGS += -s ALLOW_MEMORY_GROWTH=1 -# EMCCFLAGS += -s DISABLE_EXCEPTION_CATCHING=0 -# EMCCFLAGS += -s AGGRESSIVE_VARIABLE_ELIMINATION=1 -# EMCCFLAGS += -s ASSERTIONS=2 -# EMCCFLAGS += -s SAFE_HEAP=1 -# EMCCFLAGS += -s RELOOP=0 CXXFLAGS += $(EMCCFLAGS) LDFLAGS += $(EMCCFLAGS) LDLIBS = diff --git a/frontends/verilog/verilog_parser.y b/frontends/verilog/verilog_parser.y index 23cea27f1..f2bc9c573 100644 --- a/frontends/verilog/verilog_parser.y +++ b/frontends/verilog/verilog_parser.y @@ -139,6 +139,7 @@ static void free_attr(std::map *al) %% input: { + ast_stack.clear(); ast_stack.push_back(current_ast); } design { ast_stack.pop_back(); diff --git a/kernel/driver.cc b/kernel/driver.cc index d0ebdde77..dda27c6a6 100644 --- a/kernel/driver.cc +++ b/kernel/driver.cc @@ -78,6 +78,7 @@ USING_YOSYS_NAMESPACE extern "C" int main(int, char**); extern "C" void run(const char*); +extern "C" const char *errmsg(); extern "C" const char *prompt(); int main(int, char**) @@ -92,7 +93,21 @@ int main(int, char**) void run(const char *command) { - run_pass(command); + int selSize = GetSize(yosys_get_design()->selection_stack); + try { + log_last_error = "Internal error (see JavaScript console for details)"; + run_pass(command); + log_last_error = ""; + } catch (...) { + while (GetSize(yosys_get_design()->selection_stack) > selSize) + yosys_get_design()->selection_stack.pop_back(); + throw; + } +} + +const char *errmsg() +{ + return log_last_error.c_str(); } const char *prompt() diff --git a/kernel/log.cc b/kernel/log.cc index ada2cabb2..bf92daced 100644 --- a/kernel/log.cc +++ b/kernel/log.cc @@ -48,6 +48,7 @@ bool log_error_stderr = false; bool log_cmd_error_throw = false; bool log_quiet_warnings = false; int log_verbose_level; +string log_last_error; vector header_count; pool log_id_cache; @@ -173,6 +174,10 @@ void logv_warning(const char *format, va_list ap) void logv_error(const char *format, va_list ap) { +#ifdef EMSCRIPTEN + auto backup_log_files = log_files; +#endif + if (log_errfile != NULL) log_files.push_back(log_errfile); @@ -181,10 +186,16 @@ void logv_error(const char *format, va_list ap) if (f == stdout) f = stderr; - log("ERROR: "); - logv(format, ap); + log_last_error = vstringf(format, ap); + log("ERROR: %s", log_last_error.c_str()); log_flush(); + +#ifdef EMSCRIPTEN + log_files = backup_log_files; + throw 0; +#else exit(1); +#endif } void log(const char *format, ...) @@ -224,8 +235,8 @@ void log_cmd_error(const char *format, ...) va_start(ap, format); if (log_cmd_error_throw) { - log("ERROR: "); - logv(format, ap); + log_last_error = vstringf(format, ap); + log("ERROR: %s", log_last_error.c_str()); log_flush(); throw log_cmd_error_exception(); } diff --git a/kernel/log.h b/kernel/log.h index fd35c7bf7..16ad7b6c9 100644 --- a/kernel/log.h +++ b/kernel/log.h @@ -50,6 +50,7 @@ extern bool log_error_stderr; extern bool log_cmd_error_throw; extern bool log_quiet_warnings; extern int log_verbose_level; +extern string log_last_error; void logv(const char *format, va_list ap); void logv_header(const char *format, va_list ap); diff --git a/misc/yosysjs/demo03.html b/misc/yosysjs/demo03.html new file mode 100644 index 000000000..720b82e90 --- /dev/null +++ b/misc/yosysjs/demo03.html @@ -0,0 +1,103 @@ + +YosysJS Example Application #02 + + + + + + + +

YosysJS Example Application #03

+ Your mission: Create a behavioral Verilog model for the following circuit: +

+ +

+

module top(input clk, reset, input [7:0] A, output reg [7:0] Y);
+  always @(posedge clock) begin
+    Y <= A | {4{reset}};
+  end
+endmodule

+ +

+

 

+ + diff --git a/misc/yosysjs/yosysjs.js b/misc/yosysjs/yosysjs.js index 87c951183..a1181493e 100644 --- a/misc/yosysjs/yosysjs.js +++ b/misc/yosysjs/yosysjs.js @@ -45,6 +45,7 @@ var YosysJS = new function() { ys.verbose = false; ys.logprint = false; ys.echo = false; + ys.errmsg = ""; if (typeof(reference_element) == 'string' && reference_element != "") reference_element = document.getElementById(reference_element); @@ -151,12 +152,20 @@ var YosysJS = new function() { ys.write(""); ys.write(ys.prompt() + cmd); } - mod.ccall('run', '', ['string'], [cmd]); + try { + mod.ccall('run', '', ['string'], [cmd]); + } catch (e) { + ys.errmsg = mod.ccall('errmsg', 'string', [], []);; + } return ys.print_buffer; } ys.read_file = function(filename) { - return ys.window.FS.readFile(filename, {encoding: 'utf8'}); + try { + return ys.window.FS.readFile(filename, {encoding: 'utf8'}); + } catch (e) { + return ""; + } } ys.write_file = function(filename, text) { @@ -167,6 +176,12 @@ var YosysJS = new function() { return ys.window.FS.readdir(dirname); } + ys.remove_file = function(filename) { + try { + ys.window.FS.unlink(filename); + } catch (e) { } + } + doc.open() doc.write('