jazz_led.c 9.2 KB

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