DEBUGIO.C 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. Copyright (C) 1994-1995 Apogee Software, Ltd.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include <stdio.h>
  16. #include <stdarg.h>
  17. #include <stdlib.h>
  18. #include "debugio.h"
  19. static unsigned short disp_offset = 160 * 24;
  20. static void myutoa( unsigned num, char *string, int radix );
  21. static void myitoa( int num, char *string, int radix );
  22. void DB_SetXY
  23. (
  24. int x,
  25. int y
  26. )
  27. {
  28. disp_offset = ( x * 2 ) + ( y * 160 );
  29. }
  30. void DB_PutChar
  31. (
  32. char ch
  33. )
  34. {
  35. int j;
  36. char *disp_start = (char *)( 0xb0000 );
  37. if ( disp_offset >= 160 * 24 )
  38. {
  39. for ( j = 160; j < 160 * 24; j += 2 )
  40. {
  41. *( disp_start + j - 160 ) = *( disp_start + j );
  42. }
  43. disp_offset = 160 * 23;
  44. for ( j = disp_offset; j < ( 160 * 24 ); j += 2 )
  45. {
  46. *( disp_start + j ) = ' ';
  47. }
  48. }
  49. if ( ch >= 32 )
  50. {
  51. *( disp_start + disp_offset ) = ch;
  52. disp_offset = disp_offset + 2;
  53. }
  54. if ( ch == '\r' )
  55. {
  56. disp_offset = disp_offset / 160;
  57. disp_offset = disp_offset * 160;
  58. }
  59. if ( ch == '\n' )
  60. {
  61. disp_offset = disp_offset + 160;
  62. if ( disp_offset < 160 * 24 )
  63. {
  64. for ( j = disp_offset; j < ( ( ( disp_offset / 160 ) + 1 ) *
  65. 160 ); j += 2 )
  66. {
  67. *( disp_start + j ) = ' ';
  68. }
  69. }
  70. }
  71. }
  72. int DB_PrintString
  73. (
  74. char *string
  75. )
  76. {
  77. int count;
  78. char *ptr;
  79. ptr = string;
  80. count = 0;
  81. while ( *ptr )
  82. {
  83. DB_PutChar( *ptr );
  84. count++;
  85. ptr++;
  86. }
  87. return( count );
  88. }
  89. static void myutoa
  90. (
  91. unsigned num,
  92. char *string,
  93. int radix
  94. )
  95. {
  96. int val;
  97. int length;
  98. int pos;
  99. char temp[ 100 ];
  100. length = 0;
  101. do
  102. {
  103. val = num % radix;
  104. if ( val < 10 )
  105. {
  106. temp[ length ] = '0' + val;
  107. }
  108. else
  109. {
  110. temp[ length ] = 'A' + val - 10;
  111. }
  112. num /= radix;
  113. length++;
  114. }
  115. while( num > 0 );
  116. pos = 0;
  117. while( length > 0 )
  118. {
  119. length--;
  120. string[ length ] = temp[ pos ];
  121. pos++;
  122. }
  123. string[ pos ] = 0;
  124. }
  125. static void myitoa
  126. (
  127. int num,
  128. char *string,
  129. int radix
  130. )
  131. {
  132. if ( num < 0 )
  133. {
  134. *string++ = '-';
  135. num = -num;
  136. }
  137. myutoa( num, string, radix );
  138. }
  139. int DB_PrintNum
  140. (
  141. int number
  142. )
  143. {
  144. char string[ 100 ];
  145. int count;
  146. myitoa( number, &string[ 0 ], 10 );
  147. count = DB_PrintString( &string[ 0 ] );
  148. return( count );
  149. }
  150. int DB_PrintUnsigned
  151. (
  152. unsigned long number,
  153. int radix
  154. )
  155. {
  156. char string[ 100 ];
  157. int count;
  158. myutoa( number, &string[ 0 ], radix );
  159. count = DB_PrintString( &string[ 0 ] );
  160. return( count );
  161. }
  162. int DB_printf
  163. (
  164. char *fmt,
  165. ...
  166. )
  167. {
  168. va_list argptr;
  169. int count;
  170. char *ptr;
  171. va_start( argptr, fmt );
  172. ptr = fmt;
  173. count = 0;
  174. while( *ptr != 0 )
  175. {
  176. if ( *ptr == '%' )
  177. {
  178. ptr++;
  179. switch( *ptr )
  180. {
  181. case 0 :
  182. return( EOF );
  183. break;
  184. case 'd' :
  185. count += DB_PrintNum( va_arg( argptr, int ) );
  186. break;
  187. case 's' :
  188. count += DB_PrintString( va_arg( argptr, char * ) );
  189. break;
  190. case 'u' :
  191. count += DB_PrintUnsigned( va_arg( argptr, int ), 10 );
  192. break;
  193. case 'x' :
  194. case 'X' :
  195. count += DB_PrintUnsigned( va_arg( argptr, int ), 16 );
  196. break;
  197. }
  198. ptr++;
  199. }
  200. else
  201. {
  202. DB_PutChar( *ptr );
  203. count++;
  204. ptr++;
  205. }
  206. }
  207. va_end( argptr );
  208. return( count );
  209. }