1 | unit UFormMain;
|
---|
2 |
|
---|
3 | {$mode delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
|
---|
9 | ComCtrls, UCpu, UMachine, UInstructionWriter;
|
---|
10 |
|
---|
11 | type
|
---|
12 |
|
---|
13 | { TFormMain }
|
---|
14 |
|
---|
15 | TFormMain = class(TForm)
|
---|
16 | ButtonAssembler: TButton;
|
---|
17 | ButtonCompilerPascal: TButton;
|
---|
18 | ButtonClearMemory: TButton;
|
---|
19 | ButtonScreen: TButton;
|
---|
20 | ButtonDisassembler: TButton;
|
---|
21 | ButtonMemory: TButton;
|
---|
22 | ButtonCpuState: TButton;
|
---|
23 | ButtonConsole: TButton;
|
---|
24 | ButtonStart: TButton;
|
---|
25 | ButtonStop: TButton;
|
---|
26 | procedure ButtonAssemblerClick(Sender: TObject);
|
---|
27 | procedure ButtonClearMemoryClick(Sender: TObject);
|
---|
28 | procedure ButtonCompilerPascalClick(Sender: TObject);
|
---|
29 | procedure ButtonConsoleClick(Sender: TObject);
|
---|
30 | procedure ButtonCpuStateClick(Sender: TObject);
|
---|
31 | procedure ButtonMemoryClick(Sender: TObject);
|
---|
32 | procedure ButtonScreenClick(Sender: TObject);
|
---|
33 | procedure ButtonDisassemblerClick(Sender: TObject);
|
---|
34 | procedure ButtonStartClick(Sender: TObject);
|
---|
35 | procedure ButtonStopClick(Sender: TObject);
|
---|
36 | procedure FormCreate(Sender: TObject);
|
---|
37 | procedure FormDestroy(Sender: TObject);
|
---|
38 | procedure FormShow(Sender: TObject);
|
---|
39 | private
|
---|
40 | procedure LoadProgram;
|
---|
41 | public
|
---|
42 | Machine: TMachine;
|
---|
43 | InstructionWriter: TInstructionWriter;
|
---|
44 | end;
|
---|
45 |
|
---|
46 | var
|
---|
47 | FormMain: TFormMain;
|
---|
48 |
|
---|
49 |
|
---|
50 | implementation
|
---|
51 |
|
---|
52 | {$R *.lfm}
|
---|
53 |
|
---|
54 | uses
|
---|
55 | UFormDisassembler, UFormMemory, UFormCpuState, UFormScreen, UFormConsole,
|
---|
56 | UFormAssembler, UFormCompilerPascal;
|
---|
57 |
|
---|
58 | { TFormMain }
|
---|
59 |
|
---|
60 | procedure TFormMain.ButtonStartClick(Sender: TObject);
|
---|
61 | begin
|
---|
62 | Machine.Cpu.Start;
|
---|
63 | end;
|
---|
64 |
|
---|
65 | procedure TFormMain.ButtonDisassemblerClick(Sender: TObject);
|
---|
66 | begin
|
---|
67 | if not Assigned(FormDisassembler) then
|
---|
68 | FormDisassembler := TFormDisassembler.Create(Self);
|
---|
69 | FormDisassembler.Disassembler.Cpu := Machine.Cpu;
|
---|
70 | FormDisassembler.Show;
|
---|
71 | FormDisassembler.Reload;
|
---|
72 | end;
|
---|
73 |
|
---|
74 | procedure TFormMain.ButtonMemoryClick(Sender: TObject);
|
---|
75 | begin
|
---|
76 | if not Assigned(FormMemory) then
|
---|
77 | FormMemory := TFormMemory.Create(Self);
|
---|
78 | FormMemory.Machine := Machine;
|
---|
79 | FormMemory.Show;
|
---|
80 | FormMemory.Reload;
|
---|
81 | end;
|
---|
82 |
|
---|
83 | procedure TFormMain.ButtonScreenClick(Sender: TObject);
|
---|
84 | begin
|
---|
85 | if not Assigned(FormScreen) then
|
---|
86 | FormScreen := TFormScreen.Create(Self);
|
---|
87 | FormScreen.Machine := Machine;
|
---|
88 | FormScreen.Show;
|
---|
89 | FormScreen.Reload;
|
---|
90 | end;
|
---|
91 |
|
---|
92 | procedure TFormMain.ButtonCpuStateClick(Sender: TObject);
|
---|
93 | begin
|
---|
94 | if not Assigned(FormCpuState) then
|
---|
95 | FormCpuState := TFormCpuState.Create(Self);
|
---|
96 | FormCpuState.Machine := Machine;
|
---|
97 | FormCpuState.Show;
|
---|
98 | FormCpuState.Reload;
|
---|
99 | end;
|
---|
100 |
|
---|
101 | procedure TFormMain.ButtonConsoleClick(Sender: TObject);
|
---|
102 | begin
|
---|
103 | if not Assigned(FormConsole) then
|
---|
104 | FormConsole := TFormConsole.Create(Self);
|
---|
105 | FormConsole.Machine := Machine;
|
---|
106 | FormConsole.Show;
|
---|
107 | end;
|
---|
108 |
|
---|
109 | procedure TFormMain.ButtonAssemblerClick(Sender: TObject);
|
---|
110 | begin
|
---|
111 | if not Assigned(FormAssembler) then
|
---|
112 | FormAssembler := TFormAssembler.Create(Self);
|
---|
113 | FormAssembler.Assembler.InstructionWriter.Cpu := Machine.Cpu;
|
---|
114 | FormAssembler.Show;
|
---|
115 | end;
|
---|
116 |
|
---|
117 | procedure TFormMain.ButtonClearMemoryClick(Sender: TObject);
|
---|
118 | begin
|
---|
119 | Machine.ClearMemory;
|
---|
120 | end;
|
---|
121 |
|
---|
122 | procedure TFormMain.ButtonCompilerPascalClick(Sender: TObject);
|
---|
123 | begin
|
---|
124 | if not Assigned(FormCompilerPascal) then
|
---|
125 | FormCompilerPascal := TFormCompilerPascal.Create(Self);
|
---|
126 | FormCompilerPascal.CompilerPascal.Reader.Cpu := Machine.Cpu;
|
---|
127 | FormCompilerPascal.Show;
|
---|
128 | end;
|
---|
129 |
|
---|
130 | procedure TFormMain.ButtonStopClick(Sender: TObject);
|
---|
131 | begin
|
---|
132 | Machine.Cpu.Stop;
|
---|
133 | end;
|
---|
134 |
|
---|
135 | procedure TFormMain.FormCreate(Sender: TObject);
|
---|
136 | begin
|
---|
137 | Machine := TMachine.Create;
|
---|
138 | InstructionWriter := TInstructionWriter.Create;
|
---|
139 | InstructionWriter.Cpu := Machine.Cpu;
|
---|
140 | end;
|
---|
141 |
|
---|
142 | procedure TFormMain.FormDestroy(Sender: TObject);
|
---|
143 | begin
|
---|
144 | InstructionWriter.Free;
|
---|
145 | Machine.Free;
|
---|
146 | end;
|
---|
147 |
|
---|
148 | procedure TFormMain.FormShow(Sender: TObject);
|
---|
149 | begin
|
---|
150 | LoadProgram;
|
---|
151 | end;
|
---|
152 |
|
---|
153 | procedure TFormMain.LoadProgram;
|
---|
154 | var
|
---|
155 | R1: Byte;
|
---|
156 | R2: Byte;
|
---|
157 | R3: Byte;
|
---|
158 | R4: Byte;
|
---|
159 | LabelText: Integer;
|
---|
160 | LabelText2: Integer;
|
---|
161 | LabelPrint: Integer;
|
---|
162 | LabelClearScreen: Integer;
|
---|
163 | LabelPrintLoop: Integer;
|
---|
164 | LabelConsole: Integer;
|
---|
165 | LabelMain: Integer;
|
---|
166 | begin
|
---|
167 | R1 := 1;
|
---|
168 | R2 := 2;
|
---|
169 | R3 := 3;
|
---|
170 | R4 := 4;
|
---|
171 | with InstructionWriter do begin
|
---|
172 | Init;
|
---|
173 | DataPrefix32; Loadi(R1, $12345678);
|
---|
174 | DataPrefix8; Loadi(R1, $ff);
|
---|
175 | DataPrefix8; Convert(R1);
|
---|
176 |
|
---|
177 | LabelMain := $100;
|
---|
178 | Jump(LabelMain);
|
---|
179 |
|
---|
180 | LabelText := IP;
|
---|
181 | WriteString('Hello World!');
|
---|
182 |
|
---|
183 | LabelText2 := IP;
|
---|
184 | WriteString('Good day!');
|
---|
185 | Write8(0);
|
---|
186 | Write8(0);
|
---|
187 | Write8(0);
|
---|
188 | Write8(0);
|
---|
189 |
|
---|
190 | LabelPrint := IP;
|
---|
191 | Push(R3);
|
---|
192 | Push(R2);
|
---|
193 | Push(R1);
|
---|
194 | LabelPrintLoop := IP;
|
---|
195 | DataPrefix8; LoadMem(R3, R1);
|
---|
196 | AddrPrefix8; DataPrefix8; Output(0, R3);
|
---|
197 | Increment(R1);
|
---|
198 | Decrement(R2);
|
---|
199 | Test(R2);
|
---|
200 | AddrPrefix8; JumpRelNotZero(LabelPrintLoop);
|
---|
201 | Pop(R1);
|
---|
202 | Pop(R2);
|
---|
203 | Pop(R3);
|
---|
204 | Return;
|
---|
205 |
|
---|
206 | IP := LabelMain;
|
---|
207 |
|
---|
208 | // Print Hello world text
|
---|
209 | Loadi(R1, LabelText);
|
---|
210 | Loadi(R2, 12);
|
---|
211 | Call(LabelPrint);
|
---|
212 | Loadi(R1, LabelText2);
|
---|
213 | Loadi(R2, 9);
|
---|
214 | Call(LabelPrint);
|
---|
215 |
|
---|
216 | // Draw to video memory
|
---|
217 | Loadi(R1, Machine.Screen.MemoryBase);
|
---|
218 | Loadi(R2, $10000);
|
---|
219 | Loadi(R3, $7f);
|
---|
220 | LabelClearScreen := IP;
|
---|
221 | DataPrefix8; StoreMem(R1, R3);
|
---|
222 | Increment(R1);
|
---|
223 | Decrement(R2);
|
---|
224 | Test(R2);
|
---|
225 | AddrPrefix8; JumpRelNotZero(LabelClearScreen);
|
---|
226 |
|
---|
227 | // Update screen area
|
---|
228 | Push(R1);
|
---|
229 | Loadi(R1, 0);
|
---|
230 | Output(4, R1);
|
---|
231 | Loadi(R1, $10000);
|
---|
232 | Output(5, R1);
|
---|
233 | Pop(R1);
|
---|
234 |
|
---|
235 | // Read keyboard and print to console
|
---|
236 | Loadi(R1, 0);
|
---|
237 | LabelConsole := IP;
|
---|
238 | Increment(R1);
|
---|
239 | Loadi(R2, $ff);
|
---|
240 | DataPrefix8; StoreMem(R2, R1);
|
---|
241 | AddrPrefix8; DataPrefix8; Input(R2, 0);
|
---|
242 | AddrPrefix8; DataPrefix8; Output(0, R2);
|
---|
243 | Jump(LabelConsole);
|
---|
244 |
|
---|
245 | Halt;
|
---|
246 | end;
|
---|
247 | end;
|
---|
248 |
|
---|
249 | end.
|
---|
250 |
|
---|