| 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_other.cpp,v 1.28 2009-07-11 10:25:24 c2woody Exp $ */ |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <math.h> |
| 23 | #include "dosbox.h" |
| 24 | #include "inout.h" |
| 25 | #include "vga.h" |
| 26 | #include "mem.h" |
| 27 | #include "pic.h" |
| 28 | #include "render.h" |
| 29 | #include "mapper.h" |
| 30 | |
| 31 | static void write_crtc_index_other(Bitu /*port*/,Bitu val,Bitu /*iolen*/) { |
| 32 | vga.other.index=(Bit8u)val; |
| 33 | } |
| 34 | |
| 35 | static Bitu read_crtc_index_other(Bitu /*port*/,Bitu /*iolen*/) { |
| 36 | return vga.other.index; |
| 37 | } |
| 38 | |
| 39 | static void write_crtc_data_other(Bitu /*port*/,Bitu val,Bitu /*iolen*/) { |
| 40 | switch (vga.other.index) { |
| 41 | case 0x00: //Horizontal total |
| 42 | if (vga.other.htotal ^ val) VGA_StartResize(); |
| 43 | vga.other.htotal=(Bit8u)val; |
| 44 | break; |
| 45 | case 0x01: //Horizontal displayed chars |
| 46 | if (vga.other.hdend ^ val) VGA_StartResize(); |
| 47 | vga.other.hdend=(Bit8u)val; |
| 48 | break; |
| 49 | case 0x02: //Horizontal sync position |
| 50 | vga.other.hsyncp=(Bit8u)val; |
| 51 | break; |
| 52 | case 0x03: //Horizontal sync width |
| 53 | if (machine==MCH_TANDY) vga.other.vsyncw=(Bit8u)(val >> 4); |
| 54 | else vga.other.vsyncw = 16; // The MC6845 has a fixed v-sync width of 16 lines |
| 55 | vga.other.hsyncw=(Bit8u)(val & 0xf); |
| 56 | break; |
| 57 | case 0x04: //Vertical total |
| 58 | if (vga.other.vtotal ^ val) VGA_StartResize(); |
| 59 | vga.other.vtotal=(Bit8u)val; |
| 60 | break; |
| 61 | case 0x05: //Vertical display adjust |
| 62 | if (vga.other.vadjust ^ val) VGA_StartResize(); |
| 63 | vga.other.vadjust=(Bit8u)val; |
| 64 | break; |
| 65 | case 0x06: //Vertical rows |
| 66 | if (vga.other.vdend ^ val) VGA_StartResize(); |
| 67 | vga.other.vdend=(Bit8u)val; |
| 68 | break; |
| 69 | case 0x07: //Vertical sync position |
| 70 | vga.other.vsyncp=(Bit8u)val; |
| 71 | break; |
| 72 | case 0x09: //Max scanline |
| 73 | val &= 0x1f; // VGADOC says bit 0-3 but the MC6845 datasheet says bit 0-4 |
| 74 | if (vga.other.max_scanline ^ val) VGA_StartResize(); |
| 75 | vga.other.max_scanline=(Bit8u)val; |
| 76 | break; |
| 77 | case 0x0A: /* Cursor Start Register */ |
| 78 | vga.other.cursor_start = (Bit8u)(val & 0x3f); |
| 79 | vga.draw.cursor.sline = (Bit8u)(val&0x1f); |
| 80 | vga.draw.cursor.enabled = ((val & 0x60) != 0x20); |
| 81 | break; |
| 82 | case 0x0B: /* Cursor End Register */ |
| 83 | vga.other.cursor_end = (Bit8u)(val&0x1f); |
| 84 | vga.draw.cursor.eline = (Bit8u)(val&0x1f); |
| 85 | break; |
| 86 | case 0x0C: /* Start Address High Register */ |
| 87 | vga.config.display_start=(vga.config.display_start & 0x00FF) | (val << 8); |
| 88 | break; |
| 89 | case 0x0D: /* Start Address Low Register */ |
| 90 | vga.config.display_start=(vga.config.display_start & 0xFF00) | val; |
| 91 | break; |
| 92 | case 0x0E: /*Cursor Location High Register */ |
| 93 | vga.config.cursor_start&=0x00ff; |
| 94 | vga.config.cursor_start|=((Bit8u)val) << 8; |
| 95 | break; |
| 96 | case 0x0F: /* Cursor Location Low Register */ |
| 97 | vga.config.cursor_start&=0xff00; |
| 98 | vga.config.cursor_start|=(Bit8u)val; |
| 99 | break; |
| 100 | case 0x10: /* Light Pen High */ |
| 101 | vga.other.lightpen &= 0xff; |
| 102 | vga.other.lightpen |= (val & 0x3f)<<8; // only 6 bits |
| 103 | break; |
| 104 | case 0x11: /* Light Pen Low */ |
| 105 | vga.other.lightpen &= 0xff00; |
| 106 | vga.other.lightpen |= (Bit8u)val; |
| 107 | break; |
| 108 | default: |
| 109 | LOG(LOG_VGAMISC,LOG_NORMAL)("MC6845:Write %X to illegal index %x",val,vga.other.index); |
| 110 | } |
| 111 | } |
| 112 | static Bitu read_crtc_data_other(Bitu /*port*/,Bitu /*iolen*/) { |
| 113 | switch (vga.other.index) { |
| 114 | case 0x00: //Horizontal total |
| 115 | return vga.other.htotal; |
| 116 | case 0x01: //Horizontal displayed chars |
| 117 | return vga.other.hdend; |
| 118 | case 0x02: //Horizontal sync position |
| 119 | return vga.other.hsyncp; |
| 120 | case 0x03: //Horizontal and vertical sync width |
| 121 | if (machine==MCH_TANDY) |
| 122 | return vga.other.hsyncw | (vga.other.vsyncw << 4); |
| 123 | else return vga.other.hsyncw; |
| 124 | case 0x04: //Vertical total |
| 125 | return vga.other.vtotal; |
| 126 | case 0x05: //Vertical display adjust |
| 127 | return vga.other.vadjust; |
| 128 | case 0x06: //Vertical rows |
| 129 | return vga.other.vdend; |
| 130 | case 0x07: //Vertical sync position |
| 131 | return vga.other.vsyncp; |
| 132 | case 0x09: //Max scanline |
| 133 | return vga.other.max_scanline; |
| 134 | case 0x0A: /* Cursor Start Register */ |
| 135 | return vga.other.cursor_start; |
| 136 | case 0x0B: /* Cursor End Register */ |
| 137 | return vga.other.cursor_end; |
| 138 | case 0x0C: /* Start Address High Register */ |
| 139 | return (Bit8u)(vga.config.display_start >> 8); |
| 140 | case 0x0D: /* Start Address Low Register */ |
| 141 | return (Bit8u)(vga.config.display_start & 0xff); |
| 142 | case 0x0E: /*Cursor Location High Register */ |
| 143 | return (Bit8u)(vga.config.cursor_start >> 8); |
| 144 | case 0x0F: /* Cursor Location Low Register */ |
| 145 | return (Bit8u)(vga.config.cursor_start & 0xff); |
| 146 | case 0x10: /* Light Pen High */ |
| 147 | return (Bit8u)(vga.other.lightpen >> 8); |
| 148 | case 0x11: /* Light Pen Low */ |
| 149 | return (Bit8u)(vga.other.lightpen & 0xff); |
| 150 | default: |
| 151 | LOG(LOG_VGAMISC,LOG_NORMAL)("MC6845:Read from illegal index %x",vga.other.index); |
| 152 | } |
| 153 | return (Bitu)(~0); |
| 154 | } |
| 155 | |
| 156 | static double hue_offset = 0.0; |
| 157 | static Bit8u cga16_val = 0; |
| 158 | static void update_cga16_color(void); |
| 159 | static Bit8u herc_pal = 0; |
| 160 | |
| 161 | static void cga16_color_select(Bit8u val) { |
| 162 | cga16_val = val; |
| 163 | update_cga16_color(); |
| 164 | } |
| 165 | |
| 166 | static void update_cga16_color(void) { |
| 167 | // Algorithm provided by NewRisingSun |
| 168 | // His/Her algorithm is more complex and gives better results than the one below |
| 169 | // However that algorithm doesn't fit in our vga pallette. |
| 170 | // Therefore a simple variant is used, but the colours are bit lighter. |
| 171 | |
| 172 | // It uses an avarage over the bits to give smooth transitions from colour to colour |
| 173 | // This is represented by the j variable. The i variable gives the 16 colours |
| 174 | // The draw handler calculates the needed avarage and combines this with the colour |
| 175 | // to match an entry that is generated here. |
| 176 | |
| 177 | int baseR=0, baseG=0, baseB=0; |
| 178 | double sinhue,coshue,hue,basehue = 50.0; |
| 179 | double I,Q,Y,pixelI,pixelQ,R,G,B; |
| 180 | Bitu colorBit1,colorBit2,colorBit3,colorBit4,index; |
| 181 | |
| 182 | if (cga16_val & 0x01) baseB += 0xa8; |
| 183 | if (cga16_val & 0x02) baseG += 0xa8; |
| 184 | if (cga16_val & 0x04) baseR += 0xa8; |
| 185 | if (cga16_val & 0x08) { baseR += 0x57; baseG += 0x57; baseB += 0x57; } |
| 186 | if (cga16_val & 0x20) basehue = 35.0; |
| 187 | |
| 188 | hue = (basehue + hue_offset)*0.017453239; |
| 189 | sinhue = sin(hue); |
| 190 | coshue = cos(hue); |
| 191 | |
| 192 | for(Bitu i = 0; i < 16;i++) { |
| 193 | for(Bitu j = 0;j < 5;j++) { |
| 194 | index = 0x80|(j << 4)|i; //use upperpart of vga pallette |
| 195 | colorBit4 = (i&1)>>0; |
| 196 | colorBit3 = (i&2)>>1; |
| 197 | colorBit2 = (i&4)>>2; |
| 198 | colorBit1 = (i&8)>>3; |
| 199 | |
| 200 | //calculate lookup table |
| 201 | I = 0; Q = 0; |
| 202 | I += (double) colorBit1; |
| 203 | Q += (double) colorBit2; |
| 204 | I -= (double) colorBit3; |
| 205 | Q -= (double) colorBit4; |
| 206 | Y = (double) j / 4.0; //calculated avarage is over 4 bits |
| 207 | |
| 208 | pixelI = I * 1.0 / 3.0; //I* tvSaturnation / 3.0 |
| 209 | pixelQ = Q * 1.0 / 3.0; //Q* tvSaturnation / 3.0 |
| 210 | I = pixelI*coshue + pixelQ*sinhue; |
| 211 | Q = pixelQ*coshue - pixelI*sinhue; |
| 212 | |
| 213 | R = Y + 0.956*I + 0.621*Q; if (R < 0.0) R = 0.0; if (R > 1.0) R = 1.0; |
| 214 | G = Y - 0.272*I - 0.647*Q; if (G < 0.0) G = 0.0; if (G > 1.0) G = 1.0; |
| 215 | B = Y - 1.105*I + 1.702*Q; if (B < 0.0) B = 0.0; if (B > 1.0) B = 1.0; |
| 216 | |
| 217 | RENDER_SetPal((Bit8u)index,static_cast<Bit8u>(R*baseR),static_cast<Bit8u>(G*baseG),static_cast<Bit8u>(B*baseB)); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | static void IncreaseHue(bool pressed) { |
| 223 | if (!pressed) |
| 224 | return; |
| 225 | hue_offset += 5.0; |
| 226 | update_cga16_color(); |
| 227 | LOG_MSG("Hue at %f",hue_offset); |
| 228 | } |
| 229 | |
| 230 | static void DecreaseHue(bool pressed) { |
| 231 | if (!pressed) |
| 232 | return; |
| 233 | hue_offset -= 5.0; |
| 234 | update_cga16_color(); |
| 235 | LOG_MSG("Hue at %f",hue_offset); |
| 236 | } |
| 237 | |
| 238 | static void write_color_select(Bit8u val) { |
| 239 | vga.tandy.color_select=val; |
| 240 | switch (vga.mode) { |
| 241 | case M_TANDY2: |
| 242 | VGA_SetCGA2Table(0,val & 0xf); |
| 243 | break; |
| 244 | case M_TANDY4: |
| 245 | { |
| 246 | if ((machine==MCH_TANDY && (vga.tandy.gfx_control & 0x8)) || |
| 247 | (machine==MCH_PCJR && (vga.tandy.mode_control==0x0b))) { |
| 248 | VGA_SetCGA4Table(0,1,2,3); |
| 249 | return; |
| 250 | } |
| 251 | Bit8u base=(val & 0x10) ? 0x08 : 0; |
| 252 | /* Check for BW Mode */ |
| 253 | if (vga.tandy.mode_control & 0x4) { |
| 254 | VGA_SetCGA4Table(val & 0xf,3+base,4+base,7+base); |
| 255 | } else { |
| 256 | if (val & 0x20) VGA_SetCGA4Table(val & 0xf,3+base,5+base,7+base); |
| 257 | else VGA_SetCGA4Table(val & 0xf,2+base,4+base,6+base); |
| 258 | } |
| 259 | } |
| 260 | break; |
| 261 | case M_CGA16: |
| 262 | cga16_color_select(val); |
| 263 | break; |
| 264 | case M_TEXT: |
| 265 | case M_TANDY16: |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | static void TANDY_FindMode(void) { |
| 271 | if (vga.tandy.mode_control & 0x2) { |
| 272 | if (vga.tandy.gfx_control & 0x10) |
| 273 | VGA_SetMode(M_TANDY16); |
| 274 | else if (vga.tandy.gfx_control & 0x08) |
| 275 | VGA_SetMode(M_TANDY4); |
| 276 | else if (vga.tandy.mode_control & 0x10) |
| 277 | VGA_SetMode(M_TANDY2); |
| 278 | else |
| 279 | VGA_SetMode(M_TANDY4); |
| 280 | write_color_select(vga.tandy.color_select); |
| 281 | } else { |
| 282 | VGA_SetMode(M_TANDY_TEXT); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | void VGA_SetModeNow(VGAModes mode); |
| 287 | |
| 288 | static void PCJr_FindMode(void) { |
| 289 | if (vga.tandy.mode_control & 0x2) { |
| 290 | if (vga.tandy.mode_control & 0x10) { |
| 291 | /* bit4 of mode control 1 signals 16 colour graphics mode */ |
| 292 | if (vga.mode==M_TANDY4) VGA_SetModeNow(M_TANDY16); // TODO lowres mode only |
| 293 | else VGA_SetMode(M_TANDY16); |
| 294 | } else if (vga.tandy.gfx_control & 0x08) { |
| 295 | /* bit3 of mode control 2 signals 2 colour graphics mode */ |
| 296 | VGA_SetMode(M_TANDY2); |
| 297 | } else { |
| 298 | /* otherwise some 4-colour graphics mode */ |
| 299 | if (vga.mode==M_TANDY16) VGA_SetModeNow(M_TANDY4); |
| 300 | else VGA_SetMode(M_TANDY4); |
| 301 | } |
| 302 | write_color_select(vga.tandy.color_select); |
| 303 | } else { |
| 304 | VGA_SetMode(M_TANDY_TEXT); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | static void TandyCheckLineMask(void ) { |
| 309 | if ( vga.tandy.extended_ram & 1 ) { |
| 310 | vga.tandy.line_mask = 0; |
| 311 | } else if ( vga.tandy.mode_control & 0x2) { |
| 312 | vga.tandy.line_mask |= 1; |
| 313 | } |
| 314 | if ( vga.tandy.line_mask ) { |
| 315 | vga.tandy.line_shift = 13; |
| 316 | vga.tandy.addr_mask = (1 << 13) - 1; |
| 317 | } else { |
| 318 | vga.tandy.addr_mask = (Bitu)(~0); |
| 319 | vga.tandy.line_shift = 0; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | static void write_tandy_reg(Bit8u val) { |
| 324 | switch (vga.tandy.reg_index) { |
| 325 | case 0x0: |
| 326 | if (machine==MCH_PCJR) { |
| 327 | vga.tandy.mode_control=val; |
| 328 | VGA_SetBlinking(val & 0x20); |
| 329 | PCJr_FindMode(); |
| 330 | vga.attr.disabled = (val&0x8)? 0: 1; |
| 331 | } else { |
| 332 | LOG(LOG_VGAMISC,LOG_NORMAL)("Unhandled Write %2X to tandy reg %X",val,vga.tandy.reg_index); |
| 333 | } |
| 334 | break; |
| 335 | case 0x2: /* Border color */ |
| 336 | vga.tandy.border_color=val; |
| 337 | break; |
| 338 | case 0x3: /* More control */ |
| 339 | vga.tandy.gfx_control=val; |
| 340 | if (machine==MCH_TANDY) TANDY_FindMode(); |
| 341 | else PCJr_FindMode(); |
| 342 | break; |
| 343 | case 0x5: /* Extended ram page register */ |
| 344 | // Bit 0 enables extended ram |
| 345 | // Bit 7 Switches clock, 0 -> cga 28.6 , 1 -> mono 32.5 |
| 346 | vga.tandy.extended_ram = val; |
| 347 | //This is a bit of a hack to enable mapping video memory differently for highres mode |
| 348 | TandyCheckLineMask(); |
| 349 | VGA_SetupHandlers(); |
| 350 | break; |
| 351 | case 0x8: /* Monitor mode seletion */ |
| 352 | //Bit 1 select mode e, for 640x200x16, some double clocking thing? |
| 353 | //Bit 4 select 350 line mode for hercules emulation |
| 354 | LOG(LOG_VGAMISC,LOG_NORMAL)("Write %2X to tandy monitor mode",val ); |
| 355 | break; |
| 356 | /* palette colors */ |
| 357 | case 0x10: case 0x11: case 0x12: case 0x13: |
| 358 | case 0x14: case 0x15: case 0x16: case 0x17: |
| 359 | case 0x18: case 0x19: case 0x1a: case 0x1b: |
| 360 | case 0x1c: case 0x1d: case 0x1e: case 0x1f: |
| 361 | VGA_ATTR_SetPalette(vga.tandy.reg_index-0x10,val & 0xf); |
| 362 | break; |
| 363 | default: |
| 364 | LOG(LOG_VGAMISC,LOG_NORMAL)("Unhandled Write %2X to tandy reg %X",val,vga.tandy.reg_index); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | static void write_cga(Bitu port,Bitu val,Bitu /*iolen*/) { |
| 369 | switch (port) { |
| 370 | case 0x3d8: |
| 371 | vga.tandy.mode_control=(Bit8u)val; |
| 372 | vga.attr.disabled = (val&0x8)? 0: 1; |
| 373 | if (vga.tandy.mode_control & 0x2) { |
| 374 | if (vga.tandy.mode_control & 0x10) { |
| 375 | if (!(val & 0x4) && machine==MCH_CGA) { |
| 376 | VGA_SetMode(M_CGA16); //Video burst 16 160x200 color mode |
| 377 | } else { |
| 378 | VGA_SetMode(M_TANDY2); |
| 379 | } |
| 380 | } else VGA_SetMode(M_TANDY4); |
| 381 | write_color_select(vga.tandy.color_select); |
| 382 | } else { |
| 383 | VGA_SetMode(M_TANDY_TEXT); |
| 384 | } |
| 385 | VGA_SetBlinking(val & 0x20); |
| 386 | break; |
| 387 | case 0x3d9: |
| 388 | write_color_select((Bit8u)val); |
| 389 | break; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | static void write_tandy(Bitu port,Bitu val,Bitu /*iolen*/) { |
| 394 | switch (port) { |
| 395 | case 0x3d8: |
| 396 | vga.tandy.mode_control=(Bit8u)val; |
| 397 | TandyCheckLineMask(); |
| 398 | VGA_SetBlinking(val & 0x20); |
| 399 | TANDY_FindMode(); |
| 400 | break; |
| 401 | case 0x3d9: |
| 402 | write_color_select((Bit8u)val); |
| 403 | break; |
| 404 | case 0x3da: |
| 405 | vga.tandy.reg_index=(Bit8u)val; |
| 406 | break; |
| 407 | case 0x3db: // Clear lightpen latch |
| 408 | vga.other.lightpen_triggered = false; |
| 409 | break; |
| 410 | case 0x3dc: // Preset lightpen latch |
| 411 | if (!vga.other.lightpen_triggered) { |
| 412 | vga.other.lightpen_triggered = true; // TODO: this shows at port 3ba/3da bit 1 |
| 413 | |
| 414 | double timeInFrame = PIC_FullIndex()-vga.draw.delay.framestart; |
| 415 | double timeInLine = fmod(timeInFrame,vga.draw.delay.htotal); |
| 416 | Bitu current_scanline = (Bitu)(timeInFrame / vga.draw.delay.htotal); |
| 417 | |
| 418 | vga.other.lightpen = (Bit16u)((vga.draw.address_add/2) * (current_scanline/2)); |
| 419 | vga.other.lightpen += (Bit16u)((timeInLine / vga.draw.delay.hdend) * |
| 420 | ((float)(vga.draw.address_add/2))); |
| 421 | } |
| 422 | break; |
| 423 | // case 0x3dd: //Extended ram page address register: |
| 424 | break; |
| 425 | case 0x3de: |
| 426 | write_tandy_reg((Bit8u)val); |
| 427 | break; |
| 428 | case 0x3df: |
| 429 | vga.tandy.line_mask = (Bit8u)(val >> 6); |
| 430 | vga.tandy.draw_bank = val & ((vga.tandy.line_mask&2) ? 0x6 : 0x7); |
| 431 | vga.tandy.mem_bank = (val >> 3) & ((vga.tandy.line_mask&2) ? 0x6 : 0x7); |
| 432 | TandyCheckLineMask(); |
| 433 | VGA_SetupHandlers(); |
| 434 | break; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | static void write_pcjr(Bitu port,Bitu val,Bitu /*iolen*/) { |
| 439 | switch (port) { |
| 440 | case 0x3d9: |
| 441 | write_color_select((Bit8u)val); |
| 442 | break; |
| 443 | case 0x3da: |
| 444 | if (vga.tandy.pcjr_flipflop) write_tandy_reg((Bit8u)val); |
| 445 | else vga.tandy.reg_index=(Bit8u)val; |
| 446 | vga.tandy.pcjr_flipflop=!vga.tandy.pcjr_flipflop; |
| 447 | break; |
| 448 | case 0x3df: |
| 449 | vga.tandy.line_mask = (Bit8u)(val >> 6); |
| 450 | vga.tandy.draw_bank = val & ((vga.tandy.line_mask&2) ? 0x6 : 0x7); |
| 451 | vga.tandy.mem_bank = (val >> 3) & ((vga.tandy.line_mask&2) ? 0x6 : 0x7); |
| 452 | vga.tandy.draw_base = &MemBase[vga.tandy.draw_bank * 16 * 1024]; |
| 453 | vga.tandy.mem_base = &MemBase[vga.tandy.mem_bank * 16 * 1024]; |
| 454 | TandyCheckLineMask(); |
| 455 | VGA_SetupHandlers(); |
| 456 | break; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | static void CycleHercPal(bool pressed) { |
| 461 | if (!pressed) return; |
| 462 | if (++herc_pal>2) herc_pal=0; |
| 463 | Herc_Palette(); |
| 464 | VGA_DAC_CombineColor(1,7); |
| 465 | } |
| 466 | |
| 467 | void Herc_Palette(void) { |
| 468 | switch (herc_pal) { |
| 469 | case 0: // White |
| 470 | VGA_DAC_SetEntry(0x7,0x2a,0x2a,0x2a); |
| 471 | VGA_DAC_SetEntry(0xf,0x3f,0x3f,0x3f); |
| 472 | break; |
| 473 | case 1: // Amber |
| 474 | VGA_DAC_SetEntry(0x7,0x34,0x20,0x00); |
| 475 | VGA_DAC_SetEntry(0xf,0x3f,0x34,0x00); |
| 476 | break; |
| 477 | case 2: // Green |
| 478 | VGA_DAC_SetEntry(0x7,0x00,0x26,0x00); |
| 479 | VGA_DAC_SetEntry(0xf,0x00,0x3f,0x00); |
| 480 | break; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | static void write_hercules(Bitu port,Bitu val,Bitu /*iolen*/) { |
| 485 | switch (port) { |
| 486 | case 0x3b8: { |
| 487 | // the protected bits can always be cleared but only be set if the |
| 488 | // protection bits are set |
| 489 | if (vga.herc.mode_control&0x2) { |
| 490 | // already set |
| 491 | if (!(val&0x2)) { |
| 492 | vga.herc.mode_control &= ~0x2; |
| 493 | VGA_SetMode(M_HERC_TEXT); |
| 494 | } |
| 495 | } else { |
| 496 | // not set, can only set if protection bit is set |
| 497 | if ((val & 0x2) && (vga.herc.enable_bits & 0x1)) { |
| 498 | vga.herc.mode_control |= 0x2; |
| 499 | VGA_SetMode(M_HERC_GFX); |
| 500 | } |
| 501 | } |
| 502 | if (vga.herc.mode_control&0x80) { |
| 503 | if (!(val&0x80)) { |
| 504 | vga.herc.mode_control &= ~0x80; |
| 505 | vga.tandy.draw_base = &vga.mem.linear[0]; |
| 506 | } |
| 507 | } else { |
| 508 | if ((val & 0x80) && (vga.herc.enable_bits & 0x2)) { |
| 509 | vga.herc.mode_control |= 0x80; |
| 510 | vga.tandy.draw_base = &vga.mem.linear[32*1024]; |
| 511 | } |
| 512 | } |
| 513 | vga.draw.blinking = (val&0x20)!=0; |
| 514 | vga.herc.mode_control &= 0x82; |
| 515 | vga.herc.mode_control |= val & ~0x82; |
| 516 | break; |
| 517 | } |
| 518 | case 0x3bf: |
| 519 | vga.herc.enable_bits=(Bit8u)val; |
| 520 | break; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | /* static Bitu read_hercules(Bitu port,Bitu iolen) { |
| 525 | LOG_MSG("read from Herc port %x",port); |
| 526 | return 0; |
| 527 | } */ |
| 528 | |
| 529 | Bitu read_herc_status(Bitu /*port*/,Bitu /*iolen*/) { |
| 530 | // 3BAh (R): Status Register |
| 531 | // bit 0 Horizontal sync |
| 532 | // 1 Light pen status (only some cards) |
| 533 | // 3 Video signal |
| 534 | // 4-6 000: Hercules |
| 535 | // 001: Hercules Plus |
| 536 | // 101: Hercules InColor |
| 537 | // 111: Unknown clone |
| 538 | // 7 Vertical sync inverted |
| 539 | |
| 540 | double timeInFrame = PIC_FullIndex()-vga.draw.delay.framestart; |
| 541 | Bit8u retval=0x72; // Hercules ident; from a working card (Winbond W86855AF) |
| 542 | // Another known working card has 0x76 ("KeysoGood", full-length) |
| 543 | if (timeInFrame < vga.draw.delay.vrstart || |
| 544 | timeInFrame > vga.draw.delay.vrend) retval |= 0x80; |
| 545 | |
| 546 | double timeInLine=fmod(timeInFrame,vga.draw.delay.htotal); |
| 547 | if (timeInLine >= vga.draw.delay.hrstart && |
| 548 | timeInLine <= vga.draw.delay.hrend) retval |= 0x1; |
| 549 | |
| 550 | // 688 Attack sub checks bit 3 - as a workaround have the bit enabled |
| 551 | // if no sync active (corresponds to a completely white screen) |
| 552 | if ((retval&0x81)==0x80) retval |= 0x8; |
| 553 | return retval; |
| 554 | } |
| 555 | |
| 556 | |
| 557 | void VGA_SetupOther(void) { |
| 558 | Bitu i; |
| 559 | memset( &vga.tandy, 0, sizeof( vga.tandy )); |
| 560 | vga.attr.disabled = 0; |
| 561 | vga.config.bytes_skip=0; |
| 562 | |
| 563 | //Initialize values common for most machines, can be overwritten |
| 564 | vga.tandy.draw_base = vga.mem.linear; |
| 565 | vga.tandy.mem_base = vga.mem.linear; |
| 566 | vga.tandy.addr_mask = 8*1024 - 1; |
| 567 | vga.tandy.line_mask = 3; |
| 568 | vga.tandy.line_shift = 13; |
| 569 | |
| 570 | if (machine==MCH_CGA || IS_TANDY_ARCH) { |
| 571 | extern Bit8u int10_font_08[256 * 8]; |
| 572 | for (i=0;i<256;i++) memcpy(&vga.draw.font[i*32],&int10_font_08[i*8],8); |
| 573 | vga.draw.font_tables[0]=vga.draw.font_tables[1]=vga.draw.font; |
| 574 | } |
| 575 | if (machine==MCH_HERC) { |
| 576 | extern Bit8u int10_font_14[256 * 14]; |
| 577 | for (i=0;i<256;i++) memcpy(&vga.draw.font[i*32],&int10_font_14[i*14],14); |
| 578 | vga.draw.font_tables[0]=vga.draw.font_tables[1]=vga.draw.font; |
| 579 | MAPPER_AddHandler(CycleHercPal,MK_f11,0,"hercpal","Herc Pal"); |
| 580 | } |
| 581 | if (machine==MCH_CGA) { |
| 582 | IO_RegisterWriteHandler(0x3d8,write_cga,IO_MB); |
| 583 | IO_RegisterWriteHandler(0x3d9,write_cga,IO_MB); |
| 584 | IO_RegisterWriteHandler(0x3db,write_tandy,IO_MB); |
| 585 | IO_RegisterWriteHandler(0x3dc,write_tandy,IO_MB); |
| 586 | MAPPER_AddHandler(IncreaseHue,MK_f11,MMOD2,"inchue","Inc Hue"); |
| 587 | MAPPER_AddHandler(DecreaseHue,MK_f11,0,"dechue","Dec Hue"); |
| 588 | } |
| 589 | if (machine==MCH_TANDY) { |
| 590 | write_tandy( 0x3df, 0x0, 0 ); |
| 591 | IO_RegisterWriteHandler(0x3d8,write_tandy,IO_MB); |
| 592 | IO_RegisterWriteHandler(0x3d9,write_tandy,IO_MB); |
| 593 | IO_RegisterWriteHandler(0x3de,write_tandy,IO_MB); |
| 594 | IO_RegisterWriteHandler(0x3df,write_tandy,IO_MB); |
| 595 | IO_RegisterWriteHandler(0x3da,write_tandy,IO_MB); |
| 596 | } |
| 597 | if (machine==MCH_PCJR) { |
| 598 | //write_pcjr will setup base address |
| 599 | write_pcjr( 0x3df, 0x7 | (0x7 << 3), 0 ); |
| 600 | IO_RegisterWriteHandler(0x3d9,write_pcjr,IO_MB); |
| 601 | IO_RegisterWriteHandler(0x3da,write_pcjr,IO_MB); |
| 602 | IO_RegisterWriteHandler(0x3df,write_pcjr,IO_MB); |
| 603 | } |
| 604 | if (machine==MCH_HERC) { |
| 605 | Bitu base=0x3b0; |
| 606 | for (Bitu i = 0; i < 4; i++) { |
| 607 | // The registers are repeated as the address is not decoded properly; |
| 608 | // The official ports are 3b4, 3b5 |
| 609 | IO_RegisterWriteHandler(base+i*2,write_crtc_index_other,IO_MB); |
| 610 | IO_RegisterWriteHandler(base+i*2+1,write_crtc_data_other,IO_MB); |
| 611 | IO_RegisterReadHandler(base+i*2,read_crtc_index_other,IO_MB); |
| 612 | IO_RegisterReadHandler(base+i*2+1,read_crtc_data_other,IO_MB); |
| 613 | } |
| 614 | vga.herc.enable_bits=0; |
| 615 | vga.herc.mode_control=0xa; // first mode written will be text mode |
| 616 | vga.crtc.underline_location = 13; |
| 617 | IO_RegisterWriteHandler(0x3b8,write_hercules,IO_MB); |
| 618 | IO_RegisterWriteHandler(0x3bf,write_hercules,IO_MB); |
| 619 | IO_RegisterReadHandler(0x3ba,read_herc_status,IO_MB); |
| 620 | } |
| 621 | if (machine==MCH_CGA) { |
| 622 | Bitu base=0x3d0; |
| 623 | for (Bitu port_ct=0; port_ct<4; port_ct++) { |
| 624 | IO_RegisterWriteHandler(base+port_ct*2,write_crtc_index_other,IO_MB); |
| 625 | IO_RegisterWriteHandler(base+port_ct*2+1,write_crtc_data_other,IO_MB); |
| 626 | IO_RegisterReadHandler(base+port_ct*2,read_crtc_index_other,IO_MB); |
| 627 | IO_RegisterReadHandler(base+port_ct*2+1,read_crtc_data_other,IO_MB); |
| 628 | } |
| 629 | } |
| 630 | if (IS_TANDY_ARCH) { |
| 631 | Bitu base=0x3d4; |
| 632 | IO_RegisterWriteHandler(base,write_crtc_index_other,IO_MB); |
| 633 | IO_RegisterWriteHandler(base+1,write_crtc_data_other,IO_MB); |
| 634 | IO_RegisterReadHandler(base,read_crtc_index_other,IO_MB); |
| 635 | IO_RegisterReadHandler(base+1,read_crtc_data_other,IO_MB); |
| 636 | } |
| 637 | |
| 638 | } |