-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimio_tracer.c
353 lines (287 loc) · 7.77 KB
/
simio_tracer.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* MSPDebug - debugging tool for MSP430 MCUs
* Copyright (C) 2009, 2010 Daniel Beer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <string.h>
#include "simio_device.h"
#include "simio_tracer.h"
#include "expr.h"
#include "output.h"
#include "output_util.h"
#include "dis.h"
#define DEFAULT_HISTORY 16
typedef enum {
EVENT_WRITE_16,
EVENT_READ_16,
EVENT_WRITE_8,
EVENT_READ_8,
EVENT_IRQ_HANDLE,
EVENT_RESET
} event_type_t;
typedef unsigned long long counter_t;
struct event {
counter_t when;
event_type_t what;
address_t addr;
uint16_t data;
};
struct tracer {
struct simio_device base;
/* IO event history ring buffer. */
struct event *history;
int size;
int head;
int tail;
/* Clock and instruction counters. */
counter_t cycles[SIMIO_NUM_CLOCKS];
counter_t inscount;
/* Outstanding interrupt request. */
int irq_request;
/* Verbose mode. */
int verbose;
};
static void event_print(const struct event *e)
{
char name[128];
print_address(e->addr, name, sizeof(name));
printc(" %10lld: ", e->when);
switch (e->what) {
case EVENT_WRITE_16:
printc("write.w => %s 0x%04x\n", name, e->data);
break;
case EVENT_READ_16:
printc("read.w => %s 0x%04x\n", name);
break;
case EVENT_WRITE_8:
printc("write.b => %s 0x%02x\n", name, e->data);
break;
case EVENT_READ_8:
printc("read.b => %s\n", name);
break;
case EVENT_IRQ_HANDLE:
printc("irq handle %d\n", e->addr);
break;
case EVENT_RESET:
printc("system reset\n");
break;
default:
printc("unknown 0x%04x 0x%04x\n", e->addr, e->data);
break;
}
}
static void event_rec(struct tracer *tr, event_type_t what,
address_t addr, uint16_t data)
{
struct event *e = &tr->history[tr->head];
e->when = tr->cycles[SIMIO_MCLK];
e->what = what;
e->addr = addr;
e->data = data;
if (tr->verbose)
event_print(e);
tr->head = (tr->head + 1) % tr->size;
if (tr->head == tr->tail)
tr->tail = (tr->tail + 1) % tr->size;
}
static struct simio_device *tracer_create(char **arg_text)
{
const char *size_text = get_arg(arg_text);
int size = DEFAULT_HISTORY;
struct event *history;
struct tracer *tr;
if (size_text) {
address_t value;
if (expr_eval(size_text, &value) < 0) {
printc_err("tracer: can't parse history size: %s\n",
size_text);
return NULL;
}
size = value;
if (size < 2) {
printc_err("tracer: invalid size: %d\n", size);
return NULL;
}
}
history = malloc(sizeof(history[0]) * size);
if (!history) {
pr_error("tracer: couldn't allocate memory for history");
return NULL;
}
tr = malloc(sizeof(*tr));
if (!tr) {
pr_error("tracer: couldn't allocate memory");
return NULL;
}
memset(tr, 0, sizeof(*tr));
tr->base.type = &simio_tracer;
tr->history = history;
tr->size = size;
tr->irq_request = -1;
return (struct simio_device *)tr;
}
static void tracer_destroy(struct simio_device *dev)
{
struct tracer *tr = (struct tracer *)dev;
free(tr->history);
free(tr);
}
static void tracer_reset(struct simio_device *dev)
{
struct tracer *tr = (struct tracer *)dev;
event_rec(tr, EVENT_RESET, 0, 0);
}
static int tracer_config(struct simio_device *dev,
const char *param, char **arg_text)
{
struct tracer *tr = (struct tracer *)dev;
if (!strcasecmp(param, "verbose"))
tr->verbose = 1;
else if (!strcasecmp(param, "quiet"))
tr->verbose = 0;
else if (!strcasecmp(param, "untrigger"))
tr->irq_request = -1;
else if (!strcasecmp(param, "clear")) {
tr->head = 0;
tr->tail = 0;
memset(tr->cycles, 0, sizeof(tr->cycles));
tr->inscount = 0;
} else if (!strcasecmp(param, "trigger")) {
const char *irq_text = get_arg(arg_text);
address_t value;
if (!irq_text) {
printc_err("tracer: trigger: must specify an IRQ "
"number\n");
return -1;
}
if (expr_eval(irq_text, &value) < 0) {
printc_err("tracer: trigger: can't parse IRQ "
"number: %s\n", irq_text);
return -1;
}
if (value < 0 || value >= 16) {
printc_err("tracer: trigger: invalid IRQ: %d\n",
value);
return -1;
}
tr->irq_request = value;
} else {
printc_err("tracer: unknown config parameter: %s\n", param);
return -1;
}
return 0;
}
static int tracer_info(struct simio_device *dev)
{
struct tracer *tr = (struct tracer *)dev;
int i;
printc("Instruction count: %lld\n", tr->inscount);
printc("MCLK: %lld\n", tr->cycles[SIMIO_MCLK]);
printc("SMCLK %lld\n", tr->cycles[SIMIO_SMCLK]);
printc("ACLK: %lld\n", tr->cycles[SIMIO_ACLK]);
if (tr->irq_request >= 0)
printc("IRQ pending: %d\n", tr->irq_request);
else
printc("No IRQ is pending\n");
printc("\nIO event history (oldest first):\n");
for (i = tr->tail; i != tr->head; i = (i + 1) % tr->size)
event_print(&tr->history[i]);
return 0;
}
static int tracer_write(struct simio_device *dev,
address_t addr, uint16_t data)
{
struct tracer *tr = (struct tracer *)dev;
event_rec(tr, EVENT_WRITE_16, addr, data);
return 1;
}
static int tracer_read(struct simio_device *dev,
address_t addr, uint16_t *data)
{
struct tracer *tr = (struct tracer *)dev;
event_rec(tr, EVENT_READ_16, addr, 0);
return 1;
}
static int tracer_write_b(struct simio_device *dev,
address_t addr, uint8_t data)
{
struct tracer *tr = (struct tracer *)dev;
event_rec(tr, EVENT_WRITE_8, addr, data);
return 1;
}
static int tracer_read_b(struct simio_device *dev,
address_t addr, uint8_t *data)
{
struct tracer *tr = (struct tracer *)dev;
event_rec(tr, EVENT_READ_8, addr, 0);
return 1;
}
static int tracer_check_interrupt(struct simio_device *dev)
{
struct tracer *tr = (struct tracer *)dev;
return tr->irq_request;
}
static void tracer_ack_interrupt(struct simio_device *dev, int irq)
{
struct tracer *tr = (struct tracer *)dev;
if (tr->irq_request == irq)
tr->irq_request = -1;
event_rec(tr, EVENT_IRQ_HANDLE, irq, 0);
}
static void tracer_step(struct simio_device *dev,
uint16_t status, const int *clocks)
{
struct tracer *tr = (struct tracer *)dev;
int i;
for (i = 0; i < SIMIO_NUM_CLOCKS; i++)
tr->cycles[i] += clocks[i];
if (!(status & MSP430_SR_CPUOFF))
tr->inscount++;
}
const struct simio_class simio_tracer = {
.name = "tracer",
.help =
"A debug peripheral to implement IO tracing. This will keep a record of\n"
"IO activity which can be checked at any time. It can also be used to\n"
"manually trigger interrupts.\n"
"\n"
"Constructor arguments: [history-size]\n"
" If specified, enlarge the IO event history from its default size.\n"
"\n"
"Config arguments are:\n"
" verbose\n"
" Show IO events as they occur.\n"
" quiet\n"
" Only show IO events when requested (default).\n"
" trigger <irq>\n"
" Trigger an specific IRQ vector.\n"
" untrigger\n"
" Cancel an interrupt request.\n"
" clear\n"
" Clear the IO history and counter so far.\n",
.create = tracer_create,
.destroy = tracer_destroy,
.reset = tracer_reset,
.config = tracer_config,
.info = tracer_info,
.write = tracer_write,
.read = tracer_read,
.write_b = tracer_write_b,
.read_b = tracer_read_b,
.check_interrupt = tracer_check_interrupt,
.ack_interrupt = tracer_ack_interrupt,
.step = tracer_step
};