IBMPC::Memory Class Reference

#include <ibmpc.h>

List of all members.


Detailed Description

Handles accesses to memory and memory mapped virtual devices.

Also provides simple per-page memory protection mechanism.

Implementation
Maintains memory page descriptors array where each page can use either buffer of real memory or virtual Device driver to access data.
Protection
Protection is implemented by storing 32-bit "flags" field to each page descriptor which includes bits such as PERM_READABLE and PERM_WRITEABLE. These flags are checked before each memory access (be it to real memory or device driver attachment).
In case of protection fault special "state" bitmask will be updated to indicate error. This bitmask can then be retrieved using get_state() call.
Note:
The state bitmask is reset each memory read/write operation, so it should be checked immediately after performing the operation.

In case of error operation will NOT be performed (memory will be unaffected, and if driver is handling the page it will *not* be called).

READ operations will return 0 in case of error.

Real memory emulation
The flags also include the PAGE_ALLOCATED bit which distinguishes the page is using real memory or accesses to it are handled by one of the devices. This bit is automatically set when page_alloc() is called on the page. It can be checked with page_get_perm() function, but should NEVER be set using page_set_perm()!

Definition at line 62 of file ibmpc.h.

Public Types

 PERM_READABLE = 0x0001
 This memory page is readable.
 PERM_WRITEABLE = 0x0002
 This memory page is writeable.
 PAGE_ALLOCATED = 0x8000
 Block of memory for this page's storage was allocated by the manager.
 STATE_FAULT = 0x0001
 0:last operation succeeded; 1:failed
 STATE_DIR = 0x0002
 0:last operation was READ, 1:WRITE
enum  perm_t { PERM_READABLE = 0x0001, PERM_WRITEABLE = 0x0002, PAGE_ALLOCATED = 0x8000 }
 Memory page permissions bitmask. More...
enum  state_t { STATE_FAULT = 0x0001, STATE_DIR = 0x0002 }
 Memory bus state bitmask (used to check for error in last operation). More...

Public Member Functions

 Memory ()
 Construct instance of Memory manager.
 ~Memory ()
 Destruct instance of Memory manager.
bool alloc_pagetable (unsigned int pages_count_pow, unsigned int page_size_pow)
 Allocate/Reallocate memory page table.
unsigned int get_page_size ()
void page_set_perm (unsigned int page_index, uint32 perm)
 Set permissions for memory page (also see enum perm_t).
uint32 page_get_perm (unsigned int page_index)
 Retrieve permissions of given memory page.
bool page_alloc (unsigned int page_index)
 Allocate real memory for the memory page.
void page_attach (unsigned int page_index, Device *driver)
 Attach device driver to handle accesses to the memory page.
void page_free (unsigned int page_index)
 Free real memory block allocated for this memory page or detach page from Device driver.
uint8 mem_read8 (unsigned int addr)
 Load 8 bits from memory at given "flat" address.
void mem_write8 (unsigned int addr, uint8 data)
 Store 8 bits to memory at given "flat" address.
uint32 get_state ()
 Query last memory access status.


Member Enumeration Documentation

enum IBMPC::Memory::perm_t

Memory page permissions bitmask.

Enumerator:
PERM_READABLE  This memory page is readable.
PERM_WRITEABLE  This memory page is writeable.
PAGE_ALLOCATED  Block of memory for this page's storage was allocated by the manager.

Definition at line 66 of file ibmpc.h.

enum IBMPC::Memory::state_t

Memory bus state bitmask (used to check for error in last operation).

Enumerator:
STATE_FAULT  0:last operation succeeded; 1:failed
STATE_DIR  0:last operation was READ, 1:WRITE

Definition at line 74 of file ibmpc.h.


Constructor & Destructor Documentation

IBMPC::Memory::Memory (  ) 

Construct instance of Memory manager.

Definition at line 13 of file ibmpc.cpp.

IBMPC::Memory::~Memory (  ) 

Destruct instance of Memory manager.

Definition at line 20 of file ibmpc.cpp.


Member Function Documentation

bool IBMPC::Memory::alloc_pagetable ( unsigned int  pages_count_pow,
unsigned int  page_size_pow 
)

Allocate/Reallocate memory page table.

Attention:
The parameters are given in powers of 2, so real size will be 2^given_value. For example: pages_count_pow=2 will really allocate 4 pages, 4 will allocate 16 pages, 8 will allocate 256, 10 will allocate 1024, etc.. Same thing with page_size_pow.
Note:
It is possible to call this function multiple times. Old page table contents will be lost, associated pages cleanly deallocated and detached from drivers before reallocating.
All pages in newly allocated page table will be in freed state, and page_get_perm() call on them will return 0.
Returns:
true if succesful, false if failed to allocate memory fo the page table.
Note:
If this function fails (returns false), it's guaranteed that page table state is not altered.

Definition at line 30 of file ibmpc.cpp.

Referenced by IBMPC::VirtualMachine::VirtualMachine().

void IBMPC::Memory::page_set_perm ( unsigned int  page_index,
uint32  perm 
)

Set permissions for memory page (also see enum perm_t).

