/*	Copyright  (c)	Günter Woigk 2013 - 2013
					mailto:kio@little-bat.de

	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.

	Permission to use, copy, modify, distribute, and sell this software and
	its documentation for any purpose is hereby granted without fee, provided
	that the above copyright notice appear in all copies and that both that
	copyright notice and this permission notice appear in supporting
	documentation, and that the name of the copyright holder not be used
	in advertising or publicity pertaining to distribution of the software
	without specific, written prior permission.  The copyright holder makes no
	representations about the suitability of this software for any purpose.
	It is provided "as is" without express or implied warranty.

	THE COPYRIGHT HOLDER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
	INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
	EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY SPECIAL, INDIRECT OR
	CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
	DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
	TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
	PERFORMANCE OF THIS SOFTWARE.
*/


#include "CpuInspector.h"
#include "Cpu.h"
#include "MyLineEdit.h"
#include <QTimer>
#include <QLabel>
#include <QPushButton>
#include "util.h"


static const uint dy = 18;


// Helper:
MyLineEdit* CpuInspector::newMyLineEdit(int x,int y, cstr slot)
{
	MyLineEdit* led = new MyLineEdit("",this);
	if(slot) QWidget::connect(led,SIGNAL(returnPressed()),this,slot);
	else	 led->setEnabled(0);
	led->move(x,y);
	return led;
}

// Helper:
QLabel* CpuInspector::newMyLabel(int x,int y, cstr text)
{
	static QFont myfont("Monaco",12);

	QLabel* label = new QLabel(text,this);
	label->move(x,y);
	label->setFixedHeight(16);
	label->setFont(myfont);
	return label;
}



int CpuInspector::add_debug_buttons(int x,int y)
{
	int w=50,h=40;
	int i=24;

	button_pause	 = new QPushButton(this);										button_pause    -> setGeometry(x-2*w,y,w,h);
	button_step_over = new QPushButton(QIcon(":/resources/step_over.png"),"",this);	button_step_over-> setGeometry(x-1*w,y,w,h);
	button_step_in	 = new QPushButton(QIcon(":/resources/step_in.png"),"",this);	button_step_in  -> setGeometry(x+0*w,y,w,h);
	button_step_out	 = new QPushButton(QIcon(":/resources/step_out.png"),"",this);	button_step_out -> setGeometry(x+1*w,y,w,h);

	button_pause->setIconSize(QSize(i,i));
	button_step_in->setIconSize(QSize(i,i));
	button_step_out->setIconSize(QSize(i,i));
	button_step_over->setIconSize(QSize(i,i));

	connect(button_pause,SIGNAL(clicked()),this,SLOT(slot_run_pause()));
	connect(button_step_in,SIGNAL(clicked()),this,SLOT(slot_step_in()));
	connect(button_step_out,SIGNAL(clicked()),this,SLOT(slot_step_out()));
	connect(button_step_over,SIGNAL(clicked()),this,SLOT(slot_step_over()));

	update_singlestepbuttons();

	return y+h+2;
}


int CpuInspector::add_dregs(int x,int y)
{
	int x2 = x+84;

	dreg_alu	= newMyLineEdit(x,y,SLOT(slot_dreg_alu()));		newMyLabel(x2,y,"alu");		y+=dy;
	dreg_d0		= newMyLineEdit(x,y,SLOT(slot_dreg_d0()));		newMyLabel(x2,y,"d0");		y+=dy;
	dreg_d1		= newMyLineEdit(x,y,SLOT(slot_dreg_d1()));		newMyLabel(x2,y,"d1");		y+=dy;
	dreg_d2		= newMyLineEdit(x,y,SLOT(slot_dreg_d2()));		newMyLabel(x2,y,"d2");		y+=dy;
	dreg_d3		= newMyLineEdit(x,y,SLOT(slot_dreg_d3()));		newMyLabel(x2,y,"dtmp");	y+=dy;
	dreg_sr		= newMyLineEdit(x,y,SLOT(slot_dreg_sr()));		newMyLabel(x2,y,"sr");		y+=dy;
	dreg_sl		= newMyLineEdit(x,y,SLOT(slot_dreg_sl()));		newMyLabel(x2,y,"sl");		y+=dy;
	dreg_swap	= newMyLineEdit(x,y,SLOT(slot_dreg_swap()));	newMyLabel(x2,y,"swap");	y+=dy;
	dreg_msbit	= newMyLineEdit(x,y,SLOT(slot_dreg_msbit()));	newMyLabel(x2,y,"msbit");	y+=dy;
	dreg_cmd_lo	= newMyLineEdit(x,y,SLOT(slot_dreg_cmd_lo()));	newMyLabel(x2,y,"cmd_lo");	y+=dy;

	return y;
}


