Changeset 7 for branches/bigint/Cpu.pas
- Timestamp:
- Aug 1, 2024, 11:47:03 PM (3 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/bigint/Cpu.pas
r3 r7 10 10 11 11 TInstruction = (inNop, inHalt, inLoad, inLoadConst, inInput, inOutput, 12 inJump, inJumpZero, inJumpNotZero, inInc, inDec); 12 inJump, inJumpZero, inJumpNotZero, inInc, inDec, inLoadIndex, inPush, inPop, 13 inCall, inRet, inLoadMem, inStoreMem); 13 14 14 15 { TCpu } … … 25 26 procedure InstructionLoad; 26 27 procedure InstructionLoadConst; 28 procedure InstructionLoadIndex; 29 procedure InstructionLoadMem; 30 procedure InstructionStoreMem; 27 31 procedure InstructionInput; 28 32 procedure InstructionOutput; … … 32 36 procedure InstructionJumpZero; 33 37 procedure InstructionJumpNotZero; 38 procedure InstructionPush; 39 procedure InstructionPop; 40 procedure InstructionCall; 41 procedure InstructionRet; 34 42 procedure WriteMem(Address, Data: TInt); 35 43 function ReadMem(Address: TInt): TInt; … … 37 45 procedure WriteIo(Address, Data: TInt); 38 46 function ReadIo(Address: TInt): TInt; 47 procedure Push(Data: TInt); 48 function Pop: TInt; 39 49 public 40 50 Halted: Boolean; … … 83 93 end; 84 94 95 procedure TCpu.InstructionLoadIndex; 96 var 97 Dst, Src, Index: TInt; 98 begin 99 Dst := ReadMemPc; 100 Src := ReadMemPc; 101 Index := ReadMemPc; 102 WriteMem(Dst, ReadMem(Src + Index)); 103 end; 104 105 procedure TCpu.InstructionLoadMem; 106 var 107 Dst, Src: TInt; 108 begin 109 Dst := ReadMemPc; 110 Src := ReadMemPc; 111 WriteMem(Dst, ReadMem(ReadMem(Src))); 112 end; 113 114 procedure TCpu.InstructionStoreMem; 115 var 116 Dst, Src: TInt; 117 begin 118 Dst := ReadMemPc; 119 Src := ReadMemPc; 120 WriteMem(ReadMem(Dst), ReadMem(Src)); 121 end; 122 85 123 procedure TCpu.InstructionInput; 86 124 var … … 119 157 procedure TCpu.InstructionJump; 120 158 begin 121 PC := ReadMem (ReadMemPc);159 PC := ReadMemPc; 122 160 end; 123 161 … … 140 178 Addr := ReadMemPc; 141 179 if ReadMem(Condition) <> 0 then PC := Addr; 180 end; 181 182 procedure TCpu.InstructionPush; 183 begin 184 Push(ReadMem(ReadMemPc)); 185 end; 186 187 procedure TCpu.InstructionPop; 188 begin 189 WriteMem(ReadMemPc, Pop); 190 end; 191 192 procedure TCpu.InstructionCall; 193 var 194 Addr: TInt; 195 begin 196 Addr := ReadMemPc; 197 Push(PC); 198 PC := Addr; 199 end; 200 201 procedure TCpu.InstructionRet; 202 begin 203 PC := Pop; 142 204 end; 143 205 … … 170 232 end; 171 233 234 procedure TCpu.Push(Data: TInt); 235 begin 236 Dec(SP); 237 WriteMem(SP, Data); 238 end; 239 240 function TCpu.Pop: TInt; 241 begin 242 Result := ReadMem(SP); 243 Inc(SP); 244 end; 245 172 246 procedure TCpu.Reset; 173 247 begin 174 248 PC := 0; 175 SP := 0;249 SP := 1000; 176 250 Halted := False; 177 251 end; … … 198 272 Instructions[inHalt] := InstructionHalt; 199 273 Instructions[inLoad] := InstructionLoad; 274 Instructions[inLoadIndex] := InstructionLoadIndex; 200 275 Instructions[inLoadConst] := InstructionLoadConst; 276 Instructions[inLoadMem] := InstructionLoadMem; 277 Instructions[inStoreMem] := InstructionStoreMem; 201 278 Instructions[inInput] := InstructionInput; 202 279 Instructions[inOutput] := InstructionOutput; … … 206 283 Instructions[inInc] := InstructionInc; 207 284 Instructions[inDec] := InstructionDec; 285 Instructions[inPush] := InstructionPush; 286 Instructions[inPop] := InstructionPop; 287 Instructions[inCall] := InstructionCall; 288 Instructions[inRet] := InstructionRet; 208 289 end; 209 290
Note:
See TracChangeset
for help on using the changeset viewer.