Results 1 to 3 of 3
  1. #1
    EmuTalk Member zenogais's Avatar
    Join Date
    Jun 2004
    Location
    California
    Posts
    215

    C++ Templates In Emulators...

    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 by zenogais; July 30th, 2004 at 20:01.


    • Advertising

      advertising
      EmuTalk.net
      has no influence
      on the ads that
      are displayed
        
       

  2. #2
    EmuTalk Member Xeven's Avatar
    Join Date
    Dec 2001
    Posts
    20
    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

  3. #3
    EmuTalk Member zenogais's Avatar
    Join Date
    Jun 2004
    Location
    California
    Posts
    215
    Quote Originally Posted by Xeven
    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
    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 by zenogais; July 31st, 2004 at 21:28.

Similar Threads

  1. Replies: 33
    Last Post: July 24th, 2005, 00:10
  2. Help with Emulators
    By withoutname in forum nSX2
    Replies: 1
    Last Post: July 11th, 2004, 23:17
  3. Windows XP problem some emulators
    By joedanX7 in forum Other Emulation Discussion
    Replies: 6
    Last Post: April 24th, 2003, 02:05
  4. Emulators and the plugins sub-directory "PLUGIN"...
    By JerryKing in forum General N64 Emulation Discussion
    Replies: 3
    Last Post: December 31st, 2002, 14:02
  5. sound problems in all my emulators
    By nicc in forum Project64
    Replies: 5
    Last Post: February 15th, 2002, 12:05

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •