2 * Maxim MAX1110/1111 ADC chip emulation.
4 * Copyright (c) 2006 Openedhand Ltd.
5 * Written by Andrzej Zaborowski <balrog@zabor.org>
7 * This code is licensed under the GNU GPLv2.
14 uint8_t tb1, rb2, rb3;
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)
31 #define CHANNEL_NUM(v, b0, b1, b2) \
32 ((((v) >> (2 + (b0))) & 4) | \
33 (((v) >> (3 + (b1))) & 2) | \
34 (((v) >> (4 + (b2))) & 1))
36 uint32_t max111x_read(void *opaque)
38 struct max111x_s *s = (struct max111x_s *) opaque;
43 switch (s->cycle ++) {
53 /* Interpret a control-byte */
54 void max111x_write(void *opaque, uint32_t value)
56 struct max111x_s *s = (struct max111x_s *) opaque;
59 /* Ignore the value if START bit is zero */
60 if (!(value & CB_START))
65 if (!(value & CB_PD1)) {
73 chan = CHANNEL_NUM(value, 1, 0, 2);
75 chan = CHANNEL_NUM(value & ~CB_SEL0, 0, 1, 2);
78 measure = s->input[chan] - s->com;
80 measure = s->input[chan] - s->input[chan ^ 1];
82 if (!(value & CB_UNI))
85 s->rb2 = (measure >> 2) & 0x3f;
86 s->rb3 = (measure << 6) & 0xc0;
89 qemu_irq_raise(s->interrupt);
92 static void max111x_save(QEMUFile *f, void *opaque)
94 struct max111x_s *s = (struct max111x_s *) opaque;
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]);
106 static int max111x_load(QEMUFile *f, void *opaque, int version_id)
108 struct max111x_s *s = (struct max111x_s *) opaque;
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))
116 s->com = qemu_get_be32(f);
117 for (i = 0; i < s->inputs; i ++)
118 s->input[i] = qemu_get_byte(f);
123 static int max111x_iid = 0;
125 static struct max111x_s *max111x_init(qemu_irq cb)
128 s = (struct max111x_s *)
129 qemu_mallocz(sizeof(struct max111x_s));
130 memset(s, 0, sizeof(struct max111x_s));
134 /* TODO: add a user interface for setting these */
145 register_savevm("max111x", max111x_iid ++, 0,
146 max111x_save, max111x_load, s);
151 struct max111x_s *max1110_init(qemu_irq cb)
153 struct max111x_s *s = max111x_init(cb);
158 struct max111x_s *max1111_init(qemu_irq cb)
160 struct max111x_s *s = max111x_init(cb);
165 void max111x_set_input(struct max111x_s *s, int line, uint8_t value)
167 if (line >= s->inputs) {
168 printf("%s: There's no input %i\n", __FUNCTION__, line);
172 s->input[line] = value;