debug_gba.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (C) 2001-2004 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*
  34. * $Log$
  35. * Revision 1.6 2008/08/11 06:59:07 haraldkipp
  36. * BSD types replaced by stdint types (feature request #1282721).
  37. *
  38. * Revision 1.5 2006/10/08 16:48:07 haraldkipp
  39. * Documentation fixed
  40. *
  41. * Revision 1.4 2006/04/07 12:08:18 haraldkipp
  42. * Compiler warning fixed.
  43. *
  44. * Revision 1.3 2005/10/24 17:59:19 haraldkipp
  45. * Use correct header file, arm, not gba.
  46. *
  47. * Revision 1.2 2005/08/02 17:46:45 haraldkipp
  48. * Major API documentation update.
  49. *
  50. * Revision 1.1 2005/07/26 18:02:26 haraldkipp
  51. * Moved from dev.
  52. *
  53. * Revision 1.3 2005/04/05 17:50:46 haraldkipp
  54. * Use register names in gba.h.
  55. *
  56. * Revision 1.2 2004/11/08 18:11:49 haraldkipp
  57. * Support for carriage return added.
  58. *
  59. * Revision 1.1 2004/10/03 18:39:12 haraldkipp
  60. * GBA debug output on screen
  61. *
  62. */
  63. #include <dev/debug.h>
  64. #include <arch/arm.h>
  65. #include <sys/device.h>
  66. #include <sys/file.h>
  67. /*!
  68. * \addtogroup xgDevDebugGba
  69. */
  70. /*@{*/
  71. #define PALRAM_BASE 0x05000000
  72. #define VIDRAM_BASE 0x06000000
  73. #define LCD_COLS 30 /* Visible display columns */
  74. #define LCD_ROWS 20 /* Visible display rows */
  75. #define LCD_MCOLS 32 /* Buffer memory columns */
  76. #define LCD_MROWS 64 /* Buffer memory rows */
  77. static uint16_t pos_x; /* Current column */
  78. static uint16_t pos_y; /* Current row */
  79. static uint16_t pos_vofs; /* Vertical offset */
  80. static NUTFILE dbgfile;
  81. /* 8x8 character font, ASCII 32..127. */
  82. static uint8_t font8x8[]= {
  83. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  84. 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x10, 0x00,
  85. 0x28, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  86. 0x28, 0x28, 0xFE, 0x28, 0xFE, 0x28, 0x28, 0x00,
  87. 0x10, 0x7C, 0x80, 0x7C, 0x02, 0x7C, 0x10, 0x00,
  88. 0xC2, 0xC4, 0x08, 0x10, 0x20, 0x46, 0x86, 0x00,
  89. 0x60, 0x90, 0x60, 0x90, 0x88, 0x84, 0x7A, 0x00,
  90. 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  91. 0x08, 0x10, 0x20, 0x20, 0x20, 0x10, 0x08, 0x00,
  92. 0x20, 0x10, 0x08, 0x08, 0x08, 0x10, 0x20, 0x00,
  93. 0x44, 0x28, 0x10, 0xFE, 0x10, 0x28, 0x44, 0x00,
  94. 0x00, 0x10, 0x10, 0x7C, 0x10, 0x10, 0x00, 0x00,
  95. 0x00, 0x00, 0x00, 0x00, 0x18, 0x08, 0x10, 0x00,
  96. 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x00,
  97. 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00,
  98. 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00,
  99. 0x7C, 0x86, 0x8A, 0x92, 0xA2, 0xC2, 0x7C, 0x00,
  100. 0x10, 0x30, 0x10, 0x10, 0x10, 0x10, 0x38, 0x00,
  101. 0x7C, 0x82, 0x04, 0x08, 0x10, 0x20, 0xFE, 0x00,
  102. 0x7C, 0x82, 0x02, 0x3C, 0x02, 0x82, 0x7C, 0x00,
  103. 0x40, 0x40, 0x84, 0x84, 0xFE, 0x04, 0x04, 0x00,
  104. 0xFE, 0x80, 0x80, 0xFC, 0x02, 0x82, 0x7C, 0x00,
  105. 0x10, 0x20, 0x40, 0xFC, 0x82, 0x82, 0x7C, 0x00,
  106. 0xFE, 0x02, 0x04, 0x08, 0x10, 0x10, 0x10, 0x00,
  107. 0x7C, 0x82, 0x82, 0x7C, 0x82, 0x82, 0x7C, 0x00,
  108. 0x7C, 0x82, 0x82, 0x7E, 0x04, 0x08, 0x10, 0x00,
  109. 0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x00, 0x00,
  110. 0x00, 0x00, 0x10, 0x00, 0x10, 0x10, 0x20, 0x00,
  111. 0x04, 0x08, 0x10, 0x20, 0x10, 0x08, 0x04, 0x00,
  112. 0x00, 0x00, 0x7C, 0x00, 0x7C, 0x00, 0x00, 0x00,
  113. 0x40, 0x20, 0x10, 0x08, 0x10, 0x20, 0x40, 0x00,
  114. 0x7C, 0x82, 0x04, 0x08, 0x10, 0x00, 0x10, 0x00,
  115. 0x7C, 0x82, 0xBE, 0xA2, 0xBE, 0x80, 0x7E, 0x00,
  116. 0x10, 0x28, 0x44, 0x82, 0xFE, 0x82, 0x82, 0x00,
  117. 0xFC, 0x82, 0x82, 0xFC, 0x82, 0x82, 0xFC, 0x00,
  118. 0x7C, 0x82, 0x80, 0x80, 0x80, 0x82, 0x7C, 0x00,
  119. 0xFC, 0x82, 0x82, 0x82, 0x82, 0x82, 0xFC, 0x00,
  120. 0xFE, 0x80, 0x80, 0xFC, 0x80, 0x80, 0xFE, 0x00,
  121. 0xFE, 0x80, 0x80, 0xFC, 0x80, 0x80, 0x80, 0x00,
  122. 0x7C, 0x82, 0x80, 0x8E, 0x82, 0x82, 0x7E, 0x00,
  123. 0x82, 0x82, 0x82, 0xFE, 0x82, 0x82, 0x82, 0x00,
  124. 0x38, 0x10, 0x10, 0x10, 0x10, 0x10, 0x38, 0x00,
  125. 0x02, 0x02, 0x02, 0x02, 0x02, 0x82, 0x7C, 0x00,
  126. 0x82, 0x8C, 0xB0, 0xC0, 0xB0, 0x8C, 0x82, 0x00,
  127. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xFE, 0x00,
  128. 0x82, 0xC6, 0xAA, 0x92, 0x82, 0x82, 0x82, 0x00,
  129. 0x82, 0xC2, 0xA2, 0x92, 0x8A, 0x86, 0x82, 0x00,
  130. 0x7C, 0x82, 0x82, 0x82, 0x82, 0x82, 0x7C, 0x00,
  131. 0xFC, 0x82, 0x82, 0xFC, 0x80, 0x80, 0x80, 0x00,
  132. 0x7C, 0x82, 0x82, 0x82, 0x82, 0x84, 0x7A, 0x00,
  133. 0xFC, 0x82, 0x82, 0xFC, 0x88, 0x84, 0x82, 0x00,
  134. 0x7C, 0x82, 0x80, 0x7C, 0x02, 0x82, 0x7C, 0x00,
  135. 0xFE, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00,
  136. 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x7C, 0x00,
  137. 0x82, 0x82, 0x82, 0x82, 0x44, 0x28, 0x10, 0x00,
  138. 0x82, 0x82, 0x82, 0x92, 0x92, 0x54, 0x28, 0x00,
  139. 0x82, 0x44, 0x28, 0x10, 0x28, 0x44, 0x82, 0x00,
  140. 0x82, 0x82, 0x44, 0x28, 0x10, 0x10, 0x10, 0x00,
  141. 0xFE, 0x04, 0x08, 0x10, 0x20, 0x40, 0xFE, 0x00,
  142. 0x38, 0x20, 0x20, 0x20, 0x20, 0x20, 0x38, 0x00,
  143. 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x00,
  144. 0x38, 0x08, 0x08, 0x08, 0x08, 0x08, 0x38, 0x00,
  145. 0x10, 0x28, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00,
  146. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x00,
  147. 0x20, 0x10, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  148. 0x00, 0x00, 0x7E, 0x82, 0x82, 0x86, 0x7A, 0x00,
  149. 0x80, 0x80, 0xFC, 0x82, 0x82, 0x82, 0xFC, 0x00,
  150. 0x00, 0x00, 0x7E, 0x80, 0x80, 0x80, 0x7E, 0x00,
  151. 0x02, 0x02, 0x7E, 0x82, 0x82, 0x82, 0x7E, 0x00,
  152. 0x00, 0x00, 0x7C, 0x82, 0xFE, 0x80, 0x7C, 0x00,
  153. 0x0E, 0x10, 0x7C, 0x10, 0x10, 0x10, 0x10, 0x00,
  154. 0x00, 0x00, 0x7E, 0x82, 0x7E, 0x02, 0x7C, 0x00,
  155. 0x80, 0x80, 0xFC, 0x82, 0x82, 0x82, 0x82, 0x00,
  156. 0x10, 0x00, 0x30, 0x10, 0x10, 0x10, 0x38, 0x00,
  157. 0x08, 0x00, 0x18, 0x08, 0x08, 0x08, 0x70, 0x00,
  158. 0x80, 0x80, 0x86, 0x98, 0xE0, 0x98, 0x86, 0x00,
  159. 0x30, 0x10, 0x10, 0x10, 0x10, 0x10, 0x38, 0x00,
  160. 0x00, 0x00, 0xEC, 0x92, 0x92, 0x92, 0x82, 0x00,
  161. 0x00, 0x00, 0xBC, 0xC2, 0x82, 0x82, 0x82, 0x00,
  162. 0x00, 0x00, 0x7C, 0x82, 0x82, 0x82, 0x7C, 0x00,
  163. 0x00, 0x00, 0xFC, 0x82, 0xFC, 0x80, 0x80, 0x00,
  164. 0x00, 0x00, 0x7E, 0x82, 0x7E, 0x02, 0x02, 0x00,
  165. 0x00, 0x00, 0xBE, 0xC0, 0x80, 0x80, 0x80, 0x00,
  166. 0x00, 0x00, 0x7E, 0x80, 0x7C, 0x02, 0xFC, 0x00,
  167. 0x10, 0x10, 0x7C, 0x10, 0x10, 0x10, 0x0C, 0x00,
  168. 0x00, 0x00, 0x82, 0x82, 0x82, 0x86, 0x7A, 0x00,
  169. 0x00, 0x00, 0x82, 0x82, 0x44, 0x28, 0x10, 0x00,
  170. 0x00, 0x00, 0x92, 0x92, 0x92, 0x92, 0x6C, 0x00,
  171. 0x00, 0x00, 0x82, 0x44, 0x38, 0x44, 0x82, 0x00,
  172. 0x00, 0x00, 0x82, 0x82, 0x7E, 0x02, 0x7C, 0x00,
  173. 0x00, 0x00, 0xFE, 0x0C, 0x30, 0xC0, 0xFE, 0x00,
  174. 0x0C, 0x10, 0x10, 0x20, 0x10, 0x10, 0x0C, 0x00,
  175. 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00,
  176. 0x60, 0x10, 0x10, 0x08, 0x10, 0x10, 0x60, 0x00,
  177. 0x34, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  178. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  179. };
  180. /*!
  181. * \brief Handle I/O controls for debug device 0.
  182. *
  183. * The debug device doesn't support any.
  184. *
  185. * \return Always -1.
  186. */
  187. int DebugIOCtl(NUTDEVICE * dev, int req, void *conf)
  188. {
  189. return -1;
  190. }
  191. /*!
  192. * \brief Initialize debug device 0.
  193. *
  194. * \return Always 0.
  195. */
  196. int DebugInit(NUTDEVICE * dev)
  197. {
  198. register uint8_t ch;
  199. uint_fast16_t i;
  200. uint_fast16_t j;
  201. uint_fast16_t k;
  202. uint16_t *pal;
  203. uint16_t *vid;
  204. outw(REG_DISPCNT, 0x0400);
  205. outw(REG_BG2CNT, 0xA880);
  206. pal = (uint16_t *)PALRAM_BASE;
  207. for (i = 1; i < 256; i++) {
  208. pal[i] = 0x7FFF;
  209. }
  210. pal[0] = 0;
  211. vid = (uint16_t *)VIDRAM_BASE + 1024;
  212. for(i = 0; i < 96; i++) {
  213. for (j = 0; j < 8; j++) {
  214. ch = font8x8[i * 8 + j];
  215. for(k = 0; k < 4; k++) {
  216. *vid++ = ((ch & 0x80) ? 1 : 0) | ((ch & 0x40) ? 0x0100 : 0);
  217. ch *= 4;
  218. }
  219. }
  220. }
  221. return 0;
  222. }
  223. /*!
  224. * \brief Send a single character to debug device 0.
  225. *
  226. */
  227. static void DebugPut(char ch)
  228. {
  229. uint8_t i;
  230. uint16_t *vid = (uint16_t *)(VIDRAM_BASE + 0x4000);
  231. uint16_t pos = 0;
  232. if (ch == '\r') {
  233. pos_x = 0;
  234. }
  235. else if (pos_x == LCD_COLS || ch == '\n') {
  236. pos_x = 0;
  237. pos_y++;
  238. if ((pos_y - pos_vofs) == LCD_ROWS) {
  239. pos_vofs++;
  240. outw(REG_BG2VOFS, (pos_vofs & (LCD_MROWS - 1)) * 8);
  241. if (pos_vofs >= LCD_MROWS && pos_y >= LCD_MROWS) {
  242. pos_vofs -= LCD_MROWS;
  243. pos_y -= LCD_MROWS;
  244. }
  245. }
  246. pos = (pos_y & (LCD_MROWS - 1)) * LCD_MCOLS;
  247. for(i = 0; i < LCD_COLS; i++) {
  248. vid[pos + i] = 32;
  249. }
  250. }
  251. else {
  252. pos = (pos_y & (LCD_MROWS - 1)) * LCD_MCOLS;
  253. }
  254. if (ch >= 32 && ch <= 127) {
  255. vid[pos + pos_x++] = ch;
  256. }
  257. }
  258. /*!
  259. * \brief Send characters to debug device 0.
  260. *
  261. * A carriage return character will be automatically appended
  262. * to any linefeed.
  263. *
  264. * \return Number of characters sent.
  265. */
  266. int DebugWrite(NUTFILE * fp, const void *buffer, int len)
  267. {
  268. int c = len;
  269. const char *cp = buffer;
  270. while(c--) {
  271. DebugPut(*cp++);
  272. }
  273. return len;
  274. }
  275. /*!
  276. * \brief Open debug device 0.
  277. *
  278. * \return Pointer to a static NUTFILE structure.
  279. */
  280. NUTFILE *DebugOpen(NUTDEVICE * dev, const char *name, int mode, int acc)
  281. {
  282. dbgfile.nf_dev = dev;
  283. dbgfile.nf_fcb = NULL;
  284. return &dbgfile;
  285. }
  286. /*!
  287. * \brief Close debug device 0.
  288. *
  289. * \return Always 0.
  290. */
  291. int DebugClose(NUTFILE * fp)
  292. {
  293. return 0;
  294. }
  295. /*!
  296. * \brief Debug device 0 information structure.
  297. */
  298. NUTDEVICE devDebug0 = {
  299. 0, /*!< Pointer to next device, dev_next. */
  300. {'c', 'o', 'n', 0, 0, 0, 0, 0, 0}, /*!< Unique device name, dev_name. */
  301. 0, /*!< Type of device, dev_type. */
  302. 0, /*!< Base address, dev_base. */
  303. 0, /*!< First interrupt number, dev_irq. */
  304. 0, /*!< Interface control block, dev_icb. */
  305. 0, /*!< Driver control block, dev_dcb. */
  306. DebugInit, /*!< Driver initialization routine, dev_init. */
  307. DebugIOCtl, /*!< Driver specific control function, dev_ioctl. */
  308. 0, /*!< dev_read. */
  309. DebugWrite, /*!< dev_write. */
  310. DebugOpen, /*!< dev_opem. */
  311. DebugClose, /*!< dev_close. */
  312. 0, /*!< dev_size. */
  313. 0, /*!< Select function, optional, not yet implemented */
  314. };
  315. /*@}*/