/*
Copyright (C) 2006 - 2009 Evan Teran
                   eteran@alum.rit.edu

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 2 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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "DialogHWBreakpoints.h"
#include "State.h"
#include "Debugger.h"
#include "DebuggerCoreInterface.h"
#include <QRegExpValidator>

#if defined(EDB_X86)
	#define MAX_HEX		"8"
#elif defined(EDB_X86_64)
	#define MAX_HEX		"16"
#endif

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
DialogHWBreakpoints::DialogHWBreakpoints(QWidget *parent) : QDialog(parent) {
	ui.setupUi(this);
	
	connect(ui.cmbType1, SIGNAL(currentIndexChanged(int)), this, SLOT(type1IndexChanged(int)));
	connect(ui.cmbType2, SIGNAL(currentIndexChanged(int)), this, SLOT(type2IndexChanged(int)));
	connect(ui.cmbType3, SIGNAL(currentIndexChanged(int)), this, SLOT(type3IndexChanged(int)));
	connect(ui.cmbType4, SIGNAL(currentIndexChanged(int)), this, SLOT(type4IndexChanged(int)));
	
	ui.txtBP1->setValidator(new QRegExpValidator(QRegExp("[A-Fa-f0-9]{0," MAX_HEX "}"), this));
	ui.txtBP2->setValidator(new QRegExpValidator(QRegExp("[A-Fa-f0-9]{0," MAX_HEX "}"), this));
	ui.txtBP3->setValidator(new QRegExpValidator(QRegExp("[A-Fa-f0-9]{0," MAX_HEX "}"), this));
	ui.txtBP4->setValidator(new QRegExpValidator(QRegExp("[A-Fa-f0-9]{0," MAX_HEX "}"), this));
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogHWBreakpoints::type1IndexChanged(int index) {
	ui.cmbSize1->setEnabled(index != 0);
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogHWBreakpoints::type2IndexChanged(int index) {
	ui.cmbSize2->setEnabled(index != 0);
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogHWBreakpoints::type3IndexChanged(int index) {
	ui.cmbSize3->setEnabled(index != 0);
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogHWBreakpoints::type4IndexChanged(int index) {
	ui.cmbSize4->setEnabled(index != 0);
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogHWBreakpoints::showEvent(QShowEvent *event) {
	Q_UNUSED(event);
	
	State state;
	edb::v1::debuggerBase->getState(state);
	
	const bool bp1Enabled = (state.dr[7] & 0x00000001) != 0;
	const bool bp2Enabled = (state.dr[7] & 0x00000004) != 0;
	const bool bp3Enabled = (state.dr[7] & 0x00000010) != 0;
	const bool bp4Enabled = (state.dr[7] & 0x00000040) != 0;
	
	ui.chkBP1->setChecked(bp1Enabled);
	ui.chkBP2->setChecked(bp2Enabled);
	ui.chkBP3->setChecked(bp3Enabled);
	ui.chkBP4->setChecked(bp4Enabled);

	if(bp1Enabled) {
		ui.txtBP1->setText(edb::v1::formatPointer(state.dr[0]));
	}
	
	if(bp2Enabled) {
		ui.txtBP2->setText(edb::v1::formatPointer(state.dr[1]));
	}
	
	if(bp2Enabled) {
		ui.txtBP3->setText(edb::v1::formatPointer(state.dr[2]));
	}
	
	if(bp2Enabled) {
		ui.txtBP4->setText(edb::v1::formatPointer(state.dr[3]));
	}
}
