jazz_led.c 9.2 KB

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