#10551 - poslundc - Mon Sep 08, 2003 5:28 pm
I recently wrote a little function that I use to examine the contents of memory; thought I'd share it in case anyone else finds it useful. It's not sophisticated at all but if you need to check the value of a variable or contents of an array bit-by-bit (as I often do) it's invaluable.
To use it, call DebugVar() and pass to it a pointer to the data you want to show (eg. &myVariable or just myArray) and the number of bytes you want to display.
Every other line on the screen will display a series of eight dots each representing a bit of your variable: green if the bit is clear, red if it's set.
Note that it displays one byte at a time in the order it appears in memory, so a 32-bit variable will display bits in the order [7..0], [15..8], [23..16] and on the fourth line [31..24].
Press "A" then "B" when you're done viewing (I use the two-button sequence to debounce it when you call DebugVar multiple times in a row). It'll restore the previous mode, and depending on your app there's a slim chance your graphics might not even glitch! :)
If anyone else has similar debugging routines, I'd love to see them!
Dan.
Code: |
// use with gba.h void DebugVar(void *data, int bytes) { int n, i, j; u8 *p = (u8 *)data; u32 oldmode = REG_DISPCNT; u16 *vbuff = (u16*)0x6000000; volatile u32 *k = (volatile u32 *)0x04000130; REG_DISPCNT = 0x3 | 0x400; for (i = 0; i < 240; i += 1) for (j = 0; j < 16; j++) vbuff[i * 240 + j] = 0; i = 0; for (n = 0; n < bytes; n++) { for (j = 7; j >= 0; j--) { if ((*p >> j) & 0x01) vbuff[i * 240 + (14 - (j << 1))] = 31; else vbuff[i * 240 + (14 - (j << 1))] = 31 << 5; } p++; i += 2; } while (!((~(*k)) & 0x0001)) ; while (!((~(*k)) & 0x0002)) ; REG_DISPCNT = oldmode; } |
To use it, call DebugVar() and pass to it a pointer to the data you want to show (eg. &myVariable or just myArray) and the number of bytes you want to display.
Every other line on the screen will display a series of eight dots each representing a bit of your variable: green if the bit is clear, red if it's set.
Note that it displays one byte at a time in the order it appears in memory, so a 32-bit variable will display bits in the order [7..0], [15..8], [23..16] and on the fourth line [31..24].
Press "A" then "B" when you're done viewing (I use the two-button sequence to debounce it when you call DebugVar multiple times in a row). It'll restore the previous mode, and depending on your app there's a slim chance your graphics might not even glitch! :)
If anyone else has similar debugging routines, I'd love to see them!
Dan.