Changeset 50 for branches/ByteArray/Cpu.pas
- Timestamp:
- Nov 2, 2023, 11:18:06 PM (7 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ByteArray/Cpu.pas
r47 r50 41 41 42 42 TInstructionEvent = procedure of object; 43 TCpuThread = class; 43 44 44 45 { TCpu } … … 46 47 TCpu = class 47 48 private 48 Instructions: array[TInstruction] of TInstructionEvent; 49 FHalted: Boolean; 50 FRunning: Boolean; 51 FThread: TCpuThread; 52 FInstructions: array[TInstruction] of TInstructionEvent; 53 FTicks: QWord; 49 54 procedure Push(Value: TBigInt; Size: TBigIntSize); 50 55 function Pop(Size: TBigIntSize): TBigInt; … … 84 89 procedure InstructionDecSize; 85 90 procedure InitInstructions; 91 procedure SetRunning(AValue: Boolean); 92 procedure Run; 93 procedure Step; 86 94 public 87 95 Regs: array[TRegIndex] of TBigInt; 88 96 PC: TBigInt; 89 97 SP: TBigInt; 90 Terminated: Boolean;91 98 DataWidth: TBigIntSize; 92 99 AddressWidth: TBigIntSize; … … 99 106 procedure WriteRegister(Reg: TRegIndex); 100 107 procedure Reset; 101 procedure Run;102 procedure St ep;108 procedure Start; 109 procedure Stop; 103 110 constructor Create; 104 111 destructor Destroy; override; 112 property Ticks: QWord read FTicks; 113 property Running: Boolean read FRunning write SetRunning; 114 property Halted: Boolean read FHalted; 105 115 end; 106 116 117 { TCpuThread } 118 119 TCpuThread = class(TThread) 120 Cpu: TCpu; 121 procedure Execute; override; 122 end; 123 124 107 125 implementation 126 127 { TCpuThread } 128 129 procedure TCpuThread.Execute; 130 begin 131 repeat 132 if not Cpu.Halted then Cpu.Step 133 else Sleep(1); 134 //Cpu.CheckInterreupts; 135 until Terminated; 136 end; 108 137 109 138 { TCpu } … … 128 157 procedure TCpu.InstructionHalt; 129 158 begin 130 Terminated := True;159 FHalted := True; 131 160 end; 132 161 … … 443 472 procedure TCpu.InitInstructions; 444 473 begin 445 Instructions[inNop] := InstructionNop; 446 Instructions[inHalt] := InstructionHalt; 447 Instructions[inLoadConst] := InstructionLoadConst; 448 Instructions[inLoadConstSize] := InstructionLoadConstSize; 449 Instructions[inLoad] := InstructionLoad; 450 Instructions[inLoadSize] := InstructionLoadSize; 451 Instructions[inLoadMem] := InstructionLoadMem; 452 Instructions[inLoadMemSize] := InstructionLoadMemSize; 453 Instructions[inStoreMem] := InstructionStoreMem; 454 Instructions[inStoreMemSize] := InstructionStoreMemSize; 455 Instructions[inJump] := InstructionJump; 456 Instructions[inJumpSize] := InstructionJumpSize; 457 Instructions[inJumpNotZero] := InstructionJumpNotZero; 458 Instructions[inJumpNotZeroSize] := InstructionJumpNotZeroSize; 459 Instructions[inJumpZero] := InstructionJumpZero; 460 Instructions[inJumpZeroSize] := InstructionJumpZeroSize; 461 Instructions[inJumpRel] := InstructionJumpRel; 462 Instructions[inJumpRelSize] := InstructionJumpRelSize; 463 Instructions[inCall] := InstructionCall; 464 Instructions[inCallSize] := InstructionCallSize; 465 Instructions[inRet] := InstructionRet; 466 Instructions[inRetSize] := InstructionRetSize; 467 Instructions[inPush] := InstructionPush; 468 Instructions[inPushSize] := InstructionPushSize; 469 Instructions[inPop] := InstructionPop; 470 Instructions[inPopSize] := InstructionPopSize; 471 Instructions[inInput] := InstructionInput; 472 Instructions[inInputSize] := InstructionInputSize; 473 Instructions[inOutput] := InstructionOutput; 474 Instructions[inOutputSize] := InstructionOutputSize; 475 Instructions[inInc] := InstructionInc; 476 Instructions[inIncSize] := InstructionIncSize; 477 Instructions[inDec] := InstructionDec; 478 Instructions[inDecSize] := InstructionDecSize; 474 FInstructions[inNop] := InstructionNop; 475 FInstructions[inHalt] := InstructionHalt; 476 FInstructions[inLoadConst] := InstructionLoadConst; 477 FInstructions[inLoadConstSize] := InstructionLoadConstSize; 478 FInstructions[inLoad] := InstructionLoad; 479 FInstructions[inLoadSize] := InstructionLoadSize; 480 FInstructions[inLoadMem] := InstructionLoadMem; 481 FInstructions[inLoadMemSize] := InstructionLoadMemSize; 482 FInstructions[inStoreMem] := InstructionStoreMem; 483 FInstructions[inStoreMemSize] := InstructionStoreMemSize; 484 FInstructions[inJump] := InstructionJump; 485 FInstructions[inJumpSize] := InstructionJumpSize; 486 FInstructions[inJumpNotZero] := InstructionJumpNotZero; 487 FInstructions[inJumpNotZeroSize] := InstructionJumpNotZeroSize; 488 FInstructions[inJumpZero] := InstructionJumpZero; 489 FInstructions[inJumpZeroSize] := InstructionJumpZeroSize; 490 FInstructions[inJumpRel] := InstructionJumpRel; 491 FInstructions[inJumpRelSize] := InstructionJumpRelSize; 492 FInstructions[inCall] := InstructionCall; 493 FInstructions[inCallSize] := InstructionCallSize; 494 FInstructions[inRet] := InstructionRet; 495 FInstructions[inRetSize] := InstructionRetSize; 496 FInstructions[inPush] := InstructionPush; 497 FInstructions[inPushSize] := InstructionPushSize; 498 FInstructions[inPop] := InstructionPop; 499 FInstructions[inPopSize] := InstructionPopSize; 500 FInstructions[inInput] := InstructionInput; 501 FInstructions[inInputSize] := InstructionInputSize; 502 FInstructions[inOutput] := InstructionOutput; 503 FInstructions[inOutputSize] := InstructionOutputSize; 504 FInstructions[inInc] := InstructionInc; 505 FInstructions[inIncSize] := InstructionIncSize; 506 FInstructions[inDec] := InstructionDec; 507 FInstructions[inDecSize] := InstructionDecSize; 508 end; 509 510 procedure TCpu.SetRunning(AValue: Boolean); 511 begin 512 if FRunning = AValue then Exit; 513 if AValue then Start 514 else Stop; 479 515 end; 480 516 … … 516 552 PC := 0; 517 553 SP := 0; 518 Terminated := False; 554 FHalted := False; 555 FTicks := 0; 519 556 end; 520 557 … … 522 559 begin 523 560 Reset; 524 while not Terminated do561 while not FHalted do 525 562 Step; 526 563 end; … … 531 568 begin 532 569 Instruction := TInstruction(Byte(Read(1))); 533 Instructions[Instruction]; 570 FInstructions[Instruction]; 571 Inc(FTicks); 572 end; 573 574 procedure TCpu.Start; 575 begin 576 if not Running then begin 577 Reset; 578 FThread := TCpuThread.Create(True); 579 FThread.Cpu := Self; 580 FThread.Start; 581 FRunning := True; 582 end; 583 end; 584 585 procedure TCpu.Stop; 586 begin 587 if Running then begin 588 FHalted := True; 589 FThread.Terminate; 590 FThread.WaitFor; 591 FreeAndNil(FThread); 592 FRunning := False; 593 end; 534 594 end; 535 595 … … 544 604 destructor TCpu.Destroy; 545 605 begin 606 Stop; 546 607 FreeAndNil(Memory); 547 608 FreeAndNil(IO);
Note:
See TracChangeset
for help on using the changeset viewer.