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, ExtCtrls, UCpu, UInstructionWriter, UMachine;
|
---|
10 |
|
---|
11 | type
|
---|
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 |
|
---|
42 | var
|
---|
43 | Form1: TForm1;
|
---|
44 |
|
---|
45 | const
|
---|
46 | ItemsPerLine = 16;
|
---|
47 |
|
---|
48 |
|
---|
49 | implementation
|
---|
50 |
|
---|
51 | {$R *.lfm}
|
---|
52 |
|
---|
53 | { TForm1 }
|
---|
54 |
|
---|
55 | procedure TForm1.FormShow(Sender: TObject);
|
---|
56 | var
|
---|
57 | LabelStart: Integer;
|
---|
58 | LabelPrint: Integer;
|
---|
59 | LabelPrintBack: Integer;
|
---|
60 | R0: Integer;
|
---|
61 | R1: Integer;
|
---|
62 | R2: Integer;
|
---|
63 | R3: Integer;
|
---|
64 | R4: Integer;
|
---|
65 | begin
|
---|
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;
|
---|
98 | end;
|
---|
99 |
|
---|
100 | procedure TForm1.FormDestroy(Sender: TObject);
|
---|
101 | begin
|
---|
102 | FreeAndNil(InstructionWriter);
|
---|
103 | FreeAndNil(Machine);
|
---|
104 | end;
|
---|
105 |
|
---|
106 | procedure TForm1.FormKeyPress(Sender: TObject; var Key: char);
|
---|
107 | begin
|
---|
108 | end;
|
---|
109 |
|
---|
110 | procedure TForm1.FormCreate(Sender: TObject);
|
---|
111 | begin
|
---|
112 | Machine := TMachine.Create(nil);
|
---|
113 | Machine.OnSerialOutput := SerialOutputExecute;
|
---|
114 | InstructionWriter := TInstructionWriter.Create;
|
---|
115 | InstructionWriter.Cpu := Machine.Cpu;
|
---|
116 | end;
|
---|
117 |
|
---|
118 | procedure TForm1.ButtonStartClick(Sender: TObject);
|
---|
119 | begin
|
---|
120 | Machine.Cpu.Start;
|
---|
121 | end;
|
---|
122 |
|
---|
123 | procedure TForm1.ButtonStopClick(Sender: TObject);
|
---|
124 | begin
|
---|
125 | Machine.Cpu.Stop;
|
---|
126 | end;
|
---|
127 |
|
---|
128 | procedure TForm1.ListViewMemoryData(Sender: TObject; Item: TListItem);
|
---|
129 | var
|
---|
130 | Line: string;
|
---|
131 | I: Integer;
|
---|
132 | begin
|
---|
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;
|
---|
140 | end;
|
---|
141 |
|
---|
142 | procedure TForm1.ListViewRegistersData(Sender: TObject; Item: TListItem);
|
---|
143 | begin
|
---|
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;
|
---|
153 | end;
|
---|
154 |
|
---|
155 | procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: char);
|
---|
156 | begin
|
---|
157 | Machine.SerialInput(Key);
|
---|
158 | end;
|
---|
159 |
|
---|
160 | procedure TForm1.Timer1Timer(Sender: TObject);
|
---|
161 | begin
|
---|
162 | ReloadMemoryDump;
|
---|
163 | ReloadRegisterDump;
|
---|
164 | LabelTicks.Caption := 'Ticks: ' + IntToStr(Machine.Cpu.Ticks);
|
---|
165 | end;
|
---|
166 |
|
---|
167 | procedure TForm1.ReloadMemoryDump;
|
---|
168 | begin
|
---|
169 | ListViewMemory.Items.Count := Length(Machine.Cpu.Memory) div ItemsPerLine;
|
---|
170 | ListViewMemory.Refresh;
|
---|
171 | end;
|
---|
172 |
|
---|
173 | procedure TForm1.ReloadRegisterDump;
|
---|
174 | begin
|
---|
175 | ListViewRegisters.Items.Count := Length(Machine.Cpu.Registers);
|
---|
176 | ListViewRegisters.Refresh;
|
---|
177 | end;
|
---|
178 |
|
---|
179 | procedure TForm1.SerialOutputExecute(Sender: TObject);
|
---|
180 | var
|
---|
181 | Buffer: string;
|
---|
182 | begin
|
---|
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;
|
---|
194 | end;
|
---|
195 |
|
---|
196 | end.
|
---|
197 |
|
---|