jazz_led.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "console.h"
  25. #include "pixel_ops.h"
  26. #include "trace.h"
  27. #include "sysbus.h"
  28. typedef enum {
  29. REDRAW_NONE = 0, REDRAW_SEGMENTS = 1, REDRAW_BACKGROUND = 2,
  30. } screen_state_t;
  31. typedef struct LedState {
  32. SysBusDevice busdev;
  33. MemoryRegion iomem;
  34. uint8_t segments;
  35. DisplayState *ds;
  36. screen_state_t state;
  37. } LedState;
  38. static uint64_t jazz_led_read(void *opaque, target_phys_addr_t addr,
  39. unsigned int size)
  40. {
  41. LedState *s = opaque;
  42. uint8_t val;
  43. val = s->segments;
  44. trace_jazz_led_read(addr, val);
  45. return val;
  46. }
  47. static void jazz_led_write(void *opaque, target_phys_addr_t addr,
  48. uint64_t val, unsigned int size)
  49. {
  50. LedState *s = opaque;
  51. uint8_t new_val = val & 0xff;
  52. trace_jazz_led_write(addr, new_val);
  53. s->segments = new_val;
  54. s->state |= REDRAW_SEGMENTS;
  55. }
  56. static const MemoryRegionOps led_ops = {
  57. .read = jazz_led_read,
  58. .write = jazz_led_write,
  59. .endianness = DEVICE_NATIVE_ENDIAN,
  60. .impl.min_access_size = 1,
  61. .impl.max_access_size = 1,
  62. };
  63. /***********************************************************/
  64. /* jazz_led display */
  65. static void draw_horizontal_line(DisplayState *ds, int posy, int posx1, int posx2, uint32_t color)
  66. {
  67. uint8_t *d;
  68. int x, bpp;
  69. bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
  70. d = ds_get_data(ds) + ds_get_linesize(ds) * posy + bpp * posx1;
  71. switch(bpp) {
  72. case 1:
  73. for (x = posx1; x <= posx2; x++) {
  74. *((uint8_t *)d) = color;
  75. d++;
  76. }
  77. break;
  78. case 2:
  79. for (x = posx1; x <= posx2; x++) {
  80. *((uint16_t *)d) = color;
  81. d += 2;
  82. }
  83. break;
  84. case 4:
  85. for (x = posx1; x <= posx2; x++) {
  86. *((uint32_t *)d) = color;
  87. d += 4;
  88. }
  89. break;
  90. }
  91. }
  92. static void draw_vertical_line(DisplayState *ds, int posx, int posy1, int posy2, uint32_t color)
  93. {
  94. uint8_t *d;
  95. int y, bpp;
  96. bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
  97. d = ds_get_data(ds) + ds_get_linesize(ds) * posy1 + bpp * posx;
  98. switch(bpp) {
  99. case 1:
  100. for (y = posy1; y <= posy2; y++) {
  101. *((uint8_t *)d) = color;
  102. d += ds_get_linesize(ds);
  103. }
  104. break;
  105. case 2:
  106. for (y = posy1; y <= posy2; y++) {
  107. *((uint16_t *)d) = color;
  108. d += ds_get_linesize(ds);
  109. }
  110. break;
  111. case 4:
  112. for (y = posy1; y <= posy2; y++) {
  113. *((uint32_t *)d) = color;
  114. d += ds_get_linesize(ds);
  115. }
  116. break;
  117. }
  118. }
  119. static void jazz_led_update_display(void *opaque)
  120. {
  121. LedState *s = opaque;
  122. DisplayState *ds = s->ds;
  123. uint8_t *d1;
  124. uint32_t color_segment, color_led;
  125. int y, bpp;
  126. if (s->state & REDRAW_BACKGROUND) {
  127. /* clear screen */
  128. bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
  129. d1 = ds_get_data(ds);
  130. for (y = 0; y < ds_get_height(ds); y++) {
  131. memset(d1, 0x00, ds_get_width(ds) * bpp);
  132. d1 += ds_get_linesize(ds);
  133. }
  134. }
  135. if (s->state & REDRAW_SEGMENTS) {
  136. /* set colors according to bpp */
  137. switch (ds_get_bits_per_pixel(ds)) {
  138. case 8:
  139. color_segment = rgb_to_pixel8(0xaa, 0xaa, 0xaa);
  140. color_led = rgb_to_pixel8(0x00, 0xff, 0x00);
  141. break;
  142. case 15:
  143. color_segment = rgb_to_pixel15(0xaa, 0xaa, 0xaa);
  144. color_led = rgb_to_pixel15(0x00, 0xff, 0x00);
  145. break;
  146. case 16:
  147. color_segment = rgb_to_pixel16(0xaa, 0xaa, 0xaa);
  148. color_led = rgb_to_pixel16(0x00, 0xff, 0x00);
  149. case 24:
  150. color_segment = rgb_to_pixel24(0xaa, 0xaa, 0xaa);
  151. color_led = rgb_to_pixel24(0x00, 0xff, 0x00);
  152. break;
  153. case 32:
  154. color_segment = rgb_to_pixel32(0xaa, 0xaa, 0xaa);
  155. color_led = rgb_to_pixel32(0x00, 0xff, 0x00);
  156. break;
  157. default:
  158. return;
  159. }
  160. /* display segments */
  161. draw_horizontal_line(ds, 40, 10, 40, (s->segments & 0x02) ? color_segment : 0);
  162. draw_vertical_line(ds, 10, 10, 40, (s->segments & 0x04) ? color_segment : 0);
  163. draw_vertical_line(ds, 10, 40, 70, (s->segments & 0x08) ? color_segment : 0);
  164. draw_horizontal_line(ds, 70, 10, 40, (s->segments & 0x10) ? color_segment : 0);
  165. draw_vertical_line(ds, 40, 40, 70, (s->segments & 0x20) ? color_segment : 0);
  166. draw_vertical_line(ds, 40, 10, 40, (s->segments & 0x40) ? color_segment : 0);
  167. draw_horizontal_line(ds, 10, 10, 40, (s->segments & 0x80) ? color_segment : 0);
  168. /* display led */
  169. if (!(s->segments & 0x01))
  170. color_led = 0; /* black */
  171. draw_horizontal_line(ds, 68, 50, 50, color_led);
  172. draw_horizontal_line(ds, 69, 49, 51, color_led);
  173. draw_horizontal_line(ds, 70, 48, 52, color_led);
  174. draw_horizontal_line(ds, 71, 49, 51, color_led);
  175. draw_horizontal_line(ds, 72, 50, 50, color_led);
  176. }
  177. s->state = REDRAW_NONE;
  178. dpy_update(ds, 0, 0, ds_get_width(ds), ds_get_height(ds));
  179. }
  180. static void jazz_led_invalidate_display(void *opaque)
  181. {
  182. LedState *s = opaque;
  183. s->state |= REDRAW_SEGMENTS | REDRAW_BACKGROUND;
  184. }
  185. static void jazz_led_text_update(void *opaque, console_ch_t *chardata)
  186. {
  187. LedState *s = opaque;
  188. char buf[2];
  189. dpy_cursor(s->ds, -1, -1);
  190. qemu_console_resize(s->ds, 2, 1);
  191. /* TODO: draw the segments */
  192. snprintf(buf, 2, "%02hhx\n", s->segments);
  193. console_write_ch(chardata++, 0x00200100 | buf[0]);
  194. console_write_ch(chardata++, 0x00200100 | buf[1]);
  195. dpy_update(s->ds, 0, 0, 2, 1);
  196. }
  197. static int jazz_led_post_load(void *opaque, int version_id)
  198. {
  199. /* force refresh */
  200. jazz_led_invalidate_display(opaque);
  201. return 0;
  202. }
  203. static const VMStateDescription vmstate_jazz_led = {
  204. .name = "jazz-led",
  205. .version_id = 0,
  206. .minimum_version_id = 0,
  207. .minimum_version_id_old = 0,
  208. .post_load = jazz_led_post_load,
  209. .fields = (VMStateField[]) {
  210. VMSTATE_UINT8(segments, LedState),
  211. VMSTATE_END_OF_LIST()
  212. }
  213. };
  214. static int jazz_led_init(SysBusDevice *dev)
  215. {
  216. LedState *s = FROM_SYSBUS(LedState, dev);
  217. memory_region_init_io(&s->iomem, &led_ops, s, "led", 1);
  218. sysbus_init_mmio(dev, &s->iomem);
  219. s->ds = graphic_console_init(jazz_led_update_display,
  220. jazz_led_invalidate_display,
  221. NULL,
  222. jazz_led_text_update, s);
  223. return 0;
  224. }
  225. static void jazz_led_reset(DeviceState *d)
  226. {
  227. LedState *s = DO_UPCAST(LedState, busdev.qdev, d);
  228. s->segments = 0;
  229. s->state = REDRAW_SEGMENTS | REDRAW_BACKGROUND;
  230. qemu_console_resize(s->ds, 60, 80);
  231. }
  232. static void jazz_led_class_init(ObjectClass *klass, void *data)
  233. {
  234. DeviceClass *dc = DEVICE_CLASS(klass);
  235. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  236. k->init = jazz_led_init;
  237. dc->desc = "Jazz LED display",
  238. dc->vmsd = &vmstate_jazz_led;
  239. dc->reset = jazz_led_reset;
  240. }
  241. static TypeInfo jazz_led_info = {
  242. .name = "jazz-led",
  243. .parent = TYPE_SYS_BUS_DEVICE,
  244. .instance_size = sizeof(LedState),
  245. .class_init = jazz_led_class_init,
  246. };
  247. static void jazz_led_register(void)
  248. {
  249. type_register_static(&jazz_led_info);
  250. }
  251. type_init(jazz_led_register);