max111x.c 4.4 KB

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