-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdebug.c
285 lines (210 loc) · 5.43 KB
/
debug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <exec/types.h>
#include <exec/execbase.h>
#include <proto/exec.h>
static struct ExecBase* SysBase = NULL;
/**** OS/C/ASM WRAPPERS ******************************************************/
struct CallPutChData {
VOID (*PutChProc)(LONG c, APTR PutChDataPtr);
APTR PutChDataPtr;
};
#if defined(__MORPHOS__)
#include <emul/emulinterface.h>
VOID CallPutChProcMOS(VOID) {
struct CallPutChData* callputchdata = (struct CallPutChData*) REG_A3;
return callputchdata->PutChProc(REG_D0, callputchdata->PutChDataPtr);
}
struct EmulLibEntry CallPutChProc68k = {
TRAP_LIB, 0, CallPutChProcMOS
};
#elif defined(__amithlon__)
VOID CallPutChProc86(LONG c, struct CallPutChData* callputchdata) __attribute__((regparm(3)));
VOID CallPutChProc86(LONG c, struct CallPutChData* callputchdata) {
return callputchdata->PutChProc(c, callputchdata->PutChDataPtr);
}
struct CallPutChProc68k {
UWORD movel_a3_d1;
UWORD jmp;
APTR addr;
} CallPutChProc68k = {
0x220B,
0x4EF9, ((char*) CallPutChProc86) + 1
};
#elif defined(__mc68000__)
// This function sets up d0/a3 with the classical values but also
// pushes Char and PutChData on the stack for C programs.
static const UWORD CallPutChProc68k[] = {
0x2F0B, // MOVE.L A3,-(A7)
0x205B, // MOVEA.L (A3)+,A0
0x2653, // MOVEA.L (A3),A3
0x2F0B, // MOVE.L A3,-(A7)
0x2F00, // MOVE.L D0,-(A7)
0x4E90, // JSR (A0)
0x508F, // ADDQ.L #8,A7
0x205F, // MOVEA.L (A7)+,A0
0x214B, 0x0004, // MOVE.L A3,($0004,A0)
0x2648, // MOVEA.L A0,A3
0x4E75, // RTS
};
#else
# error Unknown OS/CPU
#endif
#if defined(USE_SERIAL)
/**** SERIAL INPUT/OUTPUT ****************************************************/
#include <clib/debug_protos.h>
#if defined(__MORPHOS__)
#undef RawIOInit
#undef RawPutChar
#undef RawMayGetChar
#endif
#include "exec_dbg_inline.h"
#define f(name) k ## name
#define F(name) K ## name
VOID KPutChar(LONG c) {
// Avoid all but one page faults by caching SysBase
if (SysBase == NULL) {
SysBase = *((struct ExecBase**) 4);
}
RawPutChar(c);
}
LONG KMayGetChar(VOID) {
// Avoid all but one page faults by caching SysBase
if (SysBase == NULL) {
SysBase = *((struct ExecBase**) 4);
}
return RawMayGetChar();
}
VOID KDoFmt(CONST_STRPTR formatString, CONST APTR dataStream,
CONST APTR putChProc, APTR putChData) {
struct CallPutChData callputchdata = {
putChProc, putChData
};
// Avoid all but one page faults by caching SysBase
if (SysBase == NULL) {
SysBase = *((struct ExecBase**) 4);
}
RawDoFmt(formatString, dataStream,
(void (*)(void)) &CallPutChProc68k, &callputchdata);
}
#else
/**** PARALLEL INPUT/OUTPUT **************************************************/
#include <clib/ddebug_protos.h>
#define f(name) d ## name
#define F(name) D ## name
LONG PRawMayGetChar(VOID) {
return -1;
}
#endif
/**** COMMON FUNCTIONS *******************************************************/
VOID F(PutStr)(CONST_STRPTR string) {
UBYTE c;
while((c = *string++) != 0) {
F(PutChar)(c);
}
}
LONG KGetChar(VOID) {
LONG c;
while((c = F(MayGetChar)()) < 0);
return c;
}
VOID F(PutFmt)(CONST_STRPTR formatString, CONST APTR values) {
KDoFmt(formatString, values, (void (*)(void)) &KPutChar, NULL);
}
#ifndef __MORPHOS__
VOID F(PrintF)(CONST_STRPTR formatString, ...) {
KDoFmt(formatString, &formatString+1, (void (*)(void)) &KPutChar, NULL);
}
#else
#include <stdarg.h>
static APTR F(PutChPPC)(APTR data, UBYTE c) {
F(PutChar)(c);
return data;
}
VOID F(PrintF)(CONST_STRPTR formatString, ...) {
va_list args;
va_start (args, formatString);
VNewRawDoFmt(formatString, F(PutChPPC), NULL, args);
}
#endif
LONG F(CmpStr)(CONST_STRPTR a, CONST_STRPTR b) {
LONG cnt = 1;
while(TRUE) {
UBYTE c = *a++;
if (c == 0) {
if (*b++ == 0) {
return 0;
}
else {
return -cnt;
}
}
if (c != *b++) {
return cnt;
}
++cnt;
}
}
LONG F(GetNum)(VOID) {
UBYTE c;
LONG num;
BOOL neg;
ULONG cnt;
while (TRUE) {
num = 0;
neg = FALSE;
cnt = 0;
c = KGetChar();
if (c == '-') {
neg = TRUE;
++cnt;
KPutChar(c);
}
while (TRUE) {
if (c == '\b') {
if (cnt > 0) {
KPutChar('\b');
KPutChar(' ');
KPutChar('\b');
--cnt;
}
if (cnt == 0) {
break;
}
else {
num /= 10;
}
}
else if( c >= '0' && c <= '9') {
num = num * 10 + (c - '0');
KPutChar(c);
++cnt;
}
else if (c == '\r') {
return neg ? -num : num;
}
c = KGetChar();
}
}
}
/**** ALIASES ****************************************************************/
#if defined(__ELF__)
# define ALIAS2(a,b) __asm(".set " #a "," #b "; .globl " #a )
#else
# define ALIAS2(a,b) __asm(".stabs \"_" #a "\",11,0,0,0;.stabs \"_" #b "\",1,0,0,0")
#endif
#define ALIAS(new,old) ALIAS2(new,old)
ALIAS(f(putchar),F(PutChar));
ALIAS(f(putc),F(PutChar));
ALIAS(F(PutCh),F(PutChar));
ALIAS(f(putch),F(PutChar));
ALIAS(f(putstr),F(PutStr));
ALIAS(F(PutS),F(PutStr));
ALIAS(f(puts),F(PutStr));
ALIAS(F(MayGetCh),F(MayGetChar));
ALIAS(f(getc),F(GetChar));
ALIAS(f(getchar),F(GetChar));
ALIAS(f(getch),F(GetChar));
ALIAS(F(GetCh),F(GetChar));
ALIAS(F(VPrintF),F(PutFmt));
ALIAS(f(printf),F(PrintF));
ALIAS(cmpstrexec,F(CmpStr));
ALIAS(f(kgetnum),F(GetNum));