max111x.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. * Contributions after 2012-01-13 are licensed under the terms of the
  10. * GNU GPL, version 2 or (at your option) any later version.
  11. */
  12. #include "hw/ssi.h"
  13. typedef struct {
  14. SSISlave parent_obj;
  15. qemu_irq interrupt;
  16. uint8_t tb1, rb2, rb3;
  17. int cycle;
  18. uint8_t input[8];
  19. int inputs, com;
  20. } MAX111xState;
  21. #define TYPE_MAX_111X "max111x"
  22. #define MAX_111X(obj) \
  23. OBJECT_CHECK(MAX111xState, (obj), TYPE_MAX_111X)
  24. #define TYPE_MAX_1110 "max1110"
  25. #define TYPE_MAX_1111 "max1111"
  26. /* Control-byte bitfields */
  27. #define CB_PD0 (1 << 0)
  28. #define CB_PD1 (1 << 1)
  29. #define CB_SGL (1 << 2)
  30. #define CB_UNI (1 << 3)
  31. #define CB_SEL0 (1 << 4)
  32. #define CB_SEL1 (1 << 5)
  33. #define CB_SEL2 (1 << 6)
  34. #define CB_START (1 << 7)
  35. #define CHANNEL_NUM(v, b0, b1, b2) \
  36. ((((v) >> (2 + (b0))) & 4) | \
  37. (((v) >> (3 + (b1))) & 2) | \
  38. (((v) >> (4 + (b2))) & 1))
  39. static uint32_t max111x_read(MAX111xState *s)
  40. {
  41. if (!s->tb1)
  42. return 0;
  43. switch (s->cycle ++) {
  44. case 1:
  45. return s->rb2;
  46. case 2:
  47. return s->rb3;
  48. }
  49. return 0;
  50. }
  51. /* Interpret a control-byte */
  52. static void max111x_write(MAX111xState *s, uint32_t value)
  53. {
  54. int measure, chan;
  55. /* Ignore the value if START bit is zero */
  56. if (!(value & CB_START))
  57. return;
  58. s->cycle = 0;
  59. if (!(value & CB_PD1)) {
  60. s->tb1 = 0;
  61. return;
  62. }
  63. s->tb1 = value;
  64. if (s->inputs == 8)
  65. chan = CHANNEL_NUM(value, 1, 0, 2);
  66. else
  67. chan = CHANNEL_NUM(value & ~CB_SEL0, 0, 1, 2);
  68. if (value & CB_SGL)
  69. measure = s->input[chan] - s->com;
  70. else
  71. measure = s->input[chan] - s->input[chan ^ 1];
  72. if (!(value & CB_UNI))
  73. measure ^= 0x80;
  74. s->rb2 = (measure >> 2) & 0x3f;
  75. s->rb3 = (measure << 6) & 0xc0;
  76. /* FIXME: When should the IRQ be lowered? */
  77. qemu_irq_raise(s->interrupt);
  78. }
  79. static uint32_t max111x_transfer(SSISlave *dev, uint32_t value)
  80. {
  81. MAX111xState *s = MAX_111X(dev);
  82. max111x_write(s, value);
  83. return max111x_read(s);
  84. }
  85. static const VMStateDescription vmstate_max111x = {
  86. .name = "max111x",
  87. .version_id = 1,
  88. .minimum_version_id = 1,
  89. .minimum_version_id_old = 1,
  90. .fields = (VMStateField[]) {
  91. VMSTATE_SSI_SLAVE(parent_obj, MAX111xState),
  92. VMSTATE_UINT8(tb1, MAX111xState),
  93. VMSTATE_UINT8(rb2, MAX111xState),
  94. VMSTATE_UINT8(rb3, MAX111xState),
  95. VMSTATE_INT32_EQUAL(inputs, MAX111xState),
  96. VMSTATE_INT32(com, MAX111xState),
  97. VMSTATE_ARRAY_INT32_UNSAFE(input, MAX111xState, inputs,
  98. vmstate_info_uint8, uint8_t),
  99. VMSTATE_END_OF_LIST()
  100. }
  101. };
  102. static int max111x_init(SSISlave *d, int inputs)
  103. {
  104. DeviceState *dev = DEVICE(d);
  105. MAX111xState *s = MAX_111X(dev);
  106. qdev_init_gpio_out(dev, &s->interrupt, 1);
  107. s->inputs = inputs;
  108. /* TODO: add a user interface for setting these */
  109. s->input[0] = 0xf0;
  110. s->input[1] = 0xe0;
  111. s->input[2] = 0xd0;
  112. s->input[3] = 0xc0;
  113. s->input[4] = 0xb0;
  114. s->input[5] = 0xa0;
  115. s->input[6] = 0x90;
  116. s->input[7] = 0x80;
  117. s->com = 0;
  118. vmstate_register(dev, -1, &vmstate_max111x, s);
  119. return 0;
  120. }
  121. static int max1110_init(SSISlave *dev)
  122. {
  123. return max111x_init(dev, 8);
  124. }
  125. static int max1111_init(SSISlave *dev)
  126. {
  127. return max111x_init(dev, 4);
  128. }
  129. void max111x_set_input(DeviceState *dev, int line, uint8_t value)
  130. {
  131. MAX111xState *s = MAX_111X(dev);
  132. assert(line >= 0 && line < s->inputs);
  133. s->input[line] = value;
  134. }
  135. static void max111x_class_init(ObjectClass *klass, void *data)
  136. {
  137. SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
  138. k->transfer = max111x_transfer;
  139. }
  140. static const TypeInfo max111x_info = {
  141. .name = TYPE_MAX_111X,
  142. .parent = TYPE_SSI_SLAVE,
  143. .instance_size = sizeof(MAX111xState),
  144. .class_init = max111x_class_init,
  145. .abstract = true,
  146. };
  147. static void max1110_class_init(ObjectClass *klass, void *data)
  148. {
  149. SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
  150. k->init = max1110_init;
  151. }
  152. static const TypeInfo max1110_info = {
  153. .name = TYPE_MAX_1110,
  154. .parent = TYPE_MAX_111X,
  155. .class_init = max1110_class_init,
  156. };
  157. static void max1111_class_init(ObjectClass *klass, void *data)
  158. {
  159. SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
  160. k->init = max1111_init;
  161. }
  162. static const TypeInfo max1111_info = {
  163. .name = TYPE_MAX_1111,
  164. .parent = TYPE_MAX_111X,
  165. .class_init = max1111_class_init,
  166. };
  167. static void max111x_register_types(void)
  168. {
  169. type_register_static(&max111x_info);
  170. type_register_static(&max1110_info);
  171. type_register_static(&max1111_info);
  172. }
  173. type_init(max111x_register_types)