Changeset 14 for branches/test2
- Timestamp:
- Nov 6, 2025, 10:35:31 PM (35 hours ago)
- Location:
- branches/test2
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/test2
-
Property svn:ignore
set to
lib
Project1
Project1.res
Project1.lps
-
Property svn:ignore
set to
-
branches/test2/Cpu.pas
r13 r14 4 4 5 5 uses 6 Classes, SysUtils ;6 Classes, SysUtils, Generics.Collections; 7 7 8 8 type 9 TOperation = (opNoOperation, opTerminate, opCopyMem, opIncrement, opAdd, opSet, 10 opInput, opOutput, opJump, opJumpRelative, opCall, opReturn, opPush, opPop); 9 TOperation = (opNoOperation, opTerminate, opCopyMem, opCopyMemIndex, opSet, 10 opExchange, 11 opIncrement, opDecrement, 12 opAdd, opSubtract, opMultiply, opDivide, 13 opShiftLeft, opShiftRight, 14 opInput, opOutput, 15 opJump, opJumpZero, opJumpNotZero, 16 opJumpRelative, opJumpRelativeZero, opJumpRelativeNotZero, 17 opPush, opPop, 18 opCall, opReturn, 19 opOr, opXor, opAnd, 20 opSetBit, opResetBit); 11 21 12 22 TData = Cardinal; … … 16 26 Source: TData; 17 27 Destination: TData; 28 Count: TData; 18 29 end; 30 31 TInstructions = TList<TInstruction>; 19 32 20 33 TInputEvent = function (Address: TData): TData of object; … … 30 43 function Pop: TData; 31 44 public 32 Instructions: array of TInstruction;45 Instructions: TInstructions; 33 46 IP: Integer; 34 47 SP: Integer; 35 48 Terminated: Boolean; 36 49 Memory: array of TData; 37 function AddInstruction(Operation: TOperation; Destination, Source: TData): TInstruction; 50 function AddInstruction(Operation: TOperation; Destination: TData = 0; 51 Source: TData = 0; Count: TData = 0): TInstruction; 38 52 procedure Reset; 39 53 procedure Step; 40 54 procedure Run; 55 constructor Create; 41 56 destructor Destroy; override; 42 57 property OnInput: TInputEvent read FOnInput write FOnInput; … … 61 76 end; 62 77 63 function TCpu.AddInstruction(Operation: TOperation; Destination ,64 Source: TData ): TInstruction;78 function TCpu.AddInstruction(Operation: TOperation; Destination: TData = 0; 79 Source: TData = 0; Count: TData = 0): TInstruction; 65 80 begin 66 81 Result.Operation := Operation; 67 82 Result.Destination := Destination; 68 83 Result.Source := Source; 69 SetLength(Instructions, Length(Instructions) + 1);70 Instructions [Length(Instructions) - 1] := Result;84 Result.Count := Count; 85 Instructions.Add(Result); 71 86 end; 72 87 … … 81 96 var 82 97 Instruction: TInstruction; 98 Temp: TData; 83 99 begin 84 100 Instruction := Instructions[IP]; 85 IP := (IP + 1) mod Length(Instructions);101 IP := (IP + 1) mod Instructions.Count; 86 102 case Instruction.Operation of 87 103 opNoOperation: ; 88 104 opTerminate: Terminated := True; 89 105 opCopyMem: Memory[Instruction.Destination] := Memory[Instruction.Source]; 106 opCopyMemIndex: Memory[Instruction.Destination] := Memory[Instruction.Source] + Instruction.Count; 107 opExchange: begin 108 Temp := Memory[Instruction.Destination]; 109 Memory[Instruction.Destination] := Memory[Instruction.Source]; 110 Memory[Instruction.Source] := Temp; 111 end; 90 112 opIncrement: Memory[Instruction.Destination] := Memory[Instruction.Destination] + 1; 113 opDecrement: Memory[Instruction.Destination] := Memory[Instruction.Destination] - 1; 91 114 opAdd: Memory[Instruction.Destination] := Memory[Instruction.Destination] + Memory[Instruction.Source]; 115 opSubtract: Memory[Instruction.Destination] := Memory[Instruction.Destination] - Memory[Instruction.Source]; 116 opMultiply: Memory[Instruction.Destination] := Memory[Instruction.Destination] * Memory[Instruction.Source]; 117 opDivide: Memory[Instruction.Destination] := Memory[Instruction.Destination] div Memory[Instruction.Source]; 92 118 opSet: Memory[Instruction.Destination] := Instruction.Source; 93 119 opOutput: if Assigned(FOnOutput) then FOnOutput(Instruction.Destination, Memory[Instruction.Source]); … … 95 121 else Memory[Instruction.Destination] := 0; 96 122 opJump: IP := Instruction.Destination; 123 opJumpZero: if Memory[Instruction.Source] = 0 then IP := Instruction.Destination; 124 opJumpNotZero: if Memory[Instruction.Source] <> 0 then IP := IP + Instruction.Destination; 97 125 opJumpRelative: IP := IP + Instruction.Destination; 126 opJumpRelativeZero: if Memory[Instruction.Source] = 0 then IP := IP + Instruction.Destination; 127 opJumpRelativeNotZero: if Memory[Instruction.Source] <> 0 then IP := IP + Instruction.Destination; 98 128 opPush: Push(Memory[Instruction.Source]); 99 129 opPop: Memory[Instruction.Destination] := Pop; … … 103 133 end; 104 134 opReturn: IP := Pop; 135 opShiftLeft: Memory[Instruction.Destination] := Memory[Instruction.Destination] shl Memory[Instruction.Source]; 136 opShiftRight: Memory[Instruction.Destination] := Memory[Instruction.Destination] shr Memory[Instruction.Source]; 137 opOr: Memory[Instruction.Destination] := Memory[Instruction.Destination] or Memory[Instruction.Source]; 138 opXor: Memory[Instruction.Destination] := Memory[Instruction.Destination] xor Memory[Instruction.Source]; 139 opAnd: Memory[Instruction.Destination] := Memory[Instruction.Destination] and Memory[Instruction.Source]; 140 opSetBit: Memory[Instruction.Destination] := Memory[Instruction.Destination] or (1 shl Memory[Instruction.Source]); 141 opResetBit: Memory[Instruction.Destination] := Memory[Instruction.Destination] and (High(TData) xor (1 shl Memory[Instruction.Source])); 105 142 end; 106 143 … … 121 158 procedure TCpu.Run; 122 159 begin 123 if Length(Instructions)= 0 then160 if Instructions.Count = 0 then 124 161 raise Exception.Create('No program for execution.'); 125 162 Reset; … … 128 165 end; 129 166 167 constructor TCpu.Create; 168 begin 169 Instructions := TInstructions.Create; 170 end; 171 130 172 destructor TCpu.Destroy; 131 173 begin 132 174 Terminated := True; 175 FreeAndNil(Instructions); 133 176 end; 134 177 -
branches/test2/FormMain.pas
r13 r14 42 42 43 43 procedure TForm1.FormShow(Sender: TObject); 44 const 45 Value0 = 0; 46 Value1 = 0; 47 Port0 = 0; 44 48 begin 45 49 with Cpu do begin 46 50 OnOutput := OutputExecute; 47 AddInstruction(opSet, 0, 10);48 AddInstruction(opOutput, 0,0);49 AddInstruction(opIncrement, 0,0);50 AddInstruction(opOutput, 0,0);51 AddInstruction(opSet, 1, 5);52 AddInstruction(opAdd, 0,1);53 AddInstruction(opOutput, 0,0);54 AddInstruction(opTerminate , 0, 0);51 AddInstruction(opSet, Value0, 10); 52 AddInstruction(opOutput, Port0, Value0); 53 AddInstruction(opIncrement, Value0); 54 AddInstruction(opOutput, Port0, Value0); 55 AddInstruction(opSet, Value1, 5); 56 AddInstruction(opAdd, Value0, Value1); 57 AddInstruction(opOutput, Port0, Value0); 58 AddInstruction(opTerminate); 55 59 SetLength(Memory, 10000); 56 60 Run;
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/portvm/chrome/site/your_project_logo.png)