summaryrefslogtreecommitdiffstats
path: root/src/kpicosim.cpp
blob: cf3eb13d37e231e4466ff933cef8564b832c8625 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/***************************************************************************
 *   Copyright (C) 2005 by Mark Six                                        *
 *   marksix@xs4all.nl                                                     *
 *                                                                         *
 *   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.             *
 ***************************************************************************/


/* Revision History
 * V0.1: - Initial Version
 * V0.2: 
 *     ADDED
 *		  - Improved debugging. Added Execution and Breakpoint icons in border.
 *        - Show more compiler messages.
 *        - Improved Syntax Highlighting (Now automatically installed).
 *		  - Improved the assembler. (Code is still a mess!)
 *        - Added icons.
 *        - Installation of the .desktop file is now in 'Development'
 * V0.3:
 *     ADDED
 *        - Export to HEX and MEM files
 * V0.4:
 *     ADDED
 *        - Expanded the debug toolbar.
 * V0.5:
 *     BUG FIX
 *        - Assembler: 'ORR sX, sY' assembled to 'ORR sX, kk' where kk was undefined
 *        - Simulator:  SUBCY sX, sY simulated wrongly (undefined behaviour)
 *                      SUBCY sX, kk simulated wrongly (undefined behaviour)
 *     IMPROVED
 *        - Change port ID per port and in serial window.
 *     ADDED
 *        - Save/Restore settings.
 *		  - Clear serial view popupmenu
 *
 * V0.6:
 *     ADDED
 *         - Debian packaging support by "Adrian Knoth"
 *         - Initial JTAG support
 *         - Initial Help
 *  
 *     IDEAS (Oct 9, 2005)
 *			- Multiple picoblaze support
 *			- IO ports (and irq) can be changed from other software. 
 *          - Download mem file with JTAG
 */


#include "kpicosim.h"

#include <qlabel.h>

#include <kmainwindow.h>
#include <klocale.h>

#include <kmenubar.h>
#include <qlayout.h>
#include <qkeysequence.h>
#include <knuminput.h>
#include <qnamespace.h>
#include <kportview.h>
#include <qdockwindow.h>
#include <kstatusbar.h>
#include <kiconloader.h>
#include "kexportdialog.h"
#include "kjtagdialog.h"
#include <kfiledialog.h>
#include <kmessagebox.h>
#include <qlayout.h>
#include <ksimpleconfig.h>
#include <khelpmenu.h>
#include <kaboutdata.h>

const char version[] = "0.6" ;

enum IDs {
	START_SIM_ID = 0,
	COMPILE_ID,
	RUN_ID,
	NEXT_ID,
	INTERRUPT_ID,
	RESET_ID,
	
	VIEW_SERIAL_ID,
	VIEW_SCRATCHPAD_ID
} ;


