source: branches/virt simple/UFormMain.pas

Last change on this file was 89, checked in by chronos, 8 years ago
  • Modified: Start/stop virtual machine by buttons. Thread moved from main form to TMachine class.
File size: 5.5 KB
Line 
1unit UFormMain;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
9 ExtCtrls, StdCtrls, ActnList, fgl, UMachine, UBigInt, UCompiler;
10
11type
12
13{ TForm1 }
14
15 TForm1 = class(TForm)
16 ASave: TAction;
17 ActionList1: TActionList;
18 ButtonCompile: TButton;
19 ButtonStart: TButton;
20 ButtonStop: TButton;
21 Label1: TLabel;
22 Label2: TLabel;
23 Label3: TLabel;
24 Label4: TLabel;
25 Label5: TLabel;
26 Label6: TLabel;
27 ListViewMemory: TListView;
28 ListViewInstructions: TListView;
29 ListViewRegisters: TListView;
30 ListViewSpecialRegisters: TListView;
31 MemoSource: TMemo;
32 MemoOutput: TMemo;
33 Timer1: TTimer;
34 procedure ASaveExecute(Sender: TObject);
35 procedure ButtonCompileClick(Sender: TObject);
36 procedure ButtonStartClick(Sender: TObject);
37 procedure ButtonStopClick(Sender: TObject);
38 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
39 procedure FormCreate(Sender: TObject);
40 procedure FormDestroy(Sender: TObject);
41 procedure FormShow(Sender: TObject);
42 procedure ListViewInstructionsData(Sender: TObject; Item: TListItem);
43 procedure ListViewMemoryData(Sender: TObject; Item: TListItem);
44 procedure ListViewRegistersData(Sender: TObject; Item: TListItem);
45 procedure ListViewSpecialRegistersData(Sender: TObject; Item: TListItem);
46 procedure Timer1Timer(Sender: TObject);
47 private
48 Console: string;
49 procedure ReloadMemory;
50 procedure DoSystemCall(Index: Integer);
51 procedure UpdateInterface;
52 public
53 Machine: TMachine;
54 Compiler: TCompiler;
55 end;
56
57var
58 Form1: TForm1;
59
60implementation
61
62const
63 DefaultFileName = 'example.vasm';
64
65{$R *.lfm}
66
67{ TForm1 }
68
69procedure TForm1.FormCreate(Sender: TObject);
70begin
71 Machine := TMachine.Create;
72 Machine.OnSystemCall := DoSystemCall;
73 Compiler := TCompiler.Create;
74end;
75
76procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
77begin
78 Machine.Stop;
79 ASave.Execute;
80end;
81
82procedure TForm1.ButtonCompileClick(Sender: TObject);
83begin
84 Compiler.Lines.Assign(MemoSource.Lines);
85 Compiler.Compile(Machine.Instructions);
86end;
87
88procedure TForm1.ASaveExecute(Sender: TObject);
89begin
90 MemoSource.Lines.SaveToFile(DefaultFileName);
91end;
92
93procedure TForm1.ButtonStartClick(Sender: TObject);
94begin
95 Machine.Start;
96 UpdateInterface;
97end;
98
99procedure TForm1.ButtonStopClick(Sender: TObject);
100begin
101 Machine.Stop;
102 UpdateInterface;
103end;
104
105procedure TForm1.FormDestroy(Sender: TObject);
106begin
107 FreeAndNil(Compiler);
108 FreeAndNil(Machine);
109end;
110
111procedure TForm1.FormShow(Sender: TObject);
112begin
113 MemoSource.Lines.LoadFromFile(DefaultFileName);
114 ReloadMemory;
115 UpdateInterface;
116 with Machine do begin
117 Init(32000, 16);
118{ with Instructions do begin
119 AddInst(opNoOperation);
120 AddInst(opIncrement, ptRegister, False, 0);
121 AddInst(opLoad, ptRegister, False, 1, ptRegister, False, 0);
122 AddInst(opLoad, ptConst, True, 2, ptRegister, False, 1);
123 AddInst(opOutput, ptConst, False, 0, ptRegister, False, 1);
124 AddInst(opPush, ptConst, False, 1);
125 AddInst(opPop, ptConst, True, 10);
126 AddInst(opLoad, ptRegister, False, 0, ptConst, False, Ord('A'));
127 AddInst(opSystemCall, ptConst, False, 0);
128 AddInst(opLoad, ptRegister, False, 0, ptConst, False, 1000);
129 AddInst(opSystemCall, ptConst, False, 2);
130 AddInst(opJumpAbsolute, ptConst, False, 0);
131 AddInst(opLoad, ptRegister, True, 4, ptRegister, True, 3);
132 AddInst(opHalt);
133 end;
134 }
135 end;
136end;
137
138procedure TForm1.ListViewInstructionsData(Sender: TObject; Item: TListItem);
139begin
140 if Item.Index < Machine.Instructions.Count then begin
141 Item.Caption := IntToStr(Item.Index);
142 Item.SubItems.Add(Machine.Instructions[Item.Index].GetString);
143 end;
144end;
145
146procedure TForm1.ListViewMemoryData(Sender: TObject; Item: TListItem);
147begin
148 if Item.Index < Machine.Memory.Count then
149 begin
150 Item.Caption := IntToHex(Item.Index, 8);
151 Item.SubItems.Add(IntToHex(Machine.Memory[Item.Index], 8));
152 end;
153end;
154
155procedure TForm1.ListViewRegistersData(Sender: TObject; Item: TListItem);
156begin
157 if Item.Index < Machine.Registers.Count then
158 begin
159 Item.Caption := 'R' + IntToStr(Item.Index);
160 Item.SubItems.Add(IntToHex(Machine.Registers[Item.Index], 8));
161 end;
162end;
163
164procedure TForm1.ListViewSpecialRegistersData(Sender: TObject; Item: TListItem);
165begin
166 if Item.Index < 2 then
167 begin
168 if Item.Index = 0 then begin
169 Item.Caption := 'IP';
170 Item.SubItems.Add(IntToHex(Machine.IP, 8));
171 end else
172 if Item.Index = 1 then begin
173 Item.Caption := 'SP';
174 Item.SubItems.Add(IntToHex(Machine.SP, 8));
175 end;
176 end;
177end;
178
179procedure TForm1.Timer1Timer(Sender: TObject);
180begin
181 ReloadMemory;
182 MemoOutput.Text := Console;
183end;
184
185procedure TForm1.ReloadMemory;
186begin
187 ListViewMemory.Items.Count := Machine.Memory.Count;
188 ListViewMemory.Refresh;
189 ListViewRegisters.Items.Count := Machine.Registers.Count;
190 ListViewRegisters.Refresh;
191 ListViewSpecialRegisters.Items.Count := 2;
192 ListViewSpecialRegisters.Refresh;
193 ListViewInstructions.Items.Count := Machine.Instructions.Count;
194 ListViewInstructions.Refresh;
195end;
196
197procedure TForm1.DoSystemCall(Index: Integer);
198begin
199 with Machine do
200 if Index = 0 then begin
201 // Write character to output
202 Console := Console + Chr((Registers[0]) and $ff);
203 end else
204 if Index = 2 then begin
205 // Sleep in milliseconds
206 Sleep(Registers[0]);
207 end;
208end;
209
210procedure TForm1.UpdateInterface;
211begin
212 ButtonStart.Enabled := not Machine.Running;
213 ButtonStop.Enabled := Machine.Running;
214end;
215
216end.
217
Note: See TracBrowser for help on using the repository browser.