source: GPL/lib32/debug.c@ 18

Last change on this file since 18 was 18, checked in by vladest, 20 years ago

initial import

File size: 17.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
30#define CR 0x0d
31#define LF 0x0a
32
33#define LEADING_ZEROES 0x8000
34#define SIGNIFICANT_FIELD 0x0007
35
36BOOL fLineTerminate=TRUE;
37int DebugLevel = 1;
38
39extern int wrOffset;
40extern char *szprintBuf;
41
42char hextab[]="0123456789ABCDEF";
43
44 //-------------------- DecLongToASCII -
45char * DecLongToASCII(char *StrPtr, ULONG lDecVal,USHORT Option)
46{
47 BOOL fNonZero=FALSE;
48 ULONG Digit;
49 ULONG Power=1000000000; // 1 billion
50 LONG lVal = (LONG)lDecVal;
51
52 if(lVal < 0) {
53 *StrPtr = '-';
54 StrPtr++;
55 lDecVal = -lDecVal;
56 }
57
58 while (Power)
59 {
60 Digit=0; // Digit=lDecVal/Power
61 while (lDecVal >=Power) // replaced with while loop
62 {
63 Digit++;
64 lDecVal-=Power;
65 }
66
67 if (Digit)
68 fNonZero=TRUE;
69
70 if (Digit ||
71 fNonZero ||
72 (Option & LEADING_ZEROES) ||
73 ((Power==1) && (fNonZero==FALSE)))
74 {
75 *StrPtr=(char)('0'+Digit);
76 StrPtr++;
77 }
78
79 if (Power==1000000000) // 1 billion
80 Power=100000000;
81 else if (Power==100000000)
82 Power=10000000;
83 else if (Power==10000000)
84 Power=1000000;
85 else if (Power==1000000)
86 Power=100000;
87 else if (Power==100000)
88 Power=10000;
89 else if (Power==10000)
90 Power=1000;
91 else if (Power==1000)
92 Power=100;
93 else if (Power==100)
94 Power=10;
95 else if (Power==10)
96 Power=1;
97 else
98 Power=0;
99 }
100 return (StrPtr);
101}
102 //-------------------- HexWordToASCII -
103 //-------------------- HexLongToASCII -
104char * HexLongToASCII(char *StrPtr, ULONG wHexVal, USHORT Option)
105{
106 BOOL fNonZero=FALSE;
107 ULONG Digit;
108 ULONG Power=0xF0000000;
109 ULONG ShiftVal=28;
110
111 while (Power)
112 {
113 Digit=(wHexVal & Power)>>ShiftVal;
114 if (Digit)
115 fNonZero=TRUE;
116
117 if (Digit ||
118 fNonZero ||
119 (Option & LEADING_ZEROES) ||
120 ((Power==0x0F) && (fNonZero==FALSE)))
121 *StrPtr++=hextab[Digit];
122
123 if (Power==0xF0000000) // 1 billion
124 Power=0xF000000;
125 else if (Power==0xF000000)
126 Power=0xF00000;
127 else if (Power==0xF00000)
128 Power=0xF0000;
129 else if (Power==0xF0000)
130 Power=0xF000;
131 else if (Power==0xF000)
132 Power=0xF00;
133 else if (Power==0xF00)
134 Power=0xF0;
135 else if (Power==0xF0)
136 Power=0xF;
137 else Power=0;
138
139 ShiftVal-=4;
140 } // end while
141
142 return (StrPtr);
143}
144
145#define DEBUG1
146
147#ifdef DEBUG1
148char BuildString[1024];
149#endif // DEBUG
150
151//------------------------- PrintfOut -
152void _cdecl DPD(int level, char *DbgStr, ...)
153{
154#ifdef DEBUG1
155 char *BuildPtr=BuildString;
156 char *pStr=(char *) DbgStr;
157 char *SubStr;
158 union {
159 void *VoidPtr;
160 USHORT *WordPtr;
161 ULONG *LongPtr;
162 ULONG *StringPtr;
163 } Parm;
164 USHORT wBuildOption;
165
166 Parm.VoidPtr=(void *) &DbgStr;
167 Parm.StringPtr++; // skip size of string pointer
168
169 while (*pStr)
170 {
171 // don't overflow target
172 if (BuildPtr >= (char *) &BuildString[1024-2])
173 break;
174
175 switch (*pStr)
176 {
177 case '%':
178 wBuildOption=0;
179 pStr++;
180 if (*pStr=='0')
181 {
182 wBuildOption|=LEADING_ZEROES;
183 pStr++;
184 }
185 if (*pStr=='u') // always unsigned
186 pStr++;
187 if (*pStr=='#')
188 pStr++;
189
190 switch(*pStr)
191 {
192 case 'x':
193 case 'X':
194 case 'p':
195 case 'P':
196 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
197 pStr++;
198 continue;
199
200 case 'd':
201 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
202 pStr++;
203 continue;
204
205 case 's':
206 SubStr=(char *)*Parm.StringPtr;
207 while (*BuildPtr++ = *SubStr++);
208 Parm.StringPtr++;
209 BuildPtr--; // remove the \0
210 pStr++;
211 continue;
212
213 case 'l':
214 pStr++;
215 switch (*pStr)
216 {
217 case 'x':
218 case 'X':
219 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
220 pStr++;
221 continue;
222
223 case 'd':
224 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
225 pStr++;
226 continue;
227 } // end switch
228 continue; // dunno what he wants
229
230 case 0:
231 continue;
232 } // end switch
233 break;
234
235 case '\\':
236 pStr++;
237 switch (*pStr)
238 {
239 case 'n':
240 *BuildPtr++=LF;
241 pStr++;
242 continue;
243
244 case 'r':
245 *BuildPtr++=CR;
246 pStr++;
247 continue;
248
249 case 0:
250 continue;
251 break;
252 } // end switch
253
254 break;
255 } // end switch
256
257 *BuildPtr++=*pStr++;
258 } // end while
259
260 *BuildPtr=0; // cauterize the string
261 StringOut((char *) BuildString); // print to comm port
262#endif //DEBUG
263}
264
265
266void _cdecl DPE(char *DbgStr, ...)
267{
268#ifdef DEBUG1
269 char *BuildPtr=BuildString;
270 char *pStr = (char *) DbgStr;
271 char *SubStr;
272 union {
273 void *VoidPtr;
274 USHORT *WordPtr;
275 ULONG *LongPtr;
276 ULONG *StringPtr;
277 } Parm;
278 USHORT wBuildOption;
279
280 Parm.VoidPtr=(void *) &DbgStr;
281 Parm.StringPtr++; // skip size of string pointer
282
283 while (*pStr)
284 {
285 // don't overflow target
286 if (BuildPtr >= (char *) &BuildString[1024-2])
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
302 switch(*pStr)
303 {
304 case 'x':
305 case 'X':
306 case 'p':
307 case 'P':
308 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
309 pStr++;
310 continue;
311
312 case 'd':
313 case 'u':
314 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
315 pStr++;
316 continue;
317
318 case 's':
319 SubStr=(char *)*Parm.StringPtr;
320 while (*BuildPtr++ = *SubStr++);
321 Parm.StringPtr++;
322 BuildPtr--; // remove the \0
323 pStr++;
324 continue;
325
326 case 'c':
327 *BuildPtr++ = (char)*Parm.LongPtr;
328 Parm.LongPtr++;
329 pStr++;
330 continue;
331
332 case 'l':
333 pStr++;
334 switch (*pStr)
335 {
336 case 'x':
337 case 'X':
338 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
339 pStr++;
340 continue;
341
342 case 'd':
343 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
344 pStr++;
345 continue;
346 } // end switch
347 continue; // dunno what he wants
348
349 case 0:
350 continue;
351 } // end switch
352 break;
353
354 case '\\':
355 pStr++;
356 switch (*pStr)
357 {
358 case 'n':
359 *BuildPtr++=LF;
360 pStr++;
361 continue;
362
363 case 'r':
364 *BuildPtr++=CR;
365 pStr++;
366 continue;
367
368 case 0:
369 continue;
370 break;
371 } // end switch
372
373 break;
374 } // end switch
375
376 *BuildPtr++=*pStr++;
377 } // end while
378
379 *BuildPtr=0; // cauterize the string
380 StringOut((char *) BuildString); // print to comm port
381#endif //DEBUG
382}
383
384struct snd_info_buffer {
385 char *buffer; /* pointer to begin of buffer */
386 char *curr; /* current position in buffer */
387 unsigned long size; /* current size */
388 unsigned long len; /* total length of buffer */
389 int stop; /* stop flag */
390 int error; /* error code */
391};
392
393typedef struct snd_info_buffer snd_info_buffer_t;
394
395int snd_iprintf(snd_info_buffer_t * buffer, char *fmt,...)
396{
397 char *BuildPtr=buffer->curr;
398 char *pStr=(char *) fmt;
399 char *SubStr;
400 int res;
401 union {
402 void *VoidPtr;
403 USHORT *WordPtr;
404 ULONG *LongPtr;
405 ULONG *StringPtr;
406 } Parm;
407 USHORT wBuildOption;
408
409 Parm.VoidPtr=(void *) &fmt;
410 Parm.StringPtr++; // skip size of string pointer
411
412 if (buffer->stop || buffer->error)
413 return 0;
414
415 while (*pStr)
416 {
417 // don't overflow target
418 if (BuildPtr >= (char *) &buffer->curr[buffer->len - 4])
419 break;
420
421 switch (*pStr)
422 {
423 case '%':
424 wBuildOption=0;
425 pStr++;
426 if (*pStr=='0')
427 {
428 wBuildOption|=LEADING_ZEROES;
429 pStr++;
430 }
431// if (*pStr=='u') // always unsigned
432// pStr++;
433 if (*pStr=='#')
434 pStr++;
435
436 switch(*pStr)
437 {
438 case 'x':
439 case 'X':
440 case 'p':
441 case 'P':
442 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
443 pStr++;
444 continue;
445
446 case 'd':
447 case 'u':
448 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
449 pStr++;
450 continue;
451
452 case 's':
453 SubStr=(char *)*Parm.StringPtr;
454 while (*BuildPtr++ = *SubStr++);
455 Parm.StringPtr++;
456 BuildPtr--; // remove the \0
457 pStr++;
458 continue;
459
460 case 'c':
461 *BuildPtr++ = (char)*Parm.LongPtr;
462 Parm.LongPtr++;
463 pStr++;
464 continue;
465
466 case 'l':
467 pStr++;
468 switch (*pStr)
469 {
470 case 'x':
471 case 'X':
472 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
473 pStr++;
474 continue;
475
476 case 'd':
477 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
478 pStr++;
479 continue;
480 } // end switch
481 continue; // dunno what he wants
482
483 case 0:
484 continue;
485 } // end switch
486 break;
487
488 case '\\':
489 pStr++;
490 switch (*pStr)
491 {
492 case 'n':
493 *BuildPtr++=LF;
494 pStr++;
495 continue;
496
497 case 'r':
498 *BuildPtr++=CR;
499 pStr++;
500 continue;
501
502 case 0:
503 continue;
504 break;
505 } // end switch
506
507 break;
508 } // end switch
509
510 *BuildPtr++=*pStr++;
511 } // end while
512
513 *BuildPtr=0; // cauterize the string
514
515 res = strlen(buffer->curr);
516 if (buffer->size + res >= buffer->len) {
517 buffer->stop = 1;
518 return 0;
519 }
520 buffer->curr += res;
521 buffer->size += res;
522 return res;
523}
524
525#ifdef DEBUG1
526//------------------------- StringOut --------------------------//
527
528#define VMDHA_FIXED 0x0002
529
530extern APIRET VMAlloc(ULONG size, ULONG flags, char NEAR* *pAddr);
531
532/**
533 * Finds the length of a string up to cchMax.
534 * @returns Length.
535 * @param psz Pointer to string.
536 * @param cchMax Max length.
537 */
538static unsigned _strnlen(const char *psz, unsigned cchMax)
539{
540 const char *pszC = psz;
541
542 while (cchMax-- > 0 && *psz != '\0')
543 psz++;
544
545 return psz - pszC;
546}
547
548void StringOut(char *DbgStr)
549{
550 int len, i;
551
552 len= _strnlen( DbgStr, 1024 );
553/*
554 while (*DbgStr)
555 CharOut(*DbgStr++);
556 */
557#ifdef DEBUG
558 for( i= 0; i < len; i++ )
559 CharOut( DbgStr[i] );
560
561 if (fLineTerminate)
562 {
563 CharOut(CR); // append carriage return,
564 CharOut(LF); // linefeed
565 }
566#endif
567 if( szprintBuf == 0 )
568 {
569 VMAlloc( 0x10000, VMDHA_FIXED, &szprintBuf );
570 if( szprintBuf )
571 memset( szprintBuf, 0, 0x10000 );
572 wrOffset= 0;
573 }
574 if( szprintBuf )
575 {
576 if( (len + wrOffset) > 0x10000 )
577 {
578 int cntr;
579 cntr= 0x10000 - wrOffset;
580 memcpy( szprintBuf + wrOffset, DbgStr, cntr );
581 DbgStr+= cntr;
582 len= len - cntr;
583 wrOffset= 0;
584 }
585 if( len )
586 {
587 memcpy( szprintBuf + wrOffset, DbgStr, len );
588 wrOffset= wrOffset + len;
589 if( wrOffset >= 0x10000 )
590 wrOffset= 0;
591 }
592 if (fLineTerminate)
593 {
594// if( (wrOffset+1) >= 0x10000 )
595// wrOffset= 0;
596 szprintBuf[wrOffset]= CR;
597 if( ++wrOffset >= 0x10000 )
598 wrOffset= 0;
599 }
600 }
601}
602#endif
603
604#ifdef DEBUG
605short int MAGIC_COMM_PORT = 0x3f8; // pulled from word ptr 40:0
606
607
608#define UART_DATA 0x00 // UART Data port
609#define UART_INT_ENAB 0x01 // UART Interrupt enable
610#define UART_INT_ID 0x02 // interrupt ID
611#define UART_LINE_CTRL 0x03 // line control registers
612#define UART_MODEM_CTRL 0x04 // modem control register
613#define UART_LINE_STAT 0x05 // line status register
614#define UART_MODEM_STAT 0x06 // modem status regiser
615#define UART_DIVISOR_LO 0x00 // divisor latch least sig
616#define UART_DIVISOR_HI 0x01h // divisor latch most sig
617
618#define DELAY nop
619#else
620short int MAGIC_COMM_PORT = 0x0; // pulled from word ptr 40:0
621#endif
622
623#ifdef DEBUG //--------------------------- CharOut -
624void CharOut(char c)
625{
626 if( MAGIC_COMM_PORT )
627 {
628
629 _asm {
630
631 mov dx, MAGIC_COMM_PORT // address of PS/2's first COM port
632 add dx, UART_LINE_STAT
633
634ReadyCheck:
635 in al, dx // wait for comm port ready signal
636
637 DELAY
638 DELAY
639 DELAY
640
641 test al, 020h
642 jz ReadyCheck
643
644 // Send the character
645
646 add dx, UART_DATA - UART_LINE_STAT
647 mov al,c
648 out dx, al
649
650 DELAY
651 DELAY
652 DELAY
653 }
654 }
655}
656#endif
Note: See TracBrowser for help on using the repository browser.