KPicoSim::KPicoSim() : KMainWindow( 0, "KPicoSim" )
{
//	set the shell's ui resource file
//	setXMLFile("kpicosimui.rc");

	m_splitter = new QSplitter( this ) ;
	m_tabWidget = new QTabWidget( m_splitter ) ;
	m_editor = new CodeEditor( m_tabWidget ) ;
	m_messages = new KListView( m_splitter, "messages" ) ;
	m_simulator = new KSimulator( this ) ;
	m_processorView = new KProcessorView( this ) ;
	
	m_tabWidget->addTab( m_editor, "Source" ) ;
	addDockWindow( m_processorView, DockLeft ) ;
	
	m_splitter->setOrientation( QSplitter::Vertical ) ;
	setCentralWidget( m_splitter ) ;
	m_messages->setAllColumnsShowFocus( true ) ;
	m_messages->setFullWidth( true ) ;
	m_messages->resize( m_messages->width(), m_splitter->height() / 5 ) ;	

	buildMenu() ;	
	
	KToolBar *toolbar = new KToolBar( this ) ;
	addDockWindow( toolbar ) ;
	
	toolbar->insertButton( "filenew", -1, SIGNAL( clicked() ), m_editor, SLOT( slotNewFile() ), true, "New" ) ;
	toolbar->insertButton( "fileopen", -1, SIGNAL( clicked() ), m_editor, SLOT( slotOpen() ), true, "Open" ) ;
	toolbar->insertButton( "filesave", -1, SIGNAL( clicked() ), m_editor, SLOT( slotSave() ), true, "Save" ) ;
	toolbar->insertButton( "filesaveas", -1, SIGNAL( clicked() ), m_editor, SLOT( slotSaveAs() ), true, "Save As" ) ;
	
	m_debugBar = new KToolBar( this ) ;
	addDockWindow( m_debugBar ) ;
	m_debugBar->insertButton( UserIcon( "rebuild" ), COMPILE_ID, SIGNAL( clicked() ), this, SLOT( compile() ), true, "Compile" ) ;
	m_debugBar->insertButton( "run", START_SIM_ID, SIGNAL( clicked() ), this, SLOT( startSim() ), true, "Start/Stop Debug" ) ;
	m_debugBar->insertSeparator() ;
	m_debugBar->insertButton( UserIcon( "continue" ), RUN_ID, SIGNAL( clicked() ), this, SLOT( startStop() ), false, "Continue" ) ;
	
	m_debugBar->insertButton( UserIcon( "next" ), NEXT_ID, SIGNAL( clicked() ), m_simulator, SLOT( next() ), false, "Next" ) ;
	m_debugBar->insertButton( UserIcon( "interrupt" ), INTERRUPT_ID, SIGNAL( clicked() ), m_simulator, SLOT( interrupt() ), false, "Interrupt" ) ; 
	m_debugBar->insertButton( UserIcon( "reset" ), RESET_ID, SIGNAL( clicked() ), m_simulator, SLOT( reset() ), false, "Reset" ) ;
	
	connect( this, SIGNAL( run() ), m_simulator, SLOT( run() ) );
	connect( this, SIGNAL( stop()  ), m_simulator, SLOT( stop()  ) ) ;
	connect( m_simulator, SIGNAL( stepped( unsigned int ) ), this, SLOT( stepped( unsigned int ) ) ) ;
	connect( m_processorView, SIGNAL( processorRegsChanged() ), this, SLOT( updateProcessorRegs() ) ) ;
	connect( m_processorView, SIGNAL( processorFlagsChanged() ), this, SLOT( updateProcessorFlags() ) ) ;
	connect( m_messages, SIGNAL( clicked( QListViewItem * ) ), this, SLOT( messageListClicked( QListViewItem * ) ) ) ;
	
	m_messages->addColumn( "Line" ) ;
	m_messages->addColumn( "Description" ) ;
	m_messages->setSorting( -1, FALSE ) ;
	m_simulator->setMessageList( m_messages ) ;

	m_simulationMode = false ;	
	m_scratchpadView = NULL ;
	m_serialView = NULL ;

	statusBar()->insertItem( QString( "Mode: Edit" ), 0 ) ;
	statusBar()->insertItem( QString( "Status: Stopped" ), 1 ) ;
	statusBar()->insertItem( QString( "Instructions: 0" ), 2 ) ;

	m_templateFile = "" ;
	m_outputDir = "" ;
	m_entityName = "mpu_rom" ;


	openGUI() ;
}


