Core commit. Compile and run on the OpenPandora
[mupen64plus-pandora.git] / source / mupen64plus-core / src / api / debugger.c
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  *   Mupen64plus-core - api/debugger.c                                     *
3  *   Mupen64Plus homepage: http://code.google.com/p/mupen64plus/           *
4  *   Copyright (C) 2009 Richard Goedeken                                   *
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  *   This program is distributed in the hope that it will be useful,       * 
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14  *   GNU General Public License for more details.                          *
15  *                                                                         *
16  *   You should have received a copy of the GNU General Public License     *
17  *   along with this program; if not, write to the                         *
18  *   Free Software Foundation, Inc.,                                       *
19  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
20  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
21                        
22 /* This file contains the Core debugger functions which will be exported
23  * outside of the core library.
24  */
25    
26 #include <stdlib.h>
27
28 #define M64P_CORE_PROTOTYPES 1
29 #include "m64p_types.h"
30 #include "m64p_debugger.h"
31 #include "callbacks.h"
32 #include "debugger.h"
33
34 #include "debugger/dbg_types.h"
35 #include "debugger/dbg_breakpoints.h"
36 #include "debugger/dbg_decoder.h"
37 #include "debugger/dbg_memory.h"
38 #include "debugger/debugger.h"
39 #include "memory/memory.h"
40 #include "r4300/r4300.h"
41
42 extern unsigned int op; /* this is in r4300/pure_interp.c */
43
44 /* local variables */
45 static void (*callback_ui_init)(void) = NULL;
46 static void (*callback_ui_update)(unsigned int) = NULL;
47 static void (*callback_ui_vi)(void) = NULL;
48
49 static void (*callback_core_compare)(unsigned int) = NULL;
50 static void (*callback_core_data_sync)(int, void *) = NULL;
51
52 /* global Functions for use by the Core */
53
54 void DebuggerCallback(eDbgCallbackType type, unsigned int param)
55 {
56     if (type == DEBUG_UI_INIT)
57     {
58         if (callback_ui_init != NULL)
59             (*callback_ui_init)();
60     }
61     else if (type == DEBUG_UI_UPDATE)
62     {
63         if (callback_ui_update != NULL)
64             (*callback_ui_update)(param);
65     }
66     else if (type == DEBUG_UI_VI)
67     {
68         if (callback_ui_vi != NULL)
69             (*callback_ui_vi)();
70     }
71 }
72
73 void CoreCompareCallback(void)
74 {
75     if (callback_core_compare != NULL)
76         (*callback_core_compare)(op);
77 }
78
79 void CoreCompareDataSync(int length, void *ptr)
80 {
81     if (callback_core_data_sync != NULL)
82         (*callback_core_data_sync)(length, ptr);
83 }
84
85 /* exported functions for use by the front-end User Interface */
86
87 EXPORT m64p_error CALL DebugSetCoreCompare(void (*dbg_core_compare)(unsigned int), void (*dbg_core_data_sync)(int, void *))
88 {
89     callback_core_compare = dbg_core_compare;
90     callback_core_data_sync = dbg_core_data_sync;
91     return M64ERR_SUCCESS;
92 }
93  
94 EXPORT m64p_error CALL DebugSetCallbacks(void (*dbg_frontend_init)(void), void (*dbg_frontend_update)(unsigned int pc), void (*dbg_frontend_vi)(void))
95 {
96 #ifdef DBG
97     callback_ui_init = dbg_frontend_init;
98     callback_ui_update = dbg_frontend_update;
99     callback_ui_vi = dbg_frontend_vi;
100     return M64ERR_SUCCESS;
101 #else
102     return M64ERR_UNSUPPORTED;
103 #endif
104 }
105
106 EXPORT m64p_error CALL DebugSetRunState(int runstate)
107 {
108 #ifdef DBG
109     run = runstate; /* in debugger/debugger.c */
110     return M64ERR_SUCCESS;
111 #else
112     return M64ERR_UNSUPPORTED;
113 #endif
114 }
115
116 EXPORT int CALL DebugGetState(m64p_dbg_state statenum)
117 {
118 #ifdef DBG
119     switch (statenum)
120     {
121         case M64P_DBG_RUN_STATE:
122             return run;
123         case M64P_DBG_PREVIOUS_PC:
124             return previousPC;
125         case M64P_DBG_NUM_BREAKPOINTS:
126             return g_NumBreakpoints;
127         case M64P_DBG_CPU_DYNACORE:
128             return r4300emu;
129         case M64P_DBG_CPU_NEXT_INTERRUPT:
130             return next_interupt;
131         default:
132             DebugMessage(M64MSG_WARNING, "Bug: invalid m64p_dbg_state input in DebugGetState()");
133             return 0;
134     }
135     return 0;
136 #else
137     DebugMessage(M64MSG_ERROR, "Bug: DebugGetState() called, but Debugger not supported in Core library");
138     return 0;
139 #endif
140 }
141
142 EXPORT m64p_error CALL DebugStep(void)
143 {
144 #ifdef DBG
145     if (!g_DebuggerActive)
146         return M64ERR_INVALID_STATE;
147     debugger_step(); /* in debugger/debugger.c */
148     return M64ERR_SUCCESS;
149 #else
150     return M64ERR_UNSUPPORTED;
151 #endif
152 }
153
154 EXPORT void CALL DebugDecodeOp(unsigned int instruction, char *op, char *args, int pc)
155 {
156 #ifdef DBG
157     r4300_decode_op(instruction, op, args, pc);
158 #else
159     DebugMessage(M64MSG_ERROR, "Bug: DebugDecodeOp() called, but Debugger not supported in Core library");
160 #endif
161 }
162
163 EXPORT void * CALL DebugMemGetRecompInfo(m64p_dbg_mem_info recomp_type, unsigned int address, int index)
164 {
165 #ifdef DBG
166     switch (recomp_type)
167     {
168         case M64P_DBG_RECOMP_OPCODE:
169             return get_recompiled_opcode(address, index);
170         case M64P_DBG_RECOMP_ARGS:
171             return get_recompiled_args(address, index);
172         case M64P_DBG_RECOMP_ADDR:
173             return get_recompiled_addr(address, index);
174         default:
175             DebugMessage(M64MSG_ERROR, "Bug: DebugMemGetRecompInfo() called with invalid m64p_dbg_mem_info");
176             return NULL;
177     }
178 #else
179     DebugMessage(M64MSG_ERROR, "Bug: DebugMemGetRecompInfo() called, but Debugger not supported in Core library");
180     return NULL;
181 #endif
182 }
183
184 EXPORT int CALL DebugMemGetMemInfo(m64p_dbg_mem_info mem_info_type, unsigned int address)
185 {
186 #ifdef DBG
187     switch (mem_info_type)
188     {
189         case M64P_DBG_MEM_TYPE:
190             return get_memory_type(address);
191         case M64P_DBG_MEM_FLAGS:
192             return get_memory_flags(address);
193         case M64P_DBG_MEM_HAS_RECOMPILED:
194             return get_has_recompiled(address);
195         case M64P_DBG_MEM_NUM_RECOMPILED:
196             return get_num_recompiled(address);
197         default:
198             DebugMessage(M64MSG_ERROR, "Bug: DebugMemGetMemInfo() called with invalid m64p_dbg_mem_info");
199             return 0;
200     }
201 #else
202     DebugMessage(M64MSG_ERROR, "Bug: DebugMemGetMemInfo() called, but Debugger not supported in Core library");
203     return 0;
204 #endif
205 }
206
207 EXPORT void * CALL DebugMemGetPointer(m64p_dbg_memptr_type mem_ptr_type)
208 {
209     switch (mem_ptr_type)
210     {
211         case M64P_DBG_PTR_RDRAM:
212             return rdram;
213         case M64P_DBG_PTR_PI_REG:
214             return &pi_register;
215         case M64P_DBG_PTR_SI_REG:
216             return &si_register;
217         case M64P_DBG_PTR_VI_REG:
218             return &vi_register;
219         case M64P_DBG_PTR_RI_REG:
220             return &ri_register;
221         case M64P_DBG_PTR_AI_REG:
222             return &ai_register;
223         default:
224             DebugMessage(M64MSG_ERROR, "Bug: DebugMemGetPointer() called with invalid m64p_dbg_memptr_type");
225             return NULL;
226     }
227 }
228
229 EXPORT unsigned long long CALL DebugMemRead64(unsigned int address)
230 {
231 #ifdef DBG
232     if ((address & 3) == 0)
233         return read_memory_64(address);
234     else
235         return read_memory_64_unaligned(address);
236 #else
237     DebugMessage(M64MSG_ERROR, "Bug: DebugMemRead64() called, but Debugger not supported in Core library");
238     return 0LL;
239 #endif
240 }
241
242 EXPORT unsigned int CALL DebugMemRead32(unsigned int address)
243 {
244 #ifdef DBG
245     if ((address & 3) == 0)
246         return read_memory_32(address);
247     else
248         return read_memory_32_unaligned(address);
249 #else
250     DebugMessage(M64MSG_ERROR, "Bug: DebugMemRead32() called, but Debugger not supported in Core library");
251     return 0;
252 #endif
253 }
254
255 EXPORT unsigned short CALL DebugMemRead16(unsigned int address)
256 {
257 #ifdef DBG
258     return read_memory_16(address);
259 #else
260     DebugMessage(M64MSG_ERROR, "Bug: DebugMemRead16() called, but Debugger not supported in Core library");
261     return 0;
262 #endif
263 }
264
265 EXPORT unsigned char CALL DebugMemRead8(unsigned int address)
266 {
267 #ifdef DBG
268     return read_memory_8(address);
269 #else
270     DebugMessage(M64MSG_ERROR, "Bug: DebugMemRead8() called, but Debugger not supported in Core library");
271     return 0;
272 #endif
273 }
274
275 EXPORT void CALL DebugMemWrite64(unsigned int address, unsigned long long value)
276 {
277 #ifdef DBG
278     if ((address & 3) == 0)
279         write_memory_64(address, value);
280     else
281         write_memory_64_unaligned(address, value);
282 #else
283     DebugMessage(M64MSG_ERROR, "Bug: DebugMemWrite64() called, but Debugger not supported in Core library");
284 #endif
285 }
286
287 EXPORT void CALL DebugMemWrite32(unsigned int address, unsigned int value)
288 {
289 #ifdef DBG
290     if ((address & 3) == 0)
291         write_memory_32(address, value);
292     else
293         write_memory_32_unaligned(address, value);
294 #else
295     DebugMessage(M64MSG_ERROR, "Bug: DebugMemWrite32() called, but Debugger not supported in Core library");
296 #endif
297 }
298
299 EXPORT void CALL DebugMemWrite16(unsigned int address, unsigned short value)
300 {
301 #ifdef DBG
302     write_memory_16(address, value);
303 #else
304     DebugMessage(M64MSG_ERROR, "Bug: DebugMemWrite16() called, but Debugger not supported in Core library");
305 #endif
306 }
307
308 EXPORT void CALL DebugMemWrite8(unsigned int address, unsigned char value)
309 {
310 #ifdef DBG
311     write_memory_8(address, value);
312 #else
313     DebugMessage(M64MSG_ERROR, "Bug: DebugMemWrite8() called, but Debugger not supported in Core library");
314 #endif
315 }
316
317 EXPORT void * CALL DebugGetCPUDataPtr(m64p_dbg_cpu_data cpu_data_type)
318 {
319     switch (cpu_data_type)
320     {
321         case M64P_CPU_PC:
322             return &PC->addr;
323         case M64P_CPU_REG_REG:
324             return reg;
325         case M64P_CPU_REG_HI:
326             return &hi;
327         case M64P_CPU_REG_LO:
328             return &lo;
329         case M64P_CPU_REG_COP0:
330             return reg_cop0;
331         case M64P_CPU_REG_COP1_DOUBLE_PTR:
332             return reg_cop1_double;
333         case M64P_CPU_REG_COP1_SIMPLE_PTR:
334             return reg_cop1_simple;
335         case M64P_CPU_REG_COP1_FGR_64:
336             return reg_cop1_fgr_64;
337         case M64P_CPU_TLB:
338             return tlb_e;
339         default:
340             DebugMessage(M64MSG_ERROR, "Bug: DebugGetCPUDataPtr() called with invalid input m64p_dbg_cpu_data");
341             return NULL;
342     }
343 }
344
345 EXPORT int CALL DebugBreakpointLookup(unsigned int address, unsigned int size, unsigned int flags)
346 {
347 #ifdef DBG
348     return lookup_breakpoint(address, size, flags);
349 #else
350     DebugMessage(M64MSG_ERROR, "Bug: DebugBreakpointLookup() called, but Debugger not supported in Core library");
351     return -1;
352 #endif
353 }
354
355 EXPORT int CALL DebugBreakpointCommand(m64p_dbg_bkp_command command, unsigned int index, void *ptr)
356 {
357 #ifdef DBG
358     switch (command)
359     {
360         case M64P_BKP_CMD_ADD_ADDR:
361             return add_breakpoint(index);
362         case M64P_BKP_CMD_ADD_STRUCT:
363             return add_breakpoint_struct((breakpoint *) ptr);
364         case M64P_BKP_CMD_REPLACE:
365             replace_breakpoint_num(index, (breakpoint *) ptr);
366             return 0;
367         case M64P_BKP_CMD_REMOVE_ADDR:
368             remove_breakpoint_by_address(index);
369             return 0;
370         case M64P_BKP_CMD_REMOVE_IDX:
371             remove_breakpoint_by_num(index);
372             return 0;
373         case M64P_BKP_CMD_ENABLE:
374             enable_breakpoint(index);
375             return 0;
376         case M64P_BKP_CMD_DISABLE:
377             disable_breakpoint(index);
378             return 0;
379         case M64P_BKP_CMD_CHECK:
380             return check_breakpoints(index);
381         default:
382             DebugMessage(M64MSG_ERROR, "Bug: DebugBreakpointCommand() called with invalid input m64p_dbg_bkp_command");
383             return -1;
384     }
385 #else
386     DebugMessage(M64MSG_ERROR, "Bug: DebugBreakpointCommand() called, but Debugger not supported in Core library");
387     return -1;
388 #endif
389 }
390