- Timestamp:
- Sep 22, 2019, 7:13:15 PM (5 years ago)
- Location:
- branches/virtcpu varint
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/virtcpu varint
-
Property svn:ignore
set to
lib
virtcpu
virtcpu.lps
-
Property svn:ignore
set to
-
branches/virtcpu varint/UFormMain.lfm
r195 r196 1 object Form 1: TForm11 object FormMain: TFormMain 2 2 Left = 385 3 3 Height = 914 4 4 Top = 208 5 5 Width = 1543 6 Caption = ' Form1'6 Caption = 'VarInt machine' 7 7 ClientHeight = 914 8 8 ClientWidth = 1543 -
branches/virtcpu varint/UFormMain.pas
r195 r196 11 11 type 12 12 13 { TForm 1}14 15 TForm 1= class(TForm)13 { TFormMain } 14 15 TFormMain = class(TForm) 16 16 Button1: TButton; 17 17 Button2: TButton; … … 38 38 procedure CpuOutput(Port, Value: T); 39 39 public 40 Cpu: TCPU;40 Machine: TMachine; 41 41 InstructionWriter: TInstructionWriter; 42 42 end; 43 43 44 44 var 45 Form 1: TForm1;45 FormMain: TFormMain; 46 46 47 47 const … … 53 53 {$R *.lfm} 54 54 55 { TForm 1}56 57 procedure TForm 1.FormShow(Sender: TObject);55 { TFormMain } 56 57 procedure TFormMain.FormShow(Sender: TObject); 58 58 var 59 59 LabelStart: Integer; … … 104 104 end; 105 105 106 procedure TForm 1.FormDestroy(Sender: TObject);107 begin 108 InstructionWriter.Free;109 Cpu.Free;110 end; 111 112 procedure TForm 1.FormKeyPress(Sender: TObject; var Key: char);113 begin 114 end; 115 116 procedure TForm 1.FormCreate(Sender: TObject);117 begin 118 Cpu := TCPU.Create(nil);119 Cpu.OnInput := CpuInput;120 Cpu.OnOutput := CpuOutput;106 procedure TFormMain.FormDestroy(Sender: TObject); 107 begin 108 FreeAndNil(InstructionWriter); 109 FreeAndNil(Machine); 110 end; 111 112 procedure TFormMain.FormKeyPress(Sender: TObject; var Key: char); 113 begin 114 end; 115 116 procedure TFormMain.FormCreate(Sender: TObject); 117 begin 118 Machine := TMachine.Create(nil); 119 Machine.Cpu.OnInput := CpuInput; 120 Machine.Cpu.OnOutput := CpuOutput; 121 121 InstructionWriter := TInstructionWriter.Create; 122 InstructionWriter.Cpu := Cpu;123 end; 124 125 procedure TForm 1.Button1Click(Sender: TObject);126 begin 127 Cpu.Start;128 end; 129 130 procedure TForm 1.Button2Click(Sender: TObject);131 begin 132 Cpu.Stop;133 end; 134 135 procedure TForm 1.ListViewMemoryData(Sender: TObject; Item: TListItem);122 InstructionWriter.Cpu := Machine.Cpu; 123 end; 124 125 procedure TFormMain.Button1Click(Sender: TObject); 126 begin 127 Machine.Cpu.Start; 128 end; 129 130 procedure TFormMain.Button2Click(Sender: TObject); 131 begin 132 Machine.Cpu.Stop; 133 end; 134 135 procedure TFormMain.ListViewMemoryData(Sender: TObject; Item: TListItem); 136 136 var 137 137 Line: string; 138 138 I: Integer; 139 139 begin 140 if Item.Index < Length(Cpu.Memory)div ItemsPerLine then begin140 if Item.Index < Machine.MemorySize div ItemsPerLine then begin 141 141 Line := ''; 142 142 for I := 0 to ItemsPerLine - 1 do 143 Line := Line + IntToHex( Cpu.Memory[Item.Index * ItemsPerLine + I], 2) + ' ';143 Line := Line + IntToHex(PByte(NativeUInt(Machine.Memory) + Item.Index * ItemsPerLine + I)^, 2) + ' '; 144 144 Item.Caption := IntToHex(Item.Index * ItemsPerLine, 8); 145 145 Item.SubItems.Add(Line); … … 147 147 end; 148 148 149 procedure TForm 1.ListViewRegistersData(Sender: TObject; Item: TListItem);150 begin 151 if Item.Index < Length( Cpu.Registers) + 1 then begin149 procedure TFormMain.ListViewRegistersData(Sender: TObject; Item: TListItem); 150 begin 151 if Item.Index < Length(Machine.Cpu.Registers) + 1 then begin 152 152 if Item.Index = 0 then begin 153 153 Item.Caption := 'IP'; 154 Item.SubItems.Add(IntToHex(Int64( Cpu.IP), 8));154 Item.SubItems.Add(IntToHex(Int64(Machine.Cpu.IP), 8)); 155 155 end else 156 156 if Item.Index = 1 then begin 157 157 Item.Caption := 'SP'; 158 Item.SubItems.Add(IntToHex(Int64( Cpu.SP), 8));158 Item.SubItems.Add(IntToHex(Int64(Machine.Cpu.SP), 8)); 159 159 end else begin 160 160 Item.Caption := 'R' + IntToStr(Item.Index - 2); 161 Item.SubItems.Add(IntToHex(Int64( Cpu.Registers[Item.Index - 2]), 8));161 Item.SubItems.Add(IntToHex(Int64(Machine.Cpu.Registers[Item.Index - 2]), 8)); 162 162 end; 163 163 end; 164 164 end; 165 165 166 procedure TForm 1.Memo1KeyPress(Sender: TObject; var Key: char);166 procedure TFormMain.Memo1KeyPress(Sender: TObject; var Key: char); 167 167 begin 168 168 SetLength(KeyInputBuffer, Length(KeyInputBuffer) + 1); … … 170 170 end; 171 171 172 procedure TForm 1.Timer1Timer(Sender: TObject);172 procedure TFormMain.Timer1Timer(Sender: TObject); 173 173 begin 174 174 ReloadMemoryDump; 175 175 ReloadRegisterDump; 176 LabelTicks.Caption := 'Ticks: ' + IntToStr( Cpu.Ticks);177 end; 178 179 procedure TForm 1.ReloadMemoryDump;180 begin 181 ListViewMemory.Items.Count := Length(Cpu.Memory)div ItemsPerLine;176 LabelTicks.Caption := 'Ticks: ' + IntToStr(Machine.Cpu.Ticks); 177 end; 178 179 procedure TFormMain.ReloadMemoryDump; 180 begin 181 ListViewMemory.Items.Count := Machine.MemorySize div ItemsPerLine; 182 182 ListViewMemory.Refresh; 183 183 end; 184 184 185 procedure TForm 1.ReloadRegisterDump;186 begin 187 ListViewRegisters.Items.Count := Length( Cpu.Registers);185 procedure TFormMain.ReloadRegisterDump; 186 begin 187 ListViewRegisters.Items.Count := Length(Machine.Cpu.Registers); 188 188 ListViewRegisters.Refresh; 189 189 end; 190 190 191 function TForm 1.CpuInput(Port: T): T;191 function TFormMain.CpuInput(Port: T): T; 192 192 begin 193 193 Result := 0; 194 194 case Integer(Port) of 195 195 0: begin 196 while (Length(KeyInputBuffer) = 0) and not Cpu.Terminated do begin196 while (Length(KeyInputBuffer) = 0) and not Machine.Cpu.Terminated do begin 197 197 Sleep(100); 198 198 Application.ProcessMessages; … … 208 208 end; 209 209 210 procedure TForm 1.CpuOutput(Port, Value: T);210 procedure TFormMain.CpuOutput(Port, Value: T); 211 211 begin 212 212 case Integer(Port) of -
branches/virtcpu varint/UInstructionWriter.pas
r195 r196 166 166 C: Integer; 167 167 begin 168 C := Value.WriteToAddr( @Cpu.Memory[IP]);168 C := Value.WriteToAddr(Pointer(NativeInt(Cpu.Memory) + IP)); 169 169 Inc(IP, C); 170 170 end; -
branches/virtcpu varint/UMachine.pas
r195 r196 1 1 unit UMachine; 2 3 2 4 3 {$mode delphi}{$H+} … … 85 84 procedure OpcodeLddr; 86 85 public 86 Memory: Pointer; 87 87 Registers: array of T; 88 88 IP: T; 89 SP: T; 89 90 Condition: Boolean; 90 SP: T;91 Memory: array of Byte;92 91 Terminated: Boolean; 93 92 Ticks: Integer; 94 93 procedure Start; 95 94 procedure Stop; 95 procedure Step; inline; 96 96 constructor Create(AOwner: TComponent); override; 97 97 published … … 100 100 end; 101 101 102 { TMachine } 103 104 TMachine = class(TComponent) 105 private 106 FMemorySize: Integer; 107 procedure SetMemorySize(AValue: Integer); 108 public 109 Cpu: TCpu; 110 Memory: Pointer; 111 property MemorySize: Integer read FMemorySize write SetMemorySize; 112 constructor Create(AOwner: TComponent); override; 113 destructor Destroy; override; 114 end; 115 102 116 103 117 implementation 104 118 119 { TMachine } 120 121 procedure TMachine.SetMemorySize(AValue: Integer); 122 begin 123 if FMemorySize = AValue then Exit; 124 FMemorySize := AValue; 125 Memory := ReAllocMem(Memory, FMemorySize); 126 Cpu.Memory := Memory; 127 end; 128 129 constructor TMachine.Create(AOwner: TComponent); 130 begin 131 inherited; 132 Cpu := TCpu.Create(nil); 133 MemorySize := 1000; 134 end; 135 136 destructor TMachine.Destroy; 137 begin 138 MemorySize := 0; 139 FreeAndNil(Cpu); 140 inherited Destroy; 141 end; 142 105 143 { TCPU } 106 144 107 145 function TCPU.ReadNext: T; 108 146 begin 109 IP := IP + Result.ReadFromAddr( @Memory[IP]);147 IP := IP + Result.ReadFromAddr(Pointer(NativeUInt(Memory) + IP)); 110 148 end; 111 149 … … 147 185 P1 := ReadNext; 148 186 P2 := ReadNext; 149 Registers[P1] := Memory[Registers[P2]];187 Registers[P1].ReadFromAddr(Pointer(NativeUInt(Memory) + Integer(Registers[P2]))); 150 188 end; 151 189 … … 157 195 P1 := ReadNext; 158 196 P2 := ReadNext; 159 Memory[Registers[P1]] := Registers[P2];197 Registers[P2].WriteToAddr(Pointer(NativeUInt(Memory) + Registers[P1])); 160 198 end; 161 199 … … 307 345 P1 := ReadNext; 308 346 SP := SP - Registers[P1].GetByteSize; 309 Registers[P1].WriteToAddr( @Memory[SP]);347 Registers[P1].WriteToAddr(Pointer(NativeUInt(Memory) + Integer(SP))); 310 348 end; 311 349 312 350 procedure TCPU.OpcodePop; 313 351 begin 314 SP := SP + Registers[ReadNext].ReadFromAddr( @Memory[SP]);352 SP := SP + Registers[ReadNext].ReadFromAddr(Pointer(NativeUInt(Memory) + Integer(SP))); 315 353 end; 316 354 … … 321 359 Addr := ReadNext; 322 360 SP := SP - IP.GetByteSize; 323 IP.WriteToAddr( @Memory[SP]);361 IP.WriteToAddr(Pointer(NativeUInt(Memory) + SP)); 324 362 IP := Addr; 325 363 end; … … 331 369 Addr := ReadNext; 332 370 SP := SP - IP.GetByteSize; 333 IP.WriteToAddr( @Memory[SP]);371 IP.WriteToAddr(Pointer(NativeUInt(Memory) + SP)); 334 372 IP := IP + Addr; 335 373 end; 336 374 337 375 procedure TCPU.OpcodeReturn; 338 var 339 S: Integer; 340 begin 341 SP := SP + IP.ReadFromAddr(@Memory[SP]); 376 begin 377 SP := SP + IP.ReadFromAddr(Pointer(NativeUInt(Memory) + SP)); 342 378 end; 343 379 … … 424 460 Src: T; 425 461 Dst: T; 426 Size: T; 462 Count: T; 463 Bytes: T; 427 464 begin 428 465 Src := ReadNext; 429 466 Dst := ReadNext; 430 Size := ReadNext; 431 while Registers[Size] > 0 do begin 432 Memory[Registers[Dst]] := Memory[Registers[Src]]; 433 Inc(Registers[Src]); 434 Inc(Registers[Dst]); 435 Dec(Registers[Size]); 467 Count := ReadNext; 468 Bytes := ReadNext; 469 while Registers[Count] > 0 do begin 470 Move(Pointer(NativeUInt(Memory) + Registers[Src])^, 471 Pointer(NativeUInt(Memory) + Registers[Dst])^, Bytes); 472 Inc(Registers[Src], Bytes); 473 Inc(Registers[Dst], Bytes); 474 Dec(Registers[Count]); 436 475 end; 437 476 end; … … 441 480 Src: T; 442 481 Dst: T; 443 Size: T; 482 Count: T; 483 Bytes: T; 444 484 begin 445 485 Src := ReadNext; 446 486 Dst := ReadNext; 447 Size := ReadNext; 448 while Registers[Size] > 0 do begin 449 Memory[Registers[Dst]] := Memory[Registers[Src]]; 450 Dec(Registers[Src]); 451 Dec(Registers[Dst]); 452 Dec(Registers[Size]); 487 Count := ReadNext; 488 Bytes := ReadNext; 489 while Registers[Count] > 0 do begin 490 Move(Pointer(NativeUInt(Memory) + Registers[Src])^, 491 Pointer(NativeUInt(Memory) + Registers[Dst])^, Bytes); 492 Dec(Registers[Src], Bytes); 493 Dec(Registers[Dst], Bytes); 494 Dec(Registers[Count]); 453 495 end; 454 496 end; 455 497 456 498 procedure TCPU.Start; 457 var458 Opcode: T;459 499 begin 460 500 Terminated := False; 461 501 Ticks := 0; 462 502 IP := 0; 463 SP := Length(Memory); 464 while not Terminated do begin 465 Opcode := ReadNext; 466 if (Opcode >= 0) and (Opcode <= T(Integer(High(TOpcode)))) then 467 OpcodeHandlers[TOpcode(Byte(Opcode))] 468 else raise Exception.Create(Format('Unsupported instruction %d', [Int64(Opcode)])); 469 Inc(Ticks); 470 end; 503 SP := MemSize(Memory); 504 while not Terminated do 505 Step; 471 506 end; 472 507 … … 474 509 begin 475 510 Terminated := True; 511 end; 512 513 procedure TCPU.Step; 514 var 515 Opcode: T; 516 begin 517 Opcode := ReadNext; 518 if (Opcode >= 0) and (Opcode <= T(Integer(High(TOpcode)))) then 519 OpcodeHandlers[TOpcode(Byte(Opcode))] 520 else raise Exception.Create(Format('Unsupported instruction %d on address %x', [Int64(Opcode), Int64(IP)])); 521 Inc(Ticks); 476 522 end; 477 523 … … 480 526 inherited; 481 527 SetLength(Registers, 16); 482 SetLength(Memory, 1024);483 528 OpcodeHandlers[opNop] := OpcodeNop; 484 529 OpcodeHandlers[opHalt] := OpcodeHalt; -
branches/virtcpu varint/UVarInt.pas
r195 r196 32 32 class operator Explicit(A: TVarInt): Int64; 33 33 class operator Inc(A: TVarInt): TVarInt; 34 class operator Inc(A: TVarInt; B: Integer): TVarInt;34 class operator Inc(A: TVarInt; B: TVarInt): TVarInt; 35 35 class operator Dec(A: TVarInt): TVarInt; 36 class operator Dec(A: TVarInt; B: TVarInt): TVarInt; 36 37 class operator Negative(A: TVarInt): TVarInt; 37 38 class operator Add(A: TVarInt; B: TVarInt): TVarInt; … … 75 76 class operator Explicit(A: TVarUInt): Int64; 76 77 class operator Inc(A: TVarUInt): TVarUInt; 77 class operator Inc(A: TVarUInt; B: Integer): TVarUInt;78 class operator Inc(A: TVarUInt; B: TVarInt): TVarUInt; 78 79 class operator Dec(A: TVarUInt): TVarUInt; 80 class operator Dec(A: TVarUInt; B: TVarUInt): TVarUInt; 79 81 class operator Negative(A: TVarUInt): TVarUInt; 80 82 class operator Add(A: TVarUInt; B: TVarUInt): TVarUInt; … … 191 193 end; 192 194 193 class operator TVarUInt.Inc(A: TVarUInt; B: Integer): TVarUInt;194 begin 195 Result.Value := A.Value + B ;195 class operator TVarUInt.Inc(A: TVarUInt; B: TVarInt): TVarUInt; 196 begin 197 Result.Value := A.Value + B.Value; 196 198 end; 197 199 … … 199 201 begin 200 202 Result.Value := A.Value - 1; 203 end; 204 205 class operator TVarUInt.Dec(A: TVarUInt; B: TVarUInt): TVarUInt; 206 begin 207 Result.Value := A.Value - B.Value; 201 208 end; 202 209 … … 424 431 end; 425 432 426 class operator TVarInt.Inc(A: TVarInt; B: Integer): TVarInt;427 begin 428 Result := A.Value + B ;433 class operator TVarInt.Inc(A: TVarInt; B: TVarInt): TVarInt; 434 begin 435 Result := A.Value + B.Value; 429 436 end; 430 437 … … 432 439 begin 433 440 Result.Value := A.Value - 1; 441 end; 442 443 class operator TVarInt.Dec(A: TVarInt; B: TVarInt): TVarInt; 444 begin 445 Result.Value := A.Value - B.Value; 434 446 end; 435 447 -
branches/virtcpu varint/virtcpu.lpi
r195 r196 72 72 <Filename Value="UFormMain.pas"/> 73 73 <IsPartOfProject Value="True"/> 74 <ComponentName Value="Form 1"/>74 <ComponentName Value="FormMain"/> 75 75 <HasResources Value="True"/> 76 76 <ResourceBaseClass Value="Form"/> -
branches/virtcpu varint/virtcpu.lpr
r195 r196 16 16 RequireDerivedFormResource:=True; 17 17 Application.Initialize; 18 Application.CreateForm(TForm 1, Form1);18 Application.CreateForm(TFormMain, FormMain); 19 19 Application.Run; 20 20 end.
Note:
See TracChangeset
for help on using the changeset viewer.