1 | unit UFormMain;
|
---|
2 |
|
---|
3 | {$mode delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
|
---|
9 | ExtCtrls, StdCtrls, ActnList, fgl, UMachine, UBigInt, UCompiler;
|
---|
10 |
|
---|
11 | type
|
---|
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 |
|
---|
57 | var
|
---|
58 | Form1: TForm1;
|
---|
59 |
|
---|
60 | implementation
|
---|
61 |
|
---|
62 | const
|
---|
63 | DefaultFileName = 'example.vasm';
|
---|
64 |
|
---|
65 | {$R *.lfm}
|
---|
66 |
|
---|
67 | { TForm1 }
|
---|
68 |
|
---|
69 | procedure TForm1.FormCreate(Sender: TObject);
|
---|
70 | begin
|
---|
71 | Machine := TMachine.Create;
|
---|
72 | Machine.OnSystemCall := DoSystemCall;
|
---|
73 | Compiler := TCompiler.Create;
|
---|
74 | end;
|
---|
75 |
|
---|
76 | procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
|
---|
77 | begin
|
---|
78 | Machine.Stop;
|
---|
79 | ASave.Execute;
|
---|
80 | end;
|
---|
81 |
|
---|
82 | procedure TForm1.ButtonCompileClick(Sender: TObject);
|
---|
83 | begin
|
---|
84 | Compiler.Lines.Assign(MemoSource.Lines);
|
---|
85 | Compiler.Compile(Machine.Instructions);
|
---|
86 | end;
|
---|
87 |
|
---|
88 | procedure TForm1.ASaveExecute(Sender: TObject);
|
---|
89 | begin
|
---|
90 | MemoSource.Lines.SaveToFile(DefaultFileName);
|
---|
91 | end;
|
---|
92 |
|
---|
93 | procedure TForm1.ButtonStartClick(Sender: TObject);
|
---|
94 | begin
|
---|
95 | Machine.Start;
|
---|
96 | UpdateInterface;
|
---|
97 | end;
|
---|
98 |
|
---|
99 | procedure TForm1.ButtonStopClick(Sender: TObject);
|
---|
100 | begin
|
---|
101 | Machine.Stop;
|
---|
102 | UpdateInterface;
|
---|
103 | end;
|
---|
104 |
|
---|
105 | procedure TForm1.FormDestroy(Sender: TObject);
|
---|
106 | begin
|
---|
107 | FreeAndNil(Compiler);
|
---|
108 | FreeAndNil(Machine);
|
---|
109 | end;
|
---|
110 |
|
---|
111 | procedure TForm1.FormShow(Sender: TObject);
|
---|
112 | begin
|
---|
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;
|
---|
136 | end;
|
---|
137 |
|
---|
138 | procedure TForm1.ListViewInstructionsData(Sender: TObject; Item: TListItem);
|
---|
139 | begin
|
---|
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;
|
---|
144 | end;
|
---|
145 |
|
---|
146 | procedure TForm1.ListViewMemoryData(Sender: TObject; Item: TListItem);
|
---|
147 | begin
|
---|
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;
|
---|
153 | end;
|
---|
154 |
|
---|
155 | procedure TForm1.ListViewRegistersData(Sender: TObject; Item: TListItem);
|
---|
156 | begin
|
---|
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;
|
---|
162 | end;
|
---|
163 |
|
---|
164 | procedure TForm1.ListViewSpecialRegistersData(Sender: TObject; Item: TListItem);
|
---|
165 | begin
|
---|
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;
|
---|
177 | end;
|
---|
178 |
|
---|
179 | procedure TForm1.Timer1Timer(Sender: TObject);
|
---|
180 | begin
|
---|
181 | ReloadMemory;
|
---|
182 | MemoOutput.Text := Console;
|
---|
183 | end;
|
---|
184 |
|
---|
185 | procedure TForm1.ReloadMemory;
|
---|
186 | begin
|
---|
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;
|
---|
195 | end;
|
---|
196 |
|
---|
197 | procedure TForm1.DoSystemCall(Index: Integer);
|
---|
198 | begin
|
---|
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;
|
---|
208 | end;
|
---|
209 |
|
---|
210 | procedure TForm1.UpdateInterface;
|
---|
211 | begin
|
---|
212 | ButtonStart.Enabled := not Machine.Running;
|
---|
213 | ButtonStop.Enabled := Machine.Running;
|
---|
214 | end;
|
---|
215 |
|
---|
216 | end.
|
---|
217 |
|
---|