root / hw / max111x.c @ 40f16dd1
History | View | Annotate | Download (3.8 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 | 54d970d1 | Juan Quintela | uint8_t 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 | 38cb3aa9 | Juan Quintela | static const VMStateDescription vmstate_max111x = { |
98 | 38cb3aa9 | Juan Quintela | .name = "max111x",
|
99 | 38cb3aa9 | Juan Quintela | .version_id = 0,
|
100 | 38cb3aa9 | Juan Quintela | .minimum_version_id = 0,
|
101 | 38cb3aa9 | Juan Quintela | .minimum_version_id_old = 0,
|
102 | 38cb3aa9 | Juan Quintela | .fields = (VMStateField[]) { |
103 | 38cb3aa9 | Juan Quintela | VMSTATE_UINT8(tb1, MAX111xState), |
104 | 38cb3aa9 | Juan Quintela | VMSTATE_UINT8(rb2, MAX111xState), |
105 | 38cb3aa9 | Juan Quintela | VMSTATE_UINT8(rb3, MAX111xState), |
106 | 38cb3aa9 | Juan Quintela | VMSTATE_INT32_EQUAL(inputs, MAX111xState), |
107 | 38cb3aa9 | Juan Quintela | VMSTATE_INT32(com, MAX111xState), |
108 | 38cb3aa9 | Juan Quintela | VMSTATE_ARRAY_INT32_UNSAFE(input, MAX111xState, inputs, |
109 | 38cb3aa9 | Juan Quintela | vmstate_info_uint8, uint8_t), |
110 | 38cb3aa9 | Juan Quintela | VMSTATE_END_OF_LIST() |
111 | 38cb3aa9 | Juan Quintela | } |
112 | 38cb3aa9 | Juan Quintela | }; |
113 | aa941b94 | balrog | |
114 | 81a322d4 | Gerd Hoffmann | static int max111x_init(SSISlave *dev, int inputs) |
115 | c824cacd | balrog | { |
116 | a984a69e | Paul Brook | MAX111xState *s = FROM_SSI_SLAVE(MAX111xState, dev); |
117 | c824cacd | balrog | |
118 | a984a69e | Paul Brook | qdev_init_gpio_out(&dev->qdev, &s->interrupt, 1);
|
119 | c824cacd | balrog | |
120 | a984a69e | Paul Brook | s->inputs = inputs; |
121 | c824cacd | balrog | /* TODO: add a user interface for setting these */
|
122 | c824cacd | balrog | s->input[0] = 0xf0; |
123 | c824cacd | balrog | s->input[1] = 0xe0; |
124 | c824cacd | balrog | s->input[2] = 0xd0; |
125 | c824cacd | balrog | s->input[3] = 0xc0; |
126 | c824cacd | balrog | s->input[4] = 0xb0; |
127 | c824cacd | balrog | s->input[5] = 0xa0; |
128 | c824cacd | balrog | s->input[6] = 0x90; |
129 | c824cacd | balrog | s->input[7] = 0x80; |
130 | c824cacd | balrog | s->com = 0;
|
131 | aa941b94 | balrog | |
132 | 38cb3aa9 | Juan Quintela | vmstate_register(&dev->qdev, -1, &vmstate_max111x, s);
|
133 | 81a322d4 | Gerd Hoffmann | return 0; |
134 | c824cacd | balrog | } |
135 | c824cacd | balrog | |
136 | 81a322d4 | Gerd Hoffmann | static int max1110_init(SSISlave *dev) |
137 | c824cacd | balrog | { |
138 | 81a322d4 | Gerd Hoffmann | return max111x_init(dev, 8); |
139 | c824cacd | balrog | } |
140 | c824cacd | balrog | |
141 | 81a322d4 | Gerd Hoffmann | static int max1111_init(SSISlave *dev) |
142 | c824cacd | balrog | { |
143 | 81a322d4 | Gerd Hoffmann | return max111x_init(dev, 4); |
144 | c824cacd | balrog | } |
145 | c824cacd | balrog | |
146 | a984a69e | Paul Brook | void max111x_set_input(DeviceState *dev, int line, uint8_t value) |
147 | c824cacd | balrog | { |
148 | a984a69e | Paul Brook | MAX111xState *s = FROM_SSI_SLAVE(MAX111xState, SSI_SLAVE_FROM_QDEV(dev)); |
149 | a984a69e | Paul Brook | assert(line >= 0 && line < s->inputs);
|
150 | c824cacd | balrog | s->input[line] = value; |
151 | c824cacd | balrog | } |
152 | a984a69e | Paul Brook | |
153 | a984a69e | Paul Brook | static SSISlaveInfo max1110_info = {
|
154 | 074f2fff | Gerd Hoffmann | .qdev.name = "max1110",
|
155 | 074f2fff | Gerd Hoffmann | .qdev.size = sizeof(MAX111xState),
|
156 | a984a69e | Paul Brook | .init = max1110_init, |
157 | a984a69e | Paul Brook | .transfer = max111x_transfer |
158 | a984a69e | Paul Brook | }; |
159 | a984a69e | Paul Brook | |
160 | a984a69e | Paul Brook | static SSISlaveInfo max1111_info = {
|
161 | 074f2fff | Gerd Hoffmann | .qdev.name = "max1111",
|
162 | 074f2fff | Gerd Hoffmann | .qdev.size = sizeof(MAX111xState),
|
163 | a984a69e | Paul Brook | .init = max1111_init, |
164 | a984a69e | Paul Brook | .transfer = max111x_transfer |
165 | a984a69e | Paul Brook | }; |
166 | a984a69e | Paul Brook | |
167 | a984a69e | Paul Brook | static void max111x_register_devices(void) |
168 | a984a69e | Paul Brook | { |
169 | 074f2fff | Gerd Hoffmann | ssi_register_slave(&max1110_info); |
170 | 074f2fff | Gerd Hoffmann | ssi_register_slave(&max1111_info); |
171 | a984a69e | Paul Brook | } |
172 | a984a69e | Paul Brook | |
173 | a984a69e | Paul Brook | device_init(max111x_register_devices) |