-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharmsimGUI.cs
840 lines (754 loc) · 32.9 KB
/
armsimGUI.cs
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
// Filename: armsimGUI.cs
// Author: Taylor Eernisse
// Date: 9/18/12
using System;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using CommandLine;
using System.Diagnostics;
using System.Threading;
namespace armsimGUI
{
/// <summary>
/// Class: armsimGUI
/// Purpose: Contains the logic to run the simulator as well as handle GUI events and updates
/// Methods: armsimGUI(string[])
/// private void openSourceToolBtn_Click(object, EventArgs)
/// protected override bool ProcessCmdKey(ref Message, Keys)
/// private void runToolBtn_Click(object, EventArgs)
/// private void stepToolBtn_Click(object, EventArgs)
/// private void stopToolBtn_Click(object, EventArgs)
/// private void resetToolBtn_Click(object, EventArgs)
/// private void findBtn_Click(object, EventArgs)
/// private void traceBtn_Click(object, EventArgs)
/// private void StartCPU()
/// private void BreakExecution()
/// private void SingleStep()
/// private void ResetToLastExe()
/// private void OpenFileDialog()
/// private void LoadFile(string)
/// private void updateGUI()
/// private void updateProgramCounterTextBox()
/// private void updateChecksumTextBox()
/// private int getChecksum()
/// public void updateRegistersPanel()
/// public void updateMemoryPanel()
/// public void updateFlagsPanel()
/// public void updateDisassemblyPanel()
/// private void updateFileOpenLabel()
/// </summary>
public partial class armsimGUI : Form, Observer
{
delegate void SetTextCallback(char text);
delegate void SetLabelTextCallback(string text, string dest);
Computer comp; // reference to the main Computer instance
StreamWriter tracefile = new StreamWriter(Directory.GetCurrentDirectory() + "\\trace.log", false); // trace file object to write to
string filename; // filename of opened executable
uint origPC; // holds original PC value for disassembly panel updating
uint finalPC; // hold final PC value for diassembly panel updating
private BackgroundWorker runThread;
private BackgroundWorker stepThread;
public armsimGUI(string[] args)
{
InitializeComponent();
comp = new Computer(this);
runThread = new BackgroundWorker();
runThread.DoWork += new DoWorkEventHandler(runThread_DoWork);
runThread.RunWorkerCompleted += new RunWorkerCompletedEventHandler(runThread_RunWorkerCompleted);
stepThread = new BackgroundWorker();
stepThread.DoWork += new DoWorkEventHandler(stepThread_DoWork);
stepThread.RunWorkerCompleted += new RunWorkerCompletedEventHandler(stepThread_RunWorkerCompleted);
updateRegistersPanel();
var options = new Options();
if (CommandLineParser.Default.ParseArguments(args, options)) // if command line arguments are valid...
{
if (options.Test)
{
testRAM.RunTests();
Computer.TestLoader();
CPU.TestCPU();
Trace.WriteLine("All tests passed!");
Trace.WriteLine("-----------------------------------"); // insert line for file readability
}
else if (options.TestFlags)
{
//comp.TestFlags();
//disassemblyListView.Items.Add(new ListViewItem(
//updateFlagsPanel();
//updateRegistersPanel();
comp.TestPutChar();
}
if (options.ELFInputFile != "")
{
if (options.Exec)
{
LoadAndRunFile(options.ELFInputFile);
}
else
{
LoadFile(options.ELFInputFile);
runToolBtn.Enabled = true;
stepToolBtn.Enabled = true;
resetToolBtn.Enabled = true;
}
}
}
// If any command line arguments are invalid, the CommandLine library displays the message as commented in the Options class.
// Additional error handling can be implemented by including an "else" statement here.
}
#region Event Handlers
//--------------------------------------------------------------
// Purpose: Event handler to open file open clicking the "Open Source" button
// Returns: nothing
//--------------------------------------------------------------
private void openSourceToolBtn_Click(object sender, EventArgs e)
{
OpenFileDialog();
runToolBtn.Enabled = true;
stepToolBtn.Enabled = true;
resetToolBtn.Enabled = true;
}
//--------------------------------------------------------------
// Purpose: Handles logic for hotkeys
// Returns: true if key combination is found
//--------------------------------------------------------------
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.O))
{
OpenFileDialog();
return true;
}
else if (keyData == (Keys.Control | Keys.F5))
{
StartCPU();
return true;
}
else if (keyData == (Keys.Control | Keys.F10))
{
SingleStep();
return true;
}
else if (keyData == (Keys.Control | Keys.B))
{
BreakExecution();
return true;
}
else if (keyData == (Keys.Control | Keys.R))
{
ResetToLastExe();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
//--------------------------------------------------------------
// Purpose: Runs the CPU when "Run" button is clicked
// Returns: nothing
//--------------------------------------------------------------
private void runToolBtn_Click(object sender, EventArgs e)
{
comp.stepBreakpoint = -1;
StartCPU();
}
//--------------------------------------------------------------
// Purpose: Executes a single step of the program when "Step" button is clicked
// Returns: nothing
//--------------------------------------------------------------
private void stepToolBtn_Click(object sender, EventArgs e)
{
SingleStep();
}
//--------------------------------------------------------------
// Purpose: Stops execution of the program when "Stop" button is clicked
// Returns: nothing
//--------------------------------------------------------------
private void stopToolBtn_Click(object sender, EventArgs e)
{
BreakExecution();
}
//--------------------------------------------------------------
// Purpose: Reloads last program into memory and resets all windows
// Returns: nothing
//--------------------------------------------------------------
private void resetToolBtn_Click(object sender, EventArgs e)
{
ResetToLastExe();
}
//--------------------------------------------------------------
// Purpose: Finds address in memoryListView when "Find" button is clicked
// Returns: nothing
//--------------------------------------------------------------
private void findBtn_Click(object sender, EventArgs e)
{
string address = findAddressTxtBox.Text;
int num;
if (address != "" && address.Length != 10 && (address[0] != 0 && address[1] != 'x') && !int.TryParse(address.Remove(0, 2), out num))
{
findAddressTxtBox.Text = "Invalid address; must follow syntax \"0x00000000\"";
}
else
{
updateMemoryPanel(true);
}
}
//--------------------------------------------------------------
// Purpose: Enables or disables trace when "Enable/Disable Trace" button is clicked
// based on the text currently displayed on the button
// Returns: nothing
//--------------------------------------------------------------
private void traceBtn_Click(object sender, EventArgs e)
{
if (!comp.running)
{
if (traceBtn.Text == "Disable Trace") tracefile.Dispose();
else tracefile = new StreamWriter(Directory.GetCurrentDirectory() + "\\trace.log", false);
traceBtn.Text = (traceBtn.Text == "Disable Trace") ? "Enable Trace" : "Disable Trace";
}
}
private void findAddressTxtBox_MouseClick(object sender, MouseEventArgs e)
{
if (findAddressTxtBox.Text == "Invalid address; must follow syntax \"0x00000000\"")
{
findAddressTxtBox.Text = "";
}
}
private void outputTxtBox_KeyPress(object sender, KeyPressEventArgs e)
{
Computer.AddCharToBuffer(e.KeyChar);
}
private void stepThread_DoWork(object sender, DoWorkEventArgs e)
{
ArrayList list = (ArrayList)e.Argument;
StreamWriter file = (StreamWriter)list[0];
Observer obs = (Observer)list[1];
comp.Step(file, obs);
}
private void stepThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
updateProgramCounterTextBox();
updateDisassemblyPanel();
updateMemoryPanel(false);
updateRegistersPanel();
updateFlagsPanel();
if (comp.end) stepToolBtn.Enabled = false;
}
private void runThread_DoWork(object sender, DoWorkEventArgs e)
{
ArrayList list = (ArrayList)e.Argument;
StreamWriter file = (StreamWriter)list[0];
Observer obs = (Observer)list[1];
comp.Run(file, obs);
}
private void runThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
updateGUI();
if (comp.breakpointend)
{
runToolBtn.Enabled = true;
}
else
{
stopToolBtn.Enabled = false;
stepToolBtn.Enabled = false;
}
}
private void breakPtRunBtn_Click(object sender, EventArgs e)
{
int val;
bool success = Int32.TryParse(stepNumTxtBox.Text, out val);
if (success && val > 0)
{
comp.stepBreakpoint = val - 1;
StartCPU();
}
else
{
stepNumTxtBox.Text = "Invalid";
}
}
private void executeBtn_Click(object sender, EventArgs e)
{
string data = executeTxtBox.Text;
if (data.Substring(0, 2) == "0x" && data.Length == 10)
{
try
{
string str = data.Substring(2);
Instruction i = Computer.GetCPU().Decode(UInt32.Parse(str, System.Globalization.NumberStyles.AllowHexSpecifier));
Computer.GetCPU().Execute(i, false);
}
catch { }
}
else { executeTxtBox.Text = "Invalid"; }
}
#endregion
#region Methods
//--------------------------------------------------------------
// Purpose: Runs the program currently loaded into memory
// Returns: nothing
//--------------------------------------------------------------
private void StartCPU()
{
runToolBtn.Enabled = false;
stopToolBtn.Enabled = true;
ArrayList items = new ArrayList();
items.Add(tracefile);
items.Add(this);
try { runThread.RunWorkerAsync(items); }
catch { }
}
public void SetLabelText(string text, string dest)
{
switch (dest)
{
case "stepNumLbl":
if (this.stepNumLbl.InvokeRequired)
{
SetLabelTextCallback d = new SetLabelTextCallback(SetLabelText);
this.Invoke(d, new object[] { text, dest });
}
else
{
this.stepNumLbl.Text = "StepNum: " + text;
}
break;
case "instrTextLbl":
if (this.instrTextLbl.InvokeRequired)
{
SetLabelTextCallback d = new SetLabelTextCallback(SetLabelText);
this.Invoke(d, new object[] { text, dest });
}
else
{
this.instrTextLbl.Text = "Instruction: " + text;
}
break;
case "disassemblyLbl":
if (this.disassemblyLbl.InvokeRequired)
{
SetLabelTextCallback d = new SetLabelTextCallback(SetLabelText);
this.Invoke(d, new object[] { text, dest });
}
else
{
this.disassemblyLbl.Text = "Disassembly: " + text;
}
break;
case "nextInstrLbl":
if (this.nextInstrLbl.InvokeRequired)
{
SetLabelTextCallback d = new SetLabelTextCallback(SetLabelText);
this.Invoke(d, new object[] { text, dest });
}
else
{
this.nextInstrLbl.Text = "Next instruction: " + text;
}
break;
case "nextDisasmLbl":
if (this.nextDisasmLbl.InvokeRequired)
{
SetLabelTextCallback d = new SetLabelTextCallback(SetLabelText);
this.Invoke(d, new object[] { text, dest });
}
else
{
this.nextDisasmLbl.Text = "Disassembly: " + text;
}
break;
}
}
public void SetText(char text)
{
if (this.outputTxtBox.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.outputTxtBox.Text += text;
}
}
//--------------------------------------------------------------
// Purpose: Stops execution of the current program
// Returns: nothing
//--------------------------------------------------------------
private void BreakExecution()
{
comp.SetEnd(true);
tracefile.Close();
runToolBtn.Enabled = true;
}
//--------------------------------------------------------------
// Purpose: Executes a single step of the program in memory
// Returns: nothing
//--------------------------------------------------------------
private void SingleStep()
{
Computer.TestInstructions();
ArrayList items = new ArrayList();
items.Add(tracefile);
items.Add(this);
try { stepThread.RunWorkerAsync(items); }
catch { }
}
//--------------------------------------------------------------
// Purpose: Loads the last file loaded
// Returns: nothing
//--------------------------------------------------------------
private void ResetToLastExe()
{
ResetRegisters();
LoadFile(filename);
ResetDisasmTesting();
updateGUI();
runToolBtn.Enabled = true;
stepToolBtn.Enabled = true;
stopToolBtn.Enabled = false;
comp.ResetEnd();
Computer.ResetInput();
try
{
tracefile = new StreamWriter(Directory.GetCurrentDirectory() + "\\trace.log", false);
}
catch { tracefile.Dispose(); }
}
private void ResetDisasmTesting()
{
stepNumTxtBox.Text = "";
stepNumLbl.Text = "StepNum:";
instrTextLbl.Text = "Instruction:";
disassemblyLbl.Text = "Disassembly:";
nextInstrLbl.Text = "Next instruction:";
nextDisasmLbl.Text = "Disassembly:";
}
//--------------------------------------------------------------
// Purpose: Opens a file dialog at the current directory to open executable files
// Returns: nothing
//--------------------------------------------------------------
private void OpenFileDialog()
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Executable files (*.exe)|*.exe";
if (open.ShowDialog(this) == DialogResult.OK)
{
ResetRegisters();
ResetDisasmTesting();
LoadFile(open.FileName);
stepToolBtn.Enabled = true;
}
}
//--------------------------------------------------------------
// Purpose: Loads the file <fileToLoad> and reads it into RAM
// Returns: nothing
//--------------------------------------------------------------
private void LoadFile(string fileToLoad)
{
try
{
comp.ResetRam();
comp.ResetEnd();
Computer.GetCPU().ClearFlags();
Memory ram = comp.progRAM;
filename = fileToLoad;
if (comp.ReadELF(fileToLoad))
{
origPC = Computer.GetCPU().registers.ReadWord((uint)regs.PC);
findAddressTxtBox.Text = "0x" + Computer.GetCPU().registers.ReadWord((uint)regs.PC).ToString("x8");
initializeDisassemblyPanel();
initializeMemoryPanel();
updateGUI();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void LoadAndRunFile(string file)
{
LoadFile(file);
StartCPU();
tracefile.Dispose();
Environment.Exit(0);
}
private void FindItemInMemoryPanel(string address)
{
uint n = ParseAddress(address.Length == 8 ? address : address.Substring(2, address.Length - 2));
FindItemInMemoryPanel(n);
}
private void FindItemInMemoryPanel(uint address)
{
string addr = "0x" + address.ToString("x8");
// locate the address in the Memory panel
ListViewItem i = memoryListView.FindItemWithText(addr, false, 0, false);
if (i != null) memoryListView.TopItem = i;
}
private static uint ParseAddress(string address)
{
// if <address> is not evenly divisible by 16, subtract the extra amount to ensure address location will succeed in ListView search
int n = Int32.Parse(address, System.Globalization.NumberStyles.AllowHexSpecifier);
int j = n % 16;
n -= j;
return (uint)n;
}
//--------------------------------------------------------------
// Purpose: Updates GUI components with current data
// Returns: nothing
//--------------------------------------------------------------
private void updateGUI()
{
updateChecksumTextBox();
updateProgramCounterTextBox();
updateRegistersPanel();
updateMemoryPanel(false);
updateFlagsPanel();
updateDisassemblyPanel();
updateFileOpenLabel();
}
//--------------------------------------------------------------
// Purpose: Updates Program Counter text to current value
// Returns: nothing
//--------------------------------------------------------------
private void updateProgramCounterTextBox()
{
Memory registers = comp.registers;
progCounterToolstripLbl.Text = "Program Counter: 0x" + registers.ReadWord((uint)regs.PC).ToString("x8");
}
//--------------------------------------------------------------
// Purpose: Updates Checksum textbox to current value
// Returns: nothing
//--------------------------------------------------------------
private void updateChecksumTextBox()
{
int checksum = getChecksum();
checksumToolStripLbl.Text = "Checksum: 0x" + checksum.ToString("x8");
}
//--------------------------------------------------------------
// Purpose: Gets the current checksum value
// Returns: int value of checksum
//--------------------------------------------------------------
private int getChecksum()
{
int checksum = Computer.calculateChecksum(comp.progRAM.memory);
return checksum;
}
private void updateStackPanel(bool findBtnClick)
{
uint stackpointer = ParseAddress(Computer.GetCPU().registers.ReadWord((uint)regs.SP).ToString("x8"));
stackpointer -= 0x00000050;
stackPanel.Items.Clear();
for (int i = 0; i < 0x00000b0; i += 16, stackpointer += 16)
{
ListViewItem item = new ListViewItem("0x" + stackpointer.ToString("x8"));
for (uint j = 0; j < 16; j++)
{
byte b = comp.progRAM.ReadByte(stackpointer | j);
item.SubItems.Add(b.ToString("x2"));
}
stackPanel.Items.Add(item);
}
//item.SubItems.Add("0x" + Computer.GetCPU().progRAM.ReadWord(stackpointer).ToString("x8"));
//item.SubItems.Add("0x" + Computer.GetCPU().progRAM.ReadWord(stackpointer + 4).ToString("x8"));
//item.SubItems.Add("0x" + Computer.GetCPU().progRAM.ReadWord(stackpointer + 8).ToString("x8"));
//item.SubItems.Add("0x" + Computer.GetCPU().progRAM.ReadWord(stackpointer + 12).ToString("x8"));
//stackPanel.Items.Add(item);
}
//--------------------------------------------------------------
// Purpose: Clears and repopulates the registers panel with current register data
// Returns: nothing
//--------------------------------------------------------------
private void updateRegistersPanel()
{
registerListView.Items.Clear();
foreach (regs value in Enum.GetValues(typeof(regs)))
{
uint val = comp.registers.ReadWord((uint)value);
ListViewItem item = new ListViewItem(value.ToString());
item.SubItems.Add("0x" + val.ToString("x8"));
registerListView.Items.Add(item);
}
}
//--------------------------------------------------------------
// Purpose: Updates the values of the rows within 50 lines before and
// after the top item of list. This is performed any time the
// "Find" button is clicked; thus, if the user scrolls to a position,
// the data will be false with the current implementation. This
// is performed to reduce lag between steps. An ideal
// solution would be to update only the rows currently in
// view, and connect the scroll event handlers to this method to
// update the newly-exposed rows, but I don't have time to
// implement this.
// Returns: nothing
//--------------------------------------------------------------
private void updateMemoryPanel(bool findBtnClick)
{
string address = findBtnClick ? findAddressTxtBox.Text : "";
if (findBtnClick) FindItemInMemoryPanel(address);
ListViewItem backup = memoryListView.TopItem;
int index = memoryListView.TopItem.Index > 50 ? memoryListView.TopItem.Index - 50 : memoryListView.TopItem.Index; // adjust index to start refreshing 50 slots before the top item
uint uaddress = 0;
int loopVal = index > 50 ? 100 : 100 - (50 - index); // to avoid referencing an index that doesn't exist
try
{
if (!findBtnClick) address = backup.SubItems[0].Text.Substring(2);
uaddress = Convert.ToUInt32(address, 16);
}
catch
{
if (!findBtnClick) address = comp.registers.ReadWord((uint)regs.PC).ToString("x8");
uaddress = Convert.ToUInt32(address, 16);
}
uint addr = (uaddress > 0x320) ? uaddress - 0x320 : uaddress; // subtract 50 * 16 from address
for (int i = 0; i < loopVal; i++, addr += 16)
{
if (addr < comp.progRAM.memory.Length)
{
backup = memoryListView.Items[index];
ListViewItem item = new ListViewItem(backup.Text);
//item.SubItems.Add(""); // populate extra space column with nothing
//uint firstWord = comp.progRAM.ReadWord(addr);
//uint secondWord = comp.progRAM.ReadWord(addr + 4);
//uint thirdWord = comp.progRAM.ReadWord(addr + 8);
//uint fourthWord = comp.progRAM.ReadWord(addr + 12);
for (uint j = 0; j < 16; j++)
{
byte b = comp.progRAM.ReadByte(addr | j);
item.SubItems.Add(b.ToString("x2"));
}
//item.SubItems.Add("0x" + firstWord.ToString("x8"));
//item.SubItems.Add("0x" + secondWord.ToString("x8"));
//item.SubItems.Add("0x" + thirdWord.ToString("x8"));
//item.SubItems.Add("0x" + fourthWord.ToString("x8"));
memoryListView.Items[index++] = item;
}
else break;
}
updateStackPanel(findBtnClick);
}
//--------------------------------------------------------------
// Purpose: Initializes the memory panel with data from memory
// Returns: nothing
//--------------------------------------------------------------
private void initializeMemoryPanel()
{
for (uint addr = 0; addr < comp.progRAM.memory.Length; )
{
uint firstWord = comp.progRAM.ReadWord(addr);
uint secondWord = comp.progRAM.ReadWord(addr + 4);
uint thirdWord = comp.progRAM.ReadWord(addr + 8);
uint fourthWord = comp.progRAM.ReadWord(addr + 12);
ListViewItem item = new ListViewItem("0x" + addr.ToString("x8"));
item.SubItems.Add("0x" + firstWord.ToString("x8"));
item.SubItems.Add("0x" + secondWord.ToString("x8"));
item.SubItems.Add("0x" + thirdWord.ToString("x8"));
item.SubItems.Add("0x" + fourthWord.ToString("x8"));
memoryListView.Items.Add(item);
addr += 16;
}
FindItemInMemoryPanel(Computer.GetCPU().registers.ReadWord((uint)regs.PC));
}
//--------------------------------------------------------------
// Purpose: Currently clears and updates flags panel with flag names and "false" values
// Returns: nothing
//--------------------------------------------------------------
private void updateFlagsPanel()
{
flagsListView.Items.Clear();
CPU c = Computer.GetCPU();
ListViewItem NFlag = new ListViewItem("N");
NFlag.SubItems.Add(c.NFlag.ToString());
ListViewItem ZFlag = new ListViewItem("Z");
ZFlag.SubItems.Add(c.ZFlag.ToString());
ListViewItem CFlag = new ListViewItem("C");
CFlag.SubItems.Add(c.CFlag.ToString());
ListViewItem FFlag = new ListViewItem("F");
FFlag.SubItems.Add(c.FFlag.ToString());
ListViewItem IFlag = new ListViewItem("I");
IFlag.SubItems.Add(c.IFlag.ToString());
flagsListView.Items.AddRange(new ListViewItem[] { NFlag, ZFlag, CFlag, FFlag, IFlag });
}
//--------------------------------------------------------------
// Purpose: Highlights the row corresponding with the current PC value
// Returns: nothing
//--------------------------------------------------------------
private void updateDisassemblyPanel()
{
uint pc = Computer.GetCPU().registers.ReadWord((uint)regs.PC);
if (pc > origPC && pc < finalPC - 4)
{
string strpc = "0x" + pc.ToString("x8");
ListViewItem i = disassemblyListView.FindItemWithText(strpc);
int index1 = i.Index;
for (int j = 0; j < disassemblyListView.Items.Count; j++) disassemblyListView.Items[j].Selected = false;
disassemblyListView.Items[index1].Selected = true;
}
}
//--------------------------------------------------------------
// Purpose: Clears and updates the disassembly panel with fake data to look good
// Returns: nothing
//--------------------------------------------------------------
private void initializeDisassemblyPanel()
{
disassemblyListView.Items.Clear();
disassemblyListView.FullRowSelect = true;
// back up program counter
uint data = 1;
uint pc = origPC; // create PC temp outside loop for efficiency reasons
bool end = false;
Memory mem = Computer.GetCPU().progRAM;
Computer.GetCPU().registers.WriteWord((uint)regs.PC, origPC); // ensure accurate PC value is stored in PC
while (!end)
{
data = Computer.GetCPU().progRAM.ReadWord(pc);
Instruction i = Computer.GetCPU().Decode(data);
ListViewItem item = new ListViewItem(i.disasm.address);
item.SubItems.Add(i.disasm.instrCode);
item.SubItems.Add(i.disasm.instruction);
item.SubItems.Add(i.disasm.value);
disassemblyListView.Items.Add(item);
pc += 4;
Computer.GetCPU().registers.WriteWord((uint)regs.PC, pc); // update PC for disassembly generation
if (data == 0)
{
end = true;
// erase the last row added to the ListView
disassemblyListView.Items.RemoveAt(disassemblyListView.Items.Count - 1);
}
}
finalPC = pc; // store final PC value
Computer.GetCPU().registers.WriteWord((uint)regs.PC, origPC); // restore value of program counter
}
//--------------------------------------------------------------
// Purpose: Sets the "Executable: " label at the bottom of the GUI
// to reflect the current filename
// Returns: nothing
//--------------------------------------------------------------
private void updateFileOpenLabel()
{
string[] parts = filename.Split('\\');
filenameLbl.Text = "Executable: " + parts[parts.Length - 1];
}
private void ResetRegisters()
{
for (uint i = 0; i < 64; i += 4)
{
Computer.GetCPU().registers.WriteWord(i, 0);
}
}
#endregion
private void disassemblyListView_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < disassemblyListView.Items.Count; i++)
{
if (disassemblyListView.Items[i].Selected)
{
comp.SetBreakpoint(disassemblyListView.Items[i].Text);
}
}
}
}
}