void KPicoSim::buildMenu()
{
	KIconLoader * ldr = KGlobal::iconLoader() ;

	KPopupMenu * exportMenu = new KPopupMenu( this ) ;

	exportMenu->insertItem( "VHDL", this, SLOT( fileExportVHDL() ) ) ;
//	exportMenu->insertItem( "COE", this, SLOT( fileExportCOE() ) ) ;
	exportMenu->insertItem( "MEM", this, SLOT( fileExportMEM() ) ) ;
	exportMenu->insertItem( "HEX", this, SLOT( fileExportHEX() ) ) ;

	m_fileMenu = new KPopupMenu( this ) ;
	m_fileMenu->insertItem( ldr->loadIcon( "filenew", KIcon::Small ), "New", this, SLOT( slotFileNew() ) ) ;
	m_fileMenu->insertItem( ldr->loadIcon( "fileopen", KIcon::Small ), "Open", m_editor, SLOT( slotOpen() ) ) ;
	m_fileMenu->insertSeparator() ;
	m_fileMenu->insertItem( ldr->loadIcon( "filesave", KIcon::Small ), "Save", m_editor, SLOT( slotSave() ), QKeySequence::QKeySequence( "CTRL+S" ) ) ;
	m_fileMenu->insertItem( ldr->loadIcon( "filesaveas", KIcon::Small ), "Save As...", m_editor, SLOT( slotSaveAs() ) ) ;
	m_fileMenu->insertSeparator() ;
	m_fileMenu->insertItem( ldr->loadIcon( "fileprint", KIcon::Small ), "Print...", m_editor, SLOT( slotPrint() ), QKeySequence::QKeySequence( "CTRL+P" ) ) ;
	m_fileMenu->insertSeparator() ;
	m_fileMenu->insertItem( "Export", exportMenu ) ;
	m_fileMenu->insertSeparator() ;
	m_fileMenu->insertItem( ldr->loadIcon( "fileclose", KIcon::Small ), "Quit", this, SLOT( slotClose() ) ) ;
	
	m_editMenu = new KPopupMenu( this ) ;
	m_editMenu->insertItem( ldr->loadIcon( "undo", KIcon::Small ), "Undo", m_editor, SLOT( slotUndo() ),QKeySequence::QKeySequence( "CTRL+Z" ) ) ;
	m_editMenu->insertItem( ldr->loadIcon( "redo", KIcon::Small ), "Redo", m_editor, SLOT( slotRedo() ),QKeySequence::QKeySequence( "CTRL+SHIFT+Z" ) ) ;
	m_editMenu->insertSeparator() ;
	m_editMenu->insertItem( "Select All", m_editor, SLOT( slotSelectAll() ),QKeySequence::QKeySequence( "CTRL+A" ) ) ;
	m_editMenu->insertSeparator() ;
	m_editMenu->insertItem( ldr->loadIcon( "editcut", KIcon::Small ), "Cut", m_editor, SLOT( slotCut() ),QKeySequence::QKeySequence( "CTRL+X" ) ) ;
	m_editMenu->insertItem( ldr->loadIcon( "editcopy", KIcon::Small ), "Copy", m_editor, SLOT( slotCopy() ),QKeySequence::QKeySequence( "CTRL+C" ) ) ;
	m_editMenu->insertItem( ldr->loadIcon( "editpaste", KIcon::Small ), "Paste", m_editor, SLOT( slotPaste() ),QKeySequence::QKeySequence( "CTRL+V" ) ) ;
	m_editMenu->insertSeparator() ;
	m_editMenu->insertItem( ldr->loadIcon( "find", KIcon::Small ), "Find...", m_editor, SLOT( slotFind() ), QKeySequence::QKeySequence( "CTRL+F" ) ) ;
	m_editMenu->insertItem( "Find Next", m_editor, SLOT( slotFindNext() ), QKeySequence::QKeySequence( "F3" ) ) ;


	m_debugMenu = new KPopupMenu( this ) ;
	m_debugMenu->insertSeparator() ;
	m_debugMenu->insertItem( ldr->loadIcon( "rebuild", KIcon::Small ), "Compile", this, SLOT( compile() ), QKeySequence::QKeySequence( "SHIFT+F9" ) ) ;
	m_debugMenu->insertItem( ldr->loadIcon( "run", KIcon::Small ), "Start Debug", this, SLOT( startSim() ), QKeySequence::QKeySequence( "F9" ) , START_SIM_ID ) ;

	m_debugMenu->insertSeparator() ;
	m_debugMenu->insertItem( "Continue", this, SLOT( startStop() ), QKeySequence::QKeySequence( "F10" ) , RUN_ID ) ;
	m_debugMenu->insertItem( "Next", m_simulator, SLOT( next() ), QKeySequence::QKeySequence( "F5" ) , NEXT_ID ) ;
	m_debugMenu->insertItem( "Interrupt", m_simulator, SLOT( interrupt() ), QKeySequence::QKeySequence( "F4" ) , INTERRUPT_ID ) ;
	m_debugMenu->insertItem( "Reset", m_simulator, SLOT( reset() ), QKeySequence::QKeySequence( "F11" ) , RESET_ID ) ;
	
	m_debugMenu->insertSeparator() ;
	m_debugMenu->insertItem( "Toggle Breakpoint", m_editor, SLOT( slotToggleBreakpoint() ), QKeySequence::QKeySequence( "F8" ) ) ;
	
	m_settingsMenu = new KPopupMenu( this ) ;
	m_settingsMenu->insertItem( "Configure Editor...", m_editor, SLOT( slotShowConfig() ) ) ;
	
	m_peripheralMenu = new KPopupMenu( this ) ;
	m_peripheralMenu->insertItem( "I/O Port", this, SLOT( newIOPort() ) ) ;
	m_peripheralMenu->insertItem( "Scratchpad", this, SLOT( showScratchpad() ), 0, VIEW_SCRATCHPAD_ID ) ;
	m_peripheralMenu->insertItem( "Serial port", this, SLOT( showSerialPort() ), 0, VIEW_SERIAL_ID ) ;
	
	m_jtagMenu = new KPopupMenu( this ) ;
	m_jtagMenu->insertItem( "Download", this, SLOT( jtagDownload() ) ) ;

	KAboutData *aboutData = new KAboutData( 
		"kpicosim", 
		"kpicosim", 
		version, 
		"IDE for the picoblaze\n\nCopyright (c) 2005 Mark Six", 
		KAboutData::License_GPL,
		0,
		0,
		"http://www.xs4all.nl/~marksix",
		"m6@xs4all.nl"
	 ) ;
	aboutData->addAuthor( "Mark Six", "m6@xs4all.nl", "http://www.xs4all.nl/~marksix" ) ;

	KHelpMenu *helpMenu = new KHelpMenu( this, aboutData, false  ) ;	
	KPopupMenu *help = helpMenu->menu() ;

	menuBar()->insertItem( "File", m_fileMenu ) ;
	menuBar()->insertItem( "Edit", m_editMenu ) ;
	menuBar()->insertItem( "Debug", m_debugMenu ) ;
	menuBar()->insertItem( "Peripheral", m_peripheralMenu ) ;
	menuBar()->insertItem( "JTAG", m_jtagMenu ) ;
	menuBar()->insertItem( "Settings", m_settingsMenu ) ;
	menuBar()->insertItem( "Help", help ) ;
	
	m_debugMenu->setItemEnabled( RUN_ID, false ) ;
	m_debugMenu->setItemEnabled( NEXT_ID, false ) ;
	m_debugMenu->setItemEnabled( INTERRUPT_ID, false ) ;
	m_debugMenu->setItemEnabled( RESET_ID, false ) ;
	
	m_peripheralMenu->setCheckable( true );
	m_peripheralMenu->setItemChecked( VIEW_SERIAL_ID, false ) ;
	m_peripheralMenu->setItemChecked( VIEW_SCRATCHPAD_ID, false ) ;

	
	menuBar()->show() ;
}