int CpuInspector::add_aregs(int x, int y)
{
	int x2 = x+84;

	areg_ip		= newMyLineEdit(x,y,SLOT(slot_areg_ip()));		newMyLabel(x2,y,"ip");		y+=dy;
	areg_sp		= newMyLineEdit(x,y,SLOT(slot_areg_sp()));		newMyLabel(x2,y,"sp");		y+=dy;
	areg_hp		= newMyLineEdit(x,y,SLOT(slot_areg_hp()));		newMyLabel(x2,y,"hp");		y+=dy;
	areg_a0		= newMyLineEdit(x,y,SLOT(slot_areg_a0()));		newMyLabel(x2,y,"a0");		y+=dy;
	areg_a1		= newMyLineEdit(x,y,SLOT(slot_areg_a1()));		newMyLabel(x2,y,"a1");		y+=dy;
	areg_atmp	= newMyLineEdit(x,y,SLOT(slot_areg_atmp()));	newMyLabel(x2,y,"atmp");	y+=dy;
	areg_ioaddr	= newMyLineEdit(x,y,SLOT(slot_areg_ioaddr()));	newMyLabel(x2,y,"ioaddr");	y+=dy + 6;
	reg_d2ar	= newMyLineEdit(x,y,SLOT(slot_reg_d2ar()));		newMyLabel(x2,y,"d2ar");	y+=dy;

	return y;
}


int CpuInspector::add_k1bus(int x1,int y)
{
	int x2 = x1+124;

	newMyLabel(x1,y,"K1-Bus:");	y+=dy;
	k1bus_select_bits		= newMyLineEdit(x1,y,SLOT(slot_k1bus_select_bits()));		k1bus_select_bits->setFixedWidth(120);		newMyLabel(x2,y,"sel");	y+=dy;
	k1bus_irpt_enable_bits	= newMyLineEdit(x1,y,SLOT(slot_k1bus_irpt_enable_bits()));	k1bus_irpt_enable_bits->setFixedWidth(120); newMyLabel(x2,y,"ie");	y+=dy;
	k1bus_irpt_pending_bits	= newMyLineEdit(x1,y,SLOT(slot_k1bus_irpt_pending_bits()));	k1bus_irpt_pending_bits->setFixedWidth(120);newMyLabel(x2,y,"int");	y+=dy;

	return y;
}


int CpuInspector::add_breakpoints(int x, int y)
{
	int x2=x+84;

	newMyLabel(x,y,"Breakpoints:");	y+=dy;
//	breakpoint_read	 = newMyLineEdit(x,y,SLOT(slot_breakpoint_read()));		newMyLabel(x2,y,"read");	y+=dy;
//	breakpoint_write = newMyLineEdit(x,y,SLOT(slot_breakpoint_write()));	newMyLabel(x2,y,"write");	y+=dy;
	breakpoint_exec  = newMyLineEdit(x,y,SLOT(slot_breakpoint_exec()));		newMyLabel(x2,y,"exec");	y+=dy;
	breakpoint_return= newMyLineEdit(x,y,SLOT(slot_breakpoint_return()));	newMyLabel(x2,y,"return");	y+=dy;

	return y;
}


int CpuInspector::add_mca(int x1, int y, int x2)
{
	mca2 = newMyLineEdit(x1,y,SLOT(slot_mca2())); newMyLabel(x1+84,y,"mca2");	mc2	= newMyLineEdit(x2,y,NULL); newMyLabel(x2+84,y,"mc2");	y+=dy;
	mca1 = newMyLineEdit(x1,y,SLOT(slot_mca1())); newMyLabel(x1+84,y,"mca1");	mc1 = newMyLineEdit(x2,y,NULL); newMyLabel(x2+84,y,"mc1");	y+=dy;

	return y;
}


CpuInspector::CpuInspector(QWidget *parent, Cpu *cpu)
:	Inspector(parent,"cpu"),
	cpu(cpu),
	old_ssbtn_state(999)
{
	setWindowTitle("CPU");

    uint width  = 300;
    uint height = 400;
    resize(width,height);


	uint x1=6;
	uint x2=x1+84;
	uint x3=150;
	uint x4=x3+84;
	uint y1=6, y2=6;

	xtal = newMyLineEdit(x1,y1,SLOT(slot_xtal()));	newMyLabel(x2,y1,"clock");	y1 += dy + 6;
	cc	 = newMyLineEdit(x3,y2,SLOT(slot_cc()));	newMyLabel(x4,y2,"cc");		y2 += dy + 6;

	y1 = y2 = add_mca(x1,y1,x3) + 6;

	y1 = add_dregs(x1,y1) + 6;
	y2 = add_aregs(x3,y2) + 6;

	y2 = y1-dy-6;
	div_ff = newMyLineEdit(x3,y2,SLOT(slot_div_ff()));	newMyLabel(x4,y2,"div.FF");

	y2 = y1 += 6;
	y1 = add_breakpoints(x1,y1);
	y2 = add_k1bus(x3,y2);

	y2 += 6;
	y2 = add_debug_buttons(x3,y2);
}


