2 * Maxim MAX1110/1111 ADC chip emulation.
4 * Copyright (c) 2006 Openedhand Ltd.
7 * This code is licensed under the GNU GPLv2.
15 uint8_t tb1, rb2, rb3;
22 /* Control-byte bitfields */
23 #define CB_PD0 (1 << 0)
24 #define CB_PD1 (1 << 1)
25 #define CB_SGL (1 << 2)
26 #define CB_UNI (1 << 3)
27 #define CB_SEL0 (1 << 4)
28 #define CB_SEL1 (1 << 5)
29 #define CB_SEL2 (1 << 6)
30 #define CB_START (1 << 7)
32 #define CHANNEL_NUM(v, b0, b1, b2) \
33 ((((v) >> (2 + (b0))) & 4) | \
34 (((v) >> (3 + (b1))) & 2) | \
35 (((v) >> (4 + (b2))) & 1))
37 uint32_t max111x_read(void *opaque)
39 struct max111x_s *s = (struct max111x_s *) opaque;
44 switch (s->cycle ++) {
54 /* Interpret a control-byte */
55 void max111x_write(void *opaque, uint32_t value)
57 struct max111x_s *s = (struct max111x_s *) opaque;
60 /* Ignore the value if START bit is zero */
61 if (!(value & CB_START))
66 if (!(value & CB_PD1)) {
74 chan = CHANNEL_NUM(value, 1, 0, 2);
76 chan = CHANNEL_NUM(value & ~CB_SEL0, 0, 1, 2);
79 measure = s->input[chan] - s->com;
81 measure = s->input[chan] - s->input[chan ^ 1];
83 if (!(value & CB_UNI))
86 s->rb2 = (measure >> 2) & 0x3f;
87 s->rb3 = (measure << 6) & 0xc0;
90 qemu_irq_raise(s->interrupt);
93 static void max111x_save(QEMUFile *f, void *opaque)
95 struct max111x_s *s = (struct max111x_s *) opaque;
98 qemu_put_8s(f, &s->tb1);
99 qemu_put_8s(f, &s->rb2);
100 qemu_put_8s(f, &s->rb3);
101 qemu_put_be32(f, s->inputs);
102 qemu_put_be32(f, s->com);
103 for (i = 0; i < s->inputs; i ++)
104 qemu_put_byte(f, s->input[i]);
107 static int max111x_load(QEMUFile *f, void *opaque, int version_id)
109 struct max111x_s *s = (struct max111x_s *) opaque;
112 qemu_get_8s(f, &s->tb1);
113 qemu_get_8s(f, &s->rb2);
114 qemu_get_8s(f, &s->rb3);
115 if (s->inputs != qemu_get_be32(f))
117 s->com = qemu_get_be32(f);
118 for (i = 0; i < s->inputs; i ++)
119 s->input[i] = qemu_get_byte(f);
124 static int max111x_iid = 0;
126 static struct max111x_s *max111x_init(qemu_irq cb)
129 s = (struct max111x_s *)
130 qemu_mallocz(sizeof(struct max111x_s));
131 memset(s, 0, sizeof(struct max111x_s));
135 /* TODO: add a user interface for setting these */
146 register_savevm("max111x", max111x_iid ++, 0,
147 max111x_save, max111x_load, s);
152 struct max111x_s *max1110_init(qemu_irq cb)
154 struct max111x_s *s = max111x_init(cb);
159 struct max111x_s *max1111_init(qemu_irq cb)
161 struct max111x_s *s = max111x_init(cb);
166 void max111x_set_input(struct max111x_s *s, int line, uint8_t value)
168 if (line >= s->inputs) {
169 printf("%s: There's no input %i\n", __FUNCTION__, line);
173 s->input[line] = value;