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