gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
gbdk-lib/include/gbdk/emu_debug.h
1 /** @file gbdk/emu_debug.h
2
3 Debug window logging and profiling support for emulators (BGB, Emulicious, etc).
4
5 Also see the `emu_debug` example project included with gbdk.
6
7 See the BGB Manual for more information
8 ("expressions, breakpoint conditions, and debug messages")
9 http://bgb.bircd.org/manual.html#expressions
10
11 */
12
13 // Suppress SDCC "info 128" warnings that are a non-issue
14 #pragma disable_warning 218
15
16 #ifndef __GBDK_EMU_DEBUG_H_INCLUDE
17 #define __GBDK_EMU_DEBUG_H_INCLUDE
18
19 #include <types.h>
20
21 #if defined(__TARGET_gb) || defined(__TARGET_duck) || defined(__TARGET_ap) || defined(__TARGET_sms) || defined(__TARGET_gg)
22
23 /** Macro to display a message in the emulator debug message window
24
25 @param message_text Quoted text string to display in the debug message window
26
27 The following special parameters can be
28 used when bracketed with "%" characters.
29 \li CPU registers: AF, BC, DE, HL, SP, PC, B, C, D,
30 E, H, L, A, ZERO, ZF, Z, CARRY, CY, IME, ALLREGS
31 \li Other state values: ROMBANK, XRAMBANK, SRAMBANK,
32 WRAMBANK, VRAMBANK, TOTALCLKS, LASTCLKS, CLKS2VBLANK
33
34 Example: print a message along with the currently active ROM bank.
35 \code{.c}
36 EMU_MESSAGE("Current ROM Bank is: %ROMBANK%");
37 \endcode
38
39
40 See the BGB Manual for more information
41 ("expressions, breakpoint conditions, and debug messages")
42 http://bgb.bircd.org/manual.html#expressions
43
44 @see EMU_PROFILE_BEGIN(), EMU_PROFILE_END()
45 */
46 #define EMU_MESSAGE(message_text) EMU_MESSAGE1(EMU_MACRONAME(__LINE__), message_text)
47 #define BGB_MESSAGE(message_text) EMU_MESSAGE(message_text)
48
49 /// \cond DOXYGEN_DO_NOT_DOCUMENT
50 #define EMU_MACRONAME(A) EMU_MACRONAME1(A)
51 #define EMU_MACRONAME1(A) EMULOG##A
52
53 #define EMU_MESSAGE1(name, message_text) \
54 __asm \
55 .MACRO name msg_t, ?llbl \
56 ld d, d \
57 jr llbl \
58 .dw 0x6464 \
59 .dw 0x0000 \
60 .ascii msg_t \
61 llbl: \
62 .ENDM \
63 name ^/message_text/ \
64 __endasm
65
66 #define EMU_MESSAGE_SUFFIX(message_text, message_suffix) EMU_MESSAGE3(EMU_MACRONAME(__LINE__), message_text, message_suffix)
67 #define EMU_MESSAGE3(name, message_text, message_suffix) \
68 __asm \
69 .MACRO name msg_t, msg_s, ?llbl \
70 ld d, d \
71 jr llbl \
72 .dw 0x6464 \
73 .dw 0x0000 \
74 .ascii msg_t \
75 .ascii msg_s \
76 llbl: \
77 .ENDM \
78 name ^/message_text/, ^/message_suffix/ \
79 __endasm
80 /// \endcond DOXYGEN_DO_NOT_DOCUMENT
81
82 /** Macro to __Start__ a profiling block for the emulator (BGB, Emulicious, etc)
83
84 @param MSG Quoted text string to display in the
85 debug message window along with the result
86
87 To complete the profiling block and print
88 the result call @ref EMU_PROFILE_END.
89
90 @see EMU_PROFILE_END(), EMU_MESSAGE()
91 */
92 #define EMU_PROFILE_BEGIN(MSG) EMU_MESSAGE_SUFFIX(MSG, "%ZEROCLKS%");
93 #define BGB_PROFILE_BEGIN(MSG) EMU_PROFILE_BEGIN(MSG)
94 /** Macro to __End__ a profiling block and print the results in the emulator debug message window
95
96 @param MSG Quoted text string to display in the
97 debug message window along with the result
98
99 This should only be called after a previous call
100 to @ref EMU_PROFILE_BEGIN()
101
102 The results are in Emulator clock units, which are
103 "1 nop in [CGB] doublespeed mode".
104
105 So when running in Normal Speed mode (i.e. non-CGB doublespeed)
106 the printed result should be __divided by 2__ to get the actual
107 ellapsed cycle count.
108
109 If running in CB Double Speed mode use the below call instead,
110 it correctly compensates for the speed difference. In this
111 scenario, the result does __not need to be divided by 2__ to
112 get the ellapsed cycle count.
113 \code{.c}
114 EMU_MESSAGE("NOP TIME: %-4+LASTCLKS%");
115 \endcode
116
117 @see EMU_PROFILE_BEGIN(), EMU_MESSAGE()
118 */
119 #if defined(NINTENDO)
120 #define EMU_PROFILE_END(MSG) EMU_MESSAGE_SUFFIX(MSG,"%-8+LASTCLKS%");
121 #define BGB_PROFILE_END(MSG) EMU_PROFILE_END(MSG)
122 #elif defined(SEGA)
123 #define EMU_PROFILE_END(MSG) EMU_MESSAGE_SUFFIX(MSG,"%-16+LASTCLKS%");
124 #define BGB_PROFILE_END(MSG) EMU_PROFILE_END(MSG)
125 #endif
126
127 #define EMU_TEXT(MSG) EMU_MESSAGE(MSG)
128 #define BGB_TEXT(MSG) EMU_TEXT(MSG)
129
130 /** Print the string and arguments given by format to the emulator debug message window
131
132 @param format The format string as per printf
133
134 Does not return the number of characters printed.
135 Currently supported:
136 \li \%hx (char as hex)
137 \li \%hu (unsigned char)
138 \li \%hd (signed char)
139 \li \%c (character)
140 \li \%u (unsigned int)
141 \li \%d (signed int)
142 \li \%x (unsigned int as hex)
143 \li \%s (string)
144
145
146 @note
147 Variables for the following 8-bit formats __MUST__ be cast to their type when passed to EMU_printf()
148 \li \%hx (char)
149 \li \%hu (unsigned char)
150 \li \%hd (signed char)
151
152 However variables for the following 8-bit format __MUST NOT__ be cast to their type when passed to EMU_printf()
153 \li \%c (char)
154
155 This behavior is __different__ than for @ref sprintf(), which does require \%c format char variables to be explicitly cast.
156
157
158 Currently supported in the Emulicious emulator, may be supported by bgb
159 */
160 void EMU_printf(const char *format, ...) PRESERVES_REGS(a, b, c);
161 #define BGB_printf(...) EMU_printf(__VA_ARGS__)
162
163 /** Print the string and arguments in the buffer buffer by the pointer given by format to the emulator debug message window
164
165 @param format The format string as per printf
166 @param data Buffer containing arguments, for example some struct
167
168 @see EMU_printf for the format string description
169
170 Currently supported in the Emulicious emulator
171 */
172 void EMU_fmtbuf(const unsigned char * format, void * data) PRESERVES_REGS(a, b, c);
173
174 /** The Emulator will break into debugger when encounters this line
175 */
176 #define EMU_BREAKPOINT __asm__("ld b, b");
177 #define BGB_BREAKPOINT EMU_BREAKPOINT
178
179 #else
180 #error Unrecognized port
181 #endif
182
183 #endif
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.