Setup stack properly, fixes wrong argc value problem, by Magnus Damm.
[qemu/dscho.git] / hw / max111x.c
blob8425bee576ae1e148b0d11525aaa6385758af3fd
1 /*
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.
8 */
10 #include <vl.h>
12 struct max111x_s {
13 qemu_irq interrupt;
14 uint8_t tb1, rb2, rb3;
15 int cycle;
17 int input[8];
18 int inputs, com;
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;
40 if (!s->tb1)
41 return 0;
43 switch (s->cycle ++) {
44 case 1:
45 return s->rb2;
46 case 2:
47 return s->rb3;
50 return 0;
53 /* Interpret a control-byte */
54 void max111x_write(void *opaque, uint32_t value)
56 struct max111x_s *s = (struct max111x_s *) opaque;
57 int measure, chan;
59 /* Ignore the value if START bit is zero */
60 if (!(value & CB_START))
61 return;
63 s->cycle = 0;
65 if (!(value & CB_PD1)) {
66 s->tb1 = 0;
67 return;
70 s->tb1 = value;
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);
77 if (value & CB_SGL)
78 measure = s->input[chan] - s->com;
79 else
80 measure = s->input[chan] - s->input[chan ^ 1];
82 if (!(value & CB_UNI))
83 measure ^= 0x80;
85 s->rb2 = (measure >> 2) & 0x3f;
86 s->rb3 = (measure << 6) & 0xc0;
88 if (s->interrupt)
89 qemu_irq_raise(s->interrupt);
92 static void max111x_save(QEMUFile *f, void *opaque)
94 struct max111x_s *s = (struct max111x_s *) opaque;
95 int i;
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;
109 int i;
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);
120 return 0;
123 static int max111x_iid = 0;
125 static struct max111x_s *max111x_init(qemu_irq cb)
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));
132 s->interrupt = cb;
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;
145 register_savevm("max111x", max111x_iid ++, 0,
146 max111x_save, max111x_load, s);
148 return s;
151 struct max111x_s *max1110_init(qemu_irq cb)
153 struct max111x_s *s = max111x_init(cb);
154 s->inputs = 8;
155 return s;
158 struct max111x_s *max1111_init(qemu_irq cb)
160 struct max111x_s *s = max111x_init(cb);
161 s->inputs = 4;
162 return s;
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);
169 return;
172 s->input[line] = value;