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