| 1 | /* |
| 2 | * Copyright (C) 2002-2010 The DOSBox Team |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | /* $Id: vga_gfx.cpp,v 1.19 2009-05-27 09:15:41 qbix79 Exp $ */ |
| 20 | |
| 21 | #include "dosbox.h" |
| 22 | #include "inout.h" |
| 23 | #include "vga.h" |
| 24 | |
| 25 | #define gfx(blah) vga.gfx.blah |
| 26 | static bool index9warned=false; |
| 27 | |
| 28 | static void write_p3ce(Bitu port,Bitu val,Bitu iolen) { |
| 29 | gfx(index)=val & 0x0f; |
| 30 | } |
| 31 | |
| 32 | static Bitu read_p3ce(Bitu port,Bitu iolen) { |
| 33 | return gfx(index); |
| 34 | } |
| 35 | |
| 36 | static void write_p3cf(Bitu port,Bitu val,Bitu iolen) { |
| 37 | switch (gfx(index)) { |
| 38 | case 0: /* Set/Reset Register */ |
| 39 | gfx(set_reset)=val & 0x0f; |
| 40 | vga.config.full_set_reset=FillTable[val & 0x0f]; |
| 41 | vga.config.full_enable_and_set_reset=vga.config.full_set_reset & |
| 42 | vga.config.full_enable_set_reset; |
| 43 | /* |
| 44 | 0 If in Write Mode 0 and bit 0 of 3CEh index 1 is set a write to |
| 45 | display memory will set all the bits in plane 0 of the byte to this |
| 46 | bit, if the corresponding bit is set in the Map Mask Register (3CEh |
| 47 | index 8). |
| 48 | 1 Same for plane 1 and bit 1 of 3CEh index 1. |
| 49 | 2 Same for plane 2 and bit 2 of 3CEh index 1. |
| 50 | 3 Same for plane 3 and bit 3 of 3CEh index 1. |
| 51 | */ |
| 52 | // LOG_DEBUG("Set Reset = %2X",val); |
| 53 | break; |
| 54 | case 1: /* Enable Set/Reset Register */ |
| 55 | gfx(enable_set_reset)=val & 0x0f; |
| 56 | vga.config.full_enable_set_reset=FillTable[val & 0x0f]; |
| 57 | vga.config.full_not_enable_set_reset=~vga.config.full_enable_set_reset; |
| 58 | vga.config.full_enable_and_set_reset=vga.config.full_set_reset & |
| 59 | vga.config.full_enable_set_reset; |
| 60 | // if (gfx(enable_set_reset)) vga.config.mh_mask|=MH_SETRESET else vga.config.mh_mask&=~MH_SETRESET; |
| 61 | break; |
| 62 | case 2: /* Color Compare Register */ |
| 63 | gfx(color_compare)=val & 0x0f; |
| 64 | /* |
| 65 | 0-3 In Read Mode 1 each pixel at the address of the byte read is compared |
| 66 | to this color and the corresponding bit in the output set to 1 if |
| 67 | they match, 0 if not. The Color Don't Care Register (3CEh index 7) |
| 68 | can exclude bitplanes from the comparison. |
| 69 | */ |
| 70 | vga.config.color_compare=val & 0xf; |
| 71 | // LOG_DEBUG("Color Compare = %2X",val); |
| 72 | break; |
| 73 | case 3: /* Data Rotate */ |
| 74 | gfx(data_rotate)=val; |
| 75 | vga.config.data_rotate=val & 7; |
| 76 | // if (val) vga.config.mh_mask|=MH_ROTATEOP else vga.config.mh_mask&=~MH_ROTATEOP; |
| 77 | vga.config.raster_op=(val>>3) & 3; |
| 78 | /* |
| 79 | 0-2 Number of positions to rotate data right before it is written to |
| 80 | display memory. Only active in Write Mode 0. |
| 81 | 3-4 In Write Mode 2 this field controls the relation between the data |
| 82 | written from the CPU, the data latched from the previous read and the |
| 83 | data written to display memory: |
| 84 | 0: CPU Data is written unmodified |
| 85 | 1: CPU data is ANDed with the latched data |
| 86 | 2: CPU data is ORed with the latch data. |
| 87 | 3: CPU data is XORed with the latched data. |
| 88 | */ |
| 89 | break; |
| 90 | case 4: /* Read Map Select Register */ |
| 91 | /* 0-1 number of the plane Read Mode 0 will read from */ |
| 92 | gfx(read_map_select)=val & 0x03; |
| 93 | vga.config.read_map_select=val & 0x03; |
| 94 | // LOG_DEBUG("Read Map %2X",val); |
| 95 | break; |
| 96 | case 5: /* Mode Register */ |
| 97 | if ((gfx(mode) ^ val) & 0xf0) { |
| 98 | gfx(mode)=val; |
| 99 | VGA_DetermineMode(); |
| 100 | } else gfx(mode)=val; |
| 101 | vga.config.write_mode=val & 3; |
| 102 | vga.config.read_mode=(val >> 3) & 1; |
| 103 | // LOG_DEBUG("Write Mode %d Read Mode %d val %d",vga.config.write_mode,vga.config.read_mode,val); |
| 104 | /* |
| 105 | 0-1 Write Mode: Controls how data from the CPU is transformed before |
| 106 | being written to display memory: |
| 107 | 0: Mode 0 works as a Read-Modify-Write operation. |
| 108 | First a read access loads the data latches of the VGA with the |
| 109 | value in video memory at the addressed location. Then a write |
| 110 | access will provide the destination address and the CPU data |
| 111 | byte. The data written is modified by the function code in the |
| 112 | Data Rotate register (3CEh index 3) as a function of the CPU |
| 113 | data and the latches, then data is rotated as specified by the |
| 114 | same register. |
| 115 | 1: Mode 1 is used for video to video transfers. |
| 116 | A read access will load the data latches with the contents of |
| 117 | the addressed byte of video memory. A write access will write |
| 118 | the contents of the latches to the addressed byte. Thus a single |
| 119 | MOVSB instruction can copy all pixels in the source address byte |
| 120 | to the destination address. |
| 121 | 2: Mode 2 writes a color to all pixels in the addressed byte of |
| 122 | video memory. Bit 0 of the CPU data is written to plane 0 et |
| 123 | cetera. Individual bits can be enabled or disabled through the |
| 124 | Bit Mask register (3CEh index 8). |
| 125 | 3: Mode 3 can be used to fill an area with a color and pattern. The |
| 126 | CPU data is rotated according to 3CEh index 3 bits 0-2 and anded |
| 127 | with the Bit Mask Register (3CEh index 8). For each bit in the |
| 128 | result the corresponding pixel is set to the color in the |
| 129 | Set/Reset Register (3CEh index 0 bits 0-3) if the bit is set and |
| 130 | to the contents of the processor latch if the bit is clear. |
| 131 | 3 Read Mode |
| 132 | 0: Data is read from one of 4 bit planes depending on the Read Map |
| 133 | Select Register (3CEh index 4). |
| 134 | 1: Data returned is a comparison between the 8 pixels occupying the |
| 135 | read byte and the color in the Color Compare Register (3CEh |
| 136 | index 2). A bit is set if the color of the corresponding pixel |
| 137 | matches the register. |
| 138 | 4 Enables Odd/Even mode if set (See 3C4h index 4 bit 2). |
| 139 | 5 Enables CGA style 4 color pixels using even/odd bit pairs if set. |
| 140 | 6 Enables 256 color mode if set. |
| 141 | */ |
| 142 | break; |
| 143 | case 6: /* Miscellaneous Register */ |
| 144 | if ((gfx(miscellaneous) ^ val) & 0x0c) { |
| 145 | gfx(miscellaneous)=val; |
| 146 | VGA_DetermineMode(); |
| 147 | } else gfx(miscellaneous)=val; |
| 148 | VGA_SetupHandlers(); |
| 149 | /* |
| 150 | 0 Indicates Graphics Mode if set, Alphanumeric mode else. |
| 151 | 1 Enables Odd/Even mode if set. |
| 152 | 2-3 Memory Mapping: |
| 153 | 0: use A000h-BFFFh |
| 154 | 1: use A000h-AFFFh VGA Graphics modes |
| 155 | 2: use B000h-B7FFh Monochrome modes |
| 156 | 3: use B800h-BFFFh CGA modes |
| 157 | */ |
| 158 | break; |
| 159 | case 7: /* Color Don't Care Register */ |
| 160 | gfx(color_dont_care)=val & 0x0f; |
| 161 | /* |
| 162 | 0 Ignore bit plane 0 in Read mode 1 if clear. |
| 163 | 1 Ignore bit plane 1 in Read mode 1 if clear. |
| 164 | 2 Ignore bit plane 2 in Read mode 1 if clear. |
| 165 | 3 Ignore bit plane 3 in Read mode 1 if clear. |
| 166 | */ |
| 167 | vga.config.color_dont_care=val & 0xf; |
| 168 | // LOG_DEBUG("Color don't care = %2X",val); |
| 169 | break; |
| 170 | case 8: /* Bit Mask Register */ |
| 171 | gfx(bit_mask)=val; |
| 172 | vga.config.full_bit_mask=ExpandTable[val]; |
| 173 | // LOG_DEBUG("Bit mask %2X",val); |
| 174 | /* |
| 175 | 0-7 Each bit if set enables writing to the corresponding bit of a byte in |
| 176 | display memory. |
| 177 | */ |
| 178 | break; |
| 179 | default: |
| 180 | if (svga.write_p3cf) { |
| 181 | svga.write_p3cf(gfx(index), val, iolen); |
| 182 | break; |
| 183 | } |
| 184 | if (gfx(index) == 9 && !index9warned) { |
| 185 | LOG(LOG_VGAMISC,LOG_NORMAL)("VGA:3CF:Write %2X to illegal index 9",val); |
| 186 | index9warned=true; |
| 187 | break; |
| 188 | } |
| 189 | LOG(LOG_VGAMISC,LOG_NORMAL)("VGA:3CF:Write %2X to illegal index %2X",val,gfx(index)); |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | static Bitu read_p3cf(Bitu port,Bitu iolen) { |
| 195 | switch (gfx(index)) { |
| 196 | case 0: /* Set/Reset Register */ |
| 197 | return gfx(set_reset); |
| 198 | case 1: /* Enable Set/Reset Register */ |
| 199 | return gfx(enable_set_reset); |
| 200 | case 2: /* Color Compare Register */ |
| 201 | return gfx(color_compare); |
| 202 | case 3: /* Data Rotate */ |
| 203 | return gfx(data_rotate); |
| 204 | case 4: /* Read Map Select Register */ |
| 205 | return gfx(read_map_select); |
| 206 | case 5: /* Mode Register */ |
| 207 | return gfx(mode); |
| 208 | case 6: /* Miscellaneous Register */ |
| 209 | return gfx(miscellaneous); |
| 210 | case 7: /* Color Don't Care Register */ |
| 211 | return gfx(color_dont_care); |
| 212 | case 8: /* Bit Mask Register */ |
| 213 | return gfx(bit_mask); |
| 214 | default: |
| 215 | if (svga.read_p3cf) |
| 216 | return svga.read_p3cf(gfx(index), iolen); |
| 217 | LOG(LOG_VGAMISC,LOG_NORMAL)("Reading from illegal index %2X in port %4X",static_cast<Bit32u>(gfx(index)),port); |
| 218 | break; |
| 219 | } |
| 220 | return 0; /* Compiler happy */ |
| 221 | } |
| 222 | |
| 223 | |
| 224 | |
| 225 | void VGA_SetupGFX(void) { |
| 226 | if (IS_EGAVGA_ARCH) { |
| 227 | IO_RegisterWriteHandler(0x3ce,write_p3ce,IO_MB); |
| 228 | IO_RegisterWriteHandler(0x3cf,write_p3cf,IO_MB); |
| 229 | if (IS_VGA_ARCH) { |
| 230 | IO_RegisterReadHandler(0x3ce,read_p3ce,IO_MB); |
| 231 | IO_RegisterReadHandler(0x3cf,read_p3cf,IO_MB); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 |