Note:
You must never specify the bit PAGE_ALLOCATED (from enum perm_t) in the "perm" parameter. In DEBUG build OE_ASSERT() will catch it and stop the program. In RELEASE mode this bit will be silently masked from the parameter.

This function preserves existing value of PAGE_ALLOCATED bit, no matter which value of this bit is in the parameter "perm" passed to this function.

In DEBUG build, the function will OE_ASSERT() if page index is out of bounds.

Definition at line 72 of file ibmpc.cpp.

uint32 IBMPC::Memory::page_get_perm ( unsigned int  page_index  ) 

Retrieve permissions of given memory page.

Returns:
bitmask with set of bits defined in enum perm_t describing permissions of this memory page.
The returned bitmask may include the PAGE_ALLOCATED bit, which can be used at runtime to tell whether the page is really allocated or attached to some driver.
Note:
For pages never allocated/attached, or those freed using page_free() this function will return 0.

In DEBUG build, the function will OE_ASSERT() if page index is out of bounds.

Definition at line 82 of file ibmpc.cpp.

bool IBMPC::Memory::page_alloc ( unsigned int  page_index  ) 

Allocate real memory for the memory page.

Returns:
true if succeeded, false if memory allocation failed.
Reallocation
If page was previously allocated or attached to device it will be deallocated/detached before allocating. Also, the page permissions returned by page_get_perm() for this page will contain the bit PAGE_ALLOCATED until the page is deallocated.
Note:
In DEBUG build, the function will OE_ASSERT() if page index is out of bounds.

Definition at line 90 of file ibmpc.cpp.

Referenced by IBMPC::VirtualMachine::VirtualMachine().

void IBMPC::Memory::page_attach ( unsigned int  page_index,
Device driver 
)

Attach device driver to handle accesses to the memory page.

Attention:
Initially, after allocation, the page will have FULL permissions (READ/WRITE). Call page_set_perm() to change these initial permissions if you need.
Reallocation
If page was previously allocated or attached to device it will be deallocated/detached before attaching this Device driver.
Note:
In DEBUG build, the function will OE_ASSERT() if page index is out of bounds.

Definition at line 105 of file ibmpc.cpp.

Referenced by IBMPC::VDD_Video_CGA_6845::init().

void IBMPC::Memory::page_free ( unsigned int  page_index  ) 

Free real memory block allocated for this memory page or detach page from Device driver.

Attention:
Initially, after attachment, the page will have FULL permissions (READ/WRITE). Call port_set_perm() to change these initial permissions if you need.
Note:
It is valid to try to free already free page. Nothing will be done during such call.

After freeing, the page permissions bitmask will be zeroed.

In DEBUG build, the function will OE_ASSERT() if page index is out of bounds.

Definition at line 116 of file ibmpc.cpp.

Referenced by alloc_pagetable(), IBMPC::VDD_Video_CGA_6845::done(), page_alloc(), page_attach(), and ~Memory().

uint8 IBMPC::Memory::mem_read8 ( unsigned int  addr  ) 

Load 8 bits from memory at given "flat" address.

Returns:
Value fetched from virtual memory, or 0 if access failed.
Attention:
The addr will be automatically wrapped around total memory installed (size of page table), so it would be impossible to provide invalid or "out of bounds" addr.
Note:
If accessed page is not ALLOCATED then it's attached to some driver (see page_attach()). In this case, the associated driver's mem_read8() method will be invoked. (see class Driver).

In case page permissions are violated (or attached Device driver performing the operation decided to fail the call) value of 0 will be returned, and state flags (state_t) set to indicate fault.

It is caller's responsibility to check for faults using call to get_state() immediately after performing the access.

Definition at line 128 of file ibmpc.cpp.

Referenced by IBMPC::VDD_DMA::read_next_word().

void IBMPC::Memory::mem_write8 ( unsigned int  addr,
uint8  data 
)

Store 8 bits to memory at given "flat" address.

Note:
If accessed page is not ALLOCATED then it's attached to some driver (see page_attach()).
Attention:
The addr will be automatically wrapped around total memory installed (size of page table), so it would be impossible to provide invalid or "out of bounds" addr. In this case, the associated driver's mem_write8() method will be invoked. (see class Driver).
Note:
In case page permissions are violated (or attached Device driver performing the operation decided to fail the call) state flags (state_t) will be set to indicate fault.

It is caller's responsibility to check for faults using call to get_state() immediately after performing the access.

Definition at line 146 of file ibmpc.cpp.

Referenced by IBMPC::copy_to_mem(), IBMPC::load_ibmpc_rom(), and IBMPC::VDD_DMA::write_next_word().

uint32 IBMPC::Memory::get_state (  ) 

Query last memory access status.

Returns:
bitmask defined by enum state_t. The value is reset each time mem_read8() or mem_write8() operation is performed.
Users who want to check for errors and violations during memory access will need to call get_state() after each call to memory accessing functions mem_read8() or mem_write8().
Note:
This will mostly be called from inside CPU emulators, but driver writers may want to check for status during DMA (direct memory access) operation they perform.


The documentation for this class was generated from the following files:
Generated on Sat Sep 9 03:50:54 2006 for Openem APIs by  doxygen 1.4.7