dol: initial dol commit
[jump.git] / dol / src / dol / visitor / systemC / SCModuleVisitor.java
diff --git a/dol/src/dol/visitor/systemC/SCModuleVisitor.java b/dol/src/dol/visitor/systemC/SCModuleVisitor.java
new file mode 100644 (file)
index 0000000..006a08c
--- /dev/null
@@ -0,0 +1,232 @@
+/* $Id: SCModuleVisitor.java 1 2010-02-24 13:03:05Z haidw $ */
+package dol.visitor.systemC;
+
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+
+import dol.datamodel.pn.Channel;
+import dol.datamodel.pn.Port;
+import dol.datamodel.pn.Process;
+import dol.datamodel.pn.ProcessNetwork;
+import dol.util.CodePrintStream;
+import dol.visitor.PNVisitor;
+
+/**
+ * This class is a class for a visitor that is used to generate
+ * the top sc module: sc_applicaion.cpp.
+ */
+public class SCModuleVisitor extends PNVisitor {
+
+    /**
+     * Constructor.
+     *
+     * @param dir path of this file
+     */
+    public SCModuleVisitor(String dir) {
+        _dir = dir;
+    }
+
+    /**
+     *
+     * @param x process network that needs to be rendered
+     */
+    public void visitComponent(ProcessNetwork x) {
+        try {
+            String filename = _dir + _delimiter + "sc_application.cpp";
+            OutputStream file = new FileOutputStream(filename);
+            _mainPS = new CodePrintStream(file);
+
+            _mainPS.printPrefixln("#include <systemc>");
+            _mainPS.printPrefixln("#include <list>");
+            _mainPS.printPrefixln("#include \"dol_fifo.h\"");
+            _mainPS.printPrefixln("#include \"dol_sched_if.h\"");
+            _mainPS.println();
+
+            for (String basename : x.getProcessBasenames()) {
+                _mainPS.printPrefixln("#include \"" + basename
+                        + "_wrapper.h\"");
+            }
+            _mainPS.println();
+            _mainPS.printPrefixln("using namespace std;");
+
+            _mainPS.println();
+            _mainPS.printPrefixln("class sc_application : public sc_module ");
+            _mainPS.printLeftBracket();
+
+            _mainPS.printPrefixln("public:");
+            _mainPS.printPrefixln("SC_HAS_PROCESS(sc_application);");
+
+            //declare processes
+            _mainPS.println();
+            for (Process p : x.getProcessList()) {
+                _mainPS.printPrefixln(p.getBasename() + "_wrapper "
+                        + p.getName() + "_ins"+ ";");
+                _mainPS.printPrefixln("sc_event " + p.getName()
+                        + "_event;");
+            }
+            _mainPS.println();
+
+            //define the scheduler
+            _mainPS.printPrefixln("sc_event sched_event;");
+            _mainPS.printPrefixln("list<sc_event* > eventList;");
+            _mainPS.printPrefixln("list<sc_event* >::iterator iter;");
+            _mainPS.println();
+
+            //declare channels
+            _mainPS.println();
+            for (Channel p : x.getChannelList()) {
+                _mainPS.printPrefixln("fifo " + p.getName() + "_ins;");
+            }
+            _mainPS.println();
+
+            //model constructor
+            _mainPS.printPrefixln("sc_application(sc_module_name name)");
+
+            //parameter of constructor
+            _mainPS.printPrefix(":       sc_module(name)");
+            for (Process p : x.getProcessList()) {
+                _mainPS.println(",");
+                _mainPS.printPrefix(p.getName() + "_ins(\""
+                        + p.getName() +"\")");
+            }
+
+            if (x.getChannelList().size() > 0) {
+                for (Channel c : x.getChannelList()) {
+                    _mainPS.println(",");
+                    _mainPS.printPrefix(c.getName() + "_ins("
+                                        + "\"" + c.getName() + "\", "
+                                        + c.getSize() * c.getTokenSize()
+                                        + ")");
+                }
+            }
+            _mainPS.println("");
+            _mainPS.printLeftBracket();
+            
+            //construtor content
+            //build the network
+            for (Channel p : x.getChannelList()) {
+                p.accept(this);
+            }
+            _mainPS.println("");
+
+            _mainPS.println("");
+
+            _mainPS.printPrefixln("SC_THREAD(thread_init);");
+            //init thread
+            _mainPS.printPrefixln("SC_THREAD(thread_sched);");
+
+            //declare concurrent non-terminating threads
+            for (Process p : x.getProcessList()) {
+                _mainPS.printPrefixln("SC_THREAD(thread_" +
+                                      p.getName() + ");");
+            }
+            _mainPS.printRightBracket();
+            _mainPS.println();
+
+            //define scheduler thread
+            _mainPS.printPrefixln("void thread_init()");
+            _mainPS.printLeftBracket();
+            //init
+            for (Process p : x.getProcessList()) {
+                _mainPS.printPrefixln(p.getName() + "_ins.initialize();");
+            }
+            _mainPS.printRightBracket();
+            _mainPS.println();
+
+
+            //different scheduling algorithm can be put here
+            _mainPS.printPrefixln("void thread_sched()");
+            _mainPS.printLeftBracket();
+            _mainPS.printPrefixln("while (1)");
+            _mainPS.printLeftBracket();
+            _mainPS.printPrefixln("for (iter=eventList.begin(); iter != "
+                    + "eventList.end(); ++iter)");
+            _mainPS.printLeftBracket();
+            _mainPS.printPrefixln("sc_event* e = (*iter);");
+            _mainPS.printPrefixln("notify(*e);");
+            _mainPS.printRightBracket();
+            _mainPS.printPrefixln("eventList.clear();");
+            _mainPS.printPrefixln("wait(sched_event);");
+            _mainPS.printRightBracket();
+            _mainPS.printRightBracket();
+            _mainPS.println();
+
+            //define threads
+            for (Process p : x.getProcessList()) {
+                p.accept(this);
+            }
+
+            _mainPS.printRightBracket(); // end of class
+            _mainPS.println(";");
+
+            //create and run the simulator
+            _mainPS.printPrefixln("int sc_main (int argc, char *argv[])");
+            _mainPS.printLeftBracket();
+            _mainPS.printPrefixln("sc_report_handler::set_actions(\""
+                    + "/IEEE_Std_1666/deprecated\", SC_DO_NOTHING);");
+            _mainPS.printPrefixln("sc_report::register_id("
+                    + "RP_ID_PARAMETER_PROBLEM, "
+                    + "\"parameter problem\" );");
+
+            //create an instance of the application model
+            //remove potential whitespaces before using the process
+            //network name as a systemc identifier
+            _mainPS.printPrefixln("sc_application my_app_mdl(\""
+                    + x.getName().replaceAll(" ", "")  + "\");");
+            _mainPS.printPrefixln("sc_start(-1,SC_NS);");
+            _mainPS.printPrefixln("return 0;");
+
+            _mainPS.printRightBracket();
+
+        }
+        catch (Exception e) {
+            System.out.println(" SystemC module visitor: exception " +
+                               "occured: " + e.getMessage());
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * Print a line for the process in the correct format for DOTTY.
+     *
+     * @param x process that needs to be rendered
+     */
+    public void visitComponent(Process x) {
+        _mainPS.printPrefixln("void thread_" + x.getName() + "()");
+        _mainPS.printLeftBracket();
+        _mainPS.printPrefixln("while (!" + x.getName()
+                + "_ins.isDetached())");
+        _mainPS.printLeftBracket();
+        _mainPS.printPrefixln(x.getName() + "_ins.fire();");
+        _mainPS.printPrefixln("eventList.push_back(&" + x.getName()
+                              + "_event);");
+        _mainPS.printPrefixln("sched_event.notify();");
+        _mainPS.printPrefixln("wait(" + x.getName() + "_event);");
+
+        _mainPS.printRightBracket();
+        _mainPS.printRightBracket();
+    }
+
+    /**
+     *
+     * @param x channel that needs to be rendered
+     */
+    public void visitComponent(Channel x) {
+        for (Port p : x.getPortList()) {
+            if (p.isOutPort()) {
+                //we get port name from channel
+                //channel.out == process.in
+                _mainPS.printPrefixln(p.getPeerResource().getName()
+                        + "_ins.INPORT_" + p.getPeerPort().getName()
+                        + "(" + x.getName() + "_ins);");
+            } else if (p.isInPort()) {
+                _mainPS.printPrefixln(p.getPeerResource().getName()
+                        + "_ins.OUTPORT_" + p.getPeerPort().getName()
+                        + "(" + x.getName() + "_ins);");
+            }
+        }
+    }
+
+    protected CodePrintStream _mainPS = null;
+    protected String _dir = null;
+}