source: cmedia/trunk/Lib32/debug.c@ 354

Last change on this file since 354 was 354, checked in by stevenhl, 17 years ago

Import untested baseline cmedia sources, work products and binaries
Binaries and work products should be deleted from repository.
once new builds are verified to work.

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