Changeset 215 for branches/virtcpu fixed int/UFormMain.pas
- Timestamp:
- Aug 19, 2020, 11:54:20 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/virtcpu fixed int/UFormMain.pas
r168 r215 7 7 uses 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls, 9 StdCtrls, ExtCtrls, U Machine, UInstructionWriter;9 StdCtrls, ExtCtrls, UCpu, UInstructionWriter, UMachine; 10 10 11 11 type … … 14 14 15 15 TForm1 = class(TForm) 16 Button 1: TButton;17 Button 2: TButton;16 ButtonStart: TButton; 17 ButtonStop: TButton; 18 18 LabelTicks: TLabel; 19 19 ListViewMemory: TListView; … … 21 21 Memo1: TMemo; 22 22 Timer1: TTimer; 23 procedure Button 1Click(Sender: TObject);24 procedure Button 2Click(Sender: TObject);23 procedure ButtonStartClick(Sender: TObject); 24 procedure ButtonStopClick(Sender: TObject); 25 25 procedure FormCreate(Sender: TObject); 26 26 procedure FormDestroy(Sender: TObject); … … 32 32 procedure Timer1Timer(Sender: TObject); 33 33 private 34 KeyInputBuffer: array of Char;35 34 procedure ReloadMemoryDump; 36 35 procedure ReloadRegisterDump; 37 function CpuInput(Port: T): T; 38 procedure CpuOutput(Port, Value: T); 36 procedure SerialOutputExecute(Sender: TObject); 39 37 public 40 Cpu: TCPU;38 Machine: TMachine; 41 39 InstructionWriter: TInstructionWriter; 42 40 end; … … 87 85 Output(0, R1); 88 86 Subtract(R1, R4); 89 JumpZero(R1, LabelPrint); 87 TestZero(R1); 88 JumpCond(LabelPrint); 90 89 LabelPrintBack := IP; 91 90 Increment(R2); … … 101 100 procedure TForm1.FormDestroy(Sender: TObject); 102 101 begin 103 InstructionWriter.Free;104 Cpu.Free;102 FreeAndNil(InstructionWriter); 103 FreeAndNil(Machine); 105 104 end; 106 105 … … 111 110 procedure TForm1.FormCreate(Sender: TObject); 112 111 begin 113 Cpu := TCPU.Create(nil); 114 Cpu.OnInput := CpuInput; 115 Cpu.OnOutput := CpuOutput; 112 Machine := TMachine.Create(nil); 113 Machine.OnSerialOutput := SerialOutputExecute; 116 114 InstructionWriter := TInstructionWriter.Create; 117 InstructionWriter.Cpu := Cpu;115 InstructionWriter.Cpu := Machine.Cpu; 118 116 end; 119 117 120 procedure TForm1.Button 1Click(Sender: TObject);118 procedure TForm1.ButtonStartClick(Sender: TObject); 121 119 begin 122 Cpu.Start;120 Machine.Cpu.Start; 123 121 end; 124 122 125 procedure TForm1.Button 2Click(Sender: TObject);123 procedure TForm1.ButtonStopClick(Sender: TObject); 126 124 begin 127 Cpu.Stop;125 Machine.Cpu.Stop; 128 126 end; 129 127 … … 133 131 I: Integer; 134 132 begin 135 if Item.Index < Length( Cpu.Memory) div ItemsPerLine then begin133 if Item.Index < Length(Machine.Cpu.Memory) div ItemsPerLine then begin 136 134 Line := ''; 137 135 for I := 0 to ItemsPerLine - 1 do 138 Line := Line + IntToHex( Cpu.Memory[Item.Index * ItemsPerLine + I], 2) + ' ';136 Line := Line + IntToHex(Machine.Cpu.Memory[Item.Index * ItemsPerLine + I], 2) + ' '; 139 137 Item.Caption := IntToHex(Item.Index * ItemsPerLine, 8); 140 138 Item.SubItems.Add(Line); … … 144 142 procedure TForm1.ListViewRegistersData(Sender: TObject; Item: TListItem); 145 143 begin 146 if Item.Index < Length( Cpu.Registers) + 1 then begin144 if Item.Index < Length(Machine.Cpu.Registers) + 1 then begin 147 145 if Item.Index = 0 then begin 148 146 Item.Caption := 'IP'; 149 Item.SubItems.Add(IntToHex( Cpu.IP, 8));147 Item.SubItems.Add(IntToHex(Machine.Cpu.IP, 8)); 150 148 end else begin 151 149 Item.Caption := 'R' + IntToStr(Item.Index - 1); 152 Item.SubItems.Add(IntToHex( Cpu.Registers[Item.Index - 1], 8));150 Item.SubItems.Add(IntToHex(Machine.Cpu.Registers[Item.Index - 1], 8)); 153 151 end; 154 152 end; … … 157 155 procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: char); 158 156 begin 159 SetLength(KeyInputBuffer, Length(KeyInputBuffer) + 1); 160 KeyInputBuffer[High(KeyInputBuffer)] := Key; 157 Machine.SerialInput(Key); 161 158 end; 162 159 … … 165 162 ReloadMemoryDump; 166 163 ReloadRegisterDump; 167 LabelTicks.Caption := 'Ticks: ' + IntToStr( Cpu.Ticks);164 LabelTicks.Caption := 'Ticks: ' + IntToStr(Machine.Cpu.Ticks); 168 165 end; 169 166 170 167 procedure TForm1.ReloadMemoryDump; 171 168 begin 172 ListViewMemory.Items.Count := Length( Cpu.Memory) div ItemsPerLine;169 ListViewMemory.Items.Count := Length(Machine.Cpu.Memory) div ItemsPerLine; 173 170 ListViewMemory.Refresh; 174 171 end; … … 176 173 procedure TForm1.ReloadRegisterDump; 177 174 begin 178 ListViewRegisters.Items.Count := Length( Cpu.Registers);175 ListViewRegisters.Items.Count := Length(Machine.Cpu.Registers); 179 176 ListViewRegisters.Refresh; 180 177 end; 181 178 182 function TForm1.CpuInput(Port: T): T; 179 procedure TForm1.SerialOutputExecute(Sender: TObject); 180 var 181 Buffer: string; 183 182 begin 184 Result := 0; 185 case Port of 186 0: begin 187 while (Length(KeyInputBuffer) = 0) and not Cpu.Terminated do begin 188 Sleep(100); 189 Application.ProcessMessages; 190 end; 191 if Length(KeyInputBuffer) > 0 then begin 192 Result := Ord(KeyInputBuffer[0]); 193 if Length(KeyInputBuffer) > 1 then 194 Move(KeyInputBuffer[1], KeyInputBuffer[0], Length(KeyInputBuffer) - 1); 195 SetLength(KeyInputBuffer, Length(KeyInputBuffer) - 1); 196 end else Result := 0; 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); 197 190 end; 198 end; 199 end; 200 201 procedure TForm1.CpuOutput(Port, Value: T); 202 begin 203 case Port of 204 0: Memo1.Lines.Text := Memo1.Lines.Text + Char(Value); 191 finally 192 Machine.SerialBufferLock.Release; 205 193 end; 206 194 end;
Note:
See TracChangeset
for help on using the changeset viewer.