Commit e89f66eca974d2a9d5d89271c6041daefdab2105

Authored by bellard
1 parent b6d78bfa

Hardware level VGA emulation (only text mode is tested)


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@344 c046a42c-6fe2-441c-8c8c-71466251a162
Showing 2 changed files with 1530 additions and 0 deletions
hw/vga.c 0 → 100644
  1 +/*
  2 + * QEMU VGA Emulator. An S3 86c968 is emulated
  3 + *
  4 + * Copyright (c) 2003 Fabrice Bellard
  5 + *
  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:
  12 + *
  13 + * The above copyright notice and this permission notice shall be included in
  14 + * all copies or substantial portions of the Software.
  15 + *
  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
  22 + * THE SOFTWARE.
  23 + */
  24 +#include <stdlib.h>
  25 +#include <stdio.h>
  26 +#include <stdarg.h>
  27 +#include <string.h>
  28 +#include <getopt.h>
  29 +#include <inttypes.h>
  30 +#include <unistd.h>
  31 +#include <sys/mman.h>
  32 +#include <fcntl.h>
  33 +#include <signal.h>
  34 +#include <time.h>
  35 +#include <sys/time.h>
  36 +#include <malloc.h>
  37 +#include <termios.h>
  38 +#include <sys/poll.h>
  39 +#include <errno.h>
  40 +#include <sys/wait.h>
  41 +#include <netinet/in.h>
  42 +
  43 +#include "cpu-i386.h"
  44 +#include "exec.h"
  45 +
  46 +#include "vl.h"
  47 +
  48 +#define NO_THUNK_TYPE_SIZE
  49 +#include "thunk.h"
  50 +
  51 +//#define DEBUG_VGA
  52 +
  53 +#define MSR_COLOR_EMULATION 0x01
  54 +#define MSR_PAGE_SELECT 0x20
  55 +
  56 +#define ST01_V_RETRACE 0x08
  57 +#define ST01_DISP_ENABLE 0x01
  58 +
  59 +typedef struct VGAState {
  60 + uint8_t *vram_ptr;
  61 + unsigned long vram_offset;
  62 + unsigned int vram_size;
  63 + uint32_t latch;
  64 + uint8_t sr_index;
  65 + uint8_t sr[8];
  66 + uint8_t gr_index;
  67 + uint8_t gr[16];
  68 + uint8_t ar_index;
  69 + uint8_t ar[21];
  70 + int ar_flip_flop;
  71 + uint8_t cr_index;
  72 + uint8_t cr[256]; /* CRT registers */
  73 + uint8_t msr; /* Misc Output Register */
  74 + uint8_t fcr; /* Feature Control Register */
  75 + uint8_t st00; /* status 0 */
  76 + uint8_t st01; /* status 1 */
  77 + uint8_t dac_state;
  78 + uint8_t dac_sub_index;
  79 + uint8_t dac_read_index;
  80 + uint8_t dac_write_index;
  81 + uint8_t dac_cache[3]; /* used when writing */
  82 + uint8_t palette[768];
  83 +
  84 + /* display refresh support */
  85 + /* tell for each page if it has been updated since the last time */
  86 + DisplayState *ds;
  87 + uint32_t font_offsets[2];
  88 + int graphic_mode;
  89 + int shift_control;
  90 + uint32_t line_offset;
  91 + uint32_t line_compare;
  92 + uint32_t start_addr;
  93 + uint8_t last_cw, last_ch;
  94 + uint32_t last_width, last_height;
  95 + uint8_t cursor_start, cursor_end;
  96 + uint32_t cursor_offset;
  97 + uint8_t vram_updated[VGA_RAM_SIZE / 4096];
  98 + uint32_t last_palette[256];
  99 +#define CH_ATTR_SIZE (132 * 60)
  100 + uint32_t last_ch_attr[CH_ATTR_SIZE]; /* XXX: make it dynamic */
  101 +} VGAState;
  102 +
  103 +/* force some bits to zero */
  104 +static const uint8_t sr_mask[8] = {
  105 + (uint8_t)~0xfc,
  106 + (uint8_t)~0xc2,
  107 + (uint8_t)~0xf0,
  108 + (uint8_t)~0xc0,
  109 + (uint8_t)~0xf1,
  110 + (uint8_t)~0xff,
  111 + (uint8_t)~0xff,
  112 + (uint8_t)~0x00,
  113 +};
  114 +
  115 +static const uint8_t gr_mask[16] = {
  116 + (uint8_t)~0xf0, /* 0x00 */
  117 + (uint8_t)~0xf0, /* 0x01 */
  118 + (uint8_t)~0xf0, /* 0x02 */
  119 + (uint8_t)~0xe0, /* 0x03 */
  120 + (uint8_t)~0xfc, /* 0x04 */
  121 + (uint8_t)~0x84, /* 0x05 */
  122 + (uint8_t)~0xf0, /* 0x06 */
  123 + (uint8_t)~0xf0, /* 0x07 */
  124 + (uint8_t)~0x00, /* 0x08 */
  125 + (uint8_t)~0xff, /* 0x09 */
  126 + (uint8_t)~0xff, /* 0x0a */
  127 + (uint8_t)~0xff, /* 0x0b */
  128 + (uint8_t)~0xff, /* 0x0c */
  129 + (uint8_t)~0xff, /* 0x0d */
  130 + (uint8_t)~0xff, /* 0x0e */
  131 + (uint8_t)~0xff, /* 0x0f */
  132 +};
  133 +
  134 +#define cbswap_32(__x) \
  135 +((uint32_t)( \
  136 + (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
  137 + (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
  138 + (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
  139 + (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
  140 +
  141 +#ifdef WORD_BIGENDIAN
  142 +#define PAT(x) cbswap_32(x)
  143 +#else
  144 +#define PAT(x) (x)
  145 +#endif
  146 +
  147 +static const uint32_t mask16[16] = {
  148 + PAT(0x00000000),
  149 + PAT(0x000000ff),
  150 + PAT(0x0000ff00),
  151 + PAT(0x0000ffff),
  152 + PAT(0x00ff0000),
  153 + PAT(0x00ff00ff),
  154 + PAT(0x00ffff00),
  155 + PAT(0x00ffffff),
  156 + PAT(0xff000000),
  157 + PAT(0xff0000ff),
  158 + PAT(0xff00ff00),
  159 + PAT(0xff00ffff),
  160 + PAT(0xffff0000),
  161 + PAT(0xffff00ff),
  162 + PAT(0xffffff00),
  163 + PAT(0xffffffff),
  164 +};
  165 +
  166 +#undef PAT
  167 +
  168 +#ifdef WORD_BIGENDIAN
  169 +#define PAT(x) (x)
  170 +#else
  171 +#define PAT(x) cbswap_32(x)
  172 +#endif
  173 +
  174 +static const uint32_t dmask16[16] = {
  175 + PAT(0x00000000),
  176 + PAT(0x000000ff),
  177 + PAT(0x0000ff00),
  178 + PAT(0x0000ffff),
  179 + PAT(0x00ff0000),
  180 + PAT(0x00ff00ff),
  181 + PAT(0x00ffff00),
  182 + PAT(0x00ffffff),
  183 + PAT(0xff000000),
  184 + PAT(0xff0000ff),
  185 + PAT(0xff00ff00),
  186 + PAT(0xff00ffff),
  187 + PAT(0xffff0000),
  188 + PAT(0xffff00ff),
  189 + PAT(0xffffff00),
  190 + PAT(0xffffffff),
  191 +};
  192 +
  193 +static const uint32_t dmask4[4] = {
  194 + PAT(0x00000000),
  195 + PAT(0x0000ffff),
  196 + PAT(0xffff0000),
  197 + PAT(0xffffffff),
  198 +};
  199 +
  200 +static uint32_t expand4[256];
  201 +static uint16_t expand2[256];
  202 +
  203 +VGAState vga_state;
  204 +int vga_io_memory;
  205 +
  206 +static uint32_t vga_ioport_read(CPUX86State *env, uint32_t addr)
  207 +{
  208 + VGAState *s = &vga_state;
  209 + int val, index;
  210 +
  211 + /* check port range access depending on color/monochrome mode */
  212 + if ((addr >= 0x3b0 && addr <= 0x3bf && (s->msr & MSR_COLOR_EMULATION)) ||
  213 + (addr >= 0x3d0 && addr <= 0x3df && !(s->msr & MSR_COLOR_EMULATION))) {
  214 + val = 0xff;
  215 + } else {
  216 + switch(addr) {
  217 + case 0x3c0:
  218 + if (s->ar_flip_flop == 0) {
  219 + val = s->ar_index;
  220 + } else {
  221 + val = 0;
  222 + }
  223 + break;
  224 + case 0x3c1:
  225 + index = s->ar_index & 0x1f;
  226 + if (index < 21)
  227 + val = s->ar[index];
  228 + else
  229 + val = 0;
  230 + break;
  231 + case 0x3c2:
  232 + val = s->st00;
  233 + break;
  234 + case 0x3c4:
  235 + val = s->sr_index;
  236 + break;
  237 + case 0x3c5:
  238 + val = s->sr[s->sr_index];
  239 + break;
  240 + case 0x3c7:
  241 + val = s->dac_state;
  242 + break;
  243 + case 0x3c9:
  244 + val = s->palette[s->dac_read_index * 3 + s->dac_sub_index];
  245 + if (++s->dac_sub_index == 3) {
  246 + s->dac_sub_index = 0;
  247 + s->dac_read_index++;
  248 + }
  249 + break;
  250 + case 0x3ca:
  251 + val = s->fcr;
  252 + break;
  253 + case 0x3cc:
  254 + val = s->msr;
  255 + break;
  256 + case 0x3ce:
  257 + val = s->gr_index;
  258 + break;
  259 + case 0x3cf:
  260 + val = s->gr[s->gr_index];
  261 + break;
  262 + case 0x3b4:
  263 + case 0x3d4:
  264 + val = s->cr_index;
  265 + break;
  266 + case 0x3b5:
  267 + case 0x3d5:
  268 + val = s->cr[s->cr_index];
  269 + break;
  270 + case 0x3ba:
  271 + case 0x3da:
  272 + /* just toggle to fool polling */
  273 + s->st01 ^= ST01_V_RETRACE | ST01_DISP_ENABLE;
  274 + val = s->st01;
  275 + s->ar_flip_flop = 0;
  276 + break;
  277 + default:
  278 + val = 0x00;
  279 + break;
  280 + }
  281 + }
  282 +#ifdef DEBUG_VGA
  283 + printf("VGA: read addr=0x%04x data=0x%02x\n", addr, val);
  284 +#endif
  285 + return val;
  286 +}
  287 +
  288 +static void vga_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
  289 +{
  290 + VGAState *s = &vga_state;
  291 + int index, v;
  292 +
  293 + /* check port range access depending on color/monochrome mode */
  294 + if ((addr >= 0x3b0 && addr <= 0x3bf && (s->msr & MSR_COLOR_EMULATION)) ||
  295 + (addr >= 0x3d0 && addr <= 0x3df && !(s->msr & MSR_COLOR_EMULATION)))
  296 + return;
  297 +
  298 +#ifdef DEBUG_VGA
  299 + printf("VGA: write addr=0x%04x data=0x%02x\n", addr, val);
  300 +#endif
  301 +
  302 + switch(addr) {
  303 + case 0x3c0:
  304 + if (s->ar_flip_flop == 0) {
  305 + val &= 0x3f;
  306 + s->ar_index = val;
  307 + } else {
  308 + index = s->ar_index & 0x1f;
  309 + switch(index) {
  310 + case 0x00 ... 0x0f:
  311 + s->ar[index] = val & 0x3f;
  312 + break;
  313 + case 0x10:
  314 + s->ar[index] = val & ~0x10;
  315 + break;
  316 + case 0x11:
  317 + s->ar[index] = val;
  318 + break;
  319 + case 0x12:
  320 + s->ar[index] = val & ~0xc0;
  321 + break;
  322 + case 0x13:
  323 + s->ar[index] = val & ~0xf0;
  324 + break;
  325 + case 0x14:
  326 + s->ar[index] = val & ~0xf0;
  327 + break;
  328 + default:
  329 + break;
  330 + }
  331 + }
  332 + s->ar_flip_flop ^= 1;
  333 + break;
  334 + case 0x3c2:
  335 + s->msr = val & ~0x10;
  336 + break;
  337 + case 0x3c4:
  338 + s->sr_index = val & 7;
  339 + break;
  340 + case 0x3c5:
  341 + s->sr[s->sr_index] = val & sr_mask[s->sr_index];
  342 + break;
  343 + case 0x3c7:
  344 + s->dac_read_index = val;
  345 + s->dac_sub_index = 0;
  346 + s->dac_state = 3;
  347 + break;
  348 + case 0x3c8:
  349 + s->dac_write_index = val;
  350 + s->dac_sub_index = 0;
  351 + s->dac_state = 0;
  352 + break;
  353 + case 0x3c9:
  354 + s->dac_cache[s->dac_sub_index] = val;
  355 + if (++s->dac_sub_index == 3) {
  356 + memcpy(&s->palette[s->dac_write_index * 3], s->dac_cache, 3);
  357 + s->dac_sub_index = 0;
  358 + s->dac_write_index++;
  359 + }
  360 + break;
  361 + case 0x3ce:
  362 + s->gr_index = val & 0x0f;
  363 + break;
  364 + case 0x3cf:
  365 + s->gr[s->gr_index] = val & gr_mask[s->gr_index];
  366 + break;
  367 + case 0x3b4:
  368 + case 0x3d4:
  369 + s->cr_index = val;
  370 + break;
  371 + case 0x3b5:
  372 + case 0x3d5:
  373 + /* handle CR0-7 protection */
  374 + if ((s->cr[11] & 0x80) && s->cr_index <= 7) {
  375 + /* can always write bit 4 of CR7 */
  376 + if (s->cr_index == 7)
  377 + s->cr[7] = (s->cr[7] & ~0x10) | (val & 0x10);
  378 + return;
  379 + }
  380 + switch(s->cr_index) {
  381 + case 0x01: /* horizontal display end */
  382 + case 0x07:
  383 + case 0x09:
  384 + case 0x0c:
  385 + case 0x0d:
  386 + case 0x12: /* veritcal display end */
  387 + s->cr[s->cr_index] = val;
  388 + break;
  389 +
  390 + /* S3 registers */
  391 + case 0x2d:
  392 + case 0x2e:
  393 + case 0x2f:
  394 + case 0x30:
  395 + /* chip ID, cannot write */
  396 + break;
  397 + case 0x31:
  398 + /* update start address */
  399 + s->cr[s->cr_index] = val;
  400 + v = (val >> 4) & 3;
  401 + s->cr[0x69] = (s->cr[69] & ~0x03) | v;
  402 + break;
  403 + case 0x51:
  404 + /* update start address */
  405 + s->cr[s->cr_index] = val;
  406 + v = val & 3;
  407 + s->cr[0x69] = (s->cr[69] & ~0x0c) | (v << 2);
  408 + break;
  409 + default:
  410 + s->cr[s->cr_index] = val;
  411 + break;
  412 + }
  413 + break;
  414 + case 0x3ba:
  415 + case 0x3da:
  416 + s->fcr = val & 0x10;
  417 + break;
  418 + }
  419 +}
  420 +
  421 +/* called for accesses between 0xa0000 and 0xc0000 */
  422 +static uint32_t vga_mem_readb(uint32_t addr)
  423 +{
  424 + VGAState *s = &vga_state;
  425 + int memory_map_mode, plane;
  426 + uint32_t ret;
  427 +
  428 + /* convert to VGA memory offset */
  429 + memory_map_mode = (s->gr[6] >> 2) & 3;
  430 + switch(memory_map_mode) {
  431 + case 0:
  432 + addr -= 0xa0000;
  433 + break;
  434 + case 1:
  435 + addr -= 0xa0000;
  436 + if (addr >= 0x10000)
  437 + return 0xff;
  438 + break;
  439 + case 2:
  440 + addr -= 0xb0000;
  441 + if (addr >= 0x8000)
  442 + return 0xff;
  443 + break;
  444 + default:
  445 + case 3:
  446 + addr -= 0xb8000;
  447 + break;
  448 + }
  449 +
  450 + if (s->sr[4] & 0x08) {
  451 + /* chain 4 mode : simplest access */
  452 + ret = s->vram_ptr[addr];
  453 + } else if (s->gr[5] & 0x10) {
  454 + /* odd/even mode (aka text mode mapping) */
  455 + plane = (s->gr[4] & 2) | (addr & 1);
  456 + ret = s->vram_ptr[((addr & ~1) << 1) | plane];
  457 + } else {
  458 + /* standard VGA latched access */
  459 + s->latch = ((uint32_t *)s->vram_ptr)[addr];
  460 +
  461 + if (!(s->gr[5] & 0x08)) {
  462 + /* read mode 0 */
  463 + plane = s->gr[4];
  464 +#ifdef WORD_BIGENDIAN
  465 + ret = (s->latch >> (24 - (plane * 8))) & 0xff;
  466 +#else
  467 + ret = (s->latch >> (plane * 8)) & 0xff;
  468 +#endif
  469 + } else {
  470 + /* read mode 1 */
  471 + ret = (s->latch ^ mask16[s->gr[2]]) & mask16[s->gr[7]];
  472 + ret |= ret >> 16;
  473 + ret |= ret >> 8;
  474 + ret = (~ret) & 0xff;
  475 + }
  476 + }
  477 + return ret;
  478 +}
  479 +
  480 +static uint32_t vga_mem_readw(uint32_t addr)
  481 +{
  482 + uint32_t v;
  483 + v = vga_mem_readb(addr);
  484 + v |= vga_mem_readb(addr + 1) << 8;
  485 + return v;
  486 +}
  487 +
  488 +static uint32_t vga_mem_readl(uint32_t addr)
  489 +{
  490 + uint32_t v;
  491 + v = vga_mem_readb(addr);
  492 + v |= vga_mem_readb(addr + 1) << 8;
  493 + v |= vga_mem_readb(addr + 2) << 16;
  494 + v |= vga_mem_readb(addr + 3) << 24;
  495 + return v;
  496 +}
  497 +
  498 +
  499 +/* called for accesses between 0xa0000 and 0xc0000 */
  500 +void vga_mem_writeb(uint32_t addr, uint32_t val)
  501 +{
  502 + VGAState *s = &vga_state;
  503 + int memory_map_mode, plane, write_mode, b, func_select;
  504 + uint32_t write_mask, bit_mask, set_mask;
  505 +
  506 +#ifdef DEBUG_VGA
  507 + printf("vga: [0x%x] = 0x%02x\n", addr, val);
  508 +#endif
  509 + /* convert to VGA memory offset */
  510 + memory_map_mode = (s->gr[6] >> 2) & 3;
  511 + switch(memory_map_mode) {
  512 + case 0:
  513 + addr -= 0xa0000;
  514 + break;
  515 + case 1:
  516 + addr -= 0xa0000;
  517 + if (addr >= 0x10000)
  518 + return;
  519 + break;
  520 + case 2:
  521 + addr -= 0xb0000;
  522 + if (addr >= 0x8000)
  523 + return;
  524 + break;
  525 + default:
  526 + case 3:
  527 + addr -= 0xb8000;
  528 + break;
  529 + }
  530 +
  531 + if (s->sr[4] & 0x08) {
  532 + /* chain 4 mode : simplest access */
  533 + plane = addr & 3;
  534 + if (s->sr[2] & (1 << plane)) {
  535 + s->vram_ptr[addr] = val;
  536 +#ifdef DEBUG_VGA
  537 + printf("vga: chain4: [0x%x]\n", addr);
  538 +#endif
  539 + s->vram_updated[addr >> 12] = 1;
  540 + }
  541 + } else if (s->gr[5] & 0x10) {
  542 + /* odd/even mode (aka text mode mapping) */
  543 + plane = (s->gr[4] & 2) | (addr & 1);
  544 + if (s->sr[2] & (1 << plane)) {
  545 + addr = ((addr & ~1) << 1) | plane;
  546 + s->vram_ptr[addr] = val;
  547 +#ifdef DEBUG_VGA
  548 + printf("vga: odd/even: [0x%x]\n", addr);
  549 +#endif
  550 + s->vram_updated[addr >> 12] = 1;
  551 + }
  552 + } else {
  553 + /* standard VGA latched access */
  554 + write_mode = s->gr[5] & 3;
  555 + switch(write_mode) {
  556 + default:
  557 + case 0:
  558 + /* rotate */
  559 + b = s->gr[3] & 7;
  560 + val = ((val >> b) | (val << (8 - b))) & 0xff;
  561 + val |= val << 8;
  562 + val |= val << 16;
  563 +
  564 + /* apply set/reset mask */
  565 + set_mask = mask16[s->gr[1]];
  566 + val = (val & ~set_mask) | (mask16[s->gr[0]] & set_mask);
  567 + bit_mask = s->gr[8];
  568 + break;
  569 + case 1:
  570 + val = s->latch;
  571 + goto do_write;
  572 + case 2:
  573 + val = mask16[val & 0x0f];
  574 + bit_mask = s->gr[8];
  575 + break;
  576 + case 3:
  577 + /* rotate */
  578 + b = s->gr[3] & 7;
  579 + val = ((val >> b) | (val << (8 - b)));
  580 +
  581 + bit_mask = s->gr[8] & val;
  582 + val = mask16[s->gr[0]];
  583 + break;
  584 + }
  585 +
  586 + /* apply logical operation */
  587 + func_select = s->gr[3] >> 3;
  588 + switch(func_select) {
  589 + case 0:
  590 + default:
  591 + /* nothing to do */
  592 + break;
  593 + case 1:
  594 + /* and */
  595 + val &= s->latch;
  596 + break;
  597 + case 2:
  598 + /* or */
  599 + val |= s->latch;
  600 + break;
  601 + case 3:
  602 + /* xor */
  603 + val ^= s->latch;
  604 + break;
  605 + }
  606 +
  607 + /* apply bit mask */
  608 + bit_mask |= bit_mask << 8;
  609 + bit_mask |= bit_mask << 16;
  610 + val = (val & bit_mask) | (s->latch & ~bit_mask);
  611 +
  612 + do_write:
  613 + /* mask data according to sr[2] */
  614 + write_mask = mask16[s->sr[2]];
  615 + ((uint32_t *)s->vram_ptr)[addr] =
  616 + (((uint32_t *)s->vram_ptr)[addr] & ~write_mask) |
  617 + (val & write_mask);
  618 +#ifdef DEBUG_VGA
  619 + printf("vga: latch: [0x%x] mask=0x%08x val=0x%08x\n",
  620 + addr * 4, write_mask, val);
  621 +#endif
  622 + s->vram_updated[addr >> 10] = 1;
  623 + }
  624 +}
  625 +
  626 +void vga_mem_writew(uint32_t addr, uint32_t val)
  627 +{
  628 + vga_mem_writeb(addr, val & 0xff);
  629 + vga_mem_writeb(addr + 1, (val >> 8) & 0xff);
  630 +}
  631 +
  632 +void vga_mem_writel(uint32_t addr, uint32_t val)
  633 +{
  634 + vga_mem_writeb(addr, val & 0xff);
  635 + vga_mem_writeb(addr + 1, (val >> 8) & 0xff);
  636 + vga_mem_writeb(addr + 2, (val >> 16) & 0xff);
  637 + vga_mem_writeb(addr + 3, (val >> 24) & 0xff);
  638 +}
  639 +
  640 +#ifdef WORD_BIGENDIAN
  641 +#define BIG 1
  642 +#else
  643 +#define BIG 0
  644 +#endif
  645 +
  646 +#ifdef WORDS_BIGENDIAN
  647 +#define GET_PLANE(data, p) (((data) >> (24 - (p) * 8)) & 0xff)
  648 +#else
  649 +#define GET_PLANE(data, p) (((data) >> ((p) * 8)) & 0xff)
  650 +#endif
  651 +
  652 +typedef void vga_draw_glyph8_func(uint8_t *d, int linesize,
  653 + const uint8_t *font_ptr, int h,
  654 + uint32_t fgcol, uint32_t bgcol);
  655 +typedef void vga_draw_glyph9_func(uint8_t *d, int linesize,
  656 + const uint8_t *font_ptr, int h,
  657 + uint32_t fgcol, uint32_t bgcol, int dup9);
  658 +typedef void vga_draw_line_func(VGAState *s1, uint8_t *d,
  659 + const uint8_t *s, int width);
  660 +
  661 +static inline unsigned int rgb_to_pixel8(unsigned int r, unsigned int g, unsigned b)
  662 +{
  663 + /* XXX: TODO */
  664 + return 0;
  665 +}
  666 +
  667 +static inline unsigned int rgb_to_pixel15(unsigned int r, unsigned int g, unsigned b)
  668 +{
  669 + return ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);
  670 +}
  671 +
  672 +static inline unsigned int rgb_to_pixel16(unsigned int r, unsigned int g, unsigned b)
  673 +{
  674 + return ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  675 +}
  676 +
  677 +static inline unsigned int rgb_to_pixel32(unsigned int r, unsigned int g, unsigned b)
  678 +{
  679 + return (r << 16) | (g << 8) | b;
  680 +}
  681 +
  682 +#define DEPTH 8
  683 +#include "vga_template.h"
  684 +
  685 +#define DEPTH 15
  686 +#include "vga_template.h"
  687 +
  688 +#define DEPTH 16
  689 +#include "vga_template.h"
  690 +
  691 +#define DEPTH 32
  692 +#include "vga_template.h"
  693 +
  694 +static inline int c6_to_8(int v)
  695 +{
  696 + int b;
  697 + v &= 0x3f;
  698 + b = v & 1;
  699 + return (v << 2) | (b << 1) | b;
  700 +}
  701 +
  702 +/* return true if the palette was modified */
  703 +static int update_palette16(VGAState *s)
  704 +{
  705 + int full_update, i, depth;
  706 + uint32_t v, col, *palette;
  707 + unsigned int (*rgb_to_pixel)(unsigned int r, unsigned int g, unsigned b);
  708 + depth = s->ds->depth;
  709 + switch(depth) {
  710 + case 8:
  711 + rgb_to_pixel = rgb_to_pixel8;
  712 + break;
  713 + case 15:
  714 + rgb_to_pixel = rgb_to_pixel15;
  715 + break;
  716 + default:
  717 + case 16:
  718 + rgb_to_pixel = rgb_to_pixel16;
  719 + break;
  720 + case 32:
  721 + rgb_to_pixel = rgb_to_pixel32;
  722 + break;
  723 + }
  724 +
  725 + full_update = 0;
  726 + palette = s->last_palette;
  727 + for(i = 0; i < 16; i++) {
  728 + v = s->ar[i];
  729 + if (s->ar[0x10] & 0x80)
  730 + v = ((s->ar[0x14] & 0xf) << 4) | (v & 0xf);
  731 + else
  732 + v = ((s->ar[0x14] & 0xc) << 4) | (v & 0x3f);
  733 + v = v * 3;
  734 + col = rgb_to_pixel(c6_to_8(s->palette[v]),
  735 + c6_to_8(s->palette[v + 1]),
  736 + c6_to_8(s->palette[v + 2]));
  737 +
  738 + if (depth == 8) {
  739 + col |= col << 8;
  740 + col |= col << 16;
  741 + } else if (depth <= 16) {
  742 + col |= col << 16;
  743 + }
  744 + // printf("%2d: %08x\n", i, col);
  745 + if (col != palette[i]) {
  746 + full_update = 1;
  747 + palette[i] = col;
  748 + }
  749 + }
  750 + return full_update;
  751 +}
  752 +
  753 +/* update start_addr and line_offset. Return TRUE if modified */
  754 +static int update_basic_params(VGAState *s)
  755 +{
  756 + int full_update;
  757 + uint32_t start_addr, line_offset, line_compare, v;
  758 +
  759 + full_update = 0;
  760 + /* compute line_offset in bytes */
  761 + v = (s->cr[0x51] >> 4) & 3; /* S3 extension */
  762 + if (v == 0)
  763 + v = (s->cr[0x43] >> 2) & 1; /* S3 extension */
  764 + line_offset = s->cr[0x13] | (v << 8);
  765 + line_offset <<= 3;
  766 +#if 0
  767 + /* XXX: check this - inconsistent with some VGA docs */
  768 + if (s->cr[0x14] & 0x40)
  769 + line_offset <<= 2;
  770 + else if (!(s->cr[0x17] & 0x40))
  771 + line_offset <<= 1;
  772 +#endif
  773 + /* starting address */
  774 + start_addr = s->cr[0x0d] | (s->cr[0x0c] << 8);
  775 + start_addr |= (s->cr[0x69] & 0x1f) << 16; /* S3 extension */
  776 +
  777 + /* line compare */
  778 + line_compare = s->cr[0x18] |
  779 + ((s->cr[0x07] & 0x10) << 4) |
  780 + ((s->cr[0x09] & 0x40) << 3);
  781 +
  782 + if (line_offset != s->line_offset ||
  783 + start_addr != s->start_addr ||
  784 + line_compare != s->line_compare) {
  785 + s->line_offset = line_offset;
  786 + s->start_addr = start_addr;
  787 + s->line_compare = line_compare;
  788 + full_update = 1;
  789 + }
  790 + return full_update;
  791 +}
  792 +
  793 +static inline int get_depth_index(int depth)
  794 +{
  795 + switch(depth) {
  796 + default:
  797 + case 8:
  798 + return 0;
  799 + case 15:
  800 + return 1;
  801 + case 16:
  802 + return 2;
  803 + case 32:
  804 + return 3;
  805 + }
  806 +}
  807 +
  808 +static vga_draw_glyph8_func *vga_draw_glyph8_table[4] = {
  809 + vga_draw_glyph8_8,
  810 + vga_draw_glyph8_16,
  811 + vga_draw_glyph8_16,
  812 + vga_draw_glyph8_32,
  813 +};
  814 +
  815 +static vga_draw_glyph9_func *vga_draw_glyph9_table[4] = {
  816 + vga_draw_glyph9_8,
  817 + vga_draw_glyph9_16,
  818 + vga_draw_glyph9_16,
  819 + vga_draw_glyph9_32,
  820 +};
  821 +
  822 +static const uint8_t cursor_glyph[32 * 4] = {
  823 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  824 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  825 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  826 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  827 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  828 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  829 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  830 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  831 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  832 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  833 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  834 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  835 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  836 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  837 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  838 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  839 +};
  840 +
  841 +/*
  842 + * Text mode update
  843 + * Missing:
  844 + * - double scan
  845 + * - double width
  846 + * - underline
  847 + * - flashing
  848 + */
  849 +static void vga_draw_text(VGAState *s, int full_update)
  850 +{
  851 + int cx, cy, cheight, cw, ch, cattr, height, width, ch_attr;
  852 + int cx_min, cx_max, linesize, x_incr;
  853 + uint32_t offset, fgcol, bgcol, v, cursor_offset;
  854 + uint8_t *d1, *d, *src, *s1, *dest, *cursor_ptr;
  855 + const uint8_t *font_ptr, *font_base[2];
  856 + int dup9, line_offset, depth_index;
  857 + uint32_t *palette;
  858 + uint32_t *ch_attr_ptr;
  859 + vga_draw_glyph8_func *vga_draw_glyph8;
  860 + vga_draw_glyph9_func *vga_draw_glyph9;
  861 +
  862 + full_update |= update_palette16(s);
  863 + palette = s->last_palette;
  864 +
  865 + /* compute font data address (in plane 2) */
  866 + v = s->sr[3];
  867 + offset = (((v >> 5) & 1) | ((v >> 1) & 6)) * 8192 * 4 + 2;
  868 + if (offset != s->font_offsets[0]) {
  869 + s->font_offsets[0] = offset;
  870 + full_update = 1;
  871 + }
  872 + font_base[0] = s->vram_ptr + offset;
  873 +
  874 + offset = (((v >> 4) & 1) | ((v << 1) & 6)) * 8192 * 4 + 2;
  875 + font_base[1] = s->vram_ptr + offset;
  876 + if (offset != s->font_offsets[1]) {
  877 + s->font_offsets[1] = offset;
  878 + full_update = 1;
  879 + }
  880 +
  881 + full_update |= update_basic_params(s);
  882 +
  883 + line_offset = s->line_offset;
  884 + s1 = s->vram_ptr + (s->start_addr * 4);
  885 +
  886 + /* total width & height */
  887 + cheight = (s->cr[9] & 0x1f) + 1;
  888 + cw = 8;
  889 + if (s->sr[1] & 0x01)
  890 + cw = 9;
  891 + x_incr = cw * ((s->ds->depth + 7) >> 3);
  892 + width = (s->cr[0x01] + 1);
  893 + height = s->cr[0x12] |
  894 + ((s->cr[0x07] & 0x02) << 7) |
  895 + ((s->cr[0x07] & 0x40) << 3);
  896 + height = (height + 1) / cheight;
  897 + if (width != s->last_width || height != s->last_height ||
  898 + cw != s->last_cw || cw != s->last_cw) {
  899 + dpy_resize(s->ds, width * cw, height * cheight);
  900 + s->last_width = width;
  901 + s->last_height = height;
  902 + s->last_ch = cheight;
  903 + s->last_cw = cw;
  904 + full_update = 1;
  905 + }
  906 + cursor_offset = ((s->cr[0x0e] << 8) | s->cr[0x0f]) - s->start_addr;
  907 + if (cursor_offset != s->cursor_offset ||
  908 + s->cr[0xa] != s->cursor_start ||
  909 + s->cr[0xb] != s->cursor_end) {
  910 + /* if the cursor position changed, we update the old and new
  911 + chars */
  912 + if (s->cursor_offset < CH_ATTR_SIZE)
  913 + s->last_ch_attr[s->cursor_offset] = -1;
  914 + if (cursor_offset < CH_ATTR_SIZE)
  915 + s->last_ch_attr[cursor_offset] = -1;
  916 + s->cursor_offset = cursor_offset;
  917 + s->cursor_start = s->cr[0xa];
  918 + s->cursor_end = s->cr[0xb];
  919 + }
  920 + cursor_ptr = s->vram_ptr + cursor_offset * 4;
  921 +
  922 + depth_index = get_depth_index(s->ds->depth);
  923 + vga_draw_glyph8 = vga_draw_glyph8_table[depth_index];
  924 + vga_draw_glyph9 = vga_draw_glyph9_table[depth_index];
  925 +
  926 + dest = s->ds->data;
  927 + linesize = s->ds->linesize;
  928 + ch_attr_ptr = s->last_ch_attr;
  929 + for(cy = 0; cy < height; cy++) {
  930 + d1 = dest;
  931 + src = s1;
  932 + cx_min = width;
  933 + cx_max = -1;
  934 + for(cx = 0; cx < width; cx++) {
  935 + ch_attr = *(uint16_t *)src;
  936 + if (full_update || ch_attr != *ch_attr_ptr) {
  937 + if (cx < cx_min)
  938 + cx_min = cx;
  939 + if (cx > cx_max)
  940 + cx_max = cx;
  941 + *ch_attr_ptr = ch_attr;
  942 +#ifdef WORDS_BIGENDIAN
  943 + ch = ch_attr >> 8;
  944 + cattr = ch_attr & 0xff;
  945 +#else
  946 + ch = ch_attr & 0xff;
  947 + cattr = ch_attr >> 8;
  948 +#endif
  949 + font_ptr = font_base[(cattr >> 3) & 1];
  950 + font_ptr += 32 * 4 * ch;
  951 + bgcol = palette[cattr >> 4];
  952 + fgcol = palette[cattr & 0x0f];
  953 + if (cw == 8) {
  954 + vga_draw_glyph8(d1, linesize,
  955 + font_ptr, cheight, fgcol, bgcol);
  956 + } else {
  957 + dup9 = 0;
  958 + if (ch >= 0xb0 && ch <= 0xdf && (s->ar[0x10] & 0x04))
  959 + dup9 = 1;
  960 + vga_draw_glyph9(d1, linesize,
  961 + font_ptr, cheight, fgcol, bgcol, dup9);
  962 + }
  963 + if (src == cursor_ptr &&
  964 + !(s->cr[0x0a] & 0x20)) {
  965 + int line_start, line_last, h;
  966 + /* draw the cursor */
  967 + line_start = s->cr[0x0a] & 0x1f;
  968 + line_last = s->cr[0x0b] & 0x1f;
  969 + /* XXX: check that */
  970 + if (line_last > cheight - 1)
  971 + line_last = cheight - 1;
  972 + if (line_last >= line_start && line_start < cheight) {
  973 + h = line_last - line_start + 1;
  974 + d = d1 + linesize * line_start;
  975 + if (cw == 8) {
  976 + vga_draw_glyph8(d, linesize,
  977 + cursor_glyph, h, fgcol, bgcol);
  978 + } else {
  979 + vga_draw_glyph9(d, linesize,
  980 + cursor_glyph, h, fgcol, bgcol, 1);
  981 + }
  982 + }
  983 + }
  984 + }
  985 + d1 += x_incr;
  986 + src += 4;
  987 + ch_attr_ptr++;
  988 + }
  989 + if (cx_max != -1) {
  990 + dpy_update(s->ds, cx_min * cw, cy * cheight,
  991 + (cx_max - cx_min + 1) * cw, cheight);
  992 + }
  993 + dest += linesize * cheight;
  994 + s1 += line_offset;
  995 + }
  996 +}
  997 +
  998 +static vga_draw_line_func *vga_draw_line_table[4 * 6] = {
  999 + vga_draw_line2_8,
  1000 + vga_draw_line2_16,
  1001 + vga_draw_line2_16,
  1002 + vga_draw_line2_32,
  1003 +
  1004 + vga_draw_line4_8,
  1005 + vga_draw_line4_16,
  1006 + vga_draw_line4_16,
  1007 + vga_draw_line4_32,
  1008 +
  1009 + vga_draw_line8_8,
  1010 + vga_draw_line8_16,
  1011 + vga_draw_line8_16,
  1012 + vga_draw_line8_32,
  1013 +
  1014 + vga_draw_line15_8,
  1015 + vga_draw_line15_15,
  1016 + vga_draw_line15_16,
  1017 + vga_draw_line15_32,
  1018 +
  1019 + vga_draw_line16_8,
  1020 + vga_draw_line16_15,
  1021 + vga_draw_line16_16,
  1022 + vga_draw_line16_32,
  1023 +
  1024 + vga_draw_line32_8,
  1025 + vga_draw_line32_15,
  1026 + vga_draw_line32_16,
  1027 + vga_draw_line32_32,
  1028 +};
  1029 +
  1030 +/*
  1031 + * graphic modes
  1032 + * Missing:
  1033 + * - double scan
  1034 + * - double width
  1035 + */
  1036 +static void vga_draw_graphic(VGAState *s, int full_update)
  1037 +{
  1038 + int y, update, y_min, y_max, page_min, page_max, linesize;
  1039 + int width, height, shift_control, line_offset, page0, page1;
  1040 + uint8_t *d;
  1041 + uint32_t v, *palette, addr1, addr;
  1042 + vga_draw_line_func *vga_draw_line;
  1043 +
  1044 + full_update |= update_palette16(s);
  1045 + palette = s->last_palette;
  1046 +
  1047 + full_update |= update_basic_params(s);
  1048 +
  1049 + width = (s->cr[0x01] + 1);
  1050 + height = s->cr[0x12] |
  1051 + ((s->cr[0x07] & 0x02) << 7) |
  1052 + ((s->cr[0x07] & 0x40) << 3);
  1053 + height = (height + 1);
  1054 +
  1055 + if (width != s->last_width ||
  1056 + height != s->last_height) {
  1057 + s->last_width = width;
  1058 + s->last_height = height;
  1059 + full_update = 1;
  1060 + }
  1061 +
  1062 + shift_control = (s->gr[0x05] >> 5) & 3;
  1063 + if (shift_control != s->shift_control) {
  1064 + full_update = 1;
  1065 + s->shift_control = shift_control;
  1066 + }
  1067 +
  1068 + if (shift_control == 0)
  1069 + v = 1; /* 4 bit/pxeil */
  1070 + else if (shift_control == 1)
  1071 + v = 0; /* 2 bit/pixel */
  1072 + else
  1073 + v = 2; /* 8 bit/pixel */
  1074 +
  1075 + vga_draw_line = vga_draw_line_table[v * 4 + get_depth_index(s->ds->depth)];
  1076 +
  1077 + line_offset = s->line_offset;
  1078 + addr1 = (s->start_addr * 4);
  1079 + y_min = height;
  1080 + y_max = -1;
  1081 + page_min = 0x7fffffff;
  1082 + page_max = -1;
  1083 + d = s->ds->data;
  1084 + linesize = s->ds->linesize;
  1085 + for(y = 0; y < height; y++) {
  1086 + addr = addr1;
  1087 + if (s->cr[0x17] & 1) {
  1088 + /* CGA compatibility handling */
  1089 + addr = (addr & ~0x2000) | ((y & 1) << 13);
  1090 + }
  1091 + if (s->cr[0x17] & 2) {
  1092 + addr = (addr & ~0x4000) | ((y & 2) << 13);
  1093 + }
  1094 + page0 = addr >> 12;
  1095 + page1 = (addr + width - 1) >> 12;
  1096 + update = full_update | s->vram_updated[page0] | s->vram_updated[page1];
  1097 + if (update) {
  1098 + if (y < y_min)
  1099 + y_min = y;
  1100 + if (y > y_max)
  1101 + y_max = y;
  1102 + if (page0 < page_min)
  1103 + page_min = page0;
  1104 + if (page1 > page_max)
  1105 + page_max = page1;
  1106 + vga_draw_line(s, d, s->vram_ptr + addr, width);
  1107 + }
  1108 + if (y == s->line_compare) {
  1109 + addr1 = 0;
  1110 + } else {
  1111 + addr1 += line_offset;
  1112 + }
  1113 + d += linesize;
  1114 + }
  1115 +
  1116 + /* reset modified pages */
  1117 + if (page_max != -1) {
  1118 + memset(s->vram_updated + page_min, 0, page_max - page_min + 1);
  1119 + }
  1120 +}
  1121 +
  1122 +/* draw text terminal (very limited, just for simple boot debug
  1123 + messages) */
  1124 +static int last_cursor_pos;
  1125 +
  1126 +void vga_draw_dumb(VGAState *s)
  1127 +{
  1128 + int c, i, cursor_pos, eol;
  1129 +
  1130 + cursor_pos = s->cr[0x0f] | (s->cr[0x0e] << 8);
  1131 + eol = 0;
  1132 + for(i = last_cursor_pos; i < cursor_pos; i++) {
  1133 + /* XXX: should use vga RAM */
  1134 + c = phys_ram_base[0xb8000 + (i) * 2];
  1135 + if (c >= ' ') {
  1136 + putchar(c);
  1137 + eol = 0;
  1138 + } else {
  1139 + if (!eol)
  1140 + putchar('\n');
  1141 + eol = 1;
  1142 + }
  1143 + }
  1144 + fflush(stdout);
  1145 + last_cursor_pos = cursor_pos;
  1146 +}
  1147 +
  1148 +void vga_update_display(void)
  1149 +{
  1150 + VGAState *s = &vga_state;
  1151 + int full_update, graphic_mode;
  1152 +
  1153 + if (s->ds->depth == 0) {
  1154 + vga_draw_dumb(s);
  1155 + } else {
  1156 + full_update = 0;
  1157 + graphic_mode = s->gr[6] & 1;
  1158 + if (graphic_mode != s->graphic_mode) {
  1159 + s->graphic_mode = graphic_mode;
  1160 + full_update = 1;
  1161 + }
  1162 + if (graphic_mode)
  1163 + vga_draw_graphic(s, full_update);
  1164 + else
  1165 + vga_draw_text(s, full_update);
  1166 + }
  1167 +}
  1168 +
  1169 +void vga_reset(VGAState *s)
  1170 +{
  1171 + memset(s, 0, sizeof(VGAState));
  1172 + /* chip ID for 8c968 */
  1173 + s->cr[0x2d] = 0x88;
  1174 + s->cr[0x2e] = 0xb0;
  1175 + s->cr[0x2f] = 0x01; /* XXX: check revision code */
  1176 + s->cr[0x30] = 0xe1;
  1177 + s->graphic_mode = -1; /* force full update */
  1178 +}
  1179 +
  1180 +CPUReadMemoryFunc *vga_mem_read[3] = {
  1181 + vga_mem_readb,
  1182 + vga_mem_readw,
  1183 + vga_mem_readl,
  1184 +};
  1185 +
  1186 +CPUWriteMemoryFunc *vga_mem_write[3] = {
  1187 + vga_mem_writeb,
  1188 + vga_mem_writew,
  1189 + vga_mem_writel,
  1190 +};
  1191 +
  1192 +int vga_init(DisplayState *ds, uint8_t *vga_ram_base,
  1193 + unsigned long vga_ram_offset, int vga_ram_size)
  1194 +{
  1195 + VGAState *s = &vga_state;
  1196 + int i, j, v;
  1197 +
  1198 + for(i = 0;i < 256; i++) {
  1199 + v = 0;
  1200 + for(j = 0; j < 8; j++) {
  1201 + v |= ((i >> j) & 1) << (j * 4);
  1202 + }
  1203 + expand4[i] = v;
  1204 +
  1205 + v = 0;
  1206 + for(j = 0; j < 4; j++) {
  1207 + v |= ((i >> (2 * j)) & 3) << (j * 4);
  1208 + }
  1209 + expand2[i] = v;
  1210 + }
  1211 +
  1212 + vga_reset(s);
  1213 +
  1214 + s->vram_ptr = vga_ram_base;
  1215 + s->vram_offset = vga_ram_offset;
  1216 + s->vram_size = vga_ram_size;
  1217 + s->ds = ds;
  1218 +
  1219 + register_ioport_write(0x3c0, 16, vga_ioport_write, 1);
  1220 +
  1221 + register_ioport_write(0x3b4, 2, vga_ioport_write, 1);
  1222 + register_ioport_write(0x3d4, 2, vga_ioport_write, 1);
  1223 + register_ioport_write(0x3ba, 1, vga_ioport_write, 1);
  1224 + register_ioport_write(0x3da, 1, vga_ioport_write, 1);
  1225 +
  1226 + register_ioport_read(0x3c0, 16, vga_ioport_read, 1);
  1227 +
  1228 + register_ioport_read(0x3b4, 2, vga_ioport_read, 1);
  1229 + register_ioport_read(0x3d4, 2, vga_ioport_read, 1);
  1230 + register_ioport_read(0x3ba, 1, vga_ioport_read, 1);
  1231 + register_ioport_read(0x3da, 1, vga_ioport_read, 1);
  1232 +
  1233 + vga_io_memory = cpu_register_io_memory(0, vga_mem_read, vga_mem_write);
  1234 + cpu_register_physical_memory(0xa0000, 0x20000, vga_io_memory);
  1235 + return 0;
  1236 +}
... ...
hw/vga_template.h 0 → 100644
  1 +/*
  2 + * QEMU VGA Emulator templates
  3 + *
  4 + * Copyright (c) 2003 Fabrice Bellard
  5 + *
  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:
  12 + *
  13 + * The above copyright notice and this permission notice shall be included in
  14 + * all copies or substantial portions of the Software.
  15 + *
  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
  22 + * THE SOFTWARE.
  23 + */
  24 +
  25 +#if DEPTH == 8
  26 +#define BPP 1
  27 +#define PIXEL_TYPE uint8_t
  28 +#elif DEPTH == 15 || DEPTH == 16
  29 +#define BPP 2
  30 +#define PIXEL_TYPE uint16_t
  31 +#elif DEPTH == 32
  32 +#define BPP 4
  33 +#define PIXEL_TYPE uint32_t
  34 +#else
  35 +#error unsupport depth
  36 +#endif
  37 +
  38 +#if DEPTH != 15
  39 +
  40 +static void glue(vga_draw_glyph8_, DEPTH)(uint8_t *d, int linesize,
  41 + const uint8_t *font_ptr, int h,
  42 + uint32_t fgcol, uint32_t bgcol)
  43 +{
  44 + uint32_t font_data, xorcol;
  45 +
  46 + xorcol = bgcol ^ fgcol;
  47 + do {
  48 + font_data = font_ptr[0];
  49 +#if BPP == 1
  50 + ((uint32_t *)d)[0] = (dmask16[(font_data >> 4)] & xorcol) ^ bgcol;
  51 + ((uint32_t *)d)[3] = (dmask16[(font_data >> 0) & 0xf] & xorcol) ^ bgcol;
  52 +#elif BPP == 2
  53 + ((uint32_t *)d)[0] = (dmask4[(font_data >> 6)] & xorcol) ^ bgcol;
  54 + ((uint32_t *)d)[1] = (dmask4[(font_data >> 4) & 3] & xorcol) ^ bgcol;
  55 + ((uint32_t *)d)[2] = (dmask4[(font_data >> 2) & 3] & xorcol) ^ bgcol;
  56 + ((uint32_t *)d)[3] = (dmask4[(font_data >> 0) & 3] & xorcol) ^ bgcol;
  57 +#else
  58 + ((uint32_t *)d)[0] = ((-(font_data >> 7)) & xorcol) ^ bgcol;
  59 + ((uint32_t *)d)[1] = ((-(font_data >> 6) & 1) & xorcol) ^ bgcol;
  60 + ((uint32_t *)d)[2] = ((-(font_data >> 5) & 1) & xorcol) ^ bgcol;
  61 + ((uint32_t *)d)[3] = ((-(font_data >> 4) & 1) & xorcol) ^ bgcol;
  62 + ((uint32_t *)d)[4] = ((-(font_data >> 3) & 1) & xorcol) ^ bgcol;
  63 + ((uint32_t *)d)[5] = ((-(font_data >> 2) & 1) & xorcol) ^ bgcol;
  64 + ((uint32_t *)d)[6] = ((-(font_data >> 1) & 1) & xorcol) ^ bgcol;
  65 + ((uint32_t *)d)[7] = ((-(font_data >> 0) & 1) & xorcol) ^ bgcol;
  66 +#endif
  67 + font_ptr += 4;
  68 + d += linesize;
  69 + } while (--h);
  70 +}
  71 +
  72 +static void glue(vga_draw_glyph9_, DEPTH)(uint8_t *d, int linesize,
  73 + const uint8_t *font_ptr, int h,
  74 + uint32_t fgcol, uint32_t bgcol, int dup9)
  75 +{
  76 + uint32_t font_data, xorcol, v;
  77 +
  78 + xorcol = bgcol ^ fgcol;
  79 + do {
  80 + font_data = font_ptr[0];
  81 + /* XXX: unaligned accesses are done */
  82 +#if BPP == 1
  83 + ((uint32_t *)d)[0] = (dmask16[(font_data >> 4)] & xorcol) ^ bgcol;
  84 + v = (dmask16[(font_data >> 0) & 0xf] & xorcol) ^ bgcol;
  85 + ((uint32_t *)d)[3] = v;
  86 + if (dup9)
  87 + *(uint8_t *)(d + 8) = v >> (24 * (1 - BIG));
  88 + else
  89 + *(uint8_t *)(d + 8) = bgcol;
  90 +
  91 +#elif BPP == 2
  92 + ((uint32_t *)d)[0] = (dmask4[(font_data >> 6)] & xorcol) ^ bgcol;
  93 + ((uint32_t *)d)[1] = (dmask4[(font_data >> 4) & 3] & xorcol) ^ bgcol;
  94 + ((uint32_t *)d)[2] = (dmask4[(font_data >> 2) & 3] & xorcol) ^ bgcol;
  95 + v = (dmask4[(font_data >> 0) & 3] & xorcol) ^ bgcol;
  96 + ((uint32_t *)d)[3] = v;
  97 + if (dup9)
  98 + *(uint16_t *)(d + 8) = v >> (16 * (1 - BIG));
  99 + else
  100 + *(uint16_t *)(d + 8) = bgcol;
  101 +#else
  102 + ((uint32_t *)d)[0] = ((-(font_data >> 7)) & xorcol) ^ bgcol;
  103 + ((uint32_t *)d)[1] = ((-(font_data >> 6) & 1) & xorcol) ^ bgcol;
  104 + ((uint32_t *)d)[2] = ((-(font_data >> 5) & 1) & xorcol) ^ bgcol;
  105 + ((uint32_t *)d)[3] = ((-(font_data >> 4) & 1) & xorcol) ^ bgcol;
  106 + ((uint32_t *)d)[4] = ((-(font_data >> 3) & 1) & xorcol) ^ bgcol;
  107 + ((uint32_t *)d)[5] = ((-(font_data >> 2) & 1) & xorcol) ^ bgcol;
  108 + ((uint32_t *)d)[6] = ((-(font_data >> 1) & 1) & xorcol) ^ bgcol;
  109 + v = ((-(font_data >> 0) & 1) & xorcol) ^ bgcol;
  110 + ((uint32_t *)d)[7] = v;
  111 + if (dup9)
  112 + *(uint32_t *)(d + 8) = v;
  113 + else
  114 + *(uint32_t *)(d + 8) = bgcol;
  115 +#endif
  116 + font_ptr += 4;
  117 + d += linesize;
  118 + } while (--h);
  119 +}
  120 +
  121 +/*
  122 + * 4 color mode
  123 + */
  124 +static void glue(vga_draw_line2_, DEPTH)(VGAState *s1, uint8_t *d,
  125 + const uint8_t *s, int width)
  126 +{
  127 + uint32_t plane_mask, *palette, data, v;
  128 + int x;
  129 +
  130 + palette = s1->last_palette;
  131 + plane_mask = mask16[s1->ar[0x12] & 0xf];
  132 + width >>= 3;
  133 + for(x = 0; x < width; x++) {
  134 + data = ((uint32_t *)s)[0];
  135 + data &= plane_mask;
  136 + v = expand2[GET_PLANE(data, 0)];
  137 + v |= expand2[GET_PLANE(data, 2)] << 2;
  138 + ((PIXEL_TYPE *)d)[0] = palette[v >> 12];
  139 + ((PIXEL_TYPE *)d)[1] = palette[(v >> 8) & 0xf];
  140 + ((PIXEL_TYPE *)d)[2] = palette[(v >> 4) & 0xf];
  141 + ((PIXEL_TYPE *)d)[3] = palette[(v >> 0) & 0xf];
  142 +
  143 + v = expand2[GET_PLANE(data, 1)];
  144 + v |= expand2[GET_PLANE(data, 3)] << 2;
  145 + ((PIXEL_TYPE *)d)[4] = palette[v >> 12];
  146 + ((PIXEL_TYPE *)d)[5] = palette[(v >> 8) & 0xf];
  147 + ((PIXEL_TYPE *)d)[6] = palette[(v >> 4) & 0xf];
  148 + ((PIXEL_TYPE *)d)[7] = palette[(v >> 0) & 0xf];
  149 + d += BPP * 8;
  150 + s += 4;
  151 + }
  152 +}
  153 +
  154 +/*
  155 + * 16 color mode
  156 + */
  157 +static void glue(vga_draw_line4_, DEPTH)(VGAState *s1, uint8_t *d,
  158 + const uint8_t *s, int width)
  159 +{
  160 + uint32_t plane_mask, data, v, *palette;
  161 + int x;
  162 +
  163 + palette = s1->last_palette;
  164 + plane_mask = mask16[s1->ar[0x12] & 0xf];
  165 + width >>= 3;
  166 + for(x = 0; x < width; x++) {
  167 + data = ((uint32_t *)s)[0];
  168 + data &= plane_mask;
  169 + v = expand4[GET_PLANE(data, 0)];
  170 + v |= expand4[GET_PLANE(data, 1)] << 1;
  171 + v |= expand4[GET_PLANE(data, 2)] << 2;
  172 + v |= expand4[GET_PLANE(data, 3)] << 3;
  173 + ((PIXEL_TYPE *)d)[0] = palette[v >> 28];
  174 + ((PIXEL_TYPE *)d)[1] = palette[(v >> 24) & 0xf];
  175 + ((PIXEL_TYPE *)d)[2] = palette[(v >> 20) & 0xf];
  176 + ((PIXEL_TYPE *)d)[3] = palette[(v >> 16) & 0xf];
  177 + ((PIXEL_TYPE *)d)[4] = palette[(v >> 12) & 0xf];
  178 + ((PIXEL_TYPE *)d)[5] = palette[(v >> 8) & 0xf];
  179 + ((PIXEL_TYPE *)d)[6] = palette[(v >> 4) & 0xf];
  180 + ((PIXEL_TYPE *)d)[7] = palette[(v >> 0) & 0xf];
  181 + d += BPP * 8;
  182 + s += 4;
  183 + }
  184 +}
  185 +
  186 +/*
  187 + * 256 color mode
  188 + *
  189 + * XXX: add plane_mask support (never used in standard VGA modes)
  190 + */
  191 +static void glue(vga_draw_line8_, DEPTH)(VGAState *s1, uint8_t *d,
  192 + const uint8_t *s, int width)
  193 +{
  194 + uint32_t *palette;
  195 + int x;
  196 +
  197 + palette = s1->last_palette;
  198 + width >>= 3;
  199 + for(x = 0; x < width; x++) {
  200 + ((PIXEL_TYPE *)d)[0] = palette[s[0]];
  201 + ((PIXEL_TYPE *)d)[1] = palette[s[1]];
  202 + ((PIXEL_TYPE *)d)[2] = palette[s[2]];
  203 + ((PIXEL_TYPE *)d)[3] = palette[s[3]];
  204 + ((PIXEL_TYPE *)d)[4] = palette[s[4]];
  205 + ((PIXEL_TYPE *)d)[5] = palette[s[5]];
  206 + ((PIXEL_TYPE *)d)[6] = palette[s[6]];
  207 + ((PIXEL_TYPE *)d)[7] = palette[s[7]];
  208 + d += BPP * 8;
  209 + s += 8;
  210 + }
  211 +}
  212 +
  213 +#endif /* DEPTH != 15 */
  214 +
  215 +
  216 +/* XXX: optimize */
  217 +
  218 +/*
  219 + * 15 bit color
  220 + */
  221 +static void glue(vga_draw_line15_, DEPTH)(VGAState *s1, uint8_t *d,
  222 + const uint8_t *s, int width)
  223 +{
  224 +#if DEPTH == 15 && !defined(WORDS_BIGENDIAN)
  225 + memcpy(d, s, width * 2);
  226 +#else
  227 + int w;
  228 + uint32_t v, r, g, b;
  229 +
  230 + w = width;
  231 + do {
  232 + v = lduw((void *)s);
  233 + r = (v >> 7) & 0xf8;
  234 + g = (v >> 2) & 0xf8;
  235 + b = (v << 3) & 0xf8;
  236 + ((PIXEL_TYPE *)d)[0] = glue(rgb_to_pixel, DEPTH)(r, g, b);
  237 + s += 2;
  238 + d += BPP;
  239 + } while (--w != 0);
  240 +#endif
  241 +}
  242 +
  243 +/*
  244 + * 16 bit color
  245 + */
  246 +static void glue(vga_draw_line16_, DEPTH)(VGAState *s1, uint8_t *d,
  247 + const uint8_t *s, int width)
  248 +{
  249 +#if DEPTH == 16 && !defined(WORDS_BIGENDIAN)
  250 + memcpy(d, s, width * 2);
  251 +#else
  252 + int w;
  253 + uint32_t v, r, g, b;
  254 +
  255 + w = width;
  256 + do {
  257 + v = lduw((void *)s);
  258 + r = (v >> 8) & 0xf8;
  259 + g = (v >> 3) & 0xfc;
  260 + b = (v << 3) & 0xf8;
  261 + ((PIXEL_TYPE *)d)[0] = glue(rgb_to_pixel, DEPTH)(r, g, b);
  262 + s += 2;
  263 + d += BPP;
  264 + } while (--w != 0);
  265 +#endif
  266 +}
  267 +
  268 +/*
  269 + * 32 bit color
  270 + */
  271 +static void glue(vga_draw_line32_, DEPTH)(VGAState *s1, uint8_t *d,
  272 + const uint8_t *s, int width)
  273 +{
  274 +#if DEPTH == 32 && !defined(WORDS_BIGENDIAN)
  275 + memcpy(d, s, width * 4);
  276 +#else
  277 + int w;
  278 + uint32_t r, g, b;
  279 +
  280 + w = width;
  281 + do {
  282 + b = s[0];
  283 + g = s[1];
  284 + r = s[2];
  285 + ((PIXEL_TYPE *)d)[0] = glue(rgb_to_pixel, DEPTH)(r, g, b);
  286 + s += 4;
  287 + d += BPP;
  288 + } while (--w != 0);
  289 +#endif
  290 +}
  291 +
  292 +#undef DEPTH
  293 +#undef BPP
  294 +#undef PIXEL_TYPE
... ...