source: branches/Machine/UFormMain.pas

Last change on this file was 86, checked in by chronos, 10 years ago
  • Added: Virtual machine test.
File size: 2.7 KB
Line 
1unit UFormMain;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
9 StdCtrls, UMachine, UCompiler;
10
11type
12
13 { TForm1 }
14
15 TForm1 = class(TForm)
16 ButtonRun: TButton;
17 ListView1: TListView;
18 ListView2: TListView;
19 procedure ButtonRunClick(Sender: TObject);
20 procedure FormCreate(Sender: TObject);
21 procedure FormDestroy(Sender: TObject);
22 procedure FormShow(Sender: TObject);
23 procedure ListView1Data(Sender: TObject; Item: TListItem);
24 procedure ListView2Data(Sender: TObject; Item: TListItem);
25 private
26 procedure ShowMemory;
27 procedure ShowRegisters;
28 public
29 Machine: TMachine;
30 Compiler: TCompiler;
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;
45 Compiler := TCompiler.Create;
46end;
47
48procedure TForm1.ButtonRunClick(Sender: TObject);
49begin
50 Machine.Run;
51 ShowMemory;
52 ShowRegisters;
53end;
54
55procedure TForm1.FormDestroy(Sender: TObject);
56begin
57 Compiler.Free;
58 Machine.Free;
59end;
60
61procedure TForm1.FormShow(Sender: TObject);
62var
63 SourceCode: TStringList;
64 I: Integer;
65begin
66 SourceCode := TStringList.Create;
67 with SourceCode do begin
68 Add('DEF IP 0^S');
69 Add('DEF COUNT 0^R');
70 Add('DEF SOURCE 1^R');
71 Add('DEF DEST 2^R');
72 Add('CP COUNT, 16');
73 Add('CP SOURCE, 0');
74 Add('CP DEST, 160');
75 Add('LABEL LOOP');
76 Add('CP DEST^M, SOURCE^M');
77 Add('ADD SOURCE');
78 Add('ADD DEST');
79 Add('SUB COUNT');
80 Add('SKIP COUNT');
81 Add('CP IP, LOOP');
82 Add('ADD 6^R, 2');
83 Add('HALT');
84 end;
85 Compiler.Compile(SourceCode);
86 SourceCode.Free;
87 SetLength(Machine.Memory, 10000);
88 for I := 0 to Length(Compiler.Code) - 1 do
89 Machine.Memory[I] := Compiler.Code[I];
90 ShowMemory;
91 ShowRegisters;
92end;
93
94procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
95var
96 Data: string;
97 I: Integer;
98begin
99 if (Item.Index >= 0) and (Item.Index < (Length(Machine.Memory) div 16)) then begin
100 Item.Caption := IntToHex(Item.Index * 16, 8);
101 Data := '';
102 for I := 0 to 15 do
103 Data := Data + ' ' + IntToHex(Machine.Memory[Item.Index * 16 + I], 2);
104 Item.SubItems.Add(Data);
105 end;
106end;
107
108procedure TForm1.ListView2Data(Sender: TObject; Item: TListItem);
109begin
110 if (Item.Index >= 0) and (Item.Index < Length(Machine.Registers)) then begin
111 Item.Caption := IntToHex(Item.Index, 2);
112 Item.SubItems.Add(IntToStr(Machine.Registers[Item.Index]));
113 end;
114end;
115
116procedure TForm1.ShowMemory;
117begin
118 ListView1.Items.Count := Length(Machine.Memory) div 16;
119 ListView1.Refresh;
120end;
121
122procedure TForm1.ShowRegisters;
123begin
124 ListView2.Items.Count := Length(Machine.Registers);
125 ListView2.Refresh;
126end;
127
128end.
129
Note: See TracBrowser for help on using the repository browser.