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