2
0

jazz_led.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * QEMU JAZZ LED emulator.
  3. *
  4. * Copyright (c) 2007-2012 Herve Poussineau
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu-common.h"
  25. #include "ui/console.h"
  26. #include "ui/pixel_ops.h"
  27. #include "trace.h"
  28. #include "sysbus.h"
  29. typedef enum {
  30. REDRAW_NONE = 0, REDRAW_SEGMENTS = 1, REDRAW_BACKGROUND = 2,
  31. } screen_state_t;
  32. typedef struct LedState {
  33. SysBusDevice busdev;
  34. MemoryRegion iomem;
  35. uint8_t segments;
  36. DisplayState *ds;
  37. screen_state_t state;
  38. } LedState;
  39. static uint64_t jazz_led_read(void *opaque, hwaddr addr,
  40. unsigned int size)
  41. {
  42. LedState *s = opaque;
  43. uint8_t val;
  44. val = s->segments;
  45. trace_jazz_led_read(addr, val);
  46. return val;
  47. }
  48. static void jazz_led_write(void *opaque, hwaddr addr,
  49. uint64_t val, unsigned int size)
  50. {
  51. LedState *s = opaque;
  52. uint8_t new_val = val & 0xff;
  53. trace_jazz_led_write(addr, new_val);
  54. s->segments = new_val;
  55. s->state |= REDRAW_SEGMENTS;
  56. }
  57. static const MemoryRegionOps led_ops = {
  58. .read = jazz_led_read,
  59. .write = jazz_led_write,
  60. .endianness = DEVICE_NATIVE_ENDIAN,
  61. .impl.min_access_size = 1,
  62. .impl.max_access_size = 1,
  63. };
  64. /***********************************************************/
  65. /* jazz_led display */
  66. static void draw_horizontal_line(DisplayState *ds, int posy, int posx1, int posx2, uint32_t color)
  67. {
  68. uint8_t *d;
  69. int x, bpp;
  70. bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
  71. d = ds_get_data(ds) + ds_get_linesize(ds) * posy + bpp * posx1;
  72. switch(bpp) {
  73. case 1:
  74. for (x = posx1; x <= posx2; x++) {
  75. *((uint8_t *)d) = color;
  76. d++;
  77. }
  78. break;
  79. case 2:
  80. for (x = posx1; x <= posx2; x++) {
  81. *((uint16_t *)d) = color;
  82. d += 2;
  83. }
  84. break;
  85. case 4:
  86. for (x = posx1; x <= posx2; x++) {
  87. *((uint32_t *)d) = color;
  88. d += 4;
  89. }
  90. break;
  91. }
  92. }
  93. static void draw_vertical_line(DisplayState *ds, int posx, int posy1, int posy2, uint32_t color)
  94. {
  95. uint8_t *d;
  96. int y, bpp;
  97. bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
  98. d = ds_get_data(ds) + ds_get_linesize(ds) * posy1 + bpp * posx;
  99. switch(bpp) {
  100. case 1:
  101. for (y = posy1; y <= posy2; y++) {
  102. *((uint8_t *)d) = color;
  103. d += ds_get_linesize(ds);
  104. }
  105. break;
  106. case 2:
  107. for (y = posy1; y <= posy2; y++) {
  108. *((uint16_t *)d) = color;
  109. d += ds_get_linesize(ds);
  110. }
  111. break;
  112. case 4:
  113. for (y = posy1; y <= posy2; y++) {
  114. *((uint32_t *)d) = color;
  115. d += ds_get_linesize(ds);
  116. }
  117. break;
  118. }
  119. }
  120. static void jazz_led_update_display(void *opaque)
  121. {
  122. LedState *s = opaque;
  123. DisplayState *ds = s->ds;
  124. uint8_t *d1;
  125. uint32_t color_segment, color_led;
  126. int y, bpp;
  127. if (s->state & REDRAW_BACKGROUND) {
  128. /* clear screen */
  129. bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
  130. d1 = ds_get_data(ds);
  131. for (y = 0; y < ds_get_height(ds); y++) {
  132. memset(d1, 0x00, ds_get_width(ds) * bpp);
  133. d1 += ds_get_linesize(ds);
  134. }
  135. }
  136. if (s->state & REDRAW_SEGMENTS) {
  137. /* set colors according to bpp */
  138. switch (ds_get_bits_per_pixel(ds)) {
  139. case 8:
  140. color_segment = rgb_to_pixel8(0xaa, 0xaa, 0xaa);
  141. color_led = rgb_to_pixel8(0x00, 0xff, 0x00);
  142. break;
  143. case 15:
  144. color_segment = rgb_to_pixel15(0xaa, 0xaa, 0xaa);
  145. color_led = rgb_to_pixel15(0x00, 0xff, 0x00);
  146. break;
  147. case 16:
  148. color_segment = rgb_to_pixel16(0xaa, 0xaa, 0xaa);
  149. color_led = rgb_to_pixel16(0x00, 0xff, 0x00);
  150. case 24:
  151. color_segment = rgb_to_pixel24(0xaa, 0xaa, 0xaa);
  152. color_led = rgb_to_pixel24(0x00, 0xff, 0x00);
  153. break;
  154. case 32:
  155. color_segment = rgb_to_pixel32(0xaa, 0xaa, 0xaa);
  156. color_led = rgb_to_pixel32(0x00, 0xff, 0x00);
  157. break;
  158. default:
  159. return;
  160. }
  161. /* display segments */
  162. draw_horizontal_line(ds, 40, 10, 40, (s->segments & 0x02) ? color_segment : 0);
  163. draw_vertical_line(ds, 10, 10, 40, (s->segments & 0x04) ? color_segment : 0);
  164. draw_vertical_line(ds, 10, 40, 70, (s->segments & 0x08) ? color_segment : 0);
  165. draw_horizontal_line(ds, 70, 10, 40, (s->segments & 0x10) ? color_segment : 0);
  166. draw_vertical_line(ds, 40, 40, 70, (s->segments & 0x20) ? color_segment : 0);
  167. draw_vertical_line(ds, 40, 10, 40, (s->segments & 0x40) ? color_segment : 0);
  168. draw_horizontal_line(ds, 10, 10, 40, (s->segments & 0x80) ? color_segment : 0);
  169. /* display led */
  170. if (!(s->segments & 0x01))
  171. color_led = 0; /* black */
  172. draw_horizontal_line(ds, 68, 50, 50, color_led);
  173. draw_horizontal_line(ds, 69, 49, 51, color_led);
  174. draw_horizontal_line(ds, 70, 48, 52, color_led);
  175. draw_horizontal_line(ds, 71, 49, 51, color_led);
  176. draw_horizontal_line(ds, 72, 50, 50, color_led);
  177. }
  178. s->state = REDRAW_NONE;
  179. dpy_gfx_update(ds, 0, 0, ds_get_width(ds), ds_get_height(ds));
  180. }
  181. static void jazz_led_invalidate_display(void *opaque)
  182. {
  183. LedState *s = opaque;
  184. s->state |= REDRAW_SEGMENTS | REDRAW_BACKGROUND;
  185. }
  186. static void jazz_led_text_update(void *opaque, console_ch_t *chardata)
  187. {
  188. LedState *s = opaque;
  189. char buf[2];
  190. dpy_text_cursor(s->ds, -1, -1);
  191. qemu_console_resize(s->ds, 2, 1);
  192. /* TODO: draw the segments */
  193. snprintf(buf, 2, "%02hhx\n", s->segments);
  194. console_write_ch(chardata++, 0x00200100 | buf[0]);
  195. console_write_ch(chardata++, 0x00200100 | buf[1]);
  196. dpy_text_update(s->ds, 0, 0, 2, 1);
  197. }
  198. static int jazz_led_post_load(void *opaque, int version_id)
  199. {
  200. /* force refresh */
  201. jazz_led_invalidate_display(opaque);
  202. return 0;
  203. }
  204. static const VMStateDescription vmstate_jazz_led = {
  205. .name = "jazz-led",
  206. .version_id = 0,
  207. .minimum_version_id = 0,
  208. .minimum_version_id_old = 0,
  209. .post_load = jazz_led_post_load,
  210. .fields = (VMStateField[]) {
  211. VMSTATE_UINT8(segments, LedState),
  212. VMSTATE_END_OF_LIST()
  213. }
  214. };
  215. static int jazz_led_init(SysBusDevice *dev)
  216. {
  217. LedState *s = FROM_SYSBUS(LedState, dev);
  218. memory_region_init_io(&s->iomem, &led_ops, s, "led", 1);
  219. sysbus_init_mmio(dev, &s->iomem);
  220. s->ds = graphic_console_init(jazz_led_update_display,
  221. jazz_led_invalidate_display,
  222. NULL,
  223. jazz_led_text_update, s);
  224. return 0;
  225. }
  226. static void jazz_led_reset(DeviceState *d)
  227. {
  228. LedState *s = DO_UPCAST(LedState, busdev.qdev, d);
  229. s->segments = 0;
  230. s->state = REDRAW_SEGMENTS | REDRAW_BACKGROUND;
  231. qemu_console_resize(s->ds, 60, 80);
  232. }
  233. static void jazz_led_class_init(ObjectClass *klass, void *data)
  234. {
  235. DeviceClass *dc = DEVICE_CLASS(klass);
  236. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  237. k->init = jazz_led_init;
  238. dc->desc = "Jazz LED display",
  239. dc->vmsd = &vmstate_jazz_led;
  240. dc->reset = jazz_led_reset;
  241. }
  242. static const TypeInfo jazz_led_info = {
  243. .name = "jazz-led",
  244. .parent = TYPE_SYS_BUS_DEVICE,
  245. .instance_size = sizeof(LedState),
  246. .class_init = jazz_led_class_init,
  247. };
  248. static void jazz_led_register(void)
  249. {
  250. type_register_static(&jazz_led_info);
  251. }
  252. type_init(jazz_led_register);