/*	Copyright  (c)	Günter Woigk 2012 - 2012
					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.
*/


#ifndef MEMORYDISASSINSPECTOR_H
#define MEMORYDISASSINSPECTOR_H

#include "kio/kio.h"
#include "MemoryInspector.h"
#include "templates/Array.h"
class Machine;
class MemoryInspector;
class SimpleTerminal;
class QPushButton;
class QLineEdit;
class CoreByteDisassembler;


struct DisassData
{
	int32	 address;
	CoreByte data[4];
};


class MemoryDisassInspector : public MemoryInspector
{
	Q_OBJECT

	SimpleTerminal*	address_view;
	SimpleTerminal*	hex_view;
	SimpleTerminal*	disass_view;
//	QComboBox*		combobox_editmode;

	QWidget*		widget_edit_mode;
	QPushButton*	button_breakpoint_r;
	QPushButton*	button_breakpoint_w;
	QPushButton*	button_breakpoint_x;
	QPushButton*	button_edit_mode;

	CoreByteDisassembler*	disass;
	Array<DisassData>		displayed_data;

	int				cw;			// Character width
	int				rh;			// Row height

	int32			pc,bc,de,hl,ix,iy,sp;	// highlight addresses for registers

	uint			edit_mode;				// as set by combobox_editmode: enum{EDITMODE_VIEW,EDITMODE_EDIT,EDITMODE_BREAKPOINTS};
	CoreByte		breakpoint_mask;		// bitmask, if edit_mode == EDITMODE_BREAKPOINTS

	bool			editing_in_hex();
	bool			editing_in_disass();

	bool			edit_flashphase;
	Time			edit_flashtime;

	int32			old_pc;
	int32			old_disass_edit_address;

	QString			disass_edit_string;
	bool			disass_edit_string_valid;
	int32			disass_edit_address;			// hex or disass
	int				disass_edit_col;				// hex or disass
	int32			hex_edit_address;			// hex or disass
	int				hex_edit_col;				// hex or disass

	void			print_rows		(int row_a, int row_e, int32 address);
	int				print_row		(int row, int32 address);
	void			print_row		(int32 address);
	void			hide_cursor		();
	void			show_hex_cursor	(bool);
	void			show_disass_cursor(bool);
	void			print_byte_at_address(int32 addr);
	int				row_for_address	(int32 address);
	void			step_left_in_hex();
	void			step_right_in_hex();
	int32			prev_opcode		(int32 address);
	int32			next_opcode		(int32 address);
	int32			prev_opcode		(int32 address, int n, bool f);
	int32			start_of_opcode	(int32 address);
	void			scroll_to_show_address(int32 address);
	void			assemble_and_store_opcode();
	void			set_breakpoint(CoreByte mask, bool f);

protected:
	virtual void	resizeEvent(QResizeEvent*);
//	virtual void	wheelEvent(QWheelEvent*);
	virtual void	paintEvent(QPaintEvent*);
	virtual void	mousePressEvent(QMouseEvent*);
//	virtual bool	event(QEvent*);
	virtual void	keyPressEvent(QKeyEvent*);
//	virtual void	keyReleaseEvent(QKeyEvent*);

	virtual void	updateScrollbar	();
	virtual void	setBaseAddress(int32 new_base_address);
	virtual void	setScrollPosition(int new_base_address);

public:
	MemoryDisassInspector (QWidget*, IsaObject*);
	~MemoryDisassInspector();

signals:

public slots:
	virtual void	updateDisplayedData();
	virtual void	setMemoryPage(int);
	virtual void	setDataSource(int);
//	void			setEditMode(int);		// combobox_editmode
	void			setEditMode			(bool);
	void			setBreakpointR		(bool f)	{ set_breakpoint(cpu_break_r,f); }
	void			setBreakpointW		(bool f)	{ set_breakpoint(cpu_break_w,f); }
	void			setBreakpointX		(bool f)	{ set_breakpoint(cpu_break_x,f); }

private slots:
	void			slotFocusChanged(bool);
};

#endif // MEMORYDISASSINSPECTOR_H
























