1 /*
2 * Copyright (c) 2008-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.xs ========
34 */
36 var SysMin = this;
37 var Core = null;
39 /*
40 * ======== module$static$init ========
41 */
42 function module$static$init(obj, params)
43 {
44 var segname = Program.sectMap[".tracebuf"];
45 var segment = Program.cpu.memoryMap[segname];
47 if (params.bufSize > segment.len) {
48 this.$logError("bufSize 0x" + Number(params.bufSize).toString(16) +
49 " configured is too large, maximum bufSize allowed is " +
50 "0x" + Number(segment.len).toString(16) + ". Decrement" +
51 " the bufSize appropriately.", this);
52 }
54 if (params.bufSize - 8 < this.LINEBUFSIZE) {
55 this.$logError("Buffer Size need to be larger than line buffer of size"
56 + " = 0x" + Number(this.LINEBUFSIZE).toString(16), this);
57 }
59 if (Program.platformName.match(/ipu/)) {
60 obj.lineBuffers.length = Core.numCores;
61 }
62 else {
63 obj.lineBuffers.length = 1;
64 }
65 for (var i = 0; i < obj.lineBuffers.length; i++) {
66 obj.lineBuffers[i].lineidx = 0;
67 for (var j = 0; j < this.LINEBUFSIZE; j++) {
68 obj.lineBuffers[i].linebuf[j] = 0;
69 }
70 }
72 obj.outbuf.length = params.bufSize - 8;
73 obj.writeidx.length = 0x1;
74 obj.readidx.length = 0x1;
75 if (params.bufSize != 0) {
76 var Memory = xdc.module('xdc.runtime.Memory');
77 Memory.staticPlace(obj.outbuf, 0x1000, params.sectionName);
78 Memory.staticPlace(obj.writeidx, 0x4, params.sectionName);
79 Memory.staticPlace(obj.readidx, 0x4, params.sectionName);
80 }
82 obj.outidx = 0;
83 obj.getTime = false;
84 obj.wrapped = false;
85 }
87 /*
88 * ======== module$use ========
89 */
90 function module$use(obj, params)
91 {
92 if (Program.platformName.match(/ipu/)) {
93 Core = xdc.module("ti.sysbios.hal.Core");
94 }
95 }
97 /*
98 * ======== viewInitModule ========
99 *
100 */
101 function viewInitModule(view, mod)
102 {
103 view.outBuf = mod.outbuf;
104 view.outBufIndex = mod.outidx;
105 view.wrapped = mod.wrapped;
106 }
108 /*
109 * ======== viewInitOutputBuffer ========
110 * Displays the contents of SysMin's output buffer in ROV.
111 */
112 function viewInitOutputBuffer(view)
113 {
114 var Program = xdc.useModule('xdc.rov.Program');
116 /*
117 * Retrieve the module's state.
118 * If this throws an exception, just allow it to propogate up.
119 */
120 var rawView = Program.scanRawView('ti.trace.SysMin');
122 /*
123 * If the buffer has not wrapped and the index of the next character to
124 * write is 0, then the buffer is empty, and we can just return.
125 */
126 if (!rawView.modState.wrapped && (rawView.modState.outidx == 0)) {
127 view.elements = new Array();
128 return;
129 }
131 /* Get the buffer size from the configuration. */
132 var bufSize = Program.getModuleConfig('ti.trace.SysMin').bufSize;
134 /* Read in the outbuf */
135 var outbuf = null;
136 try {
137 outbuf = Program.fetchArray(rawView.modState.outbuf$fetchDesc,
138 rawView.modState.outbuf, bufSize);
139 }
140 /* If there's a problem, just re-throw the exception. */
141 catch (e) {
142 throw ("Problem reading output buffer: " + e.toString());
143 }
145 /*
146 * We will create a new view element for each string terminated in a
147 * newline.
148 */
149 var elements = new Array();
151 /* Leftover characters from each read which did not end in a newline. */
152 var leftover = "";
154 /* If the output buffer has wrapped... */
155 if (rawView.modState.wrapped) {
156 /* Read from outidx to the end of the buffer. */
157 var leftover = readChars("", outbuf, rawView.modState.outidx,
158 outbuf.length - 1, elements);
159 }
161 /* Read from the beginning of the buffer to outidx */
162 leftover = readChars(leftover, outbuf, 0, rawView.modState.outidx - 1,
163 elements);
165 /*
166 * If there are any leftover characters not terminated in a newline,
167 * create an element for these and display them.
168 */
169 if (leftover != "") {
170 var elem = Program.newViewStruct('ti.trace.SysMin',
171 'OutputBuffer');
172 elem.entry = leftover;
173 elements[elements.length] = elem;
174 }
176 view.elements = elements;
177 }
179 /*
180 * ======== readChars ========
181 * Reads characters from 'buffer' from index 'begin' to 'end' and adds
182 * any newline-terminated strings as elements to the 'elements' array.
183 * If the last character is not a newline, this function returns what it's
184 * read from the "incomplete" string.
185 * The string 'leftover', the leftover incomplete string from the previous
186 * call, is prepended to the first string read.
187 */
188 function readChars(leftover, buffer, begin, end, elements)
189 {
190 /* Start with the previous incomplete string. */
191 var str = leftover;
193 /* For each of the specified characters... */
194 for (var i = begin; i <= end; i++) {
196 /* Convert the target values to characters. */
197 var ch = String.fromCharCode(buffer[i]);
199 /* Add the character to the current string. */
200 str += ch;
202 /*
203 * If a string ends in a newline, create a separate table entry for it.
204 */
205 if (ch == '\n') {
206 /*
207 * Create a view structure to display the string, and add
208 * it to the table.
209 */
210 var elem = Program.newViewStruct('ti.trace.SysMin',
211 'OutputBuffer');
212 elem.entry = str;
213 elements[elements.length] = elem;
215 /* Reset the string */
216 str = "";
217 }
218 }
220 /* Return any unfinished string characters. */
221 return (str);
222 }