- Timestamp:
- Oct 16, 2018, 11:03:59 AM (6 years ago)
- Location:
- branches/virtcpu fixed int
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/virtcpu fixed int/UFormMain.lfm
r164 r168 1 1 object Form1: TForm1 2 Left = 4562 Left = 385 3 3 Height = 762 4 Top = 1634 Top = 208 5 5 Width = 1286 6 6 Caption = 'Form1' … … 80 80 TabOrder = 4 81 81 end 82 object LabelTicks: TLabel 83 Left = 16 84 Height = 20 85 Top = 612 86 Width = 35 87 Caption = 'Ticks:' 88 ParentColor = False 89 end 82 90 object Timer1: TTimer 83 91 Interval = 500 -
branches/virtcpu fixed int/UFormMain.pas
r165 r168 16 16 Button1: TButton; 17 17 Button2: TButton; 18 LabelTicks: TLabel; 18 19 ListViewMemory: TListView; 19 20 ListViewRegisters: TListView; … … 57 58 var 58 59 LabelStart: Integer; 59 begin 60 LabelPrint: Integer; 61 LabelPrintBack: Integer; 62 R0: Integer; 63 R1: Integer; 64 R2: Integer; 65 R3: Integer; 66 R4: Integer; 67 begin 68 R0 := 0; 69 R1 := 1; 70 R2 := 2; 71 R3 := 3; 72 R4 := 4; 60 73 with InstructionWriter do begin 61 { AddNop;62 AddLdc(1, $ffff);63 AddLd(0, 1);64 AddLdc(2, $10);65 AddStm(2, 0);74 {NoOperation; 75 LoadConst(1, $ffff); 76 Load(0, 1); 77 LoadConst(2, $10); 78 StoreMemory(2, 0); 66 79 } 67 68 AddLdc(2, 0); 69 AddLdc(0, 0); 70 LabelStart := IP; 71 AddIn(1, 0); 72 AddOut(0, 1); 73 AddInc(2); 74 AddJump(LabelStart); 75 AddHalt; 80 LabelPrint := $100; 81 82 LoadConst(R2, 0); 83 LoadConst(R0, 0); 84 LoadConst(R4, Ord('a')); 85 LabelStart := IP; 86 Input(R1, 0); 87 Output(0, R1); 88 Subtract(R1, R4); 89 JumpZero(R1, LabelPrint); 90 LabelPrintBack := IP; 91 Increment(R2); 92 Jump(LabelStart); 93 Halt; 94 IP := LabelPrint; 95 LoadConst(R3, Ord('!')); 96 Output(0, R3); 97 Jump(LabelPrintBack) 76 98 end; 77 99 end; … … 122 144 procedure TForm1.ListViewRegistersData(Sender: TObject; Item: TListItem); 123 145 begin 124 if Item.Index < Length(Cpu.Registers) then begin 125 Item.Caption := 'R' + IntToStr(Item.Index); 126 Item.SubItems.Add(IntToHex(Cpu.Registers[Item.Index], 8)); 146 if Item.Index < Length(Cpu.Registers) + 1 then begin 147 if Item.Index = 0 then begin 148 Item.Caption := 'IP'; 149 Item.SubItems.Add(IntToHex(Cpu.IP, 8)); 150 end else begin 151 Item.Caption := 'R' + IntToStr(Item.Index - 1); 152 Item.SubItems.Add(IntToHex(Cpu.Registers[Item.Index - 1], 8)); 153 end; 127 154 end; 128 155 end; … … 138 165 ReloadMemoryDump; 139 166 ReloadRegisterDump; 167 LabelTicks.Caption := 'Ticks: ' + IntToStr(Cpu.Ticks); 140 168 end; 141 169 -
branches/virtcpu fixed int/UInstructionWriter.pas
r165 r168 17 17 IP: T; 18 18 procedure Write(Value: T); 19 procedure AddNop; 20 procedure AddLd(R1, R2: Integer); 21 procedure AddHalt; 22 procedure AddLdc(R, Value: Integer); 23 procedure AddLdm(R1, R2: Integer); 24 procedure AddStm(R1, R2: Integer); 25 procedure AddNeg(R: Integer); 26 procedure AddIn(R1, R2: Integer); 27 procedure AddOut(R1, R2: Integer); 28 procedure AddJump(Addr: Integer); 29 {$IFDEF EXT_REL_JUMP}procedure AddJr(Addr: Integer);{$ENDIF} 30 procedure AddInc(R: Integer); 19 procedure NoOperation; 20 procedure Load(R1, R2: Integer); 21 procedure Halt; 22 procedure LoadConst(R, Value: Integer); 23 procedure LoadMemory(R1, R2: Integer); 24 procedure StoreMemory(R1, R2: Integer); 25 procedure Neg(R: Integer); 26 procedure Input(R1, R2: Integer); 27 procedure Output(R1, R2: Integer); 28 procedure Subtract(R1, R2: Integer); 29 procedure Jump(Addr: Integer); 30 procedure JumpNotZero(R1: Integer; Addr: Integer); 31 procedure JumpZero(R1: Integer; Addr: Integer); 32 {$IFDEF EXT_REL_JUMP}procedure JumpRelative(Addr: Integer);{$ENDIF} 33 procedure Increment(R: Integer); 34 procedure Decrement(R: Integer); 31 35 constructor Create; 32 36 end; … … 37 41 { TInstructionWriter } 38 42 39 procedure TInstructionWriter. AddNop;43 procedure TInstructionWriter.NoOperation; 40 44 begin 41 45 Write(T(opNop)); 42 46 end; 43 47 44 procedure TInstructionWriter. AddLd(R1, R2: Integer);48 procedure TInstructionWriter.Load(R1, R2: Integer); 45 49 begin 46 50 Write(T(opLoad)); … … 49 53 end; 50 54 51 procedure TInstructionWriter. AddHalt;55 procedure TInstructionWriter.Halt; 52 56 begin 53 57 Write(T(opHalt)); 54 58 end; 55 59 56 procedure TInstructionWriter. AddLdc(R, Value: Integer);60 procedure TInstructionWriter.LoadConst(R, Value: Integer); 57 61 begin 58 62 Write(T(opLoadConst)); … … 61 65 end; 62 66 63 procedure TInstructionWriter. AddLdm(R1, R2: Integer);67 procedure TInstructionWriter.LoadMemory(R1, R2: Integer); 64 68 begin 65 69 Write(T(opLoadMem)); … … 68 72 end; 69 73 70 procedure TInstructionWriter. AddStm(R1, R2: Integer);74 procedure TInstructionWriter.StoreMemory(R1, R2: Integer); 71 75 begin 72 76 Write(T(opStoreMem)); … … 75 79 end; 76 80 77 procedure TInstructionWriter. AddNeg(R: Integer);81 procedure TInstructionWriter.Neg(R: Integer); 78 82 begin 79 83 Write(T(opNeg)); … … 81 85 end; 82 86 83 procedure TInstructionWriter. AddIn(R1, R2: Integer);87 procedure TInstructionWriter.Input(R1, R2: Integer); 84 88 begin 85 89 Write(T(opInput)); … … 88 92 end; 89 93 90 procedure TInstructionWriter. AddOut(R1, R2: Integer);94 procedure TInstructionWriter.Output(R1, R2: Integer); 91 95 begin 92 96 Write(T(opOutput)); … … 95 99 end; 96 100 97 procedure TInstructionWriter.AddJump(Addr: Integer); 101 procedure TInstructionWriter.Subtract(R1, R2: Integer); 102 begin 103 Write(T(opSub)); 104 Write(R1); 105 Write(R2); 106 end; 107 108 procedure TInstructionWriter.Jump(Addr: Integer); 98 109 begin 99 110 Write(T(opJump)); … … 101 112 end; 102 113 114 procedure TInstructionWriter.JumpNotZero(R1: Integer; Addr: Integer); 115 begin 116 Write(T(opJumpNotZero)); 117 Write(R1); 118 Write(Addr); 119 end; 120 121 procedure TInstructionWriter.JumpZero(R1: Integer; Addr: Integer); 122 begin 123 Write(T(opJumpZero)); 124 Write(R1); 125 Write(Addr); 126 end; 127 103 128 {$IFDEF EXT_REL_JUMP} 104 procedure TInstructionWriter. AddJr(Addr: Integer);129 procedure TInstructionWriter.JumpRelative(Addr: Integer); 105 130 begin 106 131 Write(T(opJumpRel)); … … 109 134 {$ENDIF} 110 135 111 procedure TInstructionWriter. AddInc(R: Integer);136 procedure TInstructionWriter.Increment(R: Integer); 112 137 begin 113 138 Write(T(opInc)); 139 Write(R); 140 end; 141 142 procedure TInstructionWriter.Decrement(R: Integer); 143 begin 144 Write(T(opDec)); 114 145 Write(R); 115 146 end; -
branches/virtcpu fixed int/UMachine.pas
r166 r168 5 5 {$DEFINE EXT_ARITHMETIC} 6 6 {$DEFINE EXT_CONDITIONAL} 7 //{$DEFINE EXT_LOGICAL}8 //{$DEFINE EXT_STACK}9 //{$DEFINE EXT_SUBROUTINE}10 //{$DEFINE EXT_ROTATION}11 //{$DEFINE EXT_MULTIPLICATION}12 //{$DEFINE EXT_SHIFT}13 //{$DEFINE EXT_BLOCK}14 //{$DEFINE EXT_GENERAL}15 //{$DEFINE EXT_BIT}16 //{$DEFINE EXT_REL_JUMP}7 {$DEFINE EXT_LOGICAL} 8 {$DEFINE EXT_STACK} 9 {$DEFINE EXT_SUBROUTINE} 10 {$DEFINE EXT_ROTATION} 11 {$DEFINE EXT_MULTIPLICATION} 12 {$DEFINE EXT_SHIFT} 13 {$DEFINE EXT_BLOCK} 14 {$DEFINE EXT_GENERAL} 15 {$DEFINE EXT_BIT} 16 {$DEFINE EXT_REL_JUMP} 17 17 18 18 // Extension dependencies … … 50 50 {$IFDEF EXT_STACK}opPush, opPop,{$ENDIF} 51 51 {$IFDEF EXT_CONDITIONAL} 52 {$IFDEF EXT_REL_JUMP}opJumpRel Cond,{$ENDIF}53 opJump Cond, opTestEqual, opTestNotEqual, opTestLess,52 {$IFDEF EXT_REL_JUMP}opJumpRelZero, opJumpRelNotZero,{$ENDIF} 53 opJumpZero, opJumpNotZero, opTestEqual, opTestNotEqual, opTestLess, 54 54 opTestLessEqual, opTestGreater, opTestGreaterEqual, 55 55 {$ENDIF} … … 97 97 procedure OpcodeTestLessEqual; 98 98 procedure OpcodeTestLess; 99 procedure OpcodeJumpCond; 99 procedure OpcodeJumpCondNotZero; 100 procedure OpcodeJumpCondZero; 100 101 {$IFDEF EXT_REL_JUMP} 101 procedure OpcodeJumpRelCond; 102 procedure OpcodeJumpRelCondNotZero; 103 procedure OpcodeJumpRelCondZero; 102 104 {$ENDIF} 103 105 {$ENDIF} … … 141 143 Registers: array of T; 142 144 IP: T; 143 {$IFDEF EXT_CONDITIONAL}144 Condition: Boolean;145 {$ENDIF}146 145 {$IFDEF EXT_STACK} 147 146 SP: T; … … 149 148 Memory: array of T; 150 149 Terminated: Boolean; 150 Ticks: Integer; 151 151 procedure Start; 152 152 procedure Stop; … … 255 255 {$IFDEF EXT_CONDITIONAL} 256 256 procedure TCPU.OpcodeTestEqual; 257 begin 258 Condition := ReadNext = ReadNext; 257 var 258 P1, P2: T; 259 begin 260 P1 := ReadNext; 261 P2 := ReadNext; 262 if Registers[P1] = Registers[P2] then Registers[P1] := 1 263 else Registers[P1] := 0; 259 264 end; 260 265 261 266 procedure TCPU.OpcodeTestNotEqual; 262 begin 263 Condition := ReadNext <> ReadNext; 267 var 268 P1, P2: T; 269 begin 270 P1 := ReadNext; 271 P2 := ReadNext; 272 if Registers[P1] <> Registers[P2] then Registers[P1] := 1 273 else Registers[P1] := 0; 264 274 end; 265 275 266 276 procedure TCPU.OpcodeTestGreatEqual; 267 begin 268 Condition := ReadNext >= ReadNext; 277 var 278 P1, P2: T; 279 begin 280 P1 := ReadNext; 281 P2 := ReadNext; 282 if Registers[P1] >= Registers[P2] then Registers[P1] := 1 283 else Registers[P1] := 0; 269 284 end; 270 285 271 286 procedure TCPU.OpcodeTestGreat; 272 begin 273 Condition := ReadNext > ReadNext; 287 var 288 P1, P2: T; 289 begin 290 P1 := ReadNext; 291 P2 := ReadNext; 292 if Registers[P1] > Registers[P2] then Registers[P1] := 1 293 else Registers[P1] := 0; 274 294 end; 275 295 276 296 procedure TCPU.OpcodeTestLessEqual; 277 begin 278 Condition := ReadNext <= ReadNext; 297 var 298 P1, P2: T; 299 begin 300 P1 := ReadNext; 301 P2 := ReadNext; 302 if Registers[P1] <= Registers[P2] then Registers[P1] := 1 303 else Registers[P1] := 0; 279 304 end; 280 305 281 306 procedure TCPU.OpcodeTestLess; 282 begin 283 Condition := ReadNext < ReadNext; 284 end; 285 286 procedure TCPU.OpcodeJumpCond; 287 begin 288 if Condition then IP := ReadNext; 289 end; 290 307 var 308 P1, P2: T; 309 begin 310 P1 := ReadNext; 311 P2 := ReadNext; 312 if Registers[P1] < Registers[P2] then Registers[P1] := 1 313 else Registers[P1] := 0; 314 end; 315 316 procedure TCPU.OpcodeJumpCondNotZero; 317 var 318 P1, P2: T; 319 begin 320 P1 := ReadNext; 321 P2 := ReadNext; 322 if Registers[P1] <> 0 then IP := P2; 323 end; 324 325 procedure TCPU.OpcodeJumpCondZero; 326 var 327 P1, P2: T; 328 begin 329 P1 := ReadNext; 330 P2 := ReadNext; 331 if Registers[P1] = 0 then IP := P2; 332 end; 291 333 292 334 {$IFDEF EXT_REL_JUMP} 293 procedure TCPU.OpcodeJumpRelCond; 294 begin 295 if Condition then IP := IP + ReadNext; 335 procedure TCPU.OpcodeJumpRelCondZero; 336 var 337 P1, P2: T; 338 begin 339 P1 := ReadNext; 340 P2 := ReadNext; 341 if P1 = 0 then IP := IP + P2; 342 end; 343 344 procedure TCPU.OpcodeJumpRelCondNotZero; 345 var 346 P1, P2: T; 347 begin 348 P1 := ReadNext; 349 P2 := ReadNext; 350 if P1 <> 0 then IP := IP + P2; 296 351 end; 297 352 {$ENDIF} … … 503 558 Terminated := False; 504 559 IP := 0; 560 Ticks := 0; 505 561 {$IFDEF EXT_STACK} 506 562 SP := Length(Memory); … … 511 567 OpcodeHandlers[TOpcode(Opcode)] 512 568 else raise Exception.Create(Format('Unsupported instruction %d', [Opcode])); 569 Inc(Ticks); 513 570 end; 514 571 end; … … 575 632 {$ENDIF} 576 633 {$IFDEF EXT_CONDITIONAL} 577 OpcodeHandlers[opJumpCond] := OpcodeJumpCond; 634 OpcodeHandlers[opJumpZero] := OpcodeJumpCondZero; 635 OpcodeHandlers[opJumpNotZero] := OpcodeJumpCondNotZero; 578 636 {$IFDEF EXT_REL_JUMP} 579 OpcodeHandlers[opJumpRelCond] := OpcodeJumpRelCond; 637 OpcodeHandlers[opJumpRelZero] := OpcodeJumpRelCondZero; 638 OpcodeHandlers[opJumpRelNotZero] := OpcodeJumpRelCondNotZero; 580 639 {$ENDIF} 581 640 OpcodeHandlers[opTestEqual] := OpcodeTestEqual;
Note:
See TracChangeset
for help on using the changeset viewer.