zenogais
New member
I've been using C++ for a while now, and recently I've become a big fan of using templates for adding flexibility. I just thought I'd share a few examples of how I've been using them in NeoPSX, and if anyone has templated code they've used in an emulator I think it would be cool if they showed it here as well. Well heres what I've got, and generic memory management class for an emulator:
Now I've used private inheritance here, because private inheritance, unlike public inheritance, means "is implemented in terms of". The loadPlaystationBios, is one that you can take out as it isn't vital, in fact I might even take it out and implement it another way. But enough of that. Each memory map class is only required to have a the function with a signature as follows:
Inside getPointerAt is then the only place you have to define the memory mapping you are using. And the tailing "const" for those who don't know, means that inside the body of the function variable values are not modified, in others words no "blah = blah".
Code:
template
<
typename TByteType = unsigned char,
typename TAddressType = unsigned long,
class CMemoryMap = PSXSystemMemoryMapPolicy<TByteType, TAddressType>
>
class PSXMemory : private CMemoryMap
{
public:
PSXMemory();
~PSXMemory();
/**
* Function to reset entire memory area.
* @version 1.0.0
*/
void resetMemory();
/**
* Function which loads a Playstation BIOS file into memory.
* @param strBiosFilename The name of the BIOS file which will be loaded into memory at 0xBFC00000.
* @return Boolean value indicating whether or not BIOS was sucessfully loaded.
* @version 1.0.0
*/
bool loadPlaystationBIOS(std::string strBiosFilename);
/**
* Function which writes 8-bits of data to playstation memory.
* @param address The address in playstation memory to write at.
* @param value An 8-bit value to write at the specified address.
* @version 1.0.0
*/
void writeMemory8 (TAddressType address, unsigned char value);
/**
* Function which writes 16-bits of data to playstation memory.
* @param address The address in playstation memory to write at.
* @param value The 16-bit value to write at the specified address.
* @version 1.0.0
*/
void writeMemory16(TAddressType address, unsigned short value);
/**
* Function which writes 32-bits of data to playstation memory.
* @param address The address in playstation memory to write at.
* @param value The 32-bit value to write at the specified location.
* @version 1.0.0
*/
void writeMemory32(TAddressType address, unsigned long value);
/**
* Function which reads 8-bits of data from playstation memory.
* @param address The address in playstation memory to read from.
* @return The 8-bit value at the specified memory address.
* @version 1.0.0
*/
unsigned char readMemory8 (TAddressType address) const;
/**
* Function which reads 16-bits of data from playstation memory.
* @param address The address in playstation memory to read from.
* @return The 16-bit value at the specified memory address.
* @version 1.0.0
*/
unsigned short readMemory16(TAddressType address) const;
/**
* Function which reads 32-bits of data from playstation memory.
* @param address The address in playstation memory to read from.
* @return The 32-bit value at the specified memory address.
* @version 1.0.0
*/
unsigned long readMemory32(TAddressType address) const;
};
Now I've used private inheritance here, because private inheritance, unlike public inheritance, means "is implemented in terms of". The loadPlaystationBios, is one that you can take out as it isn't vital, in fact I might even take it out and implement it another way. But enough of that. Each memory map class is only required to have a the function with a signature as follows:
Code:
unsigned char* getPointerAt(TAddressType address) const;
Inside getPointerAt is then the only place you have to define the memory mapping you are using. And the tailing "const" for those who don't know, means that inside the body of the function variable values are not modified, in others words no "blah = blah".
Last edited: