source: sbliveos2/trunk/lib32/debug.c@ 724

Last change on this file since 724 was 562, checked in by rudi, 14 years ago

SBLliveOS2: Forgot to move soem defines

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.7 KB
Line 
1/* $Id: debug.c 562 2011-07-28 09:21:19Z rudi $ */
2
3/******************************************************************************
4* Jazz16 Physical Device Driver
5* Production code and toolkit sample
6*
7* (c) Copyright IBM Corporation 1993. All rights reserved
8* (c) Copyright Media Vision Corporation 1993, 1994. All rights reserved
9*
10* DISCLAIMER OF WARRANTIES. The following [enclosed] code is
11* sample code created by IBM Corporation and Media Vision Corporation.
12* It is provided to you solely for the purpose of assisting you in the
13* development of your applications.
14* The code is provided "AS IS", without warranty of any kind.
15* IBM and Media Vision shall not be liable for any damages arising out of
16* your use of the sample code, even if they have been advised of the
17* possibility of such damages.
18*
19*******************************************************************************
20*
21* commdbg.c = debug routines to send data to COM: port
22*
23* This file contains routines to send debug information to COM: ports
24* during execution of device driver.
25* The routines provide 'C' printf like functions that send their output
26* to the serial port rather than stdout.
27* The calls to these routines are only called for debug versions of the
28* driver. See file debug.h for flags to turn on/off different debug options.
29*
30* MODIFICATION HISTORY:
31* DATE CHANGE DESCRIPTION
32* unknown Creation (Media Vision)
33* 07/30/93 Add headers for toolkit
34* 09/21/93 Header modifications
35******************************************************************************/
36
37#define INCL_NOPMAPI
38#define INCL_DOSERRORS // for ERROR_INVALID_FUNCTION
39#include <os2.h>
40
41
42#define CR 0x0d
43#define LF 0x0a
44
45
46#define LEADING_ZEROES 0x8000
47#define SIGNIFICANT_FIELD 0x0007
48
49BOOL fLineTerminate=TRUE;
50
51char hextab[]="0123456789ABCDEF";
52
53#ifdef DEBUG
54//#define MAGIC_COMM_PORT 0x3f8 // pulled from word ptr 40:0
55#define MAGIC_COMM_PORT 0x2f8 // pulled from word ptr 40:0
56
57
58#define UART_DATA 0x00 // UART Data port
59#define UART_INT_ENAB 0x01 // UART Interrupt enable
60#define UART_INT_ID 0x02 // interrupt ID
61#define UART_LINE_CTRL 0x03 // line control registers
62#define UART_MODEM_CTRL 0x04 // modem control register
63#define UART_LINE_STAT 0x05 // line status register
64#define UART_MODEM_STAT 0x06 // modem status regiser
65#define UART_DIVISOR_LO 0x00 // divisor latch least sig
66#define UART_DIVISOR_HI 0x01h // divisor latch most sig
67
68#define DELAY nop
69#endif
70
71#ifdef DEBUG //--------------------------- CharOut -
72void CharOut(char c)
73{
74 _asm {
75
76 mov dx, MAGIC_COMM_PORT // address of PS/2's first COM port
77 add dx, UART_LINE_STAT
78
79ReadyCheck:
80 in al, dx // wait for comm port ready signal
81
82 DELAY
83 DELAY
84 DELAY
85
86 test al, 020h
87 jz ReadyCheck
88
89 // Send the character
90
91 add dx, UART_DATA - UART_LINE_STAT
92 mov al,c
93 out dx, al
94
95 DELAY
96 DELAY
97 DELAY
98 }
99}
100#endif
101
102#ifdef DEBUG //------------------------- StringOut -
103void StringOut(char *DbgStr)
104{
105 while (*DbgStr)
106 CharOut(*DbgStr++);
107
108 if (fLineTerminate)
109 {
110 CharOut(CR); // append carriage return,
111 CharOut(LF); // linefeed
112 }
113}
114#endif
115
116 //-------------------- DecLongToASCII -
117char * DecLongToASCII(char *StrPtr, ULONG lDecVal,USHORT Option)
118{
119 BOOL fNonZero=FALSE;
120 ULONG Digit;
121 ULONG Power=1000000000; // 1 billion
122
123 while (Power)
124 {
125 Digit=0; // Digit=lDecVal/Power
126 while (lDecVal >=Power) // replaced with while loop
127 {
128 Digit++;
129 lDecVal-=Power;
130 }
131
132 if (Digit)
133 fNonZero=TRUE;
134
135 if (Digit ||
136 fNonZero ||
137 (Option & LEADING_ZEROES) ||
138 ((Power==1) && (fNonZero==FALSE)))
139 {
140 *StrPtr=(char)('0'+Digit);
141 StrPtr++;
142 }
143
144 if (Power==1000000000) // 1 billion
145 Power=100000000;
146 else if (Power==100000000)
147 Power=10000000;
148 else if (Power==10000000)
149 Power=1000000;
150 else if (Power==1000000)
151 Power=100000;
152 else if (Power==100000)
153 Power=10000;
154 else if (Power==10000)
155 Power=1000;
156 else if (Power==1000)
157 Power=100;
158 else if (Power==100)
159 Power=10;
160 else if (Power==10)
161 Power=1;
162 else
163 Power=0;
164 }
165 return (StrPtr);
166}
167 //-------------------- HexWordToASCII -
168 //-------------------- HexLongToASCII -
169char * HexLongToASCII(char *StrPtr, ULONG wHexVal, USHORT Option)
170{
171 BOOL fNonZero=FALSE;
172 ULONG Digit;
173 ULONG Power=0xF0000000;
174 ULONG ShiftVal=28;
175
176 while (Power)
177 {
178 Digit=(wHexVal & Power)>>ShiftVal;
179 if (Digit)
180 fNonZero=TRUE;
181
182 if (Digit ||
183 fNonZero ||
184 (Option & LEADING_ZEROES) ||
185 ((Power==0x0F) && (fNonZero==FALSE)))
186 *StrPtr++=hextab[Digit];
187
188 if (Power==0xF0000000) // 1 billion
189 Power=0xF000000;
190 else if (Power==0xF000000)
191 Power=0xF00000;
192 else if (Power==0xF00000)
193 Power=0xF0000;
194 else if (Power==0xF0000)
195 Power=0xF000;
196 else if (Power==0xF000)
197 Power=0xF00;
198 else if (Power==0xF00)
199 Power=0xF0;
200 else if (Power==0xF0)
201 Power=0xF;
202 else Power=0;
203
204 ShiftVal-=4;
205 } // end while
206
207 return (StrPtr);
208}
209
210#ifdef DEBUG
211char BuildString[1024];
212#endif // DEBUG
213
214//------------------------- PrintfOut -
215void _cdecl DPD(int level, char *DbgStr, ...)
216{
217#ifdef DEBUG
218 char *BuildPtr=BuildString;
219 char *pStr=(char *) DbgStr;
220 char *SubStr;
221 union {
222 void *VoidPtr;
223 USHORT *WordPtr;
224 ULONG *LongPtr;
225 ULONG *StringPtr;
226 } Parm;
227 USHORT wBuildOption;
228
229 Parm.VoidPtr=(void *) &DbgStr;
230 Parm.StringPtr++; // skip size of string pointer
231
232 while (*pStr)
233 {
234 // don't overflow target
235 if (BuildPtr >= (char *) &BuildString[1024-2])
236 break;
237
238 switch (*pStr)
239 {
240 case '%':
241 wBuildOption=0;
242 pStr++;
243 if (*pStr=='0')
244 {
245 wBuildOption|=LEADING_ZEROES;
246 pStr++;
247 }
248 if (*pStr=='u') // always unsigned
249 pStr++;
250 if (*pStr=='#')
251 pStr++;
252
253 switch(*pStr)
254 {
255 case 'x':
256 case 'X':
257 case 'p':
258 case 'P':
259 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
260 pStr++;
261 continue;
262
263 case 'd':
264 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
265 pStr++;
266 continue;
267
268 case 's':
269 SubStr=(char *)*Parm.StringPtr;
270 while (*BuildPtr++ = *SubStr++);
271 Parm.StringPtr++;
272 BuildPtr--; // remove the \0
273 pStr++;
274 continue;
275
276 case 'l':
277 pStr++;
278 switch (*pStr)
279 {
280 case 'x':
281 case 'X':
282 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
283 pStr++;
284 continue;
285
286 case 'd':
287 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
288 pStr++;
289 continue;
290 } // end switch
291 continue; // dunno what he wants
292
293 case 0:
294 continue;
295 } // end switch
296 break;
297
298 case '\\':
299 pStr++;
300 switch (*pStr)
301 {
302 case 'n':
303 *BuildPtr++=LF;
304 pStr++;
305 continue;
306
307 case 'r':
308 *BuildPtr++=CR;
309 pStr++;
310 continue;
311
312 case 0:
313 continue;
314 break;
315 } // end switch
316
317 break;
318 } // end switch
319
320 *BuildPtr++=*pStr++;
321 } // end while
322
323 *BuildPtr=0; // cauterize the string
324 StringOut((char *) BuildString); // print to comm port
325#endif //DEBUG
326}
327
328
329void _cdecl DPE(char *DbgStr, ...)
330{
331#ifdef DEBUG
332 char *BuildPtr=BuildString;
333 char *pStr = (char *) DbgStr;
334 char *SubStr;
335 union {
336 void *VoidPtr;
337 USHORT *WordPtr;
338 ULONG *LongPtr;
339 ULONG *StringPtr;
340 } Parm;
341 USHORT wBuildOption;
342
343 Parm.VoidPtr=(void *) &DbgStr;
344 Parm.StringPtr++; // skip size of string pointer
345
346 while (*pStr)
347 {
348 // don't overflow target
349 if (BuildPtr >= (char *) &BuildString[1024-2])
350 break;
351
352 switch (*pStr)
353 {
354 case '%':
355 wBuildOption=0;
356 pStr++;
357 if (*pStr=='0')
358 {
359 wBuildOption|=LEADING_ZEROES;
360 pStr++;
361 }
362 if (*pStr=='u') // always unsigned
363 pStr++;
364
365 switch(*pStr)
366 {
367 case 'x':
368 case 'X':
369 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
370 pStr++;
371 continue;
372
373 case 'd':
374 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
375 pStr++;
376 continue;
377
378 case 's':
379 SubStr=(char *)*Parm.StringPtr;
380 while (*BuildPtr++ = *SubStr++);
381 Parm.StringPtr++;
382 BuildPtr--; // remove the \0
383 pStr++;
384 continue;
385
386 case 'l':
387 pStr++;
388 switch (*pStr)
389 {
390 case 'x':
391 case 'X':
392 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
393 pStr++;
394 continue;
395
396 case 'd':
397 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
398 pStr++;
399 continue;
400 } // end switch
401 continue; // dunno what he wants
402
403 case 0:
404 continue;
405 } // end switch
406 break;
407
408 case '\\':
409 pStr++;
410 switch (*pStr)
411 {
412 case 'n':
413 *BuildPtr++=LF;
414 pStr++;
415 continue;
416
417 case 'r':
418 *BuildPtr++=CR;
419 pStr++;
420 continue;
421
422 case 0:
423 continue;
424 break;
425 } // end switch
426
427 break;
428 } // end switch
429
430 *BuildPtr++=*pStr++;
431 } // end while
432
433 *BuildPtr=0; // cauterize the string
434 StringOut((char *) BuildString); // print to comm port
435#endif //DEBUG
436}
437
Note: See TracBrowser for help on using the repository browser.