#include "stdafx.h"

#include "Computer6809.h"
#include <Config.h>
#include "IO/Console.h"
#include "CPU/CPU6809.h"
#include <Video/VideoNull.h>

using cfg::CONFIG;

namespace emul
{
	Computer6809::Computer6809() :
		Logger("Computer6809"),
		ComputerBase(m_memory),
		m_baseRAM("RAM", 0xC000, emul::MemoryType::RAM),
		m_osROM("ROM_OS", 0x1000, emul::MemoryType::ROM)
	{
	}

	void Computer6809::Reset()
	{
		ComputerBase::Reset();
		//GetCPU().Reset(0x400); // Override reset vector for tests
	}

	void Computer6809::Init(WORD baseRAM)
	{
		ComputerBase::Init(emul::CPUID_6809, baseRAM);

		m_baseRAM.Clear(0x69);
		m_memory.Allocate(&m_baseRAM, 0);

		//m_osROM.LoadFromFile("");

		InitInputs(1000000, 100000);
		InitVideo();
	}

	void Computer6809::InitCPU(const char* cpuid)
	{
		if (cpuid == CPUID_6809) m_cpu = new CPU6809(m_memory);
		else
		{
			LogPrintf(LOG_ERROR, "CPUType not supported: [%s]", cpuid);
			throw std::exception("CPUType not supported");
		}
	}

	void Computer6809::InitVideo()
	{
		// Dummy video card
		video::VideoNull* video = new video::VideoNull();
		video->EnableLog(CONFIG().GetLogLevel("video"));
		video->Init(&m_memory, nullptr);
		m_video = video;
	}

	bool Computer6809::Step()
	{
		if (!ComputerBase::Step())
		{
			return false;
		}

		static uint32_t cpuTicks = 0;
		cpuTicks += GetCPU().GetInstructionTicks();

		++g_ticks;

		m_video->Tick();

		GetInputs().Tick();
		if (GetInputs().IsQuit())
		{
			return false;
		}

		return true;
	}
}
