source: branches/virtcpu fixed int/UFormMain.pas

Last change on this file was 215, checked in by chronos, 4 years ago
  • Added: TMachine class which contains CPU and peripherals.
  • Added: Execute TCpu inside background thread.
File size: 4.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, ExtCtrls, UCpu, UInstructionWriter, UMachine;
10
11type
12
13 { TForm1 }
14
15 TForm1 = class(TForm)
16 ButtonStart: TButton;
17 ButtonStop: TButton;
18 LabelTicks: TLabel;
19 ListViewMemory: TListView;
20 ListViewRegisters: TListView;
21 Memo1: TMemo;
22 Timer1: TTimer;
23 procedure ButtonStartClick(Sender: TObject);
24 procedure ButtonStopClick(Sender: TObject);
25 procedure FormCreate(Sender: TObject);
26 procedure FormDestroy(Sender: TObject);
27 procedure FormKeyPress(Sender: TObject; var Key: char);
28 procedure FormShow(Sender: TObject);
29 procedure ListViewMemoryData(Sender: TObject; Item: TListItem);
30 procedure ListViewRegistersData(Sender: TObject; Item: TListItem);
31 procedure Memo1KeyPress(Sender: TObject; var Key: char);
32 procedure Timer1Timer(Sender: TObject);
33 private
34 procedure ReloadMemoryDump;
35 procedure ReloadRegisterDump;
36 procedure SerialOutputExecute(Sender: TObject);
37 public
38 Machine: TMachine;
39 InstructionWriter: TInstructionWriter;
40 end;
41
42var
43 Form1: TForm1;
44
45const
46 ItemsPerLine = 16;
47
48
49implementation
50
51{$R *.lfm}
52
53{ TForm1 }
54
55procedure TForm1.FormShow(Sender: TObject);
56var
57 LabelStart: Integer;
58 LabelPrint: Integer;
59 LabelPrintBack: Integer;
60 R0: Integer;
61 R1: Integer;
62 R2: Integer;
63 R3: Integer;
64 R4: Integer;
65begin
66 R0 := 0;
67 R1 := 1;
68 R2 := 2;
69 R3 := 3;
70 R4 := 4;
71 with InstructionWriter do begin
72 {NoOperation;
73 LoadConst(1, $ffff);
74 Load(0, 1);
75 LoadConst(2, $10);
76 StoreMemory(2, 0);
77 }
78 LabelPrint := $100;
79
80 LoadConst(R2, 0);
81 LoadConst(R0, 0);
82 LoadConst(R4, Ord('a'));
83 LabelStart := IP;
84 Input(R1, 0);
85 Output(0, R1);
86 Subtract(R1, R4);
87 TestZero(R1);
88 JumpCond(LabelPrint);
89 LabelPrintBack := IP;
90 Increment(R2);
91 Jump(LabelStart);
92 Halt;
93 IP := LabelPrint;
94 LoadConst(R3, Ord('!'));
95 Output(0, R3);
96 Jump(LabelPrintBack)
97 end;
98end;
99
100procedure TForm1.FormDestroy(Sender: TObject);
101begin
102 FreeAndNil(InstructionWriter);
103 FreeAndNil(Machine);
104end;
105
106procedure TForm1.FormKeyPress(Sender: TObject; var Key: char);
107begin
108end;
109
110procedure TForm1.FormCreate(Sender: TObject);
111begin
112 Machine := TMachine.Create(nil);
113 Machine.OnSerialOutput := SerialOutputExecute;
114 InstructionWriter := TInstructionWriter.Create;
115 InstructionWriter.Cpu := Machine.Cpu;
116end;
117
118procedure TForm1.ButtonStartClick(Sender: TObject);
119begin
120 Machine.Cpu.Start;
121end;
122
123procedure TForm1.ButtonStopClick(Sender: TObject);
124begin
125 Machine.Cpu.Stop;
126end;
127
128procedure TForm1.ListViewMemoryData(Sender: TObject; Item: TListItem);
129var
130 Line: string;
131 I: Integer;
132begin
133 if Item.Index < Length(Machine.Cpu.Memory) div ItemsPerLine then begin
134 Line := '';
135 for I := 0 to ItemsPerLine - 1 do
136 Line := Line + IntToHex(Machine.Cpu.Memory[Item.Index * ItemsPerLine + I], 2) + ' ';
137 Item.Caption := IntToHex(Item.Index * ItemsPerLine, 8);
138 Item.SubItems.Add(Line);
139 end;
140end;
141
142procedure TForm1.ListViewRegistersData(Sender: TObject; Item: TListItem);
143begin
144 if Item.Index < Length(Machine.Cpu.Registers) + 1 then begin
145 if Item.Index = 0 then begin
146 Item.Caption := 'IP';
147 Item.SubItems.Add(IntToHex(Machine.Cpu.IP, 8));
148 end else begin
149 Item.Caption := 'R' + IntToStr(Item.Index - 1);
150 Item.SubItems.Add(IntToHex(Machine.Cpu.Registers[Item.Index - 1], 8));
151 end;
152 end;
153end;
154
155procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: char);
156begin
157 Machine.SerialInput(Key);
158end;
159
160procedure TForm1.Timer1Timer(Sender: TObject);
161begin
162 ReloadMemoryDump;
163 ReloadRegisterDump;
164 LabelTicks.Caption := 'Ticks: ' + IntToStr(Machine.Cpu.Ticks);
165end;
166
167procedure TForm1.ReloadMemoryDump;
168begin
169 ListViewMemory.Items.Count := Length(Machine.Cpu.Memory) div ItemsPerLine;
170 ListViewMemory.Refresh;
171end;
172
173procedure TForm1.ReloadRegisterDump;
174begin
175 ListViewRegisters.Items.Count := Length(Machine.Cpu.Registers);
176 ListViewRegisters.Refresh;
177end;
178
179procedure TForm1.SerialOutputExecute(Sender: TObject);
180var
181 Buffer: string;
182begin
183 Machine.SerialBufferLock.Acquire;
184 try
185 if Length(Machine.SerialBufferOutput) > 0 then begin
186 SetLength(Buffer, Length(Machine.SerialBufferOutput));
187 Move(Machine.SerialBufferOutput[0], Buffer[1], Length(Machine.SerialBufferOutput));
188 Memo1.Lines.Text := Memo1.Lines.Text + Buffer;
189 SetLength(Machine.SerialBufferOutput, 0);
190 end;
191 finally
192 Machine.SerialBufferLock.Release;
193 end;
194end;
195
196end.
197
Note: See TracBrowser for help on using the repository browser.