Statistics
| Branch: | Revision:

root / hw / max111x.c @ aa941b94

History | View | Annotate | Download (3.6 kB)

1
/*
2
 * Maxim MAX1110/1111 ADC chip emulation.
3
 *
4
 * Copyright (c) 2006 Openedhand Ltd.
5
 * Written by Andrzej Zaborowski <balrog@zabor.org>
6
 *
7
 * This code is licensed under the GNU GPLv2.
8
 */
9

    
10
#include <vl.h>
11

    
12
struct max111x_s {
13
    qemu_irq interrupt;
14
    uint8_t tb1, rb2, rb3;
15
    int cycle;
16

    
17
    int input[8];
18
    int inputs, com;
19
};
20

    
21
/* Control-byte bitfields */
22
#define CB_PD0                (1 << 0)
23
#define CB_PD1                (1 << 1)
24
#define CB_SGL                (1 << 2)
25
#define CB_UNI                (1 << 3)
26
#define CB_SEL0                (1 << 4)
27
#define CB_SEL1                (1 << 5)
28
#define CB_SEL2                (1 << 6)
29
#define CB_START        (1 << 7)
30

    
31
#define CHANNEL_NUM(v, b0, b1, b2)        \
32
                        ((((v) >> (2 + (b0))) & 4) |        \
33
                         (((v) >> (3 + (b1))) & 2) |        \
34
                         (((v) >> (4 + (b2))) & 1))
35

    
36
uint32_t max111x_read(void *opaque)
37
{
38
    struct max111x_s *s = (struct max111x_s *) opaque;
39

    
40
    if (!s->tb1)
41
        return 0;
42

    
43
    switch (s->cycle ++) {
44
    case 1:
45
        return s->rb2;
46
    case 2:
47
        return s->rb3;
48
    }
49

    
50
    return 0;
51
}
52

    
53
/* Interpret a control-byte */
54
void max111x_write(void *opaque, uint32_t value)
55
{
56
    struct max111x_s *s = (struct max111x_s *) opaque;
57
    int measure, chan;
58

    
59
    /* Ignore the value if START bit is zero */
60
    if (!(value & CB_START))
61
        return;
62

    
63
    s->cycle = 0;
64

    
65
    if (!(value & CB_PD1)) {
66
        s->tb1 = 0;
67
        return;
68
    }
69

    
70
    s->tb1 = value;
71

    
72
    if (s->inputs == 8)
73
        chan = CHANNEL_NUM(value, 1, 0, 2);
74
    else
75
        chan = CHANNEL_NUM(value & ~CB_SEL0, 0, 1, 2);
76

    
77
    if (value & CB_SGL)
78
        measure = s->input[chan] - s->com;
79
    else
80
        measure = s->input[chan] - s->input[chan ^ 1];
81

    
82
    if (!(value & CB_UNI))
83
        measure ^= 0x80;
84

    
85
    s->rb2 = (measure >> 2) & 0x3f;
86
    s->rb3 = (measure << 6) & 0xc0;
87

    
88
    if (s->interrupt)
89
        qemu_irq_raise(s->interrupt);
90
}
91

    
92
static void max111x_save(QEMUFile *f, void *opaque)
93
{
94
    struct max111x_s *s = (struct max111x_s *) opaque;
95
    int i;
96

    
97
    qemu_put_8s(f, &s->tb1);
98
    qemu_put_8s(f, &s->rb2);
99
    qemu_put_8s(f, &s->rb3);
100
    qemu_put_be32(f, s->inputs);
101
    qemu_put_be32(f, s->com);
102
    for (i = 0; i < s->inputs; i ++)
103
        qemu_put_byte(f, s->input[i]);
104
}
105

    
106
static int max111x_load(QEMUFile *f, void *opaque, int version_id)
107
{
108
    struct max111x_s *s = (struct max111x_s *) opaque;
109
    int i;
110

    
111
    qemu_get_8s(f, &s->tb1);
112
    qemu_get_8s(f, &s->rb2);
113
    qemu_get_8s(f, &s->rb3);
114
    if (s->inputs != qemu_get_be32(f))
115
        return -EINVAL;
116
    s->com = qemu_get_be32(f);
117
    for (i = 0; i < s->inputs; i ++)
118
        s->input[i] = qemu_get_byte(f);
119

    
120
    return 0;
121
}
122

    
123
static int max111x_iid = 0;
124

    
125
static struct max111x_s *max111x_init(qemu_irq cb)
126
{
127
    struct max111x_s *s;
128
    s = (struct max111x_s *)
129
            qemu_mallocz(sizeof(struct max111x_s));
130
    memset(s, 0, sizeof(struct max111x_s));
131

    
132
    s->interrupt = cb;
133

    
134
    /* TODO: add a user interface for setting these */
135
    s->input[0] = 0xf0;
136
    s->input[1] = 0xe0;
137
    s->input[2] = 0xd0;
138
    s->input[3] = 0xc0;
139
    s->input[4] = 0xb0;
140
    s->input[5] = 0xa0;
141
    s->input[6] = 0x90;
142
    s->input[7] = 0x80;
143
    s->com = 0;
144

    
145
    register_savevm("max111x", max111x_iid ++, 0,
146
                    max111x_save, max111x_load, s);
147

    
148
    return s;
149
}
150

    
151
struct max111x_s *max1110_init(qemu_irq cb)
152
{
153
    struct max111x_s *s = max111x_init(cb);
154
    s->inputs = 8;
155
    return s;
156
}
157

    
158
struct max111x_s *max1111_init(qemu_irq cb)
159
{
160
    struct max111x_s *s = max111x_init(cb);
161
    s->inputs = 4;
162
    return s;
163
}
164

    
165
void max111x_set_input(struct max111x_s *s, int line, uint8_t value)
166
{
167
    if (line >= s->inputs) {
168
        printf("%s: There's no input %i\n", __FUNCTION__, line);
169
        return;
170
    }
171

    
172
    s->input[line] = value;
173
}