Changeset 154
- Timestamp:
- Apr 23, 2018, 2:13:10 PM (7 years ago)
- Location:
- branches/virtualcpu2
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/virtualcpu2/UFormMain.lfm
r153 r154 7 7 ClientHeight = 732 8 8 ClientWidth = 1504 9 DesignTimePPI = 120 9 10 OnCreate = FormCreate 10 11 OnDestroy = FormDestroy 11 12 OnShow = FormShow 12 LCLVersion = '1.8. 0.4'13 LCLVersion = '1.8.2.0' 13 14 object ButtonRun: TButton 14 15 Left = 32 -
branches/virtualcpu2/UFormMain.pas
r153 r154 95 95 Counter = 3; 96 96 Temp = 4; 97 Jump = 5; 97 98 var 98 99 Writer: TInstructionWriter; … … 103 104 Machine := Self.Machine; 104 105 AddGetDataSize(Temp); 105 AddCopyConst (Temp, 1);106 AddCopyConst8(Temp, 1); 106 107 107 108 AddCopyConst(Counter, 8); … … 116 117 AddDec(Counter); 117 118 AddTestNotZero(Counter); 118 AddJumpRelCond8(LoopAddr - Addr); 119 AddCopyConst8(Jump, LoopAddr - (Addr + 3)); 120 AddJumpRelCond8(Jump); 119 121 AddHalt; 120 122 end; -
branches/virtualcpu2/UMachine.pas
r153 r154 6 6 7 7 uses 8 Classes, SysUtils , Math;8 Classes, SysUtils; 9 9 10 10 type 11 Int8= ShortInt;12 Int16= SmallInt;13 Int32= Longint;14 PInt8 = ^Int8;15 PInt16 = ^Int16;16 PInt32 = ^Int32;17 UInt8= Byte;18 UInt16= Word;19 UInt32= Cardinal;20 PUInt8= ^UInt8;21 PUInt16= ^UInt16;22 PUInt32= ^UInt16;11 Int8 = ShortInt; 12 Int16 = SmallInt; 13 Int32 = Longint; 14 PInt8 = ^Int8; 15 PInt16 = ^Int16; 16 PInt32 = ^Int32; 17 UInt8 = Byte; 18 UInt16 = Word; 19 UInt32 = Cardinal; 20 PUInt8 = ^UInt8; 21 PUInt16 = ^UInt16; 22 PUInt32 = ^UInt16; 23 23 24 24 … … 38 38 39 39 TOpcode = ( 40 // Special 41 opNop, 42 opHalt, 43 opGetSize, 44 opSetSize, opSetSize8, opSetSize16, opSetSize32, opSetSize64, 40 45 // Data move 41 46 opCopy, … … 82 87 // Block operations 83 88 opLdir, 84 opLddr, 85 // Special 86 opNop, 87 opHalt, 88 opGetSize, 89 opSetSize, opSetSize8, opSetSize16, opSetSize32, opSetSize64 89 opLddr 90 90 ); 91 91 … … 121 121 procedure OpcodeXor; 122 122 procedure OpcodePop; 123 procedure OpcodePop8; 124 procedure OpcodePop16; 125 procedure OpcodePop32; 126 procedure OpcodePop64; 123 127 procedure OpcodePush; 124 128 procedure OpcodePush8; … … 133 137 procedure OpcodeInc64; 134 138 procedure OpcodeDec; 139 procedure OpcodeDec8; 140 procedure OpcodeDec16; 141 procedure OpcodeDec32; 142 procedure OpcodeDec64; 135 143 procedure OpcodeTestNotZero; 136 144 procedure OpcodeTestZero; 137 145 procedure OpcodeJumpRel; 146 procedure OpcodeJumpRel8; 147 procedure OpcodeJumpRel16; 148 procedure OpcodeJumpRel32; 149 procedure OpcodeJumpRel64; 138 150 procedure OpcodeJumpAbs; 151 procedure OpcodeJumpAbs8; 152 procedure OpcodeJumpAbs16; 153 procedure OpcodeJumpAbs32; 154 procedure OpcodeJumpAbs64; 139 155 procedure OpcodeJumpRelCond; 140 156 procedure OpcodeJumpRelCond8; 157 procedure OpcodeJumpRelCond16; 158 procedure OpcodeJumpRelCond32; 159 procedure OpcodeJumpRelCond64; 141 160 procedure OpcodeJumpAbsCond; 161 procedure OpcodeJumpAbsCond8; 162 procedure OpcodeJumpAbsCond16; 163 procedure OpcodeJumpAbsCond32; 164 procedure OpcodeJumpAbsCond64; 142 165 procedure OpcodeCallRel; 143 166 procedure OpcodeCallAbs; … … 151 174 procedure OpcodeSetSize64; 152 175 function ReadNext: TDataInt; 176 function ReadNext8: UInt8; 177 function ReadNext16: UInt16; 178 function ReadNext32: UInt32; 179 function ReadNext64: UInt64; 153 180 function ReadNextS: TDataInt; 154 function ReadNext8: Byte; 155 function ReadNextS8: ShortInt; 156 function ReadNext16: Word; 157 function ReadNext32: Cardinal; 158 function ReadNext64: QWord; 181 function ReadNextS8: Int8; 182 function ReadNextS16: Int16; 183 function ReadNextS32: Int32; 184 function ReadNextS64: Int64; 159 185 procedure SetOpcode(Opcode: TOpcode; Handler: TOpcodeHandler; DataSize: TDataSize = dsNative); 160 186 procedure InitOpcode; … … 192 218 procedure AddHalt; 193 219 procedure AddTestNotZero(Reg: TRegIndex); 194 procedure AddJumpRelCond(Re lAddr: Integer);195 procedure AddJumpRelCond8(Re lAddr: ShortInt);220 procedure AddJumpRelCond(Reg: Integer); 221 procedure AddJumpRelCond8(Reg: TRegIndex); 196 222 constructor Create; 197 223 end; … … 244 270 SetOpcode(opInc, OpcodeInc64, ds64); 245 271 SetOpcode(opDec, OpcodeDec); 272 SetOpcode(opDec, OpcodeDec8, ds8); 273 SetOpcode(opDec, OpcodeDec16, ds16); 274 SetOpcode(opDec, OpcodeDec32, ds32); 275 SetOpcode(opDec, OpcodeDec64, ds64); 246 276 SetOpcode(opTestZero, OpcodeTestZero); 247 277 SetOpcode(opTestNotZero, OpcodeTestNotZero); 248 278 SetOpcode(opJumpRel, OpcodeJumpRel); 279 SetOpcode(opJumpRel, OpcodeJumpRel8, ds8); 280 SetOpcode(opJumpRel, OpcodeJumpRel16, ds16); 281 SetOpcode(opJumpRel, OpcodeJumpRel32, ds32); 282 SetOpcode(opJumpRel, OpcodeJumpRel64, ds64); 249 283 SetOpcode(opJumpAbs, OpcodeJumpAbs); 284 SetOpcode(opJumpAbs, OpcodeJumpAbs8, ds8); 285 SetOpcode(opJumpAbs, OpcodeJumpAbs16, ds16); 286 SetOpcode(opJumpAbs, OpcodeJumpAbs32, ds32); 287 SetOpcode(opJumpAbs, OpcodeJumpAbs64, ds64); 250 288 SetOpcode(opJumpRelCond, OpcodeJumpRelCond); 251 289 SetOpcode(opJumpRelCond, OpcodeJumpRelCond8, ds8); 290 SetOpcode(opJumpRelCond, OpcodeJumpRelCond16, ds16); 291 SetOpcode(opJumpRelCond, OpcodeJumpRelCond32, ds32); 292 SetOpcode(opJumpRelCond, OpcodeJumpRelCond64, ds64); 252 293 SetOpcode(opJumpAbsCond, OpcodeJumpAbsCond); 253 SetOpcode(opCallRel, OpcodeCAllRel); 294 SetOpcode(opJumpAbsCond, OpcodeJumpAbsCond8, ds8); 295 SetOpcode(opJumpAbsCond, OpcodeJumpAbsCond16, ds16); 296 SetOpcode(opJumpAbsCond, OpcodeJumpAbsCond32, ds32); 297 SetOpcode(opJumpAbsCond, OpcodeJumpAbsCond64, ds64); 298 SetOpcode(opCallRel, OpcodeCallRel); 254 299 SetOpcode(opCallAbs, OpcodeCallAbs); 255 300 SetOpcode(opCallRelCond, OpcodeCallRelCond); … … 267 312 SetOpcode(opPush, OpcodePush64, ds64); 268 313 SetOpcode(opPop, OpcodePop); 314 SetOpcode(opPop, OpcodePop8, ds8); 315 SetOpcode(opPop, OpcodePop16, ds16); 316 SetOpcode(opPop, OpcodePop32, ds32); 317 SetOpcode(opPop, OpcodePop64, ds64); 318 SetOpcode(opSetSize, OpcodeSetSize); 269 319 SetOpcode(opSetSize8, OpcodeSetSize8); 320 SetOpcode(opSetSize16, OpcodeSetSize16); 321 SetOpcode(opSetSize32, OpcodeSetSize32); 322 SetOpcode(opSetSize64, OpcodeSetSize64); 270 323 end; 271 324 … … 495 548 procedure TMachine.OpcodePop; 496 549 var 497 Dest: TRegIndex;498 begin 499 Dest:= ReadNext8;500 PDataInt(@Registers[ Dest])^ := PDataInt(@Memory[SP])^;550 Reg: TRegIndex; 551 begin 552 Reg := ReadNext8; 553 PDataInt(@Registers[Reg])^ := PDataInt(@Memory[SP])^; 501 554 Inc(SP, SizeOf(TDataInt)); 555 end; 556 557 procedure TMachine.OpcodePop8; 558 var 559 Reg: TRegIndex; 560 begin 561 Reg := ReadNext8; 562 PUInt8(@Registers[Reg])^ := PUInt8(@Memory[SP])^; 563 Inc(SP, SizeOf(UInt8)); 564 end; 565 566 procedure TMachine.OpcodePop16; 567 var 568 Reg: TRegIndex; 569 begin 570 Reg := ReadNext8; 571 PUInt16(@Registers[Reg])^ := PUInt16(@Memory[SP])^; 572 Inc(SP, SizeOf(UInt16)); 573 end; 574 575 procedure TMachine.OpcodePop32; 576 var 577 Reg: TRegIndex; 578 begin 579 Reg := ReadNext8; 580 PUInt32(@Registers[Reg])^ := PUInt32(@Memory[SP])^; 581 Inc(SP, SizeOf(UInt32)); 582 end; 583 584 procedure TMachine.OpcodePop64; 585 var 586 Reg: TRegIndex; 587 begin 588 Reg := ReadNext8; 589 PUInt64(@Registers[Reg])^ := PUInt64(@Memory[SP])^; 590 Inc(SP, SizeOf(UInt64)); 502 591 end; 503 592 … … 612 701 end; 613 702 703 procedure TMachine.OpcodeDec8; 704 var 705 Reg: TRegIndex; 706 begin 707 Reg := ReadNext8; 708 PUInt8(@Registers[Reg])^ := PUInt8(@Registers[Reg])^ - 1; 709 end; 710 711 procedure TMachine.OpcodeDec16; 712 var 713 Reg: TRegIndex; 714 begin 715 Reg := ReadNext8; 716 PUInt16(@Registers[Reg])^ := PUInt16(@Registers[Reg])^ - 1; 717 end; 718 719 procedure TMachine.OpcodeDec32; 720 var 721 Reg: TRegIndex; 722 begin 723 Reg := ReadNext8; 724 PUInt32(@Registers[Reg])^ := PUInt32(@Registers[Reg])^ - 1; 725 end; 726 727 procedure TMachine.OpcodeDec64; 728 var 729 Reg: TRegIndex; 730 begin 731 Reg := ReadNext8; 732 PUInt64(@Registers[Reg])^ := PUInt64(@Registers[Reg])^ - 1; 733 end; 734 614 735 procedure TMachine.OpcodeTestNotZero; 615 736 var … … 630 751 procedure TMachine.OpcodeJumpRel; 631 752 var 632 RelAddr: TAddrInt; 633 begin 634 RelAddr := ReadNextS; 635 IP := IP + RelAddr; 753 Reg: TRegIndex; 754 begin 755 Reg := ReadNext8; 756 IP := IP + PDataInt(@Registers[Reg])^; 757 end; 758 759 procedure TMachine.OpcodeJumpRel8; 760 var 761 Reg: TRegIndex; 762 begin 763 Reg := ReadNext8; 764 IP := IP + PInt8(@Registers[Reg])^; 765 end; 766 767 procedure TMachine.OpcodeJumpRel16; 768 var 769 Reg: TRegIndex; 770 begin 771 Reg := ReadNext8; 772 IP := IP + PInt16(@Registers[Reg])^; 773 end; 774 775 procedure TMachine.OpcodeJumpRel32; 776 var 777 Reg: TRegIndex; 778 begin 779 Reg := ReadNext8; 780 IP := IP + PInt32(@Registers[Reg])^; 781 end; 782 783 procedure TMachine.OpcodeJumpRel64; 784 var 785 Reg: TRegIndex; 786 begin 787 Reg := ReadNext8; 788 IP := IP + PInt64(@Registers[Reg])^; 636 789 end; 637 790 638 791 procedure TMachine.OpcodeJumpAbs; 639 792 var 640 Addr: TAddrInt; 641 begin 642 Addr := ReadNext; 643 IP := Addr; 793 Reg: TRegIndex; 794 begin 795 Reg := ReadNext8; 796 IP := PDataInt(@Registers[Reg])^; 797 end; 798 799 procedure TMachine.OpcodeJumpAbs8; 800 var 801 Reg: TRegIndex; 802 begin 803 Reg := ReadNext8; 804 IP := PByte(@Registers[Reg])^; 805 end; 806 807 procedure TMachine.OpcodeJumpAbs16; 808 var 809 Reg: TRegIndex; 810 begin 811 Reg := ReadNext8; 812 IP := PWord(@Registers[Reg])^; 813 end; 814 815 procedure TMachine.OpcodeJumpAbs32; 816 var 817 Reg: TRegIndex; 818 begin 819 Reg := ReadNext8; 820 IP := PCardinal(@Registers[Reg])^; 821 end; 822 823 procedure TMachine.OpcodeJumpAbs64; 824 var 825 Reg: TRegIndex; 826 begin 827 Reg := ReadNext8; 828 IP := PQWord(@Registers[Reg])^; 644 829 end; 645 830 646 831 procedure TMachine.OpcodeJumpRelCond; 647 832 var 648 Re lAddr: TAddrInt;649 begin 650 Re lAddr := ReadNextS;651 if Condition then IP := IP + RelAddr;833 Reg: TRegIndex; 834 begin 835 Reg := ReadNext8; 836 if Condition then IP := IP + PDataInt(@Registers[Reg])^; 652 837 end; 653 838 654 839 procedure TMachine.OpcodeJumpRelCond8; 655 840 var 656 RelAddr: Int8; 657 begin 658 RelAddr := ReadNextS8; 659 if Condition then IP := IP + RelAddr; 841 Reg: TRegIndex; 842 begin 843 Reg := ReadNext8; 844 if Condition then IP := IP + PInt8(@Registers[Reg])^; 845 end; 846 847 procedure TMachine.OpcodeJumpRelCond16; 848 var 849 Reg: TRegIndex; 850 begin 851 Reg := ReadNext8; 852 if Condition then IP := IP + PInt16(@Registers[Reg])^; 853 end; 854 855 procedure TMachine.OpcodeJumpRelCond32; 856 var 857 Reg: TRegIndex; 858 begin 859 Reg := ReadNext8; 860 if Condition then IP := IP + PInt32(@Registers[Reg])^; 861 end; 862 863 procedure TMachine.OpcodeJumpRelCond64; 864 var 865 Reg: TRegIndex; 866 begin 867 Reg := ReadNext8; 868 if Condition then IP := IP + PInt64(@Registers[Reg])^; 660 869 end; 661 870 662 871 procedure TMachine.OpcodeJumpAbsCond; 663 872 var 664 Addr: TAddrInt; 665 begin 666 Addr := ReadNext; 667 if Condition then IP := Addr; 873 Reg: TRegIndex; 874 begin 875 Reg := ReadNext8; 876 if Condition then IP := PDataInt(@Registers[Reg])^; 877 end; 878 879 procedure TMachine.OpcodeJumpAbsCond8; 880 var 881 Reg: TRegIndex; 882 begin 883 Reg := ReadNext8; 884 if Condition then IP := PByte(@Registers[Reg])^; 885 end; 886 887 procedure TMachine.OpcodeJumpAbsCond16; 888 var 889 Reg: TRegIndex; 890 begin 891 Reg := ReadNext8; 892 if Condition then IP := PWord(@Registers[Reg])^; 893 end; 894 895 procedure TMachine.OpcodeJumpAbsCond32; 896 var 897 Reg: TRegIndex; 898 begin 899 Reg := ReadNext8; 900 if Condition then IP := PCardinal(@Registers[Reg])^; 901 end; 902 903 procedure TMachine.OpcodeJumpAbsCond64; 904 var 905 Reg: TRegIndex; 906 begin 907 Reg := ReadNext8; 908 if Condition then IP := PQWord(@Registers[Reg])^; 668 909 end; 669 910 … … 762 1003 end; 763 1004 764 function TMachine.ReadNext8: Byte;1005 function TMachine.ReadNext8: UInt8; 765 1006 begin 766 1007 CheckIP; … … 769 1010 end; 770 1011 771 function TMachine.ReadNextS8: ShortInt;1012 function TMachine.ReadNextS8: Int8; 772 1013 begin 773 1014 CheckIP; … … 776 1017 end; 777 1018 778 function TMachine.ReadNext16: Word; 1019 function TMachine.ReadNextS16: Int16; 1020 begin 1021 CheckIP; 1022 Result := PInt16(@Memory[IP])^; 1023 Inc(IP, SizeOf(Int16)); 1024 end; 1025 1026 function TMachine.ReadNextS32: Int32; 1027 begin 1028 CheckIP; 1029 Result := PInt32(@Memory[IP])^; 1030 Inc(IP, SizeOf(Int32)); 1031 end; 1032 1033 function TMachine.ReadNextS64: Int64; 1034 begin 1035 CheckIP; 1036 Result := PInt64(@Memory[IP])^; 1037 Inc(IP, SizeOf(Int64)); 1038 end; 1039 1040 function TMachine.ReadNext16: UInt16; 779 1041 begin 780 1042 CheckIP; … … 783 1045 end; 784 1046 785 function TMachine.ReadNext32: Cardinal;1047 function TMachine.ReadNext32: UInt32; 786 1048 begin 787 1049 CheckIP; … … 790 1052 end; 791 1053 792 function TMachine.ReadNext64: QWord;1054 function TMachine.ReadNext64: UInt64; 793 1055 begin 794 1056 CheckIP; … … 881 1143 end; 882 1144 883 procedure TInstructionWriter.AddJumpRelCond(Re lAddr: Integer);1145 procedure TInstructionWriter.AddJumpRelCond(Reg: Integer); 884 1146 begin 885 1147 AddData8(Integer(opJumpRelCond)); 886 AddDataS(Re lAddr - SizeOf(Byte) - SizeOf(TDataInt));887 end; 888 889 procedure TInstructionWriter.AddJumpRelCond8(Re lAddr: ShortInt);1148 AddDataS(Reg); 1149 end; 1150 1151 procedure TInstructionWriter.AddJumpRelCond8(Reg: TRegIndex); 890 1152 begin 891 1153 AddData8(Integer(opSetSize8)); 892 1154 AddData8(Integer(opJumpRelCond)); 893 AddData S8(RelAddr - SizeOf(Byte) - SizeOf(TDataInt));1155 AddData8(Reg); 894 1156 end; 895 1157
Note:
See TracChangeset
for help on using the changeset viewer.