]>
Commit | Line | Data |
---|---|---|
c824cacd AZ |
1 | /* |
2 | * Maxim MAX1110/1111 ADC chip emulation. | |
3 | * | |
4 | * Copyright (c) 2006 Openedhand Ltd. | |
5 | * Written by Andrzej Zaborowski <[email protected]> | |
6 | * | |
7 | * This code is licensed under the GNU GPLv2. | |
6b620ca3 PB |
8 | * |
9 | * Contributions after 2012-01-13 are licensed under the terms of the | |
10 | * GNU GPL, version 2 or (at your option) any later version. | |
c824cacd AZ |
11 | */ |
12 | ||
0d1c9782 | 13 | #include "qemu/osdep.h" |
8fd06719 | 14 | #include "hw/ssi/ssi.h" |
c824cacd | 15 | |
a984a69e | 16 | typedef struct { |
7c77b654 PC |
17 | SSISlave parent_obj; |
18 | ||
c824cacd AZ |
19 | qemu_irq interrupt; |
20 | uint8_t tb1, rb2, rb3; | |
21 | int cycle; | |
22 | ||
54d970d1 | 23 | uint8_t input[8]; |
c824cacd | 24 | int inputs, com; |
a984a69e | 25 | } MAX111xState; |
c824cacd | 26 | |
5ef4a1c3 PC |
27 | #define TYPE_MAX_111X "max111x" |
28 | ||
7c77b654 PC |
29 | #define MAX_111X(obj) \ |
30 | OBJECT_CHECK(MAX111xState, (obj), TYPE_MAX_111X) | |
31 | ||
5ef4a1c3 PC |
32 | #define TYPE_MAX_1110 "max1110" |
33 | #define TYPE_MAX_1111 "max1111" | |
34 | ||
c824cacd AZ |
35 | /* Control-byte bitfields */ |
36 | #define CB_PD0 (1 << 0) | |
37 | #define CB_PD1 (1 << 1) | |
38 | #define CB_SGL (1 << 2) | |
39 | #define CB_UNI (1 << 3) | |
40 | #define CB_SEL0 (1 << 4) | |
41 | #define CB_SEL1 (1 << 5) | |
42 | #define CB_SEL2 (1 << 6) | |
43 | #define CB_START (1 << 7) | |
44 | ||
45 | #define CHANNEL_NUM(v, b0, b1, b2) \ | |
46 | ((((v) >> (2 + (b0))) & 4) | \ | |
47 | (((v) >> (3 + (b1))) & 2) | \ | |
48 | (((v) >> (4 + (b2))) & 1)) | |
49 | ||
a984a69e | 50 | static uint32_t max111x_read(MAX111xState *s) |
c824cacd | 51 | { |
c824cacd AZ |
52 | if (!s->tb1) |
53 | return 0; | |
54 | ||
55 | switch (s->cycle ++) { | |
56 | case 1: | |
57 | return s->rb2; | |
58 | case 2: | |
59 | return s->rb3; | |
60 | } | |
61 | ||
62 | return 0; | |
63 | } | |
64 | ||
65 | /* Interpret a control-byte */ | |
a984a69e | 66 | static void max111x_write(MAX111xState *s, uint32_t value) |
c824cacd | 67 | { |
c824cacd AZ |
68 | int measure, chan; |
69 | ||
70 | /* Ignore the value if START bit is zero */ | |
71 | if (!(value & CB_START)) | |
72 | return; | |
73 | ||
74 | s->cycle = 0; | |
75 | ||
76 | if (!(value & CB_PD1)) { | |
77 | s->tb1 = 0; | |
78 | return; | |
79 | } | |
80 | ||
81 | s->tb1 = value; | |
82 | ||
83 | if (s->inputs == 8) | |
84 | chan = CHANNEL_NUM(value, 1, 0, 2); | |
85 | else | |
86 | chan = CHANNEL_NUM(value & ~CB_SEL0, 0, 1, 2); | |
87 | ||
88 | if (value & CB_SGL) | |
89 | measure = s->input[chan] - s->com; | |
90 | else | |
91 | measure = s->input[chan] - s->input[chan ^ 1]; | |
92 | ||
93 | if (!(value & CB_UNI)) | |
94 | measure ^= 0x80; | |
95 | ||
96 | s->rb2 = (measure >> 2) & 0x3f; | |
97 | s->rb3 = (measure << 6) & 0xc0; | |
98 | ||
a984a69e PB |
99 | /* FIXME: When should the IRQ be lowered? */ |
100 | qemu_irq_raise(s->interrupt); | |
101 | } | |
102 | ||
103 | static uint32_t max111x_transfer(SSISlave *dev, uint32_t value) | |
104 | { | |
7c77b654 | 105 | MAX111xState *s = MAX_111X(dev); |
a984a69e PB |
106 | max111x_write(s, value); |
107 | return max111x_read(s); | |
c824cacd AZ |
108 | } |
109 | ||
38cb3aa9 JQ |
110 | static const VMStateDescription vmstate_max111x = { |
111 | .name = "max111x", | |
66530953 PC |
112 | .version_id = 1, |
113 | .minimum_version_id = 1, | |
8f1e884b | 114 | .fields = (VMStateField[]) { |
7c77b654 | 115 | VMSTATE_SSI_SLAVE(parent_obj, MAX111xState), |
38cb3aa9 JQ |
116 | VMSTATE_UINT8(tb1, MAX111xState), |
117 | VMSTATE_UINT8(rb2, MAX111xState), | |
118 | VMSTATE_UINT8(rb3, MAX111xState), | |
119 | VMSTATE_INT32_EQUAL(inputs, MAX111xState), | |
120 | VMSTATE_INT32(com, MAX111xState), | |
121 | VMSTATE_ARRAY_INT32_UNSAFE(input, MAX111xState, inputs, | |
122 | vmstate_info_uint8, uint8_t), | |
123 | VMSTATE_END_OF_LIST() | |
124 | } | |
125 | }; | |
aa941b94 | 126 | |
1a7d9ee6 | 127 | static int max111x_init(SSISlave *d, int inputs) |
c824cacd | 128 | { |
1a7d9ee6 | 129 | DeviceState *dev = DEVICE(d); |
7c77b654 | 130 | MAX111xState *s = MAX_111X(dev); |
c824cacd | 131 | |
1a7d9ee6 | 132 | qdev_init_gpio_out(dev, &s->interrupt, 1); |
c824cacd | 133 | |
a984a69e | 134 | s->inputs = inputs; |
c824cacd AZ |
135 | /* TODO: add a user interface for setting these */ |
136 | s->input[0] = 0xf0; | |
137 | s->input[1] = 0xe0; | |
138 | s->input[2] = 0xd0; | |
139 | s->input[3] = 0xc0; | |
140 | s->input[4] = 0xb0; | |
141 | s->input[5] = 0xa0; | |
142 | s->input[6] = 0x90; | |
143 | s->input[7] = 0x80; | |
144 | s->com = 0; | |
aa941b94 | 145 | |
1a7d9ee6 | 146 | vmstate_register(dev, -1, &vmstate_max111x, s); |
81a322d4 | 147 | return 0; |
c824cacd AZ |
148 | } |
149 | ||
81a322d4 | 150 | static int max1110_init(SSISlave *dev) |
c824cacd | 151 | { |
81a322d4 | 152 | return max111x_init(dev, 8); |
c824cacd AZ |
153 | } |
154 | ||
81a322d4 | 155 | static int max1111_init(SSISlave *dev) |
c824cacd | 156 | { |
81a322d4 | 157 | return max111x_init(dev, 4); |
c824cacd AZ |
158 | } |
159 | ||
a984a69e | 160 | void max111x_set_input(DeviceState *dev, int line, uint8_t value) |
c824cacd | 161 | { |
7c77b654 | 162 | MAX111xState *s = MAX_111X(dev); |
a984a69e | 163 | assert(line >= 0 && line < s->inputs); |
c824cacd AZ |
164 | s->input[line] = value; |
165 | } | |
a984a69e | 166 | |
5ef4a1c3 | 167 | static void max111x_class_init(ObjectClass *klass, void *data) |
cd6c4cf2 AL |
168 | { |
169 | SSISlaveClass *k = SSI_SLAVE_CLASS(klass); | |
170 | ||
cd6c4cf2 AL |
171 | k->transfer = max111x_transfer; |
172 | } | |
173 | ||
5ef4a1c3 PC |
174 | static const TypeInfo max111x_info = { |
175 | .name = TYPE_MAX_111X, | |
39bffca2 AL |
176 | .parent = TYPE_SSI_SLAVE, |
177 | .instance_size = sizeof(MAX111xState), | |
5ef4a1c3 PC |
178 | .class_init = max111x_class_init, |
179 | .abstract = true, | |
180 | }; | |
181 | ||
182 | static void max1110_class_init(ObjectClass *klass, void *data) | |
183 | { | |
184 | SSISlaveClass *k = SSI_SLAVE_CLASS(klass); | |
185 | ||
186 | k->init = max1110_init; | |
187 | } | |
188 | ||
189 | static const TypeInfo max1110_info = { | |
190 | .name = TYPE_MAX_1110, | |
191 | .parent = TYPE_MAX_111X, | |
39bffca2 | 192 | .class_init = max1110_class_init, |
a984a69e PB |
193 | }; |
194 | ||
cd6c4cf2 AL |
195 | static void max1111_class_init(ObjectClass *klass, void *data) |
196 | { | |
197 | SSISlaveClass *k = SSI_SLAVE_CLASS(klass); | |
198 | ||
199 | k->init = max1111_init; | |
cd6c4cf2 AL |
200 | } |
201 | ||
8c43a6f0 | 202 | static const TypeInfo max1111_info = { |
5ef4a1c3 PC |
203 | .name = TYPE_MAX_1111, |
204 | .parent = TYPE_MAX_111X, | |
39bffca2 | 205 | .class_init = max1111_class_init, |
a984a69e PB |
206 | }; |
207 | ||
83f7d43a | 208 | static void max111x_register_types(void) |
a984a69e | 209 | { |
5ef4a1c3 | 210 | type_register_static(&max111x_info); |
39bffca2 AL |
211 | type_register_static(&max1110_info); |
212 | type_register_static(&max1111_info); | |
a984a69e PB |
213 | } |
214 | ||
83f7d43a | 215 | type_init(max111x_register_types) |