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
27 #include "pixel_ops.h"
31 #define TCX_DAC_NREGS 16
32 #define TCX_THC_NREGS_8 0x081c
33 #define TCX_THC_NREGS_24 0x1000
34 #define TCX_TEC_NREGS 0x1000
36 typedef struct TCXState
{
37 target_phys_addr_t addr
;
41 uint32_t *vram24
, *cplane
;
42 ram_addr_t vram_offset
, vram24_offset
, cplane_offset
;
43 uint16_t width
, height
, depth
;
44 uint8_t r
[256], g
[256], b
[256];
45 uint32_t palette
[256];
46 uint8_t dac_index
, dac_state
;
49 static void tcx_screen_dump(void *opaque
, const char *filename
);
50 static void tcx24_screen_dump(void *opaque
, const char *filename
);
51 static void tcx_invalidate_display(void *opaque
);
52 static void tcx24_invalidate_display(void *opaque
);
54 static void update_palette_entries(TCXState
*s
, int start
, int end
)
57 for(i
= start
; i
< end
; i
++) {
58 switch(ds_get_bits_per_pixel(s
->ds
)) {
61 s
->palette
[i
] = rgb_to_pixel8(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
65 s
->palette
[i
] = rgb_to_pixel15bgr(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
67 s
->palette
[i
] = rgb_to_pixel15(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
71 s
->palette
[i
] = rgb_to_pixel16bgr(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
73 s
->palette
[i
] = rgb_to_pixel16(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
77 s
->palette
[i
] = rgb_to_pixel32bgr(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
79 s
->palette
[i
] = rgb_to_pixel32(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
84 tcx24_invalidate_display(s
);
86 tcx_invalidate_display(s
);
89 static void tcx_draw_line32(TCXState
*s1
, uint8_t *d
,
90 const uint8_t *s
, int width
)
94 uint32_t *p
= (uint32_t *)d
;
96 for(x
= 0; x
< width
; x
++) {
98 *p
++ = s1
->palette
[val
];
102 static void tcx_draw_line16(TCXState
*s1
, uint8_t *d
,
103 const uint8_t *s
, int width
)
107 uint16_t *p
= (uint16_t *)d
;
109 for(x
= 0; x
< width
; x
++) {
111 *p
++ = s1
->palette
[val
];
115 static void tcx_draw_line8(TCXState
*s1
, uint8_t *d
,
116 const uint8_t *s
, int width
)
121 for(x
= 0; x
< width
; x
++) {
123 *d
++ = s1
->palette
[val
];
128 XXX Could be much more optimal:
129 * detect if line/page/whole screen is in 24 bit mode
130 * if destination is also BGR, use memcpy
132 static inline void tcx24_draw_line32(TCXState
*s1
, uint8_t *d
,
133 const uint8_t *s
, int width
,
134 const uint32_t *cplane
,
139 uint32_t *p
= (uint32_t *)d
;
143 for(x
= 0; x
< width
; x
++, s
++, s24
++) {
144 if ((be32_to_cpu(*cplane
++) & 0xff000000) == 0x03000000) {
145 // 24-bit direct, BGR order
152 dval
= rgb_to_pixel32bgr(r
, g
, b
);
154 dval
= rgb_to_pixel32(r
, g
, b
);
157 dval
= s1
->palette
[val
];
163 static inline int check_dirty(ram_addr_t page
, ram_addr_t page24
,
169 ret
= cpu_physical_memory_get_dirty(page
, VGA_DIRTY_FLAG
);
170 for (off
= 0; off
< TARGET_PAGE_SIZE
* 4; off
+= TARGET_PAGE_SIZE
) {
171 ret
|= cpu_physical_memory_get_dirty(page24
+ off
, VGA_DIRTY_FLAG
);
172 ret
|= cpu_physical_memory_get_dirty(cpage
+ off
, VGA_DIRTY_FLAG
);
177 static inline void reset_dirty(TCXState
*ts
, ram_addr_t page_min
,
178 ram_addr_t page_max
, ram_addr_t page24
,
181 cpu_physical_memory_reset_dirty(page_min
, page_max
+ TARGET_PAGE_SIZE
,
183 page_min
-= ts
->vram_offset
;
184 page_max
-= ts
->vram_offset
;
185 cpu_physical_memory_reset_dirty(page24
+ page_min
* 4,
186 page24
+ page_max
* 4 + TARGET_PAGE_SIZE
,
188 cpu_physical_memory_reset_dirty(cpage
+ page_min
* 4,
189 cpage
+ page_max
* 4 + TARGET_PAGE_SIZE
,
193 /* Fixed line length 1024 allows us to do nice tricks not possible on
195 static void tcx_update_display(void *opaque
)
197 TCXState
*ts
= opaque
;
198 ram_addr_t page
, page_min
, page_max
;
199 int y
, y_start
, dd
, ds
;
201 void (*f
)(TCXState
*s1
, uint8_t *dst
, const uint8_t *src
, int width
);
203 if (ds_get_bits_per_pixel(ts
->ds
) == 0)
205 page
= ts
->vram_offset
;
207 page_min
= 0xffffffff;
209 d
= ds_get_data(ts
->ds
);
211 dd
= ds_get_linesize(ts
->ds
);
214 switch (ds_get_bits_per_pixel(ts
->ds
)) {
230 for(y
= 0; y
< ts
->height
; y
+= 4, page
+= TARGET_PAGE_SIZE
) {
231 if (cpu_physical_memory_get_dirty(page
, VGA_DIRTY_FLAG
)) {
238 f(ts
, d
, s
, ts
->width
);
241 f(ts
, d
, s
, ts
->width
);
244 f(ts
, d
, s
, ts
->width
);
247 f(ts
, d
, s
, ts
->width
);
252 /* flush to display */
253 dpy_update(ts
->ds
, 0, y_start
,
254 ts
->width
, y
- y_start
);
262 /* flush to display */
263 dpy_update(ts
->ds
, 0, y_start
,
264 ts
->width
, y
- y_start
);
266 /* reset modified pages */
267 if (page_min
<= page_max
) {
268 cpu_physical_memory_reset_dirty(page_min
, page_max
+ TARGET_PAGE_SIZE
,
273 static void tcx24_update_display(void *opaque
)
275 TCXState
*ts
= opaque
;
276 ram_addr_t page
, page_min
, page_max
, cpage
, page24
;
277 int y
, y_start
, dd
, ds
;
279 uint32_t *cptr
, *s24
;
281 if (ds_get_bits_per_pixel(ts
->ds
) != 32)
283 page
= ts
->vram_offset
;
284 page24
= ts
->vram24_offset
;
285 cpage
= ts
->cplane_offset
;
287 page_min
= 0xffffffff;
289 d
= ds_get_data(ts
->ds
);
293 dd
= ds_get_linesize(ts
->ds
);
296 for(y
= 0; y
< ts
->height
; y
+= 4, page
+= TARGET_PAGE_SIZE
,
297 page24
+= TARGET_PAGE_SIZE
, cpage
+= TARGET_PAGE_SIZE
) {
298 if (check_dirty(page
, page24
, cpage
)) {
305 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
310 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
315 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
320 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
327 /* flush to display */
328 dpy_update(ts
->ds
, 0, y_start
,
329 ts
->width
, y
- y_start
);
339 /* flush to display */
340 dpy_update(ts
->ds
, 0, y_start
,
341 ts
->width
, y
- y_start
);
343 /* reset modified pages */
344 if (page_min
<= page_max
) {
345 reset_dirty(ts
, page_min
, page_max
, page24
, cpage
);
349 static void tcx_invalidate_display(void *opaque
)
351 TCXState
*s
= opaque
;
354 for (i
= 0; i
< MAXX
*MAXY
; i
+= TARGET_PAGE_SIZE
) {
355 cpu_physical_memory_set_dirty(s
->vram_offset
+ i
);
359 static void tcx24_invalidate_display(void *opaque
)
361 TCXState
*s
= opaque
;
364 tcx_invalidate_display(s
);
365 for (i
= 0; i
< MAXX
*MAXY
* 4; i
+= TARGET_PAGE_SIZE
) {
366 cpu_physical_memory_set_dirty(s
->vram24_offset
+ i
);
367 cpu_physical_memory_set_dirty(s
->cplane_offset
+ i
);
371 static void tcx_save(QEMUFile
*f
, void *opaque
)
373 TCXState
*s
= opaque
;
375 qemu_put_be16s(f
, &s
->height
);
376 qemu_put_be16s(f
, &s
->width
);
377 qemu_put_be16s(f
, &s
->depth
);
378 qemu_put_buffer(f
, s
->r
, 256);
379 qemu_put_buffer(f
, s
->g
, 256);
380 qemu_put_buffer(f
, s
->b
, 256);
381 qemu_put_8s(f
, &s
->dac_index
);
382 qemu_put_8s(f
, &s
->dac_state
);
385 static int tcx_load(QEMUFile
*f
, void *opaque
, int version_id
)
387 TCXState
*s
= opaque
;
390 if (version_id
!= 3 && version_id
!= 4)
393 if (version_id
== 3) {
394 qemu_get_be32s(f
, &dummy
);
395 qemu_get_be32s(f
, &dummy
);
396 qemu_get_be32s(f
, &dummy
);
398 qemu_get_be16s(f
, &s
->height
);
399 qemu_get_be16s(f
, &s
->width
);
400 qemu_get_be16s(f
, &s
->depth
);
401 qemu_get_buffer(f
, s
->r
, 256);
402 qemu_get_buffer(f
, s
->g
, 256);
403 qemu_get_buffer(f
, s
->b
, 256);
404 qemu_get_8s(f
, &s
->dac_index
);
405 qemu_get_8s(f
, &s
->dac_state
);
406 update_palette_entries(s
, 0, 256);
408 tcx24_invalidate_display(s
);
410 tcx_invalidate_display(s
);
415 static void tcx_reset(void *opaque
)
417 TCXState
*s
= opaque
;
419 /* Initialize palette */
420 memset(s
->r
, 0, 256);
421 memset(s
->g
, 0, 256);
422 memset(s
->b
, 0, 256);
423 s
->r
[255] = s
->g
[255] = s
->b
[255] = 255;
424 update_palette_entries(s
, 0, 256);
425 memset(s
->vram
, 0, MAXX
*MAXY
);
426 cpu_physical_memory_reset_dirty(s
->vram_offset
, s
->vram_offset
+
427 MAXX
* MAXY
* (1 + 4 + 4), VGA_DIRTY_FLAG
);
432 static uint32_t tcx_dac_readl(void *opaque
, target_phys_addr_t addr
)
437 static void tcx_dac_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
439 TCXState
*s
= opaque
;
442 saddr
= (addr
& (TCX_DAC_NREGS
- 1)) >> 2;
445 s
->dac_index
= val
>> 24;
449 switch (s
->dac_state
) {
451 s
->r
[s
->dac_index
] = val
>> 24;
452 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
456 s
->g
[s
->dac_index
] = val
>> 24;
457 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
461 s
->b
[s
->dac_index
] = val
>> 24;
462 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
463 s
->dac_index
= (s
->dac_index
+ 1) & 255; // Index autoincrement
475 static CPUReadMemoryFunc
*tcx_dac_read
[3] = {
481 static CPUWriteMemoryFunc
*tcx_dac_write
[3] = {
487 static uint32_t tcx_dummy_readl(void *opaque
, target_phys_addr_t addr
)
492 static void tcx_dummy_writel(void *opaque
, target_phys_addr_t addr
,
497 static CPUReadMemoryFunc
*tcx_dummy_read
[3] = {
503 static CPUWriteMemoryFunc
*tcx_dummy_write
[3] = {
509 void tcx_init(DisplayState
*ds
, target_phys_addr_t addr
, uint8_t *vram_base
,
510 unsigned long vram_offset
, int vram_size
, int width
, int height
,
514 int io_memory
, dummy_memory
;
517 s
= qemu_mallocz(sizeof(TCXState
));
522 s
->vram_offset
= vram_offset
;
530 cpu_register_physical_memory(addr
+ 0x00800000ULL
, size
, vram_offset
);
534 io_memory
= cpu_register_io_memory(0, tcx_dac_read
, tcx_dac_write
, s
);
535 cpu_register_physical_memory(addr
+ 0x00200000ULL
, TCX_DAC_NREGS
,
538 dummy_memory
= cpu_register_io_memory(0, tcx_dummy_read
, tcx_dummy_write
,
540 cpu_register_physical_memory(addr
+ 0x00700000ULL
, TCX_TEC_NREGS
,
544 size
= vram_size
* 4;
545 s
->vram24
= (uint32_t *)vram_base
;
546 s
->vram24_offset
= vram_offset
;
547 cpu_register_physical_memory(addr
+ 0x02000000ULL
, size
, vram_offset
);
552 size
= vram_size
* 4;
553 s
->cplane
= (uint32_t *)vram_base
;
554 s
->cplane_offset
= vram_offset
;
555 cpu_register_physical_memory(addr
+ 0x0a000000ULL
, size
, vram_offset
);
556 s
->console
= graphic_console_init(s
->ds
, tcx24_update_display
,
557 tcx24_invalidate_display
,
558 tcx24_screen_dump
, NULL
, s
);
560 cpu_register_physical_memory(addr
+ 0x00300000ULL
, TCX_THC_NREGS_8
,
562 s
->console
= graphic_console_init(s
->ds
, tcx_update_display
,
563 tcx_invalidate_display
,
564 tcx_screen_dump
, NULL
, s
);
566 // NetBSD writes here even with 8-bit display
567 cpu_register_physical_memory(addr
+ 0x00301000ULL
, TCX_THC_NREGS_24
,
570 register_savevm("tcx", addr
, 4, tcx_save
, tcx_load
, s
);
571 qemu_register_reset(tcx_reset
, s
);
573 qemu_console_resize(s
->console
, width
, height
);
576 static void tcx_screen_dump(void *opaque
, const char *filename
)
578 TCXState
*s
= opaque
;
583 f
= fopen(filename
, "wb");
586 fprintf(f
, "P6\n%d %d\n%d\n", s
->width
, s
->height
, 255);
588 for(y
= 0; y
< s
->height
; y
++) {
590 for(x
= 0; x
< s
->width
; x
++) {
603 static void tcx24_screen_dump(void *opaque
, const char *filename
)
605 TCXState
*s
= opaque
;
608 uint32_t *s24
, *cptr
, dval
;
611 f
= fopen(filename
, "wb");
614 fprintf(f
, "P6\n%d %d\n%d\n", s
->width
, s
->height
, 255);
618 for(y
= 0; y
< s
->height
; y
++) {
620 for(x
= 0; x
< s
->width
; x
++, d
++, s24
++) {
621 if ((*cptr
++ & 0xff000000) == 0x03000000) { // 24-bit direct
622 dval
= *s24
& 0x00ffffff;
623 fputc((dval
>> 16) & 0xff, f
);
624 fputc((dval
>> 8) & 0xff, f
);
625 fputc(dval
& 0xff, f
);