1    /*
     2     * Copyright (c) 2011-2013, Texas Instruments Incorporated
     3     * All rights reserved.
     4     *
     5     * Redistribution and use in source and binary forms, with or without
     6     * modification, are permitted provided that the following conditions
     7     * are met:
     8     *
     9     * *  Redistributions of source code must retain the above copyright
    10     *    notice, this list of conditions and the following disclaimer.
    11     *
    12     * *  Redistributions in binary form must reproduce the above copyright
    13     *    notice, this list of conditions and the following disclaimer in the
    14     *    documentation and/or other materials provided with the distribution.
    15     *
    16     * *  Neither the name of Texas Instruments Incorporated nor the names of
    17     *    its contributors may be used to endorse or promote products derived
    18     *    from this software without specific prior written permission.
    19     *
    20     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    21     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    22     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    23     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    24     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    25     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    26     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
    27     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    28     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    29     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    30     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    31     */
    32    /*
    33     *  ======== SysMin.xdc ========
    34     */
    35    
    36    /*!
    37     *  ======== SysMin ========
    38     *
    39     *  Minimal implementation of `{@link ISystemSupport}`.
    40     *
    41     *  This implementation provides a fully functional implementation of
    42     *  all methods specified by `ISystemSupport`.
    43     *
    44     *  The module maintains an internal buffer (with a configurable size)
    45     *  that stores on the "output". When full, the data is over-written.  When
    46     *  `System_flush()` is called the characters in the internal buffer are
    47     *  "output" using the user configuratble `{@link #outputFxn}`.
    48     *
    49     *  As with all `ISystemSupport` modules, this module is the back-end for the
    50     *  `{@link System}` module; application code does not directly call these
    51     *  functions.
    52     */
    53    @Template("./SysMin.xdt")
    54    @ModuleStartup
    55    module SysMin inherits xdc.runtime.ISystemSupport {
    56    
    57        metaonly struct ModuleView {
    58            Ptr outBuf;
    59            UInt outBufIndex;
    60            Bool getTime;   /* set to true for each new trace */
    61            Bool wrapped;    /* has the buffer wrapped */
    62        };
    63    
    64        metaonly struct BufferEntryView {
    65            String entry;
    66        }
    67    
    68        /*!
    69         *  ======== rovViewInfo ========
    70         *  @_nodoc
    71         */
    72        @Facet
    73        metaonly config xdc.rov.ViewInfo.Instance rovViewInfo =
    74            xdc.rov.ViewInfo.create({
    75                viewMap: [
    76                    ['Module',
    77                        {
    78                            type: xdc.rov.ViewInfo.MODULE,
    79                            viewInitFxn: 'viewInitModule',
    80                            structName: 'ModuleView'
    81                        }
    82                    ],
    83                    ['OutputBuffer',
    84                        {
    85                            type: xdc.rov.ViewInfo.MODULE_DATA,
    86                            viewInitFxn: 'viewInitOutputBuffer',
    87                            structName: 'BufferEntryView'
    88                        }
    89                    ]
    90                ]
    91            });
    92    
    93        /*!
    94         *  ======== LINEBUFSIZE ========
    95         *  Size (in MAUs) of the line buffer
    96         *
    97         *  An internal line buffer of this size is allocated. All output is stored
    98         *  in this internal buffer until a new line is output.
    99         */
   100        const SizeT LINEBUFSIZE = 0x100;
   101    
   102        /*!
   103         *  ======== bufSize ========
   104         *  Size (in MAUs) of the output.
   105         *
   106         *  An internal buffer of this size is allocated. All output is stored
   107         *  in this internal buffer.
   108         *
   109         *  If 0 is specified for the size, no buffer is created and ALL
   110         *  tracing is disabled.
   111         */
   112        config SizeT bufSize = 0x1000;
   113    
   114        /*!
   115         *  ======== sectionName ========
   116         *  Section where the internal character output buffer is placed
   117         *
   118         *  The default is to have no explicit placement; i.e., the linker is
   119         *  free to place it anywhere in the memory map.
   120         */
   121        metaonly config String sectionName = null;
   122    
   123        /*!
   124         *  ======== flushAtExit ========
   125         *  Flush the internal buffer during `{@link #exit}` or `{@link #abort}`.
   126         *
   127         *  If the application's target is a TI target, the internal buffer is
   128         *  flushed via the `HOSTwrite` function in the TI C Run Time Support
   129         *  (RTS) library.
   130         *
   131         *  If the application's target is not a TI target, the internal buffer
   132         *  is flushed to `stdout` via `fwrite(..., stdout)`.
   133         *
   134         *  Setting this parameter to `false` reduces the footprint of the
   135         *  application at the expense of not getting output when the application
   136         *  ends via a `System_exit()`, `System_abort()`, `exit()` or `abort()`.
   137         */
   138        config Bool flushAtExit = true;
   139    
   140        /*!
   141         *  ======== abort ========
   142         *  Backend for `{@link System#abort()}`
   143         *
   144         *  This abort function writes the string to the internal
   145         *  output buffer and then gives all internal output to the
   146         *  `{@link #outputFxn}` function if the `{@link #flushAtExit}`
   147         *  configuration parameter is true.
   148         *
   149         *  @param(str)  message to output just prior to aborting
   150         *
   151         *      If non-`NULL`, this string should be output just prior to
   152         *      terminating.
   153         *
   154         *  @see ISystemSupport#abort
   155         */
   156        override Void abort(CString str);
   157    
   158        /*!
   159         *  ======== exit ========
   160         *  Backend for `{@link System#exit()}`
   161         *
   162         *  This exit function gives all internal output to the
   163         *  `{@link #outputFxn}` function if the `{@link #flushAtExit}`
   164         *  configuration parameter is true.
   165         *
   166         *  @see ISystemSupport#exit
   167         */
   168        override Void exit(Int stat);
   169    
   170        /*!
   171         *  ======== flush ========
   172         *  Backend for `{@link System#flush()}`
   173         *
   174         *  The `flush` writes the contents of the internal character buffer
   175         *  via the `{@link #outputFxn}` function.
   176         *
   177         *  @a(Warning)
   178         *  The `{@link System}` gate is used for thread safety during the
   179         *  entire flush operation, so care must be taken when flushing with
   180         *  this `ISystemSupport` module.  Depending on the nature of the
   181         *  `System` gate, your application's interrupt latency
   182         *  may become a function of the `bufSize` parameter!
   183         *
   184         *  @see ISystemSupport#flush
   185         */
   186        override Void flush();
   187    
   188        /*!
   189         *  ======== putch ========
   190         *  Backend for `{@link System#printf()}` and `{@link System#putch()}`
   191         *
   192         *  Places the character into an internal buffer. The `{@link #flush}`
   193         *  sends the internal buffer to the `{@link #outputFxn}` function.
   194         *  The internal buffer is also sent to the `SysMin_outputFxn`
   195         *  function by `{@link #exit}` and `{@link #abort}` if the
   196         *  `{@link #flushAtExit}` configuration parameter is true.
   197         *
   198         *  @see ISystemSupport#putch
   199         */
   200        override Void putch(Char ch);
   201    
   202        /*!
   203         *  ======== ready ========
   204         *  Test if character output can proceed
   205         *
   206         *  This function returns true if the internal buffer is non-zero.
   207         *
   208         *  @see ISystemSupport#ready
   209         */
   210        override Bool ready();
   211    
   212    internal:
   213    
   214        struct LineBuffer {
   215            UInt lineidx;              /* index within linebuf to write next Char */
   216            Char linebuf[LINEBUFSIZE]; /* local line buffer */
   217        }
   218    
   219        struct Module_State {
   220            LineBuffer  lineBuffers[];  /* internal line buffers */
   221            Char        outbuf[];   /* the output buffer */
   222            UInt        outidx;     /* index within outbuf to next Char to write */
   223            Bool        getTime;    /* set to true for each new trace */
   224            Bool        wrapped;    /* has the index (outidx) wrapped */
   225            UInt        writeidx[]; /* index to the last "\n" char */
   226            UInt        readidx[];  /* index to the last char read by external
   227                                     * observer */
   228        }
   229    }