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