void KPicoSim::jtagDownload()
{
	/* JTAG is still in its infancy. This code works for me. I'm using the Xilinx Spartan-3
     * development board. If it works for you, great, if not too bad...
	 */

	KJTAGDialog dlg( this ) ;
	dlg.setFilename( m_bitfile ) ;
	dlg.exec() ;
	m_bitfile = dlg.getFilename() ;
}

void KPicoSim::fileExportCOE()
{
	KMessageBox::information( this, "This function is not supported yet", "Export COE" ) ;

//	if ( compile() ) {
		//m_simulator->exportCOE() ;
//	}
}

void KPicoSim::fileExportHEX()
{

	QString filename = KFileDialog::getSaveFileName( QString::null, 
                                                     "*.hex|HEX files\n*|All files", 
		   										     this,
  												     "Export HEX" ) ;
	if ( filename != "" && compile() ) {
		m_simulator->exportHEX( filename, FALSE ) ;
	}
}

void KPicoSim::fileExportMEM()
{

	QString filename = KFileDialog::getSaveFileName( QString::null, 
                                                     "*.mem|MEM files\n*|All files", 
		   										     this,
  												     "Export MEM" ) ;
	if ( filename != "" && compile() ) {
		m_simulator->exportHEX( filename, TRUE ) ;
	}
}

