source: GPL/branches/uniaud32-2.1.x/lib32/debug.c@ 504

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

Bring branch up-to-date with trunk

File size: 16.3 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
252char BuildString[1024];
253
254#if 0
255//------------------------- PrintfOut -
256void _cdecl DPD(int level, char *DbgStr, ...)
257{
258 char *BuildPtr=BuildString;
259 char *pStr=(char *) DbgStr;
260 char *SubStr;
261 union {
262 void *VoidPtr;
263 USHORT *WordPtr;
264 ULONG *LongPtr;
265 ULONG *StringPtr;
266 } Parm;
267 USHORT wBuildOption;
268
269 Parm.VoidPtr=(void *) &DbgStr;
270 Parm.StringPtr++; // skip size of string pointer
271
272 while (*pStr)
273 {
274 // don't overflow target
275 if (BuildPtr >= (char *) &BuildString[1024-2])
276 break;
277
278 switch (*pStr)
279 {
280 case '%':
281 wBuildOption=0;
282 pStr++;
283 if (*pStr=='0')
284 {
285 wBuildOption|=LEADING_ZEROES;
286 pStr++;
287 }
288 if (*pStr=='u') // always unsigned
289 pStr++;
290 if (*pStr=='#')
291 pStr++;
292
293 switch(*pStr)
294 {
295 case 'x':
296 case 'X':
297 case 'p':
298 case 'P':
299 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
300 pStr++;
301 continue;
302
303 case 'd':
304 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
305 pStr++;
306 continue;
307
308 case 's':
309 SubStr=(char *)*Parm.StringPtr;
310 while (*BuildPtr++ = *SubStr++);
311 Parm.StringPtr++;
312 BuildPtr--; // remove the \0
313 pStr++;
314 continue;
315
316 case 'l':
317 pStr++;
318 switch (*pStr)
319 {
320 case 'x':
321 case 'X':
322 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
323 pStr++;
324 continue;
325
326 case 'd':
327 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
328 pStr++;
329 continue;
330 } // end switch
331 continue; // dunno what he wants
332
333 case 0:
334 continue;
335 } // end switch
336 break;
337
338 case '\\':
339 pStr++;
340 switch (*pStr)
341 {
342 case 'n':
343 *BuildPtr++=LF;
344 pStr++;
345 continue;
346
347 case 'r':
348 *BuildPtr++=CR;
349 pStr++;
350 continue;
351
352 case 0:
353 continue;
354 break;
355 } // end switch
356
357 break;
358 } // end switch
359
360 *BuildPtr++=*pStr++;
361 } // end while
362
363 *BuildPtr=0; // cauterize the string
364 StringOut((char *) BuildString);
365}
366#endif
367
368void _cdecl DPE(char *DbgStr, ...)
369{
370 char *BuildPtr=BuildString;
371 char *pStr = (char *) DbgStr;
372 char *SubStr;
373 union {
374 void *VoidPtr;
375 USHORT *WordPtr;
376 ULONG *LongPtr;
377 ULONG *StringPtr;
378 } Parm;
379 USHORT wBuildOption;
380
381 Parm.VoidPtr=(void *) &DbgStr;
382 Parm.StringPtr++; // skip size of string pointer
383
384 while (*pStr)
385 {
386 // don't overflow target
387 if (BuildPtr >= (char *) &BuildString[1024-2])
388 break;
389
390 switch (*pStr)
391 {
392 case '%':
393 wBuildOption=0;
394 pStr++;
395 if (*pStr=='0')
396 {
397 wBuildOption|=LEADING_ZEROES;
398 pStr++;
399 }
400// if (*pStr=='u') // always unsigned
401// pStr++;
402
403 switch(*pStr)
404 {
405 case 'x':
406 case 'X':
407 case 'p':
408 case 'P':
409 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
410 pStr++;
411 continue;
412
413 case 'd':
414 case 'u':
415 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
416 pStr++;
417 continue;
418
419 case 's':
420 SubStr=(char *)*Parm.StringPtr;
421 while (*BuildPtr++ = *SubStr++);
422 Parm.StringPtr++;
423 BuildPtr--; // remove the \0
424 pStr++;
425 continue;
426
427 case 'c':
428 *BuildPtr++ = (char)*Parm.LongPtr;
429 Parm.LongPtr++;
430 pStr++;
431 continue;
432
433 case 'l':
434 pStr++;
435 switch (*pStr)
436 {
437 case 'x':
438 case 'X':
439 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
440 pStr++;
441 continue;
442
443 case 'd':
444 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
445 pStr++;
446 continue;
447 } // end switch
448 continue; // dunno what he wants
449
450 case 0:
451 continue;
452 } // end switch
453 break;
454
455 case '\\':
456 pStr++;
457 switch (*pStr)
458 {
459 case 'n':
460 *BuildPtr++=LF;
461 pStr++;
462 continue;
463
464 case 'r':
465 *BuildPtr++=CR;
466 pStr++;
467 continue;
468
469 case 0:
470 continue;
471 break;
472 } // end switch
473
474 break;
475 } // end switch
476
477 *BuildPtr++=*pStr++;
478 } // end while
479
480 *BuildPtr=0; // cauterize the string
481 StringOut((char *) BuildString);
482}
483
484struct snd_info_buffer {
485 char *buffer; /* pointer to begin of buffer */
486 char *curr; /* current position in buffer */
487 unsigned long size; /* current size */
488 unsigned long len; /* total length of buffer */
489 int stop; /* stop flag */
490 int error; /* error code */
491};
492
493typedef struct snd_info_buffer snd_info_buffer_t;
494
495int snd_iprintf(snd_info_buffer_t * buffer, char *fmt,...)
496{
497 char *BuildPtr=buffer->curr;
498 char *pStr=(char *) fmt;
499 char *SubStr;
500 int res;
501 union {
502 void *VoidPtr;
503 USHORT *WordPtr;
504 ULONG *LongPtr;
505 ULONG *StringPtr;
506 } Parm;
507 USHORT wBuildOption;
508
509 Parm.VoidPtr=(void *) &fmt;
510 Parm.StringPtr++; // skip size of string pointer
511
512 if (buffer->stop || buffer->error)
513 return 0;
514
515 while (*pStr)
516 {
517 // don't overflow target
518 if (BuildPtr >= (char *) &buffer->curr[buffer->len - 4])
519 break;
520
521 switch (*pStr)
522 {
523 case '%':
524 wBuildOption=0;
525 pStr++;
526 if (*pStr=='0')
527 {
528 wBuildOption|=LEADING_ZEROES;
529 pStr++;
530 }
531// if (*pStr=='u') // always unsigned
532// pStr++;
533 if (*pStr=='#')
534 pStr++;
535
536 switch(*pStr)
537 {
538 case 'x':
539 case 'X':
540 case 'p':
541 case 'P':
542 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
543 pStr++;
544 continue;
545
546 case 'd':
547 case 'u':
548 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
549 pStr++;
550 continue;
551
552 case 's':
553 SubStr=(char *)*Parm.StringPtr;
554 while (*BuildPtr++ = *SubStr++);
555 Parm.StringPtr++;
556 BuildPtr--; // remove the \0
557 pStr++;
558 continue;
559
560 case 'c':
561 *BuildPtr++ = (char)*Parm.LongPtr;
562 Parm.LongPtr++;
563 pStr++;
564 continue;
565
566 case 'l':
567 pStr++;
568 switch (*pStr)
569 {
570 case 'x':
571 case 'X':
572 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
573 pStr++;
574 continue;
575
576 case 'd':
577 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
578 pStr++;
579 continue;
580 } // end switch
581 continue; // dunno what he wants
582
583 case 0:
584 continue;
585 } // end switch
586 break;
587
588 case '\\':
589 pStr++;
590 switch (*pStr)
591 {
592 case 'n':
593 *BuildPtr++=LF;
594 pStr++;
595 continue;
596
597 case 'r':
598 *BuildPtr++=CR;
599 pStr++;
600 continue;
601
602 case 0:
603 continue;
604 break;
605 } // end switch
606
607 break;
608 } // end switch
609
610 *BuildPtr++=*pStr++;
611 } // end while
612
613 *BuildPtr=0; // cauterize the string
614
615 res = strlen(buffer->curr);
616 if (buffer->size + res >= buffer->len) {
617 buffer->stop = 1;
618 return 0;
619 }
620 buffer->curr += res;
621 buffer->size += res;
622 return res;
623}
624
Note: See TracBrowser for help on using the repository browser.