2
0

jazz_led.c 9.2 KB

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