DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
include/vga.h
00001  /*
00002  *  Copyright (C) 2002-2015  The DOSBox Team
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 
00020 #ifndef DOSBOX_VGA_H
00021 #define DOSBOX_VGA_H
00022 
00023 #ifndef DOSBOX_DOSBOX_H
00024 #include "dosbox.h"
00025 #endif
00026 #include <iostream>
00027 
00028 #define VGA_LFB_MAPPED
00029 
00030 class PageHandler;
00031 
00032 enum VGAModes {
00033         M_CGA2, M_CGA4,
00034         M_EGA, M_VGA,
00035         M_LIN4, M_LIN8, M_LIN15, M_LIN16, M_LIN24, M_LIN32,
00036         M_TEXT,
00037         M_HERC_GFX, M_HERC_TEXT,
00038         M_CGA16, M_TANDY2, M_TANDY4, M_TANDY16, M_TANDY_TEXT, M_AMSTRAD,
00039     M_PC98,
00040 
00041     M_FM_TOWNS,//STUB
00042 
00043         M_ERROR
00044 };
00045 
00046 enum VGA_Vsync {
00047         VS_Off,
00048         VS_On,
00049         VS_Force,
00050         VS_Host,
00051 };
00052 
00053 struct vsync_state {
00054         double period;
00055         bool manual;            // use manual vsync timing
00056         bool persistent;        // use persistent timer (to keep in sync even after internal mode switches)
00057         bool faithful;          // use faithful framerate adjustment
00058 };
00059 
00060 extern struct vsync_state vsync;
00061 extern float uservsyncjolt;
00062 
00063 #define CLK_25 25175u
00064 #define CLK_28 28322u
00065 
00066 #define MIN_VCO 180000u
00067 #define MAX_VCO 360000u
00068 
00069 #define S3_CLOCK_REF    14318u  /* KHz */
00070 #define S3_CLOCK(_M,_N,_R)      ((S3_CLOCK_REF * (((Bitu)_M) + 2ul)) / ((((Bitu)_N) + 2ul) * (1ul << ((Bitu)_R))))
00071 #define S3_MAX_CLOCK    150000u /* KHz */
00072 
00073 #define S3_XGA_1024             0x00u
00074 #define S3_XGA_1152             0x01u
00075 #define S3_XGA_640              0x40u
00076 #define S3_XGA_800              0x80u
00077 #define S3_XGA_1280             0xc0u
00078 #define S3_XGA_1600             0x81u
00079 #define S3_XGA_WMASK    (S3_XGA_640|S3_XGA_800|S3_XGA_1024|S3_XGA_1152|S3_XGA_1280)
00080 
00081 #define S3_XGA_8BPP  0x00u
00082 #define S3_XGA_16BPP 0x10u
00083 #define S3_XGA_32BPP 0x30u
00084 #define S3_XGA_CMASK (S3_XGA_8BPP|S3_XGA_16BPP|S3_XGA_32BPP)
00085 
00086 typedef struct {
00087         bool attrindex;
00088 } VGA_Internal;
00089 
00090 typedef struct {
00091 /* Memory handlers */
00092         Bitu mh_mask;
00093 
00094 /* Video drawing */
00095         Bitu display_start;
00096         Bitu real_start;
00097         bool retrace;                                   /* A retrace is active */
00098         Bitu scan_len;
00099         Bitu cursor_start;
00100 
00101 /* Some other screen related variables */
00102         Bitu line_compare;
00103         bool chained;                                   /* Enable or Disabled Chain 4 Mode */
00104         bool compatible_chain4;
00105 
00106         /* Pixel Scrolling */
00107         Bit8u pel_panning;                              /* Amount of pixels to skip when starting horizontal line */
00108         Bit8u hlines_skip;
00109         Bit8u bytes_skip;
00110         Bit8u addr_shift;
00111 
00112 /* Specific stuff memory write/read handling */
00113         
00114         Bit8u read_mode;
00115         Bit8u write_mode;
00116         Bit8u read_map_select;
00117         Bit8u color_dont_care;
00118         Bit8u color_compare;
00119         Bit8u data_rotate;
00120         Bit8u raster_op;
00121 
00122         Bit32u full_bit_mask;
00123         Bit32u full_map_mask;
00124         Bit32u full_not_map_mask;
00125         Bit32u full_set_reset;
00126         Bit32u full_not_enable_set_reset;
00127         Bit32u full_enable_set_reset;
00128         Bit32u full_enable_and_set_reset;
00129 } VGA_Config;
00130 
00131 typedef enum {
00132         LINE,
00133         EGALINE
00134 } Drawmode;
00135 
00136 typedef struct {
00137         bool resizing;
00138         Bitu width;
00139         Bitu height;
00140         Bitu blocks;
00141         Bitu address;
00142         Bitu panning;
00143         Bitu bytes_skip;
00144         Bit8u *linear_base;
00145         Bitu linear_mask;
00146     Bitu planar_mask;
00147         Bitu address_add;
00148         Bitu line_length;
00149         Bitu address_line_total;
00150         Bitu address_line;
00151         Bitu lines_total;
00152         Bitu vblank_skip;
00153         Bitu lines_done;
00154         Bitu split_line;
00155         Bitu byte_panning_shift;
00156     Bitu render_step,render_max;
00157         struct {
00158                 double framestart;
00159                 double vrstart, vrend;          // V-retrace
00160                 double hrstart, hrend;          // H-retrace
00161                 double hblkstart, hblkend;      // H-blanking
00162                 double vblkstart, vblkend;      // V-Blanking
00163                 double vdend, vtotal;
00164                 double hdend, htotal;
00165                 float singleline_delay;
00166         } delay;
00167         double screen_ratio;
00168         double refresh;
00169         Bit8u font[516*1024]; /* enlarged to 516KB for PC-98 character font data (256*16) + (128*2*128*16) */
00170         Bit8u * font_tables[2];
00171         Bitu blinking;
00172         bool blink;
00173         bool char9dot;
00174         struct {
00175                 Bitu address;
00176                 Bit8u sline,eline;
00177                 Bit8u count,delay;
00178                 Bit8u enabled;
00179         } cursor;
00180         Drawmode mode;
00181         bool has_split;
00182         bool vret_triggered;
00183         bool vga_override;
00184         bool doublescan_set;
00185         bool doublescan_effect;
00186         bool char9_set;
00187         Bitu bpp;
00188         double clock;
00189         Bit8u cga_snow[80];                     // one bit per horizontal column where snow should occur
00190 } VGA_Draw;
00191 
00192 typedef struct {
00193         Bit8u curmode;
00194         Bit16u originx, originy;
00195         Bit8u fstackpos, bstackpos;
00196         Bit8u forestack[4];
00197         Bit8u backstack[4];
00198         Bit16u startaddr;
00199         Bit8u posx, posy;
00200         Bit8u mc[64][64];
00201 } VGA_HWCURSOR;
00202 
00203 typedef struct {
00204         Bit8u reg_lock1;
00205         Bit8u reg_lock2;
00206         Bit8u reg_31;
00207         Bit8u reg_35;
00208         Bit8u reg_36; // RAM size
00209         Bit8u reg_3a; // 4/8/doublepixel bit in there
00210         Bit8u reg_40; // 8415/A functionality register
00211         Bit8u reg_41; // BIOS flags 
00212         Bit8u reg_42; // CR42 Mode Control
00213         Bit8u reg_43;
00214         Bit8u reg_45; // Hardware graphics cursor
00215         Bit8u reg_50;
00216         Bit8u reg_51;
00217         Bit8u reg_52;
00218         Bit8u reg_55;
00219         Bit8u reg_58;
00220         Bit8u reg_6b; // LFB BIOS scratchpad
00221         Bit8u ex_hor_overflow;
00222         Bit8u ex_ver_overflow;
00223         Bit16u la_window;
00224         Bit8u misc_control_2;
00225         Bit8u ext_mem_ctrl;
00226         Bitu xga_screen_width;
00227         VGAModes xga_color_mode;
00228         struct {
00229                 Bit8u r;
00230                 Bit8u n;
00231                 Bit8u m;
00232         } clk[4],mclk;
00233         struct {
00234                 Bit8u lock;
00235                 Bit8u cmd;
00236         } pll;
00237         VGA_HWCURSOR hgc;
00238 } VGA_S3;
00239 
00240 typedef struct {
00241         Bit8u mode_control;
00242         Bit8u enable_bits;
00243         bool blend;
00244 } VGA_HERC;
00245 
00246 typedef struct {
00247         Bit32u mask_plane;
00248         Bit8u write_plane;
00249         Bit8u read_plane;
00250         Bit8u border_color;
00251 } VGA_AMSTRAD;
00252 
00253 typedef struct {
00254         Bit8u index;
00255         Bit8u htotal;
00256         Bit8u hdend;
00257         Bit8u hsyncp;
00258         Bit8u hsyncw;
00259         Bit8u vtotal;
00260         Bit8u vdend;
00261         Bit8u vadjust;
00262         Bit8u vsyncp;
00263         Bit8u vsyncw;
00264         Bit8u max_scanline;
00265         Bit16u lightpen;
00266         bool lightpen_triggered;
00267         Bit8u cursor_start;
00268         Bit8u cursor_end;
00269 } VGA_OTHER;
00270 
00271 typedef struct {
00272         Bit8u pcjr_flipflop;
00273         Bit8u mode_control;
00274         Bit8u color_select;
00275         Bit8u disp_bank;
00276         Bit8u reg_index;
00277         Bit8u gfx_control;
00278         Bit8u palette_mask;
00279         Bit8u extended_ram;
00280         Bit8u border_color;
00281         Bit8u line_mask, line_shift;
00282         Bit8u draw_bank, mem_bank;
00283         Bit8u *draw_base, *mem_base;
00284         Bitu addr_mask;
00285 } VGA_TANDY;
00286 
00287 typedef struct {
00288         Bit8u index;
00289         Bit8u reset;
00290         Bit8u clocking_mode;
00291         Bit8u map_mask;
00292         Bit8u character_map_select;
00293         Bit8u memory_mode;
00294 } VGA_Seq;
00295 
00296 typedef struct {
00297         Bit8u palette[16];
00298         Bit8u mode_control;
00299         Bit8u horizontal_pel_panning;
00300         Bit8u overscan_color;
00301         Bit8u color_plane_enable;
00302         Bit8u color_select;
00303         Bit8u index;
00304         Bit8u disabled; // Used for disabling the screen.
00305                                         // Bit0: screen disabled by attribute controller index
00306                                         // Bit1: screen disabled by sequencer index 1 bit 5
00307                                         // These are put together in one variable for performance reasons:
00308                                         // the line drawing function is called maybe 60*480=28800 times/s,
00309                                         // and we only need to check one variable for zero this way.
00310 } VGA_Attr;
00311 
00312 typedef struct {
00313         Bit8u horizontal_total;
00314         Bit8u horizontal_display_end;
00315         Bit8u start_horizontal_blanking;
00316         Bit8u end_horizontal_blanking;
00317         Bit8u start_horizontal_retrace;
00318         Bit8u end_horizontal_retrace;
00319         Bit8u vertical_total;
00320         Bit8u overflow;
00321         Bit8u preset_row_scan;
00322         Bit8u maximum_scan_line;
00323         Bit8u cursor_start;
00324         Bit8u cursor_end;
00325         Bit8u start_address_high;
00326         Bit8u start_address_low;
00327         Bit8u cursor_location_high;
00328         Bit8u cursor_location_low;
00329         Bit8u vertical_retrace_start;
00330         Bit8u vertical_retrace_end;
00331         Bit8u vertical_display_end;
00332         Bit8u offset;
00333         Bit8u underline_location;
00334         Bit8u start_vertical_blanking;
00335         Bit8u end_vertical_blanking;
00336         Bit8u mode_control;
00337         Bit8u line_compare;
00338 
00339         Bit8u index;
00340         bool read_only;
00341 } VGA_Crtc;
00342 
00343 typedef struct {
00344         Bit8u index;
00345         Bit8u set_reset;
00346         Bit8u enable_set_reset;
00347         Bit8u color_compare;
00348         Bit8u data_rotate;
00349         Bit8u read_map_select;
00350         Bit8u mode;
00351         Bit8u miscellaneous;
00352         Bit8u color_dont_care;
00353         Bit8u bit_mask;
00354 } VGA_Gfx;
00355 
00356 typedef struct  {
00357         Bit8u red;
00358         Bit8u green;
00359         Bit8u blue;
00360 } RGBEntry;
00361 
00362 typedef struct {
00363         Bit8u bits;                                             /* DAC bits, usually 6 or 8 */
00364         Bit8u pel_mask;
00365         Bit8u pel_index;        
00366         Bit8u state;
00367         Bit8u write_index;
00368         Bit8u read_index;
00369         Bitu first_changed;
00370         Bit8u combine[16];
00371         RGBEntry rgb[0x100];
00372         Bit16u xlat16[256];
00373         Bit32u xlat32[256];
00374         Bit8u hidac_counter;
00375         Bit8u reg02;
00376 } VGA_Dac;
00377 
00378 typedef struct {
00379         Bitu    readStart, writeStart;
00380         Bitu    bankMask;
00381         Bitu    bank_read_full;
00382         Bitu    bank_write_full;
00383         Bit8u   bank_read;
00384         Bit8u   bank_write;
00385         Bitu    bank_size;
00386 } VGA_SVGA;
00387 
00388 typedef union {
00389         Bit32u d;
00390         Bit8u b[4];
00391 } VGA_Latch;
00392 
00393 typedef struct {
00394         Bit8u* linear;
00395         Bit8u* linear_orgptr;
00396 } VGA_Memory;
00397 
00398 typedef struct {
00399         //Add a few more just to be safe
00400         Bit8u*  map; /* allocated dynamically: [(VGA_MEMORY >> VGA_CHANGE_SHIFT) + 32] */
00401         Bit8u   checkMask, frame, writeMask;
00402         bool    active;
00403         Bit32u  clearMask;
00404         Bit32u  start, last;
00405         Bit32u  lastAddress;
00406 } VGA_Changes;
00407 
00408 typedef struct {
00409         Bit32u page;
00410         Bit32u addr;
00411         Bit32u mask;
00412         PageHandler *handler;
00413 } VGA_LFB;
00414 
00415 typedef struct {
00416         VGAModes mode;                                                          /* The mode the vga system is in */
00417         VGAModes lastmode;
00418         Bit8u misc_output;
00419         VGA_Draw draw;
00420         VGA_Config config;
00421         VGA_Internal internal;
00422 /* Internal module groups */
00423         VGA_Seq seq;
00424         VGA_Attr attr;
00425         VGA_Crtc crtc;
00426         VGA_Gfx gfx;
00427         VGA_Dac dac;
00428         VGA_Latch latch;
00429         VGA_S3 s3;
00430         VGA_SVGA svga;
00431         VGA_HERC herc;
00432         VGA_TANDY tandy;
00433         VGA_AMSTRAD amstrad;
00434         VGA_OTHER other;
00435         VGA_Memory mem;
00436         Bit32u vmemwrap; /* this is assumed to be power of 2 */
00437         Bit32u vmemsize;
00438     Bit32u vmemsize_alloced;
00439         VGA_LFB lfb;
00440 } VGA_Type;
00441 
00442 
00443 /* Hercules Palette function */
00444 void Herc_Palette(void);
00445 
00446 /* CGA Mono Palette function */
00447 void Mono_CGA_Palette(void);
00448 
00449 /* Functions for different resolutions */
00450 void VGA_SetMode(VGAModes mode);
00451 void VGA_DetermineMode(void);
00452 void VGA_SetupHandlers(void);
00453 void VGA_StartResize(Bitu delay=50);
00454 void VGA_SetupDrawing(Bitu val);
00455 void VGA_CheckScanLength(void);
00456 void VGA_ChangedBank(void);
00457 
00458 /* Some DAC/Attribute functions */
00459 void VGA_DAC_CombineColor(Bit8u attr,Bit8u pal);
00460 void VGA_DAC_SetEntry(Bitu entry,Bit8u red,Bit8u green,Bit8u blue);
00461 void VGA_ATTR_SetPalette(Bit8u index,Bit8u val);
00462 
00463 typedef enum {CGA, EGA, MONO} EGAMonitorMode;
00464 
00465 void VGA_ATTR_SetEGAMonitorPalette(EGAMonitorMode m);
00466 
00467 /* The VGA Subfunction startups */
00468 void VGA_SetupAttr(void);
00469 void VGA_SetupMemory(void);
00470 void VGA_SetupDAC(void);
00471 void VGA_SetupCRTC(void);
00472 void VGA_SetupMisc(void);
00473 void VGA_SetupGFX(void);
00474 void VGA_SetupSEQ(void);
00475 void VGA_SetupOther(void);
00476 void VGA_SetupXGA(void);
00477 
00478 /* Some Support Functions */
00479 void VGA_SetClock(Bitu which,Bitu target);
00480 void VGA_DACSetEntirePalette(void);
00481 void VGA_StartRetrace(void);
00482 void VGA_StartUpdateLFB(void);
00483 void VGA_SetBlinking(Bitu enabled);
00484 void VGA_SetCGA2Table(Bit8u val0,Bit8u val1);
00485 void VGA_SetCGA4Table(Bit8u val0,Bit8u val1,Bit8u val2,Bit8u val3);
00486 void VGA_ActivateHardwareCursor(void);
00487 void VGA_KillDrawing(void);
00488 
00489 void VGA_SetOverride(bool vga_override);
00490 
00491 extern VGA_Type vga;
00492 
00493 /* Support for modular SVGA implementation */
00494 /* Video mode extra data to be passed to FinishSetMode_SVGA().
00495    This structure will be in flux until all drivers (including S3)
00496    are properly separated. Right now it contains only three overflow
00497    fields in S3 format and relies on drivers re-interpreting those.
00498    For reference:
00499    ver_overflow:X|line_comp10|X|vretrace10|X|vbstart10|vdispend10|vtotal10
00500    hor_overflow:X|X|X|hretrace8|X|hblank8|hdispend8|htotal8
00501    offset is not currently used by drivers (useful only for S3 itself)
00502    It also contains basic int10 mode data - number, vtotal, htotal
00503    */
00504 typedef struct {
00505         Bit8u ver_overflow;
00506         Bit8u hor_overflow;
00507         Bitu offset;
00508         Bitu modeNo;
00509         Bitu htotal;
00510         Bitu vtotal;
00511 } VGA_ModeExtraData;
00512 
00513 // Vector function prototypes
00514 typedef void (*tWritePort)(Bitu reg,Bitu val,Bitu iolen);
00515 typedef Bitu (*tReadPort)(Bitu reg,Bitu iolen);
00516 typedef void (*tFinishSetMode)(Bitu crtc_base, VGA_ModeExtraData* modeData);
00517 typedef void (*tDetermineMode)();
00518 typedef void (*tSetClock)(Bitu which,Bitu target);
00519 typedef Bitu (*tGetClock)();
00520 typedef bool (*tHWCursorActive)();
00521 typedef bool (*tAcceptsMode)(Bitu modeNo);
00522 typedef void (*tSetupDAC)();
00523 typedef void (*tINT10Extensions)();
00524 
00525 struct SVGA_Driver {
00526         tWritePort write_p3d5;
00527         tReadPort read_p3d5;
00528         tWritePort write_p3c5;
00529         tReadPort read_p3c5;
00530         tWritePort write_p3c0;
00531         tReadPort read_p3c1;
00532         tWritePort write_p3cf;
00533         tReadPort read_p3cf;
00534 
00535         tFinishSetMode set_video_mode;
00536         tDetermineMode determine_mode;
00537         tSetClock set_clock;
00538         tGetClock get_clock;
00539         tHWCursorActive hardware_cursor_active;
00540         tAcceptsMode accepts_mode;
00541         tSetupDAC setup_dac;
00542         tINT10Extensions int10_extensions;
00543 };
00544 
00545 extern SVGA_Driver svga;
00546 extern int enableCGASnow;
00547 
00548 void SVGA_Setup_S3Trio(void);
00549 void SVGA_Setup_TsengET4K(void);
00550 void SVGA_Setup_TsengET3K(void);
00551 void SVGA_Setup_ParadisePVGA1A(void);
00552 void SVGA_Setup_Driver(void);
00553 
00554 // Amount of video memory required for a mode, implemented in int10_modes.cpp
00555 Bitu VideoModeMemSize(Bitu mode);
00556 
00557 extern Bit32u ExpandTable[256];
00558 extern Bit32u FillTable[16];
00559 extern Bit32u CGA_2_Table[16];
00560 extern Bit32u CGA_4_Table[256];
00561 extern Bit32u CGA_4_HiRes_Table[256];
00562 extern Bit32u CGA_16_Table[256];
00563 extern Bit32u TXT_Font_Table[16];
00564 extern Bit32u TXT_FG_Table[16];
00565 extern Bit32u TXT_BG_Table[16];
00566 extern Bit32u Expand16Table[4][16];
00567 extern Bit32u Expand16BigTable[0x10000];
00568 
00569 void VGA_DAC_UpdateColorPalette();
00570 
00571 extern uint32_t GFX_Rmask;
00572 extern unsigned char GFX_Rshift;
00573 
00574 extern uint32_t GFX_Gmask;
00575 extern unsigned char GFX_Gshift;
00576 
00577 extern uint32_t GFX_Bmask;
00578 extern unsigned char GFX_Bshift;
00579 
00580 extern uint32_t GFX_Amask;
00581 extern unsigned char GFX_Ashift;
00582 
00583 extern unsigned char GFX_bpp;
00584 
00585 #endif