void CpuInspector::update_singlestepbuttons()
{
	if(old_ssbtn_state!=cpu->state)
	{
		old_ssbtn_state = cpu->state;
		bool f = cpu->is_stopped();
		button_pause->setIcon( f ? QIcon(":/resources/pause_red.png") : QIcon(":/resources/pause.png"));
		button_step_in->setEnabled(f);
		button_step_out->setEnabled(f);
		button_step_over->setEnabled(f);
	}
}


void CpuInspector::update()
{
#define showR(R)  R->setText(usingstr("$%04X",(uint)cpu->R));
#define showBR(R) R->setText(~cpu->R ? usingstr("$%04X",(uint)cpu->R) : "");

	div_ff->setText(binstr(cpu->div_ff,"--------","rRYGeh42"));
	showR(areg_ip);
	showR(areg_sp);
	showR(areg_a0);
	showR(areg_a1);
	showR(areg_hp);
	showR(areg_atmp);
	areg_ioaddr->setText(binstr(cpu->areg_ioaddr,"%000000","%111111"));

	showR(dreg_d0);
	showR(dreg_d1);
	showR(dreg_d2);
	showR(dreg_d3);
	showR(dreg_alu);
	showR(dreg_swap);
	showR(dreg_sl);
	showR(dreg_sr);
	showR(dreg_msbit);
	showR(dreg_cmd_lo);
	showR(reg_d2ar);

	showR(mca1);
	showR(mca2);
	mc1->setText(usingstr("$%06X",cpu->mc[cpu->mca1]));
	mc2->setText(usingstr("$%06X",cpu->mc[cpu->mca2]));

	showBR(breakpoint_exec);
//	showBR(breakpoint_read);
	showBR(breakpoint_return);
//	showBR(breakpoint_write);

	cc->setText(numstr(cpu->cc));
	xtal->setText(MHzStr(cpu->xtal));

	k1bus_select_bits->setText(binstr(~cpu->k1bus_select_bits,"0000000000000000","----------------"));
	k1bus_irpt_enable_bits->setText(binstr(~cpu->k1bus_irpt_enable_bits,"0000000000000000","----------------"));
	k1bus_irpt_pending_bits->setText(binstr(~cpu->k1bus_irpt_pending_bits,"0000000000000000","----------------"));

	update_singlestepbuttons();
}


#define setR(R) uint n = intValue(R->text().toUtf8().data()); if(errno==ok) cpu->R = n; showR(R);
#define setBR(R) cstr s = R->text().toUtf8().data(); if(*s) { uint n = intValue(s); if(errno==ok && n<=0xFFFF) cpu->R = n; } else cpu->R = ~0; showBR(R);

void CpuInspector::slot_areg_ip()	{ setR(areg_ip); }
void CpuInspector::slot_areg_sp()	{ setR(areg_sp); }
void CpuInspector::slot_areg_a0()	{ setR(areg_a0); }
void CpuInspector::slot_areg_a1()	{ setR(areg_a1); }
void CpuInspector::slot_areg_hp()	{ setR(areg_hp); }
void CpuInspector::slot_areg_atmp()	{ setR(areg_atmp); }
void CpuInspector::slot_areg_ioaddr()
{
	uint n = intValue(areg_ioaddr->text().toUtf8().data());
	if(errno==ok) cpu->areg_ioaddr = n;
	areg_ioaddr->setText(binstr(cpu->areg_ioaddr,"%000000","%111111"));
}
void CpuInspector::slot_dreg_d0()	{ setR(dreg_d0); }
void CpuInspector::slot_dreg_d1()	{ setR(dreg_d1); }
void CpuInspector::slot_dreg_d2()	{ setR(dreg_d2); }
void CpuInspector::slot_dreg_d3()	{ setR(dreg_d3); }
void CpuInspector::slot_dreg_alu()	{ setR(dreg_alu); }
void CpuInspector::slot_dreg_swap()	{ setR(dreg_swap); }
void CpuInspector::slot_dreg_sr()	{ setR(dreg_sr); }
void CpuInspector::slot_dreg_sl()	{ setR(dreg_sl); }
void CpuInspector::slot_dreg_msbit(){ setR(dreg_msbit); }
void CpuInspector::slot_dreg_cmd_lo(){ setR(dreg_cmd_lo); }
void CpuInspector::slot_reg_d2ar()	{ setR(reg_d2ar); }

