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 struct max111x_s *max111x_init(qemu_irq cb)
95 s = (struct max111x_s *)
96 qemu_mallocz(sizeof(struct max111x_s));
97 memset(s, 0, sizeof(struct max111x_s));
101 /* TODO: add a user interface for setting these */
114 struct max111x_s *max1110_init(qemu_irq cb)
116 struct max111x_s *s = max111x_init(cb);
121 struct max111x_s *max1111_init(qemu_irq cb)
123 struct max111x_s *s = max111x_init(cb);
128 void max111x_set_input(struct max111x_s *s, int line, uint8_t value)
130 if (line >= s->inputs) {
131 printf("%s: There's no input %i\n", __FUNCTION__, line);
135 s->input[line] = value;