root / hw / max111x.c @ 7fc2f2c0
History | View | Annotate | Download (4 kB)
1 | c824cacd | balrog | /*
|
---|---|---|---|
2 | c824cacd | balrog | * Maxim MAX1110/1111 ADC chip emulation.
|
3 | c824cacd | balrog | *
|
4 | c824cacd | balrog | * Copyright (c) 2006 Openedhand Ltd.
|
5 | c824cacd | balrog | * Written by Andrzej Zaborowski <balrog@zabor.org>
|
6 | c824cacd | balrog | *
|
7 | c824cacd | balrog | * This code is licensed under the GNU GPLv2.
|
8 | c824cacd | balrog | */
|
9 | c824cacd | balrog | |
10 | a984a69e | Paul Brook | #include "ssi.h" |
11 | c824cacd | balrog | |
12 | a984a69e | Paul Brook | typedef struct { |
13 | a984a69e | Paul Brook | SSISlave ssidev; |
14 | c824cacd | balrog | qemu_irq interrupt; |
15 | c824cacd | balrog | uint8_t tb1, rb2, rb3; |
16 | c824cacd | balrog | int cycle;
|
17 | c824cacd | balrog | |
18 | c824cacd | balrog | int input[8]; |
19 | c824cacd | balrog | int inputs, com;
|
20 | a984a69e | Paul Brook | } MAX111xState; |
21 | c824cacd | balrog | |
22 | c824cacd | balrog | /* Control-byte bitfields */
|
23 | c824cacd | balrog | #define CB_PD0 (1 << 0) |
24 | c824cacd | balrog | #define CB_PD1 (1 << 1) |
25 | c824cacd | balrog | #define CB_SGL (1 << 2) |
26 | c824cacd | balrog | #define CB_UNI (1 << 3) |
27 | c824cacd | balrog | #define CB_SEL0 (1 << 4) |
28 | c824cacd | balrog | #define CB_SEL1 (1 << 5) |
29 | c824cacd | balrog | #define CB_SEL2 (1 << 6) |
30 | c824cacd | balrog | #define CB_START (1 << 7) |
31 | c824cacd | balrog | |
32 | c824cacd | balrog | #define CHANNEL_NUM(v, b0, b1, b2) \
|
33 | c824cacd | balrog | ((((v) >> (2 + (b0))) & 4) | \ |
34 | c824cacd | balrog | (((v) >> (3 + (b1))) & 2) | \ |
35 | c824cacd | balrog | (((v) >> (4 + (b2))) & 1)) |
36 | c824cacd | balrog | |
37 | a984a69e | Paul Brook | static uint32_t max111x_read(MAX111xState *s)
|
38 | c824cacd | balrog | { |
39 | c824cacd | balrog | if (!s->tb1)
|
40 | c824cacd | balrog | return 0; |
41 | c824cacd | balrog | |
42 | c824cacd | balrog | switch (s->cycle ++) {
|
43 | c824cacd | balrog | case 1: |
44 | c824cacd | balrog | return s->rb2;
|
45 | c824cacd | balrog | case 2: |
46 | c824cacd | balrog | return s->rb3;
|
47 | c824cacd | balrog | } |
48 | c824cacd | balrog | |
49 | c824cacd | balrog | return 0; |
50 | c824cacd | balrog | } |
51 | c824cacd | balrog | |
52 | c824cacd | balrog | /* Interpret a control-byte */
|
53 | a984a69e | Paul Brook | static void max111x_write(MAX111xState *s, uint32_t value) |
54 | c824cacd | balrog | { |
55 | c824cacd | balrog | int measure, chan;
|
56 | c824cacd | balrog | |
57 | c824cacd | balrog | /* Ignore the value if START bit is zero */
|
58 | c824cacd | balrog | if (!(value & CB_START))
|
59 | c824cacd | balrog | return;
|
60 | c824cacd | balrog | |
61 | c824cacd | balrog | s->cycle = 0;
|
62 | c824cacd | balrog | |
63 | c824cacd | balrog | if (!(value & CB_PD1)) {
|
64 | c824cacd | balrog | s->tb1 = 0;
|
65 | c824cacd | balrog | return;
|
66 | c824cacd | balrog | } |
67 | c824cacd | balrog | |
68 | c824cacd | balrog | s->tb1 = value; |
69 | c824cacd | balrog | |
70 | c824cacd | balrog | if (s->inputs == 8) |
71 | c824cacd | balrog | chan = CHANNEL_NUM(value, 1, 0, 2); |
72 | c824cacd | balrog | else
|
73 | c824cacd | balrog | chan = CHANNEL_NUM(value & ~CB_SEL0, 0, 1, 2); |
74 | c824cacd | balrog | |
75 | c824cacd | balrog | if (value & CB_SGL)
|
76 | c824cacd | balrog | measure = s->input[chan] - s->com; |
77 | c824cacd | balrog | else
|
78 | c824cacd | balrog | measure = s->input[chan] - s->input[chan ^ 1];
|
79 | c824cacd | balrog | |
80 | c824cacd | balrog | if (!(value & CB_UNI))
|
81 | c824cacd | balrog | measure ^= 0x80;
|
82 | c824cacd | balrog | |
83 | c824cacd | balrog | s->rb2 = (measure >> 2) & 0x3f; |
84 | c824cacd | balrog | s->rb3 = (measure << 6) & 0xc0; |
85 | c824cacd | balrog | |
86 | a984a69e | Paul Brook | /* FIXME: When should the IRQ be lowered? */
|
87 | a984a69e | Paul Brook | qemu_irq_raise(s->interrupt); |
88 | a984a69e | Paul Brook | } |
89 | a984a69e | Paul Brook | |
90 | a984a69e | Paul Brook | static uint32_t max111x_transfer(SSISlave *dev, uint32_t value)
|
91 | a984a69e | Paul Brook | { |
92 | a984a69e | Paul Brook | MAX111xState *s = FROM_SSI_SLAVE(MAX111xState, dev); |
93 | a984a69e | Paul Brook | max111x_write(s, value); |
94 | a984a69e | Paul Brook | return max111x_read(s);
|
95 | c824cacd | balrog | } |
96 | c824cacd | balrog | |
97 | aa941b94 | balrog | static void max111x_save(QEMUFile *f, void *opaque) |
98 | aa941b94 | balrog | { |
99 | bc24a225 | Paul Brook | MAX111xState *s = (MAX111xState *) opaque; |
100 | aa941b94 | balrog | int i;
|
101 | aa941b94 | balrog | |
102 | aa941b94 | balrog | qemu_put_8s(f, &s->tb1); |
103 | aa941b94 | balrog | qemu_put_8s(f, &s->rb2); |
104 | aa941b94 | balrog | qemu_put_8s(f, &s->rb3); |
105 | aa941b94 | balrog | qemu_put_be32(f, s->inputs); |
106 | aa941b94 | balrog | qemu_put_be32(f, s->com); |
107 | aa941b94 | balrog | for (i = 0; i < s->inputs; i ++) |
108 | aa941b94 | balrog | qemu_put_byte(f, s->input[i]); |
109 | aa941b94 | balrog | } |
110 | aa941b94 | balrog | |
111 | aa941b94 | balrog | static int max111x_load(QEMUFile *f, void *opaque, int version_id) |
112 | aa941b94 | balrog | { |
113 | bc24a225 | Paul Brook | MAX111xState *s = (MAX111xState *) opaque; |
114 | aa941b94 | balrog | int i;
|
115 | aa941b94 | balrog | |
116 | aa941b94 | balrog | qemu_get_8s(f, &s->tb1); |
117 | aa941b94 | balrog | qemu_get_8s(f, &s->rb2); |
118 | aa941b94 | balrog | qemu_get_8s(f, &s->rb3); |
119 | aa941b94 | balrog | if (s->inputs != qemu_get_be32(f))
|
120 | aa941b94 | balrog | return -EINVAL;
|
121 | aa941b94 | balrog | s->com = qemu_get_be32(f); |
122 | aa941b94 | balrog | for (i = 0; i < s->inputs; i ++) |
123 | aa941b94 | balrog | s->input[i] = qemu_get_byte(f); |
124 | aa941b94 | balrog | |
125 | aa941b94 | balrog | return 0; |
126 | aa941b94 | balrog | } |
127 | aa941b94 | balrog | |
128 | 81a322d4 | Gerd Hoffmann | static int max111x_init(SSISlave *dev, int inputs) |
129 | c824cacd | balrog | { |
130 | a984a69e | Paul Brook | MAX111xState *s = FROM_SSI_SLAVE(MAX111xState, dev); |
131 | c824cacd | balrog | |
132 | a984a69e | Paul Brook | qdev_init_gpio_out(&dev->qdev, &s->interrupt, 1);
|
133 | c824cacd | balrog | |
134 | a984a69e | Paul Brook | s->inputs = inputs; |
135 | c824cacd | balrog | /* TODO: add a user interface for setting these */
|
136 | c824cacd | balrog | s->input[0] = 0xf0; |
137 | c824cacd | balrog | s->input[1] = 0xe0; |
138 | c824cacd | balrog | s->input[2] = 0xd0; |
139 | c824cacd | balrog | s->input[3] = 0xc0; |
140 | c824cacd | balrog | s->input[4] = 0xb0; |
141 | c824cacd | balrog | s->input[5] = 0xa0; |
142 | c824cacd | balrog | s->input[6] = 0x90; |
143 | c824cacd | balrog | s->input[7] = 0x80; |
144 | c824cacd | balrog | s->com = 0;
|
145 | aa941b94 | balrog | |
146 | 18be5187 | pbrook | register_savevm("max111x", -1, 0, max111x_save, max111x_load, s); |
147 | 81a322d4 | Gerd Hoffmann | return 0; |
148 | c824cacd | balrog | } |
149 | c824cacd | balrog | |
150 | 81a322d4 | Gerd Hoffmann | static int max1110_init(SSISlave *dev) |
151 | c824cacd | balrog | { |
152 | 81a322d4 | Gerd Hoffmann | return max111x_init(dev, 8); |
153 | c824cacd | balrog | } |
154 | c824cacd | balrog | |
155 | 81a322d4 | Gerd Hoffmann | static int max1111_init(SSISlave *dev) |
156 | c824cacd | balrog | { |
157 | 81a322d4 | Gerd Hoffmann | return max111x_init(dev, 4); |
158 | c824cacd | balrog | } |
159 | c824cacd | balrog | |
160 | a984a69e | Paul Brook | void max111x_set_input(DeviceState *dev, int line, uint8_t value) |
161 | c824cacd | balrog | { |
162 | a984a69e | Paul Brook | MAX111xState *s = FROM_SSI_SLAVE(MAX111xState, SSI_SLAVE_FROM_QDEV(dev)); |
163 | a984a69e | Paul Brook | assert(line >= 0 && line < s->inputs);
|
164 | c824cacd | balrog | s->input[line] = value; |
165 | c824cacd | balrog | } |
166 | a984a69e | Paul Brook | |
167 | a984a69e | Paul Brook | static SSISlaveInfo max1110_info = {
|
168 | 074f2fff | Gerd Hoffmann | .qdev.name = "max1110",
|
169 | 074f2fff | Gerd Hoffmann | .qdev.size = sizeof(MAX111xState),
|
170 | a984a69e | Paul Brook | .init = max1110_init, |
171 | a984a69e | Paul Brook | .transfer = max111x_transfer |
172 | a984a69e | Paul Brook | }; |
173 | a984a69e | Paul Brook | |
174 | a984a69e | Paul Brook | static SSISlaveInfo max1111_info = {
|
175 | 074f2fff | Gerd Hoffmann | .qdev.name = "max1111",
|
176 | 074f2fff | Gerd Hoffmann | .qdev.size = sizeof(MAX111xState),
|
177 | a984a69e | Paul Brook | .init = max1111_init, |
178 | a984a69e | Paul Brook | .transfer = max111x_transfer |
179 | a984a69e | Paul Brook | }; |
180 | a984a69e | Paul Brook | |
181 | a984a69e | Paul Brook | static void max111x_register_devices(void) |
182 | a984a69e | Paul Brook | { |
183 | 074f2fff | Gerd Hoffmann | ssi_register_slave(&max1110_info); |
184 | 074f2fff | Gerd Hoffmann | ssi_register_slave(&max1111_info); |
185 | a984a69e | Paul Brook | } |
186 | a984a69e | Paul Brook | |
187 | a984a69e | Paul Brook | device_init(max111x_register_devices) |