source: GPL/trunk/lib32/debug.c@ 518

Last change on this file since 518 was 518, checked in by David Azarewicz, 15 years ago

Some of my updates from the 2.1.x branch

File size: 10.2 KB
Line 
1/* $Id: debug.c,v 1.1.1.1 2003/07/02 13:57:02 eleph Exp $ */
2/*
3 * COM port debugging functions
4 *
5 * (C) 2000-2002 InnoTek Systemberatung GmbH
6 * (C) 2000-2001 Sander van Leeuwen (sandervl@xs4all.nl)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
21 * USA.
22 *
23 */
24#define INCL_NOPMAPI
25#define INCL_DOSERRORS // for ERROR_INVALID_FUNCTION
26#include <os2.h>
27
28#include <string.h>
29#include <dbgos2.h>
30
31#define COMM_DEBUG
32
33#define CR 0x0d
34#define LF 0x0a
35
36#define LEADING_ZEROES 0x8000
37#define SIGNIFICANT_FIELD 0x0007
38
39int DebugLevel;
40
41char hextab[]="0123456789ABCDEF";
42
43 //-------------------- DecLongToASCII -
44char * DecLongToASCII(char *StrPtr, ULONG lDecVal,USHORT Option)
45{
46 BOOL fNonZero=FALSE;
47 ULONG Digit;
48 ULONG Power=1000000000; // 1 billion
49 LONG lVal = (LONG)lDecVal;
50
51 if(lVal < 0) {
52 *StrPtr = '-';
53 StrPtr++;
54 lDecVal = -lDecVal;
55 }
56
57 while (Power)
58 {
59 Digit=0; // Digit=lDecVal/Power
60 while (lDecVal >=Power) // replaced with while loop
61 {
62 Digit++;
63 lDecVal-=Power;
64 }
65
66 if (Digit)
67 fNonZero=TRUE;
68
69 if (Digit ||
70 fNonZero ||
71 (Option & LEADING_ZEROES) ||
72 ((Power==1) && (fNonZero==FALSE)))
73 {
74 *StrPtr=(char)('0'+Digit);
75 StrPtr++;
76 }
77
78 if (Power==1000000000) // 1 billion
79 Power=100000000;
80 else if (Power==100000000)
81 Power=10000000;
82 else if (Power==10000000)
83 Power=1000000;
84 else if (Power==1000000)
85 Power=100000;
86 else if (Power==100000)
87 Power=10000;
88 else if (Power==10000)
89 Power=1000;
90 else if (Power==1000)
91 Power=100;
92 else if (Power==100)
93 Power=10;
94 else if (Power==10)
95 Power=1;
96 else
97 Power=0;
98 }
99 return (StrPtr);
100}
101 //-------------------- HexWordToASCII -
102 //-------------------- HexLongToASCII -
103char * HexLongToASCII(char *StrPtr, ULONG wHexVal, USHORT Option)
104{
105 BOOL fNonZero=FALSE;
106 ULONG Digit;
107 ULONG Power=0xF0000000;
108 ULONG ShiftVal=28;
109
110 while (Power)
111 {
112 Digit=(wHexVal & Power)>>ShiftVal;
113 if (Digit)
114 fNonZero=TRUE;
115
116 if (Digit ||
117 fNonZero ||
118 (Option & LEADING_ZEROES) ||
119 ((Power==0x0F) && (fNonZero==FALSE)))
120 *StrPtr++=hextab[Digit];
121
122 if (Power==0xF0000000) // 1 billion
123 Power=0xF000000;
124 else if (Power==0xF000000)
125 Power=0xF00000;
126 else if (Power==0xF00000)
127 Power=0xF0000;
128 else if (Power==0xF0000)
129 Power=0xF000;
130 else if (Power==0xF000)
131 Power=0xF00;
132 else if (Power==0xF00)
133 Power=0xF0;
134 else if (Power==0xF0)
135 Power=0xF;
136 else Power=0;
137
138 ShiftVal-=4;
139 } // end while
140
141 return (StrPtr);
142}
143
144/**
145 * Finds the length of a string up to cchMax.
146 * @returns Length.
147 * @param psz Pointer to string.
148 * @param cchMax Max length.
149 */
150static unsigned _strnlen(const char *psz, unsigned cchMax)
151{
152 const char *pszC = psz;
153
154 while (cchMax-- > 0 && *psz != '\0')
155 psz++;
156
157 return psz - pszC;
158}
159
160#ifdef COMM_DEBUG
161short int MAGIC_COMM_PORT = 0; // pulled from word ptr 40:0
162
163
164#define UART_DATA 0x00 // UART Data port
165#define UART_INT_ENAB 0x01 // UART Interrupt enable
166#define UART_INT_ID 0x02 // interrupt ID
167#define UART_LINE_CTRL 0x03 // line control registers
168#define UART_MODEM_CTRL 0x04 // modem control register
169#define UART_LINE_STAT 0x05 // line status register
170#define UART_MODEM_STAT 0x06 // modem status regiser
171#define UART_DIVISOR_LO 0x00 // divisor latch least sig
172#define UART_DIVISOR_HI 0x01h // divisor latch most sig
173
174#define DELAY nop
175
176void CharOut(char c)
177{
178 if( MAGIC_COMM_PORT )
179 {
180
181 _asm {
182
183 mov dx, MAGIC_COMM_PORT // address of PS/2's first COM port
184 add dx, UART_LINE_STAT
185
186ReadyCheck:
187 in al, dx // wait for comm port ready signal
188
189 DELAY
190 DELAY
191 DELAY
192
193 test al, 020h
194 jz ReadyCheck
195
196 // Send the character
197
198 add dx, UART_DATA - UART_LINE_STAT
199 mov al,c
200 out dx, al
201
202 DELAY
203 DELAY
204 DELAY
205 }
206 }
207}
208#endif
209
210//------------------------- StringOut --------------------------//
211void StringOut(char *DbgStr)
212{
213 int len;
214
215 len= _strnlen( DbgStr, 1024 );
216
217#ifdef COMM_DEBUG
218 if (MAGIC_COMM_PORT) {
219 int i;
220
221 for( i= 0; i < len; i++ ) CharOut( DbgStr[i] );
222
223 CharOut(CR); // append carriage return,
224 CharOut(LF); // linefeed
225 }
226#endif
227
228 if (szprintBuf == 0) return;
229
230 if( (len + wrOffset) > DBG_MAX_BUF_SIZE ) {
231 int cntr;
232 cntr = DBG_MAX_BUF_SIZE - wrOffset;
233 memcpy( szprintBuf + wrOffset, DbgStr, cntr );
234 DbgStr += cntr;
235 len = len - cntr;
236 wrOffset= 0;
237 }
238
239 if ( len ) {
240 memcpy( szprintBuf + wrOffset, DbgStr, len );
241 wrOffset= wrOffset + len;
242 if( wrOffset >= DBG_MAX_BUF_SIZE ) wrOffset= 0;
243 }
244
245 if ( (wrOffset+1) >= DBG_MAX_BUF_SIZE ) wrOffset= 0;
246 szprintBuf[wrOffset]= CR;
247 if ( ++wrOffset >= DBG_MAX_BUF_SIZE ) wrOffset= 0;
248 szprintBuf[wrOffset]= LF;
249 if ( ++wrOffset >= DBG_MAX_BUF_SIZE ) wrOffset= 0;
250}
251
252struct snd_info_buffer {
253 char *buffer; /* pointer to begin of buffer */
254 char *curr; /* current position in buffer */
255 unsigned long size; /* current size */
256 unsigned long len; /* total length of buffer */
257 int stop; /* stop flag */
258 int error; /* error code */
259};
260
261typedef struct snd_info_buffer snd_info_buffer_t;
262
263int snd_iprintf(snd_info_buffer_t * buffer, char *fmt,...)
264{
265 char *BuildPtr=buffer->curr;
266 char *pStr=(char *) fmt;
267 char *SubStr;
268 int res;
269 union {
270 void *VoidPtr;
271 USHORT *WordPtr;
272 ULONG *LongPtr;
273 ULONG *StringPtr;
274 } Parm;
275 USHORT wBuildOption;
276
277 Parm.VoidPtr=(void *) &fmt;
278 Parm.StringPtr++; // skip size of string pointer
279
280 if (buffer->stop || buffer->error)
281 return 0;
282
283 while (*pStr)
284 {
285 // don't overflow target
286 if (BuildPtr >= (char *) &buffer->curr[buffer->len - 4])
287 break;
288
289 switch (*pStr)
290 {
291 case '%':
292 wBuildOption=0;
293 pStr++;
294 if (*pStr=='0')
295 {
296 wBuildOption|=LEADING_ZEROES;
297 pStr++;
298 }
299// if (*pStr=='u') // always unsigned
300// pStr++;
301 if (*pStr=='#')
302 pStr++;
303
304 switch(*pStr)
305 {
306 case 'x':
307 case 'X':
308 case 'p':
309 case 'P':
310 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
311 pStr++;
312 continue;
313
314 case 'd':
315 case 'u':
316 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
317 pStr++;
318 continue;
319
320 case 's':
321 SubStr=(char *)*Parm.StringPtr;
322 while (*BuildPtr++ = *SubStr++);
323 Parm.StringPtr++;
324 BuildPtr--; // remove the \0
325 pStr++;
326 continue;
327
328 case 'c':
329 *BuildPtr++ = (char)*Parm.LongPtr;
330 Parm.LongPtr++;
331 pStr++;
332 continue;
333
334 case 'l':
335 pStr++;
336 switch (*pStr)
337 {
338 case 'x':
339 case 'X':
340 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
341 pStr++;
342 continue;
343
344 case 'd':
345 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
346 pStr++;
347 continue;
348 } // end switch
349 continue; // dunno what he wants
350
351 case 0:
352 continue;
353 } // end switch
354 break;
355
356 case '\\':
357 pStr++;
358 switch (*pStr)
359 {
360 case 'n':
361 *BuildPtr++=LF;
362 pStr++;
363 continue;
364
365 case 'r':
366 *BuildPtr++=CR;
367 pStr++;
368 continue;
369
370 case 0:
371 continue;
372 break;
373 } // end switch
374
375 break;
376 } // end switch
377
378 *BuildPtr++=*pStr++;
379 } // end while
380
381 *BuildPtr=0; // cauterize the string
382
383 res = strlen(buffer->curr);
384 if (buffer->size + res >= buffer->len) {
385 buffer->stop = 1;
386 return 0;
387 }
388 buffer->curr += res;
389 buffer->size += res;
390 return res;
391}
392
Note: See TracBrowser for help on using the repository browser.