kulp / tenyr
1
#include <stdlib.h>
2
#include <string.h>
3

4
#include "common.h"
5
#include "device.h"
6
#include "sim.h"
7
#include "ram.h"
8

9
struct ram_state {
10
    size_t memsize;
11
    int32_t *mem;
12
    int32_t base;
13
};
14

15 1
static int ram_init(struct plugin_cookie *pcookie, struct device *device, void *cookie)
16
{
17 1
    struct ram_state *ram = *(void**)cookie;
18

19 1
    if (!ram) {
20 1
        int32_t base = device->bounds[0];
21 1
        int32_t end  = device->bounds[1];
22 1
        ram = *(void**)cookie = malloc(sizeof *ram);
23 1
        ram->memsize = (size_t)(end - base + 1);
24 1
        ram->mem = malloc(ram->memsize * sizeof *ram->mem);
25 1
        ram->base = base;
26
    }
27

28
    // set all 32-bit words to 0xffffffff, the trap instruction
29 1
    memset(ram->mem, 0xff, ram->memsize * sizeof *ram->mem);
30

31 1
    return 0;
32
}
33

34 1
static int ram_fini(void *cookie)
35
{
36 1
    struct ram_state *ram = cookie;
37 1
    free(ram->mem);
38 1
    free(ram);
39

40 1
    return 0;
41
}
42

43 1
static int ram_op(void *cookie, int op, int32_t addr, int32_t *data)
44
{
45 1
    struct ram_state *ram = cookie;
46

47 1
    if (op == OP_WRITE)
48 1
        ram->mem[addr - ram->base] = *data;
49 1
    else if (op == OP_INSN_READ || op == OP_DATA_READ)
50 1
        *data = ram->mem[addr - ram->base];
51
    else
52 0
        return 1;
53

54 1
    return 0;
55
}
56

57 1
int ram_add_device_sized(struct device *device, int32_t base, int32_t len)
58
{
59 1
    *device = (struct device){
60 1
        .bounds = { base, base + len - 1 },
61
        .ops = {
62
            .op = ram_op,
63
            .init = ram_init,
64
            .fini = ram_fini,
65
        },
66
    };
67

68 1
    return 0;
69
}
70

71 1
int ram_add_device(struct device *device)
72
{
73 1
    return ram_add_device_sized(device, RAM_BASE, RAM_END - RAM_BASE + 1);
74
}
75

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

Read our documentation on viewing source code .

Loading