g364fb.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /*
  2. * QEMU G364 framebuffer Emulator.
  3. *
  4. * Copyright (c) 2007-2009 Herve Poussineau
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "hw.h"
  21. #include "mips.h"
  22. #include "console.h"
  23. #include "pixel_ops.h"
  24. //#define DEBUG_G364
  25. #ifdef DEBUG_G364
  26. #define DPRINTF(fmt, args...) \
  27. do { printf("g364: " fmt , ##args); } while (0)
  28. #else
  29. #define DPRINTF(fmt, args...) do {} while (0)
  30. #endif
  31. #define BADF(fmt, args...) \
  32. do { fprintf(stderr, "g364 ERROR: " fmt , ##args);} while (0)
  33. typedef struct G364State {
  34. /* hardware */
  35. uint8_t *vram;
  36. ram_addr_t vram_offset;
  37. int vram_size;
  38. qemu_irq irq;
  39. /* registers */
  40. uint8_t color_palette[256][3];
  41. uint8_t cursor_palette[3][3];
  42. uint16_t cursor[512];
  43. uint32_t cursor_position;
  44. uint32_t ctla;
  45. uint32_t top_of_screen;
  46. uint32_t width, height; /* in pixels */
  47. /* display refresh support */
  48. DisplayState *ds;
  49. int depth;
  50. int blanked;
  51. } G364State;
  52. #define REG_ID 0x000000
  53. #define REG_BOOT 0x080000
  54. #define REG_DISPLAY 0x080118
  55. #define REG_VDISPLAY 0x080150
  56. #define REG_CTLA 0x080300
  57. #define REG_TOP 0x080400
  58. #define REG_CURS_PAL 0x080508
  59. #define REG_CURS_POS 0x080638
  60. #define REG_CLR_PAL 0x080800
  61. #define REG_CURS_PAT 0x081000
  62. #define REG_RESET 0x180000
  63. #define CTLA_FORCE_BLANK 0x00000400
  64. #define CTLA_NO_CURSOR 0x00800000
  65. static inline int check_dirty(ram_addr_t page)
  66. {
  67. return cpu_physical_memory_get_dirty(page, VGA_DIRTY_FLAG);
  68. }
  69. static inline void reset_dirty(G364State *s,
  70. ram_addr_t page_min, ram_addr_t page_max)
  71. {
  72. cpu_physical_memory_reset_dirty(page_min, page_max + TARGET_PAGE_SIZE - 1,
  73. VGA_DIRTY_FLAG);
  74. }
  75. static void g364fb_draw_graphic8(G364State *s)
  76. {
  77. int i, w;
  78. uint8_t *vram;
  79. uint8_t *data_display, *dd;
  80. ram_addr_t page, page_min, page_max;
  81. int x, y;
  82. int xmin, xmax;
  83. int ymin, ymax;
  84. int xcursor, ycursor;
  85. unsigned int (*rgb_to_pixel)(unsigned int r, unsigned int g, unsigned int b);
  86. switch (ds_get_bits_per_pixel(s->ds)) {
  87. case 8:
  88. rgb_to_pixel = rgb_to_pixel8;
  89. w = 1;
  90. break;
  91. case 15:
  92. rgb_to_pixel = rgb_to_pixel15;
  93. w = 2;
  94. break;
  95. case 16:
  96. rgb_to_pixel = rgb_to_pixel16;
  97. w = 2;
  98. break;
  99. case 32:
  100. rgb_to_pixel = rgb_to_pixel32;
  101. w = 4;
  102. break;
  103. default:
  104. BADF("unknown host depth %d\n", ds_get_bits_per_pixel(s->ds));
  105. return;
  106. }
  107. page = s->vram_offset;
  108. page_min = (ram_addr_t)-1;
  109. page_max = 0;
  110. x = y = 0;
  111. xmin = s->width;
  112. xmax = 0;
  113. ymin = s->height;
  114. ymax = 0;
  115. if (!(s->ctla & CTLA_NO_CURSOR)) {
  116. xcursor = s->cursor_position >> 12;
  117. ycursor = s->cursor_position & 0xfff;
  118. } else {
  119. xcursor = ycursor = -65;
  120. }
  121. vram = s->vram + s->top_of_screen;
  122. /* XXX: out of range in vram? */
  123. data_display = dd = ds_get_data(s->ds);
  124. while (y < s->height) {
  125. if (check_dirty(page)) {
  126. if (y < ymin)
  127. ymin = ymax = y;
  128. if (page_min == (ram_addr_t)-1)
  129. page_min = page;
  130. page_max = page;
  131. if (x < xmin)
  132. xmin = x;
  133. for (i = 0; i < TARGET_PAGE_SIZE; i++) {
  134. uint8_t index;
  135. unsigned int color;
  136. if (unlikely((y >= ycursor && y < ycursor + 64) &&
  137. (x >= xcursor && x < xcursor + 64))) {
  138. /* pointer area */
  139. int xdiff = x - xcursor;
  140. uint16_t curs = s->cursor[(y - ycursor) * 8 + xdiff / 8];
  141. int op = (curs >> ((xdiff & 7) * 2)) & 3;
  142. if (likely(op == 0)) {
  143. /* transparent */
  144. index = *vram;
  145. color = (*rgb_to_pixel)(
  146. s->color_palette[index][0],
  147. s->color_palette[index][1],
  148. s->color_palette[index][2]);
  149. } else {
  150. /* get cursor color */
  151. index = op - 1;
  152. color = (*rgb_to_pixel)(
  153. s->cursor_palette[index][0],
  154. s->cursor_palette[index][1],
  155. s->cursor_palette[index][2]);
  156. }
  157. } else {
  158. /* normal area */
  159. index = *vram;
  160. color = (*rgb_to_pixel)(
  161. s->color_palette[index][0],
  162. s->color_palette[index][1],
  163. s->color_palette[index][2]);
  164. }
  165. memcpy(dd, &color, w);
  166. dd += w;
  167. x++;
  168. vram++;
  169. if (x == s->width) {
  170. xmax = s->width - 1;
  171. y++;
  172. if (y == s->height) {
  173. ymax = s->height - 1;
  174. goto done;
  175. }
  176. data_display = dd = data_display + ds_get_linesize(s->ds);
  177. xmin = 0;
  178. x = 0;
  179. }
  180. }
  181. if (x > xmax)
  182. xmax = x;
  183. if (y > ymax)
  184. ymax = y;
  185. } else {
  186. int dy;
  187. if (page_min != (ram_addr_t)-1) {
  188. reset_dirty(s, page_min, page_max);
  189. page_min = (ram_addr_t)-1;
  190. page_max = 0;
  191. dpy_update(s->ds, xmin, ymin, xmax - xmin + 1, ymax - ymin + 1);
  192. xmin = s->width;
  193. xmax = 0;
  194. ymin = s->height;
  195. ymax = 0;
  196. }
  197. x += TARGET_PAGE_SIZE;
  198. dy = x / s->width;
  199. x = x % s->width;
  200. y += dy;
  201. vram += TARGET_PAGE_SIZE;
  202. data_display += dy * ds_get_linesize(s->ds);
  203. dd = data_display + x * w;
  204. }
  205. page += TARGET_PAGE_SIZE;
  206. }
  207. done:
  208. if (page_min != (ram_addr_t)-1) {
  209. dpy_update(s->ds, xmin, ymin, xmax - xmin + 1, ymax - ymin + 1);
  210. reset_dirty(s, page_min, page_max);
  211. }
  212. }
  213. static void g364fb_draw_blank(G364State *s)
  214. {
  215. int i, w;
  216. uint8_t *d;
  217. if (s->blanked) {
  218. /* Screen is already blank. No need to redraw it */
  219. return;
  220. }
  221. w = s->width * ((ds_get_bits_per_pixel(s->ds) + 7) >> 3);
  222. d = ds_get_data(s->ds);
  223. for (i = 0; i < s->height; i++) {
  224. memset(d, 0, w);
  225. d += ds_get_linesize(s->ds);
  226. }
  227. dpy_update(s->ds, 0, 0, s->width, s->height);
  228. s->blanked = 1;
  229. }
  230. static void g364fb_update_display(void *opaque)
  231. {
  232. G364State *s = opaque;
  233. if (s->width == 0 || s->height == 0)
  234. return;
  235. if (s->width != ds_get_width(s->ds) || s->height != ds_get_height(s->ds)) {
  236. qemu_console_resize(s->ds, s->width, s->height);
  237. }
  238. if (s->ctla & CTLA_FORCE_BLANK) {
  239. g364fb_draw_blank(s);
  240. } else if (s->depth == 8) {
  241. g364fb_draw_graphic8(s);
  242. } else {
  243. BADF("unknown guest depth %d\n", s->depth);
  244. }
  245. qemu_irq_raise(s->irq);
  246. }
  247. static void inline g364fb_invalidate_display(void *opaque)
  248. {
  249. G364State *s = opaque;
  250. int i;
  251. s->blanked = 0;
  252. for (i = 0; i < s->vram_size; i += TARGET_PAGE_SIZE) {
  253. cpu_physical_memory_set_dirty(s->vram_offset + i);
  254. }
  255. }
  256. static void g364fb_reset(void *opaque)
  257. {
  258. G364State *s = opaque;
  259. qemu_irq_lower(s->irq);
  260. memset(s->color_palette, 0, sizeof(s->color_palette));
  261. memset(s->cursor_palette, 0, sizeof(s->cursor_palette));
  262. memset(s->cursor, 0, sizeof(s->cursor));
  263. s->cursor_position = 0;
  264. s->ctla = 0;
  265. s->top_of_screen = 0;
  266. s->width = s->height = 0;
  267. memset(s->vram, 0, s->vram_size);
  268. g364fb_invalidate_display(opaque);
  269. }
  270. static void g364fb_screen_dump(void *opaque, const char *filename)
  271. {
  272. G364State *s = opaque;
  273. int y, x;
  274. uint8_t index;
  275. uint8_t *data_buffer;
  276. FILE *f;
  277. if (s->depth != 8) {
  278. BADF("unknown guest depth %d\n", s->depth);
  279. return;
  280. }
  281. f = fopen(filename, "wb");
  282. if (!f)
  283. return;
  284. if (s->ctla & CTLA_FORCE_BLANK) {
  285. /* blank screen */
  286. fprintf(f, "P4\n%d %d\n",
  287. s->width, s->height);
  288. for (y = 0; y < s->height; y++)
  289. for (x = 0; x < s->width; x++)
  290. fputc(0, f);
  291. } else {
  292. data_buffer = s->vram + s->top_of_screen;
  293. fprintf(f, "P6\n%d %d\n%d\n",
  294. s->width, s->height, 255);
  295. for (y = 0; y < s->height; y++)
  296. for (x = 0; x < s->width; x++, data_buffer++) {
  297. index = *data_buffer;
  298. fputc(s->color_palette[index][0], f);
  299. fputc(s->color_palette[index][1], f);
  300. fputc(s->color_palette[index][2], f);
  301. }
  302. }
  303. fclose(f);
  304. }
  305. /* called for accesses to io ports */
  306. static uint32_t g364fb_ctrl_readl(void *opaque, target_phys_addr_t addr)
  307. {
  308. G364State *s = opaque;
  309. uint32_t val;
  310. if (addr >= REG_CURS_PAT && addr < REG_CURS_PAT + 0x1000) {
  311. /* cursor pattern */
  312. int idx = (addr - REG_CURS_PAT) >> 3;
  313. val = s->cursor[idx];
  314. } else if (addr >= REG_CURS_PAL && addr < REG_CURS_PAL + 0x18) {
  315. /* cursor palette */
  316. int idx = (addr - REG_CURS_PAL) >> 3;
  317. val = ((uint32_t)s->cursor_palette[idx][0] << 16);
  318. val |= ((uint32_t)s->cursor_palette[idx][1] << 8);
  319. val |= ((uint32_t)s->cursor_palette[idx][2] << 0);
  320. } else {
  321. switch (addr) {
  322. case REG_ID:
  323. val = 0x10; /* Mips G364 */
  324. break;
  325. case REG_DISPLAY:
  326. val = s->width / 4;
  327. break;
  328. case REG_VDISPLAY:
  329. val = s->height * 2;
  330. break;
  331. case REG_CTLA:
  332. val = s->ctla;
  333. break;
  334. default:
  335. {
  336. BADF("invalid read at [" TARGET_FMT_plx "]\n", addr);
  337. val = 0;
  338. break;
  339. }
  340. }
  341. }
  342. DPRINTF("read 0x%08x at [" TARGET_FMT_plx "]\n", val, addr);
  343. return val;
  344. }
  345. static uint32_t g364fb_ctrl_readw(void *opaque, target_phys_addr_t addr)
  346. {
  347. uint32_t v = g364fb_ctrl_readl(opaque, addr & ~0x3);
  348. if (addr & 0x2)
  349. return v >> 16;
  350. else
  351. return v & 0xffff;
  352. }
  353. static uint32_t g364fb_ctrl_readb(void *opaque, target_phys_addr_t addr)
  354. {
  355. uint32_t v = g364fb_ctrl_readl(opaque, addr & ~0x3);
  356. return (v >> (8 * (addr & 0x3))) & 0xff;
  357. }
  358. static void g364fb_update_depth(G364State *s)
  359. {
  360. const static int depths[8] = { 1, 2, 4, 8, 15, 16, 0 };
  361. s->depth = depths[(s->ctla & 0x00700000) >> 20];
  362. }
  363. static void g364_invalidate_cursor_position(G364State *s)
  364. {
  365. int ymin, ymax, start, end, i;
  366. /* invalidate only near the cursor */
  367. ymin = s->cursor_position & 0xfff;
  368. ymax = MIN(s->height, ymin + 64);
  369. start = ymin * ds_get_linesize(s->ds);
  370. end = (ymax + 1) * ds_get_linesize(s->ds);
  371. for (i = start; i < end; i += TARGET_PAGE_SIZE) {
  372. cpu_physical_memory_set_dirty(s->vram_offset + i);
  373. }
  374. }
  375. static void g364fb_ctrl_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
  376. {
  377. G364State *s = opaque;
  378. DPRINTF("write 0x%08x at [" TARGET_FMT_plx "]\n", val, addr);
  379. if (addr >= REG_CLR_PAL && addr < REG_CLR_PAL + 0x800) {
  380. /* color palette */
  381. int idx = (addr - REG_CLR_PAL) >> 3;
  382. s->color_palette[idx][0] = (val >> 16) & 0xff;
  383. s->color_palette[idx][1] = (val >> 8) & 0xff;
  384. s->color_palette[idx][2] = val & 0xff;
  385. g364fb_invalidate_display(s);
  386. } else if (addr >= REG_CURS_PAT && addr < REG_CURS_PAT + 0x1000) {
  387. /* cursor pattern */
  388. int idx = (addr - REG_CURS_PAT) >> 3;
  389. s->cursor[idx] = val;
  390. g364fb_invalidate_display(s);
  391. } else if (addr >= REG_CURS_PAL && addr < REG_CURS_PAL + 0x18) {
  392. /* cursor palette */
  393. int idx = (addr - REG_CURS_PAL) >> 3;
  394. s->cursor_palette[idx][0] = (val >> 16) & 0xff;
  395. s->cursor_palette[idx][1] = (val >> 8) & 0xff;
  396. s->cursor_palette[idx][2] = val & 0xff;
  397. g364fb_invalidate_display(s);
  398. } else {
  399. switch (addr) {
  400. case REG_ID: /* Card identifier; read-only */
  401. case REG_BOOT: /* Boot timing */
  402. case 0x80108: /* Line timing: half sync */
  403. case 0x80110: /* Line timing: back porch */
  404. case 0x80120: /* Line timing: short display */
  405. case 0x80128: /* Frame timing: broad pulse */
  406. case 0x80130: /* Frame timing: v sync */
  407. case 0x80138: /* Frame timing: v preequalise */
  408. case 0x80140: /* Frame timing: v postequalise */
  409. case 0x80148: /* Frame timing: v blank */
  410. case 0x80158: /* Line timing: line time */
  411. case 0x80160: /* Frame store: line start */
  412. case 0x80168: /* vram cycle: mem init */
  413. case 0x80170: /* vram cycle: transfer delay */
  414. case 0x80200: /* vram cycle: mask register */
  415. /* ignore */
  416. break;
  417. case REG_TOP:
  418. s->top_of_screen = val;
  419. g364fb_invalidate_display(s);
  420. break;
  421. case REG_DISPLAY:
  422. s->width = val * 4;
  423. break;
  424. case REG_VDISPLAY:
  425. s->height = val / 2;
  426. break;
  427. case REG_CTLA:
  428. s->ctla = val;
  429. g364fb_update_depth(s);
  430. g364fb_invalidate_display(s);
  431. break;
  432. case REG_CURS_POS:
  433. g364_invalidate_cursor_position(s);
  434. s->cursor_position = val;
  435. g364_invalidate_cursor_position(s);
  436. break;
  437. case REG_RESET:
  438. g364fb_reset(s);
  439. break;
  440. default:
  441. BADF("invalid write of 0x%08x at [" TARGET_FMT_plx "]\n", val, addr);
  442. break;
  443. }
  444. }
  445. qemu_irq_lower(s->irq);
  446. }
  447. static void g364fb_ctrl_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
  448. {
  449. uint32_t old_val = g364fb_ctrl_readl(opaque, addr & ~0x3);
  450. if (addr & 0x2)
  451. val = (val << 16) | (old_val & 0x0000ffff);
  452. else
  453. val = val | (old_val & 0xffff0000);
  454. g364fb_ctrl_writel(opaque, addr & ~0x3, val);
  455. }
  456. static void g364fb_ctrl_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
  457. {
  458. uint32_t old_val = g364fb_ctrl_readl(opaque, addr & ~0x3);
  459. switch (addr & 3) {
  460. case 0:
  461. val = val | (old_val & 0xffffff00);
  462. break;
  463. case 1:
  464. val = (val << 8) | (old_val & 0xffff00ff);
  465. break;
  466. case 2:
  467. val = (val << 16) | (old_val & 0xff00ffff);
  468. break;
  469. case 3:
  470. val = (val << 24) | (old_val & 0x00ffffff);
  471. break;
  472. }
  473. g364fb_ctrl_writel(opaque, addr & ~0x3, val);
  474. }
  475. static CPUReadMemoryFunc *g364fb_ctrl_read[3] = {
  476. g364fb_ctrl_readb,
  477. g364fb_ctrl_readw,
  478. g364fb_ctrl_readl,
  479. };
  480. static CPUWriteMemoryFunc *g364fb_ctrl_write[3] = {
  481. g364fb_ctrl_writeb,
  482. g364fb_ctrl_writew,
  483. g364fb_ctrl_writel,
  484. };
  485. static int g364fb_load(QEMUFile *f, void *opaque, int version_id)
  486. {
  487. G364State *s = opaque;
  488. unsigned int i, vram_size;
  489. if (version_id != 1)
  490. return -EINVAL;
  491. vram_size = qemu_get_be32(f);
  492. if (vram_size < s->vram_size)
  493. return -EINVAL;
  494. qemu_get_buffer(f, s->vram, s->vram_size);
  495. for (i = 0; i < 256; i++)
  496. qemu_get_buffer(f, s->color_palette[i], 3);
  497. for (i = 0; i < 3; i++)
  498. qemu_get_buffer(f, s->cursor_palette[i], 3);
  499. qemu_get_buffer(f, (uint8_t *)s->cursor, sizeof(s->cursor));
  500. s->cursor_position = qemu_get_be32(f);
  501. s->ctla = qemu_get_be32(f);
  502. s->top_of_screen = qemu_get_be32(f);
  503. s->width = qemu_get_be32(f);
  504. s->height = qemu_get_be32(f);
  505. /* force refresh */
  506. g364fb_update_depth(s);
  507. g364fb_invalidate_display(s);
  508. return 0;
  509. }
  510. static void g364fb_save(QEMUFile *f, void *opaque)
  511. {
  512. G364State *s = opaque;
  513. int i;
  514. qemu_put_be32(f, s->vram_size);
  515. qemu_put_buffer(f, s->vram, s->vram_size);
  516. for (i = 0; i < 256; i++)
  517. qemu_put_buffer(f, s->color_palette[i], 3);
  518. for (i = 0; i < 3; i++)
  519. qemu_put_buffer(f, s->cursor_palette[i], 3);
  520. qemu_put_buffer(f, (uint8_t *)s->cursor, sizeof(s->cursor));
  521. qemu_put_be32(f, s->cursor_position);
  522. qemu_put_be32(f, s->ctla);
  523. qemu_put_be32(f, s->top_of_screen);
  524. qemu_put_be32(f, s->width);
  525. qemu_put_be32(f, s->height);
  526. }
  527. int g364fb_mm_init(uint8_t *vram, ram_addr_t vram_offset,
  528. int vram_size, target_phys_addr_t vram_base,
  529. target_phys_addr_t ctrl_base, int it_shift,
  530. qemu_irq irq)
  531. {
  532. G364State *s;
  533. int io_ctrl;
  534. s = qemu_mallocz(sizeof(G364State));
  535. s->vram = vram;
  536. s->vram_offset = vram_offset;
  537. s->vram_size = vram_size;
  538. s->irq = irq;
  539. qemu_register_reset(g364fb_reset, s);
  540. register_savevm("g364fb", 0, 1, g364fb_save, g364fb_load, s);
  541. g364fb_reset(s);
  542. s->ds = graphic_console_init(g364fb_update_display,
  543. g364fb_invalidate_display,
  544. g364fb_screen_dump, NULL, s);
  545. cpu_register_physical_memory(vram_base, s->vram_size, s->vram_offset);
  546. io_ctrl = cpu_register_io_memory(0, g364fb_ctrl_read, g364fb_ctrl_write, s);
  547. cpu_register_physical_memory(ctrl_base, 0x200000, io_ctrl);
  548. return 0;
  549. }