Changeset 197 for branches/virtcpu varint/Forms/UFormMain.pas
- Timestamp:
- Sep 22, 2019, 9:31:49 PM (5 years ago)
- Location:
- branches/virtcpu varint/Forms
- Files:
-
- 1 added
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/virtcpu varint/Forms/UFormMain.pas
r196 r197 7 7 uses 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls, 9 StdCtrls, ExtCtrls, UMachine, UInstructionWriter ;9 StdCtrls, ExtCtrls, UMachine, UInstructionWriter, UCpu; 10 10 11 11 type … … 14 14 15 15 TFormMain = class(TForm) 16 Button1: TButton; 17 Button2: TButton; 18 LabelTicks: TLabel; 19 ListViewMemory: TListView; 20 ListViewRegisters: TListView; 21 Memo1: TMemo; 16 ButtonStart: TButton; 17 ButtonStop: TButton; 18 ButtonAssembler: TButton; 19 ButtonDisassembler: TButton; 20 ButtonCpuState: TButton; 21 ButtonScreen: TButton; 22 ButtonMemory: TButton; 23 ButtonConsole: TButton; 22 24 Timer1: TTimer; 23 procedure Button1Click(Sender: TObject); 24 procedure Button2Click(Sender: TObject); 25 procedure ButtonStartClick(Sender: TObject); 26 procedure ButtonStopClick(Sender: TObject); 27 procedure ButtonAssemblerClick(Sender: TObject); 28 procedure ButtonConsoleClick(Sender: TObject); 29 procedure ButtonCpuStateClick(Sender: TObject); 30 procedure ButtonDisassemblerClick(Sender: TObject); 31 procedure ButtonMemoryClick(Sender: TObject); 32 procedure ButtonScreenClick(Sender: TObject); 25 33 procedure FormCreate(Sender: TObject); 26 34 procedure FormDestroy(Sender: TObject); 27 procedure FormKeyPress(Sender: TObject; var Key: char);28 35 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 36 private 34 KeyInputBuffer: array of Char;35 procedure ReloadMemoryDump;36 procedure ReloadRegisterDump;37 function CpuInput(Port: T): T;38 procedure CpuOutput(Port, Value: T);39 37 public 40 38 Machine: TMachine; … … 45 43 FormMain: TFormMain; 46 44 47 const48 ItemsPerLine = 16;49 50 45 51 46 implementation 52 47 53 48 {$R *.lfm} 49 50 uses 51 UFormConsole, UFormMemory, UFormScreen, UFormCpuState, UFormAssembler, 52 UFormDisassembler; 54 53 55 54 { TFormMain } … … 110 109 end; 111 110 112 procedure TFormMain.FormKeyPress(Sender: TObject; var Key: char);113 begin114 end;115 116 111 procedure TFormMain.FormCreate(Sender: TObject); 117 112 begin 118 113 Machine := TMachine.Create(nil); 119 Machine.Cpu.OnInput := CpuInput;120 Machine.Cpu.OnOutput := CpuOutput;121 114 InstructionWriter := TInstructionWriter.Create; 122 InstructionWriter.Cpu := Machine.Cpu;123 115 end; 124 116 125 procedure TFormMain.Button 1Click(Sender: TObject);117 procedure TFormMain.ButtonStartClick(Sender: TObject); 126 118 begin 127 119 Machine.Cpu.Start; 128 120 end; 129 121 130 procedure TFormMain.Button 2Click(Sender: TObject);122 procedure TFormMain.ButtonStopClick(Sender: TObject); 131 123 begin 132 124 Machine.Cpu.Stop; 133 125 end; 134 126 135 procedure TFormMain.ListViewMemoryData(Sender: TObject; Item: TListItem); 136 var 137 Line: string; 138 I: Integer; 127 procedure TFormMain.ButtonAssemblerClick(Sender: TObject); 139 128 begin 140 if Item.Index < Machine.MemorySize div ItemsPerLine then begin 141 Line := ''; 142 for I := 0 to ItemsPerLine - 1 do 143 Line := Line + IntToHex(PByte(NativeUInt(Machine.Memory) + Item.Index * ItemsPerLine + I)^, 2) + ' '; 144 Item.Caption := IntToHex(Item.Index * ItemsPerLine, 8); 145 Item.SubItems.Add(Line); 146 end; 129 if not Assigned(FormAssembler) then 130 FormAssembler := TFormAssembler.Create(Self); 131 FormAssembler.Assembler.InstructionWriter.Memory := Machine.Memory; 132 FormAssembler.Show; 147 133 end; 148 134 149 procedure TFormMain. ListViewRegistersData(Sender: TObject; Item: TListItem);135 procedure TFormMain.ButtonConsoleClick(Sender: TObject); 150 136 begin 151 if Item.Index < Length(Machine.Cpu.Registers) + 1 then begin 152 if Item.Index = 0 then begin 153 Item.Caption := 'IP'; 154 Item.SubItems.Add(IntToHex(Int64(Machine.Cpu.IP), 8)); 155 end else 156 if Item.Index = 1 then begin 157 Item.Caption := 'SP'; 158 Item.SubItems.Add(IntToHex(Int64(Machine.Cpu.SP), 8)); 159 end else begin 160 Item.Caption := 'R' + IntToStr(Item.Index - 2); 161 Item.SubItems.Add(IntToHex(Int64(Machine.Cpu.Registers[Item.Index - 2]), 8)); 162 end; 163 end; 137 if not Assigned(FormConsole) then 138 FormConsole := TFormConsole.Create(nil); 139 FormConsole.Machine := Machine; 140 FormConsole.Show; 164 141 end; 165 142 166 procedure TFormMain. Memo1KeyPress(Sender: TObject; var Key: char);143 procedure TFormMain.ButtonCpuStateClick(Sender: TObject); 167 144 begin 168 SetLength(KeyInputBuffer, Length(KeyInputBuffer) + 1); 169 KeyInputBuffer[High(KeyInputBuffer)] := Key; 145 if not Assigned(FormCpuState) then 146 FormCpuState := TFormCpuState.Create(Self); 147 FormCpuState.Machine := Machine; 148 FormCpuState.Show; 149 FormCpuState.Reload; 170 150 end; 171 151 172 procedure TFormMain. Timer1Timer(Sender: TObject);152 procedure TFormMain.ButtonDisassemblerClick(Sender: TObject); 173 153 begin 174 ReloadMemoryDump; 175 ReloadRegisterDump; 176 LabelTicks.Caption := 'Ticks: ' + IntToStr(Machine.Cpu.Ticks); 154 if not Assigned(FormDisassembler) then 155 FormDisassembler := TFormDisassembler.Create(Self); 156 FormDisassembler.Disassembler.Cpu := Machine.Cpu; 157 FormDisassembler.Show; 158 FormDisassembler.Reload; 177 159 end; 178 160 179 procedure TFormMain. ReloadMemoryDump;161 procedure TFormMain.ButtonMemoryClick(Sender: TObject); 180 162 begin 181 ListViewMemory.Items.Count := Machine.MemorySize div ItemsPerLine; 182 ListViewMemory.Refresh; 163 if not Assigned(FormMemory) then 164 FormMemory := TFormMemory.Create(Self); 165 FormMemory.Machine := Machine; 166 FormMemory.Show; 167 FormMemory.Reload; 183 168 end; 184 169 185 procedure TFormMain. ReloadRegisterDump;170 procedure TFormMain.ButtonScreenClick(Sender: TObject); 186 171 begin 187 ListViewRegisters.Items.Count := Length(Machine.Cpu.Registers); 188 ListViewRegisters.Refresh; 189 end; 190 191 function TFormMain.CpuInput(Port: T): T; 192 begin 193 Result := 0; 194 case Integer(Port) of 195 0: begin 196 while (Length(KeyInputBuffer) = 0) and not Machine.Cpu.Terminated do begin 197 Sleep(100); 198 Application.ProcessMessages; 199 end; 200 if Length(KeyInputBuffer) > 0 then begin 201 Result := Ord(KeyInputBuffer[0]); 202 if Length(KeyInputBuffer) > 1 then 203 Move(KeyInputBuffer[1], KeyInputBuffer[0], Length(KeyInputBuffer) - 1); 204 SetLength(KeyInputBuffer, Length(KeyInputBuffer) - 1); 205 end else Result := 0; 206 end; 207 end; 208 end; 209 210 procedure TFormMain.CpuOutput(Port, Value: T); 211 begin 212 case Integer(Port) of 213 0: Memo1.Lines.Text := Memo1.Lines.Text + Char(Value); 214 end; 172 if not Assigned(FormScreen) then 173 FormScreen := TFormScreen.Create(Self); 174 FormScreen.Machine := Machine; 175 FormScreen.Show; 176 FormScreen.Reload; 215 177 end; 216 178
Note:
See TracChangeset
for help on using the changeset viewer.