2004-10-01 06:13:50 +08:00
|
|
|
/*
|
2005-03-13 17:43:36 +08:00
|
|
|
* QEMU TCX Frame buffer
|
2004-10-01 06:13:50 +08:00
|
|
|
*
|
2005-03-13 17:43:36 +08:00
|
|
|
* Copyright (c) 2003-2005 Fabrice Bellard
|
2004-10-01 06:13:50 +08:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "vl.h"
|
|
|
|
|
|
|
|
#define MAXX 1024
|
|
|
|
#define MAXY 768
|
2005-03-13 17:43:36 +08:00
|
|
|
#define TCX_DAC_NREGS 16
|
2004-10-01 06:13:50 +08:00
|
|
|
|
|
|
|
typedef struct TCXState {
|
2004-10-05 05:23:09 +08:00
|
|
|
uint32_t addr;
|
2004-10-01 06:13:50 +08:00
|
|
|
DisplayState *ds;
|
2004-10-05 05:23:09 +08:00
|
|
|
uint8_t *vram;
|
2006-08-03 06:19:33 +08:00
|
|
|
ram_addr_t vram_offset;
|
2005-03-13 17:43:36 +08:00
|
|
|
uint16_t width, height;
|
2004-12-20 07:18:01 +08:00
|
|
|
uint8_t r[256], g[256], b[256];
|
2006-09-09 19:31:34 +08:00
|
|
|
uint32_t palette[256];
|
2005-03-13 17:43:36 +08:00
|
|
|
uint8_t dac_index, dac_state;
|
2004-10-01 06:13:50 +08:00
|
|
|
} TCXState;
|
|
|
|
|
2006-04-09 09:06:34 +08:00
|
|
|
static void tcx_screen_dump(void *opaque, const char *filename);
|
|
|
|
|
2006-09-09 19:31:34 +08:00
|
|
|
/* XXX: unify with vga draw line functions */
|
|
|
|
static inline unsigned int rgb_to_pixel8(unsigned int r, unsigned int g, unsigned b)
|
|
|
|
{
|
|
|
|
return ((r >> 5) << 5) | ((g >> 5) << 2) | (b >> 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int rgb_to_pixel15(unsigned int r, unsigned int g, unsigned b)
|
|
|
|
{
|
|
|
|
return ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int rgb_to_pixel16(unsigned int r, unsigned int g, unsigned b)
|
|
|
|
{
|
|
|
|
return ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int rgb_to_pixel32(unsigned int r, unsigned int g, unsigned b)
|
|
|
|
{
|
|
|
|
return (r << 16) | (g << 8) | b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_palette_entries(TCXState *s, int start, int end)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = start; i < end; i++) {
|
|
|
|
switch(s->ds->depth) {
|
|
|
|
default:
|
|
|
|
case 8:
|
|
|
|
s->palette[i] = rgb_to_pixel8(s->r[i], s->g[i], s->b[i]);
|
|
|
|
break;
|
|
|
|
case 15:
|
|
|
|
s->palette[i] = rgb_to_pixel15(s->r[i], s->g[i], s->b[i]);
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
s->palette[i] = rgb_to_pixel16(s->r[i], s->g[i], s->b[i]);
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
s->palette[i] = rgb_to_pixel32(s->r[i], s->g[i], s->b[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-20 07:18:01 +08:00
|
|
|
static void tcx_draw_line32(TCXState *s1, uint8_t *d,
|
|
|
|
const uint8_t *s, int width)
|
2004-10-01 06:13:50 +08:00
|
|
|
{
|
2004-12-20 07:18:01 +08:00
|
|
|
int x;
|
|
|
|
uint8_t val;
|
2006-12-22 01:24:45 +08:00
|
|
|
uint32_t *p = (uint32_t *)d;
|
2004-12-20 07:18:01 +08:00
|
|
|
|
|
|
|
for(x = 0; x < width; x++) {
|
|
|
|
val = *s++;
|
2006-12-22 01:24:45 +08:00
|
|
|
*p++ = s1->palette[val];
|
2004-12-20 07:18:01 +08:00
|
|
|
}
|
2004-10-01 06:13:50 +08:00
|
|
|
}
|
|
|
|
|
2006-09-09 19:31:34 +08:00
|
|
|
static void tcx_draw_line16(TCXState *s1, uint8_t *d,
|
2004-12-20 07:18:01 +08:00
|
|
|
const uint8_t *s, int width)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
uint8_t val;
|
2006-12-22 01:24:45 +08:00
|
|
|
uint16_t *p = (uint16_t *)d;
|
2004-10-05 05:23:09 +08:00
|
|
|
|
2004-12-20 07:18:01 +08:00
|
|
|
for(x = 0; x < width; x++) {
|
|
|
|
val = *s++;
|
2006-12-22 01:24:45 +08:00
|
|
|
*p++ = s1->palette[val];
|
2004-12-20 07:18:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tcx_draw_line8(TCXState *s1, uint8_t *d,
|
|
|
|
const uint8_t *s, int width)
|
2004-10-01 06:13:50 +08:00
|
|
|
{
|
2004-12-20 07:18:01 +08:00
|
|
|
int x;
|
|
|
|
uint8_t val;
|
|
|
|
|
|
|
|
for(x = 0; x < width; x++) {
|
|
|
|
val = *s++;
|
2006-09-09 19:31:34 +08:00
|
|
|
*d++ = s1->palette[val];
|
2004-10-01 06:13:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-20 07:18:01 +08:00
|
|
|
/* Fixed line length 1024 allows us to do nice tricks not possible on
|
|
|
|
VGA... */
|
2006-04-09 09:06:34 +08:00
|
|
|
static void tcx_update_display(void *opaque)
|
2004-10-01 06:13:50 +08:00
|
|
|
{
|
2004-12-20 07:18:01 +08:00
|
|
|
TCXState *ts = opaque;
|
2006-08-03 06:19:33 +08:00
|
|
|
ram_addr_t page, page_min, page_max;
|
|
|
|
int y, y_start, dd, ds;
|
2004-12-20 07:18:01 +08:00
|
|
|
uint8_t *d, *s;
|
|
|
|
void (*f)(TCXState *s1, uint8_t *d, const uint8_t *s, int width);
|
|
|
|
|
|
|
|
if (ts->ds->depth == 0)
|
|
|
|
return;
|
2005-03-13 17:43:36 +08:00
|
|
|
page = ts->vram_offset;
|
2004-12-20 07:18:01 +08:00
|
|
|
y_start = -1;
|
2006-08-03 06:19:33 +08:00
|
|
|
page_min = 0xffffffff;
|
|
|
|
page_max = 0;
|
2004-12-20 07:18:01 +08:00
|
|
|
d = ts->ds->data;
|
2005-03-13 17:43:36 +08:00
|
|
|
s = ts->vram;
|
2004-12-20 07:18:01 +08:00
|
|
|
dd = ts->ds->linesize;
|
|
|
|
ds = 1024;
|
|
|
|
|
|
|
|
switch (ts->ds->depth) {
|
|
|
|
case 32:
|
|
|
|
f = tcx_draw_line32;
|
|
|
|
break;
|
2006-09-09 19:31:34 +08:00
|
|
|
case 15:
|
|
|
|
case 16:
|
|
|
|
f = tcx_draw_line16;
|
2004-12-20 07:18:01 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case 8:
|
|
|
|
f = tcx_draw_line8;
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
return;
|
|
|
|
}
|
2005-01-28 08:00:27 +08:00
|
|
|
|
2005-03-13 17:43:36 +08:00
|
|
|
for(y = 0; y < ts->height; y += 4, page += TARGET_PAGE_SIZE) {
|
2005-02-11 06:00:27 +08:00
|
|
|
if (cpu_physical_memory_get_dirty(page, VGA_DIRTY_FLAG)) {
|
2004-12-20 07:18:01 +08:00
|
|
|
if (y_start < 0)
|
|
|
|
y_start = y;
|
|
|
|
if (page < page_min)
|
|
|
|
page_min = page;
|
|
|
|
if (page > page_max)
|
|
|
|
page_max = page;
|
2005-03-13 17:43:36 +08:00
|
|
|
f(ts, d, s, ts->width);
|
2004-12-20 07:18:01 +08:00
|
|
|
d += dd;
|
|
|
|
s += ds;
|
2005-03-13 17:43:36 +08:00
|
|
|
f(ts, d, s, ts->width);
|
2004-12-20 07:18:01 +08:00
|
|
|
d += dd;
|
|
|
|
s += ds;
|
2005-03-13 17:43:36 +08:00
|
|
|
f(ts, d, s, ts->width);
|
2004-12-20 07:18:01 +08:00
|
|
|
d += dd;
|
|
|
|
s += ds;
|
2005-03-13 17:43:36 +08:00
|
|
|
f(ts, d, s, ts->width);
|
2004-12-20 07:18:01 +08:00
|
|
|
d += dd;
|
|
|
|
s += ds;
|
|
|
|
} else {
|
|
|
|
if (y_start >= 0) {
|
|
|
|
/* flush to display */
|
|
|
|
dpy_update(ts->ds, 0, y_start,
|
2005-03-13 17:43:36 +08:00
|
|
|
ts->width, y - y_start);
|
2004-12-20 07:18:01 +08:00
|
|
|
y_start = -1;
|
|
|
|
}
|
|
|
|
d += dd * 4;
|
|
|
|
s += ds * 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (y_start >= 0) {
|
|
|
|
/* flush to display */
|
|
|
|
dpy_update(ts->ds, 0, y_start,
|
2005-03-13 17:43:36 +08:00
|
|
|
ts->width, y - y_start);
|
2004-12-20 07:18:01 +08:00
|
|
|
}
|
|
|
|
/* reset modified pages */
|
2006-08-03 06:19:33 +08:00
|
|
|
if (page_min <= page_max) {
|
2005-02-11 06:00:27 +08:00
|
|
|
cpu_physical_memory_reset_dirty(page_min, page_max + TARGET_PAGE_SIZE,
|
|
|
|
VGA_DIRTY_FLAG);
|
2004-12-20 07:18:01 +08:00
|
|
|
}
|
2004-10-01 06:13:50 +08:00
|
|
|
}
|
|
|
|
|
2006-04-09 09:06:34 +08:00
|
|
|
static void tcx_invalidate_display(void *opaque)
|
2004-10-01 06:13:50 +08:00
|
|
|
{
|
2004-12-20 07:18:01 +08:00
|
|
|
TCXState *s = opaque;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MAXX*MAXY; i += TARGET_PAGE_SIZE) {
|
|
|
|
cpu_physical_memory_set_dirty(s->vram_offset + i);
|
|
|
|
}
|
2004-10-01 06:13:50 +08:00
|
|
|
}
|
|
|
|
|
2004-12-20 07:18:01 +08:00
|
|
|
static void tcx_save(QEMUFile *f, void *opaque)
|
2004-10-01 06:13:50 +08:00
|
|
|
{
|
|
|
|
TCXState *s = opaque;
|
2004-12-20 07:18:01 +08:00
|
|
|
|
|
|
|
qemu_put_be32s(f, (uint32_t *)&s->addr);
|
|
|
|
qemu_put_be32s(f, (uint32_t *)&s->vram);
|
2005-03-13 17:43:36 +08:00
|
|
|
qemu_put_be16s(f, (uint16_t *)&s->height);
|
|
|
|
qemu_put_be16s(f, (uint16_t *)&s->width);
|
2004-12-20 07:18:01 +08:00
|
|
|
qemu_put_buffer(f, s->r, 256);
|
|
|
|
qemu_put_buffer(f, s->g, 256);
|
|
|
|
qemu_put_buffer(f, s->b, 256);
|
2005-03-13 17:43:36 +08:00
|
|
|
qemu_put_8s(f, &s->dac_index);
|
|
|
|
qemu_put_8s(f, &s->dac_state);
|
2004-10-01 06:13:50 +08:00
|
|
|
}
|
|
|
|
|
2004-12-20 07:18:01 +08:00
|
|
|
static int tcx_load(QEMUFile *f, void *opaque, int version_id)
|
2004-10-01 06:13:50 +08:00
|
|
|
{
|
2004-12-20 07:18:01 +08:00
|
|
|
TCXState *s = opaque;
|
|
|
|
|
|
|
|
if (version_id != 1)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
qemu_get_be32s(f, (uint32_t *)&s->addr);
|
|
|
|
qemu_get_be32s(f, (uint32_t *)&s->vram);
|
2005-03-13 17:43:36 +08:00
|
|
|
qemu_get_be16s(f, (uint16_t *)&s->height);
|
|
|
|
qemu_get_be16s(f, (uint16_t *)&s->width);
|
2004-12-20 07:18:01 +08:00
|
|
|
qemu_get_buffer(f, s->r, 256);
|
|
|
|
qemu_get_buffer(f, s->g, 256);
|
|
|
|
qemu_get_buffer(f, s->b, 256);
|
2005-03-13 17:43:36 +08:00
|
|
|
qemu_get_8s(f, &s->dac_index);
|
|
|
|
qemu_get_8s(f, &s->dac_state);
|
2006-09-09 19:31:34 +08:00
|
|
|
update_palette_entries(s, 0, 256);
|
2007-04-14 03:24:07 +08:00
|
|
|
tcx_invalidate_display(s);
|
|
|
|
|
2004-12-20 07:18:01 +08:00
|
|
|
return 0;
|
2004-10-01 06:13:50 +08:00
|
|
|
}
|
|
|
|
|
2004-12-20 07:18:01 +08:00
|
|
|
static void tcx_reset(void *opaque)
|
2004-10-01 06:13:50 +08:00
|
|
|
{
|
2004-12-20 07:18:01 +08:00
|
|
|
TCXState *s = opaque;
|
|
|
|
|
|
|
|
/* Initialize palette */
|
|
|
|
memset(s->r, 0, 256);
|
|
|
|
memset(s->g, 0, 256);
|
|
|
|
memset(s->b, 0, 256);
|
|
|
|
s->r[255] = s->g[255] = s->b[255] = 255;
|
2006-09-09 19:31:34 +08:00
|
|
|
update_palette_entries(s, 0, 256);
|
2004-12-20 07:18:01 +08:00
|
|
|
memset(s->vram, 0, MAXX*MAXY);
|
2005-02-11 06:00:27 +08:00
|
|
|
cpu_physical_memory_reset_dirty(s->vram_offset, s->vram_offset + MAXX*MAXY,
|
|
|
|
VGA_DIRTY_FLAG);
|
2005-03-13 17:43:36 +08:00
|
|
|
s->dac_index = 0;
|
|
|
|
s->dac_state = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t tcx_dac_readl(void *opaque, target_phys_addr_t addr)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tcx_dac_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
|
|
|
|
{
|
|
|
|
TCXState *s = opaque;
|
|
|
|
uint32_t saddr;
|
|
|
|
|
|
|
|
saddr = (addr & (TCX_DAC_NREGS - 1)) >> 2;
|
|
|
|
switch (saddr) {
|
|
|
|
case 0:
|
|
|
|
s->dac_index = val >> 24;
|
|
|
|
s->dac_state = 0;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
switch (s->dac_state) {
|
|
|
|
case 0:
|
|
|
|
s->r[s->dac_index] = val >> 24;
|
2006-09-09 19:31:34 +08:00
|
|
|
update_palette_entries(s, s->dac_index, s->dac_index + 1);
|
2005-03-13 17:43:36 +08:00
|
|
|
s->dac_state++;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
s->g[s->dac_index] = val >> 24;
|
2006-09-09 19:31:34 +08:00
|
|
|
update_palette_entries(s, s->dac_index, s->dac_index + 1);
|
2005-03-13 17:43:36 +08:00
|
|
|
s->dac_state++;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
s->b[s->dac_index] = val >> 24;
|
2006-09-09 19:31:34 +08:00
|
|
|
update_palette_entries(s, s->dac_index, s->dac_index + 1);
|
2005-03-13 17:43:36 +08:00
|
|
|
default:
|
|
|
|
s->dac_state = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
2004-10-01 06:13:50 +08:00
|
|
|
}
|
|
|
|
|
2005-03-13 17:43:36 +08:00
|
|
|
static CPUReadMemoryFunc *tcx_dac_read[3] = {
|
|
|
|
tcx_dac_readl,
|
|
|
|
tcx_dac_readl,
|
|
|
|
tcx_dac_readl,
|
|
|
|
};
|
|
|
|
|
|
|
|
static CPUWriteMemoryFunc *tcx_dac_write[3] = {
|
|
|
|
tcx_dac_writel,
|
|
|
|
tcx_dac_writel,
|
|
|
|
tcx_dac_writel,
|
|
|
|
};
|
|
|
|
|
2006-04-09 09:06:34 +08:00
|
|
|
void tcx_init(DisplayState *ds, uint32_t addr, uint8_t *vram_base,
|
|
|
|
unsigned long vram_offset, int vram_size, int width, int height)
|
2004-10-01 06:13:50 +08:00
|
|
|
{
|
|
|
|
TCXState *s;
|
2005-03-13 17:43:36 +08:00
|
|
|
int io_memory;
|
2004-10-01 06:13:50 +08:00
|
|
|
|
|
|
|
s = qemu_mallocz(sizeof(TCXState));
|
|
|
|
if (!s)
|
2006-04-09 09:06:34 +08:00
|
|
|
return;
|
2004-10-01 06:13:50 +08:00
|
|
|
s->ds = ds;
|
2004-10-05 05:23:09 +08:00
|
|
|
s->addr = addr;
|
2004-12-20 07:18:01 +08:00
|
|
|
s->vram = vram_base;
|
|
|
|
s->vram_offset = vram_offset;
|
2005-03-13 17:43:36 +08:00
|
|
|
s->width = width;
|
|
|
|
s->height = height;
|
2004-12-20 07:18:01 +08:00
|
|
|
|
2005-03-13 17:43:36 +08:00
|
|
|
cpu_register_physical_memory(addr + 0x800000, vram_size, vram_offset);
|
|
|
|
io_memory = cpu_register_io_memory(0, tcx_dac_read, tcx_dac_write, s);
|
|
|
|
cpu_register_physical_memory(addr + 0x200000, TCX_DAC_NREGS, io_memory);
|
2004-12-20 07:18:01 +08:00
|
|
|
|
2006-04-09 09:06:34 +08:00
|
|
|
graphic_console_init(s->ds, tcx_update_display, tcx_invalidate_display,
|
|
|
|
tcx_screen_dump, s);
|
2004-12-20 07:18:01 +08:00
|
|
|
register_savevm("tcx", addr, 1, tcx_save, tcx_load, s);
|
|
|
|
qemu_register_reset(tcx_reset, s);
|
|
|
|
tcx_reset(s);
|
2005-03-13 17:43:36 +08:00
|
|
|
dpy_resize(s->ds, width, height);
|
2004-10-01 06:13:50 +08:00
|
|
|
}
|
|
|
|
|
2006-04-09 09:06:34 +08:00
|
|
|
static void tcx_screen_dump(void *opaque, const char *filename)
|
2004-10-05 05:23:09 +08:00
|
|
|
{
|
2004-12-20 07:18:01 +08:00
|
|
|
TCXState *s = opaque;
|
2004-10-05 05:23:09 +08:00
|
|
|
FILE *f;
|
2004-12-20 07:18:01 +08:00
|
|
|
uint8_t *d, *d1, v;
|
2004-10-05 05:23:09 +08:00
|
|
|
int y, x;
|
|
|
|
|
|
|
|
f = fopen(filename, "wb");
|
|
|
|
if (!f)
|
2004-12-20 07:18:01 +08:00
|
|
|
return;
|
2005-03-13 17:43:36 +08:00
|
|
|
fprintf(f, "P6\n%d %d\n%d\n", s->width, s->height, 255);
|
|
|
|
d1 = s->vram;
|
|
|
|
for(y = 0; y < s->height; y++) {
|
2004-10-05 05:23:09 +08:00
|
|
|
d = d1;
|
2005-03-13 17:43:36 +08:00
|
|
|
for(x = 0; x < s->width; x++) {
|
2004-10-05 05:23:09 +08:00
|
|
|
v = *d;
|
2004-12-20 07:18:01 +08:00
|
|
|
fputc(s->r[v], f);
|
|
|
|
fputc(s->g[v], f);
|
|
|
|
fputc(s->b[v], f);
|
2004-10-05 05:23:09 +08:00
|
|
|
d++;
|
|
|
|
}
|
2004-12-20 07:18:01 +08:00
|
|
|
d1 += MAXX;
|
2004-10-05 05:23:09 +08:00
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|