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
;
40 uint32_t *vram24
, *cplane
;
41 ram_addr_t vram_offset
, vram24_offset
, cplane_offset
;
42 uint16_t width
, height
, depth
;
43 uint8_t r
[256], g
[256], b
[256];
44 uint32_t palette
[256];
45 uint8_t dac_index
, dac_state
;
48 static void tcx_screen_dump(void *opaque
, const char *filename
);
49 static void tcx24_screen_dump(void *opaque
, const char *filename
);
50 static void tcx_invalidate_display(void *opaque
);
51 static void tcx24_invalidate_display(void *opaque
);
53 static void update_palette_entries(TCXState
*s
, int start
, int end
)
56 for(i
= start
; i
< end
; i
++) {
57 switch(ds_get_bits_per_pixel(s
->ds
)) {
60 s
->palette
[i
] = rgb_to_pixel8(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
63 s
->palette
[i
] = rgb_to_pixel15(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
66 s
->palette
[i
] = rgb_to_pixel16(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
69 if (is_surface_bgr(s
->ds
->surface
))
70 s
->palette
[i
] = rgb_to_pixel32bgr(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
72 s
->palette
[i
] = rgb_to_pixel32(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
77 tcx24_invalidate_display(s
);
79 tcx_invalidate_display(s
);
82 static void tcx_draw_line32(TCXState
*s1
, uint8_t *d
,
83 const uint8_t *s
, int width
)
87 uint32_t *p
= (uint32_t *)d
;
89 for(x
= 0; x
< width
; x
++) {
91 *p
++ = s1
->palette
[val
];
95 static void tcx_draw_line16(TCXState
*s1
, uint8_t *d
,
96 const uint8_t *s
, int width
)
100 uint16_t *p
= (uint16_t *)d
;
102 for(x
= 0; x
< width
; x
++) {
104 *p
++ = s1
->palette
[val
];
108 static void tcx_draw_line8(TCXState
*s1
, uint8_t *d
,
109 const uint8_t *s
, int width
)
114 for(x
= 0; x
< width
; x
++) {
116 *d
++ = s1
->palette
[val
];
121 XXX Could be much more optimal:
122 * detect if line/page/whole screen is in 24 bit mode
123 * if destination is also BGR, use memcpy
125 static inline void tcx24_draw_line32(TCXState
*s1
, uint8_t *d
,
126 const uint8_t *s
, int width
,
127 const uint32_t *cplane
,
132 uint32_t *p
= (uint32_t *)d
;
135 bgr
= is_surface_bgr(s1
->ds
->surface
);
136 for(x
= 0; x
< width
; x
++, s
++, s24
++) {
137 if ((be32_to_cpu(*cplane
++) & 0xff000000) == 0x03000000) {
138 // 24-bit direct, BGR order
145 dval
= rgb_to_pixel32bgr(r
, g
, b
);
147 dval
= rgb_to_pixel32(r
, g
, b
);
150 dval
= s1
->palette
[val
];
156 static inline int check_dirty(ram_addr_t page
, ram_addr_t page24
,
162 ret
= cpu_physical_memory_get_dirty(page
, VGA_DIRTY_FLAG
);
163 for (off
= 0; off
< TARGET_PAGE_SIZE
* 4; off
+= TARGET_PAGE_SIZE
) {
164 ret
|= cpu_physical_memory_get_dirty(page24
+ off
, VGA_DIRTY_FLAG
);
165 ret
|= cpu_physical_memory_get_dirty(cpage
+ off
, VGA_DIRTY_FLAG
);
170 static inline void reset_dirty(TCXState
*ts
, ram_addr_t page_min
,
171 ram_addr_t page_max
, ram_addr_t page24
,
174 cpu_physical_memory_reset_dirty(page_min
, page_max
+ TARGET_PAGE_SIZE
,
176 page_min
-= ts
->vram_offset
;
177 page_max
-= ts
->vram_offset
;
178 cpu_physical_memory_reset_dirty(page24
+ page_min
* 4,
179 page24
+ page_max
* 4 + TARGET_PAGE_SIZE
,
181 cpu_physical_memory_reset_dirty(cpage
+ page_min
* 4,
182 cpage
+ page_max
* 4 + TARGET_PAGE_SIZE
,
186 /* Fixed line length 1024 allows us to do nice tricks not possible on
188 static void tcx_update_display(void *opaque
)
190 TCXState
*ts
= opaque
;
191 ram_addr_t page
, page_min
, page_max
;
192 int y
, y_start
, dd
, ds
;
194 void (*f
)(TCXState
*s1
, uint8_t *dst
, const uint8_t *src
, int width
);
196 if (ds_get_bits_per_pixel(ts
->ds
) == 0)
198 page
= ts
->vram_offset
;
200 page_min
= 0xffffffff;
202 d
= ds_get_data(ts
->ds
);
204 dd
= ds_get_linesize(ts
->ds
);
207 switch (ds_get_bits_per_pixel(ts
->ds
)) {
223 for(y
= 0; y
< ts
->height
; y
+= 4, page
+= TARGET_PAGE_SIZE
) {
224 if (cpu_physical_memory_get_dirty(page
, VGA_DIRTY_FLAG
)) {
231 f(ts
, d
, s
, ts
->width
);
234 f(ts
, d
, s
, ts
->width
);
237 f(ts
, d
, s
, ts
->width
);
240 f(ts
, d
, s
, ts
->width
);
245 /* flush to display */
246 dpy_update(ts
->ds
, 0, y_start
,
247 ts
->width
, y
- y_start
);
255 /* flush to display */
256 dpy_update(ts
->ds
, 0, y_start
,
257 ts
->width
, y
- y_start
);
259 /* reset modified pages */
260 if (page_min
<= page_max
) {
261 cpu_physical_memory_reset_dirty(page_min
, page_max
+ TARGET_PAGE_SIZE
,
266 static void tcx24_update_display(void *opaque
)
268 TCXState
*ts
= opaque
;
269 ram_addr_t page
, page_min
, page_max
, cpage
, page24
;
270 int y
, y_start
, dd
, ds
;
272 uint32_t *cptr
, *s24
;
274 if (ds_get_bits_per_pixel(ts
->ds
) != 32)
276 page
= ts
->vram_offset
;
277 page24
= ts
->vram24_offset
;
278 cpage
= ts
->cplane_offset
;
280 page_min
= 0xffffffff;
282 d
= ds_get_data(ts
->ds
);
286 dd
= ds_get_linesize(ts
->ds
);
289 for(y
= 0; y
< ts
->height
; y
+= 4, page
+= TARGET_PAGE_SIZE
,
290 page24
+= TARGET_PAGE_SIZE
, cpage
+= TARGET_PAGE_SIZE
) {
291 if (check_dirty(page
, page24
, cpage
)) {
298 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
303 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
308 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
313 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
320 /* flush to display */
321 dpy_update(ts
->ds
, 0, y_start
,
322 ts
->width
, y
- y_start
);
332 /* flush to display */
333 dpy_update(ts
->ds
, 0, y_start
,
334 ts
->width
, y
- y_start
);
336 /* reset modified pages */
337 if (page_min
<= page_max
) {
338 reset_dirty(ts
, page_min
, page_max
, page24
, cpage
);
342 static void tcx_invalidate_display(void *opaque
)
344 TCXState
*s
= opaque
;
347 for (i
= 0; i
< MAXX
*MAXY
; i
+= TARGET_PAGE_SIZE
) {
348 cpu_physical_memory_set_dirty(s
->vram_offset
+ i
);
352 static void tcx24_invalidate_display(void *opaque
)
354 TCXState
*s
= opaque
;
357 tcx_invalidate_display(s
);
358 for (i
= 0; i
< MAXX
*MAXY
* 4; i
+= TARGET_PAGE_SIZE
) {
359 cpu_physical_memory_set_dirty(s
->vram24_offset
+ i
);
360 cpu_physical_memory_set_dirty(s
->cplane_offset
+ i
);
364 static void tcx_save(QEMUFile
*f
, void *opaque
)
366 TCXState
*s
= opaque
;
368 qemu_put_be16s(f
, &s
->height
);
369 qemu_put_be16s(f
, &s
->width
);
370 qemu_put_be16s(f
, &s
->depth
);
371 qemu_put_buffer(f
, s
->r
, 256);
372 qemu_put_buffer(f
, s
->g
, 256);
373 qemu_put_buffer(f
, s
->b
, 256);
374 qemu_put_8s(f
, &s
->dac_index
);
375 qemu_put_8s(f
, &s
->dac_state
);
378 static int tcx_load(QEMUFile
*f
, void *opaque
, int version_id
)
380 TCXState
*s
= opaque
;
383 if (version_id
!= 3 && version_id
!= 4)
386 if (version_id
== 3) {
387 qemu_get_be32s(f
, &dummy
);
388 qemu_get_be32s(f
, &dummy
);
389 qemu_get_be32s(f
, &dummy
);
391 qemu_get_be16s(f
, &s
->height
);
392 qemu_get_be16s(f
, &s
->width
);
393 qemu_get_be16s(f
, &s
->depth
);
394 qemu_get_buffer(f
, s
->r
, 256);
395 qemu_get_buffer(f
, s
->g
, 256);
396 qemu_get_buffer(f
, s
->b
, 256);
397 qemu_get_8s(f
, &s
->dac_index
);
398 qemu_get_8s(f
, &s
->dac_state
);
399 update_palette_entries(s
, 0, 256);
401 tcx24_invalidate_display(s
);
403 tcx_invalidate_display(s
);
408 static void tcx_reset(void *opaque
)
410 TCXState
*s
= opaque
;
412 /* Initialize palette */
413 memset(s
->r
, 0, 256);
414 memset(s
->g
, 0, 256);
415 memset(s
->b
, 0, 256);
416 s
->r
[255] = s
->g
[255] = s
->b
[255] = 255;
417 update_palette_entries(s
, 0, 256);
418 memset(s
->vram
, 0, MAXX
*MAXY
);
419 cpu_physical_memory_reset_dirty(s
->vram_offset
, s
->vram_offset
+
420 MAXX
* MAXY
* (1 + 4 + 4), VGA_DIRTY_FLAG
);
425 static uint32_t tcx_dac_readl(void *opaque
, target_phys_addr_t addr
)
430 static void tcx_dac_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
432 TCXState
*s
= opaque
;
436 s
->dac_index
= val
>> 24;
440 switch (s
->dac_state
) {
442 s
->r
[s
->dac_index
] = val
>> 24;
443 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
447 s
->g
[s
->dac_index
] = val
>> 24;
448 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
452 s
->b
[s
->dac_index
] = val
>> 24;
453 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
454 s
->dac_index
= (s
->dac_index
+ 1) & 255; // Index autoincrement
466 static CPUReadMemoryFunc
*tcx_dac_read
[3] = {
472 static CPUWriteMemoryFunc
*tcx_dac_write
[3] = {
478 static uint32_t tcx_dummy_readl(void *opaque
, target_phys_addr_t addr
)
483 static void tcx_dummy_writel(void *opaque
, target_phys_addr_t addr
,
488 static CPUReadMemoryFunc
*tcx_dummy_read
[3] = {
494 static CPUWriteMemoryFunc
*tcx_dummy_write
[3] = {
500 void tcx_init(target_phys_addr_t addr
, uint8_t *vram_base
,
501 unsigned long vram_offset
, int vram_size
, int width
, int height
,
505 int io_memory
, dummy_memory
;
508 s
= qemu_mallocz(sizeof(TCXState
));
510 s
->vram_offset
= vram_offset
;
518 cpu_register_physical_memory(addr
+ 0x00800000ULL
, size
, vram_offset
);
522 io_memory
= cpu_register_io_memory(0, tcx_dac_read
, tcx_dac_write
, s
);
523 cpu_register_physical_memory(addr
+ 0x00200000ULL
, TCX_DAC_NREGS
,
526 dummy_memory
= cpu_register_io_memory(0, tcx_dummy_read
, tcx_dummy_write
,
528 cpu_register_physical_memory(addr
+ 0x00700000ULL
, TCX_TEC_NREGS
,
532 size
= vram_size
* 4;
533 s
->vram24
= (uint32_t *)vram_base
;
534 s
->vram24_offset
= vram_offset
;
535 cpu_register_physical_memory(addr
+ 0x02000000ULL
, size
, vram_offset
);
540 size
= vram_size
* 4;
541 s
->cplane
= (uint32_t *)vram_base
;
542 s
->cplane_offset
= vram_offset
;
543 cpu_register_physical_memory(addr
+ 0x0a000000ULL
, size
, vram_offset
);
544 s
->ds
= graphic_console_init(tcx24_update_display
,
545 tcx24_invalidate_display
,
546 tcx24_screen_dump
, NULL
, s
);
548 cpu_register_physical_memory(addr
+ 0x00300000ULL
, TCX_THC_NREGS_8
,
550 s
->ds
= graphic_console_init(tcx_update_display
,
551 tcx_invalidate_display
,
552 tcx_screen_dump
, NULL
, s
);
554 // NetBSD writes here even with 8-bit display
555 cpu_register_physical_memory(addr
+ 0x00301000ULL
, TCX_THC_NREGS_24
,
558 register_savevm("tcx", addr
, 4, tcx_save
, tcx_load
, s
);
559 qemu_register_reset(tcx_reset
, s
);
561 qemu_console_resize(s
->ds
, width
, height
);
564 static void tcx_screen_dump(void *opaque
, const char *filename
)
566 TCXState
*s
= opaque
;
571 f
= fopen(filename
, "wb");
574 fprintf(f
, "P6\n%d %d\n%d\n", s
->width
, s
->height
, 255);
576 for(y
= 0; y
< s
->height
; y
++) {
578 for(x
= 0; x
< s
->width
; x
++) {
591 static void tcx24_screen_dump(void *opaque
, const char *filename
)
593 TCXState
*s
= opaque
;
596 uint32_t *s24
, *cptr
, dval
;
599 f
= fopen(filename
, "wb");
602 fprintf(f
, "P6\n%d %d\n%d\n", s
->width
, s
->height
, 255);
606 for(y
= 0; y
< s
->height
; y
++) {
608 for(x
= 0; x
< s
->width
; x
++, d
++, s24
++) {
609 if ((*cptr
++ & 0xff000000) == 0x03000000) { // 24-bit direct
610 dval
= *s24
& 0x00ffffff;
611 fputc((dval
>> 16) & 0xff, f
);
612 fputc((dval
>> 8) & 0xff, f
);
613 fputc(dval
& 0xff, f
);