root / hw / max111x.c @ 91834991
History | View | Annotate | Download (3.6 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 | 87ecb68b | pbrook | #include "hw.h" |
11 | 87ecb68b | pbrook | #include "i2c.h" |
12 | c824cacd | balrog | |
13 | c824cacd | balrog | struct max111x_s {
|
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 | c824cacd | balrog | }; |
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 | c824cacd | balrog | uint32_t max111x_read(void *opaque)
|
38 | c824cacd | balrog | { |
39 | c824cacd | balrog | struct max111x_s *s = (struct max111x_s *) opaque; |
40 | c824cacd | balrog | |
41 | c824cacd | balrog | if (!s->tb1)
|
42 | c824cacd | balrog | return 0; |
43 | c824cacd | balrog | |
44 | c824cacd | balrog | switch (s->cycle ++) {
|
45 | c824cacd | balrog | case 1: |
46 | c824cacd | balrog | return s->rb2;
|
47 | c824cacd | balrog | case 2: |
48 | c824cacd | balrog | return s->rb3;
|
49 | c824cacd | balrog | } |
50 | c824cacd | balrog | |
51 | c824cacd | balrog | return 0; |
52 | c824cacd | balrog | } |
53 | c824cacd | balrog | |
54 | c824cacd | balrog | /* Interpret a control-byte */
|
55 | c824cacd | balrog | void max111x_write(void *opaque, uint32_t value) |
56 | c824cacd | balrog | { |
57 | c824cacd | balrog | struct max111x_s *s = (struct max111x_s *) opaque; |
58 | c824cacd | balrog | int measure, chan;
|
59 | c824cacd | balrog | |
60 | c824cacd | balrog | /* Ignore the value if START bit is zero */
|
61 | c824cacd | balrog | if (!(value & CB_START))
|
62 | c824cacd | balrog | return;
|
63 | c824cacd | balrog | |
64 | c824cacd | balrog | s->cycle = 0;
|
65 | c824cacd | balrog | |
66 | c824cacd | balrog | if (!(value & CB_PD1)) {
|
67 | c824cacd | balrog | s->tb1 = 0;
|
68 | c824cacd | balrog | return;
|
69 | c824cacd | balrog | } |
70 | c824cacd | balrog | |
71 | c824cacd | balrog | s->tb1 = value; |
72 | c824cacd | balrog | |
73 | c824cacd | balrog | if (s->inputs == 8) |
74 | c824cacd | balrog | chan = CHANNEL_NUM(value, 1, 0, 2); |
75 | c824cacd | balrog | else
|
76 | c824cacd | balrog | chan = CHANNEL_NUM(value & ~CB_SEL0, 0, 1, 2); |
77 | c824cacd | balrog | |
78 | c824cacd | balrog | if (value & CB_SGL)
|
79 | c824cacd | balrog | measure = s->input[chan] - s->com; |
80 | c824cacd | balrog | else
|
81 | c824cacd | balrog | measure = s->input[chan] - s->input[chan ^ 1];
|
82 | c824cacd | balrog | |
83 | c824cacd | balrog | if (!(value & CB_UNI))
|
84 | c824cacd | balrog | measure ^= 0x80;
|
85 | c824cacd | balrog | |
86 | c824cacd | balrog | s->rb2 = (measure >> 2) & 0x3f; |
87 | c824cacd | balrog | s->rb3 = (measure << 6) & 0xc0; |
88 | c824cacd | balrog | |
89 | c824cacd | balrog | if (s->interrupt)
|
90 | c824cacd | balrog | qemu_irq_raise(s->interrupt); |
91 | c824cacd | balrog | } |
92 | c824cacd | balrog | |
93 | aa941b94 | balrog | static void max111x_save(QEMUFile *f, void *opaque) |
94 | aa941b94 | balrog | { |
95 | aa941b94 | balrog | struct max111x_s *s = (struct max111x_s *) opaque; |
96 | aa941b94 | balrog | int i;
|
97 | aa941b94 | balrog | |
98 | aa941b94 | balrog | qemu_put_8s(f, &s->tb1); |
99 | aa941b94 | balrog | qemu_put_8s(f, &s->rb2); |
100 | aa941b94 | balrog | qemu_put_8s(f, &s->rb3); |
101 | aa941b94 | balrog | qemu_put_be32(f, s->inputs); |
102 | aa941b94 | balrog | qemu_put_be32(f, s->com); |
103 | aa941b94 | balrog | for (i = 0; i < s->inputs; i ++) |
104 | aa941b94 | balrog | qemu_put_byte(f, s->input[i]); |
105 | aa941b94 | balrog | } |
106 | aa941b94 | balrog | |
107 | aa941b94 | balrog | static int max111x_load(QEMUFile *f, void *opaque, int version_id) |
108 | aa941b94 | balrog | { |
109 | aa941b94 | balrog | struct max111x_s *s = (struct max111x_s *) opaque; |
110 | aa941b94 | balrog | int i;
|
111 | aa941b94 | balrog | |
112 | aa941b94 | balrog | qemu_get_8s(f, &s->tb1); |
113 | aa941b94 | balrog | qemu_get_8s(f, &s->rb2); |
114 | aa941b94 | balrog | qemu_get_8s(f, &s->rb3); |
115 | aa941b94 | balrog | if (s->inputs != qemu_get_be32(f))
|
116 | aa941b94 | balrog | return -EINVAL;
|
117 | aa941b94 | balrog | s->com = qemu_get_be32(f); |
118 | aa941b94 | balrog | for (i = 0; i < s->inputs; i ++) |
119 | aa941b94 | balrog | s->input[i] = qemu_get_byte(f); |
120 | aa941b94 | balrog | |
121 | aa941b94 | balrog | return 0; |
122 | aa941b94 | balrog | } |
123 | aa941b94 | balrog | |
124 | aa941b94 | balrog | static int max111x_iid = 0; |
125 | aa941b94 | balrog | |
126 | c824cacd | balrog | static struct max111x_s *max111x_init(qemu_irq cb) |
127 | c824cacd | balrog | { |
128 | c824cacd | balrog | struct max111x_s *s;
|
129 | c824cacd | balrog | s = (struct max111x_s *)
|
130 | c824cacd | balrog | qemu_mallocz(sizeof(struct max111x_s)); |
131 | c824cacd | balrog | memset(s, 0, sizeof(struct max111x_s)); |
132 | c824cacd | balrog | |
133 | c824cacd | balrog | s->interrupt = cb; |
134 | c824cacd | balrog | |
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 | aa941b94 | balrog | register_savevm("max111x", max111x_iid ++, 0, |
147 | aa941b94 | balrog | max111x_save, max111x_load, s); |
148 | aa941b94 | balrog | |
149 | c824cacd | balrog | return s;
|
150 | c824cacd | balrog | } |
151 | c824cacd | balrog | |
152 | c824cacd | balrog | struct max111x_s *max1110_init(qemu_irq cb)
|
153 | c824cacd | balrog | { |
154 | c824cacd | balrog | struct max111x_s *s = max111x_init(cb);
|
155 | c824cacd | balrog | s->inputs = 8;
|
156 | c824cacd | balrog | return s;
|
157 | c824cacd | balrog | } |
158 | c824cacd | balrog | |
159 | c824cacd | balrog | struct max111x_s *max1111_init(qemu_irq cb)
|
160 | c824cacd | balrog | { |
161 | c824cacd | balrog | struct max111x_s *s = max111x_init(cb);
|
162 | c824cacd | balrog | s->inputs = 4;
|
163 | c824cacd | balrog | return s;
|
164 | c824cacd | balrog | } |
165 | c824cacd | balrog | |
166 | c824cacd | balrog | void max111x_set_input(struct max111x_s *s, int line, uint8_t value) |
167 | c824cacd | balrog | { |
168 | c824cacd | balrog | if (line >= s->inputs) {
|
169 | c824cacd | balrog | printf("%s: There's no input %i\n", __FUNCTION__, line);
|
170 | c824cacd | balrog | return;
|
171 | c824cacd | balrog | } |
172 | c824cacd | balrog | |
173 | c824cacd | balrog | s->input[line] = value; |
174 | c824cacd | balrog | } |