Changeset 185
- Timestamp:
- May 1, 2019, 9:48:46 PM (6 years ago)
- Location:
- branches/virtualcpu4
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/virtualcpu4/Forms/UFormAssembler.lfm
r184 r185 1 1 object FormAssembler: TFormAssembler 2 2 Left = 474 3 Height = 5403 Height = 648 4 4 Top = 313 5 Width = 7055 Width = 846 6 6 Caption = 'Assembler' 7 ClientHeight = 5408 ClientWidth = 7059 DesignTimePPI = 1 207 ClientHeight = 648 8 ClientWidth = 846 9 DesignTimePPI = 144 10 10 OnCreate = FormCreate 11 11 OnDestroy = FormDestroy 12 LCLVersion = '2.0. 2.0'12 LCLVersion = '2.0.0.4' 13 13 object MemoSource: TMemo 14 Left = 815 Height = 38316 Top = 817 Width = 69114 Left = 10 15 Height = 459 16 Top = 10 17 Width = 829 18 18 Anchors = [akTop, akLeft, akRight, akBottom] 19 19 Lines.Strings = ( 20 'LD R1, R2' 21 'LDI R1, 10' 20 ' // Print Hello world text' 21 ' LDI R1, HelloWorld' 22 ' LDI R2, 13' 23 ' CALL Print' 24 ' HALT' 25 '' 26 'Print:' 27 ' PUSH R3' 28 ' PUSH R2' 29 ' PUSH R1' 30 'PrintLoop:' 31 ' DP8 ;LDM R3, R1' 32 ' AP8; DP8; OUT (0), R3' 33 ' INC R1' 34 ' DEC R2' 35 ' TEST R2' 36 ' AP8; JRNZ PrintLoop' 37 ' POP R1' 38 ' POP R2' 39 ' POP R3' 40 ' RET' 41 'HelloWorld:' 42 ' STRING ''Hello_World!''' 22 43 ) 44 ParentFont = False 23 45 ScrollBars = ssAutoBoth 24 46 TabOrder = 0 25 47 end 26 48 object ButtonLoadFromFile: TButton 27 Left = 828 Height = 3 129 Top = 50230 Width = 1 3649 Left = 10 50 Height = 37 51 Top = 603 52 Width = 163 31 53 Anchors = [akLeft, akBottom] 32 54 Caption = 'Load from file' 33 55 OnClick = ButtonLoadFromFileClick 56 ParentFont = False 34 57 TabOrder = 1 35 58 end 36 59 object ButtonSaveToFile: TButton 37 Left = 1 6038 Height = 3 139 Top = 50240 Width = 1 2860 Left = 192 61 Height = 37 62 Top = 603 63 Width = 154 41 64 Anchors = [akLeft, akBottom] 42 65 Caption = 'Save to file' 43 66 OnClick = ButtonSaveToFileClick 67 ParentFont = False 44 68 TabOrder = 2 45 69 end 46 70 object ButtonCompile: TButton 47 Left = 29748 Height = 3 149 Top = 50250 Width = 1 2671 Left = 356 72 Height = 37 73 Top = 603 74 Width = 151 51 75 Anchors = [akLeft, akBottom] 52 76 Caption = 'Compile' 53 77 OnClick = ButtonCompileClick 78 ParentFont = False 54 79 TabOrder = 3 55 80 end 56 81 object MemoMessages: TMemo 57 Left = 858 Height = 9759 Top = 4 0260 Width = 68982 Left = 10 83 Height = 116 84 Top = 483 85 Width = 826 61 86 Anchors = [akLeft, akRight, akBottom] 87 ParentFont = False 62 88 ReadOnly = True 63 89 ScrollBars = ssAutoBoth … … 65 91 end 66 92 object OpenDialog1: TOpenDialog 67 left = 1 1468 top = 1 5293 left = 137 94 top = 182 69 95 end 70 96 object SaveDialog1: TSaveDialog 71 left = 37972 top = 1 5297 left = 455 98 top = 182 73 99 end 74 100 end -
branches/virtualcpu4/Forms/UFormMain.lfm
r184 r185 1 1 object FormMain: TFormMain 2 2 Left = 780 3 Height = 2 243 Height = 269 4 4 Top = 527 5 Width = 5495 Width = 659 6 6 Caption = 'VirtCpu4' 7 ClientHeight = 2 248 ClientWidth = 5499 DesignTimePPI = 1 207 ClientHeight = 269 8 ClientWidth = 659 9 DesignTimePPI = 144 10 10 OnCreate = FormCreate 11 11 OnDestroy = FormDestroy 12 12 OnShow = FormShow 13 LCLVersion = '2.0. 2.0'13 LCLVersion = '2.0.0.4' 14 14 object ButtonStart: TButton 15 Left = 19116 Height = 3 117 Top = 1 418 Width = 9415 Left = 229 16 Height = 37 17 Top = 17 18 Width = 113 19 19 Caption = 'Start' 20 20 OnClick = ButtonStartClick … … 23 23 end 24 24 object ButtonStop: TButton 25 Left = 19126 Height = 3 127 Top = 5428 Width = 9425 Left = 229 26 Height = 37 27 Top = 65 28 Width = 113 29 29 Caption = 'Stop' 30 30 OnClick = ButtonStopClick … … 33 33 end 34 34 object ButtonDisassembler: TButton 35 Left = 3 0936 Height = 3 237 Top = 1 438 Width = 1 5435 Left = 371 36 Height = 38 37 Top = 17 38 Width = 185 39 39 Caption = 'Disassembler' 40 40 OnClick = ButtonDisassemblerClick … … 43 43 end 44 44 object ButtonMemory: TButton 45 Left = 3 0946 Height = 3 247 Top = 5348 Width = 1 5445 Left = 371 46 Height = 38 47 Top = 64 48 Width = 185 49 49 Caption = 'Memory' 50 50 OnClick = ButtonMemoryClick … … 53 53 end 54 54 object ButtonCpuState: TButton 55 Left = 3 0956 Height = 3 257 Top = 9258 Width = 1 5455 Left = 371 56 Height = 38 57 Top = 110 58 Width = 185 59 59 Caption = 'CPU state' 60 60 OnClick = ButtonCpuStateClick … … 63 63 end 64 64 object ButtonScreen: TButton 65 Left = 2 066 Height = 3 267 Top = 1 368 Width = 1 5465 Left = 24 66 Height = 38 67 Top = 16 68 Width = 185 69 69 Caption = 'Screen' 70 70 OnClick = ButtonScreenClick … … 73 73 end 74 74 object ButtonConsole: TButton 75 Left = 2 076 Height = 3 277 Top = 5378 Width = 1 5475 Left = 24 76 Height = 38 77 Top = 64 78 Width = 185 79 79 Caption = 'Console' 80 80 OnClick = ButtonConsoleClick … … 83 83 end 84 84 object ButtonAssembler: TButton 85 Left = 3 0986 Height = 3 287 Top = 1 2888 Width = 1 5485 Left = 371 86 Height = 38 87 Top = 154 88 Width = 185 89 89 Caption = 'Assembler' 90 90 OnClick = ButtonAssemblerClick … … 92 92 TabOrder = 7 93 93 end 94 object ButtonClearMemory: TButton 95 Left = 371 96 Height = 38 97 Top = 192 98 Width = 185 99 Caption = 'Clear memory' 100 OnClick = ButtonClearMemoryClick 101 ParentFont = False 102 TabOrder = 8 103 end 94 104 end -
branches/virtualcpu4/Forms/UFormMain.pas
r184 r185 15 15 TFormMain = class(TForm) 16 16 ButtonAssembler: TButton; 17 ButtonClearMemory: TButton; 17 18 ButtonScreen: TButton; 18 19 ButtonDisassembler: TButton; … … 23 24 ButtonStop: TButton; 24 25 procedure ButtonAssemblerClick(Sender: TObject); 26 procedure ButtonClearMemoryClick(Sender: TObject); 25 27 procedure ButtonConsoleClick(Sender: TObject); 26 28 procedure ButtonCpuStateClick(Sender: TObject); … … 111 113 end; 112 114 115 procedure TFormMain.ButtonClearMemoryClick(Sender: TObject); 116 begin 117 Machine.ClearMemory; 118 end; 119 113 120 procedure TFormMain.ButtonStopClick(Sender: TObject); 114 121 begin -
branches/virtualcpu4/UAssembler.pas
r184 r185 20 20 Text: string; 21 21 procedure Expect(Text: string); 22 function IsOperator(C: Char): Boolean; 23 function IsWhiteSpace(C: Char): Boolean; 22 24 function ReadNext: string; 23 25 function EndOfText: Boolean; … … 25 27 end; 26 28 29 TLabelRef = class 30 Address: QWord; 31 BitWidth: TBitWidth; 32 Relative: Boolean; 33 end; 34 35 { TLabel } 36 27 37 TLabel = class 28 38 Name: string; 29 39 Address: QWord; 30 end; 40 Refs: TFPGList<TLabelRef>; 41 constructor Create; 42 destructor Destroy; override; 43 end; 44 45 { TLabels } 31 46 32 47 TLabels = class(TFPGObjecTList<TLabel>) 33 48 function SearchByName(Name: string): TLabel; 34 49 end; 35 50 … … 40 55 FOnError: TErrorEvent; 41 56 OpcodeDefs: TOpcodeDefs; 57 InstructionIP: QWord; 42 58 procedure Error(Text: string); 43 59 procedure ParseParam(Param: TOpcodeParam); 60 procedure WriteRefAddr(Name: string; Relative: Boolean = False); 61 procedure WriteRefData(Name: string); 62 procedure UpdateLabelRef; 63 procedure ParseInstruction; 44 64 public 45 65 Parser: TParser; … … 56 76 implementation 57 77 78 { TLabels } 79 80 function TLabels.SearchByName(Name: string): TLabel; 81 var 82 I: Integer; 83 begin 84 I := 0; 85 while (I < Count) and (Items[I].Name <> Name) do Inc(I); 86 if I < Count then Result := Items[I] 87 else Result := nil; 88 end; 89 90 { TLabel } 91 92 constructor TLabel.Create; 93 begin 94 Refs := TFPGList<TLabelRef>.Create; 95 end; 96 97 destructor TLabel.Destroy; 98 begin 99 Refs.Free; 100 inherited Destroy; 101 end; 102 58 103 { TParser } 59 104 … … 72 117 end; 73 118 119 function TParser.IsOperator(C: Char): Boolean; 120 begin 121 Result := (C = ',') or (C = ';'); 122 end; 123 124 function TParser.IsWhiteSpace(C: Char): Boolean; 125 begin 126 Result := (C = ' ') or (C = #8); 127 end; 128 74 129 function TParser.ReadNext: string; 75 130 var … … 78 133 Text := Trim(Text); 79 134 P := 1; 80 if (Length(Text) > 0) and (Text[P] = ',') then begin135 if (Length(Text) > 0) and IsOperator(Text[P]) then begin 81 136 Result := Text[P]; 82 137 Delete(Text, 1, 1); 83 138 end else begin 84 while (P <= Length(Text)) and (Text[P] <> ' ') and (Text[P] <> ',') do Inc(P);139 while (P <= Length(Text)) and not IsWhiteSpace(Text[P]) and not IsOperator(Text[P]) do Inc(P); 85 140 Result := Copy(Text, 1, P - 1); 86 141 Delete(Text, 1, P - 1); … … 103 158 var 104 159 Reg: TRegIndex; 105 Addr: QWord;160 Addr: Int64; 106 161 Next: string; 107 162 begin … … 115 170 if Param = prData then begin 116 171 Next := Parser.ReadNext; 117 InstructionWriter.WriteData(StrToInt(Next)); 172 if TryStrToInt64(Next, Addr) then 173 InstructionWriter.WriteData(Addr) 174 else WriteRefData(Next); 118 175 end else 119 176 if Param = prAddr then begin 120 177 Next := Parser.ReadNext; 121 InstructionWriter.WriteAddress(StrToInt(Next)); 178 if TryStrToInt64(Next, Addr) then 179 InstructionWriter.WriteAddress(Addr) 180 else WriteRefAddr(Next); 122 181 end else 123 182 if Param = prAddrRel then begin 124 183 Next := Parser.ReadNext; 125 InstructionWriter.WriteAddress(StrToInt(Next)); 126 end; 184 if TryStrToInt64(Next, Addr) then 185 InstructionWriter.WriteAddress(InstructionWriter.IP + Addr) 186 else WriteRefAddr(Next, True); 187 end; 188 end; 189 190 procedure TAssembler.WriteRefAddr(Name: string; Relative: Boolean = False); 191 var 192 L: TLabel; 193 NewRef: TLabelRef; 194 begin 195 L := Labels.SearchByName(Name); 196 if Assigned(L) then begin 197 if Relative then 198 InstructionWriter.WriteAddressSigned(InstructionWriter.GetRelativeAddr( 199 InstructionWriter.AddrSize, InstructionIP, L.Address)) 200 else InstructionWriter.WriteAddress(L.Address); 201 end else begin 202 L := TLabel.Create; 203 L.Name := Name; 204 NewRef := TLabelRef.Create; 205 NewRef.Address := InstructionWriter.IP; 206 NewRef.BitWidth := InstructionWriter.AddrSize; 207 if Relative then NewRef.Relative := True; 208 L.Refs.Add(NewRef); 209 Labels.Add(L); 210 InstructionWriter.WriteAddress(0); 211 end; 212 end; 213 214 procedure TAssembler.WriteRefData(Name: string); 215 var 216 L: TLabel; 217 NewRef: TLabelRef; 218 begin 219 L := Labels.SearchByName(Name); 220 if Assigned(L) then begin 221 InstructionWriter.WriteData(L.Address); 222 end else begin 223 L := TLabel.Create; 224 L.Name := Name; 225 NewRef := TLabelRef.Create; 226 NewRef.Address := InstructionWriter.IP; 227 NewRef.BitWidth := InstructionWriter.DataSize; 228 L.Refs.Add(NewRef); 229 Labels.Add(L); 230 InstructionWriter.WriteData(0); 231 end; 232 end; 233 234 procedure TAssembler.UpdateLabelRef; 235 var 236 I: Integer; 237 R: Integer; 238 begin 239 for I := 0 to Labels.Count - 1 do 240 with TLabel(Labels[I]) do begin 241 for R := 0 to Refs.Count - 1 do 242 begin 243 InstructionWriter.IP := Refs[R].Address; 244 InstructionWriter.AddrSize := Refs[R].BitWidth; 245 if Refs[R].Relative then InstructionWriter.WriteAddressSigned(InstructionWriter.GetRelativeAddr( 246 InstructionWriter.AddrSize, InstructionWriter.IP - 1, Address)) 247 else InstructionWriter.WriteAddress(Address); 248 end; 249 end; 250 end; 251 252 procedure TAssembler.ParseInstruction; 253 var 254 Next: string; 255 LabelName: string; 256 NewLabel: TLabel; 257 OpcodeDef: TOpcodeDef; 258 begin 259 Next := Parser.ReadNext; 260 if Next = '' then Exit; 261 if (Length(Next) > 0) and (Next[Length(Next)] = ':') then begin 262 LabelName := Copy(Next, 1, Length(Next) - 1); 263 NewLabel := Labels.SearchByName(LabelName); 264 if not Assigned(NewLabel) then begin 265 NewLabel := TLabel.Create; 266 NewLabel.Name := LabelName; 267 Labels.Add(NewLabel); 268 end; 269 NewLabel.Address := InstructionWriter.IP; 270 Next := Parser.ReadNext; 271 end; 272 if Next = '' then Exit; 273 OpcodeDef := OpcodeDefs.SearchByName(Next); 274 if Assigned(OpcodeDef) then begin 275 if OpcodeDef.Prefix then InstructionWriter.Prefix := True; 276 if OpcodeDef.Opcode = opDataPrefix16 then InstructionWriter.DataSize := bw8 277 else if OpcodeDef.Opcode = opDataPrefix16 then InstructionWriter.DataSize := bw16 278 else if OpcodeDef.Opcode = opDataPrefix32 then InstructionWriter.DataSize := bw32 279 else if OpcodeDef.Opcode = opDataPrefix64 then InstructionWriter.DataSize := bw64 280 else if OpcodeDef.Opcode = opAddrPrefix8 then InstructionWriter.AddrSize := bw8 281 else if OpcodeDef.Opcode = opAddrPrefix16 then InstructionWriter.AddrSize := bw16 282 else if OpcodeDef.Opcode = opAddrPrefix32 then InstructionWriter.AddrSize := bw32 283 else if OpcodeDef.Opcode = opAddrPrefix64 then InstructionWriter.AddrSize := bw64; 284 InstructionIP := InstructionWriter.IP; 285 InstructionWriter.Write8(Byte(OpcodeDef.Opcode)); 286 ParseParam(OpcodeDef.Param1); 287 if OpcodeDef.Param2 <> prNone then begin 288 Parser.Expect(','); 289 ParseParam(OpcodeDef.Param2); 290 if OpcodeDef.Param3 <> prNone then begin 291 Parser.Expect(','); 292 ParseParam(OpcodeDef.Param3); 293 end; 294 end; 295 if not OpcodeDef.Prefix then begin 296 InstructionWriter.DataSize := InstructionWriter.DataSizeBase; 297 InstructionWriter.AddrSize := InstructionWriter.AddrSizeBase; 298 end; 299 end else 300 if Next = 'STRING' then begin 301 Next := Parser.ReadNext; 302 if (Length(Next) >= 2) and (Next[1] = '''') and (Next[Length(Next)] = '''') then 303 InstructionWriter.WriteString(Copy(Next, 2, Length(Next) - 2)); 304 end else 305 Error('Unknown instruction ' + Next); 127 306 end; 128 307 … … 130 309 var 131 310 I: Integer; 132 NewLabel: TLabel;133 311 Next: string; 134 OpcodeDef: TOpcodeDef;135 312 begin 136 313 InstructionWriter.Init; … … 138 315 for I := 0 to Source.Count - 1 do begin 139 316 Parser.Text := Source[I]; 140 Next := Parser.ReadNext; 141 if (Length(Next) > 0) and (Next[Length(Next)] = ':') then begin 142 NewLabel := TLabel.Create; 143 NewLabel.Name := Copy(Next, 1, Length(Next) - 1); 144 //NewLabel.Address := ; 145 Labels.Add(NewLabel); 317 ParseInstruction; 318 repeat 146 319 Next := Parser.ReadNext; 147 end; 148 OpcodeDef := OpcodeDefs.SearchByName(Next); 149 if Assigned(OpcodeDef) then begin 150 InstructionWriter.Write8(Byte(OpcodeDef.Opcode)); 151 ParseParam(OpcodeDef.Param1); 152 if OpcodeDef.Param2 <> prNone then begin 153 Parser.Expect(','); 154 ParseParam(OpcodeDef.Param2); 155 if OpcodeDef.Param3 <> prNone then begin 156 Parser.Expect(','); 157 ParseParam(OpcodeDef.Param3); 158 end; 159 end; 160 end else Error('Unknown instruction ' + Next); 161 end; 320 if (Next = '') or (Next <> ';') then Break; 321 ParseInstruction; 322 until False; 323 end; 324 UpdateLabelRef; 162 325 end; 163 326 -
branches/virtualcpu4/UDisassembler.pas
r184 r185 29 29 30 30 implementation 31 32 const33 SignText: array[TValueSign] of string = ('-', '', '+');34 35 function SignedIntToHex(Value: Int64; Digits: Byte): string;36 begin37 Result := SignText[Sign(Value)] + IntToHex(Abs(Value), Digits);38 end;39 31 40 32 { TDisassembler } … … 84 76 Data := ReadData; 85 77 Line.Opcode := Line.Opcode + ' ' + IntToHex(Data, BitWidthBytes[DataSize] * 2); 86 Line.Instruction := Line.Instruction + ' $' + IntToHex(Data, BitWidthBytes[DataSize] * 2);78 Line.Instruction := Line.Instruction + ' ' + IntToHexEx(Data, -1, '$'); 87 79 end; 88 80 prAddr: begin 89 81 Address := ReadAddress; 90 82 Line.Opcode := Line.Opcode + ' ' + IntToHex(Address, BitWidthBytes[AddrSize] * 2); 91 Line.Instruction := Line.Instruction + ' $' + IntToHex(Address, BitWidthBytes[AddrSize] * 2);83 Line.Instruction := Line.Instruction + ' ' + IntToHexEx(Address, -1, '$'); 92 84 end; 93 85 prAddrRel: begin 94 86 AddressRel := ReadAddressSigned; 95 Line.Opcode := Line.Opcode + ' ' + IntToHex (QWord(AddressRel), BitWidthBytes[AddrSize] * 2);96 Line.Instruction := Line.Instruction + ' $' + SignedIntToHex(AddressRel, BitWidthBytes[AddrSize] * 2);87 Line.Opcode := Line.Opcode + ' ' + IntToHexEx(QWord(AddressRel), BitWidthBytes[AddrSize] * 2); 88 Line.Instruction := Line.Instruction + ' ' + IntToHexEx(AddressRel, -1, '$'); 97 89 end; 98 90 end; … … 105 97 prData: begin 106 98 Data := ReadData; 107 Line.Opcode := Line.Opcode + ' ' + IntToHex (Data, BitWidthBytes[DataSize] * 2);108 Line.Instruction := Line.Instruction + ', $' + IntToHex(Data, BitWidthBytes[DataSize] * 2);99 Line.Opcode := Line.Opcode + ' ' + IntToHexEx(Data, BitWidthBytes[DataSize] * 2); 100 Line.Instruction := Line.Instruction + ', ' + IntToHexEx(Data, -1, '$'); 109 101 end; 110 102 prAddr: begin 111 103 Address := ReadAddress; 112 104 Line.Opcode := Line.Opcode + ' ' + IntToHex(Address, BitWidthBytes[AddrSize] * 2); 113 Line.Instruction := Line.Instruction + ', $' + IntToHex(Address, BitWidthBytes[AddrSize] * 2);105 Line.Instruction := Line.Instruction + ', ' + IntToHexEx(Address, -1, '$'); 114 106 end; 115 107 prAddrRel: begin 116 108 AddressRel := ReadAddressSigned; 117 Line.Opcode := Line.Opcode + ' ' + IntToHex( AddressRel, BitWidthBytes[AddrSize] * 2);118 Line.Instruction := Line.Instruction + ', $' + SignedIntToHex(AddressRel, BitWidthBytes[AddrSize] * 2);109 Line.Opcode := Line.Opcode + ' ' + IntToHex(QWord(AddressRel), BitWidthBytes[AddrSize] * 2); 110 Line.Instruction := Line.Instruction + ', ' + IntToHexEx(AddressRel, -1, '$'); 119 111 end; 120 112 end; … … 128 120 Data := ReadData; 129 121 Line.Opcode := Line.Opcode + ' ' + IntToHex(Data, BitWidthBytes[DataSize] * 2); 130 Line.Instruction := Line.Instruction + ', $' + IntToHex(Data, BitWidthBytes[DataSize] * 2);122 Line.Instruction := Line.Instruction + ', ' + IntToHexEx(Data, -1, '$'); 131 123 end; 132 124 prAddr: begin 133 125 Address := ReadAddress; 134 126 Line.Opcode := Line.Opcode + ' ' + IntToHex(Address, BitWidthBytes[AddrSize] * 2); 135 Line.Instruction := Line.Instruction + ', $' + IntToHex(Address, BitWidthBytes[AddrSize] * 2);127 Line.Instruction := Line.Instruction + ', ' + IntToHexEx(Address, -1, '$'); 136 128 end; 137 129 prAddrRel: begin 138 130 AddressRel := ReadAddressSigned; 139 131 Line.Opcode := Line.Opcode + ' ' + IntToHex(QWord(AddressRel), BitWidthBytes[AddrSize] * 2); 140 Line.Instruction := Line.Instruction + ', $' + SignedIntToHex(AddressRel, BitWidthBytes[AddrSize] * 2);132 Line.Instruction := Line.Instruction + ', ' + IntToHexEx(AddressRel, -1, '$'); 141 133 end; 142 134 end; 143 135 Output.Add(Line); 136 if not Prefix then begin 137 DataSize := DataSizeBase; 138 AddrSize := AddrSizeBase; 139 end; 144 140 end else begin 145 141 {Line := TDisassemblerLine.Create; … … 149 145 Output.Add(Line); 150 146 } 151 end;152 if not Prefix then begin153 DataSize := DataSizeBase;154 AddrSize := AddrSizeBase;155 147 end; 156 148 end; -
branches/virtualcpu4/UInstructionWriter.pas
r184 r185 25 25 Prefix: Boolean; 26 26 procedure Init; 27 function GetRelativeAddr(BitWidth: TBitWidth; BaseIP, TargetAddr: QWord): Int64; 27 28 procedure Write8(Value: Byte); 28 29 procedure Write16(Value: Word); … … 132 133 end; 133 134 135 function TInstructionWriter.GetRelativeAddr(BitWidth: TBitWidth; BaseIP, TargetAddr: QWord): Int64; 136 begin 137 Result := Int64(TargetAddr) - (BaseIP + 1 + BitWidthBytes[BitWidth]); 138 end; 139 134 140 procedure TInstructionWriter.JumpRel(Addr: QWord); 135 141 var 136 NextIP: QWord;137 begin 138 PrefixBegin; 139 NextIP := IP + 1 + BitWidthBytes[Cpu.AddrSize];142 RelAddr: Int64; 143 begin 144 PrefixBegin; 145 RelAddr := GetRelativeAddr(AddrSize, IP, Addr); 140 146 Write8(Byte(opJumpRel)); 141 WriteAddressSigned( Int64(Addr) - Int64(NextIP));147 WriteAddressSigned(RelAddr); 142 148 PrefixEnd; 143 149 end; … … 145 151 procedure TInstructionWriter.JumpRelNotZero(Addr: QWord); 146 152 var 147 NextIP: QWord;148 begin 149 PrefixBegin; 150 NextIP := IP + 1 + BitWidthBytes[Cpu.AddrSize];153 RelAddr: Int64; 154 begin 155 PrefixBegin; 156 RelAddr := GetRelativeAddr(AddrSize, IP, Addr); 151 157 Write8(Byte(opJumpRelNotZero)); 152 WriteAddressSigned( Int64(Addr) - Int64(NextIP));158 WriteAddressSigned(RelAddr); 153 159 PrefixEnd; 154 160 end; … … 156 162 procedure TInstructionWriter.JumpRelZero(Addr: QWord); 157 163 var 158 NextIP: QWord;159 begin 160 PrefixBegin; 161 NextIP := IP + 1 + BitWidthBytes[Cpu.AddrSize];164 RelAddr: Int64; 165 begin 166 PrefixBegin; 167 RelAddr := GetRelativeAddr(AddrSize, IP, Addr); 162 168 Write8(Byte(opJumpRelZero)); 163 WriteAddressSigned( Int64(Addr) - Int64(NextIP));169 WriteAddressSigned(RelAddr); 164 170 PrefixEnd; 165 171 end; … … 360 366 begin 361 367 case AddrSize of 362 bw8: Write8(Value );363 bw16: Write16(Value );364 bw32: Write32(Value );368 bw8: Write8(Value and $ff); 369 bw16: Write16(Value and $ffff); 370 bw32: Write32(Value and $ffffffff); 365 371 bw64: Write64(Value); 366 372 end; … … 370 376 begin 371 377 case AddrSize of 372 bw8: Write8(Byte(Value ));373 bw16: Write16(Word(Value ));374 bw32: Write32(DWord(Value ));378 bw8: Write8(Byte(Value and $ff)); 379 bw16: Write16(Word(Value and $ffff)); 380 bw32: Write32(DWord(Value and $ffffffff)); 375 381 bw64: Write64(QWord(Value)); 376 382 end; … … 380 386 begin 381 387 case DataSize of 382 bw8: Write8(Value );383 bw16: Write16(Value );384 bw32: Write32(Value );388 bw8: Write8(Value and $ff); 389 bw16: Write16(Value and $ffff); 390 bw32: Write32(Value and $ffffffff); 385 391 bw64: Write64(Value); 386 392 end; -
branches/virtualcpu4/UMachine.pas
r182 r185 34 34 LockInput: TCriticalSection; 35 35 LockOutput: TCriticalSection; 36 pro perty MemorySize: Integer read GetMemorySize write SetMemorySize;36 procedure ClearMemory; 37 37 constructor Create; 38 38 destructor Destroy; override; 39 property MemorySize: Integer read GetMemorySize write SetMemorySize; 39 40 end; 40 41 … … 159 160 end; 160 161 162 procedure TMachine.ClearMemory; 163 begin 164 FillChar(Memory^, MemorySize, $ff); 165 end; 166 161 167 162 168 end. -
branches/virtualcpu4/UOpcode.pas
r184 r185 29 29 end; 30 30 31 function IntToHexEx(Value: Int64; Digits: ShortInt; Prefix: string = ''): string; overload; 32 function IntToHexEx(Value: QWord; Digits: ShortInt; Prefix: string = ''): string; overload; 33 31 34 32 35 implementation 36 37 const 38 HexChars: array[0..15] of Char = '0123456789ABCDEF'; 39 40 function IntToHexEx(Value: Int64; Digits: ShortInt; Prefix: string = ''): string; 41 var 42 I: Integer; 43 Negative: Boolean; 44 begin 45 Negative := Value < 0; 46 if Negative then Value := -Value; 47 Result := ''; 48 if Digits >= 0 then begin 49 for I := 0 to Digits - 1 do begin 50 Result := HexChars[Value and $f] + Result; 51 Value := Value shr 4; 52 end; 53 end else begin 54 if Value <> 0 then begin 55 while QWord(Value) > 0 do begin 56 Result := HexChars[Value and $f] + Result; 57 Value := Value shr 4; 58 end; 59 end else Result := '0'; 60 end; 61 Result := Prefix + Result; 62 if Negative then Result := '-' + Result; 63 end; 64 65 function IntToHexEx(Value: QWord; Digits: ShortInt; Prefix: string = ''): string; 66 var 67 I: Integer; 68 begin 69 Result := ''; 70 if Digits >= 0 then begin 71 for I := 0 to Digits - 1 do begin 72 Result := HexChars[Value and $f] + Result; 73 Value := Value shr 4; 74 end; 75 end else begin 76 if Value <> 0 then begin 77 while Value > 0 do begin 78 Result := HexChars[Value and $f] + Result; 79 Value := Value shr 4; 80 end; 81 end else Result := '0'; 82 end; 83 Result := Prefix + Result; 84 end; 33 85 34 86 { TOpcodeDefs } … … 100 152 AddNew(opShr, 'SHR', prReg, prReg, prNone, False); 101 153 AddNew(opDataPrefix8, 'DP8', prNone, prNone, prNone, True); 102 AddNew(opDataPrefix16, 'DP16', prNone, prNone, prNone, False);154 AddNew(opDataPrefix16, 'DP16', prNone, prNone, prNone, True); 103 155 AddNew(opDataPrefix32, 'DP32', prNone, prNone, prNone, True); 104 156 AddNew(opDataPrefix64, 'DP64', prNone, prNone, prNone, True); … … 117 169 AddNew(opAddrPrefix16, 'AP16', prNone, prNone, prNone, True); 118 170 AddNew(opAddrPrefix32, 'AP32', prNone, prNone, prNone, True); 119 AddNew(op DataPrefix64, 'AP64', prNone, prNone, prNone, True);171 AddNew(opAddrPrefix64, 'AP64', prNone, prNone, prNone, True); 120 172 AddNew(opConvert, 'CON', prReg, prNone, prNone, True); 121 173 end; -
branches/virtualcpu4/virtucpu4.lpi
r184 r185 154 154 <IsPartOfProject Value="True"/> 155 155 <ComponentName Value="FormAssembler"/> 156 <HasResources Value="True"/> 156 157 <ResourceBaseClass Value="Form"/> 157 158 </Unit15>
Note:
See TracChangeset
for help on using the changeset viewer.