void KPicoSim::fileExportVHDL()
{
	KExportDialog dlg( this  ) ;
	
	dlg.setTemplateFile( m_templateFile ) ;
	dlg.setOutputDir( m_outputDir ) ;
	dlg.setEntityName( m_entityName ) ;
	dlg.modal() ;
	
	if ( dlg.isCanceled() ) 
		return ;
	
	m_templateFile = dlg.getTemplateFile() ;
	m_outputDir = dlg.getOutputDir() ;
	m_entityName = dlg.getEntityName() ;
		
	if ( compile() && m_simulator->exportVHDL( m_templateFile, m_outputDir, m_entityName ) ) {
		appendMessage( "File '" + m_outputDir + "/" + m_entityName + ".vhd' exported" ) ;
		appendMessage( "Template file '" + m_templateFile + "' used" ) ;
		appendMessage( "***Export Success***" ) ;
	} else {
		appendMessage( "***Export failed***" ) ;
	}
}

void KPicoSim::slotFileNew()
{
	if ( m_editor->close() )
		m_editor->slotNewFile() ;
}

void KPicoSim::slotClose()
{
	close() ;
}

void KPicoSim::closeEvent( QCloseEvent * e )
{
	if ( m_editor->close() )
		e->accept() ;


	// Save filename last opened
	// Save windows IO Ports, peripherals et al.
	closeGUI() ;
}

void KPicoSim::newIOPort()
{	
	KPortView * ioport = new KPortView( m_simulator->getCpu(), 0 ) ;			/* port id is 0 */
//	m_ioList.append( ioport ) ;
	addDockWindow( ioport, DockRight ) ;
//	connect( ioport, SIGNAL( closing( KPortView* ) ), this, SLOT( removeIOPort( KPortView* ) ) ) ;
}

void KPicoSim::showSerialPort()
{
	if ( m_serialView == NULL ) {
		m_serialView = new KSerialView( m_simulator->getCpu(), m_tabWidget ) ;
		m_tabWidget->addTab( m_serialView, "Serial" ) ;
		m_peripheralMenu->setItemChecked( VIEW_SERIAL_ID, true ) ;
	} else {
		m_peripheralMenu->setItemChecked( VIEW_SERIAL_ID, false ) ;
		delete m_serialView ;
		m_serialView = NULL ;
	}
}

void KPicoSim::showScratchpad()
{
	if ( m_scratchpadView == NULL ) {
		m_scratchpadView = new KScratchpadView( this ) ;
		updateScratchpadView() ;
		addDockWindow( m_scratchpadView, DockRight ) ;
		m_peripheralMenu->setItemChecked( VIEW_SCRATCHPAD_ID, true ) ;
	} else {
		m_peripheralMenu->setItemChecked( VIEW_SCRATCHPAD_ID, false ) ;
		delete m_scratchpadView ; 
		m_scratchpadView = NULL ;
	}
}

KPicoSim::~KPicoSim()
{
	// Delete dockwindows 
	// These are the IO ports, scratchpad and the processorview
	dockWindows().setAutoDelete( true ) ;
	dockWindows().clear() ;
	
	if ( m_serialView )	
		delete m_serialView ;
			
	delete m_simulator ;
	
	delete m_debugMenu ;
	delete m_editMenu ;
 	delete m_peripheralMenu ;
	delete m_fileMenu ;
	delete m_settingsMenu ;
}

