g364fb.c 18 KB

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