What's new

C++ Templates In Emulators...

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:

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:

Xeven

New member
I wouldn't recommend it at all, this is incredibly slow and one way or another if you start optimizing you'll end up with some kind of C/C++ hybrid. Which is incredibly ugly, believe me Exo always wanted to kill me for that :evil:
 
OP
zenogais

zenogais

New member
Xeven said:
I wouldn't recommend it at all, this is incredibly slow and one way or another if you start optimizing you'll end up with some kind of C/C++ hybrid. Which is incredibly ugly, believe me Exo always wanted to kill me for that :evil:

lol, ok. I just thought it was an interesting idea to explore. I'm still gonna keep going a bit with this concept, and see how far I can take it. I always figured this was gonna be kinda slow though, as two function calls per memory read/write is inherently slow.
 
Last edited:

Top