void KPicoSim::startStop()
{
	if ( m_simulationMode ) {
		
		if ( !m_simulator->isRunning() ) {
			m_debugMenu->changeItem( RUN_ID, "Stop" ) ;
			m_editor->clearExecutionMarker() ;
			m_simulator->run() ;
			statusBar()->changeItem( QString( "Status: Running" ), 1 ) ;
			m_debugBar->setButton( RUN_ID, true ) ;
		} else {
			m_simulator->stop() ;
			updateViews() ;
			m_debugMenu->changeItem( RUN_ID, "Continue" ) ;
			m_editor->setExecutionMarker( m_simulator->getNextSourceLine() ) ;
			statusBar()->changeItem( QString( "Status: Stopped" ), 1 ) ;
			QString str ;
			str.sprintf( "Instructions: %u", m_nrInstructions ) ;
			statusBar()->changeItem( str, 2 ) ;
			m_debugBar->setButton( RUN_ID, false ) ;
		}
	}
}

void KPicoSim::messageListClicked( QListViewItem *item ) 
{
	if ( item ) {	
		bool ok ;
		int line = item->text(0).toInt( &ok, 10 ) ;
	
		if ( ok ) 
			m_editor->setCursor( line - 1 ) ;				// C-programmers do it from zero
	}
}

void KPicoSim::updateProcessorRegs()
{
	unsigned char regValues[ 16 ] ;
	m_processorView->getRegisterValues( regValues ) ;
	m_simulator->setRegisterValues( regValues ) ;
}

void KPicoSim::updateProcessorFlags()
{
	m_simulator->setFlags( m_processorView->getFlags() ) ;
}

void KPicoSim::updateViews() 
{
	unsigned char regValues[ 16 ] ;
	m_simulator->getRegisterValues( regValues ) ;
	m_processorView->setRegisterValues( regValues ) ;
	m_processorView->setFlags( m_simulator->getFlags() ) ;
	
	updateScratchpadView() ;

	QString str ;
	str.sprintf( "Instructions: %u", m_nrInstructions ) ;
	statusBar()->changeItem( str, 2 ) ;
}

void KPicoSim::updateScratchpadView()
{
	if ( m_scratchpadView != NULL ) {
		unsigned char sp_ram[ 64 ] ;
		m_simulator->getScratchpad( sp_ram ) ;
		m_scratchpadView->setContent( sp_ram, sizeof( sp_ram ) ) ;
	}
}

void KPicoSim::stepped( unsigned int line )
{
	m_nrInstructions++ ;
	if ( m_simulator->isRunning() ) {
		if ( m_editor->isBreakpoint( line ) ) { ;
			startStop() ;
			m_editor->setExecutionMarker( line ) ;
		} else if ( (m_nrInstructions % 100 ) == 0 ) {
			updateViews() ;
		}
	} else {
		m_editor->setExecutionMarker( line ) ;
		updateViews() ;
	}
}

void KPicoSim::appendMessage( QString str )
{
	QListViewItem *item = new QListViewItem( m_messages, m_messages->lastChild() ) ;
	item->setText( 0, "" ) ;
	item->setText( 1, str ) ;

}

bool KPicoSim::compile()
{
	m_simulator->clear() ;
	m_messages->clear() ;
	
	if ( !m_editor->save() ) 
		return FALSE;

	appendMessage( "File '" + m_editor->getFilename() + "' saved" ) ;
	m_simulator->setFilename( m_editor->getFilename() ) ;
		
	if ( m_simulator->compile() == TRUE ) {
		appendMessage( "***Compile Success*** " ) ;
		return TRUE ;
	} else {
		appendMessage( "***Compile Failed*** " ) ;
		return FALSE ;
	}
}