void CpuInspector::slot_mca1()	// only works if cpu is halted
{
	uint32 n = intValue(mca1->text().toUtf8().data()) & 0x7FFF;
	if(errno==ok) cpu->mca1 = n; else n = cpu->mca1;
	mca1->setText(catstr("$",hexstr(n,6)));
	mc1->setText(catstr("$",hexstr(cpu->mc[n],4)));
}
void CpuInspector::slot_mca2()	// only works if cpu is halted
{
	uint32 n = intValue(mca2->text().toUtf8().data()) & 0x7FFF;
	if(errno==ok) cpu->mca2 = n; else n = cpu->mca2;
	mca2->setText(catstr("$",hexstr(n,6)));
	mc2->setText(catstr("$",hexstr(cpu->mc[n],4)));
}

//void CpuInspector::slot_breakpoint_read()	{ setBR(breakpoint_read); }
//void CpuInspector::slot_breakpoint_write()	{ setBR(breakpoint_write); }
void CpuInspector::slot_breakpoint_exec()	{ setBR(breakpoint_exec); }
void CpuInspector::slot_breakpoint_return()	{ setBR(breakpoint_return); }

void CpuInspector::slot_k1bus_select_bits()
{
	cstr s = k1bus_select_bits->text().toUtf8().data();
	if(strlen(s)==16)
	{
		s = replacedstr(s,'-','1');
		uint n = intValue(catstr("%",s));
		if(errno==ok) cpu->k1bus_select_bits = ~n;
	}
	k1bus_select_bits->setText(binstr(~cpu->k1bus_select_bits,"0000000000000000","----------------"));
}

void CpuInspector::slot_k1bus_irpt_enable_bits()
{
	cstr s = k1bus_irpt_enable_bits->text().toUtf8().data();
	if(strlen(s)==16)
	{
		s = replacedstr(s,'-','1');
		uint n = intValue(catstr("%",s));
		if(errno==ok) cpu->k1bus_irpt_enable_bits = ~n;
	}
	k1bus_irpt_enable_bits->setText(binstr(~cpu->k1bus_irpt_enable_bits,"0000000000000000","----------------"));
}

void CpuInspector::slot_k1bus_irpt_pending_bits()
{
	cstr s = k1bus_irpt_pending_bits->text().toUtf8().data();
	if(strlen(s)==16)
	{
		s = replacedstr(s,'-','1');
		uint n = intValue(catstr("%",s));
		if(errno==ok) cpu->k1bus_irpt_pending_bits = ~n;
	}
	k1bus_irpt_pending_bits->setText(binstr(~cpu->k1bus_irpt_pending_bits,"0000000000000000","----------------"));
}

void CpuInspector::slot_xtal()
{
	cstr s = xtal->text().toUtf8().data();
	Frequency f = mhzValue(s);
	if(f!=-1) cpu->xtal = f;
	xtal->setText(MHzStr(cpu->xtal));
}

void CpuInspector::slot_div_ff()
{
	cstr s = xtal->text().toUtf8().data();
	cpu->div_ff = (strchr(s,'R')?64:0)	// led red
				+ (strchr(s,'Y')?32:0)	// led yellow
				+ (strchr(s,'G')?16:0)	// led green
				+ (strchr(s,'e')?8:0)	// enable interrupts
				+ (strchr(s,'h')?4:0)	// halt until irpt
				+ (strchr(s,'4')?2:0)	// don't ÷ 4
				+ (strchr(s,'2')?1:0);	// don't ÷ 2
	div_ff->setText(binstr(cpu->div_ff,"--------","rRYGeh42"));
}


void CpuInspector::slot_cc()
{

}


void CpuInspector::slot_run_pause()
{
	if(cpu->is_stopped()) cpu->run(cpu->cc+1,no);	// falls wir auf einem Breakpoint stehen
	cpu->toggle_state();
	update_singlestepbuttons();
}

void CpuInspector::slot_step_in()
{
	//TODO
}

void CpuInspector::slot_step_out()
{
	//TODO
}

void CpuInspector::slot_step_over()
{
	cpu->run(cpu->cc+1,no/*!stop_on_breakpoints*/);
}















