1 | unit UFormMain;
|
---|
2 |
|
---|
3 | {$mode delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
|
---|
9 | StdCtrls, UMachine, UCompiler;
|
---|
10 |
|
---|
11 | type
|
---|
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 |
|
---|
33 | var
|
---|
34 | Form1: TForm1;
|
---|
35 |
|
---|
36 | implementation
|
---|
37 |
|
---|
38 | {$R *.lfm}
|
---|
39 |
|
---|
40 | { TForm1 }
|
---|
41 |
|
---|
42 | procedure TForm1.FormCreate(Sender: TObject);
|
---|
43 | begin
|
---|
44 | Machine := TMachine.Create;
|
---|
45 | Compiler := TCompiler.Create;
|
---|
46 | end;
|
---|
47 |
|
---|
48 | procedure TForm1.ButtonRunClick(Sender: TObject);
|
---|
49 | begin
|
---|
50 | Machine.Run;
|
---|
51 | ShowMemory;
|
---|
52 | ShowRegisters;
|
---|
53 | end;
|
---|
54 |
|
---|
55 | procedure TForm1.FormDestroy(Sender: TObject);
|
---|
56 | begin
|
---|
57 | Compiler.Free;
|
---|
58 | Machine.Free;
|
---|
59 | end;
|
---|
60 |
|
---|
61 | procedure TForm1.FormShow(Sender: TObject);
|
---|
62 | var
|
---|
63 | SourceCode: TStringList;
|
---|
64 | I: Integer;
|
---|
65 | begin
|
---|
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;
|
---|
92 | end;
|
---|
93 |
|
---|
94 | procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
|
---|
95 | var
|
---|
96 | Data: string;
|
---|
97 | I: Integer;
|
---|
98 | begin
|
---|
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;
|
---|
106 | end;
|
---|
107 |
|
---|
108 | procedure TForm1.ListView2Data(Sender: TObject; Item: TListItem);
|
---|
109 | begin
|
---|
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;
|
---|
114 | end;
|
---|
115 |
|
---|
116 | procedure TForm1.ShowMemory;
|
---|
117 | begin
|
---|
118 | ListView1.Items.Count := Length(Machine.Memory) div 16;
|
---|
119 | ListView1.Refresh;
|
---|
120 | end;
|
---|
121 |
|
---|
122 | procedure TForm1.ShowRegisters;
|
---|
123 | begin
|
---|
124 | ListView2.Items.Count := Length(Machine.Registers);
|
---|
125 | ListView2.Refresh;
|
---|
126 | end;
|
---|
127 |
|
---|
128 | end.
|
---|
129 |
|
---|