source: sbliveos2/trunk/drv16/commdbg.c@ 142

Last change on this file since 142 was 142, checked in by ktk, 25 years ago

Import

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