2
0

max111x.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #include "hw.h"
  10. #include "i2c.h"
  11. struct max111x_s {
  12. qemu_irq interrupt;
  13. uint8_t tb1, rb2, rb3;
  14. int cycle;
  15. int input[8];
  16. int inputs, com;
  17. };
  18. /* Control-byte bitfields */
  19. #define CB_PD0 (1 << 0)
  20. #define CB_PD1 (1 << 1)
  21. #define CB_SGL (1 << 2)
  22. #define CB_UNI (1 << 3)
  23. #define CB_SEL0 (1 << 4)
  24. #define CB_SEL1 (1 << 5)
  25. #define CB_SEL2 (1 << 6)
  26. #define CB_START (1 << 7)
  27. #define CHANNEL_NUM(v, b0, b1, b2) \
  28. ((((v) >> (2 + (b0))) & 4) | \
  29. (((v) >> (3 + (b1))) & 2) | \
  30. (((v) >> (4 + (b2))) & 1))
  31. uint32_t max111x_read(void *opaque)
  32. {
  33. struct max111x_s *s = (struct max111x_s *) opaque;
  34. if (!s->tb1)
  35. return 0;
  36. switch (s->cycle ++) {
  37. case 1:
  38. return s->rb2;
  39. case 2:
  40. return s->rb3;
  41. }
  42. return 0;
  43. }
  44. /* Interpret a control-byte */
  45. void max111x_write(void *opaque, uint32_t value)
  46. {
  47. struct max111x_s *s = (struct max111x_s *) opaque;
  48. int measure, chan;
  49. /* Ignore the value if START bit is zero */
  50. if (!(value & CB_START))
  51. return;
  52. s->cycle = 0;
  53. if (!(value & CB_PD1)) {
  54. s->tb1 = 0;
  55. return;
  56. }
  57. s->tb1 = value;
  58. if (s->inputs == 8)
  59. chan = CHANNEL_NUM(value, 1, 0, 2);
  60. else
  61. chan = CHANNEL_NUM(value & ~CB_SEL0, 0, 1, 2);
  62. if (value & CB_SGL)
  63. measure = s->input[chan] - s->com;
  64. else
  65. measure = s->input[chan] - s->input[chan ^ 1];
  66. if (!(value & CB_UNI))
  67. measure ^= 0x80;
  68. s->rb2 = (measure >> 2) & 0x3f;
  69. s->rb3 = (measure << 6) & 0xc0;
  70. if (s->interrupt)
  71. qemu_irq_raise(s->interrupt);
  72. }
  73. static void max111x_save(QEMUFile *f, void *opaque)
  74. {
  75. struct max111x_s *s = (struct max111x_s *) opaque;
  76. int i;
  77. qemu_put_8s(f, &s->tb1);
  78. qemu_put_8s(f, &s->rb2);
  79. qemu_put_8s(f, &s->rb3);
  80. qemu_put_be32(f, s->inputs);
  81. qemu_put_be32(f, s->com);
  82. for (i = 0; i < s->inputs; i ++)
  83. qemu_put_byte(f, s->input[i]);
  84. }
  85. static int max111x_load(QEMUFile *f, void *opaque, int version_id)
  86. {
  87. struct max111x_s *s = (struct max111x_s *) opaque;
  88. int i;
  89. qemu_get_8s(f, &s->tb1);
  90. qemu_get_8s(f, &s->rb2);
  91. qemu_get_8s(f, &s->rb3);
  92. if (s->inputs != qemu_get_be32(f))
  93. return -EINVAL;
  94. s->com = qemu_get_be32(f);
  95. for (i = 0; i < s->inputs; i ++)
  96. s->input[i] = qemu_get_byte(f);
  97. return 0;
  98. }
  99. static struct max111x_s *max111x_init(qemu_irq cb)
  100. {
  101. struct max111x_s *s;
  102. s = (struct max111x_s *)
  103. qemu_mallocz(sizeof(struct max111x_s));
  104. memset(s, 0, sizeof(struct max111x_s));
  105. s->interrupt = cb;
  106. /* TODO: add a user interface for setting these */
  107. s->input[0] = 0xf0;
  108. s->input[1] = 0xe0;
  109. s->input[2] = 0xd0;
  110. s->input[3] = 0xc0;
  111. s->input[4] = 0xb0;
  112. s->input[5] = 0xa0;
  113. s->input[6] = 0x90;
  114. s->input[7] = 0x80;
  115. s->com = 0;
  116. register_savevm("max111x", -1, 0, max111x_save, max111x_load, s);
  117. return s;
  118. }
  119. struct max111x_s *max1110_init(qemu_irq cb)
  120. {
  121. struct max111x_s *s = max111x_init(cb);
  122. s->inputs = 8;
  123. return s;
  124. }
  125. struct max111x_s *max1111_init(qemu_irq cb)
  126. {
  127. struct max111x_s *s = max111x_init(cb);
  128. s->inputs = 4;
  129. return s;
  130. }
  131. void max111x_set_input(struct max111x_s *s, int line, uint8_t value)
  132. {
  133. if (line >= s->inputs) {
  134. printf("%s: There's no input %i\n", __FUNCTION__, line);
  135. return;
  136. }
  137. s->input[line] = value;
  138. }