dol: initial dol commit
[jump.git] / dol / src / dol / visitor / rtems / RtemsModuleVisitor.java
1 /* $Id: RtemsModuleVisitor.java 114 2010-07-05 07:47:02Z haidw $ */
2 package dol.visitor.rtems;
3
4 import java.io.FileOutputStream;
5 import java.io.OutputStream;
6 import java.util.HashMap;
7 import java.util.Vector;
8
9 import dol.datamodel.architecture.Architecture;
10 import dol.datamodel.mapping.ComputationBinding;
11 import dol.datamodel.mapping.Mapping;
12 import dol.datamodel.mapping.Schedule;
13 import dol.datamodel.mapping.ScheduleEntry;
14 import dol.datamodel.mapping.SchedulingPolicy;
15 import dol.datamodel.pn.Channel;
16 import dol.datamodel.pn.Port;
17 import dol.datamodel.pn.Process;
18 import dol.datamodel.pn.ProcessNetwork;
19 import dol.main.UserInterface;
20 import dol.parser.xml.archischema.ArchiXmlParser;
21 import dol.parser.xml.mapschema.MapXmlParser;
22 import dol.util.CodePrintStream;
23 import dol.visitor.PNVisitor;
24
25 /**
26  * This class is a class for a visitor that is used to generate
27  * the main program.
28  */
29 public class RtemsModuleVisitor extends PNVisitor {
30
31     /**
32      * Constructor.
33      *
34      * @param dir path of this file
35      */
36     public RtemsModuleVisitor(String dir, HashMap<Port, Integer> portMap) {
37         _dir = dir;
38         _portMap = portMap;
39     }
40
41     /**
42      * Visit process network.
43      *
44      * @param x process network that needs to be rendered
45      */
46     public void visitComponent(ProcessNetwork x) {
47         try {
48             _ui = UserInterface.getInstance();
49             String filename = _dir + _delimiter + "main.c";
50             OutputStream file = new FileOutputStream(filename);
51             _mainPS = new CodePrintStream(file);
52
53             Vector<Process> pList = x.getProcessList();
54             Vector<String> processList = new Vector<String>();
55             for (Process p : x.getProcessList()) {
56                 String basename = p.getBasename();
57                 if (!processList.contains(basename)) {
58                     processList.add(basename);
59                 }
60             }
61
62             Architecture arch = null;
63             Mapping mapping = null;
64
65             //create header section
66             if (_ui.getRtemsBSP().equals("pc386")) {
67                 _mainPS.println("#include <bsp.h>");
68                 _mainPS.println("#include <stdlib.h>");
69                 _mainPS.println("#include <stdio.h>");
70                 _mainPS.println("#include <inttypes.h>");
71                 _mainPS.println();
72                 _mainPS.println("rtems_task Init(rtems_task_argument argument);");
73                 _mainPS.println();
74                 _mainPS.println("#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER");
75                 _mainPS.println("#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER");
76                 _mainPS.println("#define CONFIGURE_RTEMS_INIT_TASKS_TABLE");
77                 _mainPS.println("#define CONFIGURE_MAXIMUM_TASKS "
78                         + (x.getProcessList().size() + 2));
79                 _mainPS.println("#define CONFIGURE_MAXIMUM_MESSAGE_QUEUES "
80                         + x.getChannelList().size());
81                 _mainPS.println("#define CONFIGURE_INIT");
82                 _mainPS.println("#include <rtems/confdefs.h>");
83                 _mainPS.println();
84             } else if (_ui.getRtemsBSP().equals("mparm")) {
85                 _mainPS.println("#define TEST_INIT");
86                 _mainPS.println("#include <bsp.h>");
87                 _mainPS.println("#include <stdlib.h>");
88                 _mainPS.println("#include <stdio.h>");
89                 _mainPS.println();
90                 _mainPS.println("#define RTEMS_TRACE_MAIN_APP");
91                 _mainPS.println("#include \"system.h\"");
92                 _mainPS.println("#include \"appsupport.h\"");
93                 _mainPS.println("#include \"scratch_queue.h\"");
94                 _mainPS.println();
95             }
96
97             _mainPS.println("#include \"dol.h\"");
98             _mainPS.println("#include \"rtems_process_wrapper.h\"");
99             _mainPS.println();
100
101             if (_ui.getRtemsBSP().equals("pc386")) {
102                 _mainPS.println("rtems_task CleanupTask(rtems_task_argument arg);");
103                 _mainPS.println("rtems_id queue_id["
104                                 + x.getChannelList().size() + "];");
105                 _mainPS.println();
106             } else if (_ui.getRtemsBSP().equals("mparm")) {
107                 //if no mapping is provided, map each process to a new
108                 //processor
109                 //shaper
110                 _mainPS.println("#ifdef QUEUE_BUFF_SHAPER");
111                 _mainPS.println("#include \"traffic_shaping.h\"");
112                 //_mainPS.println("#define shaper_PROCESSOR "
113                 //        + (pList.size()+1));
114                 _mainPS.println("#endif // shaper");
115                 _mainPS.println();
116
117                 if (_ui.getMappingFileName() == null) {
118
119                     int i = 0;
120                     for (Process p : pList) {
121                         _mainPS.println("#define " + p.getName()
122                                 + "_PROCESSOR " + ++i); //count from 1
123                     }
124                     _mainPS.println();
125
126                     _mainPS.print("unsigned int number_of_processes["
127                             + pList.size() + "] = { ");
128                     for (i = 0; i < pList.size(); i++) {
129                         _mainPS.print("1, ");
130                     }
131                     _mainPS.println("};");
132                     _mainPS.println();
133
134                 } else { //map processes according to mapping file
135                     ArchiXmlParser archParser = new ArchiXmlParser();
136                     arch = archParser.doParse(_ui.getPlatformFileName());
137                     MapXmlParser mappingParser = new MapXmlParser(x, arch);
138                     mapping = mappingParser.doParse(_ui.getMappingFileName());
139                     int numOfUsedProcessors = mapping.getProcessorList().size();
140                     int numOfProcessors = arch.getProcessorList().size();
141                     int processesPerProcessor[] = new int[numOfProcessors];
142
143                     // need to check processors used start from 0 and
144                     //continuous
145                     for (Process p : pList) {
146                         for (ComputationBinding b : mapping.getCompBindList()){
147                             int processorIndex = Integer.valueOf(
148                                  b.getProcessor().getName().
149                                  replaceAll(".*_", ""));
150                             if (b.getProcess().getName().
151                                 equals(p.getName())) {
152                                 processesPerProcessor[processorIndex]++;
153                                 //processor indices start at 1 in MPARM
154                                 _mainPS.println("#define " + p.getName()
155                                         + "_PROCESSOR "
156                                         + (processorIndex + 1));
157                             }
158                         }
159                     }
160
161
162                     for (int i=0; i < numOfUsedProcessors; i++) {
163                     if (processesPerProcessor[i] < 1)
164                         throw new Exception("No process is mapped to "
165                                 + "PROCESSOR[" + i + "]. For the MPARM "
166                                 + "platform, processors should be used "
167                                 + "starting from 0 until N (in the "
168                                 + "generated code, from 1 to N + 1), and "
169                                 + "every processor between 0 and N "
170                                 + "should have at least one process "
171                                 + "mapped on it!");
172                     }
173
174                     _mainPS.println();
175                     /*
176                     _mainPS.println("#ifdef QUEUE_BUFF_SHAPER");
177                     _mainPS.println("#define shaper_PROCESSOR "
178                     + (numOfUsedProcessors+1));
179                     _mainPS.println("#endif // shaper");
180                     _mainPS.println();
181                     */
182                     _mainPS.print("unsigned int number_of_processes[]"
183                             + " = { ");
184                     for (int i = 0; i < numOfUsedProcessors; i++) {
185                         _mainPS.print(processesPerProcessor[i] + ", ");
186                     }
187                     _mainPS.println("};");
188                     _mainPS.println();
189                 }
190
191                 _mainPS.println("unsigned int active_processes;");
192                 _mainPS.println("inline void processor_init() "
193                         + "{active_processes"
194                         + " = number_of_processes[get_id() - 1];}");
195                 _mainPS.println();
196
197                 for (Process p : pList) {
198                     _mainPS.println("void " + "rtems_" + p.getName()
199                             + "_init(rtems_task_argument arg);");
200                 }
201                 _mainPS.println();
202             }
203
204             for (String pName : x.getProcessBasenames()) {
205                 _mainPS.println("extern rtems_task " + pName
206                         + "_task(rtems_task_argument argument);");
207             }
208             _mainPS.println();
209
210             //declare processes and channels
211             for (Process process : x.getProcessList()) {
212                 _mainPS.println("RtemsProcessWrapper *" + process.getName()
213                         + "_wrapper;");
214             }
215             _mainPS.println();
216
217             _mainPS.println("#ifdef PRINTF_TO_DEBUG");
218             _mainPS.println("#undef printf");
219             _mainPS.println("#define printf(...) \\");
220             _mainPS.println("  do {              \\");
221             _mainPS.println("    char _buffer[128];             \\");
222             _mainPS.println("    sprintf(_buffer, __VA_ARGS__); \\");
223             _mainPS.println("    SHOW_DEBUG((int)_buffer);      \\");
224             _mainPS.println("  } while (0)");
225             _mainPS.println("#endif");
226             _mainPS.println();
227             _mainPS.println("#ifdef WORKLOAD_EXTRACT");
228             _mainPS.println("void callback(int code, int* arg, int size) "
229                     + "{");
230             _mainPS.println("  long unsigned int time = get_cycle1();");
231             _mainPS.println("  Thread_Control *x = "
232                     + "(Thread_Control*)arg;");
233             _mainPS.println("  SHOW_DEBUG(\"log callback\");");
234             _mainPS.println("  SHOW_DEBUG_INT((int)time);");
235             _mainPS.println("  SHOW_DEBUG_INT(x->Object.id);");
236             _mainPS.println("}");
237             _mainPS.println("#endif");
238             _mainPS.println();
239             _mainPS.println("rtems_id timer_sem_id;");
240             _mainPS.println();
241             //RTEMS init task
242             _mainPS.println("/**");
243             _mainPS.println(" *");
244             _mainPS.println(" */");
245             _mainPS.println("rtems_task Init(rtems_task_argument arg) {");
246
247             if (_ui.getRtemsBSP().equals("mparm")) {
248                 // //////////////////// mparm /////////////////////////
249                 int processCount = 0;
250
251                 _mainPS.println("  rtems_id task_id;");
252                 _mainPS.println("  rtems_status_code status;");
253                 _mainPS.println();
254                 _mainPS.println("  status = rtems_semaphore_create(rtems_build_name('c','s','e','m'), 1, RTEMS_BINARY_SEMAPHORE |RTEMS_NO_PRIORITY_CEILING |RTEMS_LOCAL |RTEMS_PRIORITY, 0,&timer_sem_id);");
255                 _mainPS.println("  if (status != RTEMS_SUCCESSFUL) {");
256                 _mainPS.println("     printf(\"[init    ] Could not create semaphore (status %d).\", status);");
257                 _mainPS.println("      rtems_shutdown_executive(0);");
258                 _mainPS.println("  }");
259
260
261                 _mainPS.println();
262
263                 _mainPS.println("  scratch_queue_autoinit_system();");
264                 _mainPS.println();
265
266                 _mainPS.println("#ifdef WORKLOAD_EXTRACT");
267                 _mainPS.println("  rtems_monitor_register_callback"
268                         + "(callback);");
269                 for (Channel c : x.getChannelList()) {
270                     _mainPS.println("  SHOW_DEBUG(\"log "
271                             + "create_channel\");");
272                     _mainPS.println("  SHOW_DEBUG(\"log " + c.getName()
273                             + "\");");
274                     _mainPS.println("  SHOW_DEBUG_INT((int)"
275                             + (c.getSize() * c.getTokenSize()) + ");");
276                 }
277                 _mainPS.println("#endif");
278
279                 //shaper process
280                 _mainPS.println("#ifdef QUEUE_BUFF_SHAPER");
281                 _mainPS.println("  if ((get_id()-1) == shaper_PROCESSOR) "
282                         + "{");
283                 _mainPS.println("    rtems_name name = rtems_build_name"
284                                 +"('s', 's', '0', ' ');");
285                 _mainPS.println();
286
287                 // create pre task
288                 _mainPS.println("    //one more init to prevent the"
289                         + "blocking instatiation of scrach queue");
290                 _mainPS.println("    status = rtems_task_create(");
291                 _mainPS.println("          name,");
292                 _mainPS.println("          1,");
293                 _mainPS.println("          RTEMS_MINIMUM_STACK_SIZE,");
294                 _mainPS.println("          RTEMS_DEFAULT_MODES,");
295                 _mainPS.println("          RTEMS_LOCAL,");
296                 _mainPS.println("          &task_id, -1);");
297                 _mainPS.println("    if (status != RTEMS_SUCCESSFUL) {");
298                 _mainPS.println("      printf(\"[init    ] "
299                         + "Could not create init shaper"
300                         + "(status %d).\\n\", status);");
301                 _mainPS.println("      rtems_shutdown_executive(0);");
302                 _mainPS.println("    }");
303                 _mainPS.println();
304
305                 // start pre init
306                 _mainPS.println("    status = rtems_task_start(task_id, "
307                         + "shaping_init, arg);");
308                 _mainPS.println("    if (status != RTEMS_SUCCESSFUL) {");
309                 _mainPS.println("      printf(\"[init    ] "
310                         + "Could not start init shaping"
311                         + " (status %d).\\n\", status);");
312                 _mainPS.println("    rtems_shutdown_executive(0);");
313                 _mainPS.println("    }");
314                 _mainPS.println("  }");
315                 _mainPS.println("#endif // QUEUE_BUFF_SHAPER");
316                 _mainPS.println();
317
318                 for (Process process : x.getProcessList()) {
319                     _mainPS.println("  if (get_id() == "
320                             + process.getName() + "_PROCESSOR) {");
321                     _mainPS.println("    processor_init();");
322                     _mainPS.println("    rtems_name name = "
323                             + "rtems_build_name"
324                             + "('p', 'i', 0x" + ++processCount
325                             + ", ' ');");
326                     _mainPS.println();
327
328                     // create pre task
329                     _mainPS.println("    //one more init to prevent the"
330                             + "blocking instatiation of scrach queue");
331                     _mainPS.println("    status = rtems_task_create(");
332                     _mainPS.println("          name,");
333                     _mainPS.println("          1,");
334                     _mainPS.println("          RTEMS_MINIMUM_STACK_SIZE"
335                             + ",");
336                     _mainPS.println("          RTEMS_DEFAULT_MODES,");
337                     _mainPS.println("          RTEMS_LOCAL,");
338                     _mainPS.println("          &task_id, -1);");
339                     _mainPS.println("    if (status != RTEMS_SUCCESSFUL) "
340                             + "{");
341                     _mainPS.println("      printf(\"[init    ] "
342                             + "Could not create init task["
343                             + process.getName() + "] "
344                             +"(status %d).\\n\", status);");
345                     _mainPS.println("      rtems_shutdown_executive(0);");
346                     _mainPS.println("    }");
347                     _mainPS.println();
348
349                     // start pre init
350                     _mainPS.println("    status = rtems_task_start(task_id, "
351                             + "rtems_" + process.getName()
352                             + "_init, arg);");
353                     _mainPS.println("    if (status != RTEMS_SUCCESSFUL) {");
354                     _mainPS.println("      printf(\"[init    ] "
355                             + "Could not start init task [ "
356                             + process.getName()
357                             + "] (status %d).\\n\", status);");
358                     _mainPS.println("    rtems_shutdown_executive(0);");
359                     _mainPS.println("    }");
360                     /*
361                     _mainPS.println("    rtems_" + process.getName()
362                             + "_init(arg);");
363                     _mainPS.println("    rtems_task_delete(RTEMS_SELF);");
364                     */
365                     _mainPS.println("  }");
366                     _mainPS.println();
367                 }
368
369                 // shaper process
370                 _mainPS.println("#ifdef QUEUE_BUFF_SHAPER");
371                 _mainPS.println("  if ((get_id()-1) == shaper_PROCESSOR) "
372                         + "{");
373                 _mainPS.println("    rtems_name name = rtems_build_name"
374                         +"('s', 's', '0', ' ');");
375                 _mainPS.println();
376
377                 // create pre task
378                 _mainPS.println("    //one more init to prevent the"
379                         + "blocking instatiation of scrach queue");
380                 _mainPS.println("    status = rtems_task_create(");
381                 _mainPS.println("          name,");
382                 _mainPS.println("          128,");
383                 _mainPS.println("          RTEMS_MINIMUM_STACK_SIZE,");
384                 _mainPS.println("          RTEMS_DEFAULT_MODES,");
385                 _mainPS.println("          RTEMS_LOCAL,");
386                 _mainPS.println("          &task_id, -1);");
387                 _mainPS.println("    if (status != RTEMS_SUCCESSFUL) {");
388                 _mainPS.println("      printf(\"[init    ] "
389                         + "Could not create init shaper"
390                         +"(status %d).\\n\", status);");
391                 _mainPS.println("      rtems_shutdown_executive(0);");
392                 _mainPS.println("    }");
393                 _mainPS.println();
394
395                 // start pre init
396                 _mainPS.println("    status = rtems_task_start(task_id, "
397                         + "shaping_init, arg);");
398                 _mainPS.println("    if (status != RTEMS_SUCCESSFUL) {");
399                 _mainPS.println("      printf(\"[init    ] "
400                         + "Could not start init shaping"
401                         + " (status %d).\\n\", status);");
402                 _mainPS.println("    rtems_shutdown_executive(0);");
403                 _mainPS.println("    }");
404                 _mainPS.println("  }");
405                 _mainPS.println("#endif // QUEUE_BUFF_SHAPER");
406                 _mainPS.println();
407
408                 _mainPS.println("if (number_of_processes[get_id() - 1]"
409                         + " == 0) {");
410                 _mainPS.println("    printf(\"No process on processor "
411                         + "%d.\", get_id());");
412                 _mainPS.println("    rtems_shutdown_executive(0);");
413                 _mainPS.println("}");
414                 _mainPS.println();
415
416                 _mainPS.println("  rtems_task_delete(RTEMS_SELF);");
417                 _mainPS.println("}");
418                 _mainPS.println();
419                 // end of init
420
421                 processCount = 0;
422                 for (Process process : x.getProcessList()) {
423                     String processName = process.getName();
424                     _mainPS.println("void rtems_" + process.getName()
425                             + "_init(rtems_task_argument arg) {");
426                     _mainPS.println("  rtems_id task_id;");
427                     _mainPS.println("  rtems_status_code status;");
428
429
430                     _mainPS.println("  " + process.getName()
431                             + "_wrapper = "
432                             + "(RtemsProcessWrapper *)malloc"
433                             + "(sizeof(RtemsProcessWrapper));");
434
435                     int numOfOutports = process.getNumOfOutports();
436                     int numOfInports  = process.getNumOfInports();
437                     if (numOfOutports > 0) {
438                         _mainPS.println("  int* "
439                                 + process.getName()
440                                 + "_out_port_id = (int *)malloc("
441                                 + numOfOutports
442                                 + " * sizeof(int));");
443                         _mainPS.println("  SCRATCH_QUEUE_PRODUCER **"
444                                 + process.getName()
445                                 + "_out_queue_id = "
446                                 + "(SCRATCH_QUEUE_PRODUCER **)"
447                                 + "malloc("
448                                 + numOfOutports
449                                 + "*sizeof(SCRATCH_QUEUE_PRODUCER));");
450                     }
451
452                     if (numOfInports > 0) {
453                         _mainPS.println("  int *"
454                                 + process.getName()
455                                 + "_in_port_id = (int *)malloc("
456                                 + numOfInports
457                                 + " * sizeof(int));");
458                         _mainPS.println("  SCRATCH_QUEUE_CONSUMER **"
459                                 + process.getName()
460                                 + "_in_queue_id = "
461                                 + "(SCRATCH_QUEUE_CONSUMER **)"
462                                 + "malloc("
463                                 + numOfOutports
464                                 + "*sizeof(SCRATCH_QUEUE_CONSUMER));");
465                     }
466                     _mainPS.println();
467
468                     // create task
469                     _mainPS.println("  status = rtems_task_create(");
470                     _mainPS.println("          " + ++processCount + ",");
471                     _mainPS.println("          1,");
472                     _mainPS.println("          RTEMS_MINIMUM_STACK_SIZE,");
473                     _mainPS.println("          RTEMS_DEFAULT_MODES,");
474                     _mainPS.println("          RTEMS_LOCAL,");
475                     _mainPS.println("          &task_id, -1);");
476                     _mainPS.println("  if (status != RTEMS_SUCCESSFUL) {");
477                     _mainPS.println("    printf(\"[init    ] "
478                             + "Could not create task["
479                             + processName + "] "
480                             +"(status %d).\\n\", status);");
481                     _mainPS.println("    rtems_shutdown_executive(0);");
482                     _mainPS.println("  }");
483
484                     _mainPS.println("#ifdef WORKLOAD_EXTRACT");
485                     _mainPS.println("  SHOW_DEBUG(\"log create_task\");");
486                     _mainPS.println("  SHOW_DEBUG_INT(task_id);");
487                     _mainPS.println("  SHOW_DEBUG(\"log " + processName + "\");");
488                     for (Port port : process.getPortList()) {
489                         Channel c = (Channel) port.getPeerResource();
490                         if (port.isInPort()) {
491                             _mainPS.println("  SHOW_DEBUG(\"log in_port\");");
492                             _mainPS.println("  SHOW_DEBUG_INT((int)" + _portMap.get(port) + ");");
493                             _mainPS.println("  SHOW_DEBUG(\"log " + c.getName() + "\");");
494                         }
495                         if (port.isOutPort()) {
496                             _mainPS.println("  SHOW_DEBUG(\"log out_port\");");
497                             _mainPS.println("  SHOW_DEBUG_INT((int)" + _portMap.get(port) + ");");
498                             _mainPS.println("  SHOW_DEBUG(\"log " + c.getName() + "\");");
499                         }
500                     }
501                     _mainPS.println("#endif");
502                     _mainPS.println();
503
504                     // connect ports to channels
505                     HashMap<Channel, Integer> channel_map =
506                         new HashMap<Channel, Integer>();
507                     int channelCount = 1;
508                     for (Channel c : x.getChannelList()) {
509                         channel_map.put(c, channelCount++);
510                     }
511                     _mainPS.println();
512
513                     // fill the wrapper, instantiate queues
514                     int i = 0, j = 0;
515                     for (Port port : process.getPortList()) {
516                         Channel c = (Channel)(port.getPeerResource());
517                         if (port.isInPort()) {
518                             _mainPS.println("  " + processName + "_in_port_id["
519                                     + i + "] = "
520                                     + _portMap.get(port) + ";");
521                             _mainPS.println("  " + processName
522                                     + "_in_queue_id["
523                                     + i + "] = ");
524                             _mainPS.println("#if defined (QUEUE_BUFF_IN_PRODUCER) || (QUEUE_BUFF_IN_PRODUCER_DMA)");
525                             _mainPS.println("  scratch_queue_autoinit_consumer("
526                                     + channel_map.get(c)
527                                     + ", true);");
528                             _mainPS.println("#elif defined (QUEUE_BUFF_IN_CONSUMER) || (QUEUE_BUFF_IN_CONSUMER_DMA)");
529                             _mainPS.println("  scratch_queue_autoinit_consumer("
530                                     + c.getOrigin().getName()
531                                     + "_PROCESSOR, "
532                                     + channel_map.get(c) + ", "
533                                     + c.getSize() + ", "
534                                     + c.getTokenSize()
535                                     + ");");
536                             _mainPS.println("#elif defined (QUEUE_BUFF_IN_SHARDMEM)");
537                             _mainPS.println("  scratch_queue_autoinit_consumer("
538                                     + channel_map.get(c)
539                                     + ", true);");
540                             _mainPS.println("#elif defined (QUEUE_BUFF_SHAPER)");
541                             _mainPS.println(" scratch_queue_autoinit_consumer("
542                                     + channel_map.get(c)
543                                     + ");");
544                             _mainPS.println("#endif");
545
546
547                             i++;
548                         } else if (port.isOutPort()) {
549                             _mainPS.println("  " + processName+"_out_port_id["
550                                     + j + "] = "
551                                     + _portMap.get(port) + ";");
552                             _mainPS.println("  " + processName
553                                     + "_out_queue_id["
554                                     + j + "] = ");
555
556                             _mainPS.println("#if defined (QUEUE_BUFF_IN_PRODUCER) || (QUEUE_BUFF_IN_PRODUCER_DMA)");
557                             _mainPS.println("  scratch_queue_autoinit_producer("
558                                     + c.getTarget().getName()
559                                     + "_PROCESSOR,"
560                                     + channel_map.get(c) + ", "
561                                     + c.getSize() + ", "
562                                     + c.getTokenSize()
563                                     + ", 0);");
564                             _mainPS.println("#elif defined (QUEUE_BUFF_IN_CONSUMER) || (QUEUE_BUFF_IN_CONSUMER_DMA)");
565                             _mainPS.println("  scratch_queue_autoinit_producer("
566                                     + channel_map.get(c)
567                                     + ", true);");
568                             _mainPS.println("#elif defined (QUEUE_BUFF_IN_SHARDMEM)");
569                             _mainPS.println("  scratch_queue_autoinit_producer("
570                                     + c.getTarget().getName()
571                                     + "_PROCESSOR,"
572                                     + channel_map.get(c) + ", "
573                                     + c.getSize() + ", "
574                                     + c.getTokenSize()
575                                     + ", 1);");
576                             _mainPS.println("#elif defined (QUEUE_BUFF_SHAPER)");
577                             _mainPS.println("  scratch_queue_autoinit_producer("
578                                     + c.getTarget().getName()
579                                     + "_PROCESSOR,"
580                                     + channel_map.get(c) + ", "
581                                     + c.getSize() + ", "
582                                     + c.getTokenSize()
583                                     + ");");
584                             _mainPS.println("#endif");
585
586                             j++;
587                         }
588                     }
589
590                     if (numOfInports > 0) {
591                         _mainPS.println("  " + processName
592                                 + "_wrapper->in_port_id = "
593                                 + processName
594                                 + "_in_port_id;");
595                         _mainPS.println("  " + processName
596                                 + "_wrapper->in_queue_id = "
597                                 + processName
598                                 + "_in_queue_id;");
599                         _mainPS.println("  " + processName
600                                 + "_wrapper->number_of_in_ports = "
601                                 + numOfInports + ";");
602                     }
603
604                     if (numOfOutports > 0) {
605                         _mainPS.println("  " + processName
606                                 + "_wrapper->out_port_id = "
607                                 + processName
608                                 + "_out_port_id;");
609                         _mainPS.println("  " + processName
610                                 + "_wrapper->out_queue_id = "
611                                 + processName
612                                 + "_out_queue_id;");
613                         _mainPS.println("  " + processName
614                                 + "_wrapper->number_of_out_ports = "
615                                 + numOfOutports + ";");
616                     }
617
618                     _mainPS.println("  " + processName
619                             + "_wrapper->is_detached = 0;");
620
621                     int priority = 127;
622                     if (mapping != null) {
623                         Schedule s = mapping.getScheduleByResource(
624                                 process.getProcessor().getName());
625                         if (s != null && s.getSchedPolicy() ==
626                                 SchedulingPolicy.FIXEDPRIORITY) {
627                             ScheduleEntry e = s.getScheduleEntry(
628                                     process.getName());
629                             if (e != null) {
630                                 String value = e.getCfgValue("priority");
631                                 if (value != null) {
632                                     priority = Integer.parseInt(value);
633                                 }
634                             }
635                         }
636                     }
637                     _mainPS.println("  " + processName
638                             + "_wrapper->priority = " + priority + ";");
639
640                     _mainPS.println("  " + processName
641                             + "_wrapper->name = (char *)malloc(("
642                             + processName.length()
643                             + " + 1) * sizeof(char));");
644                     _mainPS.println("  strcpy(" + processName
645                             + "_wrapper->name, " + "\""
646                             + processName + "\");");
647                     _mainPS.println();
648
649                     _mainPS.println("  status = rtems_task_start("
650                             + "task_id, " + process.getBasename()
651                             + "_task, " + "(rtems_task_argument)"
652                             + process.getName() + "_wrapper);");
653                     _mainPS.println("  if (status != RTEMS_SUCCESSFUL) "
654                             + "{");
655                     _mainPS.println("    printf(\"[init    ] "
656                             + "Could not start " + process.getName()
657                             + " (status %d).\\n\", status);");
658                     _mainPS.println("    rtems_shutdown_executive(0);");
659                     _mainPS.println("  }");
660                     _mainPS.println("  rtems_task_delete(RTEMS_SELF);");
661                     _mainPS.println("}");
662                     _mainPS.println();
663                     _mainPS.println();
664                 }
665             } else if (_ui.getRtemsBSP().equals("pc386")) {
666                 // //////////////////// pc386 ////////////////////////
667                 _mainPS.println("  int j;");
668                 _mainPS.println("  rtems_id task_id[" +
669                                 + (x.getProcessList().size() + 1) + "];");
670                 _mainPS.println("  rtems_status_code status;");
671                 _mainPS.println();
672
673                 //initialize process-specific information
674                 for (Process process : x.getProcessList()) {
675                     int numberOfPorts = x.getProcess(process.getName())
676                         .getPortList().size();
677                     _mainPS.println("  " + process.getName()
678                             + "_wrapper = "
679                             + "malloc(sizeof(RtemsProcessWrapper));");
680                     _mainPS.println("  int *"
681                             + process.getName() + "_port_id = malloc("
682                             + numberOfPorts + " * sizeof(int));");
683                     _mainPS.println("  int *"
684                             + process.getName()
685                             + "_port_queue_id = malloc("
686                             + numberOfPorts + " * sizeof(int));");
687                     _mainPS.println();
688                 }
689
690                 //create a task for each process
691                 _mainPS.println("  for (j = 0; j < "
692                         + (x.getProcessList().size()+1) + "; j++) {");
693                 _mainPS.println("    status = rtems_task_create(");
694                 _mainPS.println("            j + 1,");
695                 _mainPS.println("            128,");
696                 _mainPS.println("            RTEMS_MINIMUM_STACK_SIZE,");
697                 _mainPS.println("            RTEMS_DEFAULT_MODES,");
698                 _mainPS.println("            RTEMS_DEFAULT_ATTRIBUTES,");
699                 _mainPS.println("            &(task_id[j]));");
700                 _mainPS.println("    if (status != RTEMS_SUCCESSFUL) {");
701                 _mainPS.println("      printf(\"[init    ] "
702                         + "Could not create task[%d] (status %d).\\n\", "
703                         + "(int)j, status);");
704                 _mainPS.println("      rtems_shutdown_executive(0);");
705                 _mainPS.println("    }");
706                 _mainPS.println("  }");
707                 _mainPS.println();
708
709                 //create a message queue for each channel
710                 int j = 0;
711                 for (Channel channel : x.getChannelList()) {
712                     _mainPS.println("  status = "
713                             + "rtems_message_queue_create(");
714                     _mainPS.println("          " + (j + 1) + ",");
715                     _mainPS.println("          " + channel.getSize()
716                             + ",");
717                     _mainPS.println("          1,");
718                     _mainPS.println("          RTEMS_DEFAULT_ATTR"
719                             + "IBUTES,");
720                     _mainPS.println("          &queue_id[" + j + "]);");
721                     _mainPS.println("  if (status != RTEMS_SUCCESSFUL) "
722                             + "{");
723                     _mainPS.println("    printf(\"[init    ] "
724                             + "Could not create queue[" + j
725                             + "] (status %d).\\n\", status);");
726                     _mainPS.println("      rtems_shutdown_executive(0);");
727                     _mainPS.println("    }");
728                     _mainPS.println();
729                     j++;
730                 }
731
732                 //connect ports to channels
733                 HashMap<Channel, Integer> channel_map =
734                     new HashMap<Channel, Integer>();
735                 j = 0;
736                 for (Channel c : x.getChannelList()) {
737                     channel_map.put(c, j++);
738                 }
739
740                 for (Process process : x.getProcessList()) {
741                     String processName = process.getName();
742                     int i = 0;
743                     for (Port port : process.getPortList()) {
744                         Channel c = (Channel)(port.getPeerResource());
745
746                         _mainPS.println("  " + processName + "_port_id["
747                                 + i + "] = "
748                                 + _portMap.get(port) + ";");
749                         _mainPS.println("  " + processName
750                                 + "_port_queue_id[" + i + "] = queue_id["
751                                 + channel_map.get(c) + "];");
752                         i++;
753                     }
754                     _mainPS.println("  " + processName
755                             + "_wrapper->port_id = " + processName
756                             + "_port_id;");
757                     _mainPS.println("  " + processName
758                             + "_wrapper->port_queue_id = "
759                             + processName
760                             + "_port_queue_id;");
761                     _mainPS.println("  " + processName
762                             + "_wrapper->number_of_ports = "
763                             + i + ";");
764                     _mainPS.println("  " + processName
765                             + "_wrapper->is_detached = 0;");
766                     _mainPS.println("  " + processName
767                             + "_wrapper->name = malloc(("
768                             + processName.length()
769                             + " + 1) * sizeof(char));");
770                     _mainPS.println("  strcpy(" + processName
771                             + "_wrapper->name, " + "\""
772                             + processName + "\");");
773                     _mainPS.println();
774                 }
775
776                 //start cleanup task
777                 _mainPS.println("  printf(\"[init    ] "
778                         + "Start cleanup.\\n\");");
779                 _mainPS.println("  status = rtems_task_start(task_id[0], "
780                         + "CleanupTask, 0);");
781                 _mainPS.println("  if (status != RTEMS_SUCCESSFUL) {");
782                 _mainPS.println("    printf(\"[init    ] Could not start "
783                         + "cleanup (status %d).\\n\", status);");
784                 _mainPS.println("    rtems_shutdown_executive(0);");
785                 _mainPS.println("  }");
786                 _mainPS.println();
787
788                 //start processes
789                 j = 1;
790                 for (Process process : x.getProcessList()) {
791                     _mainPS.println("  printf(\"[init    ] Start "
792                             + process.getName() + ".\\n\");");
793                     _mainPS.println("  status = rtems_task_start(task_id["
794                             + j + "], " + process.getBasename()
795                             + "_task, "
796                             + "(rtems_task_argument)"
797                             + process.getName()
798                             + "_wrapper);");
799                     _mainPS.println("  if (status != RTEMS_SUCCESSFUL) "
800                             + "{");
801                     _mainPS.println("    printf(\"[init    ] "
802                             + "Could not start " + process.getName()
803                             + " (status %d).\\n\", status);");
804                     _mainPS.println("    rtems_shutdown_executive(0);");
805                     _mainPS.println("  }");
806                     _mainPS.println();
807                     j++;
808                 }
809
810                 _mainPS.println("  printf(\"[init    ] Done.\\n\");");
811                 _mainPS.println("  rtems_task_delete(RTEMS_SELF);");
812                 _mainPS.println("}");
813                 _mainPS.println();
814
815                 _mainPS.println("rtems_task CleanupTask(rtems_task_argument "
816                         + "argument) {");
817                 _mainPS.println("  printf(\"[cleanup ] Started.\\n\");");
818                 _mainPS.println("  do {");
819                 _mainPS.println("    rtems_task_wake_after(0);");
820                 _mainPS.println("  } while (");
821                 j = 0;
822                 for (Process process : x.getProcessList()) {
823                     _mainPS.print("           !("
824                                   + process.getName()
825                                   + "_wrapper->is_detached)");
826                     j++;
827                     if (j < x.getProcessList().size()) {
828                         _mainPS.println(" || ");
829                     } else {
830                         _mainPS.println(");");
831                     }
832                 }
833                 _mainPS.println();
834                 _mainPS.println("  printf(\"[cleanup ] Shutdown.\\n\");");
835                 _mainPS.println("  rtems_shutdown_executive(0);");
836                 _mainPS.println("}");
837                 _mainPS.println();
838             }
839         }
840         catch (Exception e) {
841             System.out.println("RtemsModuleVisitor: exception occured: "
842                                + e.getMessage());
843             e.printStackTrace();
844         }
845     }
846
847     /**
848      *
849      * @param x process that needs to be processed
850      */
851     public void visitComponent(Process x) {
852     }
853
854     /**
855      *
856      * @param x channel that needs to be processed
857      */
858     public void visitComponent(Channel x) {
859     }
860
861     protected CodePrintStream _mainPS = null;
862     protected String _dir = null;
863     protected HashMap<Port, Integer> _portMap;
864 }