Source

gb_emulator / gb_emulator / include / gb_emulator / gb_cpu.hpp

Full commit
/*  Copyright © 2011 Chris Spencer <spencercw@gmail.com>

    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 3 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#ifndef GB_CPU_HPP_5A64B0A0_8A06_11E0_A0CA_0002A5D5C51B
#define GB_CPU_HPP_5A64B0A0_8A06_11E0_A0CA_0002A5D5C51B

#include <stdint.h>

#include <gb_emulator/constants.hpp>
#include <gb_emulator/defs.hpp>

class GbCpuData;
class GbDebugger;
class GbMemory;

//! GameBoy CPU emulator.
class GB_EMULATOR_API GbCpu
{
public:
	//! Standard registers.
	union
	{
		uint8_t  r8 [REG_COUNT_8];
		uint16_t r16[REG_COUNT_16];
	} r;

	//! Stack pointer.
	uint16_t sp;
	//! Program counter.
	uint16_t pc;

	//! Interrupt master enable register.
	int ime;
	//! CPU halted flag.
	bool halt;
	//! CPU stopped flag.
	bool stop;
	//! CPU pause flag.
	bool pause;

	//! Constructor.
	/**
	 * \param memory The memory manager to use.
	 */
	GbCpu(GbMemory &memory);

	//! Resets the CPU emulator to its default state.
	void reset();

	//! Executes a number of instructions.
	/**
	 * \param cycles The number of cycles to execute.
	 * \param speedSwitch Flag set to \c true if a speed switch occurs. If a speed switch occurs no
	 * further instructions will be executed and the function will return immediately. This is so
	 * any calculations made against the number of cycles executed can be adjusted to account for
	 * the new clock frequency.
	 * \return The number of cycles executed. This will be at least <c>cycles</c>, unless a speed
	 * switch occurs, in which case \c speedSwitch will be set to \c true and the function will
	 * return immediately.
	 */
	int poll(int cycles, bool &speedSwitch);

	//! Handles CPU interrupts.
	void handleInterrupts();

	//! Attaches the given debugger to the CPU emulator.
	void attachDebugger(GbDebugger *debugger)
	{
		debugger_ = debugger;
	}

	//! Saves the current state of the CPU emulator into the given message.
	void save(GbCpuData &data) const;

	//! Loads the CPU emulator state from the given message.
	void load(const GbCpuData &data);

private:
	// Memory manager
	GbMemory &mem_;

	// Debugger, if attached
	GbDebugger *debugger_;

	// Disabled operations
	GbCpu(const GbCpu &);
	GbCpu & operator=(const GbCpu &);
};

#endif