DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
src/dos/dos_tables.cpp
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 #include "dosbox.h"
00021 #include "mem.h"
00022 #include "dos_inc.h"
00023 #include "callback.h"
00024 #include <assert.h>
00025 
00026 extern Bitu DOS_PRIVATE_SEGMENT_Size;
00027 
00028 void CALLBACK_DeAllocate(Bitu in);
00029 
00030 std::list<DOS_GetMemLog_Entry> DOS_GetMemLog;
00031 
00032 #ifdef _MSC_VER
00033 #pragma pack(1)
00034 #endif
00035 struct DOS_TableCase {  
00036         Bit16u size;
00037         Bit8u chars[256];
00038 }
00039 GCC_ATTRIBUTE (packed);
00040 #ifdef _MSC_VER
00041 #pragma pack ()
00042 #endif
00043 
00044 RealPt DOS_TableUpCase;
00045 RealPt DOS_TableLowCase;
00046 
00047 static Bitu call_casemap = 0;
00048 
00049 void DOS_Casemap_Free(void) {
00050     if (call_casemap != 0) {
00051         CALLBACK_DeAllocate(call_casemap);
00052         call_casemap = 0;
00053     }
00054 }
00055 
00056 static Bit16u dos_memseg=0;//DOS_PRIVATE_SEGMENT;
00057 
00058 extern Bitu VGA_BIOS_SEG_END;
00059 bool DOS_GetMemory_unmapped = false;
00060 
00061 void DOS_GetMemory_reset() {
00062         dos_memseg = 0;
00063 }
00064 
00065 void DOS_GetMemory_reinit() {
00066     DOS_GetMemory_unmapped = false;
00067     DOS_GetMemory_reset();
00068 }
00069 
00070 void DOS_GetMemory_unmap() {
00071         if (DOS_PRIVATE_SEGMENT != 0) {
00072                 LOG(LOG_MISC,LOG_DEBUG)("Unmapping DOS private segment 0x%04x-0x%04x",DOS_PRIVATE_SEGMENT,DOS_PRIVATE_SEGMENT_END-1u);
00073                 if (DOS_PRIVATE_SEGMENT >= 0xA000u) MEM_unmap_physmem((unsigned int)DOS_PRIVATE_SEGMENT<<4u,((unsigned int)DOS_PRIVATE_SEGMENT_END<<4u)-1u);
00074                 DOS_GetMemory_unmapped = true;
00075                 DOS_PRIVATE_SEGMENT_END = 0;
00076                 DOS_PRIVATE_SEGMENT = 0;
00077                 dos_memseg = 0;
00078         }
00079 }
00080 
00081 void DOS_GetMemory_Choose() {
00082         if (DOS_PRIVATE_SEGMENT == 0) {
00083         /* DOSBox-X non-compatible: Position ourself just past the VGA BIOS */
00084         /* NTS: Code has been arranged so that DOS kernel init follows BIOS INT10h init */
00085         DOS_PRIVATE_SEGMENT=VGA_BIOS_SEG_END;
00086         DOS_PRIVATE_SEGMENT_END=DOS_PRIVATE_SEGMENT + DOS_PRIVATE_SEGMENT_Size;
00087 
00088                 if (DOS_PRIVATE_SEGMENT >= 0xA000) {
00089                         memset(GetMemBase()+((unsigned int)DOS_PRIVATE_SEGMENT<<4u),0x00,(unsigned int)(DOS_PRIVATE_SEGMENT_END-DOS_PRIVATE_SEGMENT)<<4u);
00090                         MEM_map_RAM_physmem((unsigned int)DOS_PRIVATE_SEGMENT<<4u,((unsigned int)DOS_PRIVATE_SEGMENT_END<<4u)-1u);
00091                 }
00092 
00093                 LOG(LOG_MISC,LOG_DEBUG)("DOS private segment set to 0x%04x-0x%04x",DOS_PRIVATE_SEGMENT,DOS_PRIVATE_SEGMENT_END-1);
00094         }
00095 }
00096 
00097 Bit16u DOS_GetMemory(Bit16u pages,const char *who) {
00098         if (who == NULL) who = "";
00099         if (dos_memseg == 0) {
00100                 if (DOS_GetMemory_unmapped) E_Exit("DOS:Attempt to use DOS_GetMemory() when private area was unmapped by BOOT");
00101                 if (DOS_PRIVATE_SEGMENT == 0) DOS_GetMemory_Choose();
00102                 dos_memseg = DOS_PRIVATE_SEGMENT;
00103                 if (dos_memseg == 0) E_Exit("DOS:DOS_GetMemory() before private area has been initialized");
00104         }
00105 
00106         if (((Bitu)pages+(Bitu)dos_memseg) > DOS_PRIVATE_SEGMENT_END) {
00107                 LOG(LOG_DOSMISC,LOG_ERROR)("DOS_GetMemory(%u) failed for '%s' (alloc=0x%04x segment=0x%04x end=0x%04x)",
00108                         pages,who,dos_memseg,DOS_PRIVATE_SEGMENT,DOS_PRIVATE_SEGMENT_END);
00109                 E_Exit("DOS:Not enough memory for internal tables");
00110         }
00111         Bit16u page=dos_memseg;
00112         LOG(LOG_DOSMISC,LOG_DEBUG)("DOS_GetMemory(0x%04x pages,\"%s\") = 0x%04x",pages,who,page);
00113 
00114     {
00115         DOS_GetMemLog_Entry ent;
00116 
00117         ent.segbase = page;
00118         ent.pages = pages;
00119         ent.who = who;
00120 
00121         DOS_GetMemLog.push_back(ent);
00122     }
00123 
00124         dos_memseg+=pages;
00125         return page;
00126 }
00127 
00128 static Bitu DOS_CaseMapFunc(void) {
00129         //LOG(LOG_DOSMISC,LOG_ERROR)("Case map routine called : %c",reg_al);
00130         return CBRET_NONE;
00131 }
00132 
00133 static Bit8u country_info[0x22] = {
00134 /* Date format      */  0x00, 0x00,
00135 /* Currencystring   */  0x24, 0x00, 0x00, 0x00, 0x00,
00136 /* Thousands sep    */  0x2c, 0x00,
00137 /* Decimal sep      */  0x2e, 0x00,
00138 /* Date sep         */  0x2d, 0x00,
00139 /* time sep         */  0x3a, 0x00,
00140 /* currency form    */  0x00,
00141 /* digits after dec */  0x02,
00142 /* Time format      */  0x00,
00143 /* Casemap          */  0x00, 0x00, 0x00, 0x00,
00144 /* Data sep         */  0x2c, 0x00,
00145 /* Reservered 5     */  0x00, 0x00, 0x00, 0x00, 0x00,
00146 /* Reservered 5     */  0x00, 0x00, 0x00, 0x00, 0x00
00147 };
00148 
00149 extern bool enable_dbcs_tables;
00150 extern bool enable_filenamechar;
00151 extern bool enable_collating_uppercase;
00152 
00153 void DOS_SetupTables(void) {
00154         Bit16u seg;Bitu i;
00155         dos.tables.mediaid=RealMake(DOS_GetMemory(4,"dos.tables.mediaid"),0);
00156         dos.tables.tempdta=RealMake(DOS_GetMemory(4,"dos.tables.tempdta"),0);
00157         dos.tables.tempdta_fcbdelete=RealMake(DOS_GetMemory(4,"dos.tables.fcbdelete"),0);
00158         for (i=0;i<DOS_DRIVES;i++) mem_writew(Real2Phys(dos.tables.mediaid)+i*2,0);
00159         /* Create the DOS Info Block */
00160         dos_infoblock.SetLocation(DOS_INFOBLOCK_SEG); //c2woody
00161    
00162         /* create SDA */
00163         DOS_SDA(DOS_SDA_SEG,0).Init();
00164 
00165         /* Some weird files >20 detection routine */
00166         /* Possibly obselete when SFT is properly handled */
00167         real_writed(DOS_CONSTRING_SEG,0x0a,0x204e4f43);
00168         real_writed(DOS_CONSTRING_SEG,0x1a,0x204e4f43);
00169         real_writed(DOS_CONSTRING_SEG,0x2a,0x204e4f43);
00170 
00171         /* create a CON device driver */
00172         seg=DOS_CONDRV_SEG;
00173         real_writed(seg,0x00,0xffffffff);       // next ptr
00174         real_writew(seg,0x04,0x8013);           // attributes
00175         real_writed(seg,0x06,0xffffffff);       // strategy routine
00176         real_writed(seg,0x0a,0x204e4f43);       // driver name
00177         real_writed(seg,0x0e,0x20202020);       // driver name
00178         dos_infoblock.SetDeviceChainStart(RealMake(seg,0));
00179    
00180         /* Create a fake Current Directory Structure */
00181         seg=DOS_CDS_SEG;
00182         real_writed(seg,0x00,0x005c3a43);
00183         dos_infoblock.SetCurDirStruct(RealMake(seg,0));
00184 
00185 
00186 
00187         /* Allocate DCBS DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
00188         if (enable_dbcs_tables) {
00189                 dos.tables.dbcs=RealMake(DOS_GetMemory(12,"dos.tables.dbcs"),0);
00190                 mem_writed(Real2Phys(dos.tables.dbcs),0); //empty table
00191         }
00192         else {
00193                 dos.tables.dbcs=0;
00194         }
00195         /* FILENAME CHARACTER TABLE */
00196         if (enable_filenamechar) {
00197                 dos.tables.filenamechar=RealMake(DOS_GetMemory(2,"dos.tables.filenamechar"),0);
00198                 mem_writew(Real2Phys(dos.tables.filenamechar)+0x00,0x16);
00199                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x02,0x01);
00200                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x03,0x00);       // allowed chars from
00201                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x04,0xff);       // ...to
00202                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x05,0x00);
00203                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x06,0x00);       // excluded chars from
00204                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x07,0x20);       // ...to
00205                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x08,0x02);
00206                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x09,0x0e);       // number of illegal separators
00207                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x0a,0x2e);
00208                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x0b,0x22);
00209                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x0c,0x2f);
00210                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x0d,0x5c);
00211                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x0e,0x5b);
00212                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x0f,0x5d);
00213                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x10,0x3a);
00214                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x11,0x7c);
00215                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x12,0x3c);
00216                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x13,0x3e);
00217                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x14,0x2b);
00218                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x15,0x3d);
00219                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x16,0x3b);
00220                 mem_writeb(Real2Phys(dos.tables.filenamechar)+0x17,0x2c);
00221         }
00222         else {
00223                 dos.tables.filenamechar = 0;
00224         }
00225         /* COLLATING SEQUENCE TABLE + UPCASE TABLE*/
00226         // 256 bytes for col table, 128 for upcase, 4 for number of entries
00227         if (enable_collating_uppercase) {
00228                 dos.tables.collatingseq=RealMake(DOS_GetMemory(25,"dos.tables.collatingseq"),0);
00229                 mem_writew(Real2Phys(dos.tables.collatingseq),0x100);
00230                 for (i=0; i<256; i++) mem_writeb(Real2Phys(dos.tables.collatingseq)+i+2,i);
00231                 dos.tables.upcase=dos.tables.collatingseq+258;
00232                 mem_writew(Real2Phys(dos.tables.upcase),0x80);
00233                 for (i=0; i<128; i++) mem_writeb(Real2Phys(dos.tables.upcase)+i+2,0x80+i);
00234         }
00235         else {
00236                 dos.tables.collatingseq = 0;
00237                 dos.tables.upcase = 0;
00238         }
00239 
00240         /* Create a fake FCB SFT */
00241         seg=DOS_GetMemory(4,"Fake FCB SFT");
00242         real_writed(seg,0,0xffffffff);          //Last File Table
00243         real_writew(seg,4,100);                         //File Table supports 100 files
00244         dos_infoblock.SetFCBTable(RealMake(seg,0));
00245 
00246         /* Create a fake DPB */
00247         dos.tables.dpb=DOS_GetMemory(2,"dos.tables.dpb");
00248         for(Bitu d=0;d<26;d++) real_writeb(dos.tables.dpb,d,d);
00249 
00250         /* Create a fake disk buffer head */
00251         seg=DOS_GetMemory(6,"Fake disk buffer head");
00252         for (Bitu ct=0; ct<0x20; ct++) real_writeb(seg,ct,0);
00253         real_writew(seg,0x00,0xffff);           // forward ptr
00254         real_writew(seg,0x02,0xffff);           // backward ptr
00255         real_writeb(seg,0x04,0xff);                     // not in use
00256         real_writeb(seg,0x0a,0x01);                     // number of FATs
00257         real_writed(seg,0x0d,0xffffffff);       // pointer to DPB
00258         dos_infoblock.SetDiskBufferHeadPt(RealMake(seg,0));
00259 
00260         /* Set buffers to a nice value */
00261         dos_infoblock.SetBuffers(50,50);
00262 
00263         /* case map routine INT 0x21 0x38 */
00264         call_casemap = CALLBACK_Allocate();
00265         CALLBACK_Setup(call_casemap,DOS_CaseMapFunc,CB_RETF,"DOS CaseMap");
00266         /* Add it to country structure */
00267         host_writed(country_info + 0x12, CALLBACK_RealPointer(call_casemap));
00268         dos.tables.country=country_info;
00269 }
00270