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

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

SBLliveOS2: Adapt to newer OpenWatcom versions

  • 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 561 2011-07-28 09:16:56Z 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 //--------------------------- CharOut -
54void CharOut(char c)
55{
56 _asm {
57
58 mov dx, MAGIC_COMM_PORT // address of PS/2's first COM port
59 add dx, UART_LINE_STAT
60
61ReadyCheck:
62 in al, dx // wait for comm port ready signal
63
64 DELAY
65 DELAY
66 DELAY
67
68 test al, 020h
69 jz ReadyCheck
70
71 // Send the character
72
73 add dx, UART_DATA - UART_LINE_STAT
74 mov al,c
75 out dx, al
76
77 DELAY
78 DELAY
79 DELAY
80 }
81}
82#endif
83
84#ifdef DEBUG //------------------------- StringOut -
85void StringOut(char *DbgStr)
86{
87 while (*DbgStr)
88 CharOut(*DbgStr++);
89
90 if (fLineTerminate)
91 {
92 CharOut(CR); // append carriage return,
93 CharOut(LF); // linefeed
94 }
95}
96#endif
97
98
99 //-------------------- DecLongToASCII -
100char * DecLongToASCII(char *StrPtr, ULONG lDecVal,USHORT Option)
101{
102 BOOL fNonZero=FALSE;
103 ULONG Digit;
104 ULONG Power=1000000000; // 1 billion
105
106 while (Power)
107 {
108 Digit=0; // Digit=lDecVal/Power
109 while (lDecVal >=Power) // replaced with while loop
110 {
111 Digit++;
112 lDecVal-=Power;
113 }
114
115 if (Digit)
116 fNonZero=TRUE;
117
118 if (Digit ||
119 fNonZero ||
120 (Option & LEADING_ZEROES) ||
121 ((Power==1) && (fNonZero==FALSE)))
122 {
123 *StrPtr=(char)('0'+Digit);
124 StrPtr++;
125 }
126
127 if (Power==1000000000) // 1 billion
128 Power=100000000;
129 else if (Power==100000000)
130 Power=10000000;
131 else if (Power==10000000)
132 Power=1000000;
133 else if (Power==1000000)
134 Power=100000;
135 else if (Power==100000)
136 Power=10000;
137 else if (Power==10000)
138 Power=1000;
139 else if (Power==1000)
140 Power=100;
141 else if (Power==100)
142 Power=10;
143 else if (Power==10)
144 Power=1;
145 else
146 Power=0;
147 }
148 return (StrPtr);
149}
150 //-------------------- HexWordToASCII -
151 //-------------------- HexLongToASCII -
152char * HexLongToASCII(char *StrPtr, ULONG wHexVal, USHORT Option)
153{
154 BOOL fNonZero=FALSE;
155 ULONG Digit;
156 ULONG Power=0xF0000000;
157 ULONG ShiftVal=28;
158
159 while (Power)
160 {
161 Digit=(wHexVal & Power)>>ShiftVal;
162 if (Digit)
163 fNonZero=TRUE;
164
165 if (Digit ||
166 fNonZero ||
167 (Option & LEADING_ZEROES) ||
168 ((Power==0x0F) && (fNonZero==FALSE)))
169 *StrPtr++=hextab[Digit];
170
171 if (Power==0xF0000000) // 1 billion
172 Power=0xF000000;
173 else if (Power==0xF000000)
174 Power=0xF00000;
175 else if (Power==0xF00000)
176 Power=0xF0000;
177 else if (Power==0xF0000)
178 Power=0xF000;
179 else if (Power==0xF000)
180 Power=0xF00;
181 else if (Power==0xF00)
182 Power=0xF0;
183 else if (Power==0xF0)
184 Power=0xF;
185 else Power=0;
186
187 ShiftVal-=4;
188 } // end while
189
190 return (StrPtr);
191}
192
193#ifdef DEBUG
194char BuildString[1024];
195#endif // DEBUG
196
197//------------------------- PrintfOut -
198void _cdecl DPD(int level, char *DbgStr, ...)
199{
200#ifdef DEBUG
201 char *BuildPtr=BuildString;
202 char *pStr=(char *) DbgStr;
203 char *SubStr;
204 union {
205 void *VoidPtr;
206 USHORT *WordPtr;
207 ULONG *LongPtr;
208 ULONG *StringPtr;
209 } Parm;
210 USHORT wBuildOption;
211
212 Parm.VoidPtr=(void *) &DbgStr;
213 Parm.StringPtr++; // skip size of string pointer
214
215 while (*pStr)
216 {
217 // don't overflow target
218 if (BuildPtr >= (char *) &BuildString[1024-2])
219 break;
220
221 switch (*pStr)
222 {
223 case '%':
224 wBuildOption=0;
225 pStr++;
226 if (*pStr=='0')
227 {
228 wBuildOption|=LEADING_ZEROES;
229 pStr++;
230 }
231 if (*pStr=='u') // always unsigned
232 pStr++;
233 if (*pStr=='#')
234 pStr++;
235
236 switch(*pStr)
237 {
238 case 'x':
239 case 'X':
240 case 'p':
241 case 'P':
242 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
243 pStr++;
244 continue;
245
246 case 'd':
247 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
248 pStr++;
249 continue;
250
251 case 's':
252 SubStr=(char *)*Parm.StringPtr;
253 while (*BuildPtr++ = *SubStr++);
254 Parm.StringPtr++;
255 BuildPtr--; // remove the \0
256 pStr++;
257 continue;
258
259 case 'l':
260 pStr++;
261 switch (*pStr)
262 {
263 case 'x':
264 case 'X':
265 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
266 pStr++;
267 continue;
268
269 case 'd':
270 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
271 pStr++;
272 continue;
273 } // end switch
274 continue; // dunno what he wants
275
276 case 0:
277 continue;
278 } // end switch
279 break;
280
281 case '\\':
282 pStr++;
283 switch (*pStr)
284 {
285 case 'n':
286 *BuildPtr++=LF;
287 pStr++;
288 continue;
289
290 case 'r':
291 *BuildPtr++=CR;
292 pStr++;
293 continue;
294
295 case 0:
296 continue;
297 break;
298 } // end switch
299
300 break;
301 } // end switch
302
303 *BuildPtr++=*pStr++;
304 } // end while
305
306 *BuildPtr=0; // cauterize the string
307 StringOut((char *) BuildString); // print to comm port
308#endif //DEBUG
309}
310
311
312void _cdecl DPE(char *DbgStr, ...)
313{
314#ifdef DEBUG
315 char *BuildPtr=BuildString;
316 char *pStr = (char *) DbgStr;
317 char *SubStr;
318 union {
319 void *VoidPtr;
320 USHORT *WordPtr;
321 ULONG *LongPtr;
322 ULONG *StringPtr;
323 } Parm;
324 USHORT wBuildOption;
325
326 Parm.VoidPtr=(void *) &DbgStr;
327 Parm.StringPtr++; // skip size of string pointer
328
329 while (*pStr)
330 {
331 // don't overflow target
332 if (BuildPtr >= (char *) &BuildString[1024-2])
333 break;
334
335 switch (*pStr)
336 {
337 case '%':
338 wBuildOption=0;
339 pStr++;
340 if (*pStr=='0')
341 {
342 wBuildOption|=LEADING_ZEROES;
343 pStr++;
344 }
345 if (*pStr=='u') // always unsigned
346 pStr++;
347
348 switch(*pStr)
349 {
350 case 'x':
351 case 'X':
352 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
353 pStr++;
354 continue;
355
356 case 'd':
357 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
358 pStr++;
359 continue;
360
361 case 's':
362 SubStr=(char *)*Parm.StringPtr;
363 while (*BuildPtr++ = *SubStr++);
364 Parm.StringPtr++;
365 BuildPtr--; // remove the \0
366 pStr++;
367 continue;
368
369 case 'l':
370 pStr++;
371 switch (*pStr)
372 {
373 case 'x':
374 case 'X':
375 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
376 pStr++;
377 continue;
378
379 case 'd':
380 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
381 pStr++;
382 continue;
383 } // end switch
384 continue; // dunno what he wants
385
386 case 0:
387 continue;
388 } // end switch
389 break;
390
391 case '\\':
392 pStr++;
393 switch (*pStr)
394 {
395 case 'n':
396 *BuildPtr++=LF;
397 pStr++;
398 continue;
399
400 case 'r':
401 *BuildPtr++=CR;
402 pStr++;
403 continue;
404
405 case 0:
406 continue;
407 break;
408 } // end switch
409
410 break;
411 } // end switch
412
413 *BuildPtr++=*pStr++;
414 } // end while
415
416 *BuildPtr=0; // cauterize the string
417 StringOut((char *) BuildString); // print to comm port
418#endif //DEBUG
419}
420
421#ifdef DEBUG
422//#define MAGIC_COMM_PORT 0x3f8 // pulled from word ptr 40:0
423#define MAGIC_COMM_PORT 0x2f8 // pulled from word ptr 40:0
424
425
426#define UART_DATA 0x00 // UART Data port
427#define UART_INT_ENAB 0x01 // UART Interrupt enable
428#define UART_INT_ID 0x02 // interrupt ID
429#define UART_LINE_CTRL 0x03 // line control registers
430#define UART_MODEM_CTRL 0x04 // modem control register
431#define UART_LINE_STAT 0x05 // line status register
432#define UART_MODEM_STAT 0x06 // modem status regiser
433#define UART_DIVISOR_LO 0x00 // divisor latch least sig
434#define UART_DIVISOR_HI 0x01h // divisor latch most sig
435
436#define DELAY nop
437#endif
438
Note: See TracBrowser for help on using the repository browser.