source: branches/virtualcpu2/UFormMain.pas

Last change on this file was 154, checked in by chronos, 7 years ago
  • Added: More opcodes with handlers.
File size: 2.8 KB
Line 
1unit UFormMain;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
9 ComCtrls, Menus, UMachine;
10
11type
12
13 { TForm1 }
14
15 TForm1 = class(TForm)
16 ButtonRun: TButton;
17 ListViewMemory: TListView;
18 ListViewRegisters: TListView;
19 procedure ButtonRunClick(Sender: TObject);
20 procedure FormCreate(Sender: TObject);
21 procedure FormDestroy(Sender: TObject);
22 procedure FormShow(Sender: TObject);
23 procedure ListViewMemoryData(Sender: TObject; Item: TListItem);
24 procedure ListViewRegistersData(Sender: TObject; Item: TListItem);
25 private
26 procedure Example;
27 procedure ShowMemory;
28 procedure ShowRegisters;
29 public
30 Machine: TMachine;
31 end;
32
33var
34 Form1: TForm1;
35
36implementation
37
38{$R *.lfm}
39
40{ TForm1 }
41
42procedure TForm1.FormCreate(Sender: TObject);
43begin
44 Machine := TMachine.Create;
45end;
46
47procedure TForm1.ButtonRunClick(Sender: TObject);
48begin
49 Machine.Run;
50 ShowMemory;
51 ShowRegisters;
52end;
53
54procedure TForm1.FormDestroy(Sender: TObject);
55begin
56 Machine.Free;
57end;
58
59procedure TForm1.FormShow(Sender: TObject);
60begin
61 SetLength(Machine.Memory, 1000);
62
63 Example;
64 ShowMemory;
65 ShowRegisters;
66end;
67
68procedure TForm1.ListViewMemoryData(Sender: TObject; Item: TListItem);
69var
70 Data: string;
71 I: Integer;
72begin
73 if (Item.Index >= 0) and (Item.Index < (Length(Machine.Memory) div 16)) then begin
74 Item.Caption := IntToHex(Item.Index * 16, 8);
75 Data := '';
76 for I := 0 to 15 do
77 Data := Data + ' ' + IntToHex(Machine.Memory[Item.Index * 16 + I], 2);
78 Item.SubItems.Add(Data);
79 end;
80end;
81
82procedure TForm1.ListViewRegistersData(Sender: TObject; Item: TListItem);
83begin
84 if (Item.Index >= 0) and (Item.Index < Length(Machine.Registers)) then begin
85 Item.Caption := IntToHex(Item.Index, 2);
86 Item.SubItems.Add(IntToHex(Machine.Registers[Item.Index], 8));
87 end;
88end;
89
90procedure TForm1.Example;
91const
92 Acc = 0;
93 Source = 1;
94 Dest = 2;
95 Counter = 3;
96 Temp = 4;
97 Jump = 5;
98var
99 Writer: TInstructionWriter;
100 LoopAddr: Integer;
101begin
102 Writer := TInstructionWriter.Create;
103 with Writer do begin
104 Machine := Self.Machine;
105 AddGetDataSize(Temp);
106 AddCopyConst8(Temp, 1);
107
108 AddCopyConst(Counter, 8);
109 AddCopyConst(Source, $30);
110 AddCopyConst(Dest, $60);
111
112 LoopAddr := Addr;
113 AddCopyFromMem(Acc, Source);
114 AddCopyToMem(Acc, Dest);
115 AddInc(Source);
116 AddInc(Dest);
117 AddDec(Counter);
118 AddTestNotZero(Counter);
119 AddCopyConst8(Jump, LoopAddr - (Addr + 3));
120 AddJumpRelCond8(Jump);
121 AddHalt;
122 end;
123 Writer.Free;
124end;
125
126procedure TForm1.ShowMemory;
127begin
128 ListViewMemory.Items.Count := Length(Machine.Memory) div 16;
129 ListViewMemory.Refresh;
130end;
131
132procedure TForm1.ShowRegisters;
133begin
134 ListViewRegisters.Items.Count := Length(Machine.Registers);
135 ListViewRegisters.Refresh;
136end;
137
138
139end.
140
Note: See TracBrowser for help on using the repository browser.