| 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_crtc.cpp,v 1.34 2009-03-18 18:08:16 c2woody Exp $ */ |
| 20 |
|
| 21 |
#include <stdlib.h> |
| 22 |
#include "dosbox.h" |
| 23 |
#include "inout.h" |
| 24 |
#include "vga.h" |
| 25 |
#include "debug.h" |
| 26 |
#include "cpu.h" |
| 27 |
#include "video.h" |
| 28 |
#include "pic.h" |
| 29 |
|
| 30 |
#define crtc(blah) vga.crtc.blah |
| 31 |
|
| 32 |
|
| 33 |
void VGA_MapMMIO(void); |
| 34 |
void VGA_UnmapMMIO(void); |
| 35 |
|
| 36 |
void vga_write_p3d5(Bitu port,Bitu val,Bitu iolen); |
| 37 |
Bitu DEBUG_EnableDebugger(void); |
| 38 |
|
| 39 |
void vga_write_p3d4(Bitu port,Bitu val,Bitu iolen) { |
| 40 |
crtc(index)=val; |
| 41 |
} |
| 42 |
|
| 43 |
Bitu vga_read_p3d4(Bitu port,Bitu iolen) { |
| 44 |
return crtc(index); |
| 45 |
} |
| 46 |
|
| 47 |
void vga_write_p3d5(Bitu port,Bitu val,Bitu iolen) { |
| 48 |
// if (crtc(index)>0x18) LOG_MSG("VGA CRCT write %X to reg %X",val,crtc(index)); |
| 49 |
switch(crtc(index)) { |
| 50 |
case 0x00: /* Horizontal Total Register */ |
| 51 |
if (crtc(read_only)) break; |
| 52 |
crtc(horizontal_total)=val; |
| 53 |
/* 0-7 Horizontal Total Character Clocks-5 */ |
| 54 |
break; |
| 55 |
case 0x01: /* Horizontal Display End Register */ |
| 56 |
if (crtc(read_only)) break; |
| 57 |
if (val != crtc(horizontal_display_end)) { |
| 58 |
crtc(horizontal_display_end)=val; |
| 59 |
VGA_StartResize(); |
| 60 |
} |
| 61 |
/* 0-7 Number of Character Clocks Displayed -1 */ |
| 62 |
break; |
| 63 |
case 0x02: /* Start Horizontal Blanking Register */ |
| 64 |
if (crtc(read_only)) break; |
| 65 |
crtc(start_horizontal_blanking)=val; |
| 66 |
/* 0-7 The count at which Horizontal Blanking starts */ |
| 67 |
break; |
| 68 |
case 0x03: /* End Horizontal Blanking Register */ |
| 69 |
if (crtc(read_only)) break; |
| 70 |
crtc(end_horizontal_blanking)=val; |
| 71 |
/* |
| 72 |
0-4 Horizontal Blanking ends when the last 6 bits of the character |
| 73 |
counter equals this field. Bit 5 is at 3d4h index 5 bit 7. |
| 74 |
5-6 Number of character clocks to delay start of display after Horizontal |
| 75 |
Total has been reached. |
| 76 |
7 Access to Vertical Retrace registers if set. If clear reads to 3d4h |
| 77 |
index 10h and 11h access the Lightpen read back registers ?? |
| 78 |
*/ |
| 79 |
break; |
| 80 |
case 0x04: /* Start Horizontal Retrace Register */ |
| 81 |
if (crtc(read_only)) break; |
| 82 |
crtc(start_horizontal_retrace)=val; |
| 83 |
/* 0-7 Horizontal Retrace starts when the Character Counter reaches this value. */ |
| 84 |
break; |
| 85 |
case 0x05: /* End Horizontal Retrace Register */ |
| 86 |
if (crtc(read_only)) break; |
| 87 |
crtc(end_horizontal_retrace)=val; |
| 88 |
/* |
| 89 |
0-4 Horizontal Retrace ends when the last 5 bits of the character counter |
| 90 |
equals this value. |
| 91 |
5-6 Number of character clocks to delay start of display after Horizontal |
| 92 |
Retrace. |
| 93 |
7 bit 5 of the End Horizontal Blanking count (See 3d4h index 3 bit 0-4) |
| 94 |
*/ |
| 95 |
break; |
| 96 |
case 0x06: /* Vertical Total Register */ |
| 97 |
if (crtc(read_only)) break; |
| 98 |
if (val != crtc(vertical_total)) { |
| 99 |
crtc(vertical_total)=val; |
| 100 |
VGA_StartResize(); |
| 101 |
} |
| 102 |
/* 0-7 Lower 8 bits of the Vertical Total. Bit 8 is found in 3d4h index 7 |
| 103 |
bit 0. Bit 9 is found in 3d4h index 7 bit 5. |
| 104 |
Note: For the VGA this value is the number of scan lines in the display -2. |
| 105 |
*/ |
| 106 |
break; |
| 107 |
case 0x07: /* Overflow Register */ |
| 108 |
//Line compare bit ignores read only */ |
| 109 |
vga.config.line_compare=(vga.config.line_compare & 0x6ff) | (val & 0x10) << 4; |
| 110 |
if (crtc(read_only)) break; |
| 111 |
if ((vga.crtc.overflow ^ val) & 0xd6) { |
| 112 |
crtc(overflow)=val; |
| 113 |
VGA_StartResize(); |
| 114 |
} else crtc(overflow)=val; |
| 115 |
/* |
| 116 |
0 Bit 8 of Vertical Total (3d4h index 6) |
| 117 |
1 Bit 8 of Vertical Display End (3d4h index 12h) |
| 118 |
2 Bit 8 of Vertical Retrace Start (3d4h index 10h) |
| 119 |
3 Bit 8 of Start Vertical Blanking (3d4h index 15h) |
| 120 |
4 Bit 8 of Line Compare Register (3d4h index 18h) |
| 121 |
5 Bit 9 of Vertical Total (3d4h index 6) |
| 122 |
6 Bit 9 of Vertical Display End (3d4h index 12h) |
| 123 |
7 Bit 9 of Vertical Retrace Start (3d4h index 10h) |
| 124 |
*/ |
| 125 |
break; |
| 126 |
case 0x08: /* Preset Row Scan Register */ |
| 127 |
crtc(preset_row_scan)=val; |
| 128 |
vga.config.hlines_skip=val&31; |
| 129 |
if (IS_VGA_ARCH) vga.config.bytes_skip=(val>>5)&3; |
| 130 |
else vga.config.bytes_skip=0; |
| 131 |
// LOG_DEBUG("Skip lines %d bytes %d",vga.config.hlines_skip,vga.config.bytes_skip); |
| 132 |
/* |
| 133 |
0-4 Number of lines we have scrolled down in the first character row. |
| 134 |
Provides Smooth Vertical Scrolling.b |
| 135 |
5-6 Number of bytes to skip at the start of scanline. Provides Smooth |
| 136 |
Horizontal Scrolling together with the Horizontal Panning Register |
| 137 |
(3C0h index 13h). |
| 138 |
*/ |
| 139 |
break; |
| 140 |
case 0x09: /* Maximum Scan Line Register */ |
| 141 |
if (IS_VGA_ARCH) |
| 142 |
vga.config.line_compare=(vga.config.line_compare & 0x5ff)|(val&0x40)<<3; |
| 143 |
|
| 144 |
if (IS_VGA_ARCH && (svgaCard==SVGA_None) && (vga.mode==M_EGA || vga.mode==M_VGA)) { |
| 145 |
// in vgaonly mode we take special care of line repeats (excluding CGA modes) |
| 146 |
if ((vga.crtc.maximum_scan_line ^ val) & 0x20) { |
| 147 |
crtc(maximum_scan_line)=val; |
| 148 |
VGA_StartResize(); |
| 149 |
} else { |
| 150 |
crtc(maximum_scan_line)=val; |
| 151 |
} |
| 152 |
vga.draw.address_line_total = (val &0x1F) + 1; |
| 153 |
if (val&0x80) vga.draw.address_line_total*=2; |
| 154 |
} else { |
| 155 |
if ((vga.crtc.maximum_scan_line ^ val) & 0xbf) { |
| 156 |
crtc(maximum_scan_line)=val; |
| 157 |
VGA_StartResize(); |
| 158 |
} else { |
| 159 |
crtc(maximum_scan_line)=val; |
| 160 |
} |
| 161 |
} |
| 162 |
/* |
| 163 |
0-4 Number of scan lines in a character row -1. In graphics modes this is |
| 164 |
the number of times (-1) the line is displayed before passing on to |
| 165 |
the next line (0: normal, 1: double, 2: triple...). |
| 166 |
This is independent of bit 7, except in CGA modes which seems to |
| 167 |
require this field to be 1 and bit 7 to be set to work. |
| 168 |
5 Bit 9 of Start Vertical Blanking |
| 169 |
6 Bit 9 of Line Compare Register |
| 170 |
7 Doubles each scan line if set. I.e. displays 200 lines on a 400 display. |
| 171 |
*/ |
| 172 |
break; |
| 173 |
case 0x0A: /* Cursor Start Register */ |
| 174 |
crtc(cursor_start)=val; |
| 175 |
vga.draw.cursor.sline=val&0x1f; |
| 176 |
if (IS_VGA_ARCH) vga.draw.cursor.enabled=!(val&0x20); |
| 177 |
else vga.draw.cursor.enabled=true; |
| 178 |
/* |
| 179 |
0-4 First scanline of cursor within character. |
| 180 |
5 Turns Cursor off if set |
| 181 |
*/ |
| 182 |
break; |
| 183 |
case 0x0B: /* Cursor End Register */ |
| 184 |
crtc(cursor_end)=val; |
| 185 |
vga.draw.cursor.eline=val&0x1f; |
| 186 |
vga.draw.cursor.delay=(val>>5)&0x3; |
| 187 |
|
| 188 |
/* |
| 189 |
0-4 Last scanline of cursor within character |
| 190 |
5-6 Delay of cursor data in character clocks. |
| 191 |
*/ |
| 192 |
break; |
| 193 |
case 0x0C: /* Start Address High Register */ |
| 194 |
crtc(start_address_high)=val; |
| 195 |
vga.config.display_start=(vga.config.display_start & 0xFF00FF)| (val << 8); |
| 196 |
/* 0-7 Upper 8 bits of the start address of the display buffer */ |
| 197 |
break; |
| 198 |
case 0x0D: /* Start Address Low Register */ |
| 199 |
crtc(start_address_low)=val; |
| 200 |
vga.config.display_start=(vga.config.display_start & 0xFFFF00)| val; |
| 201 |
/* 0-7 Lower 8 bits of the start address of the display buffer */ |
| 202 |
break; |
| 203 |
case 0x0E: /*Cursor Location High Register */ |
| 204 |
crtc(cursor_location_high)=val; |
| 205 |
vga.config.cursor_start&=0xff00ff; |
| 206 |
vga.config.cursor_start|=val << 8; |
| 207 |
/* 0-7 Upper 8 bits of the address of the cursor */ |
| 208 |
break; |
| 209 |
case 0x0F: /* Cursor Location Low Register */ |
| 210 |
//TODO update cursor on screen |
| 211 |
crtc(cursor_location_low)=val; |
| 212 |
vga.config.cursor_start&=0xffff00; |
| 213 |
vga.config.cursor_start|=val; |
| 214 |
/* 0-7 Lower 8 bits of the address of the cursor */ |
| 215 |
break; |
| 216 |
case 0x10: /* Vertical Retrace Start Register */ |
| 217 |
crtc(vertical_retrace_start)=val; |
| 218 |
/* |
| 219 |
0-7 Lower 8 bits of Vertical Retrace Start. Vertical Retrace starts when |
| 220 |
the line counter reaches this value. Bit 8 is found in 3d4h index 7 |
| 221 |
bit 2. Bit 9 is found in 3d4h index 7 bit 7. |
| 222 |
*/ |
| 223 |
break; |
| 224 |
case 0x11: /* Vertical Retrace End Register */ |
| 225 |
crtc(vertical_retrace_end)=val; |
| 226 |
|
| 227 |
if (IS_EGAVGA_ARCH && !(val & 0x10)) { |
| 228 |
vga.draw.vret_triggered=false; |
| 229 |
if (GCC_UNLIKELY(machine==MCH_EGA)) PIC_DeActivateIRQ(9); |
| 230 |
} |
| 231 |
if (IS_VGA_ARCH) crtc(read_only)=(val & 128)>0; |
| 232 |
else crtc(read_only)=false; |
| 233 |
/* |
| 234 |
0-3 Vertical Retrace ends when the last 4 bits of the line counter equals |
| 235 |
this value. |
| 236 |
4 if clear Clears pending Vertical Interrupts. |
| 237 |
5 Vertical Interrupts (IRQ 2) disabled if set. Can usually be left |
| 238 |
disabled, but some systems (including PS/2) require it to be enabled. |
| 239 |
6 If set selects 5 refresh cycles per scanline rather than 3. |
| 240 |
7 Disables writing to registers 0-7 if set 3d4h index 7 bit 4 is not |
| 241 |
affected by this bit. |
| 242 |
*/ |
| 243 |
break; |
| 244 |
case 0x12: /* Vertical Display End Register */ |
| 245 |
if (val!=crtc(vertical_display_end)) { |
| 246 |
if (abs((Bits)val-(Bits)crtc(vertical_display_end))<3) { |
| 247 |
// delay small vde changes a bit to avoid screen resizing |
| 248 |
// if they are reverted in a short timeframe |
| 249 |
PIC_RemoveEvents(VGA_SetupDrawing); |
| 250 |
vga.draw.resizing=false; |
| 251 |
crtc(vertical_display_end)=val; |
| 252 |
VGA_StartResize(150); |
| 253 |
} else { |
| 254 |
crtc(vertical_display_end)=val; |
| 255 |
VGA_StartResize(); |
| 256 |
} |
| 257 |
} |
| 258 |
/* |
| 259 |
0-7 Lower 8 bits of Vertical Display End. The display ends when the line |
| 260 |
counter reaches this value. Bit 8 is found in 3d4h index 7 bit 1. |
| 261 |
Bit 9 is found in 3d4h index 7 bit 6. |
| 262 |
*/ |
| 263 |
break; |
| 264 |
case 0x13: /* Offset register */ |
| 265 |
crtc(offset)=val; |
| 266 |
vga.config.scan_len&=0x300; |
| 267 |
vga.config.scan_len|=val; |
| 268 |
VGA_CheckScanLength(); |
| 269 |
/* |
| 270 |
0-7 Number of bytes in a scanline / K. Where K is 2 for byte mode, 4 for |
| 271 |
word mode and 8 for Double Word mode. |
| 272 |
*/ |
| 273 |
break; |
| 274 |
case 0x14: /* Underline Location Register */ |
| 275 |
crtc(underline_location)=val; |
| 276 |
if (IS_VGA_ARCH) { |
| 277 |
//Byte,word,dword mode |
| 278 |
if ( crtc(underline_location) & 0x20 ) |
| 279 |
vga.config.addr_shift = 2; |
| 280 |
else if ( crtc( mode_control) & 0x40 ) |
| 281 |
vga.config.addr_shift = 0; |
| 282 |
else |
| 283 |
vga.config.addr_shift = 1; |
| 284 |
} else { |
| 285 |
vga.config.addr_shift = 1; |
| 286 |
} |
| 287 |
/* |
| 288 |
0-4 Position of underline within Character cell. |
| 289 |
5 If set memory address is only changed every fourth character clock. |
| 290 |
6 Double Word mode addressing if set |
| 291 |
*/ |
| 292 |
break; |
| 293 |
case 0x15: /* Start Vertical Blank Register */ |
| 294 |
if (val!=crtc(start_vertical_blanking)) { |
| 295 |
crtc(start_vertical_blanking)=val; |
| 296 |
VGA_StartResize(); |
| 297 |
} |
| 298 |
/* |
| 299 |
0-7 Lower 8 bits of Vertical Blank Start. Vertical blanking starts when |
| 300 |
the line counter reaches this value. Bit 8 is found in 3d4h index 7 |
| 301 |
bit 3. |
| 302 |
*/ |
| 303 |
break; |
| 304 |
case 0x16: /* End Vertical Blank Register */ |
| 305 |
crtc(end_vertical_blanking)=val; |
| 306 |
/* |
| 307 |
0-6 Vertical blanking stops when the lower 7 bits of the line counter |
| 308 |
equals this field. Some SVGA chips uses all 8 bits! |
| 309 |
*/ |
| 310 |
break; |
| 311 |
case 0x17: /* Mode Control Register */ |
| 312 |
crtc(mode_control)=val; |
| 313 |
vga.tandy.line_mask = (~val) & 3; |
| 314 |
//Byte,word,dword mode |
| 315 |
if ( crtc(underline_location) & 0x20 ) |
| 316 |
vga.config.addr_shift = 2; |
| 317 |
else if ( crtc( mode_control) & 0x40 ) |
| 318 |
vga.config.addr_shift = 0; |
| 319 |
else |
| 320 |
vga.config.addr_shift = 1; |
| 321 |
|
| 322 |
if ( vga.tandy.line_mask ) { |
| 323 |
vga.tandy.line_shift = 13; |
| 324 |
vga.tandy.addr_mask = (1 << 13) - 1; |
| 325 |
} else { |
| 326 |
vga.tandy.addr_mask = ~0; |
| 327 |
vga.tandy.line_shift = 0; |
| 328 |
} |
| 329 |
//Should we really need to do a determinemode here? |
| 330 |
// VGA_DetermineMode(); |
| 331 |
/* |
| 332 |
0 If clear use CGA compatible memory addressing system |
| 333 |
by substituting character row scan counter bit 0 for address bit 13, |
| 334 |
thus creating 2 banks for even and odd scan lines. |
| 335 |
1 If clear use Hercules compatible memory addressing system by |
| 336 |
substituting character row scan counter bit 1 for address bit 14, |
| 337 |
thus creating 4 banks. |
| 338 |
2 If set increase scan line counter only every second line. |
| 339 |
3 If set increase memory address counter only every other character clock. |
| 340 |
5 When in Word Mode bit 15 is rotated to bit 0 if this bit is set else |
| 341 |
bit 13 is rotated into bit 0. |
| 342 |
6 If clear system is in word mode. Addresses are rotated 1 position up |
| 343 |
bringing either bit 13 or 15 into bit 0. |
| 344 |
7 Clearing this bit will reset the display system until the bit is set again. |
| 345 |
*/ |
| 346 |
break; |
| 347 |
case 0x18: /* Line Compare Register */ |
| 348 |
crtc(line_compare)=val; |
| 349 |
vga.config.line_compare=(vga.config.line_compare & 0x700) | val; |
| 350 |
/* |
| 351 |
0-7 Lower 8 bits of the Line Compare. When the Line counter reaches this |
| 352 |
value, the display address wraps to 0. Provides Split Screen |
| 353 |
facilities. Bit 8 is found in 3d4h index 7 bit 4. |
| 354 |
Bit 9 is found in 3d4h index 9 bit 6. |
| 355 |
*/ |
| 356 |
break; |
| 357 |
default: |
| 358 |
if (svga.write_p3d5) { |
| 359 |
svga.write_p3d5(crtc(index), val, iolen); |
| 360 |
} else { |
| 361 |
LOG(LOG_VGAMISC,LOG_NORMAL)("VGA:CRTC:Write to unknown index %X",crtc(index)); |
| 362 |
} |
| 363 |
break; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
Bitu vga_read_p3d5(Bitu port,Bitu iolen) { |
| 368 |
// LOG_MSG("VGA CRCT read from reg %X",crtc(index)); |
| 369 |
switch(crtc(index)) { |
| 370 |
case 0x00: /* Horizontal Total Register */ |
| 371 |
return crtc(horizontal_total); |
| 372 |
case 0x01: /* Horizontal Display End Register */ |
| 373 |
return crtc(horizontal_display_end); |
| 374 |
case 0x02: /* Start Horizontal Blanking Register */ |
| 375 |
return crtc(start_horizontal_blanking); |
| 376 |
case 0x03: /* End Horizontal Blanking Register */ |
| 377 |
return crtc(end_horizontal_blanking); |
| 378 |
case 0x04: /* Start Horizontal Retrace Register */ |
| 379 |
return crtc(start_horizontal_retrace); |
| 380 |
case 0x05: /* End Horizontal Retrace Register */ |
| 381 |
return crtc(end_horizontal_retrace); |
| 382 |
case 0x06: /* Vertical Total Register */ |
| 383 |
return crtc(vertical_total); |
| 384 |
case 0x07: /* Overflow Register */ |
| 385 |
return crtc(overflow); |
| 386 |
case 0x08: /* Preset Row Scan Register */ |
| 387 |
return crtc(preset_row_scan); |
| 388 |
case 0x09: /* Maximum Scan Line Register */ |
| 389 |
return crtc(maximum_scan_line); |
| 390 |
case 0x0A: /* Cursor Start Register */ |
| 391 |
return crtc(cursor_start); |
| 392 |
case 0x0B: /* Cursor End Register */ |
| 393 |
return crtc(cursor_end); |
| 394 |
case 0x0C: /* Start Address High Register */ |
| 395 |
return crtc(start_address_high); |
| 396 |
case 0x0D: /* Start Address Low Register */ |
| 397 |
return crtc(start_address_low); |
| 398 |
case 0x0E: /*Cursor Location High Register */ |
| 399 |
return crtc(cursor_location_high); |
| 400 |
case 0x0F: /* Cursor Location Low Register */ |
| 401 |
return crtc(cursor_location_low); |
| 402 |
case 0x10: /* Vertical Retrace Start Register */ |
| 403 |
return crtc(vertical_retrace_start); |
| 404 |
case 0x11: /* Vertical Retrace End Register */ |
| 405 |
return crtc(vertical_retrace_end); |
| 406 |
case 0x12: /* Vertical Display End Register */ |
| 407 |
return crtc(vertical_display_end); |
| 408 |
case 0x13: /* Offset register */ |
| 409 |
return crtc(offset); |
| 410 |
case 0x14: /* Underline Location Register */ |
| 411 |
return crtc(underline_location); |
| 412 |
case 0x15: /* Start Vertical Blank Register */ |
| 413 |
return crtc(start_vertical_blanking); |
| 414 |
case 0x16: /* End Vertical Blank Register */ |
| 415 |
return crtc(end_vertical_blanking); |
| 416 |
case 0x17: /* Mode Control Register */ |
| 417 |
return crtc(mode_control); |
| 418 |
case 0x18: /* Line Compare Register */ |
| 419 |
return crtc(line_compare); |
| 420 |
default: |
| 421 |
if (svga.read_p3d5) { |
| 422 |
return svga.read_p3d5(crtc(index), iolen); |
| 423 |
} else { |
| 424 |
LOG(LOG_VGAMISC,LOG_NORMAL)("VGA:CRTC:Read from unknown index %X",crtc(index)); |
| 425 |
return 0x0; |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|