1 /*****************************************************************************
2 ** $Source: /cygdrive/d/Private/_SVNROOT/bluemsx/blueMSX/Src/Debugger/DebugDeviceManager.c,v $
3 **
4 ** $Revision: 1.14 $
5 **
6 ** $Date: 2008-03-30 18:38:39 $
7 **
8 ** More info: http://www.bluemsx.com
9 **
10 ** Copyright (C) 2003-2006 Daniel Vik
11 **
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License as published by
14 ** the Free Software Foundation; either version 2 of the License, or
15 ** (at your option) any later version.
16 **
17 ** This program is distributed in the hope that it will be useful,
18 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ** GNU General Public License for more details.
21 **
22 ** You should have received a copy of the GNU General Public License
23 ** along with this program; if not, write to the Free Software
24 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 **
26 ******************************************************************************
27 */
28 #include "DebugDeviceManager.h"
29 #include "Board.h"
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #define MAX_DEVICES 64
34 
35 typedef struct {
36     int handle;
37     DebugCallbacks callbacks;
38     void* ref;
39     char  name[32];
40     DbgDeviceType type;
41 } DebugDeviceInfo;
42 
43 typedef struct {
44     DebugDeviceInfo di[MAX_DEVICES];
45     int count;
46     int lastHandle;
47 } DebugDeviceManager;
48 
49 static DebugDeviceManager devManager;
50 
debugDeviceManagerReset()51 void debugDeviceManagerReset()
52 {
53     devManager.count = 0;
54 //    devManager.lastHandle = 0;
55 }
56 
debugDeviceRegister(DbgDeviceType type,const char * name,DebugCallbacks * callbacks,void * ref)57 int debugDeviceRegister(DbgDeviceType type, const char* name, DebugCallbacks* callbacks, void* ref)
58 {
59     if (devManager.count >= MAX_DEVICES) {
60         return 0;
61     }
62 
63     devManager.di[devManager.count].handle    = ++devManager.lastHandle;
64     devManager.di[devManager.count].callbacks = *callbacks;
65     devManager.di[devManager.count].ref       = ref;
66     devManager.di[devManager.count].type      = type;
67 
68     strcpy(devManager.di[devManager.count].name, name);
69 
70     devManager.count++;
71 
72     return devManager.lastHandle - 1;
73 }
74 
debugDeviceUnregister(int handle)75 void debugDeviceUnregister(int handle)
76 {
77     int i;
78 
79     if (devManager.count == 0) {
80         return;
81     }
82 
83     for (i = 0; i < devManager.count; i++) {
84         if (devManager.di[i].handle == handle + 1) {
85             break;
86         }
87     }
88 
89     if (i == devManager.count) {
90         return;
91     }
92 
93     devManager.count--;
94     while (i < devManager.count) {
95         devManager.di[i] = devManager.di[i + 1];
96         i++;
97     }
98 }
99 
debugDeviceGetSnapshot(DbgDevice ** dbgDeviceList,int * count)100 void debugDeviceGetSnapshot(DbgDevice** dbgDeviceList, int* count)
101 {
102     int index = 0;
103     int i;
104 
105     for (i = 0; i < devManager.count; i++) {
106         if (devManager.di[i].handle != 0) {
107             dbgDeviceList[index] = calloc(1, sizeof(DbgDevice));
108             strcpy(dbgDeviceList[index]->name, devManager.di[i].name);
109             dbgDeviceList[index]->type = devManager.di[i].type;
110             dbgDeviceList[index]->deviceHandle = devManager.di[i].handle;
111             if (devManager.di[i].callbacks.getDebugInfo != NULL) {
112                 devManager.di[i].callbacks.getDebugInfo(devManager.di[i].ref, dbgDeviceList[index++]);
113             }
114         }
115     }
116 
117     *count = index;
118 }
119 
debugDeviceWriteMemory(DbgMemoryBlock * memoryBlock,void * data,int startAddr,int size)120 int debugDeviceWriteMemory(DbgMemoryBlock* memoryBlock, void* data, int startAddr, int size)
121 {
122     int i;
123 
124     for (i = 0; i < devManager.count; i++) {
125         if (devManager.di[i].handle == memoryBlock->deviceHandle) {
126             if (devManager.di[i].callbacks.writeMemory != NULL) {
127                 return devManager.di[i].callbacks.writeMemory(devManager.di[i].ref, memoryBlock->name, data, startAddr, size);
128             }
129         }
130     }
131     return 0;
132 }
133 
debugDeviceWriteRegister(DbgRegisterBank * regBank,int regIndex,UInt32 value)134 int debugDeviceWriteRegister(DbgRegisterBank* regBank, int regIndex, UInt32 value)
135 {
136     int i;
137 
138     for (i = 0; i < devManager.count; i++) {
139         if (devManager.di[i].handle == regBank->deviceHandle) {
140             if (devManager.di[i].callbacks.writeRegister != NULL) {
141                 return devManager.di[i].callbacks.writeRegister(devManager.di[i].ref, regBank->name, regIndex, value);
142             }
143         }
144     }
145     return 0;
146 }
147 
debugDeviceWriteIoPort(DbgIoPorts * ioPorts,int portIndex,UInt32 value)148 int debugDeviceWriteIoPort(DbgIoPorts* ioPorts, int portIndex, UInt32 value)
149 {
150     int i;
151 
152     for (i = 0; i < devManager.count; i++) {
153         if (devManager.di[i].handle == ioPorts->deviceHandle) {
154             if (devManager.di[i].callbacks.writeIoPort != NULL) {
155                 return devManager.di[i].callbacks.writeIoPort(devManager.di[i].ref, ioPorts->name, portIndex, value);
156             }
157         }
158     }
159     return 0;
160 }
161 
dbgDeviceCreate(int handle)162 DbgDevice* dbgDeviceCreate(int handle)
163 {
164     DbgDevice* device = calloc(1, sizeof(DbgDevice));
165 
166     strcpy(device->name, devManager.di[handle].name);
167     device->deviceHandle = devManager.di[handle].handle;
168 
169     return device;
170 }
171 
dbgDeviceAddMemoryBlock(DbgDevice * dbgDevice,const char * name,int writeProtected,UInt32 startAddress,UInt32 size,UInt8 * memory)172 DbgMemoryBlock* dbgDeviceAddMemoryBlock(DbgDevice* dbgDevice,
173                                         const char* name,
174                                         int   writeProtected,
175                                         UInt32 startAddress,
176                                         UInt32 size,
177                                         UInt8* memory)
178 {
179     DbgMemoryBlock* mem;
180     int i;
181     for (i = 0; i < MAX_DBG_COMPONENTS; i++) {
182         if (dbgDevice->memoryBlock[i] == NULL) {
183             break;
184         }
185     }
186 
187     if (i == MAX_DBG_COMPONENTS) {
188         return NULL;
189     }
190 
191     mem = malloc(sizeof(DbgMemoryBlock) + size);
192     strcpy(mem->name, name);
193     mem->writeProtected = writeProtected;
194     mem->startAddress = startAddress;
195     mem->size = size;
196     mem->deviceHandle = dbgDevice->deviceHandle;
197     memcpy(mem->memory, memory, size);
198 
199     dbgDevice->memoryBlock[i] = mem;
200     dbgDevice->memoryBlockCount = i + 1;
201 
202     return mem;
203 }
204 
dbgDeviceAddCallstack(DbgDevice * dbgDevice,const char * name,UInt16 * callstack,int size)205 DbgCallstack* dbgDeviceAddCallstack(DbgDevice* dbgDevice,
206                                     const char* name,
207                                     UInt16* callstack,
208                                     int size)
209 {
210     DbgCallstack* stack;
211     int i;
212 
213     if (dbgDevice->callstack != NULL) {
214         return NULL;
215     }
216     stack = malloc(sizeof(DbgCallstack) + sizeof(UInt32) * size);
217     for (i = 0; i < size; i++) {
218         stack->callstack[i] = callstack[i];
219     }
220     stack->size = size;
221     stack->deviceHandle = dbgDevice->deviceHandle;
222     strcpy(stack->name, name);
223 
224     dbgDevice->callstack = stack;
225     return stack;
226 }
227 
228 
dbgDeviceAddRegisterBank(DbgDevice * dbgDevice,const char * name,UInt32 registerCount)229 DbgRegisterBank* dbgDeviceAddRegisterBank(DbgDevice* dbgDevice,
230                                           const char* name,
231                                           UInt32 registerCount)
232 {
233     DbgRegisterBank* regBank;
234     int i;
235     for (i = 0; i < MAX_DBG_COMPONENTS; i++) {
236         if (dbgDevice->registerBank[i] == NULL) {
237             break;
238         }
239     }
240 
241     if (i == MAX_DBG_COMPONENTS) {
242         return NULL;
243     }
244 
245     regBank = calloc(1, sizeof(DbgRegisterBank) + registerCount * sizeof(struct DbgRegister));
246     strcpy(regBank->name, name);
247     regBank->count = registerCount;
248     regBank->deviceHandle = dbgDevice->deviceHandle;
249 
250     dbgDevice->registerBank[i] = regBank;
251     dbgDevice->registerBankCount = i + 1;
252 
253     return regBank;
254 }
255 
dbgRegisterBankAddRegister(DbgRegisterBank * regBank,int index,const char * name,UInt8 width,UInt32 value)256 void dbgRegisterBankAddRegister(DbgRegisterBank* regBank,
257                                 int index,
258                                 const char* name,
259                                 UInt8 width,
260                                 UInt32 value)
261 {
262     strcpy(regBank->reg[index].name, name);
263     regBank->reg[index].width = width;
264     regBank->reg[index].value = value;
265 }
266 
dbgDeviceAddIoPorts(DbgDevice * dbgDevice,const char * name,UInt32 ioPortsCount)267 DbgIoPorts* dbgDeviceAddIoPorts(DbgDevice* dbgDevice,
268                                 const char* name,
269                                 UInt32 ioPortsCount)
270 {
271     DbgIoPorts* ioPorts;
272     int i;
273     for (i = 0; i < MAX_DBG_COMPONENTS; i++) {
274         if (dbgDevice->ioPorts[i] == NULL) {
275             break;
276         }
277     }
278 
279     if (i == MAX_DBG_COMPONENTS) {
280         return NULL;
281     }
282 
283     ioPorts = calloc(1, sizeof(DbgIoPorts) + ioPortsCount * sizeof(struct DbgIoPort));
284     strcpy(ioPorts->name, name);
285     ioPorts->count = ioPortsCount;
286     ioPorts->deviceHandle = dbgDevice->deviceHandle;
287 
288     dbgDevice->ioPorts[i] = ioPorts;
289     dbgDevice->ioPortsCount = i + 1;
290 
291     return ioPorts;
292 }
293 
dbgIoPortsAddPort(DbgIoPorts * ioPorts,int index,UInt16 port,DbgIoPortDirection direction,UInt8 value)294 void dbgIoPortsAddPort(DbgIoPorts* ioPorts,
295                        int index,
296                        UInt16 port,
297                        DbgIoPortDirection direction,
298                        UInt8 value)
299 {
300     if (index >= 0 && (UInt32)index < ioPorts->count) {
301         ioPorts->port[index].port       = port;
302         ioPorts->port[index].direction  = direction;
303         ioPorts->port[index].value      = value;
304     }
305 }
306 
307 typedef struct Watchpoint {
308     struct Watchpoint* next;
309     int address;
310     DbgWatchpointCondition condition;
311     UInt32 refValue;
312     int size;
313 } Watchpoint;
314 
315 Watchpoint* watchpoints[MAX_DEVICES];
316 
debugDeviceSetMemoryWatchpoint(DbgDeviceType devType,int address,DbgWatchpointCondition condition,UInt32 refValue,int size)317 void debugDeviceSetMemoryWatchpoint(DbgDeviceType devType, int address, DbgWatchpointCondition condition, UInt32 refValue, int size)
318 {
319     Watchpoint* watchpoint = watchpoints[devType];
320 
321     while (watchpoint != NULL) {
322         if (watchpoint->address == address) {
323             break;
324         }
325         watchpoint = watchpoint->next;
326     }
327     if (watchpoint == NULL) {
328         watchpoint = (Watchpoint*)calloc(1, sizeof(Watchpoint));
329         watchpoint->next = watchpoints[devType];
330         watchpoints[devType] = watchpoint;
331     }
332 
333     watchpoint->address = address;
334     watchpoint->condition = condition;
335     watchpoint->refValue = refValue;
336     watchpoint->size = size;
337 }
338 
debugDeviceClearMemoryWatchpoint(DbgDeviceType devType,int address)339 void debugDeviceClearMemoryWatchpoint(DbgDeviceType devType, int address)
340 {
341     Watchpoint* watchpoint = watchpoints[devType];
342     Watchpoint* prevWatchpoint = NULL;
343     while (watchpoint != NULL) {
344         if (watchpoint->address == address) {
345             if (prevWatchpoint == NULL) {
346                 watchpoints[devType] = watchpoint->next;
347             }
348             else {
349                 prevWatchpoint->next = watchpoint->next;
350             }
351             free(watchpoint);
352             break;
353         }
354         prevWatchpoint = watchpoint;
355         watchpoint = watchpoint->next;
356     }
357 }
358 
tryWatchpoint(DbgDeviceType devType,int address,UInt8 value,void * ref,WatchpointReadMemCallback callback)359 void tryWatchpoint(DbgDeviceType devType, int address, UInt8 value, void* ref, WatchpointReadMemCallback callback) {
360     Watchpoint* watchpoint = watchpoints[devType];
361     while (watchpoint != NULL) {
362         if (address >= watchpoint->address && address < watchpoint->address + watchpoint->size) {
363             UInt32 checkValue = 0;
364             int breakpointHit = 0;
365             if (watchpoint->size == 1) {
366                 checkValue = value;
367             }
368             else {
369                 int i;
370                 for (i = 0; i < watchpoint->size; i++) {
371                     checkValue <<= 8;
372                     if (callback) {
373                         checkValue |= callback(ref, watchpoint->address + i);
374                     }
375                     else if (watchpoint->address + i == address) {
376                         checkValue |= value;
377                     }
378                 }
379             }
380             switch (watchpoint->condition) {
381             case DBGWP_ANY:
382                 breakpointHit = 1;
383                 break;
384             case DBGWP_EQUALS:
385                 breakpointHit = checkValue == watchpoint->refValue;
386                 break;
387             case DBGWP_NOT_EQUALS:
388                 breakpointHit = checkValue != watchpoint->refValue;
389                 break;
390             case DBGWP_GREATER_THAN:
391                 breakpointHit = checkValue > watchpoint->refValue;
392                 break;
393             case DBGWP_LESS_THAN:
394                 breakpointHit = checkValue < watchpoint->refValue;
395                 break;
396             }
397             if (breakpointHit) {
398                 boardOnBreakpoint(0);
399                 return;
400             }
401         }
402         watchpoint = watchpoint->next;
403     }
404 }
405