]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/hal/synth/arch/v2_0/doc/synth.sgml
Initial revision
[karo-tx-redboot.git] / packages / hal / synth / arch / v2_0 / doc / synth.sgml
1 <!-- DOCTYPE part  PUBLIC "-//OASIS//DTD DocBook V3.1//EN" -->
2
3 <!-- {{{ Banner                         -->
4
5 <!-- =============================================================== -->
6 <!--                                                                 -->
7 <!--     synth.sgml                                                  -->
8 <!--                                                                 -->
9 <!--     Synthetic target architectural documentation.               -->
10 <!--                                                                 -->
11 <!-- =============================================================== -->
12 <!-- ####COPYRIGHTBEGIN####                                          -->
13 <!--                                                                 -->
14 <!-- =============================================================== -->
15 <!-- Copyright (C) 2002 Bart Veer.                                   -->
16 <!-- Copyright (C) 2002 Red Hat, Inc.                                -->
17 <!-- This material may be distributed only subject to the terms      -->
18 <!-- and conditions set forth in the Open Publication License, v1.0  -->
19 <!-- or later (the latest version is presently available at          -->
20 <!-- http://www.opencontent.org/openpub/)                            -->
21 <!-- Distribution of the work or derivative of the work in any       -->
22 <!-- standard (paper) book form is prohibited unless prior           -->
23 <!-- permission obtained from the copyright holder                   -->
24 <!-- =============================================================== -->
25 <!--                                                                 -->      
26 <!-- ####COPYRIGHTEND####                                            -->
27 <!-- =============================================================== -->
28 <!-- =============================================================== -->
29 <!-- #####DESCRIPTIONBEGIN####                                       -->
30 <!--                                                                 -->
31 <!-- Author(s):   bartv                                              -->
32 <!-- Contact(s):  bartv                                              -->
33 <!-- Date:        2002/02/24                                         -->
34 <!-- Version:     0.01                                               -->
35 <!--                                                                 -->
36 <!-- ####DESCRIPTIONEND####                                          -->
37 <!-- =============================================================== -->
38
39 <!-- }}} -->
40
41 <part id="hal-synth-arch"><title>eCos Synthetic Target</title>
42
43 <!-- {{{ Overview                       -->
44
45 <refentry id="synth">
46   <refmeta>
47     <refentrytitle>Overview</refentrytitle>
48   </refmeta>
49   <refnamediv>
50     <refname>The eCos synthetic target</refname>
51     <refpurpose>Overview</refpurpose>
52   </refnamediv>
53
54   <refsect1 id="synth-description"><title>Description</title>
55     <para>
56 Usually eCos runs on either a custom piece of hardware, specially
57 designed to meet the needs of a specific application, or on a
58 development board of some sort that is available before the final
59 hardware. Such boards have a number of things in common:
60     </para>
61     <orderedlist>
62       <listitem><para>
63 Obviously there has to be at least one processor to do the work. Often
64 this will be a 32-bit processor, but it can be smaller or larger.
65 Processor speed will vary widely, depending on the expected needs of
66 the application. However the exact processor being used tends not to
67 matter very much for most of the development process: the use of
68 languages such as C or C++ means that the compiler will handle those
69 details.
70       </para></listitem>
71       <listitem><para>
72 There needs to be memory for code and for data. A typical system will
73 have two different types of memory. There will be some non-volatile
74 memory such as flash, EPROM or masked ROM. There will also be some
75 volatile memory such as DRAM or SRAM. Often the code for the final
76 application will reside in the non-volatile memory and all of the RAM
77 will be available for data. However updating non-volatile memory
78 requires a non-trivial amount of effort, so for much of the
79 development process it is more convenient to burn suitable firmware,
80 for example RedBoot, into the non-volatile memory and then use that to
81 load the application being debugged into RAM, alongside the
82 application data and a small area reserved for use by the firmware.
83       </para></listitem>
84       <listitem><para>
85 The platform must provide certain mimimal I/O facilities. Most eCos
86 configurations require a clock signal of some sort. There must also be
87 some way of outputting diagnostics to the user, often but not always 
88 via a serial port. Unless special debug hardware is being used, source
89 level debugging will require bidirectional communication between a
90 host machine and the target hardware, usually via a serial port or an
91 ethernet device.
92       </para></listitem>
93       <listitem><para>
94 All the above is not actually very useful yet because there is no way
95 for the embedded device to interact with the rest of the world, except
96 by generating diagnostics. Therefore an embedded device will have
97 additional I/O hardware. This may be fairly standard hardware such as
98 an ethernet or USB interface, or special hardware designed
99 specifically for the intended application, or quite often some
100 combination. Standard hardware such as ethernet or USB may be
101 supported by eCos device drivers and protocol stacks, whereas the
102 special hardware will be driven directly by application code.
103       </para></listitem>
104     </orderedlist>
105     <para>
106 Much of the above can be emulated on a typical PC running Linux.
107 Instead of running the embedded application being developed on a
108 target board of some sort, it can be run as a Linux process. The
109 processor will be the PC's own processor, for example an x86, and the
110 memory will be the process' address space. Some I/O facilities can be
111 emulated directly through system calls. For example clock hardware can
112 be emulated by setting up a <literal>SIGALRM</literal> signal, which
113 will cause the process to be interrupted at regular intervals. This
114 emulation of real hardware will not be particularly accurate, the
115 number of cpu cycles available to the eCos application between clock
116 ticks will vary widely depending on what else is running on the PC,
117 but for much development work it will be good enough.
118     </para>
119     <para>
120 Other I/O facilities are provided through an I/O auxiliary process,
121 ecosynth, that gets spawned by the eCos application during startup.
122 When an eCos device driver wants to perform some I/O operation, for
123 example send out an ethernet packet, it sends a request to the I/O
124 auxiliary. That is an ordinary Linux application so it has ready
125 access to all normal Linux I/O facilities. To emulate a device
126 interrupt the I/O auxiliary can raise a <literal>SIGIO</literal>
127 signal within the eCos application. The HAL's interrupt subsystem
128 installs a signal handler for this, which will then invoke the
129 standard eCos ISR/DSR mechanisms. The I/O auxiliary is based around
130 Tcl scripting, making it easy to extend and customize. It should be
131 possible to configure the synthetic target so that its I/O
132 functionality is similar to what will be available on the final target
133 hardware for the application being developed.
134     </para>
135     <informalfigure PgWide=1>
136       <mediaobject>
137         <imageobject>
138           <imagedata fileref="synth-io-overview.png" Scalefit=1 Align="Center">
139         </imageobject>
140       </mediaobject>
141     </informalfigure>
142     <para>
143 A key requirement for synthetic target code is that the embedded
144 application must not be linked with any of the standard Linux
145 libraries such as the GNU C library: that would lead to a confusing
146 situation where both eCos and the Linux libraries attempted to provide
147 functions such as <function>printf</function>. Instead the synthetic
148 target support must be implemented directly on top of the Linux
149 kernels' system call interface. For example, the kernel provides a
150 system call for write operations. The actual function
151 <function>write</function> is implemented in the system's C library,
152 but all it does is move its arguments on to the stack or into certain
153 registers and then execute a special trap instruction such as
154 <literal>int&nbsp;0x80</literal>. When this instruction is executed
155 control transfers into the kernel, which will validate the arguments
156 and perform the appropriate operation. Now, a synthetic target
157 application cannot be linked with the system's C library. Instead it
158 contains a function <function>cyg_hal_sys_write</function> which, like
159 the C library's <function>write</function> function, pushes its
160 arguments on to the stack and executes the trap instruction. The Linux
161 kernel cannot tell the difference, so it will perform the I/O
162 operation requested by the synthetic target. With appropriate
163 knowledge of what system calls are available, this makes it possible
164 to emulate the required I/O facilities. For example, spawning the
165 ecosynth I/O auxiliary involves system calls
166 <function>cyg_hal_sys_fork</function> and
167 <function>cyg_hal_sys_execve</function>, and sending a request to the
168 auxiliary uses <function>cyg_hal_sys_write</function>.
169     </para>
170     <para>
171 In many ways developing for the synthetic target is no different from
172 developing for real embedded targets. eCos must be configured
173 appropriately: selecting a suitable target such as
174 <userinput>i386linux</userinput> will cause the configuration system
175 to load the appropriate packages for this hardware; this includes an
176 architectural HAL package and a platform-specific package; the
177 architectural package contains generic code applicable to all Linux
178 platforms, whereas the platform package is for specific Linux
179 implementations such as the x86 version and contains any
180 processor-specific code. Selecting this target will also bring in some
181 device driver packages. Other aspects of the configuration such as
182 which API's are supported are determined by the template, by adding
183 and removing packages, and by fine-grained configuration.
184     </para>
185     <para>
186 In other ways developing for the synthetic target can be much easier
187 than developing for a real embedded target. For example there is no
188 need to worry about building and installing suitable firmware on the
189 target hardware, and then downloading and debugging the actual
190 application over a serial line or a similar connection. Instead an
191 eCos application built for the synthetic target is mostly
192 indistinguishable from an ordinary Linux program. It can be run simply
193 by typing the name of the executable file at a shell prompt.
194 Alternatively you can debug the application using whichever version of
195 gdb is provided by your Linux distribution. There is no need to build
196 or install special toolchains. Essentially using the synthetic target
197 means that the various problems associated with real embedded hardware
198 can be bypassed for much of the development process.
199     </para>
200     <para>
201 The eCos synthetic target provides emulation, not simulation. It is
202 possible to run eCos in suitable architectural simulators but that
203 involves a rather different approach to software development. For
204 example, when running eCos on the psim PowerPC simulator you need
205 appropriate cross-compilation tools that allow you to build PowerPC
206 executables. These are then loaded into the simulator which interprets
207 every instruction and attempts to simulate what would happen if the
208 application were running on real hardware. This involves a lot of
209 processing overhead, but depending on the functionality provided by
210 the simulator it can give very accurate results. When developing for
211 the synthetic target the executable is compiled for the PC's own
212 processor and will be executed at full speed, with no need for a
213 simulator or special tools. This will be much faster and somewhat
214 simpler than using an architectural simulator, but no attempt is made
215 to accurately match the behaviour of a real embedded target.
216     </para>
217   </refsect1>
218 </refentry>
219
220 <!-- }}} -->
221 <!-- {{{ Installation                   -->
222
223 <refentry id="synth-install">
224   <refmeta>
225     <refentrytitle>Installation</refentrytitle>
226   </refmeta>
227   <refnamediv>
228     <refname>Installation</refname>
229     <refpurpose>Preparing to use the synthetic target</refpurpose>
230   </refnamediv>
231
232   <refsect1 id="synth-install-host"><title>Host-side Software</title>
233     <para>
234 To get the full functionality of the synthetic target, users must
235 build and install the I/O auxiliary ecosynth and various support
236 files. It is possible to develop applications for the synthetic target
237 without the auxiliary, but only limited I/O facilities will be
238 available. The relevant code resides in the <filename
239 class="directory">host</filename> subdirectory of the synthetic target
240 architectural HAL package, and building it involves the standard
241 <command>configure</command>, <command>make</command>, and
242 <command>make install</command> steps.
243     </para>
244     <para>
245 There are two main ways of building the host-side software. It is
246 possible to build both the generic host-side software and all
247 package-specific host-side software, including the I/O auxiliary. in a
248 single build tree. This involves using the
249 <command>configure</command> script at the toplevel of the eCos
250 repository, which will automatically search the <filename
251 class="directory">packages</filename> hierarchy for host-side
252 software. For more information on this, see the
253 <filename>README.host</filename> file at the top of the repository.
254 Note that if you have an existing build tree which does not include
255 the synthetic target architectural HAL package then it will be
256 necessary to rerun the toplevel configure script: the search for
257 appropriate packages happens at configure time.
258     </para>
259     <para>
260 The alternative is to build just the host-side for this package.
261 This involves creating a suitable build directory and running the
262 <command>configure</command> script. Note that building directly in
263 the source tree is not allowed.
264     </para>
265     <screen>
266 $ cd &lt;somewhere suitable&gt;
267 $ mkdir synth_build
268 $ cd synth_build
269 $ &lt;repo&lt;&gt;/packages/hal/synth/arch/&lt;version&gt;/host/configure &lt;options&gt;
270 $ make
271 $ make install
272 </screen>
273     <para>
274 The code makes extensive use of Tcl/TK and requires version 8.3 or
275 later. This is checked by the <command>configure</command> script. By
276 default it will use the system's Tcl installation in <filename
277 class="directory">/usr</filename>. If a different, more recent Tcl
278 installation should be used then its location can be specified using
279 the options <option>--with-tcl=&lt;path&gt;</option>,
280 <option>--with-tcl-header=&lt;path&gt;</option> and
281 <option>--with-tcl-lib=&lt;path&gt;</option>. For more information on these options
282 see the <filename>README.host</filename> file at the toplevel of the
283 eCos repository.
284     </para>
285     <para>
286 Some users may also want to specify the install location using a
287 <option>--prefix=&lt;path&gt;</option> option. The default install
288 location is <filename class="directory">/usr/local</filename>. It is
289 essential that the <filename class="directory">bin</filename>
290 subdirectory of the install location is on the user's search
291 <envar>PATH</envar>, otherwise the eCos application will be unable to
292 locate and execute the I/O auxiliary ecosynth.
293     </para>
294     <para>
295 Because ecosynth is run automatically by an eCos application rather
296 than explicitly by the user, it is not installed in the <filename
297 class="directory">bin</filename> subdirectory itself. Instead it is
298 installed below <filename class="directory">libexec</filename>,
299 together with various support files such as images. At configure time
300 it is usually possible to specify an alternative location for
301 <filename class="directory">libexec</filename> using
302 <option>--exec-prefix=&lt;path&gt;</option> or
303 <option>--libexecdir=&lt;path&gt;</option>. These options should not
304 be used for this package because the eCos application is built
305 completely separately and does not know how the host-side was
306 configured. 
307     </para>
308   </refsect1>
309
310   <refsect1 id="synth-tools"><title>Toolchain</title>
311     <para>
312 When developing eCos applications for a normal embedded target it is
313 necessary to use a suitable cross-compiler and related tools such as
314 the linker. Developing for the synthetic target is easier because you
315 can just use the standard GNU tools (gcc, g++, ld, &hellip;) which
316 were provided with your Linux distribution, or which you used to build
317 your own Linux setup. Any reasonably recent version of the tools, for
318 example gcc 2.96(Red Hat) as shipped with Red Hat Linux 7, should be
319 sufficient.
320     </para>
321     <para>
322 There is one important limitation when using these tools: current gdb
323 will not support debugging of eCos threads on the synthetic target. As
324 far as gdb is concerned a synthetic target application is
325 indistinguishable from a normal Linux application, so it assumes that
326 any threads will be created by calls to the Linux
327 <function>pthread_create</function> function provided by the C
328 library. Obviously this is not the case since the application is never
329 linked with that library. Therefore gdb never notices the eCos thread
330 mechanisms and assumes the application is single-threaded. Fixing this
331 is possible but would involve non-trivial changes to gdb.
332     </para>
333     <para>
334 Theoretically it is possible to develop synthetic target applications
335 on, for example, a PC running Windows and then run the resulting
336 executables on another machine that runs Linux. This is rarely useful:
337 if a Linux machine is available then usually that machine will also be
338 used for building ecos and the application. However, if for some
339 reason it is necessary or desirable to build on another machine then
340 this requires a suitable cross-compiler and related tools. If the
341 application will be running on a typical PC with an x86 processor then
342 a suitable configure triplet would be
343 <userinput>i686-pc-linux-gnu</userinput>. The installation
344 instructions for the various GNU tools should be consulted for further
345 information. 
346     </para>
347   </refsect1>
348
349   <refsect1 id="synth-hardware"><title>Hardware Preparation</title>
350     <para>
351 Preparing a real embedded target for eCos development can be tricky.
352 Often the first step is to install suitable firmware, usually RedBoot.
353 This means creating and building a special configuration for eCos with
354 the RedBoot template, then somehow updating the target's flash chips
355 with the resulting RedBoot image. Typically it will also be necessary
356 to get a working serial connection, and possibly set up ethernet as
357 well. Although usually none of the individual steps are particularly
358 complicated, there are plenty of ways in which things can go wrong and
359 it can be hard to figure out what is actually happening. Of course
360 some board manufacturers make life easier for their developers by
361 shipping hardware with RedBoot preinstalled, but even then it is still
362 necessary to set up communication between host and target.
363     </para>
364     <para>
365 None of this is applicable to the synthetic target. Instead you can
366 just build a normal eCos configuration, link your application with the
367 resulting libraries, and you end up with an executable that you can
368 run directly on your Linux machine or via gdb. A useful side effect of
369 this is that application development can start before any real
370 embedded hardware is actually available.
371     </para>
372     <para>
373 Typically the memory map for a synthetic target application will be
374 set up such that there is a read-only ROM region containing all the
375 code and constant data, and a read-write RAM region for the data. The
376 default locations and sizes of these regions depend on the specific
377 platform being used for development. Note that the application always
378 executes out of ROM: on a real embedded target much of the development
379 would involve running RedBoot firmware there, with application code
380 and data loaded into RAM; usually this would change for the final
381 system; the firmware would be replaced by the eCos application itself,
382 configured for ROM bootstrap, and it would perform the appropriate
383 hardware initialization. Therefore the synthetic target actually
384 emulates the behaviour of a final system, not of a development
385 environment. In practice this is rarely significant, although having
386 the code in read-only memory can help catch some problems in
387 application code.
388     </para>
389   </refsect1>
390
391 </refentry>
392
393 <!-- }}} -->
394 <!-- {{{ Running the application        -->
395
396 <refentry id="synth-running">
397   <refmeta>
398     <refentrytitle>Running a Synthetic Target Application</refentrytitle>
399   </refmeta>
400   <refnamediv>
401     <refname>Execution</refname>
402     <refpurpose>Arguments and configuration files</refpurpose>
403   </refnamediv>
404
405   <refsect1 id="synth-running-description"><title>Description</title>
406     <para>
407 The procedure for configuring and building eCos and an application for
408 the synthetic target is the same as for any other eCos target. Once an
409 executable has been built it can be run like any Linux program, for
410 example from a shell prompt,
411     </para>
412     <screen>
413 $ ecos_hello &lt;options&gt;
414 </screen>
415     <para>
416 or using gdb:
417     </para>
418     <screen>
419 $ gdb --nw --quiet --args ecos_hello &lt;options&gt;
420 (gdb) run
421 Starting program: ecos_hello &lt;options&gt;
422 </screen>
423     <para>
424 By default use of the I/O auxiliary is disabled. If its I/O facilities
425 are required then the option <option>--io</option> must be used.
426     </para>
427     <note><para>
428 In future the default behaviour may change, with the I/O auxiliary
429 being started by default. The option <option>--nio</option> can be
430 used to prevent the auxiliary from being run.
431     </para></note>
432   </refsect1>
433
434   <refsect1 id="synth-running-arguments"><title>Command-line Arguments</title>
435     <para>
436 The syntax for running a synthetic target application is:
437     </para>
438     <screen>
439 $ &lt;ecos_app&gt; [options] [-- [app_options]]
440 </screen>
441     <para>
442 Command line options up to the <option>--</option> are passed on to
443 the I/O auxiliary. Subsequent arguments are not passed on to the
444 auxiliary, and hence can be used by the eCos application itself. The
445 full set of arguments can be accessed through the variables
446 <varname>cyg_hal_sys_argc</varname> and
447 <varname>cyg_hal_sys_argv</varname>. 
448     </para>
449     <para>
450 The following options are accepted as standard:
451     </para>
452     <variablelist>
453       <varlistentry>
454         <term><option>--io</option></term>
455          <listitem><para>
456 This option causes the eCos application to spawn the I/O auxiliary
457 during HAL initialization. Without this option only limited I/O will
458 be available.
459          </para></listitem>
460       </varlistentry>
461       <varlistentry>
462         <term><option>--nio</option></term>
463          <listitem><para>
464 This option prevents the eCos application from spawning the I/O
465 auxiliary. In the current version of the software this is the default.
466          </para></listitem>
467       </varlistentry>
468       <varlistentry>
469         <term><option>-nw</option>, <option>--no-windows</option></term>
470          <listitem><para>
471 The I/O auxiliary can either provide a graphical user interface, or it
472 can run in a text-only mode. The default is to provide the graphical
473 interface, but this can be disabled with <option>-nw</option>.
474 Emulation of some devices, for example buttons connected to digital
475 inputs, requires the graphical interface.
476          </para></listitem>
477       </varlistentry>
478       <varlistentry>
479         <term><option>-w</option>, <option>--windows</option></term>
480          <listitem><para>
481 The <option>-w</option> causes the I/O auxiliary to provide a
482 graphical user interface. This is the default.
483          </para></listitem>
484       </varlistentry>
485       <varlistentry>
486         <term><option>-v</option>, <option>--version</option></term>
487          <listitem><para>
488 The <option>-v</option> option can be used to determine the version of
489 the I/O auxiliary being used and where it has been installed. Both the
490 auxiliary and the eCos application will exit immediately.
491          </para></listitem>
492       </varlistentry>
493       <varlistentry>
494         <term><option>-h</option>, <option>--help</option></term>
495          <listitem><para>
496 <option>-h</option> causes the I/O auxiliary to list all accepted
497 command-line arguments. This happens after all devices have been
498 initialized, since the host-side support for some of the devices may
499 extend the list of recognised options. After this both the auxiliary
500 and the eCos application will exit immediately. This option implies
501 <option>-nw</option>. 
502          </para></listitem>
503       </varlistentry>
504       <varlistentry>
505         <term><option>-k</option>, <option>--keep-going</option></term>
506          <listitem><para>
507 If an error occurs in the I/O auxiliary while reading in any of the
508 configuration files or initializing devices, by default both the 
509 auxiliary and the eCos application will exit. The <option>-k</option>
510 option can be used to make the auxiliary continue in spite of errors,
511 although obviously it may not be fully functional.
512          </para></listitem>
513       </varlistentry>
514       <varlistentry>
515         <term><option>-nr</option>, <option>--no-rc</option></term>
516          <listitem><para>
517 Normally the auxiliary processes two <link
518 linkend="synth-running-user-config">user configuration files</link>
519 during startup: <filename>initrc.tcl</filename> and
520 <filename>mainrc.tcl</filename>. This can be suppressed using the
521 <option>-nr</option> option.
522          </para></listitem>
523       </varlistentry>
524       <varlistentry>
525         <term><option>-x</option>, <option>--exit</option></term>
526          <listitem><para>
527 When providing a graphical user interface the I/O auxiliary will
528 normally continue running even after the eCos application has exited.
529 This allows the user to take actions such as saving the current
530 contents of the main text window. If run with <option>-x</option> then
531 the auxiliary will exit as soon the application exits.
532          </para></listitem>
533       </varlistentry>
534       <varlistentry>
535         <term><option>-nx</option>, <option>--no-exit</option></term>
536          <listitem><para>
537 When the graphical user interface is disabled with
538 <option>-nw</option> the I/O auxiliary will normally exit immediately
539 when the eCos application exits. Without the graphical frontend there
540 is usually no way for the user to interact directly with the
541 auxiliary, so there is no point in continuing to run once the eCos
542 application will no longer request any I/O operations. Specifying the
543 <option>-nx</option> option causes the auxiliary to continue running
544 even after the application has exited.
545          </para></listitem>
546       </varlistentry>
547       <varlistentry>
548         <term><option>-V</option>, <option>--verbose</option></term>
549          <listitem><para>
550 This option causes the I/O auxiliary to output some additional
551 information, especially during initialization.
552          </para></listitem>
553       </varlistentry>
554       <varlistentry>
555         <term><option>-l &lt;file&gt;</option>, <option>--logfile &lt;file&gt;</option></term>
556          <listitem><para>
557 Much of the output of the eCos application and the I/O auxiliary is
558 simple text, for example resulting from eCos
559 <function>printf</function> or <function>diag_printf</function> calls.
560 When running in graphical mode this output goes to a central text
561 window, and can be saved to a file or edited via menus. The
562 <option>-l</option> can be used to automatically generate an
563 additional logfile containing all the text. If graphical
564 mode is disabled then by default all the text just goes to the current
565 standard output. Specifying <option>-l</option> causes most of the
566 text to go into a logfile instead, although some messages such as
567 errors generated by the auxiliary itself will still go to stdout as
568 well. 
569          </para></listitem>
570       </varlistentry>
571       <varlistentry>
572         <term><option>-t &lt;file&gt;</option>, <option>--target &lt;file&gt;</option></term>
573          <listitem><para>
574 During initialization the I/O auxiliary reads in a target definition
575 file. This file holds information such as which Linux devices should
576 be used to emulate the various eCos devices. The <option>-t</option>
577 option can be used to specify which target definition should be used
578 for the current run, defaulting to <filename>default.tdf</filename>.
579 It is not necessary to include the <filename>.tdf</filename> suffix,
580 this will be appended automatically if necessary.
581          </para></listitem>
582       </varlistentry>
583       <varlistentry>
584         <term><option>-geometry &lt;geometry&gt;</option></term>
585          <listitem><para>
586 This option can be used to control the size and position of the main
587 window, as per X conventions.
588          </para></listitem>
589       </varlistentry>
590     </variablelist>
591     <para>
592 The I/O auxiliary loads support for the various devices dynamically
593 and some devices may accept additional command line arguments. Details
594 of these can be obtained using the <option>-h</option> option or by
595 consulting the device-specific documentation. If an unrecognised
596 command line argument is used then a warning will be issued.
597     </para>
598   </refsect1>
599
600   <refsect1 id="synth-running-tdf"><title>The Target Definition File</title>
601     <para>
602 The eCos application will want to access devices such as
603 <varname>eth0</varname> or <varname>/dev/ser0</varname>. These need to
604 be mapped on to Linux devices. For example some users may all traffic
605 on the eCos <varname>/dev/ser0</varname> serial device to go via the
606 Linux serial device <varname>/dev/ttyS1</varname>, while ethernet I/O
607 for the eCos <varname>eth0</varname> device should be mapped to the
608 Linux ethertap device <varname>tap3</varname>. Some devices may need
609 additional configuration information, for example to limit the
610 number of packets that should be buffered within the I/O auxiliary.
611 The target definition file provides all this information.
612     </para>
613     <para>
614 By default the I/O auxiliary will look for a file
615 <filename>default.tdf</filename>. An alternative target definition can
616 be specified on the command line using <option>-t</option>, for
617 example:
618     </para>
619     <screen>
620 $ bridge_app --io -t twineth
621 </screen>
622     <para>
623 A <filename>.tdf</filename> suffix will be appended automatically if
624 necessary. If a relative pathname is used then the I/O auxiliary will
625 search for the target definition file in the current directory, then
626 in <filename class="directory">~/.ecos/synth/</filename>, and finally
627 in its install location.
628     </para>
629     <para>
630 A typical target definition file might look like this:
631     </para>
632     <programlisting>
633 synth_device console {
634     # appearance -foreground white -background black
635     filter trace {^TRACE:.*} -foreground HotPink1 -hide 1
636 }
637
638 synth_device ethernet {
639     eth0 real eth1
640     eth1 ethertap tap4 00:01:02:03:FE:06
641
642     ## Maximum number of packets that should be buffered per interface.
643     ## Default 16
644     #max_buffer 32
645
646     ## Filters for the various recognised protocols.
647     ## By default all filters are visible and use standard colours.
648     filter ether  -hide 0
649     #filter arp    -hide 1
650     #filter ipv4   -hide 1
651     #filter ipv6   -hide 1
652 }
653 </programlisting>
654     <para>
655 A target definition file is actually a Tcl script that gets run in the
656 main interpreter of the I/O auxiliary during initialization. This
657 provides a lot of flexibility if necessary. For example the script
658 could open a socket to a resource management server of some sort to
659 determine which hardware facilities are already in use and adapt
660 accordingly. Another possibility is to adapt based on <link
661 linkend="synth-new-host-args">command line arguments</link>. Users who
662 are not familiar with Tcl programming should still be able to edit a
663 simple target definition file without too much difficulty, using a
664 mixture of cut'n'paste, commenting or uncommenting various lines, and
665 making small edits such as changing <literal>tap4</literal> to
666 <literal>eth2</literal>. 
667     </para>
668     <para>
669 Each type of device will have its own entry in the target definition
670 file, taking the form:
671     </para>
672     <programlisting>
673 synth_device &lt;device type&gt; {
674     &lt;options&gt;
675 }
676 </programlisting>
677     <para>
678 The documentaton for each synthetic target device should provide
679 details of the options available for that device, and often a suitable
680 fragment that can be pasted into a target definition file and edited.
681 There is no specific set of options that a given device will always
682 provide. However in practice many devices will use common code
683 exported by the main I/O auxiliary, or their implementation will
684 involve some re-use of code for an existing device. Hence certain
685 types of option are common to many devices.
686     </para>
687     <para>
688 A good example of this is filters, which control the appearance of
689 text output. The above target definition file defines a filter
690 <varname>trace</varname> for output from the eCos application. The
691 regular expression will match output from the infrastructure package's
692 tracing facilities when <varname>CYGDBG_USE_TRACING</varname> and
693 <varname>CYGDBG_INFRA_DEBUG_TRACE_ASSERT_SIMPLE</varname> are enabled.
694 With the current settings this output will not be visible by default,
695 but can be made visible using the menu item <guimenuitem>System
696 Filters</guimenuitem>. If made visible the trace output will appear in
697 an unusual colour, so users can easily distinguish the trace output
698 from other text. All filters accept the following options:
699     </para>
700     <variablelist>
701       <varlistentry>
702          <term><option>-hide [0|1]</option></term>
703          <listitem><para>
704 This controls whether or not text matching this filter should be
705 invisible by default or not. At run-time the visibility of each filter
706 can be controlled using the <guimenuitem>System Filters</guimenuitem>
707 menu item.
708          </para></listitem>
709       </varlistentry>
710       <varlistentry>
711          <term><option>-foreground &lt;colour&gt;</option></term>
712          <listitem><para>
713 This specifies the foreground colour for all text matching this
714 filter. The colour can be specified using an RGB value such as
715 <literal>#F08010</literal>, or a symbolic name such as
716 <literal>"light steel blue"</literal>. The X11 utility
717 <application>showrgb</application> can be used to find out
718 about the available colours.
719          </para></listitem>
720       </varlistentry>
721       <varlistentry>
722          <term><option>-background &lt;colour&gt;</option></term>
723          <listitem><para>
724 This specifies the background colour for all text matching the filter.
725 As with <option>-foreground</option> the colour can be specified using
726 a symbolic name or an RGB value.
727          </para></listitem>
728       </varlistentry>
729     </variablelist>
730     <para>
731 Some devices may create their own subwindows, for example to monitor
732 ethernet traffic or to provide additional I/O facilities such as
733 emulated LED's or buttons. Usually the target definition file can be
734 used to control the <link linkend="synth-gui-layout">layout</link> of
735 these windows.
736     </para>
737     <para>
738 The I/O auxiliary will not normally warn about
739 <command>synth_device</command> entries in the target definition file
740 for devices that are not actually needed by the current eCos
741 application. This makes it easier to use a single file for several
742 different applications. However it can lead to confusion if an entry
743 is spelled incorrectly and hence does not actually get used. The
744 <option>-V</option> command line option can be used to get warnings
745 about unused device entries in the target definition file.
746     </para>
747     <para>
748 If the body of a <command>synth_device</command> command contains an
749 unrecognised option and the relevant device is in use, the I/O
750 auxiliary will always issue a warning about such options.
751     </para>
752   </refsect1>
753
754   <refsect1 id="synth-running-user-config"><title>User Configuration Files</title>
755     <para>
756 During initialization the I/O auxiliary will execute two user
757 configuration files, <filename>initrc.tcl</filename> and
758 <filename>mainrc.tcl</filename>. It will look for these files in the
759 directory <filename class="directory">~/.ecos/synth/</filename>. If
760 that directory does not yet exist it will be created and populated
761 with initial dummy files.
762     </para>
763     <para>
764 Both of these configuration files are Tcl scripts and will be run in
765 the main interpreter used by the I/O auxiliary itself. This means that
766 they have full access to the internals of the auxiliary including the
767 various Tk widgets, and they can perform file or socket I/O if
768 desired. The section <xref linkend="synth-new-host"> contains
769 information about the facilities available on the host-side for
770 writing new device drivers, and these can also be used in the
771 initialization scripts.
772     </para>
773     <para>
774 The <filename>initrc.tcl</filename> script is run before the auxiliary
775 has processed any requests from the eCos application, and hence before
776 any devices have been instantiated. At this point the generic
777 command-line arguments has been processed, the target definition file
778 has been read in, and the hooks functionality has been initialized. If
779 running in graphical mode the main window will have been created, but
780 has been withdrawn from the screen to allow new widgets to be added
781 without annoying screen flicker. A typical
782 <filename>initrc.tcl</filename> script could add some menu or toolbar
783 options, or install a hook function that will be run when the
784 eCos application exits.
785     </para>
786     <para>
787 The <filename>mainrc.tcl</filename> script is run after eCos has
788 performed all its device initialization and after C++ static
789 constructors have run, and just before the call to
790 <function>cyg_start</function> which will end up transferring control
791 to the application itself. A typical <filename>mainrc.tcl</filename>
792 script could look at what interrupt vectors have been allocated to
793 which devices and create a little monitor window that shows interrupt
794 activity. 
795     </para>
796   </refsect1>
797
798   <refsect1 id="synth-running-session"><title>Session Information</title>
799     <para>
800 When running in graphical mode, the I/O auxiliary will read in a file
801 <filename>~/.ecos/synth/guisession</filename> containing session
802 information. This file should not normally be edited manually, instead
803 it gets updated automatically when the auxiliary exits. The purpose of
804 this file is to hold configuration options that are manipulated via
805 the graphical interface, for example which browser should be used to
806 display online help.
807     </para>
808     <warning><para>
809 GUI session functionality is not yet available in the current release.
810 When that functionality is fully implemented it is possible that some
811 target definition file options may be removed, to be replaced by
812 graphical editing via a suitable preferences dialog, with the
813 current settings saved in the session file.
814     </para></warning>
815   </refsect1>
816
817 </refentry>
818
819 <!-- }}} -->
820 <!-- {{{ ecosynth user interface        -->
821
822 <refentry id="synth-gui">
823   <refmeta>
824     <refentrytitle>The I/O Auxiliary's User Interface</refentrytitle>
825   </refmeta>
826   <refnamediv>
827     <refname>User Interface</refname>
828     <refpurpose>Controlling the I/O Auxiliary</refpurpose>
829   </refnamediv>
830
831   <refsect1 id="synth-gui-description"><title>Description</title>
832     <para>
833 The synthetic target auxiliary is designed to support both extensions
834 and user customization. Support for the desired devices is dynamically
835 loaded, and each device can extend the user interface. For example it
836 is possible for a device to add menu options, place new buttons on the
837 toolbar, create its own sub-window within the overall layout, or even
838 create entire new toplevel windows. These subwindows or toplevels
839 could show graphs of activity such as interrupts or packets being
840 transferred. They could also allow users to interact with the eCos
841 application, for example by showing a number of buttons which will be
842 mapped on to digital inputs in the eCos application. Different
843 applications will have their own I/O requirements, changing the
844 host-side support files that get loaded and that may modify the user
845 interface. The I/O auxiliary also reads in user configuration scripts
846 which can enhance the interface in the same way. Therefore the exact
847 user interface will depend on the user and on the eCos application
848 being run. However the overall layout is likely to remain the same.
849     </para>
850     <informalfigure PgWide=1>
851       <mediaobject>
852         <imageobject>
853           <imagedata fileref="screen_main.png" Scalefit=1 Align="Center">
854         </imageobject>
855       </mediaobject>
856     </informalfigure>
857     <para>
858 The title bar identifies the window as belonging to an eCos synthetic
859 target application and lists both the application name and its process
860 id. The latter is especially useful if the application was started
861 directly from a shell prompt and the user now wants to attach a gdb
862 session. The window has a conventional menu bar with the usual
863 entries, plus a toolbar with buttons for common operations such as cut
864 and paste. Balloon help is supported.
865     </para>
866     <para>
867 There is a central <link linkend="synth-gui-text">text window</link>,
868 possibly surrounded by various sub-windows for various devices. For
869 example there could be a row of emulated LED's above the text window,
870 and monitors of ethernet traffic and interrupt activity on the right.
871 At the bottom of the window is a status line, including a small
872 animation that shows whether or not the eCos application is still
873 running.
874     </para>
875   </refsect1>
876
877   <refsect1 id="synth-gui-menus"><title>Menus and the Toolbar</title>
878     <para>
879 Usually there will be four menus on the menu bar:
880 <guimenu>File</guimenu>, <guimenu>Edit</guimenu>,
881 <guimenu>View</guimenu> and <guimenu>Help</guimenu>.
882     </para>
883       <informalfigure PgWide=1>
884       <mediaobject>
885         <imageobject>
886           <imagedata fileref="menu_file.png" Scalefit=1 Align="Center">
887         </imageobject>
888       </mediaobject>
889     </informalfigure>
890     <para>
891 On the <guimenu>File</guimenu> menu there are three entries related to
892 saving the current contents of the central text window.
893 <guimenuitem>Save</guimenuitem> is used to save the currently visible
894 contents of the text window. Any text that is hidden because of
895 filters will not be written to the savefile. If there has been a
896 previous <guimenuitem>Save</guimenuitem> or <guimenuitem>Save
897 As</guimenuitem> operation then the existing savefile will be re-used,
898 otherwise the user will be asked to select a suitable file.
899 <guimenuitem>Save As</guimenuitem> also saves just the currently
900 visible contents but will always prompt the user for a filename.
901 <guimenuitem>Save All</guimenuitem> can be used to save the full
902 contents of the text window, including any text that is currently
903 hidden. It will always prompt for a new filename, to avoid confusion
904 with partial savefiles.
905     </para>
906     <para>
907 Usually the eCos application will be run from inside gdb or from a
908 shell prompt. Killing off the application while it is being debugged
909 in a gdb session is not a good idea, it would be better to use gdb's
910 own <command>kill</command> command. Alternatively the eCos
911 application itself can use the <function>CYG_TEST_EXIT</function> or
912 <filename>cyg_hal_sys_exit</filename> functionality. However it is
913 possible to terminate the application from the I/O auxiliary using
914 <guimenuitem>Kill eCos</guimenuitem>. A clean shutdown will be
915 attempted, but that can fail if the application is currently halted
916 inside gdb or if it has crashed completely. As a last resort
917 <constant>SIGKILL</constant> will be used.
918     </para>
919     <para>
920 When operating in graphical mode the I/O auxiliary will normally
921 continue to run even after the eCos application has exited. This
922 allows the user to examine the last few lines of output, and perhaps
923 perform actions such as saving the output to a file. The
924 <guimenuitem>Exit</guimenuitem> menu item can be used to shut down the
925 auxiliary. Note that this behaviour can be changed with command line
926 arguments <link
927 linkend="synth-running-arguments"><option>--exit</option></link> and
928 <link
929 linkend="synth-running-arguments"><option>--no-exit</option></link>.
930     </para>
931     <para>
932 If <guimenuitem>Exit</guimenuitem> is used while the eCos application
933 is still running then the I/O auxiliary will first attempt to
934 terminate the application cleanly, and then exit.
935     </para>
936     <informalfigure PgWide=1>
937       <mediaobject>
938         <imageobject>
939           <imagedata fileref="menu_edit.png" Scalefit=1 Align="Center">
940         </imageobject>
941       </mediaobject>
942     </informalfigure>
943     <para>
944 The <guimenu>Edit</guimenu> menu contains the usual entries for
945 text manipulation: <guimenuitem>Cut</guimenuitem>,
946 <guimenuitem>Copy</guimenuitem>, <guimenuitem>Paste</guimenuitem>,
947 <guimenuitem>Clear</guimenuitem> and <guimenuitem>Select
948 All</guimenuitem>. These all operate on the central text window. By
949 default this window cannot be edited so the cut, paste and clear
950 operations are disabled. If the user wants to edit the contents of the
951 text window then the <guimenuitem>Read Only</guimenuitem> checkbutton
952 should be toggled.
953     </para>
954     <para>
955 The <guimenuitem>Preferences</guimenuitem> menu item brings up a
956 miscellaneous preferences dialog. One of the preferences relates to
957 online help: the I/O auxiliary does not currently have a built-in html
958 viewer; instead it will execute an external browser of some sort. With
959 the example settings shown, the I/O auxiliary will first attempt to
960 interact with an existing mozilla session. If that fails it will try
961 to run a new mozilla instance, or as a last result use the Gnome help
962 viewer. 
963     </para>
964     <informalfigure PgWide=1>
965       <mediaobject>
966         <imageobject>
967           <imagedata fileref="preferences.png" Scalefit=1 Align="Center">
968         </imageobject>
969       </mediaobject>
970     </informalfigure>
971     <para>
972 The <guimenu>View</guimenu> menu contains the <guimenuitem>System
973 Filters</guimenuitem> entry, used to edit the settings for the current
974 <link linkend="synth-gui-text">filters</link>.
975     </para>
976      <informalfigure PgWide=1>
977       <mediaobject>
978         <imageobject>
979           <imagedata fileref="menu_view.png" Scalefit=1 Align="Center">
980         </imageobject>
981       </mediaobject>
982     </informalfigure>
983     <para>
984 The <guimenu>Help</guimenu> menu can be used to activate online help
985 for eCos generally, for the synthetic target as a whole, and for
986 specific devices supported by the generic target. The Preferences
987 dialog can be used to select the browser that will be used.
988     </para>
989     <informalfigure PgWide=1>
990       <mediaobject>
991         <imageobject>
992           <imagedata fileref="menu_help.png" Scalefit=1 Align="Center">
993         </imageobject>
994       </mediaobject>
995     </informalfigure>
996     <note><para>
997 At the time of writing there is no well-defined toplevel index file
998 for all eCos documentation. Hence the relevant menu item is disabled.
999 Documentation for the synthetic target and the supported devices
1000 is stored as part of the package itself so can usually be found fairly
1001 easily. It may be necessary to set the <envar>ECOS_REPOSITORY</envar>
1002 environment variable.
1003     </para></note>
1004
1005   </refsect1>
1006
1007   <refsect1 id="synth-gui-text"><title>The Main Text Window</title>
1008     <para>
1009 The central text window holds the console output from the eCos
1010 application: the screen shot above shows DHCP initialization data from
1011 the TCP/IP stack, and some output from the <function>main</function>
1012 thread at the bottom. Some devices can insert text of their own, for
1013 example the ethernet device support can be configured to show details
1014 of incoming and outgoing packets. Mixing the output from the eCos
1015 application and the various devices can make it easier to understand
1016 the order in which events occur.
1017     </para>
1018     <para>
1019 The appearance of text from different sources can be controlled by
1020 means of filters, and it is also possible to hide some of the text.
1021 For example, if tracing is enabled in the eCos configuration then the
1022 trace output can be given its own colour scheme, making it stand out
1023 from the rest of the output. In addition the trace output is generally
1024 voluminous so it can be hidden by default, made visible only to find
1025 out more about what was happening when a particular problem occurred.
1026 Similarly the ethernet device support can output details of the
1027 various packets being transferred, and using a different background
1028 colour for this output again makes it easier to distinguish from
1029 console output.
1030     </para>
1031     <para>
1032 The default appearance for most filters is controlled via the
1033 <link linkend="synth-running-tdf">target definition file</link>. An
1034 example entry might be:
1035     </para>
1036     <programlisting>
1037   filter trace {^TRACE:.*} -foreground HotPink1 -hide 1
1038 </programlisting>
1039     <para>
1040 The various colours and the hide flag for each filter can be changed
1041 at run-time, using the <guimenuitem>System Filters</guimenuitem> item
1042 on the <guimenu>View</guimenu> menu. This will bring up a dialog like
1043 the following:
1044     </para>
1045     <informalfigure PgWide=1>
1046       <mediaobject>
1047         <imageobject>
1048           <imagedata fileref="filters.png" Scalefit=1 Align="Center">
1049         </imageobject>
1050       </mediaobject>
1051     </informalfigure>
1052     <para>
1053 It should be noted that the text window is line-oriented, not
1054 character-oriented. If an eCos application sends a partial line of
1055 text then that will remain buffered until a newline character is
1056 received, rather than being displayed immediately. This avoids
1057 confusion when there is concurrent output from several sources.
1058     </para>
1059     <para>
1060 By default the text window is read-only. This means it will not allow
1061 cut, paste and clear operations, and keyboard input will be ignored.
1062 The <guimenu>Edit</guimenu> menu has a checkbutton <guimenuitem>Read
1063 Only</guimenuitem> which can be toggled to allow write operations. For
1064 example, a user could type in a reminder of what was happening at this
1065 time, or paste in part of a gdb session. Such keyboard input does not
1066 get forwarded to the eCos application: if the latter requires keyboard
1067 input then that should happen via a separate keyboard device.
1068     </para>
1069   </refsect1>
1070
1071   <refsect1 id="synth-gui-layout"><title>Positioning Optional Windows</title>
1072     <para>
1073 Some devices may create their own subwindows, for example to monitor
1074 ethernet traffic or to provide additional I/O facilities such as
1075 emulated LED's or buttons. Usually the target definition file can be
1076 used to control the <link linkend="synth-gui-layout">layout</link> of
1077 these windows. This requires an understanding of the overall layout of
1078 the display.
1079     </para>
1080     <informalfigure PgWide=1>
1081       <mediaobject>
1082         <imageobject>
1083           <imagedata fileref="layout.png" Scalefit=1 Align="Center">
1084         </imageobject>
1085       </mediaobject>
1086     </informalfigure>
1087     <para>
1088 Subwindows are generally packed in one of eight frames surrounding the
1089 central text window: <varname>.main.nw</varname>,
1090 <varname>.main.n</varname>, <varname>.main.ne</varname>,
1091 <varname>.main.w</varname>, <varname>.main.e</varname>,
1092 <varname>.main.sw</varname>, <varname>.main.s</varname>, and
1093 <varname>.main.se</varname>. To position a row of LED's above the text
1094 window and towards the left, a target definition file could contain an
1095 entry such as:
1096     </para>
1097     <programlisting>
1098 synth_device led {
1099     pack -in .main.n -side left
1100     &hellip;
1101 }
1102 </programlisting>
1103     <para>
1104 Similarly, to put a traffic monitor window on the right of the text
1105 window would involve something like:
1106     </para>
1107     <programlisting>
1108     &hellip;
1109     monitor_pack -in .main.e -side bottom
1110     &hellip;
1111 </programlisting>
1112     <para>
1113 Often it will be sufficient to specify a container frame and one of
1114 <constant>left</constant>, <constant>right</constant>,
1115 <constant>top</constant> or <constant>bottom</constant>. Full control
1116 over the positioning requires an understanding of Tcl/Tk and in
1117 particular the packing algorithm, and an appropriate reference work
1118 should be consulted.
1119     </para>
1120   </refsect1>
1121
1122   <refsect1 id="synth-gui-global-config"><title>Global Settings</title>
1123     <note><para>
1124 This section still to be written - it should document the interaction
1125 between X resources and ecosynth, and how users can control settings
1126 such as the main foreground and background colours.
1127     </para></note>
1128   </refsect1>
1129
1130 </refentry>
1131
1132 <!-- }}} -->
1133 <!-- {{{ The console device             -->
1134
1135 <refentry id="synth-console">
1136   <refmeta>
1137     <refentrytitle>The Console Device</refentrytitle>
1138   </refmeta>
1139   <refnamediv>
1140     <refname>The console device </refname>
1141     <refpurpose>Show output from the eCos application</refpurpose>
1142   </refnamediv>
1143
1144   <refsect1 id="synth-console-description"><title>Description</title>
1145     <para>
1146 The eCos application can generate text output in a variety of ways,
1147 including calling <function>printf</function> or
1148 <function>diag_printf</function>. When the I/O auxiliary is enabled
1149 the eCos startup code will instantiate a console device to process all
1150 such output. If operating in text mode the output will simply go to
1151 standard output, or to a logfile if the <option>-l</option> command
1152 line option is specified. If operating in graphical mode the output
1153 will go to the central text window, and optionally to a logfile as
1154 well. In addition it is possible to control the appearance of the main
1155 text via the target definition file, and to install extra filters for
1156 certain types of text.
1157     </para>
1158     <para>
1159 It should be noted that the console device is line-oriented, not
1160 character-oriented. This means that outputting partial lines is not
1161 supported, and some functions such as <function>fflush</function> and
1162 <function>setvbuf</function> will not operate as expected. This
1163 limitation prevents much possible confusion when using filters to
1164 control the appearance of the text window, and has some performance
1165 benefits - especially when the eCos application generates a great deal
1166 of output such as when tracing is enabled. For most applications this
1167 is not a problem, but it is something that developers should be aware
1168 of. 
1169     </para>
1170     <para>
1171 The console device is output-only, it does not provide any support for
1172 keyboard input. If the application requires keyboard input then that
1173 should be handled by a separate eCos device package and matching
1174 host-side code.
1175     </para>
1176   </refsect1>
1177
1178   <refsect1 id="synth-console-install"><title>Installation</title>
1179     <para>
1180 The eCos side of the console device is implemented by the
1181 architectural HAL itself, in the source file
1182 <filename>synth_diag.c</filename>, rather than in a separate device
1183 package. Similarly the host-side implementation,
1184 <function>console.tcl</function>, is part of the architectural HAL's
1185 host-side support. It gets installed automatically alongside the I/O
1186 auxiliary itself, so no separate installation procedure is required.
1187     </para>
1188   </refsect1>
1189
1190   <refsect1 id="synth-console-tdf"><title>Target Definition File</title>
1191     <para>
1192 The <link linkend="synth-running-tdf">target definition file</link>
1193 can contain a number of entries related to the console device. These
1194 are all optional, they only control the appearance of text output. If
1195 such control is desired then the relevant options should appear in the
1196 body of a <command>synth_device</command> entry:
1197     </para>
1198     <programlisting>
1199 synth_device console {
1200     &hellip;
1201 }
1202 </programlisting>
1203     <para>
1204 The first option is <command>appearance</command>, used to control the
1205 appearance of any text generated by the eCos application that does not
1206 match one of the installed filters. This option takes the same
1207 argument as any other filter, for example:
1208     </para>
1209     <programlisting>
1210 synth_device console {
1211     appearance -foreground white -background black
1212     &hellip;
1213 }
1214 </programlisting>
1215     <para>
1216 Any number of additional filters can be created with a
1217 <command>filter</command> option, for example:
1218     </para>
1219     <programlisting>
1220 synth_device console {
1221     &hellip;
1222     filter trace {^TRACE:.*} -foreground HotPink1 -hide 1
1223     &hellip;
1224 }
1225 </programlisting>
1226     <para>
1227 The first argument gives the new filter a name which will be used in
1228 the <link linkend="synth-gui-text">filters dialog</link>. Filter names
1229 should be unique. The second argument is a Tcl regular expression. The
1230 console support will match each line of eCos output against this
1231 regular expression, and if a match is found then the filter will be
1232 used for this line of text. The above example matches any line of
1233 output that begins with <literal>TRACE:</literal>, which corresponds
1234 to the eCos infrastructure's tracing facilities. The remaining options
1235 control the desired appearance for matched text. If some eCos output
1236 matches the regular expressions for several different filters then
1237 only the first match will be used.
1238     </para>
1239   </refsect1>
1240
1241   <refsect1 id="synth-console-target-config"><title>Target-side
1242   Configuration Options</title>
1243     <para>
1244 There are no target-side configuration options related to the console
1245 device. 
1246     </para>
1247   </refsect1>
1248
1249   <refsect1 id="synth-console-arguments"><title>Command Line Arguments</title>
1250     <para>
1251 The console device does not use any command-line arguments.
1252     </para>
1253   </refsect1>
1254
1255   <refsect1 id="synth-console-hooks"><title>Hooks</title>
1256     <para>
1257 The console device does not provide any hooks.
1258     </para>
1259   </refsect1>
1260
1261   <refsect1><title>Additional Tcl Procedures</title>
1262     <para>
1263 The console device does not provide any additional Tcl procedures that
1264 can be used by other scripts.
1265     </para>
1266   </refsect1>
1267
1268 </refentry>
1269
1270 <!-- }}} -->
1271 <!-- {{{ System calls                   -->
1272
1273 <refentry id="synth-syscalls">
1274   <refmeta>
1275     <refentrytitle>System Calls</refentrytitle>
1276   </refmeta>
1277
1278   <refnamediv>
1279     <refname>cyg_hal_sys_xyz</refname>
1280     <refpurpose>Access Linux system facilities</refpurpose>
1281   </refnamediv>
1282
1283   <refsynopsisdiv>
1284     <funcsynopsis>
1285       <funcsynopsisinfo>
1286 #include &lt;cyg/hal/hal_io.h&gt;
1287       </funcsynopsisinfo>
1288       <funcprototype>
1289         <funcdef>int <function>cyg_hal_sys_xyzzy</function></funcdef>
1290         <varargs>
1291       </funcprototype>
1292     </funcsynopsis>
1293   </refsynopsisdiv>
1294
1295   <refsect1 id="synth-syscalls-description"><title>Description</title>
1296     <para>
1297 On a real embedded target eCos interacts with the hardware by peeking
1298 and poking various registers, manipulating special regions of memory,
1299 and so on. The synthetic target does not access hardware directly.
1300 Instead I/O and other operations are emulated by making appropriate
1301 Linux system calls. The HAL package exports a number of functions
1302 which allow other packages, or even application code, to make these
1303 same system calls. However this facility must be used with care: any
1304 code which calls, for example, <function>cyg_hal_sys_write</function>
1305 will only ever run on the synthetic target; that functionality is
1306 obviously not provided on any real hardware because there is no
1307 underlying Linux kernel to implement it.
1308     </para>
1309     <para>
1310 The synthetic target only provides a subset of the available system
1311 calls, specifically those calls which have proved useful to implement
1312 I/O emulation. This subset can be extended fairly easily if necessary.
1313 All of the available calls, plus associated data structures and
1314 macros, are defined in the header file <filename
1315 class="headerfile">cyg/hal/hal_io.h</filename>. There is a simple
1316 convention: given a Linux system call such as
1317 <function>open</function>, the synthetic target will prefix
1318 <literal>cyg_hal_sys</literal> and provide a function with that name.
1319 The second argument to the <function>open</function> system call is
1320 a set of flags such as <constant>O_RDONLY</constant>, and the header
1321 file will define a matching constant
1322 <constant>CYG_HAL_SYS_O_RDONLY</constant>. There are also data
1323 structures such as <structname>cyg_hal_sys_sigset_t</structname>,
1324 matching the Linux data structure <structname>sigset_t</structname>.
1325     </para>
1326     <para>
1327 In most cases the functions provided by the synthetic target behave as
1328 per the documentation for the Linux system calls, and section 2 of the
1329 Linux man pages can be consulted for more information. There is one
1330 important difference: typically the documentation will say that a
1331 function returns <literal>-1</literal> to indicate an error, with the
1332 actual error code held in <varname>errno</varname>; the actual
1333 underlying system call and hence the
1334 <function>cyg_hal_sys_xyz</function> provided by eCos instead returns
1335 a negative number to indicate an error, with the absolute value of
1336 that number corresponding to the error code; usually it is the C
1337 library which handles this and manipulates errno, but of course
1338 synthetic target applications are not linked with that Linux library.
1339     </para>
1340     <para>
1341 However, there are some exceptions. The Linux kernel has evolved over
1342 the years, and some of the original system call interfaces are no
1343 longer appropriate. For example the original
1344 <function>select</function> system call has been superseded by
1345 <function>_newselect</function>, and that is what the
1346 <function>select</function> function in the C library actually uses.
1347 The old call is still available to preserve binary compatibility but,
1348 like the C library, eCos makes use of the new one because it provides
1349 the appropriate functionality. In an attempt to reduce confusion the
1350 eCos function is called <function>cyg_hal_sys__newselect</function>,
1351 in other words it matches the official system call naming scheme. The
1352 authoritive source of information on such matters is the Linux kernel
1353 sources themselves, and especially its header files.
1354     </para>
1355     <para>
1356 eCos packages and applications should never
1357 <literal>#include</literal> Linux header files directly. For example,
1358 doing a <literal>#include&nbsp;&lt;/usr/include/fcntl.h&gt;</literal>
1359 to access additional macros or structure definitions, or alternatively
1360 manipulating the header file search path, will lead to problems
1361 because the Linux header files are likely to duplicate and clash with
1362 definitions in the eCos headers. Instead the appropriate functionality
1363 should be extracted from the Linux headers and moved into either
1364 <filename class="headerfile">cyg/hal/hal_io.h</filename> or into
1365 application code, with suitable renaming to avoid clashes with eCos
1366 names. Users should be aware that large-scale copying may involve
1367 licensing complications.
1368     </para>
1369     <para>
1370 Adding more system calls is usually straightforward and involves
1371 adding one or more lines to the platform-specific file in the
1372 appropriate platform HAL, for example
1373 <filename>syscall-i386-linux-1.0.S</filename>. However it is necessary
1374 to do some research first about the exact interface implemented by the
1375 system call, because of issues such as old system calls that have been
1376 superseded. The required information can usually be found fairly
1377 easily by searching through the Linux kernel sources and possibly the
1378 GNU C library sources.
1379     </para>
1380   </refsect1>
1381 </refentry>
1382
1383 <!-- }}} -->
1384 <!-- {{{ New devices - target-side      -->
1385
1386 <refentry id="synth-new-target">
1387   <refmeta>
1388     <refentrytitle>Writing New Devices - target</refentrytitle>
1389   </refmeta>
1390   <refnamediv>
1391     <refname>Writing New Devices</refname>
1392     <refpurpose>extending the synthetic target, target-side</refpurpose>
1393   </refnamediv>
1394
1395   <refsynopsisdiv>
1396     <funcsynopsis>
1397       <funcsynopsisinfo>
1398 #include &lt;cyg/hal/hal_io.h&gt;
1399       </funcsynopsisinfo>
1400
1401       <funcprototype>
1402         <funcdef>int <function>synth_auxiliary_instantiate</function></funcdef>
1403         <paramdef>const char* <parameter>package</parameter></paramdef>
1404         <paramdef>const char* <parameter>version</parameter></paramdef>
1405         <paramdef>const char* <parameter>device</parameter></paramdef>
1406         <paramdef>const char* <parameter>instance</parameter></paramdef>
1407         <paramdef>const char* <parameter>data</parameter></paramdef>
1408       </funcprototype>
1409       <funcprototype>
1410         <funcdef>void <function>synth_auxiliary_xchgmsg</function></funcdef>
1411         <paramdef>int <parameter>device_id</parameter></paramdef>
1412         <paramdef>int <parameter>request</parameter></paramdef>
1413         <paramdef>int <parameter>arg1</parameter></paramdef>
1414         <paramdef>int <parameter>arg2</parameter></paramdef>
1415         <paramdef>const unsigned char* <parameter>txdata</parameter></paramdef>
1416         <paramdef>int <parameter>txlen</parameter></paramdef>
1417         <paramdef>int* <parameter>reply</parameter></paramdef>
1418         <paramdef>unsigned char* <parameter>rxdata</parameter></paramdef>
1419         <paramdef>int* <parameter>rxlen</parameter></paramdef>
1420         <paramdef>int <parameter>max_rxlen</parameter></paramdef>
1421       </funcprototype>
1422     </funcsynopsis>
1423   </refsynopsisdiv>
1424
1425   <refsect1 id="synth-new-target-description"><title>Description</title>
1426     <para>
1427 In some ways writing a device driver for the synthetic target is very
1428 similar to writing one for a real target. Obviously it has to provide
1429 the standard interface for that class of device, so for example an
1430 ethernet device has to provide <function>can_send</function>,
1431 <function>send</function>, <function>recv</function> and similar
1432 functions. Many devices will involve interrupts, so the driver
1433 contains ISR and DSR functions and will call
1434 <function>cyg_drv_interrupt_create</function>,
1435 <function>cyg_drv_interrupt_acknowledge</function>, and related
1436 functions.
1437     </para>
1438     <para>
1439 In other ways writing a device driver for the synthetic target is very
1440 different. Usually the driver will not have any direct access to the
1441 underlying hardware. In fact for some devices the I/O may not involve
1442 real hardware, instead everything is emulated by widgets on the
1443 graphical display. Therefore the driver cannot just peek and poke
1444 device registers, instead it must interact with host-side code by
1445 exchanging message. The synthetic target HAL provides a function
1446 <function>synth_auxiliary_xchgmsg</function> for this purpose.
1447     </para>
1448     <para>
1449 Initialization of a synthetic target device driver is also very
1450 different. On real targets the device hardware already exists when the
1451 driver's initialization routine runs. On the synthetic target it is
1452 first necessary to instantiate the device inside the I/O auxiliary, by
1453 a call to <function>synth_auxiliary_instantiate</function>. That
1454 function performs a special message exchange with the I/O auxiliary,
1455 causing it to load a Tcl script for the desired type of device and run
1456 an instantiation procedure within that script.
1457     </para>
1458     <para>
1459 Use of the I/O auxiliary is optional: if the user does not specify
1460 <option>--io</option> on the command line then the auxiliary will not
1461 be started and hence most I/O operations will not be possible. Device
1462 drivers should allow for this possibility, for example by just
1463 discarding any data that gets written. The HAL exports a flag
1464 <varname>synth_auxiliary_running</varname> which should be checked.
1465     </para>
1466   </refsect1>
1467
1468   <refsect1 id="synth-new-target-instantiate"><title>Instantiating a Device</title>
1469     <para>
1470 Device instantiation should happen during the C++ prioritized static
1471 constructor phase of system initialization, before control switches to
1472 <function>cyg_user_start</function> and general application code. This
1473 ensures that there is a clearly defined point at which the I/O
1474 auxiliary knows that all required devices have been loaded. It can
1475 then perform various consistency checks and clean-ups, run the user's
1476 <filename>mainrc.tcl</filename> script, and make the main window
1477 visible.
1478     </para>
1479     <para>
1480 For standard devices generic eCos I/O code will call the device
1481 initialization routines at the right time, iterating through the
1482 <varname>DEVTAB</varname> table in a static constructor. The same
1483 holds for network devices and file systems. For more custom devices
1484 code like the following can be used:
1485     </para>
1486     <programlisting>
1487 #include &lt;cyg/infra/cyg_type.h&gt;
1488 class mydev_init {
1489   public:
1490     mydev_init() {
1491         &hellip;
1492     }
1493 };
1494 static mydev_init mydev_init_object CYGBLD_ATTRIB_INIT_PRI(CYG_INIT_IO);
1495 </programlisting>
1496     <para>
1497 Some care has to be taken because the object
1498 <varname>mydev_init_object</varname> will typically not be referenced
1499 by other code, and hence may get eliminated at link-time. If the code
1500 is part of an eCos package then problems can be avoided by putting the
1501 relevant file in <filename>libextras.a</filename>:
1502     </para>
1503     <programlisting>
1504 cdl_package CYGPKG_DEVS_MINE {
1505     &hellip;
1506     compile -library=libextras.a init.cxx
1507 }
1508 </programlisting>
1509     <para>
1510 For devices inside application code the same can be achieved by
1511 linking the relevant module as a <filename>.o</filename> file rather
1512 than putting it in a <filename>.a</filename> library.
1513     </para>
1514     <para>
1515 In the device initialization routine the main operation is a call to
1516 <function>synth_auxiliary_instantiate</function>. This takes five
1517 arguments, all of which should be strings:
1518     </para>
1519     <variablelist>
1520       <varlistentry>
1521         <term><varname>package</varname></term>
1522         <listitem><para>
1523 For device drivers which are eCos packages this should be a directory
1524 path relative to the eCos repository, for example
1525 <literal>devs/eth/synth/ecosynth</literal>. This will allow the I/O
1526 auxiliary to find the various host-side support files for this package
1527 within the install tree. If the device is application-specific and not
1528 part of an eCos package then a NULL pointer can be used, causing the
1529 I/O auxiliary to search for the support files in the current directory
1530 and then in <filename class="directory">~/.ecos/synth</filename>
1531 instead. 
1532         </para></listitem>
1533       </varlistentry>
1534       <varlistentry>
1535         <term><varname>version</varname></term>
1536         <listitem><para>
1537 For eCos packages this argument should be the version of the package
1538 that is being used, for example <literal>current</literal>. A simple
1539 way to get this version is to use the
1540 <function>SYNTH_MAKESTRING</function> macro on the package name.
1541 If the device is application-specific then a NULL pointer should be
1542 used. 
1543         </para></listitem>
1544       </varlistentry>
1545       <varlistentry>
1546         <term><varname>device</varname></term>
1547         <listitem><para>
1548 This argument specifies the type of device being instantiated, for
1549 example <literal>ethernet</literal>. More specifically the I/O
1550 auxiliary will append a <filename>.tcl</filename> suffix, giving
1551 the name of a Tcl script that will handle all I/O requests for the
1552 device. If the application requires several instances of a type
1553 of device then the script will only be loaded once, but the script
1554 will contain an instantiation procedure that will be called for each
1555 device instance. 
1556         </para></listitem>
1557       </varlistentry>
1558       <varlistentry>
1559         <term><varname>instance</varname></term>
1560         <listitem><para>
1561 If it is possible to have multiple instances of a device then this
1562 argument identifies the particular instance, for example
1563 <literal>eth0</literal> or <literal>eth1</literal>. Otherwise a NULL
1564 pointer can be used.
1565         </para></listitem>
1566       </varlistentry>
1567       <varlistentry>
1568         <term><varname>data</varname></term>
1569         <listitem><para>
1570 This argument can be used to pass additional initialization data from
1571 eCos to the host-side support. This is useful for devices where eCos
1572 configury must control certain aspects of the device, rather than
1573 host-side configury such as the target definition file, because eCos
1574 has compile-time dependencies on some or all of the relevant options.
1575 An example might be an emulated frame buffer where eCos has been
1576 statically configured for a particular screen size, orientation and
1577 depth. There is no fixed format for this string, it will be
1578 interpreted only by the device-specific host-side Tcl script. However
1579 the string length should be limited to a couple of hundred bytes to
1580 avoid possible buffer overflow problems.
1581         </para></listitem>
1582       </varlistentry>
1583     </variablelist>
1584     <para>
1585 Typical usage would look like:
1586     </para>
1587     <programlisting>
1588     if (!synth_auxiliary_running) {
1589       return;
1590     }
1591     id = synth_auxiliary_instantiate("devs/eth/synth/ecosynth",
1592              SYNTH_MAKESTRING(CYGPKG_DEVS_ETH_ECOSYNTH),
1593              "ethernet",
1594              "eth0",
1595              (const char*) 0);
1596 </programlisting>
1597     <para>
1598 The return value will be a device identifier which can be used for
1599 subsequent calls to <function>synth_auxiliary_xchgmsg</function>. If
1600 the device could not be instantiated then <literal>-1</literal> will
1601 be returned. It is the responsibility of the host-side software to
1602 issue suitable diagnostics explaining what went wrong, so normally the
1603 target-side code should fail silently.
1604     </para>
1605     <para>
1606 Once the desired device has been instantiated, often it will be
1607 necessary to do some additional initialization by a message exchange.
1608 For example an ethernet device might need information from the
1609 host-side about the MAC address, the <link
1610 linkend="synth-new-target-interrupts">interrupt vector</link>, and
1611 whether or not multicasting is supported.
1612     </para>
1613   </refsect1>
1614
1615   <refsect1 id="synth-new-target-xchgmsg"><title>Communicating with a Device</title>
1616     <para>
1617 Once a device has been instantiated it is possible to perform I/O by
1618 sending messages to the appropriate Tcl script running inside the
1619 auxiliary, and optionally getting back replies. I/O operations are
1620 always initiated by the eCos target-side, it is not possible for the
1621 host-side software to initiate data transfers. However the host-side
1622 can raise interrupts, and the interrupt handler inside the target can
1623 then exchange one or more messages with the host.
1624     </para>
1625     <para>
1626 There is a single function to perform I/O operations,
1627 <function>synth_auxiliary_xchgmsg</function>. This takes the following
1628 arguments: 
1629     </para>
1630     <variablelist>
1631       <varlistentry>
1632         <term><varname>device_id</varname></term>
1633          <listitem><para>
1634 This should be one of the identifiers returned by a previous
1635 call to <function>synth_auxiliary_instantiate</function>, specifying the
1636 particular device which should perform some I/O.
1637          </para></listitem>
1638        </varlistentry>
1639       <varlistentry>
1640         <term><varname>request</varname></term>
1641          <listitem><para>
1642 Request are just signed 32-bit integers that identify the particular
1643 I/O operation being requested. There is no fixed set of codes, instead
1644 each type of device can define its own.
1645          </para></listitem>
1646        </varlistentry>
1647       <varlistentry>
1648         <term><varname>arg1</varname></term>
1649         <term><varname>arg2</varname></term>
1650          <listitem><para>
1651 For some requests it is convenient to pass one or two additional
1652 parameters alongside the request code. For example an ethernet device
1653 could define a multicast-all request, with <varname>arg1</varname>
1654 controlling whether this mode should be enabled or disabled. Both
1655 <varname>arg1</varname> and <varname>arg2</varname> should be signed
1656 32-bit integers, and their values are interpreted only by the
1657 device-specific Tcl script.
1658          </para></listitem>
1659        </varlistentry>
1660       <varlistentry>
1661         <term><varname>txdata</varname></term>
1662         <term><varname>txlen</varname></term>
1663          <listitem><para>
1664 Some I/O operations may involve sending additional data, for example
1665 an ethernet packet. Alternatively a control operation may require many
1666 more parameters than can easily be encoded in <varname>arg1</varname>
1667 and <varname>arg2</varname>, so those parameters have to be placed in
1668 a suitable buffer and extracted at the other end.
1669 <varname>txdata</varname> is an arbitrary buffer of
1670 <varname>txlen</varname> bytes that should be sent to the host-side.
1671 There is no specific upper bound on the number of bytes that can be
1672 sent, but usually it is a good idea to allocate the transmit buffer
1673 statically and keep transfers down to at most several kilobytes.
1674          </para></listitem>
1675        </varlistentry>
1676       <varlistentry>
1677         <term><varname>reply</varname></term>
1678          <listitem><para>
1679 If the host-side is expected to send a reply message then
1680 <varname>reply</varname> should be a pointer to an integer variable
1681 and will be updated with a reply code, a simple 32-bit integer. The
1682 synthetic target HAL code assumes that the host-side and target-side
1683 agree on the protocol being used: if the host-side will not send a
1684 reply to this message then the <varname>reply</varname> argument
1685 should be a NULL pointer; otherwise the host-side must always send
1686 a reply code and the <varname>reply</varname> argument must be valid.
1687          </para></listitem>
1688        </varlistentry>
1689       <varlistentry>
1690         <term><varname>rxdata</varname></term>
1691         <term><varname>rxlen</varname></term>
1692          <listitem><para>
1693 Some operations may involve additional data coming from the host-side,
1694 for example an incoming ethernet packet. <varname>rxdata</varname>
1695 should be a suitably-sized buffer, and <varname>rxlen</varname> a
1696 pointer to an integer variable that will end up containing the number
1697 of bytes that were actually received. These arguments will only be
1698 used if the host-side is expected to send a reply and hence the
1699 <varname>reply</varname> argument was not NULL.
1700          </para></listitem>
1701        </varlistentry>
1702       <varlistentry>
1703         <term><varname>max_rxlen</varname></term>
1704          <listitem><para>
1705 If a reply to this message is expected and that reply may involve
1706 additional data, <varname>max_rxlen</varname> limits the size of that
1707 reply. In other words, it corresponds to the size of the
1708 <varname>rxdata</varname> buffer.
1709          </para></listitem>
1710        </varlistentry>
1711     </variablelist>
1712     <para>
1713 Most I/O operations involve only some of the arguments. For example
1714 transmitting an ethernet packet would use the
1715 <varname>request</varname>, <varname>txdata</varname> and
1716 <varname>txlen</varname> fields (in addition to
1717 <varname>device_id</varname> which is always required), but would not
1718 involve <varname>arg1</varname> or <varname>arg2</varname> and no
1719 reply would be expected. Receiving an ethernet packet would involve
1720 <varname>request</varname>, <varname>rxdata</varname>,
1721 <varname>rxlen</varname> and <varname>max_rxlen</varname>; in addition
1722 <varname>reply</varname> is needed to get any reply from the host-side
1723 at all, and could be used to indicate whether or not any more packets
1724 are buffered up. A control operation such as enabling multicast mode
1725 would involve <varname>request</varname> and <varname>arg1</varname>,
1726 but none of the remaining arguments.
1727     </para>
1728   </refsect1>
1729
1730   <refsect1 id="synth-new-target-interrupts"><title>Interrupt Handling</title>
1731     <para>
1732 Interrupt handling in the synthetic target is much the same as on a
1733 real target. An interrupt object is created using
1734 <function>cyg_drv_interrupt_create</function>, attached, and unmasked.
1735 The emulated device - in other words the Tcl script running inside the
1736 I/O auxiliary - can raise an interrupt. Subject to interrupts being
1737 disabled and the appropriate vector being masked, the system will
1738 invoke the specified ISR function. The synthetic target HAL
1739 implementation does have some limitations: there is no support for
1740 nested interrupts, interrupt priorities, or a separate interrupt
1741 stack. Supporting those might be appropriate when targetting a
1742 simulator that attempts to model real hardware accurately, but not for
1743 the simple emulation provided by the synthetic target.
1744     </para>
1745     <para>
1746 Of course the actual implementation of the ISR and DSR functions will
1747 be rather different for a synthetic target device driver. For real
1748 hardware the device driver will interact with the device by reading
1749 and writing device registers, managing DMA engines, and the like. A
1750 synthetic target driver will instead call
1751 <function>synth_auxiliary_xchgmsg</function> to perform the I/O
1752 operations.
1753     </para>
1754     <para>
1755 There is one other significant difference between interrupt handling
1756 on the synthetic target and on real hardware. Usually the eCos code
1757 will know which interrupt vectors are used for which devices. That
1758 information is fixed when the target hardware is designed. With the
1759 synthetic target interrupt vectors are assigned to devices on the host
1760 side, either via the target definition file or dynamically when the
1761 device is instantiated. Therefore the initialization code for a
1762 target-side device driver will need to request interrupt vector
1763 information from the host-side, via a message exchange. Such interrupt
1764 vectors will be in the range 1 to 31 inclusive, with interrupt 0 being
1765 reserved for the real-time clock.
1766     </para>
1767   </refsect1>
1768
1769 </refentry>
1770
1771 <!-- }}} -->
1772 <!-- {{{ New devices - host-side        -->
1773
1774 <refentry id="synth-new-host">
1775   <refmeta>
1776     <refentrytitle>Writing New Devices - host</refentrytitle>
1777   </refmeta>
1778   <refnamediv>
1779     <refname>Writing New Devices</refname>
1780     <refpurpose>extending the synthetic target, host-side</refpurpose>
1781   </refnamediv>
1782
1783   <refsect1 id="synth-new-host-description"><title>Description</title>
1784     <para>
1785 On the host-side adding a new device means writing a Tcl/Tk script
1786 that will handle instantiation and subsequent requests from the
1787 target-side. These scripts all run in the same full interpreter,
1788 extended with various commands provided by the main I/O auxiliary
1789 code, and running in an overall GUI framework. Some knowledge of
1790 programming with Tcl/Tk is required to implement host-side device
1791 support.
1792     </para>
1793     <para>
1794 Some devices can be implemented entirely using a Tcl/Tk script. For
1795 example, if the final system will have some buttons then those can be
1796 emulated in the synthetic target using a few Tk widgets. A simple
1797 emulation could just have the right number of buttons in a row. A more
1798 advanced emulation could organize the buttons with the right layout,
1799 perhaps even matching the colour scheme, the shapes, and the relative
1800 sizes. With other devices it may be necessary for the Tcl script to
1801 interact with an external program, because the required functionality
1802 cannot easily be accessed from a Tcl script. For example interacting
1803 with a raw ethernet device involves some <function>ioctl</function>
1804 calls, which is easier to do in a C program. Therefore the
1805 <filename>ethernet.tcl</filename> script which implements the
1806 host-side ethernet support spawns a separate program
1807 <filename>rawether</filename>, written in C, that performs the
1808 low-level I/O. Raw ethernet access usually also requires root
1809 privileges, and running a small program <filename>rawether</filename>
1810 with such privileges is somewhat less of a security risk than the
1811 whole eCos application, the I/O auxiliary, and various dynamically
1812 loaded Tcl scripts.
1813     </para>
1814     <para>
1815 Because all scripts run in a single interpreter, some care has
1816 to be taken to avoid accidental sharing of global variables. The best
1817 way to avoid problems is to have each script create its own Tcl
1818 namespace, so for example the <filename>ethernet.tcl</filename> script
1819 creates a namespace <varname>ethernet::</varname> and all variables
1820 and procedures reside in this namespace. Similarly the I/O auxiliary
1821 itself makes use of a <varname>synth::</varname> namespace.
1822     </para>
1823   </refsect1>
1824
1825   <refsect1 id="synth-new-host-build"><title>Building and Installation</title>
1826     <para>
1827 When an eCos device driver or application code instantiates a device,
1828 the I/O auxiliary will attempt to load a matching Tcl script. The
1829 third argument to <function>synth_auxiliary_instantiate</function>
1830 specifies the type of device, for example <literal>ethernet</literal>,
1831 and the I/O auxiliary will append a <filename>.tcl</filename> suffix
1832 and look for a script <filename>ethernet.tcl</filename>.
1833     </para>
1834     <para>
1835 If the device being instantiated is application-specific rather than
1836 part of an eCos package, the I/O auxiliary will look first in the
1837 current directory, then in <filename
1838 class="directory">~/.ecos/synth</filename>. If it is part of an eCos
1839 package then the auxiliary will expect to find the Tcl script and any
1840 support files below <filename
1841 class="directory">libexec/ecos</filename> in the install tree - note
1842 that the same install tree must be used for the I/O auxiliary itself
1843 and for any device driver support. The directory hierarchy below
1844 <filename class="directory">libexec/ecos</filename> matches the
1845 structure of the eCos repository, allowing multiple versions of a
1846 package to be installed to allow for incompatible protocol changes.
1847     </para>
1848     <para>
1849 The preferred way to build host-side software is to use
1850 <command>autoconf</command> and <command>automake</command>. Usually
1851 this involves little more than copying the
1852 <filename>acinclude.m4</filename>, <filename>configure.in</filename>
1853 and <filename>Makefile.am</filename> files from an existing package,
1854 for example the synthetic target ethernet driver, and then making
1855 minor edits. In <filename>acinclude.m4</filename> it may be necessary
1856 to adjust the path to the root of the repository.
1857 <filename>configure.in</filename> may require a similar change, and
1858 the <function>AC_INIT</function> macro invocation will have to be
1859 changed to match one of the files in the new package. A critical macro
1860 in this file is <filename>ECOS_PACKAGE_DIRS</filename> which will set
1861 up the correct install directory. <filename>Makefile.am</filename> may
1862 require some more changes, for example to specify the data files that
1863 should be installed (including the Tcl script). These files should
1864 then be processed using <command>aclocal</command>,
1865 <command>autoconf</command> and <command>automake</command> in that
1866 order. Actually building the software then just involves
1867 <command>configure</command>, <command>make</command> and
1868 <command>make install</command>, as per the instructions in the
1869 toplevel <filename>README.host</filename> file.
1870     </para>
1871     <para>
1872 To assist developers, if the environment variable
1873 <envar>ECOSYNTH_DEVEL</envar> is set then a slightly different
1874 algorithm is used for locating device Tcl scripts. Instead of looking
1875 only in the install tree the I/O auxiliary will also look in the
1876 source tree, and if the script there is more recent than the installed
1877 version it will be used in preference. This allows developers to
1878 modify the master copy without having to run <command>make
1879 install</command> all the time.
1880     </para>
1881     <para>
1882 If a script needs to know where it has been installed it can examine
1883 the Tcl variable <varname>synth::device_install_dir</varname> . This
1884 variable gets updated whenever a  script is loaded, so if the
1885 value may be needed later it should be saved away in a device-specific
1886 variable. 
1887     </para>
1888   </refsect1>
1889
1890   <refsect1 id="synth-new-host-instantiation"><title>Instantiation</title>
1891     <para>
1892 The I/O auxiliary will <command>source</command> the device-specific
1893 Tcl script when the eCos application first attempts to instantiate a
1894 device of that type. The script should return a procedure that will be
1895 invoked to instantiate a device.
1896     </para>
1897     <programlisting>
1898 namespace eval ethernet {
1899     &hellip;
1900     proc instantiate { id instance data } {
1901         &hellip;
1902         return ethernet::handle_request
1903     }
1904 }
1905 return ethernet::instantiate
1906 </programlisting>
1907     <para>
1908 The <varname>id</varname> argument is a unique identifier for this
1909 device instance. It will also be supplied on subsequent calls to the
1910 request handler, and will match the return value of
1911 <function>synth_auxiliary_instantiate</function> on the target side. A
1912 common use for this value is as an array index to support multiple
1913 instances of this types of device. The <varname>instance</varname> and
1914 <varname>data</varname> arguments match the corresponding arguments to
1915 <function>synth_auxiliary_instantiate</function> on the target side, so
1916 a typical value for <varname>instance</varname> would be
1917 <literal>eth0</literal>, and <varname>data</varname> is used to pass
1918 arbitrary initialization parameters from target to host.
1919     </para>
1920     <para>
1921 The actual work done by the instantiation procedure is obviously
1922 device-specific. It may involve allocating an <link
1923 linkend="synth-new-host-interrupts">interrupt vector</link>, adding a
1924 device-specific subwindow to the display, opening a real Linux device,
1925 establishing a socket connection to some server, spawning a separate
1926 process to handle the actual I/O, or a combination of some or all of
1927 the above.
1928     </para>
1929     <para>
1930 If the device is successfully instantiated then the return value
1931 should be a handler for subsequent I/O requests. Otherwise the return
1932 value should be an empty string, and on the target-side the
1933 <function>synth_auxiliary_instantiate</function> call will return
1934 <literal>-1</literal>. The script is responsible for providing
1935 <link linkend="synth-new-host-output">diagnostics</link> explaining
1936 why the device could not be instantiated.
1937     </para>
1938   </refsect1>
1939
1940   <refsect1 id="synth-new-host-requests"><title>Handling Requests</title>
1941     <para>
1942 When the target-side calls
1943 <function>synth_auxiliary_xchgmsg</function>, the I/O auxiliary will
1944 end up calling the request handler for the appropriate device instance
1945 returned during instantiation:
1946     </para>
1947     <programlisting>
1948 namespace eval ethernet {
1949     &hellip;
1950     proc handle_request { id request arg1 arg2 txdata txlen max_rxlen } {
1951         &hellip;
1952         if { &lt;some condition&gt; } {
1953             synth::send_reply &lt;error code&gt; 0 ""
1954             return
1955         }
1956         &hellip;
1957         synth::send_reply &lt;reply code&gt; $packet_len $packet
1958     }
1959     &hellip;
1960 }
1961 </programlisting>
1962     <para>
1963 The <varname>id</varname> argument is the same device id that was
1964 passed to the instantiate function, and is typically used as an array
1965 index to access per-device data. The <varname>request</varname>,
1966 <varname>arg1</varname>, <varname>arg2</varname>, and
1967 <varname>max_rxlen</varname> are the same values that were passed to
1968 <function>synth_auxiliary_xchgmsg</function> on the target-side,
1969 although since this is a Tcl script obviously the numbers have been
1970 converted to strings. The <varname>txdata</varname> buffer is raw data
1971 as transmitted by the target, or an empty string if the I/O operation
1972 does not involve any additional data. The Tcl procedures
1973 <command>binary scan</command>, <command>string index</command> and
1974 <command>string range</command> may be found especially useful when
1975 manipulating this buffer. <varname>txlen</varname> is provided for
1976 convenience, although <command>string length $txdata</command> would
1977 give the same information.
1978     </para>
1979     <para>
1980 The code for actually processing the request is of course device
1981 specific. If the target does not expect a reply then the request
1982 handler should just return when finished. If a reply is expected then
1983 there should be a call to <command>synth::send_reply</command>. The
1984 first argument is the reply code, and will be turned into a 32-bit
1985 integer on the target side. The second argument specifies the length
1986 of the reply data, and the third argument is the reply data itself.
1987 For some devices the Tcl procedure <command>binary format</command>
1988 may prove useful. If the reply involves just a code and no additional
1989 data, the second and third arguments should be <literal>0</literal>
1990 and an empty string respectively.
1991     </para>
1992     <para>
1993 Attempts to send a reply when none is expected, fail to send a reply
1994 when one is expected, or send a reply that is larger than the
1995 target-side expects, will all be detected by the I/O auxiliary and
1996 result in run-time error messages.
1997     </para>
1998     <para>
1999 It is not possible for the host-side code to send unsolicited messages
2000 to the target. If host-side code needs attention from the target, for
2001 example because some I/O operation has completed, then an interrupt
2002 should be raised.
2003     </para>
2004   </refsect1>
2005
2006   <refsect1 id="synth-new-host-interrupts"><title>Interrupts</title>
2007     <para>
2008 The I/O auxiliary provides a number of procedures for interrupt
2009 handling. 
2010     </para>
2011     <programlisting>
2012 synth::interrupt_allocate &lt;name&gt;
2013 synth::interrupt_get_max
2014 synth::interrupt_get_devicename &lt;vector&gt;
2015 synth::interrupt_raise &lt;vector&gt;
2016 </programlisting>
2017     <para>
2018 <command>synth::interrupt_allocate</command> is normally called during
2019 device instantiation, and returns the next free interrupt vector. This
2020 can be passed on to the target-side device driver in response to a
2021 suitable request, and it can then install an interrupt handler on that
2022 vector. Interrupt vector <literal>0</literal> is used within the
2023 target-side code for the real-time clock, so the allocated vectors
2024 will start at <literal>1</literal>. The argument identifies the
2025 device, for example <literal>eth0</literal>. This is not actually used
2026 internally, but can be accessed by user-initialization scripts that
2027 provide some sort of interrupt monitoring facility (typically via the
2028 <literal>interrupt</literal> <link
2029 linkend="synth-new-host-hooks">hook</link>). It is possible for a
2030 single device to allocate multiple interrupt vectors, but the
2031 synthetic target supports a maximum of 32 such vectors.
2032     </para>
2033     <para>
2034 <command>synth::interrupt_get_max</command> returns the highest
2035 interrupt vector that has been allocated, or <literal>0</literal> if
2036 there have been no calls to
2037 <command>synth::interrupt_allocate</command>.
2038 <command>synth::interrupt_get_devicename</command> returns the string
2039 that was passed to <command>synth::interrupt_allocate</command> when
2040 the vector was allocated.
2041     </para>
2042     <para>
2043 <command>synth::interrupt_raise</command> can be called any time after
2044 initialization. The argument should be the vector returned by
2045 <command>synth::interrupt_allocate</command> for this device. It will
2046 activate the normal eCos interrupt handling mechanism so, subject to
2047 interrupts being enabled and this particular interrupt not being
2048 masked out, the appropriate ISR will run.
2049     </para>
2050     <note><para>
2051 At this time it is not possible for a device to allocate a specific
2052 interrupt vector. The order in which interrupt vectors are assigned to
2053 devices effectively depends on the order in which the eCos devices get
2054 initialized, and that may change if the eCos application is rebuilt. A
2055 future extension may allow devices to allocate specific vectors, thus
2056 making things more deterministic. However that will introduce new
2057 problems, in particular the code will have to start worrying about
2058 requests for vectors that have already been allocated.
2059     </para></note>
2060   </refsect1>
2061
2062   <refsect1 id="synth-new-host-args"><title>Flags and Command Line Arguments</title>
2063     <para>
2064 The generic I/O auxiliary code will process the standard command line
2065 arguments, and will set various flag variables accordingly. Some of
2066 these should be checked by device-specific scripts.
2067     </para>
2068     <variablelist>
2069       <varlistentry>
2070         <term><varname>synth::flag_gui</varname></term>
2071         <listitem><para>
2072 This is set when the I/O auxiliary is operating in graphical mode
2073 rather than text mode. Some functionality such as filters and the GUI
2074 layout are only available in graphical mode.
2075         </para>
2076         <programlisting>
2077     if { $synth::flag_gui } {
2078         &hellip;
2079     }
2080 </programlisting></listitem>
2081       </varlistentry>
2082       <varlistentry>
2083         <term><varname>synth::flag_verbose</varname></term>
2084          <listitem><para>
2085 The user has requested additional information during startup. Each
2086 device driver can decide how much additional information, if any,
2087 should be produced.
2088          </para></listitem>
2089       </varlistentry>
2090       <varlistentry>
2091         <term><varname>synth::flag_keep_going</varname></term>
2092         <listitem><para>
2093 The user has specified <option>-k</option> or
2094 <option>--keep-going</option>, so even if an error occurs the I/O
2095 auxiliary and the various device driver scripts should continue running
2096 if at all possible. Diagnostics should still be generated.
2097         </para></listitem>
2098       </varlistentry>
2099     </variablelist>
2100     <para>
2101 Some scripts may want to support additional command line arguments.
2102 This facility should be used with care since there is no way to
2103 prevent two different scripts from trying to use the same argument.
2104 The following Tcl procedures are available:
2105     </para>
2106     <programlisting>
2107 synth::argv_defined &lt;name&gt;
2108 synth::argv_get_value &lt;name&gt;
2109 </programlisting>
2110     <para>
2111 <command>synth::argv_defined</command> returns a boolean to indicate
2112 whether or not a particular argument is present. If the argument is
2113 the name part of a name/value pair, an <literal>=</literal> character
2114 should be appended. Typical uses might be:
2115     </para>
2116     <programlisting>
2117     if { [synth::argv_defined "-o13"] } {
2118         &hellip;
2119     }
2120
2121     if { [synth::argv_defined "-mark="] } {
2122         &hellip;
2123     }
2124 </programlisting>
2125     <para>
2126 The first call checks for a flag <literal>-o13</literal> or
2127 <literal>--o13</literal> - the code treats options with single and
2128 double hyphens interchangeably. The second call checks for an argument
2129 of the form <literal>-mark=&lt;value&gt;</literal> or a pair of
2130 arguments <literal>-mark &lt;value&gt;</literal>. The value part of a
2131 name/value pair can be obtained using
2132 <command>synth::argv_get_value</command>;
2133     </para>
2134     <programlisting>
2135     variable speed 1
2136     if { [synth::argv_defined "-mark="] } {
2137         set mark [synth::argv_get_value "-mark="]
2138         if { ![string is integer $mark] || ($mark < 1) || ($mark > 9) } {
2139             &lt;issue diagnostic&gt;
2140         } else {
2141             set speed $mark
2142         }
2143     }
2144 </programlisting>
2145     <para>
2146 <command>synth::argv_get_value</command> should only be used after a
2147 successful call to <command>synth::argv_defined</command>.
2148 At present there is no support for some advanced forms of command line
2149 argument processing. For example it is not possible to repeat a
2150 certain option such as <option>-v</option> or
2151 <option>--verbose</option>, with each occurrence increasing the level
2152 of verbosity. 
2153     </para>
2154     <para>
2155 If a script is going to have its own set of command-line arguments
2156 then it should give appropriate details if the user specifies
2157 <option>--help</option>. This involves a hook function:
2158     </para>
2159     <programlisting>
2160 namespace eval my_device {
2161     proc help_hook { } {
2162         puts " -o13          : activate the omega 13 device"
2163         puts " -mark &lt;speed&gt; : set speed. Valid values are 1 to 9."
2164     }
2165
2166     synth::hook_add "help" my_device::help_hook
2167 }
2168 </programlisting>
2169   </refsect1>
2170
2171   <refsect1 id="synth-new-host-tdf"><title>The Target Definition File</title>
2172     <para>
2173 Most device scripts will want to check entries in the target
2174 definition file for run-time configuration information. The Tcl
2175 procedures for this are as follows:
2176     </para>
2177     <programlisting>
2178 synth::tdf_has_device &lt;name&gt;
2179 synth::tdf_get_devices
2180 synth::tdf_has_option &lt;devname&gt; &lt;option&gt;
2181 synth::tdf_get_option &lt;devname&gt; &lt;option&gt;
2182 synth::tdf_get_options &lt;devname&gt; &lt;option&gt;
2183 synth::tdf_get_all_options &lt;devname&gt;
2184 </programlisting>
2185     <para>
2186 <command>synth::tdf_has_device</command> can be used to check whether
2187 or not the target definition file had an entry
2188 <literal>synth_device&nbsp;&lt;name&gt;</literal>. Usually the name
2189 will match the type of device, so the
2190 <filename>console.tcl</filename> script will look for a target
2191 definition file entry <literal>console</literal>.
2192 <command>synth::tdf_get_devices</command> returns a list of all
2193 device entries in the target definition file.
2194     </para>
2195     <para>
2196 Once it is known that the target definition file has an entry for a
2197 certain device, it is possible to check for options within the entry.
2198 <command>synth::tdf_has_option</command> just checks for the presence,
2199 returning a boolean:
2200     </para>
2201     <programlisting>
2202     if { [synth::tdf_has_option "console" "appearance"] } {
2203         &hellip;
2204     }
2205 </programlisting>
2206     <para>
2207 <command>synth::tdf_get_option</command> returns a list of all the
2208 arguments for a given option. For example, if the target definition
2209 file contains an entry:
2210     </para>
2211     <programlisting>
2212 synth_device console {
2213     appearance -foreground white -background black
2214     filter trace {^TRACE:.*} -foreground HotPink1 -hide 1
2215     filter xyzzy {.*xyzzy.*} -foreground PapayaWhip
2216 }
2217 </programlisting>
2218     <para>
2219 A call
2220 <command>synth::tdf_get_option&nbsp;console&nbsp;appearance</command>
2221 will return the list <literal>{-foreground white -background
2222 black}</literal>. This list can be manipulated using standard Tcl routines
2223 such as <command>llength</command> and <command>lindex</command>. Some
2224 options can occur multiple times in one entry, for example
2225 <option>filter</option> in the <literal>console</literal> entry.
2226 <command>synth::tdf_get_options</command> returns a list of lists,
2227 with one entry for each option occurrence.
2228 <command>synth::tdf_get_all_options</command> returns a list of lists
2229 of all options. This time each entry will include the option name as
2230 well.
2231     </para>
2232     <para>
2233 The I/O auxiliary will not issue warnings about entries in the target
2234 definition file for devices which were not loaded, unless the
2235 <option>-v</option> or <option>--verbose</option> command line
2236 argument was used. This makes it easier to use a single target
2237 definition file for different applications. However the auxiliary will
2238 issue warnings about options within an entry that were ignored,
2239 because often these indicate a typing mistake of some sort. Hence a
2240 script should always call <command>synth::tdf_has_option</command>,
2241 <command>synth:;tdf_get_option</command> or
2242 <command>synth::tdf_get_options</command> for all valid options, even
2243 if some of the options preclude the use of others.
2244     </para>
2245   </refsect1>
2246
2247   <refsect1 id="synth-new-host-hooks"><title>Hooks</title>
2248     <para>
2249 Some scripts may want to take action when particular events occur, for
2250 example when the eCos application has exited and there is no need for
2251 further I/O. This is supported using hooks:
2252     </para>
2253     <programlisting>
2254 namespace eval my_device {
2255     &hellip;
2256     proc handle_ecos_exit { arg_list } {
2257         &hellip;
2258     }
2259     synth::hook_add "ecos_exit" my_device::handle_ecos_exit
2260 }
2261 </programlisting>
2262     <para>
2263 It is possible for device scripts to add their own hooks and call all
2264 functions registered for those hooks. A typical use for this is by
2265 user initialization scripts that want to monitor some types of I/O.
2266 The available Tcl procedures for manipulating hooks are:
2267     </para>
2268     <programlisting>
2269 synth::hook_define &lt;name&gt;
2270 synth::hook_defined &lt;name&gt;
2271 synth::hook_add &lt;name&gt; &lt;function&gt;
2272 synth::hook_call &lt;name&gt; &lt;args&gt;
2273 </programlisting>
2274     <para>
2275 <command>synth::hook_define</command> creates a new hook with the
2276 specified name. This hook must not already exist.
2277 <command>synth::hook_defined</command> can be used to check for the
2278 existence of a hook. <command>synth::hook_add</command> allows other
2279 scripts to register a callback function for this hook, and
2280 <command>synth::hook_call</command> allows the owner script to invoke
2281 all such callback functions. A hook must already be defined before a
2282 callback can be attached. Therefore typically device scripts will only
2283 use standard hooks and their own hooks, not hooks created by some
2284 other device, because the order of device initialization is not
2285 sufficiently defined. User scripts run from
2286 <filename>mainrc.tcl</filename> can use any hooks that have been
2287 defined.
2288     </para>
2289     <para>
2290 <command>synth::hook_call</command> takes an arbitrary list of
2291 arguments, for example:
2292     </para>
2293     <programlisting>
2294     synth::hook_call "ethernet_rx" "eth0" $packet
2295 </programlisting>
2296     <para>
2297 The callback function will always be invoked with a single argument,
2298 a list of the arguments that were passed to
2299 <command>synth::hook_call</command>: 
2300     </para>
2301     <programlisting>
2302     proc rx_callback { arg_list } {
2303         set device [lindex $arg_list 0]
2304         set packet [lindex $arg_list 1]
2305     }
2306 </programlisting>
2307     <para>
2308 Although it might seem more appropriate to use Tcl's
2309 <command>eval</command> procedure and have the callback functions
2310 invoked with the right number of arguments rather than a single list,
2311 that would cause serious problems if any of the data contained special
2312 characters such as <literal>[</literal> or <literal>$</literal>. The
2313 current implementation of hooks avoids such problems, at the cost of
2314 minor inconvenience when writing callbacks.
2315     </para>
2316     <para>
2317 A number of hooks are defined as standard. Some devices will add
2318 additional hooks, and the device-specific documentation should be
2319 consulted for those. User scripts can add their own hooks if desired.
2320     </para>
2321     <variablelist>
2322       <varlistentry>
2323         <term><literal>exit</literal></term>
2324         <listitem><para>
2325 This hook is called just before the I/O auxiliary exits. Hence it
2326 provides much the same functionality as <function>atexit</function> in
2327 C programs. The argument list passed to the callback function will be
2328 empty. 
2329         </para></listitem>
2330       </varlistentry>
2331       <varlistentry>
2332         <term><literal>ecos_exit</literal></term>
2333         <listitem><para>
2334 This hook is called when the eCos application has exited. It is used
2335 mainly to shut down I/O operations: if the application is no longer
2336 running then there is no point in raising interrupts or storing
2337 incoming packets. The callback argument list will be empty.
2338         </para></listitem>
2339       </varlistentry>
2340       <varlistentry>
2341         <term><literal>ecos_initialized</literal></term>
2342         <listitem><para>
2343 The synthetic target HAL will send a request to the I/O auxiliary once
2344 the static constructors have been run. All devices should now have been
2345 instantiated. A script could now check how many instances there are of
2346 a given type of device, for example ethernet devices, and create a
2347 little monitor window showing traffic on all the devices. The
2348 <literal>ecos_initialized</literal> callbacks will be run just before
2349 the user's <filename>mainrc.tcl</filename> script. The callback
2350 argument list will be empty.
2351         </para></listitem>
2352       </varlistentry>
2353       <varlistentry>
2354         <term><literal>help</literal></term>
2355         <listitem><para>
2356 This hook is also invoked once static constructors have been run, but
2357 only if the user specified <option>-h</option> or
2358 <option>--help</option>. Any scripts that add their own command line
2359 arguments should add a callback to this hook which outputs details of
2360 the additional arguments. The callback argument list will be empty.
2361         </para></listitem>
2362       </varlistentry>
2363       <varlistentry>
2364         <term><literal>interrupt</literal></term>
2365         <listitem><para>
2366 Whenever a device calls <command>synth::interrupt_raise</command> the
2367 <literal>interrupt</literal> hook will be called with a single
2368 argument, the interrupt vector. The main use for this is to allow
2369 user scripts to monitor interrupt traffic.
2370         </para></listitem>
2371       </varlistentry>
2372     </variablelist>
2373   </refsect1>
2374   
2375   <refsect1 id="synth-new-host-output"><title>Output and Filters</title>
2376     <para>
2377 Scripts can use conventional facilities for sending text output to the
2378 user, for example calling <command>puts</command> or directly
2379 manipulating the central text widget
2380 <varname>.main.centre.text</varname>. However in nearly all cases it
2381 is better to use output facilities provided by the I/O auxiliary
2382 itself: 
2383     </para>
2384     <programlisting>
2385 synth::report &lt;msg&gt;
2386 synth::report_warning &lt;msg&gt;
2387 synth::report_error &lt;msg&gt;
2388 synth::internal_error &lt;msg&gt;
2389 synth::output &lt;msg&gt; &lt;filter&gt;
2390 </programlisting>
2391     <para>
2392 <command>synth::report</command> is intended for messages related to
2393 the operation of the I/O auxiliary itself, especially additional
2394 output resulting from <option>-v</option> or
2395 <option>--verbose</option>. If running in text mode the output will go
2396 to standard output. If running in graphical mode the output will go to
2397 the central text window. In both modes, use of <option>-l</option> or
2398 <option>--logfile</option> will modify the behaviour.
2399     </para>
2400     <para>
2401 <command>synth::report_warning</command>,
2402 <command>synth::report_error</command> and
2403 <command>synth::internal_error</command> have the obvious meaning,
2404 including prepending strings such as <literal>Warning:</literal> and
2405 <literal>Error:</literal>. When the eCos application informs the I/O
2406 auxiliary that all static constructors have run, if at that point
2407 there have been any calls to <command>synth::error</command> then the
2408 I/O auxiliary will exit. This can be suppressed with command line
2409 arguments <option>-k</option> or <option>--keep-going</option>.
2410 <command>synth::internal_error</command> will output some information
2411 about the current state of the I/O auxiliary and then exit
2412 immediately. Of course it should never be necessary to call this
2413 function. 
2414     </para>
2415     <para>
2416 <command>synth::output</command> is the main routine for outputting
2417 text. The second argument identifies a filter. If running in text mode
2418 the filter is ignored, but if running in graphical mode the filter can
2419 be used to control the appearance of this output. A typical use would
2420 be:
2421     </para>
2422     <programlisting>
2423     synth::output $line "console"
2424 </programlisting>
2425     <para>
2426 This outputs a single line of text using the
2427 <literal>console</literal> filter. If running in graphical mode the
2428 default appearance of this text can be modified with the
2429 <option>appearance</option> option in the
2430 <command>synth_device&nbsp;console</command> entry of the target
2431 definition file. The <guimenuitem>System filters</guimenuitem> menu
2432 option can be used to change the appearance at run-time.
2433     </para>
2434     <para>
2435 Filters should be created before they are used. The procedures
2436 available for this are:
2437     </para>
2438     <programlisting>
2439 synth::filter_exists &lt;name&gt;
2440 synth::filter_get_list
2441 synth::filter_add &lt;name&gt; [options]
2442 synth::filter_parse_options &lt;options&gt; &lt;parsed_options&gt; &lt;message&gt;
2443 synth::filter_add_parsed &lt;name&gt; &lt;parsed_options&gt;
2444 </programlisting>
2445     <para>
2446 <command>synth::filter_exists</command> can be used to check whether
2447 or not a particular filter already exists: creating two filters with
2448 the same name is not allowed.
2449 <command>synth::filter_get_list</command> returns a list of the
2450 current known filters. <command>synth::filter_add</command> can be
2451 used to create a new filter. The first argument names the new filter,
2452 and the remaining arguments control the initial appearance. A typical
2453 use might be:
2454     </para>
2455     <programlisting>
2456     synth::filter_add "my_device_tx" -foreground yellow -hide 1
2457 </programlisting>
2458     <para>
2459 It is assumed that the supplied arguments are valid, which typically
2460 means that they are hard-wired in the script. If instead the data
2461 comes out of a configuration file and hence may be invalid, the
2462 I/O auxiliary provides a parsing utility. Typical usage would be:
2463     </para>
2464     <programlisting>
2465     array set parsed_options [list]
2466     set message ""
2467     if { ![synth::filter_parse_options $console_appearance parsed_options message] } {
2468         synth::report_error \
2469                 "Invalid entry in target definition file $synth::target_definition\
2470                  \n  synth_device \"console\", entry \"appearance\"\n$message"
2471     } else {
2472         synth::filter_add_parsed "console" parsed_options
2473     }
2474 </programlisting>
2475     <para>
2476 On success <varname>parsed_options</varname> will be updated with an
2477 internal representation of the desired appearance, which can then be
2478 used in a call to <command>synth::filter_add_parsed</command>. On
2479 failure <varname>message</varname> will be updated with details of the
2480 parsing error that occurred.
2481     </para>
2482   </refsect1>
2483   
2484   <refsect1 id="synth-new-host-gui"><title>The Graphical Interface</title>
2485     <para>
2486 When the I/O auxiliary is running in graphical mode, many scripts will
2487 want to update the user interface in some way. This may be as simple
2488 as adding another entry to the help menu for the device, or adding a
2489 new button to the toolbar. It may also involve adding new subwindows,
2490 or even creating entire new toplevel windows. These may be simple
2491 monitor windows, displaying additional information about what is going
2492 on in the system in a graphical format. Alternatively they may emulate
2493 actual I/O operations, for example button widgets could be used to
2494 emulate real physical buttons.
2495     </para>
2496     <para>
2497 The I/O auxiliary does not provide many procedures related to the
2498 graphical interface. Instead it is expected that scripts will just
2499 update the widget hierarchy directly.
2500     </para>
2501     <informalfigure PgWide=1>
2502       <mediaobject>
2503         <imageobject>
2504           <imagedata fileref="layout.png" Scalefit=1 Align="Center">
2505         </imageobject>
2506       </mediaobject>
2507     </informalfigure>
2508     <para>
2509 So adding a new item to the <guimenu>Help</guimenu> menu involves a
2510 <command>.menubar.help&nbsp;add</command> operation with suitable
2511 arguments. Adding a new button to the toolbar involves creating a
2512 child window in <varname>.toolbar</varname> and packing it
2513 appropriately. Scripts can create their own subwindows and then pack
2514 it into one of <varname>.main.nw</varname>,
2515 <varname>.main.n</varname>, <varname>.main.ne</varname>,
2516 <varname>.main.w</varname>, <varname>.main.e</varname>,
2517 <varname>.main.sw</varname>, <varname>.main.s</varname> or
2518 <varname>.main.se</varname>. Normally the user should be allowed to
2519 <link linkend="synth-gui-layout">control</link> this via the target
2520 definition file. The central window <varname>.main.centre</varname>
2521 should normally be left alone by other scripts since it gets used for
2522 text output.
2523     </para>
2524     <para>
2525 The following graphics-related utilities may be found useful:
2526     </para>
2527     <programlisting>
2528 synth::load_image &lt;image name&gt; &lt;filename&gt;
2529 synth::register_ballon_help &lt;widget&gt; &lt;message&gt;
2530 synth::handle_help &lt;URL&gt;
2531 </programlisting>
2532     <para>
2533 <command>synth::load_image</command> can be used to add a new image to
2534 the current interpreter. If the specified file has a
2535 <filename>.xbm</filename> extension then the image will be a
2536 monochrome bitmap, otherwise it will be a colour image of some sort.
2537 A boolean will be returned to indicate success or failure, and
2538 suitable diagnostics will be generated if necessary.
2539     </para>
2540     <para>
2541 <command>synth::register_balloon_help</command> provides balloon help
2542 for a specific widget, usually a button on the toolbar.
2543     </para>
2544     <para>
2545 <command>synth::handle_help</command> is a utility routine that can be
2546 installed as the command for displaying online help, for example:
2547     </para>
2548     <programlisting>
2549     .menubar.help add command -label "my device" -command \
2550         [list synth::handle_help "file://$path"]
2551 </programlisting>
2552   </refsect1>
2553   
2554 </refentry>
2555
2556 <!-- }}} -->
2557 <!-- {{{ Porting                        -->
2558
2559 <refentry id="synth-porting">
2560   <refmeta>
2561     <refentrytitle>Porting</refentrytitle>
2562   </refmeta>
2563   <refnamediv>
2564     <refname>Porting</refname>
2565     <refpurpose>Adding support for other hosts</refpurpose>
2566   </refnamediv>
2567
2568   <refsect1 id="synth-porting-description"><title>Description</title>
2569     <para>
2570 The initial development effort of the eCos synthetic target happened
2571 on x86 Linux machines. Porting to other platforms involves addressing
2572 a number of different issues. Some ports should be fairly
2573 straightforward, for example a port to Linux on a processor other than
2574 an x86. Porting to Unix or Unix-like operating systems other than
2575 Linux may be possible, but would involve more effort. Porting to a
2576 completely different operating system such as Windows would be very
2577 difficult. The text below complements the eCos Porting Guide.
2578     </para>
2579   </refsect1>
2580
2581   <refsect1 id="synth-porting-linux"><title>Other Linux Platforms</title>
2582     <para>
2583 Porting the synthetic target to a Linux platform that uses a processor
2584 other than x86 should be straightforward. The simplest approach is to
2585 copy the existing <filename class="directory">i386linux</filename>
2586 directory tree in the <filename class="directory">hal/synth</filename>
2587 hierarchy, then rename and edit the ten or so files in this package.
2588 Most of the changes should be pretty obvious, for example on a 64-bit
2589 processor some new data types will be needed in the
2590 <filename>basetype.h</filename> header file. It will also be necessary
2591 to update the toplevel <filename>ecos.db</filename> database with an
2592 entry for the new HAL package, and a new target entry will be needed.
2593     </para>
2594     <para>
2595 Obviously a different processor will have different register sets and
2596 calling conventions, so the code for saving and restoring thread
2597 contexts and for implementing <function>setjmp</function> and
2598 <function>longjmp</function> will need to be updated. The exact way of
2599 performing Linux system calls will vary: on x86 linux this usually
2600 involves pushing some registers on the stack and then executing an
2601 <literal>int&nbsp;0x080</literal> trap instruction, but on a different
2602 processor the arguments might be passed in registers instead and
2603 certainly a different trap instruction will be used. The startup code
2604 is written in assembler, but needs to do little more than extract the
2605 process' argument and environment variables and then jump to the main
2606 <function>linux_entry</function> function provided by the
2607 architectural synthetic target HAL package.
2608     </para>
2609     <para>
2610 The header file <filename>hal_io.h</filename> provided by the
2611 architectural HAL package provides various structure definitions,
2612 function prototypes, and macros related to system calls. These are
2613 correct for x86 linux, but there may be problems on other processors.
2614 For example a structure field that is currently defined as a 32-bit
2615 number may in fact may be a 64-bit number instead.
2616     </para>
2617     <para>
2618 The synthetic target's memory map is defined in two files in the
2619 <filename class="directory">include/pkgconf</filename> subdirectory.
2620 For x86 the default memory map involves eight megabytes of read-only
2621 memory for the code at location 0x1000000 and another eight megabytes
2622 for data at 0x2000000. These address ranges may be reserved for other
2623 purposes on the new architecture, so may need changing. There may be
2624 some additional areas of memory allocated by the system for other
2625 purposes, for example the startup stack and any environment variables,
2626 but usually eCos applications can and should ignore those.
2627     </para>
2628     <para>
2629 Other HAL functionality such as interrupt handling, diagnostics, and
2630 the system clock are provided by the architectural HAL package and
2631 should work on different processors with few if any changes. There may
2632 be some problems in the code that interacts with the I/O auxiliary
2633 because of lurking assumptions about endianness or the sizes of
2634 various data types.
2635     </para>
2636     <para>
2637 When porting to other processors, a number of sources of information
2638 are likely to prove useful. Obviously the Linux kernel sources and
2639 header files constitute the ultimate authority on how things work at
2640 the system call level. The GNU C library sources may also prove very
2641 useful: for a normal Linux application it is the C library that
2642 provides the startup code and the system call interface.
2643     </para>
2644   </refsect1>
2645
2646   <refsect1 id="synth-porting-unix"><title>Other Unix Platforms</title>
2647     <para>
2648 Porting to a Unix or Unix-like operating system other than Linux would
2649 be somewhat more involved. The first requirement is toolchains: the
2650 GNU compilers, gcc and g++, must definitely be used; use of other GNU
2651 tools such as the linker may be needed as well, because eCos depends
2652 on functionality such as prioritizing C++ static constructors, and
2653 other linkers may not implement this or may implement it in a
2654 different and incompatible way. A closely related requirement is the
2655 use of ELF format for binary executables: if the operating system
2656 still uses an older format such as COFF then there are likely to be
2657 problems because they do not provide the flexibility required by eCos.
2658     </para>
2659     <para>
2660 In the architectural HAL there should be very little code that is
2661 specific to Linux. Instead the code should work on any operating
2662 system that provides a reasonable implementation of the POSIX
2663 standard. There may be some problems with program startup, but those
2664 could be handled at the architectural level. Some changes may also be
2665 required to the exception handling code. However one file which will
2666 present a problem is <filename>hal_io.h</filename>, which contains
2667 various structure definitions and macros used with the system call
2668 interface. It is likely that many of these definitions will need
2669 changing, and it may well be appropriate to implement variant HAL
2670 packages for the different operating systems where this information
2671 can be separated out. Another possible problem is that the generic
2672 code assumes that system calls such as
2673 <function>cyg_hal_sys_write</function> are available. On an operating
2674 system other than Linux it is possible that some of these are not
2675 simple system calls, and instead wrapper functions will need to be
2676 implemented at the variant HAL level.
2677     </para>
2678     <para>
2679 The generic I/O auxiliary code should be fairly portable to other Unix
2680 platforms. However some of the device drivers may contain code that is
2681 specific to Linux, for example the <literal>PF_PACKET</literal> socket
2682 address family and the ethertap virtual tunnelling interface. These
2683 may prove quite difficult to port.
2684     </para>
2685     <para>
2686 The remaining porting task is to implement one or more platform HAL
2687 packages, one per processor type that is supported. This should
2688 involve much the same work as a port to <link
2689 linkend="synth-porting-linux">another processor running Linux</link>.
2690     </para>
2691     <para>
2692 When using other Unix operating systems the kernel source code may not
2693 be available, which would make any porting effort more challenging.
2694 However there is still a good chance that the GNU C library will have
2695 been ported already, so its source code may contain much useful
2696 information. 
2697     </para>
2698   </refsect1>
2699
2700   <refsect1 id="synth-porting-other"><title>Windows Platforms</title>
2701     <para>
2702 Porting the current synthetic target code to some version of Windows
2703 or to another non-Unix platform is likely to prove very difficult. The
2704 first hurdle that needs to be crossed is the file format for binary
2705 executables: current Windows implementations do not use ELF, instead
2706 they use their own format PE which is a variant of the rather old and
2707 limited COFF format. It may well prove easier to first write an ELF
2708 loader for Windows executables, rather than try to get eCos to work
2709 within the constraints of PE. Of course that introduces new problems,
2710 for example existing source-level debuggers will still expect
2711 executables to be in PE format.
2712     </para>
2713     <para>
2714 Under Linux a synthetic target application is not linked with the
2715 system's C library or any other standard system library. That would
2716 cause confusion, for example both eCos and the system's C library
2717 might try to define the <function>printf</function> function, and
2718 introduce complications such as working with shared libraries. For
2719 much the same reasons, a synthetic target application under Windows
2720 should not be linked with any Windows DLL's. If an ELF loader has been
2721 specially written then this may not be much of a problem.
2722     </para>
2723     <para>
2724 The next big problem is the system call interface. Under Windows
2725 system calls are generally made via DLL's, and it is not clear that
2726 the underlying trap mechanism is well-documented or consistent between
2727 different releases of Windows.
2728     </para>
2729     <para>
2730 The current code depends on the operating system providing an
2731 implementation of POSIX signal handling. This is used for I/O
2732 purposes, for example <literal>SIGALRM</literal> is used for the
2733 system clock, and for exceptions. It is not known what equivalent
2734 functionality is available under Windows.
2735     </para>
2736     <para>
2737 Given the above problems a port of the synthetic target to Windows may
2738 or may not be technically feasible, but it would certainly require a
2739 very large amount of effort.
2740     </para>
2741   </refsect1>
2742
2743 </refentry>
2744
2745 <!-- }}} -->
2746
2747 </part>