void KPicoSim::startSim()
{
	KIconLoader * ldr = KGlobal::iconLoader() ;

	if ( !m_simulationMode ) {
		if ( compile() ) {
			setCaption( m_editor->getFilename() + " [Debugging]" ) ;
			m_debugMenu->changeItem( START_SIM_ID, ldr->loadIcon( "stop", KIcon::Small ), "Stop Debug" ) ;
			m_debugBar->setButton( START_SIM_ID, true ) ;

			m_simulator->reset() ;
			m_nrInstructions = 0 ;
			m_simulationMode = TRUE ;
		}
	} else {
		if ( m_simulator->isRunning() )
			startStop() ;
			
		setCaption( m_editor->getFilename() ) ;
		m_debugMenu->changeItem( START_SIM_ID, ldr->loadIcon( "run", KIcon::Small ), "Start Debug" ) ;
		m_debugBar->setButton( START_SIM_ID, false ) ;
		m_editor->clearExecutionMarker() ;
		m_simulationMode = FALSE ;
	}
	
	if ( m_simulationMode ) {
		statusBar()->changeItem( QString( "Mode: Debug" ), 0 ) ;
	} else {
		statusBar()->changeItem( QString( "Mode: Edit" ), 0 ) ;
	}
	
	m_debugMenu->setItemEnabled( RUN_ID, m_simulationMode ) ;
	m_debugMenu->setItemEnabled( NEXT_ID, m_simulationMode ) ;
	m_debugMenu->setItemEnabled( INTERRUPT_ID, m_simulationMode ) ;
	m_debugMenu->setItemEnabled( RESET_ID, m_simulationMode ) ;

	m_debugBar->setItemEnabled( RUN_ID, m_simulationMode ) ;
	m_debugBar->setItemEnabled( NEXT_ID, m_simulationMode ) ;
	m_debugBar->setItemEnabled( INTERRUPT_ID, m_simulationMode ) ;
	m_debugBar->setItemEnabled( RESET_ID, m_simulationMode ) ;

}
/*
void KPicoSim::removeIOPort( KPortView* ioport )
{
	m_ioList.removeRef( ioport ) ;
}
*/
void KPicoSim::closeGUI()
{
	KSimpleConfig config( "kpicosim" ) ;

	config.setGroup( "Peripherals" ) ;
	
	config.writeEntry( "serial", m_serialView != NULL ) ;
	config.writeEntry( "scratchpad", m_scratchpadView != NULL ) ; 
	config.writeEntry( "filename", m_editor->getFilename() ) ;
	config.writeEntry( "bitfile", m_bitfile ) ;

/*
	config.writeEntry( "numIOPorts", m_ioList.count() ) ;
	for ( int i = 0 ; i < m_ioList.count() ; i++ ) {
		QString group ;	
		group.sprintf( "IO Port %d", i ) ;
		m_ioList.at(i)->writeConfig( config, group ) ;
	}
*/
}

void KPicoSim::openGUI()
{
	KSimpleConfig config( "kpicosim" ) ;

	config.setGroup( "Peripherals" ) ;

	if ( config.readPropertyEntry( "serial", QVariant::Bool ).toBool() )     
		showSerialPort() ;
	if ( config.readPropertyEntry( "scratchpad", QVariant::Bool ).toBool() )	
		showScratchpad() ;
	m_editor->open( config.readEntry( "filename" ) ) ;
	m_bitfile = config.readEntry( "bitfile" ) ;

/*
	int nports = config.readPropertyEntry( "numIOPorts", QVariant::Int ).toInt() ;

	for ( int i = 0 ; i < nports ; i++ ) {
		QString group ;
		group.sprintf( "IO Port %d", i ) ;
		KPortView * ioport = new KPortView( m_simulator->getCpu(), 0 ) ;
		ioport->readConfig( config, group ) ;
		m_ioList.append( ioport ) ;
		addDockWindow( ioport, DockRight ) ;
		connect( ioport, SIGNAL( closing( KPortView* ) ), this, SLOT( removeIOPort( KPortView* ) ) ) ;
	}
*/
}

#include "kpicosim.moc"