kulp / tenyr
1
#include "sim.h"
2

3
#include <stdlib.h>
4

5 1
struct device * new_device(struct sim_state *s)
6
{
7 1
    struct device_list *d = calloc(1, sizeof *d);
8 1
    d->next = NULL;
9 1
    struct device_list ***p = &s->machine.last_device;
10 1
    **p = d;
11 1
    *p = &d->next;
12 1
    return &d->device;
13
}
14

15 1
static int devices_does_match(const int32_t *addr, const struct device *device)
16
{
17 1
    if (*addr <= device->bounds[1]) {
18 1
        if (*addr >= device->bounds[0]) {
19 1
            return 0;
20
        } else {
21 1
            return -1;
22
        }
23
    } else {
24 1
        return 1;
25
    }
26
}
27

28 1
int dispatch_op(void *ud, int op, int32_t addr, int32_t *data)
29
{
30 1
    struct sim_state *s = ud;
31 1
    struct device *device = NULL;
32 1
    for (struct device_list *dl = s->machine.devices; dl && !device; dl = dl->next)
33 1
        if (devices_does_match(&addr, &dl->device) == 0)
34 1
            device = &dl->device;
35

36 1
    if (device == NULL) {
37 1
        fprintf(stderr, "No device handles address %#x\n", addr);
38 1
        return -1;
39
    }
40

41
    // TODO don't send in the whole simulator state ? the op should have
42
    // access to some state, in order to redispatch and potentially use other
43
    // machine.devices, but it shouldn't see the whole state
44 1
    int result = device->ops.op(device->cookie, op, addr, data);
45

46 1
    if (s->conf.verbose > 2) {
47 1
        printf("%-5s @ 0x%08x = 0x%08x\n",
48
               (op == OP_WRITE) ? "write" : "read", addr, *data);
49
    }
50

51 1
    return result;
52
}
53

54 1
int devices_setup(struct sim_state *s)
55
{
56 1
    s->machine.devices = NULL;
57 1
    s->machine.last_device = &s->machine.devices;
58

59 1
    return 0;
60
}
61

62 1
int devices_finalise(struct sim_state *s)
63
{
64 1
    int rc = 0;
65

66 1
    for (struct device_list *d = s->machine.devices; d; d = d->next)
67 1
        rc |= d->device.ops.init(&s->plugin_cookie, &d->device, &d->device.cookie);
68

69 1
    return rc;
70
}
71

72 1
int devices_teardown(struct sim_state *s)
73
{
74 1
    int rc = 0;
75

76 1
    for (struct device_list *d = s->machine.devices, *e; d; d = e) {
77 1
        e = d->next;
78 1
        int result = d->device.ops.fini(d->device.cookie);
79 1
        if (result)
80 1
            debug(1, "Finalising device %p (cookie %p) returned %d",
81
                     d, d->device.cookie, result);
82 1
        rc |= !!result;
83 1
        free(d);
84
    }
85

86 1
    return rc;
87
}
88

89 1
int devices_dispatch_cycle(struct sim_state *s)
90
{
91 1
    int rc = 0;
92 1
    for (struct device_list *d = s->machine.devices; d; d = d->next)
93 1
        if (d->device.ops.cycle)
94 1
            rc |= !!d->device.ops.cycle(d->device.cookie);
95

96 1
    return rc;
97
}
98

99
/* vi: set ts=4 sw=4 et: */

Read our documentation on viewing source code .

Loading