DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
include/bios.h
00001 /*
00002  *  Copyright (C) 2002-2020  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 along
00015  *  with this program; if not, write to the Free Software Foundation, Inc.,
00016  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 
00019 #include <stdio.h>
00020 #include <stddef.h>
00021 #include <stdint.h>
00022 
00023 #include "regionalloctracking.h"
00024 
00025 #ifndef DOSBOX_BIOS_H
00026 #define DOSBOX_BIOS_H
00027 
00028 #define BIOS_BASE_ADDRESS_COM1          0x400
00029 #define BIOS_BASE_ADDRESS_COM2          0x402
00030 #define BIOS_BASE_ADDRESS_COM3          0x404
00031 #define BIOS_BASE_ADDRESS_COM4          0x406
00032 #define BIOS_ADDRESS_LPT1               0x408
00033 #define BIOS_ADDRESS_LPT2               0x40a
00034 #define BIOS_ADDRESS_LPT3               0x40c
00035 /* 0x40e is reserved */
00036 #define BIOS_CONFIGURATION              0x410
00037 /* 0x412 is reserved */
00038 #define BIOS_MEMORY_SIZE                0x413
00039 #define BIOS_TRUE_MEMORY_SIZE           0x415
00040 /* #define bios_expansion_memory_size      (*(unsigned int   *) 0x415) */
00041 #define BIOS_KEYBOARD_STATE             0x417
00042 #define BIOS_KEYBOARD_FLAGS1            BIOS_KEYBOARD_STATE
00043 #define BIOS_KEYBOARD_FLAGS1_RSHIFT_PRESSED                     (1 << 0)
00044 #define BIOS_KEYBOARD_FLAGS1_LSHIFT_PRESSED                     (1 << 1)
00045 #define BIOS_KEYBOARD_FLAGS1_CTRL_PRESSED                       (1 << 2)
00046 #define BIOS_KEYBOARD_FLAGS1_ALT_PRESSED                        (1 << 3)
00047 #define BIOS_KEYBOARD_FLAGS1_SCROLL_LOCK_ACTIVE         (1 << 4)
00048 #define BIOS_KEYBOARD_FLAGS1_NUMLOCK_ACTIVE                     (1 << 5)
00049 #define BIOS_KEYBOARD_FLAGS1_CAPS_LOCK_ACTIVE           (1 << 6)
00050 #define BIOS_KEYBOARD_FLAGS1_INSERT_ACTIVE                      (1 << 7)
00051 
00052 #define BIOS_KEYBOARD_FLAGS2            0x418
00053 #define BIOS_KEYBOARD_FLAGS2_LCTRL_PRESSED                      (1 << 0)
00054 #define BIOS_KEYBOARD_FLAGS2_LALT_PRESSED                       (1 << 1)
00055 #define BIOS_KEYBOARD_FLAGS2_SYSTEMKEY_HELD                     (1 << 2)
00056 #define BIOS_KEYBOARD_FLAGS2_SUSPENDKEY_TOGGLED         (1 << 3)
00057 #define BIOS_KEYBOARD_FLAGS2_SCROLL_LOCK_PRESSED        (1 << 4)
00058 #define BIOS_KEYBOARD_FLAGS2_NUM_LOCK_PRESSED           (1 << 5)
00059 #define BIOS_KEYBOARD_FLAGS2_CAPS_LOCK_PRESSED          (1 << 6)
00060 #define BIOS_KEYBOARD_FLAGS2_INSERT_PRESSED                     (1 << 7)
00061 
00062 #define BIOS_KEYBOARD_TOKEN             0x419
00063 /* used for keyboard input with Alt-Number */
00064 #define BIOS_KEYBOARD_BUFFER_HEAD       0x41a
00065 #define BIOS_KEYBOARD_BUFFER_TAIL       0x41c
00066 #define BIOS_KEYBOARD_BUFFER            0x41e
00067 /* #define bios_keyboard_buffer            (*(unsigned int   *) 0x41e) */
00068 #define BIOS_DRIVE_ACTIVE               0x43e
00069 #define BIOS_DRIVE_RUNNING              0x43f
00070 #define BIOS_DISK_MOTOR_TIMEOUT         0x440
00071 #define BIOS_DISK_STATUS                0x441
00072 /* #define bios_fdc_result_buffer          (*(unsigned short *) 0x442) */
00073 #define BIOS_VIDEO_MODE                 0x449
00074 #define BIOS_SCREEN_COLUMNS             0x44a
00075 #define BIOS_VIDEO_MEMORY_USED          0x44c
00076 #define BIOS_VIDEO_MEMORY_ADDRESS       0x44e
00077 #define BIOS_VIDEO_CURSOR_POS           0x450
00078 
00079 
00080 #define BIOS_CURSOR_SHAPE               0x460
00081 #define BIOS_CURSOR_LAST_LINE           0x460
00082 #define BIOS_CURSOR_FIRST_LINE          0x461
00083 #define BIOS_CURRENT_SCREEN_PAGE        0x462
00084 #define BIOS_VIDEO_PORT                 0x463
00085 #define BIOS_VDU_CONTROL                0x465
00086 #define BIOS_VDU_COLOR_REGISTER         0x466
00087 /* 0x467-0x468 is reserved */
00088 #define BIOS_LAST_UNEXPECTED_IRQ        0x46b
00089 #define BIOS_TIMER                      0x46c
00090 #define BIOS_24_HOURS_FLAG              0x470
00091 #define BIOS_CTRL_BREAK_FLAG            0x471
00092 #define BIOS_CTRL_ALT_DEL_FLAG          0x472
00093 #define BIOS_HARDDISK_COUNT             0x475
00094 /* 0x474, 0x476, 0x477 is reserved */
00095 #define BIOS_LPT1_TIMEOUT               0x478
00096 #define BIOS_LPT2_TIMEOUT               0x479
00097 #define BIOS_LPT3_TIMEOUT               0x47a
00098 /* 0x47b is reserved */
00099 #define BIOS_COM1_TIMEOUT               0x47c
00100 #define BIOS_COM2_TIMEOUT               0x47d
00101 #define BIOS_COM3_TIMEOUT               0x47e
00102 #define BIOS_COM4_TIMEOUT               0x47f
00103 /* 0x47e is reserved */ //<- why that?
00104 /* 0x47f-0x4ff is unknow for me */
00105 #define BIOS_KEYBOARD_BUFFER_START      0x480
00106 #define BIOS_KEYBOARD_BUFFER_END        0x482
00107 
00108 #define BIOS_ROWS_ON_SCREEN_MINUS_1     0x484
00109 #define BIOS_FONT_HEIGHT                0x485
00110 
00111 #define BIOS_VIDEO_INFO_0               0x487
00112 #define BIOS_VIDEO_INFO_1               0x488
00113 #define BIOS_VIDEO_INFO_2               0x489
00114 #define BIOS_VIDEO_COMBO                0x48a
00115 
00116 #define BIOS_KEYBOARD_FLAGS3            0x496
00117 #define BIOS_KEYBOARD_FLAGS3_HIDDEN_E1                  (1 << 0)
00118 #define BIOS_KEYBOARD_FLAGS3_HIDDEN_E0                  (1 << 1)
00119 #define BIOS_KEYBOARD_FLAGS3_RCTRL_PRESSED              (1 << 2)
00120 #define BIOS_KEYBOARD_FLAGS3_RALT_PRESSED               (1 << 3)
00121 #define BIOS_KEYBOARD_FLAGS3_ENHANCED_KEYBOARD  (1 << 4)
00122 #define BIOS_KEYBOARD_FLAGS3_NUM_LOCK_FORCED    (1 << 5)
00123 #define BIOS_KEYBOARD_FLAGS3_ID_CHAR_WAS_LAST   (1 << 6)
00124 #define BIOS_KEYBOARD_FLAGS3_ID_READ_IN_PROCESS (1 << 7)
00125 
00126 #define BIOS_KEYBOARD_LEDS              0x497
00127 #define BIOS_KEYBOARD_LEDS_SCROLL_LOCK    (1 << 0)
00128 #define BIOS_KEYBOARD_LEDS_NUM_LOCK       (1 << 1)
00129 #define BIOS_KEYBOARD_LEDS_CAPS_LOCK      (1 << 2)
00130 #define BIOS_KEYBOARD_LEDS_CIRCUS         (1 << 3)
00131 #define BIOS_KEYBOARD_LEDS_ACK            (1 << 4)
00132 #define BIOS_KEYBOARD_LEDS_RESEND         (1 << 5)
00133 #define BIOS_KEYBOARD_LEDS_MODE           (1 << 6)
00134 #define BIOS_KEYBOARD_LEDS_TRANSMIT_ERROR (1 << 7)
00135 
00136 #define BIOS_WAIT_FLAG_POINTER          0x498
00137 #define BIOS_WAIT_FLAG_COUNT            0x49c           
00138 #define BIOS_WAIT_FLAG_ACTIVE                   0x4a0
00139 #define BIOS_WAIT_FLAG_TEMP                             0x4a1
00140 
00141 
00142 #define BIOS_PRINT_SCREEN_FLAG          0x500
00143 
00144 #define BIOS_VIDEO_SAVEPTR              0x4a8
00145 
00146 #define CURSOR_SCAN_LINE_NORMAL                 (0x6)
00147 #define CURSOR_SCAN_LINE_INSERT                 (0x4)
00148 #define CURSOR_SCAN_LINE_END                    (0x7)
00149 
00150 //#define BIOS_DEFAULT_IRQ0_LOCATION            (RealMake(0xf000,0xfea5))
00151 //#define BIOS_DEFAULT_IRQ1_LOCATION            (RealMake(0xf000,0xe987))
00152 //#define BIOS_DEFAULT_IRQ2_LOCATION            (RealMake(0xf000,0xff55))
00153 //#define BIOS_DEFAULT_HANDLER_LOCATION         (RealMake(0xf000,0xff53))
00154 //#define BIOS_VIDEO_TABLE_LOCATION             (RealMake(0xf000,0xf0a4))
00155 //#define BIOS_DEFAULT_RESET_LOCATION           (RealMake(0xf000,0xe05b))
00156 
00157 extern Bitu BIOS_DEFAULT_IRQ0_LOCATION;         // (RealMake(0xf000,0xfea5))
00158 extern Bitu BIOS_DEFAULT_IRQ1_LOCATION;         // (RealMake(0xf000,0xe987))
00159 extern Bitu BIOS_DEFAULT_IRQ2_LOCATION;         // (RealMake(0xf000,0xff55))
00160 extern Bitu BIOS_DEFAULT_HANDLER_LOCATION;      // (RealMake(0xf000,0xff53))
00161 extern Bitu BIOS_DEFAULT_INT5_LOCATION;         // (RealMake(0xf000,0xff54))
00162 extern Bitu BIOS_VIDEO_TABLE_LOCATION;          // (RealMake(0xf000,0xf0a4))
00163 extern Bitu BIOS_DEFAULT_RESET_LOCATION;        // RealMake(0xf000,0xe05b)
00164 extern Bitu BIOS_VIDEO_TABLE_SIZE;
00165 
00166 Bitu ROMBIOS_GetMemory(Bitu bytes,const char *who=NULL,Bitu alignment=1,Bitu must_be_at=0);
00167 
00168 extern RegionAllocTracking rombios_alloc;
00169 
00170 /* maximum of scancodes handled by keyboard bios routines */
00171 #define MAX_SCAN_CODE 0x58
00172 
00173 /* The Section handling Bios Disk Access */
00174 //#define BIOS_MAX_DISK 10
00175 
00176 //#define MAX_SWAPPABLE_DISKS 20
00177 
00178 void BIOS_ZeroExtendedSize(bool in);
00179 
00180 bool BIOS_AddKeyToBuffer(Bit16u code);
00181 
00182 void INT10_ReloadRomFonts();
00183 
00184 void BIOS_SetLPTPort (Bitu port, Bit16u baseaddr);
00185 
00186 // \brief Synchronizes emulator num lock state with host.
00187 void BIOS_SynchronizeNumLock();
00188 
00189 // \brief Synchronizes emulator caps lock state with host.
00190 void BIOS_SynchronizeCapsLock();
00191 
00192 // \brief Synchronizes emulator scroll lock state with host.
00193 void BIOS_SynchronizeScrollLock();
00194 
00195 bool ISAPNP_RegisterSysDev(const unsigned char *raw,Bitu len,bool already=false);
00196 
00197 enum {
00198     UNHANDLED_IRQ_SIMPLE=0,
00199     UNHANDLED_IRQ_COOPERATIVE_2ND       // PC-98 type IRQ 8-15 handling
00200 };
00201 
00202 extern int unhandled_irq_method;
00203 
00204 class ISAPnPDevice {
00205 public:
00206         ISAPnPDevice();
00207         virtual ~ISAPnPDevice();
00208 public:
00209         void checksum_ident();
00210 public:
00211         enum SmallTags {
00212                 PlugAndPlayVersionNumber =              0x1,
00213                 LogicalDeviceID =                       0x2,
00214                 CompatibleDeviceID =                    0x3,
00215                 IRQFormat =                             0x4,
00216                 DMAFormat =                             0x5,
00217                 StartDependentFunctions =               0x6,
00218                 EndDependentFunctions =                 0x7,
00219                 IOPortDescriptor =                      0x8,
00220                 FixedLocationIOPortDescriptor =         0x9,
00221                 SmallVendorDefined =                    0xE,
00222                 EndTag =                                0xF
00223         };
00224         enum LargeTags {
00225                 MemoryRangeDescriptor =                 0x1,
00226                 IdentifierStringANSI =                  0x2,
00227                 IdentifierStringUNICODE =               0x3,
00228                 LargeVendorDefined =                    0x4,
00229                 MemoryRange32Descriptor =               0x5,
00230                 FixedLocationMemoryRangeDescriptor =    0x6
00231         };
00232         // StartDependentFunction config
00233         enum DependentFunctionConfig {
00234                 PreferredDependentConfiguration =       0x0,
00235                 AcceptableDependentConfiguration =      0x1,
00236                 SubOptimalDependentConfiguration =      0x2
00237         };
00238         // IRQ format, signal types (bitfield)
00239         enum {
00240                 IRQFormatInfo_HighTrueEdgeSensitive =   0x1,
00241                 IRQFormatInfo_LowTrueEdgeSensitive =    0x2,
00242                 IRQFormatInfo_HighTrueLevelSensitive =  0x4,
00243                 IRQFormatInfo_LowTrueLevelSensitive =   0x8
00244         };
00245         // IRQ format, helper IRQ mask generator
00246         static inline uint16_t irq2mask(const int IRQ) {
00247                 if (IRQ < 0 || IRQ > 15) return 0;
00248                 return (uint16_t)(1U << (unsigned char)IRQ);
00249         }
00250         static inline uint16_t irq2mask(const int a,const int b) {
00251                 return irq2mask(a) | irq2mask(b);
00252         }
00253         static inline uint16_t irq2mask(const int a,const int b,const int c) {
00254                 return irq2mask(a) | irq2mask(b) | irq2mask(c);
00255         }
00256         static inline uint16_t irq2mask(const int a,const int b,const int c,const int d) {
00257                 return irq2mask(a) | irq2mask(b) | irq2mask(c) | irq2mask(d);
00258         }
00259         static inline uint16_t irq2mask(const int a,const int b,const int c,const int d,const int e) {
00260                 return irq2mask(a) | irq2mask(b) | irq2mask(c) | irq2mask(d) | irq2mask(e);
00261         }
00262         // DMA format transfer type
00263         enum {
00264                 DMATransferType_8bitOnly=0,
00265                 DMATransferType_8_and_16bit=1,
00266                 DMATransferType_16bitOnly=2
00267         };
00268         enum {
00269                 DMASpeedSupported_Compat=0,
00270                 DMASpeedSupported_TypeA=1,
00271                 DMASpeedSupported_TypeB=2,
00272                 DMASpeedSupported_TypeF=3
00273         };
00274         // DMA format
00275         static inline uint16_t dma2mask(const int DMA) {
00276                 if (DMA < 0 || DMA > 7 || DMA == 4) return 0;
00277                 return (uint16_t)(1U << (unsigned char)DMA);
00278         }
00279         static inline uint16_t dma2mask(const int a,const int b) {
00280                 return dma2mask(a) | dma2mask(b);
00281         }
00282         static inline uint16_t dma2mask(const int a,const int b,const int c) {
00283                 return dma2mask(a) | dma2mask(b) | dma2mask(c);
00284         }
00285         static inline uint16_t dma2mask(const int a,const int b,const int c,const int d) {
00286                 return dma2mask(a) | dma2mask(b) | dma2mask(c) | dma2mask(d);
00287         }
00288         static inline uint16_t dma2mask(const int a,const int b,const int c,const int d,const int e) {
00289                 return dma2mask(a) | dma2mask(b) | dma2mask(c) | dma2mask(d) | dma2mask(e);
00290         }
00291 public:
00292         virtual void config(Bitu val);
00293         virtual void wakecsn(Bitu val);
00294         virtual void select_logical_device(Bitu val);
00295         virtual void on_pnp_key();
00296         /* ISA PnP I/O data read/write */
00297         virtual uint8_t read(Bitu addr);
00298         virtual void write(Bitu addr,Bitu val);
00299         virtual bool alloc(size_t sz);
00300         void write_begin_SMALLTAG(const ISAPnPDevice::SmallTags stag,unsigned char len);
00301         void write_begin_LARGETAG(const ISAPnPDevice::LargeTags stag,unsigned int len);
00302         void write_nstring(const char *str,const size_t l);
00303         void write_byte(const unsigned char c);
00304         void begin_write_res();
00305         void end_write_res();
00306         void write_END();
00307         void write_ISAPnP_version(unsigned char major,unsigned char minor,unsigned char vendor);
00308         void write_Identifier_String(const char *str);
00309         void write_Logical_Device_ID(const char c1,const char c2,const char c3,const char c4,const char c5,const char c6,const char c7);
00310         void write_Compatible_Device_ID(const char c1,const char c2,const char c3,const char c4,const char c5,const char c6,const char c7);
00311         void write_Device_ID(const char c1,const char c2,const char c3,const char c4,const char c5,const char c6,const char c7);
00312         void write_Dependent_Function_Start(const ISAPnPDevice::DependentFunctionConfig cfg,const bool force=false);
00313         void write_IRQ_Format(const uint16_t IRQ_mask,const unsigned char IRQ_signal_type=0);
00314         void write_DMA_Format(const uint8_t DMA_mask,const unsigned char transfer_type_preference,const bool is_bus_master,const bool byte_mode,const bool word_mode,const unsigned char speed_supported);
00315         void write_IO_Port(const uint16_t min_port,const uint16_t max_port,const uint8_t count,const uint8_t alignment=1,const bool full16bitdecode=true);
00316         void write_End_Dependent_Functions();
00317 public:
00318     unsigned char       CSN = 0;
00319     unsigned char       logical_device = 0;
00320         unsigned char           ident[9];               /* 72-bit vendor + serial + checksum identity */
00321     unsigned char       ident_bp = 0;   /* bit position of identity read */
00322     unsigned char       ident_2nd = 0;
00323     unsigned char       resource_ident = 0;
00324     unsigned char*      resource_data = NULL;
00325     size_t              resource_data_len = 0;
00326     unsigned int        resource_data_pos = 0;
00327     size_t              alloc_write = 0;
00328     unsigned char*      alloc_res = NULL;
00329     size_t              alloc_sz = 0;
00330 };
00331 
00332 /* abc = ASCII letters of the alphabet
00333  * defg = hexadecimal digits */
00334 #define ISAPNP_ID(a,b,c,d,e,f,g) \
00335          (((a)&0x1F)    <<  2) | \
00336          (((b)&0x1F)    >>  3) | \
00337         ((((b)&0x1F)&7) << 13) | \
00338          (((c)&0x1F)    <<  8) | \
00339          (((d)&0x1F)    << 20) | \
00340          (((e)&0x1F)    << 16) | \
00341          (((f)&0x1F)    << 28) | \
00342          (((g)&0x1F)    << 24)
00343 
00344 #define ISAPNP_TYPE(a,b,c) (a),(b),(c)
00345 #define ISAPNP_SMALL_TAG(t,l) (((t) << 3) | (l))
00346 
00347 #define ISAPNP_SYSDEV_HEADER(id,type,ctrl) \
00348         ( (id)        & 0xFF), \
00349         (((id) >>  8) & 0xFF), \
00350         (((id) >> 16) & 0xFF), \
00351         (((id) >> 24) & 0xFF), \
00352         type, \
00353         ( (ctrl)       & 0xFF), \
00354         (((ctrl) >> 8) & 0xFF)
00355 
00356 #define ISAPNP_IO_RANGE(info,min,max,align,len) \
00357         ISAPNP_SMALL_TAG(8,7), \
00358         (info), \
00359         ((min) & 0xFF), (((min) >> 8) & 0xFF), \
00360         ((max) & 0xFF), (((max) >> 8) & 0xFF), \
00361         (align), \
00362         (len)
00363 
00364 #define ISAPNP_IRQ_SINGLE(irq,flags) \
00365         ISAPNP_SMALL_TAG(4,3), \
00366         ((1 << (irq)) & 0xFF), \
00367         (((1 << (irq)) >> 8) & 0xFF), \
00368         (flags)
00369 
00370 #define ISAPNP_IRQ(irqmask,flags) \
00371         ISAPNP_SMALL_TAG(4,3), \
00372         ((irqmask) & 0xFF), \
00373         (((irqmask) >> 8) & 0xFF), \
00374         (flags)
00375 
00376 #define ISAPNP_DMA_SINGLE(dma,flags) \
00377         ISAPNP_SMALL_TAG(5,2), \
00378         ((1 << (dma)) & 0xFF), \
00379         (flags)
00380 
00381 #define ISAPNP_DMA(dma,flags) \
00382         ISAPNP_SMALL_TAG(5,2), \
00383         ((dma) & 0xFF), \
00384         (flags)
00385 
00386 #define ISAPNP_END \
00387         ISAPNP_SMALL_TAG(0xF,1), 0x00
00388 
00389 void ISA_PNP_devreg(ISAPnPDevice *x);
00390 #endif