jazz_led.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * QEMU JAZZ LED emulator.
  3. *
  4. * Copyright (c) 2007 Hervé 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 "hw.h"
  25. #include "mips.h"
  26. #include "console.h"
  27. #include "pixel_ops.h"
  28. //#define DEBUG_LED
  29. #ifdef DEBUG_LED
  30. #define DPRINTF(fmt, ...) \
  31. do { printf("jazz led: " fmt , ## __VA_ARGS__); } while (0)
  32. #else
  33. #define DPRINTF(fmt, ...) do {} while (0)
  34. #endif
  35. #define BADF(fmt, ...) \
  36. do { fprintf(stderr, "jazz led ERROR: " fmt , ## __VA_ARGS__);} while (0)
  37. typedef enum {
  38. REDRAW_NONE = 0, REDRAW_SEGMENTS = 1, REDRAW_BACKGROUND = 2,
  39. } screen_state_t;
  40. typedef struct LedState {
  41. uint8_t segments;
  42. DisplayState *ds;
  43. screen_state_t state;
  44. } LedState;
  45. static uint32_t led_readb(void *opaque, target_phys_addr_t addr)
  46. {
  47. LedState *s = opaque;
  48. uint32_t val;
  49. switch (addr) {
  50. case 0:
  51. val = s->segments;
  52. break;
  53. default:
  54. BADF("invalid read at [" TARGET_FMT_plx "]\n", addr);
  55. val = 0;
  56. }
  57. DPRINTF("read addr=" TARGET_FMT_plx " val=0x%02x\n", addr, val);
  58. return val;
  59. }
  60. static uint32_t led_readw(void *opaque, target_phys_addr_t addr)
  61. {
  62. uint32_t v;
  63. #ifdef TARGET_WORDS_BIGENDIAN
  64. v = led_readb(opaque, addr) << 8;
  65. v |= led_readb(opaque, addr + 1);
  66. #else
  67. v = led_readb(opaque, addr);
  68. v |= led_readb(opaque, addr + 1) << 8;
  69. #endif
  70. return v;
  71. }
  72. static uint32_t led_readl(void *opaque, target_phys_addr_t addr)
  73. {
  74. uint32_t v;
  75. #ifdef TARGET_WORDS_BIGENDIAN
  76. v = led_readb(opaque, addr) << 24;
  77. v |= led_readb(opaque, addr + 1) << 16;
  78. v |= led_readb(opaque, addr + 2) << 8;
  79. v |= led_readb(opaque, addr + 3);
  80. #else
  81. v = led_readb(opaque, addr);
  82. v |= led_readb(opaque, addr + 1) << 8;
  83. v |= led_readb(opaque, addr + 2) << 16;
  84. v |= led_readb(opaque, addr + 3) << 24;
  85. #endif
  86. return v;
  87. }
  88. static void led_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
  89. {
  90. LedState *s = opaque;
  91. DPRINTF("write addr=" TARGET_FMT_plx " val=0x%02x\n", addr, val);
  92. switch (addr) {
  93. case 0:
  94. s->segments = val;
  95. s->state |= REDRAW_SEGMENTS;
  96. break;
  97. default:
  98. BADF("invalid write of 0x%08x at [" TARGET_FMT_plx "]\n", val, addr);
  99. break;
  100. }
  101. }
  102. static void led_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
  103. {
  104. #ifdef TARGET_WORDS_BIGENDIAN
  105. led_writeb(opaque, addr, (val >> 8) & 0xff);
  106. led_writeb(opaque, addr + 1, val & 0xff);
  107. #else
  108. led_writeb(opaque, addr, val & 0xff);
  109. led_writeb(opaque, addr + 1, (val >> 8) & 0xff);
  110. #endif
  111. }
  112. static void led_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
  113. {
  114. #ifdef TARGET_WORDS_BIGENDIAN
  115. led_writeb(opaque, addr, (val >> 24) & 0xff);
  116. led_writeb(opaque, addr + 1, (val >> 16) & 0xff);
  117. led_writeb(opaque, addr + 2, (val >> 8) & 0xff);
  118. led_writeb(opaque, addr + 3, val & 0xff);
  119. #else
  120. led_writeb(opaque, addr, val & 0xff);
  121. led_writeb(opaque, addr + 1, (val >> 8) & 0xff);
  122. led_writeb(opaque, addr + 2, (val >> 16) & 0xff);
  123. led_writeb(opaque, addr + 3, (val >> 24) & 0xff);
  124. #endif
  125. }
  126. static CPUReadMemoryFunc * const led_read[3] = {
  127. led_readb,
  128. led_readw,
  129. led_readl,
  130. };
  131. static CPUWriteMemoryFunc * const led_write[3] = {
  132. led_writeb,
  133. led_writew,
  134. led_writel,
  135. };
  136. /***********************************************************/
  137. /* jazz_led display */
  138. static void draw_horizontal_line(DisplayState *ds, int posy, int posx1, int posx2, uint32_t color)
  139. {
  140. uint8_t *d;
  141. int x, bpp;
  142. bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
  143. d = ds_get_data(ds) + ds_get_linesize(ds) * posy + bpp * posx1;
  144. switch(bpp) {
  145. case 1:
  146. for (x = posx1; x <= posx2; x++) {
  147. *((uint8_t *)d) = color;
  148. d++;
  149. }
  150. break;
  151. case 2:
  152. for (x = posx1; x <= posx2; x++) {
  153. *((uint16_t *)d) = color;
  154. d += 2;
  155. }
  156. break;
  157. case 4:
  158. for (x = posx1; x <= posx2; x++) {
  159. *((uint32_t *)d) = color;
  160. d += 4;
  161. }
  162. break;
  163. }
  164. }
  165. static void draw_vertical_line(DisplayState *ds, int posx, int posy1, int posy2, uint32_t color)
  166. {
  167. uint8_t *d;
  168. int y, bpp;
  169. bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
  170. d = ds_get_data(ds) + ds_get_linesize(ds) * posy1 + bpp * posx;
  171. switch(bpp) {
  172. case 1:
  173. for (y = posy1; y <= posy2; y++) {
  174. *((uint8_t *)d) = color;
  175. d += ds_get_linesize(ds);
  176. }
  177. break;
  178. case 2:
  179. for (y = posy1; y <= posy2; y++) {
  180. *((uint16_t *)d) = color;
  181. d += ds_get_linesize(ds);
  182. }
  183. break;
  184. case 4:
  185. for (y = posy1; y <= posy2; y++) {
  186. *((uint32_t *)d) = color;
  187. d += ds_get_linesize(ds);
  188. }
  189. break;
  190. }
  191. }
  192. static void jazz_led_update_display(void *opaque)
  193. {
  194. LedState *s = opaque;
  195. DisplayState *ds = s->ds;
  196. uint8_t *d1;
  197. uint32_t color_segment, color_led;
  198. int y, bpp;
  199. if (s->state & REDRAW_BACKGROUND) {
  200. /* clear screen */
  201. bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
  202. d1 = ds_get_data(ds);
  203. for (y = 0; y < ds_get_height(ds); y++) {
  204. memset(d1, 0x00, ds_get_width(ds) * bpp);
  205. d1 += ds_get_linesize(ds);
  206. }
  207. }
  208. if (s->state & REDRAW_SEGMENTS) {
  209. /* set colors according to bpp */
  210. switch (ds_get_bits_per_pixel(ds)) {
  211. case 8:
  212. color_segment = rgb_to_pixel8(0xaa, 0xaa, 0xaa);
  213. color_led = rgb_to_pixel8(0x00, 0xff, 0x00);
  214. break;
  215. case 15:
  216. color_segment = rgb_to_pixel15(0xaa, 0xaa, 0xaa);
  217. color_led = rgb_to_pixel15(0x00, 0xff, 0x00);
  218. break;
  219. case 16:
  220. color_segment = rgb_to_pixel16(0xaa, 0xaa, 0xaa);
  221. color_led = rgb_to_pixel16(0x00, 0xff, 0x00);
  222. case 24:
  223. color_segment = rgb_to_pixel24(0xaa, 0xaa, 0xaa);
  224. color_led = rgb_to_pixel24(0x00, 0xff, 0x00);
  225. break;
  226. case 32:
  227. color_segment = rgb_to_pixel32(0xaa, 0xaa, 0xaa);
  228. color_led = rgb_to_pixel32(0x00, 0xff, 0x00);
  229. break;
  230. default:
  231. return;
  232. }
  233. /* display segments */
  234. draw_horizontal_line(ds, 40, 10, 40, (s->segments & 0x02) ? color_segment : 0);
  235. draw_vertical_line(ds, 10, 10, 40, (s->segments & 0x04) ? color_segment : 0);
  236. draw_vertical_line(ds, 10, 40, 70, (s->segments & 0x08) ? color_segment : 0);
  237. draw_horizontal_line(ds, 70, 10, 40, (s->segments & 0x10) ? color_segment : 0);
  238. draw_vertical_line(ds, 40, 40, 70, (s->segments & 0x20) ? color_segment : 0);
  239. draw_vertical_line(ds, 40, 10, 40, (s->segments & 0x40) ? color_segment : 0);
  240. draw_horizontal_line(ds, 10, 10, 40, (s->segments & 0x80) ? color_segment : 0);
  241. /* display led */
  242. if (!(s->segments & 0x01))
  243. color_led = 0; /* black */
  244. draw_horizontal_line(ds, 68, 50, 50, color_led);
  245. draw_horizontal_line(ds, 69, 49, 51, color_led);
  246. draw_horizontal_line(ds, 70, 48, 52, color_led);
  247. draw_horizontal_line(ds, 71, 49, 51, color_led);
  248. draw_horizontal_line(ds, 72, 50, 50, color_led);
  249. }
  250. s->state = REDRAW_NONE;
  251. dpy_update(ds, 0, 0, ds_get_width(ds), ds_get_height(ds));
  252. }
  253. static void jazz_led_invalidate_display(void *opaque)
  254. {
  255. LedState *s = opaque;
  256. s->state |= REDRAW_SEGMENTS | REDRAW_BACKGROUND;
  257. }
  258. static void jazz_led_screen_dump(void *opaque, const char *filename)
  259. {
  260. printf("jazz_led_screen_dump() not implemented\n");
  261. }
  262. static void jazz_led_text_update(void *opaque, console_ch_t *chardata)
  263. {
  264. LedState *s = opaque;
  265. char buf[2];
  266. dpy_cursor(s->ds, -1, -1);
  267. qemu_console_resize(s->ds, 2, 1);
  268. /* TODO: draw the segments */
  269. snprintf(buf, 2, "%02hhx\n", s->segments);
  270. console_write_ch(chardata++, 0x00200100 | buf[0]);
  271. console_write_ch(chardata++, 0x00200100 | buf[1]);
  272. dpy_update(s->ds, 0, 0, 2, 1);
  273. }
  274. void jazz_led_init(target_phys_addr_t base)
  275. {
  276. LedState *s;
  277. int io;
  278. s = qemu_mallocz(sizeof(LedState));
  279. s->state = REDRAW_SEGMENTS | REDRAW_BACKGROUND;
  280. io = cpu_register_io_memory(led_read, led_write, s,
  281. DEVICE_NATIVE_ENDIAN);
  282. cpu_register_physical_memory(base, 1, io);
  283. s->ds = graphic_console_init(jazz_led_update_display,
  284. jazz_led_invalidate_display,
  285. jazz_led_screen_dump,
  286. jazz_led_text_update, s);
  287. qemu_console_resize(s->ds, 60, 80);
  288. }