diff --git a/include/mem.h b/include/mem.h index 9723e830..23e8fcea 100644 --- a/include/mem.h +++ b/include/mem.h @@ -26,37 +26,27 @@ typedef Bit32u PhysPt; typedef Bit8u * HostPt; typedef Bit32u RealPt; -typedef Bit8u (*MEMORY_ReadHandler)(PhysPt pt); -typedef void (*MEMORY_WriteHandler)(PhysPt pt,Bit8u val); +typedef Bits MemHandle; -#define PAGE_KB 16 -#define PAGE_SIZE (PAGE_KB*1024) -#define PAGE_SHIFT 14 -#define PAGE_COUNT(A) (A & ((1 << PAGE_SHIFT)-1) ? 1+(A >> PAGE_SHIFT) : (A >> PAGE_SHIFT) ) - -extern HostPt ReadHostTable[]; -extern HostPt WriteHostTable[]; -extern MEMORY_ReadHandler ReadHandlerTable[]; -extern MEMORY_WriteHandler WriteHandlerTable[]; - - -INLINE Bit16u PAGES(Bit32u bytes) { - if ((bytes & 4095) == 0) return (Bit16u)(bytes>>12); - return (Bit16u)(1+(bytes>>12)); -} - -void MEM_SetupPageHandlers(Bitu startpage,Bitu pages,MEMORY_ReadHandler read,MEMORY_WriteHandler write); -void MEM_ClearPageHandlers(Bitu startpage,Bitu pages); - -void MEM_SetupMapping(Bitu startpage,Bitu pages,void * data); -void MEM_ClearMapping(Bitu startpage,Bitu pages); +#define MEM_PAGESIZE 4096 bool MEM_A20_Enabled(void); void MEM_A20_Enable(bool enable); -Bitu MEM_TotalSize(void); //Memory size in KB +/* Memory management / EMS mapping */ +HostPt MEM_GetBlockPage(void); +Bitu MEM_FreeTotal(void); //Free 4 kb pages +Bitu MEM_FreeLargest(void); //Largest free 4 kb pages block +Bitu MEM_TotalPages(void); //Total amount of 4 kb pages +MemHandle MEM_AllocatePages(Bitu pages,bool sequence); +PhysPt MEM_AllocatePage(void); +void MEM_ReleasePages(MemHandle handle); +bool MEM_ReAllocatePages(MemHandle & handle,Bitu pages,bool sequence); +void MEM_MapPages(Bitu phys_page,MemHandle mem,Bitu mem_page,Bitu pages); +void MEM_UnmapPages(Bitu phys_page,Bitu pages); -extern HostPt memory; + +MemHandle MEM_NextHandle(MemHandle handle); /* The folowing six functions are used everywhere in the end so these should be changed for @@ -88,9 +78,9 @@ INLINE void writed(HostPt off,Bit32u val) { off[3]=(Bit8u)(val >> 24); }; -#define MLEB(_MLE_VAL_) (_MLE_VAL_) -#define MLEW(_MLE_VAL_) ( (_MLE_VAL_ >> 8) | (_MLE_VAL_ << 8)) -#define MLED(_MLE_VAL_) ( (_MLE_VAL_ >> 24)|((_MLE_VAL_ >> 8)&0xFF00)|((_MLE_VAL_ << 8)&0xFF0000)|((_MLE_VAL_ << 24)&0xFF000000)) +#define MLEB(_MLE_VAL_) (_MLE_VAL_) +#define MLEW(_MLE_VAL_) ((_MLE_VAL_ >> 8) | (_MLE_VAL_ << 8)) +#define MLED(_MLE_VAL_) ((_MLE_VAL_ >> 24)|((_MLE_VAL_ >> 8)&0xFF00)|((_MLE_VAL_ << 8)&0xFF0000)|((_MLE_VAL_ << 24)&0xFF000000)) #else @@ -125,8 +115,6 @@ INLINE void writed(HostPt off,Bit32u val) { if (sizeof(VAR_)==4) VAR_=MLED(VAL_); /* The Folowing six functions are slower but they recognize the paged memory system */ -//TODO maybe make em inline to go a bit faster - Bit8u mem_readb(PhysPt pt); Bit16u mem_readw(PhysPt pt); @@ -136,63 +124,22 @@ void mem_writeb(PhysPt pt,Bit8u val); void mem_writew(PhysPt pt,Bit16u val); void mem_writed(PhysPt pt,Bit32u val); -INLINE void mem_writeb_inline(PhysPt pt,Bit8u val) { - if (WriteHostTable[pt >> PAGE_SHIFT]) writeb(WriteHostTable[pt >> PAGE_SHIFT]+pt,val); - else { - WriteHandlerTable[pt >> PAGE_SHIFT](pt,val); - } -} +void phys_writeb(PhysPt addr,Bit8u val); +void phys_writew(PhysPt addr,Bit16u val); +void phys_writed(PhysPt addr,Bit32u val); -INLINE void mem_writew_inline(PhysPt pt,Bit16u val) { - if (WriteHostTable[pt >> PAGE_SHIFT]) writew(WriteHostTable[pt >> PAGE_SHIFT]+pt,val); - else { - WriteHandlerTable[pt >> PAGE_SHIFT](pt+0,(Bit8u)(val & 0xff)); - WriteHandlerTable[pt >> PAGE_SHIFT](pt+1,(Bit8u)((val >> 8) & 0xff) ); - } -} - -INLINE void mem_writed_inline(PhysPt pt,Bit32u val) { - if (WriteHostTable[pt >> PAGE_SHIFT]) writed(WriteHostTable[pt >> PAGE_SHIFT]+pt,val); - else { - WriteHandlerTable[pt >> PAGE_SHIFT](pt+0,(Bit8u)(val & 0xff)); - WriteHandlerTable[pt >> PAGE_SHIFT](pt+1,(Bit8u)((val >> 8) & 0xff) ); - WriteHandlerTable[pt >> PAGE_SHIFT](pt+2,(Bit8u)((val >> 16) & 0xff) ); - WriteHandlerTable[pt >> PAGE_SHIFT](pt+3,(Bit8u)((val >> 24) & 0xff) ); - } -} - -INLINE Bit8u mem_readb_inline(PhysPt pt) { - if (ReadHostTable[pt >> PAGE_SHIFT]) return readb(ReadHostTable[pt >> PAGE_SHIFT]+pt); - else { - return ReadHandlerTable[pt >> PAGE_SHIFT](pt); - } -} - -INLINE Bit16u mem_readw_inline(PhysPt pt) { - if (ReadHostTable[pt >> PAGE_SHIFT]) return readw(ReadHostTable[pt >> PAGE_SHIFT]+pt); - else { - return - (ReadHandlerTable[pt >> PAGE_SHIFT](pt+0)) | - (ReadHandlerTable[pt >> PAGE_SHIFT](pt+1)) << 8; - } -} - -INLINE Bit32u mem_readd_inline(PhysPt pt){ - if (ReadHostTable[pt >> PAGE_SHIFT]) return readd(ReadHostTable[pt >> PAGE_SHIFT]+pt); - else { - return - (ReadHandlerTable[pt >> PAGE_SHIFT](pt+0)) | - (ReadHandlerTable[pt >> PAGE_SHIFT](pt+1)) << 8 | - (ReadHandlerTable[pt >> PAGE_SHIFT](pt+2)) << 16 | - (ReadHandlerTable[pt >> PAGE_SHIFT](pt+3)) << 24; - } -} +/* These don't check for alignment, better be sure it's correct */ +Bit32u phys_page_readd(Bitu page,Bitu off); void MEM_BlockWrite(PhysPt pt,void * data,Bitu size); void MEM_BlockRead(PhysPt pt,void * data,Bitu size); void MEM_BlockCopy(PhysPt dest,PhysPt src,Bitu size); void MEM_StrCopy(PhysPt pt,char * data,Bitu size); +void mem_memcpy(PhysPt dest,PhysPt src,Bitu size); +Bitu mem_strlen(PhysPt pt); +void mem_strcpy(PhysPt dest,PhysPt src); + /* The folowing functions are all shortcuts to the above functions using physical addressing */ INLINE Bit8u real_readb(Bit16u seg,Bit16u off) { @@ -215,13 +162,6 @@ INLINE void real_writed(Bit16u seg,Bit16u off,Bit32u val) { mem_writed(((seg<<4)+off),val); } -INLINE HostPt HostMake(Bit16u seg,Bit16u off) { - return memory+(seg<<4)+off; -} - -INLINE HostPt Phys2Host(PhysPt pt) { - return memory+pt; -} INLINE Bit16u RealSeg(RealPt pt) { return (Bit16u)(pt>>16); @@ -239,10 +179,6 @@ INLINE PhysPt PhysMake(Bit16u seg,Bit16u off) { return (seg<<4)+off; } -INLINE HostPt Real2Host(RealPt pt) { - return memory+(RealSeg(pt)<<4) +RealOff(pt); -} - INLINE RealPt RealMake(Bit16u seg,Bit16u off) { return (seg<<16)+off; } diff --git a/include/paging.h b/include/paging.h new file mode 100644 index 00000000..172612a9 --- /dev/null +++ b/include/paging.h @@ -0,0 +1,225 @@ +/* + * Copyright (C) 2002 The DOSBox Team + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef _PAGING_H_ +#define _PAGING_H_ + +#include "mem.h" + +class PageDirectory; +struct PageEntry; +struct PageLink; + +#define MEM_PAGE_SIZE (4096) +#define XMS_START (0x110) + +enum EntryTypes { //The type of memory contained in this link + ENTRY_VGA, + ENTRY_CHANGES, + ENTRY_INIT, + ENTRY_NA, + ENTRY_ROM, + ENTRY_LFB, + ENTRY_RAM, + ENTRY_ALLOC, +}; + +enum VGA_RANGES { + VGA_RANGE_A000, + VGA_RANGE_B000, + VGA_RANGE_B800, +}; + + +class PageChange { +public: + virtual void Changed(PageLink * link,Bitu start,Bitu end)=0; +}; + +/* Some other functions */ +void PAGING_Enable(bool enabled); +bool PAGING_Enabled(void); + +void MEM_CheckLinks(PageEntry * theentry); + +PageDirectory * MEM_DefaultDirectory(void); +Bitu PAGING_GetDirBase(void); +void PAGING_SetDirBase(Bitu cr3); + +PageLink * MEM_LinkPage(Bitu phys_page,PhysPt lin_base); + +void MEM_UnlinkPage(PageLink * link); +void MEM_SetLFB(Bitu _page,Bitu _pages,HostPt _pt); + +#pragma pack(1) +typedef struct { + Bit32u p:1; + Bit32u wr:1; + Bit32u us:1; + Bit32u pwt:1; + Bit32u pcd:1; + Bit32u a:1; + Bit32u d:1; + Bit32u pat:1; + Bit32u g:1; + Bit32u avl:3; + Bit32u base:20; +} X86_PageEntryBlock GCC_ATTRIBUTE(packed); +#pragma pack() + +union X86PageEntry { + Bit32u load; + X86_PageEntryBlock block; +}; + +struct PageLink { + HostPt read; + HostPt write; + PageChange * change; + PhysPt lin_base; + PageEntry * entry; + union { + PageDirectory * dir; + Bitu table; + } data; + PageLink * next; +}; + +struct PageEntry { + PageLink * links; + EntryTypes type; + union { + HostPt mem; + PhysPt vga_base; + PageDirectory * dir; + } data; + MemHandle next_handle; +}; + +class PageDirectory { +public: + PageDirectory(); + ~PageDirectory(); + void ClearDirectory(void); + void SetBase(PhysPt page); + void LinkPage(Bitu lin_page,Bitu phys_page); + bool InitPage(Bitu lin_address); + bool InitPageLinear(Bitu lin_address); + void InvalidateTable(Bitu table); + void InvalidateLink(Bitu table,Bitu index); + PageDirectory * next; + PageLink *links[1024*1024]; + PageLink *tables[1024]; + PageLink *link_dir; //Handler for main directory table + PageEntry entry_init; //Handler for pages that need init + PageLink link_init; //Handler for pages that need init + Bit32u base_page; //Base got from CR3 + PageChange * table_change; + PageChange * dir_change; +}; + +struct PagingBlock { + PageDirectory * cache; + PageDirectory * dir; + PageLink * free_link; + Bitu cr3; + bool enabled; +}; + +extern PagingBlock paging; + +/* Some support functions */ + +static INLINE PageLink * GetPageLink(PhysPt address) { + Bitu index=(address>>12); + return paging.dir->links[index]; +} + +void PAGING_AddFreePageLink(PageLink * link); + +PageLink * PAGING_GetFreePageLink(void); +void MEM_SetupVGA(VGA_RANGES range,HostPt base); + +/* Page Handler functions */ + +Bit8u ENTRY_readb(PageEntry * pentry,PhysPt address); +Bit16u ENTRY_readw(PageEntry * pentry,PhysPt address); +Bit32u ENTRY_readd(PageEntry * pentry,PhysPt address); +void ENTRY_writeb(PageEntry * pentry,PhysPt address,Bit8u val); +void ENTRY_writew(PageEntry * pentry,PhysPt address,Bit16u val); +void ENTRY_writed(PageEntry * pentry,PhysPt address,Bit32u val); + +/* Unaligned address handlers */ +Bit16u mem_unalignedreadw(PhysPt address); +Bit32u mem_unalignedreadd(PhysPt address); +void mem_unalignedwritew(PhysPt address,Bit16u val); +void mem_unalignedwrited(PhysPt address,Bit32u val); + +/* Special inlined memory reading/writing */ + +INLINE Bit8u mem_readb_inline(PhysPt address) { + PageLink * plink=GetPageLink(address); + + if (plink->read) return readb(plink->read+address); + else return ENTRY_readb(plink->entry,address); +} + +INLINE Bit16u mem_readw_inline(PhysPt address) { + if (address & 1) return mem_unalignedreadw(address); + PageLink * plink=GetPageLink(address); + + if (plink->read) return readw(plink->read+address); + else return ENTRY_readw(plink->entry,address); +} + + +INLINE Bit32u mem_readd_inline(PhysPt address) { + if (address & 3) return mem_unalignedreadd(address); + PageLink * plink=GetPageLink(address); + + if (plink->read) return readd(plink->read+address); + else return ENTRY_readd(plink->entry,address); +} + +INLINE void mem_writeb_inline(PhysPt address,Bit8u val) { + PageLink * plink=GetPageLink(address); + + if (plink->write) writeb(plink->write+address,val); + else ENTRY_writeb(plink->entry,address,val); +} + +INLINE void mem_writew_inline(PhysPt address,Bit16u val) { + if (address & 1) {mem_unalignedwritew(address,val);return;} + + PageLink * plink=GetPageLink(address); + + if (plink->write) writew(plink->write+address,val); + else ENTRY_writew(plink->entry,address,val); +} + +INLINE void mem_writed_inline(PhysPt address,Bit32u val) { + if (address & 3) {mem_unalignedwrited(address,val);return;} + + PageLink * plink=GetPageLink(address); + + if (plink->write) writed(plink->write+address,val); + else ENTRY_writed(plink->entry,address,val); +} + + +#endif \ No newline at end of file