next-fb.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * NeXT Cube/Station Framebuffer Emulation
  3. *
  4. * Copyright (c) 2011 Bryce Lanham
  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 "qapi/error.h"
  26. #include "ui/console.h"
  27. #include "hw/loader.h"
  28. #include "framebuffer.h"
  29. #include "ui/pixel_ops.h"
  30. #include "hw/m68k/next-cube.h"
  31. #include "qom/object.h"
  32. OBJECT_DECLARE_SIMPLE_TYPE(NeXTFbState, NEXTFB)
  33. struct NeXTFbState {
  34. SysBusDevice parent_obj;
  35. MemoryRegion fb_mr;
  36. MemoryRegionSection fbsection;
  37. QemuConsole *con;
  38. uint32_t cols;
  39. uint32_t rows;
  40. int invalidate;
  41. };
  42. static void nextfb_draw_line(void *opaque, uint8_t *d, const uint8_t *s,
  43. int width, int pitch)
  44. {
  45. NeXTFbState *nfbstate = NEXTFB(opaque);
  46. static const uint32_t pal[4] = {
  47. 0xFFFFFFFF, 0xFFAAAAAA, 0xFF555555, 0xFF000000
  48. };
  49. uint32_t *buf = (uint32_t *)d;
  50. int i = 0;
  51. for (i = 0; i < nfbstate->cols / 4; i++) {
  52. int j = i * 4;
  53. uint8_t src = s[i];
  54. buf[j + 3] = pal[src & 0x3];
  55. src >>= 2;
  56. buf[j + 2] = pal[src & 0x3];
  57. src >>= 2;
  58. buf[j + 1] = pal[src & 0x3];
  59. src >>= 2;
  60. buf[j + 0] = pal[src & 0x3];
  61. }
  62. }
  63. static void nextfb_update(void *opaque)
  64. {
  65. NeXTFbState *s = NEXTFB(opaque);
  66. int dest_width = 4;
  67. int src_width;
  68. int first = 0;
  69. int last = 0;
  70. DisplaySurface *surface = qemu_console_surface(s->con);
  71. src_width = s->cols / 4 + 8;
  72. dest_width = s->cols * 4;
  73. if (s->invalidate) {
  74. framebuffer_update_memory_section(&s->fbsection, &s->fb_mr, 0,
  75. s->cols, src_width);
  76. s->invalidate = 0;
  77. }
  78. framebuffer_update_display(surface, &s->fbsection, s->cols, s->rows,
  79. src_width, dest_width, 0, 1, nextfb_draw_line,
  80. s, &first, &last);
  81. dpy_gfx_update(s->con, 0, 0, s->cols, s->rows);
  82. }
  83. static void nextfb_invalidate(void *opaque)
  84. {
  85. NeXTFbState *s = NEXTFB(opaque);
  86. s->invalidate = 1;
  87. }
  88. static const GraphicHwOps nextfb_ops = {
  89. .invalidate = nextfb_invalidate,
  90. .gfx_update = nextfb_update,
  91. };
  92. static void nextfb_realize(DeviceState *dev, Error **errp)
  93. {
  94. NeXTFbState *s = NEXTFB(dev);
  95. memory_region_init_ram(&s->fb_mr, OBJECT(dev), "next-video", 0x1CB100,
  96. &error_fatal);
  97. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->fb_mr);
  98. s->invalidate = 1;
  99. s->cols = 1120;
  100. s->rows = 832;
  101. s->con = graphic_console_init(dev, 0, &nextfb_ops, s);
  102. qemu_console_resize(s->con, s->cols, s->rows);
  103. }
  104. static void nextfb_class_init(ObjectClass *oc, void *data)
  105. {
  106. DeviceClass *dc = DEVICE_CLASS(oc);
  107. set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
  108. dc->realize = nextfb_realize;
  109. /* Note: This device does not have any state that we have to reset or migrate */
  110. }
  111. static const TypeInfo nextfb_info = {
  112. .name = TYPE_NEXTFB,
  113. .parent = TYPE_SYS_BUS_DEVICE,
  114. .instance_size = sizeof(NeXTFbState),
  115. .class_init = nextfb_class_init,
  116. };
  117. static void nextfb_register_types(void)
  118. {
  119. type_register_static(&nextfb_info);
  120. }
  121. type_init(nextfb_register_types)