2 * QEMU TCX Frame buffer
4 * Copyright (c) 2003-2005 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "pixel_ops.h"
29 #define TCX_DAC_NREGS 16
30 #define TCX_THC_NREGS_8 0x081c
31 #define TCX_THC_NREGS_24 0x1000
32 #define TCX_TEC_NREGS 0x1000
34 typedef struct TCXState
{
35 target_phys_addr_t addr
;
38 uint32_t *vram24
, *cplane
;
39 ram_addr_t vram_offset
, vram24_offset
, cplane_offset
;
40 uint16_t width
, height
, depth
;
41 uint8_t r
[256], g
[256], b
[256];
42 uint32_t palette
[256];
43 uint8_t dac_index
, dac_state
;
46 static void tcx_screen_dump(void *opaque
, const char *filename
);
47 static void tcx24_screen_dump(void *opaque
, const char *filename
);
48 static void tcx_invalidate_display(void *opaque
);
49 static void tcx24_invalidate_display(void *opaque
);
51 static void update_palette_entries(TCXState
*s
, int start
, int end
)
54 for(i
= start
; i
< end
; i
++) {
55 switch(s
->ds
->depth
) {
58 s
->palette
[i
] = rgb_to_pixel8(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
62 s
->palette
[i
] = rgb_to_pixel15bgr(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
64 s
->palette
[i
] = rgb_to_pixel15(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
68 s
->palette
[i
] = rgb_to_pixel16bgr(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
70 s
->palette
[i
] = rgb_to_pixel16(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
74 s
->palette
[i
] = rgb_to_pixel32bgr(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
76 s
->palette
[i
] = rgb_to_pixel32(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
81 tcx24_invalidate_display(s
);
83 tcx_invalidate_display(s
);
86 static void tcx_draw_line32(TCXState
*s1
, uint8_t *d
,
87 const uint8_t *s
, int width
)
91 uint32_t *p
= (uint32_t *)d
;
93 for(x
= 0; x
< width
; x
++) {
95 *p
++ = s1
->palette
[val
];
99 static void tcx_draw_line16(TCXState
*s1
, uint8_t *d
,
100 const uint8_t *s
, int width
)
104 uint16_t *p
= (uint16_t *)d
;
106 for(x
= 0; x
< width
; x
++) {
108 *p
++ = s1
->palette
[val
];
112 static void tcx_draw_line8(TCXState
*s1
, uint8_t *d
,
113 const uint8_t *s
, int width
)
118 for(x
= 0; x
< width
; x
++) {
120 *d
++ = s1
->palette
[val
];
124 static inline void tcx24_draw_line32(TCXState
*s1
, uint8_t *d
,
125 const uint8_t *s
, int width
,
126 const uint32_t *cplane
,
131 uint32_t *p
= (uint32_t *)d
;
134 for(x
= 0; x
< width
; x
++, s
++, s24
++) {
135 if ((bswap32(*cplane
++) & 0xff000000) == 0x03000000) { // 24-bit direct
136 dval
= bswap32(*s24
) & 0x00ffffff;
139 dval
= s1
->palette
[val
];
145 static inline int check_dirty(TCXState
*ts
, ram_addr_t page
, ram_addr_t page24
,
151 ret
= cpu_physical_memory_get_dirty(page
, VGA_DIRTY_FLAG
);
152 for (off
= 0; off
< TARGET_PAGE_SIZE
* 4; off
+= TARGET_PAGE_SIZE
) {
153 ret
|= cpu_physical_memory_get_dirty(page24
+ off
, VGA_DIRTY_FLAG
);
154 ret
|= cpu_physical_memory_get_dirty(cpage
+ off
, VGA_DIRTY_FLAG
);
159 static inline void reset_dirty(TCXState
*ts
, ram_addr_t page_min
,
160 ram_addr_t page_max
, ram_addr_t page24
,
163 cpu_physical_memory_reset_dirty(page_min
, page_max
+ TARGET_PAGE_SIZE
,
165 page_min
-= ts
->vram_offset
;
166 page_max
-= ts
->vram_offset
;
167 cpu_physical_memory_reset_dirty(page24
+ page_min
* 4,
168 page24
+ page_max
* 4 + TARGET_PAGE_SIZE
,
170 cpu_physical_memory_reset_dirty(cpage
+ page_min
* 4,
171 cpage
+ page_max
* 4 + TARGET_PAGE_SIZE
,
175 /* Fixed line length 1024 allows us to do nice tricks not possible on
177 static void tcx_update_display(void *opaque
)
179 TCXState
*ts
= opaque
;
180 ram_addr_t page
, page_min
, page_max
;
181 int y
, y_start
, dd
, ds
;
183 void (*f
)(TCXState
*s1
, uint8_t *dst
, const uint8_t *src
, int width
);
185 if (ts
->ds
->depth
== 0)
187 page
= ts
->vram_offset
;
189 page_min
= 0xffffffff;
193 dd
= ts
->ds
->linesize
;
196 switch (ts
->ds
->depth
) {
212 for(y
= 0; y
< ts
->height
; y
+= 4, page
+= TARGET_PAGE_SIZE
) {
213 if (cpu_physical_memory_get_dirty(page
, VGA_DIRTY_FLAG
)) {
220 f(ts
, d
, s
, ts
->width
);
223 f(ts
, d
, s
, ts
->width
);
226 f(ts
, d
, s
, ts
->width
);
229 f(ts
, d
, s
, ts
->width
);
234 /* flush to display */
235 dpy_update(ts
->ds
, 0, y_start
,
236 ts
->width
, y
- y_start
);
244 /* flush to display */
245 dpy_update(ts
->ds
, 0, y_start
,
246 ts
->width
, y
- y_start
);
248 /* reset modified pages */
249 if (page_min
<= page_max
) {
250 cpu_physical_memory_reset_dirty(page_min
, page_max
+ TARGET_PAGE_SIZE
,
255 static void tcx24_update_display(void *opaque
)
257 TCXState
*ts
= opaque
;
258 ram_addr_t page
, page_min
, page_max
, cpage
, page24
;
259 int y
, y_start
, dd
, ds
;
261 uint32_t *cptr
, *s24
;
263 if (ts
->ds
->depth
!= 32)
265 page
= ts
->vram_offset
;
266 page24
= ts
->vram24_offset
;
267 cpage
= ts
->cplane_offset
;
269 page_min
= 0xffffffff;
275 dd
= ts
->ds
->linesize
;
278 for(y
= 0; y
< ts
->height
; y
+= 4, page
+= TARGET_PAGE_SIZE
,
279 page24
+= TARGET_PAGE_SIZE
, cpage
+= TARGET_PAGE_SIZE
) {
280 if (check_dirty(ts
, page
, page24
, cpage
)) {
287 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
292 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
297 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
302 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
309 /* flush to display */
310 dpy_update(ts
->ds
, 0, y_start
,
311 ts
->width
, y
- y_start
);
321 /* flush to display */
322 dpy_update(ts
->ds
, 0, y_start
,
323 ts
->width
, y
- y_start
);
325 /* reset modified pages */
326 if (page_min
<= page_max
) {
327 reset_dirty(ts
, page_min
, page_max
, page24
, cpage
);
331 static void tcx_invalidate_display(void *opaque
)
333 TCXState
*s
= opaque
;
336 for (i
= 0; i
< MAXX
*MAXY
; i
+= TARGET_PAGE_SIZE
) {
337 cpu_physical_memory_set_dirty(s
->vram_offset
+ i
);
341 static void tcx24_invalidate_display(void *opaque
)
343 TCXState
*s
= opaque
;
346 tcx_invalidate_display(s
);
347 for (i
= 0; i
< MAXX
*MAXY
* 4; i
+= TARGET_PAGE_SIZE
) {
348 cpu_physical_memory_set_dirty(s
->vram24_offset
+ i
);
349 cpu_physical_memory_set_dirty(s
->cplane_offset
+ i
);
353 static void tcx_save(QEMUFile
*f
, void *opaque
)
355 TCXState
*s
= opaque
;
357 qemu_put_be16s(f
, (uint16_t *)&s
->height
);
358 qemu_put_be16s(f
, (uint16_t *)&s
->width
);
359 qemu_put_be16s(f
, (uint16_t *)&s
->depth
);
360 qemu_put_buffer(f
, s
->r
, 256);
361 qemu_put_buffer(f
, s
->g
, 256);
362 qemu_put_buffer(f
, s
->b
, 256);
363 qemu_put_8s(f
, &s
->dac_index
);
364 qemu_put_8s(f
, &s
->dac_state
);
367 static int tcx_load(QEMUFile
*f
, void *opaque
, int version_id
)
369 TCXState
*s
= opaque
;
372 if (version_id
!= 3 && version_id
!= 4)
375 if (version_id
== 3) {
376 qemu_get_be32s(f
, (uint32_t *)&dummy
);
377 qemu_get_be32s(f
, (uint32_t *)&dummy
);
378 qemu_get_be32s(f
, (uint32_t *)&dummy
);
380 qemu_get_be16s(f
, (uint16_t *)&s
->height
);
381 qemu_get_be16s(f
, (uint16_t *)&s
->width
);
382 qemu_get_be16s(f
, (uint16_t *)&s
->depth
);
383 qemu_get_buffer(f
, s
->r
, 256);
384 qemu_get_buffer(f
, s
->g
, 256);
385 qemu_get_buffer(f
, s
->b
, 256);
386 qemu_get_8s(f
, &s
->dac_index
);
387 qemu_get_8s(f
, &s
->dac_state
);
388 update_palette_entries(s
, 0, 256);
390 tcx24_invalidate_display(s
);
392 tcx_invalidate_display(s
);
397 static void tcx_reset(void *opaque
)
399 TCXState
*s
= opaque
;
401 /* Initialize palette */
402 memset(s
->r
, 0, 256);
403 memset(s
->g
, 0, 256);
404 memset(s
->b
, 0, 256);
405 s
->r
[255] = s
->g
[255] = s
->b
[255] = 255;
406 update_palette_entries(s
, 0, 256);
407 memset(s
->vram
, 0, MAXX
*MAXY
);
408 cpu_physical_memory_reset_dirty(s
->vram_offset
, s
->vram_offset
+
409 MAXX
* MAXY
* (1 + 4 + 4), VGA_DIRTY_FLAG
);
414 static uint32_t tcx_dac_readl(void *opaque
, target_phys_addr_t addr
)
419 static void tcx_dac_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
421 TCXState
*s
= opaque
;
424 saddr
= (addr
& (TCX_DAC_NREGS
- 1)) >> 2;
427 s
->dac_index
= val
>> 24;
431 switch (s
->dac_state
) {
433 s
->r
[s
->dac_index
] = val
>> 24;
434 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
438 s
->g
[s
->dac_index
] = val
>> 24;
439 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
443 s
->b
[s
->dac_index
] = val
>> 24;
444 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
445 s
->dac_index
= (s
->dac_index
+ 1) & 255; // Index autoincrement
457 static CPUReadMemoryFunc
*tcx_dac_read
[3] = {
463 static CPUWriteMemoryFunc
*tcx_dac_write
[3] = {
469 static uint32_t tcx_dummy_readl(void *opaque
, target_phys_addr_t addr
)
474 static void tcx_dummy_writel(void *opaque
, target_phys_addr_t addr
,
479 static CPUReadMemoryFunc
*tcx_dummy_read
[3] = {
485 static CPUWriteMemoryFunc
*tcx_dummy_write
[3] = {
491 void tcx_init(DisplayState
*ds
, target_phys_addr_t addr
, uint8_t *vram_base
,
492 unsigned long vram_offset
, int vram_size
, int width
, int height
,
496 int io_memory
, dummy_memory
;
499 s
= qemu_mallocz(sizeof(TCXState
));
504 s
->vram_offset
= vram_offset
;
512 cpu_register_physical_memory(addr
+ 0x00800000ULL
, size
, vram_offset
);
516 io_memory
= cpu_register_io_memory(0, tcx_dac_read
, tcx_dac_write
, s
);
517 cpu_register_physical_memory(addr
+ 0x00200000ULL
, TCX_DAC_NREGS
, io_memory
);
519 dummy_memory
= cpu_register_io_memory(0, tcx_dummy_read
, tcx_dummy_write
,
521 cpu_register_physical_memory(addr
+ 0x00700000ULL
, TCX_TEC_NREGS
,
525 size
= vram_size
* 4;
526 s
->vram24
= (uint32_t *)vram_base
;
527 s
->vram24_offset
= vram_offset
;
528 cpu_register_physical_memory(addr
+ 0x02000000ULL
, size
, vram_offset
);
533 size
= vram_size
* 4;
534 s
->cplane
= (uint32_t *)vram_base
;
535 s
->cplane_offset
= vram_offset
;
536 cpu_register_physical_memory(addr
+ 0x0a000000ULL
, size
, vram_offset
);
537 graphic_console_init(s
->ds
, tcx24_update_display
,
538 tcx24_invalidate_display
, tcx24_screen_dump
, s
);
540 cpu_register_physical_memory(addr
+ 0x00300000ULL
, TCX_THC_NREGS_8
,
542 graphic_console_init(s
->ds
, tcx_update_display
, tcx_invalidate_display
,
545 // NetBSD writes here even with 8-bit display
546 cpu_register_physical_memory(addr
+ 0x00301000ULL
, TCX_THC_NREGS_24
,
549 register_savevm("tcx", addr
, 4, tcx_save
, tcx_load
, s
);
550 qemu_register_reset(tcx_reset
, s
);
552 dpy_resize(s
->ds
, width
, height
);
555 static void tcx_screen_dump(void *opaque
, const char *filename
)
557 TCXState
*s
= opaque
;
562 f
= fopen(filename
, "wb");
565 fprintf(f
, "P6\n%d %d\n%d\n", s
->width
, s
->height
, 255);
567 for(y
= 0; y
< s
->height
; y
++) {
569 for(x
= 0; x
< s
->width
; x
++) {
582 static void tcx24_screen_dump(void *opaque
, const char *filename
)
584 TCXState
*s
= opaque
;
587 uint32_t *s24
, *cptr
, dval
;
590 f
= fopen(filename
, "wb");
593 fprintf(f
, "P6\n%d %d\n%d\n", s
->width
, s
->height
, 255);
597 for(y
= 0; y
< s
->height
; y
++) {
599 for(x
= 0; x
< s
->width
; x
++, d
++, s24
++) {
600 if ((*cptr
++ & 0xff000000) == 0x03000000) { // 24-bit direct
601 dval
= *s24
& 0x00ffffff;
602 fputc((dval
>> 16) & 0xff, f
);
603 fputc((dval
>> 8) & 0xff, f
);
604 fputc(dval
& 0xff, f
);