Changeset 55
- Timestamp:
- Nov 22, 2023, 10:09:35 PM (13 months ago)
- Location:
- branches/ByteArray
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ByteArray/Assembler.pas
r46 r55 245 245 end else Error('Expected register name starting with R character.', Token.Pos); 246 246 Parser.Expect(tkSpecialSymbol, ')'); 247 end else 248 if InstructionInfo.Params[I] = ptRegIndirectGroup then begin 249 Parser.Expect(tkSpecialSymbol, '('); 250 Token := Parser.ReadNext; 251 if (Token.Value <> '') and (Token.Value[1] = 'R') then begin 252 Token.Value := Copy(Token.Value, 2, MaxInt); 253 if TryStrToInt(Token.Value, Number) then begin 254 Memory.WritePos(1, Number); 255 Parser.Expect(tkSpecialSymbol, ':'); 256 Token := Parser.ReadNext; 257 if (Token.Value <> '') and (Token.Value[1] = 'R') then begin 258 Token.Value := Copy(Token.Value, 2, MaxInt); 259 if TryStrToInt(Token.Value, Number) then begin 260 Memory.WritePos(1, Number); 261 end else Error('Expected numeric register index error', Token.Pos); 262 end else Error('Expected register name starting with R character.', Token.Pos); 263 end else Error('Expected numeric register index error', Token.Pos); 264 end else Error('Expected register name starting with R character.', Token.Pos); 265 Parser.Expect(tkSpecialSymbol, ')'); 266 end else 247 end else Error('Unsupported parameter type', Token.Pos); 267 248 end; 268 249 end; -
branches/ByteArray/BigInt.pas
r52 r55 36 36 class operator Implicit(A: TBigInt): Int64; 37 37 class operator Implicit(A: TBigInt): PByte; 38 class operator Negative(A: TBigInt): TBigInt; 38 39 class operator Inc(A: TBigInt) : TBigInt; 39 40 class operator Dec(A: TBigInt) : TBigInt; … … 46 47 class operator GreaterThan(A, B: TBigInt): Boolean; 47 48 class operator GreaterThanOrEqual(A, B: TBigInt): Boolean; 49 class operator BitwiseXor(A, B: TBigInt): TBigInt; 50 class operator BitwiseAnd(A, B: TBigInt): TBigInt; 51 class operator BitwiseOr(A, B: TBigInt): TBigInt; 52 class operator LeftShift(A: TBigInt; B: Integer): TBigInt; 53 class operator RightShift(A: TBigInt; B: Integer): TBigInt; 54 class operator Modulus(A, B: TBigInt): TBigInt; 55 class operator IntDivide(A, B: TBigInt): TBigInt; 48 56 procedure Shrink; 49 57 function IsNegative: Boolean; 50 58 function IsZero: Boolean; 51 59 function IsPositive: Boolean; 60 procedure Invert; 52 61 procedure SetByteArray(AData: TArrayOfByte; ASize: TBigIntSize); 53 62 procedure GetByteArray(var AData: TArrayOfByte; ASize: TBigIntSize); 54 function Copy(Size: TBigIntSize): TBigInt; 63 function Copy(Size: TBigIntSize): TBigInt; overload; 64 function Copy: TBigInt; overload; 55 65 property Size: TBigIntSize read GetSize write SetSize; 56 66 property Data[Index: TBigIntSize]: Byte read GetData write SetData; … … 58 68 59 69 function TryStrToBigInt(const S: string; out I: TBigInt): Boolean; 70 function IntToStr(Value: TBigInt): string; overload; 71 function IntToHex(Value: TBigInt): string; overload; 72 function IntToHex(Value: TBigInt; Digits: Integer): string; overload; 60 73 61 74 … … 64 77 resourcestring 65 78 SUnsupportedByteSize = 'Unsupported byte size'; 79 SOutOfRange = 'Out of range'; 80 81 const 82 HexDigits: array[0..15] of Char = '0123456789ABCDEF'; 66 83 67 84 function TryStrToBigInt(const S: string; out I: TBigInt): Boolean; … … 73 90 end; 74 91 92 function IntToHex(Value: TBigInt): string; 93 begin 94 Result := ''; 95 while Value <> 0 do begin 96 Result := HexDigits[Value and 15] + Result; 97 Value := Value shr 4; 98 end; 99 if Result = '' then Result := '0'; 100 end; 101 102 function IntToHex(Value: TBigInt; Digits: Integer): string; 103 var 104 I: Integer; 105 begin 106 if Digits = 0 then 107 Digits := 1; 108 Result := ''; 109 SetLength(Result, Digits); 110 for I := 0 to Digits - 1 do 111 begin 112 Result[Digits - I] := HexDigits[Value and 15]; 113 Value := Value shr 4; 114 end ; 115 while Value <> 0 do begin 116 Result := HexDigits[Value and 15] + Result; 117 Value := Value shr 4; 118 end; 119 end; 120 121 function IntToStr(Value: TBigInt): string; 122 begin 123 Result := ''; 124 if Value < 0 then begin 125 Value := -Value; 126 while Value > 9 do begin 127 Result := Chr(Ord('0') + (Value mod 10)) + Result; 128 Value := Value div 10; 129 end; 130 Result := '-' + Chr(Ord('0') + (Value mod 10)) + Result; 131 end else begin 132 while Value > 9 do begin 133 Result := Chr(Ord('0') + (Value mod 10)) + Result; 134 Value := Value div 10; 135 end; 136 Result := Chr(Ord('0') + Value) + Result; 137 end; 138 end; 139 75 140 { TBigInt } 76 141 … … 88 153 procedure TBigInt.SetData(Index: TBigIntSize; AValue: Byte); 89 154 begin 90 if Index > Integer(Size) - 1 then Size := Index + 1;155 if Index >= Integer(Size) then raise Exception.Create(SOutOfRange); 91 156 FData[Index] := AValue; 92 157 end; … … 209 274 end; 210 275 276 class operator TBigInt.Negative(A: TBigInt): TBigInt; 277 begin 278 Result := A.Copy(A.Size); 279 Result.Invert; 280 Result := Result + 1; 281 end; 282 211 283 class operator TBigInt.Inc(A: TBigInt): TBigInt; 212 284 begin … … 224 296 V: SmallInt; 225 297 Carry: Byte; 226 begin 227 Result.Size := Max(A.Size, B.Size); 298 Size: Byte; 299 begin 300 Size := Max(A.Size, B.Size); 301 Result.Size := Size; 228 302 Carry := 0; 229 303 I := 0; 230 while (I < Result.Size) or (Carry > 0) do begin 304 while (I < Size) or (Carry > 0) do begin 305 if I >= Result.Size then Result.Size := I + 1; 231 306 V := A.Data[I] + B.Data[I] + Carry; 232 307 if V >= 256 then begin … … 240 315 class operator TBigInt.Subtract(A, B: TBigInt): TBigInt; 241 316 var 242 I: Integer; 243 V: SmallInt; 244 Carry: Byte; 245 begin 246 Result.Size := Max(A.Size, B.Size); 247 Carry := 0; 248 I := 0; 249 while (I < Result.Size) or (Carry > 0) do begin 250 V := A.Data[I] - B.Data[I] - Carry; 251 if V < 0 then begin 252 Carry := 1; 253 end else Carry := 0; 254 Result.Data[I] := V and $ff; 255 Inc(I); 256 end; 317 Size: TBigIntSize; 318 begin 319 Size := Max(A.Size, B.Size); 320 Result := A + -B; 321 Result.Size := Size; 322 Result.Shrink; 257 323 end; 258 324 … … 316 382 end; 317 383 384 class operator TBigInt.BitwiseXor(A, B: TBigInt): TBigInt; 385 var 386 I: TBigIntSize; 387 begin 388 Result.Size := Max(A.Size, B.Size); 389 for I := 0 to Result.Size - 1 do 390 Result.Data[I] := A.Data[I] xor B.Data[I]; 391 Result.Shrink; 392 end; 393 394 class operator TBigInt.BitwiseAnd(A, B: TBigInt): TBigInt; 395 var 396 I: TBigIntSize; 397 begin 398 Result.Size := Max(A.Size, B.Size); 399 for I := 0 to Result.Size - 1 do 400 Result.Data[I] := A.Data[I] and B.Data[I]; 401 Result.Shrink; 402 end; 403 404 class operator TBigInt.BitwiseOr(A, B: TBigInt): TBigInt; 405 var 406 I: TBigIntSize; 407 begin 408 Result.Size := Max(A.Size, B.Size); 409 for I := 0 to Result.Size - 1 do 410 Result.Data[I] := A.Data[I] or B.Data[I]; 411 Result.Shrink; 412 end; 413 414 class operator TBigInt.LeftShift(A: TBigInt; B: Integer): TBigInt; 415 var 416 ByteShift: TBigIntSize; 417 BitShift: Byte; 418 I: Integer; 419 begin 420 if (B and 7) = 0 then begin 421 // Full byte shift 422 ByteShift := B shr 3; 423 Result.Size := A.Size + ByteShift; 424 Move(A.FData[0], Result.FData[ByteShift], A.Size); 425 FillChar(Result.FData[0], ByteShift, 0); 426 end else begin 427 // Partial byte shift 428 ByteShift := B shr 3; 429 BitShift := B - ByteShift; 430 Result.Size := Result.Size + ByteShift + 1; 431 for I := 0 to A.Size - 1 do begin 432 Result.FData[I + ByteShift] := (A.FData[I] shl BitShift) and $ff; 433 if I > 0 then Result.FData[I + ByteShift] := Result.FData[I + ByteShift] or (A.FData[I] shl BitShift shl 8); 434 end; 435 end; 436 Result.Shrink; 437 end; 438 439 class operator TBigInt.RightShift(A: TBigInt; B: Integer): TBigInt; 440 var 441 ByteShift: TBigIntSize; 442 BitShift: Byte; 443 I: Integer; 444 begin 445 if (B and 7) = 0 then begin 446 // Full byte shift 447 ByteShift := B shr 3; 448 Result.Size := A.Size - ByteShift; 449 Move(A.FData[ByteShift], Result.FData[0], A.Size); 450 end else begin 451 // Partial byte shift 452 ByteShift := B shr 3; 453 BitShift := B - ByteShift; 454 Result.Size := Result.Size - ByteShift; 455 for I := 0 to A.Size - 1 do begin 456 Result.FData[I] := (A.FData[I + ByteShift] shr BitShift) and $ff; 457 if I < (A.Size - 1) then Result.FData[I] := Result.FData[I] or (((A.FData[I + ByteShift] shl 8) shr BitShift) and $ff); 458 end; 459 end; 460 Result.Shrink; 461 end; 462 463 class operator TBigInt.Modulus(A, B: TBigInt): TBigInt; 464 begin 465 466 end; 467 468 class operator TBigInt.IntDivide(A, B: TBigInt): TBigInt; 469 begin 470 471 end; 472 318 473 procedure TBigInt.Shrink; 319 474 var … … 346 501 begin 347 502 Result := (FData[Size - 1] and $80) = 0; 503 end; 504 505 procedure TBigInt.Invert; 506 var 507 I: Byte; 508 begin 509 for I := 0 to Size - 1 do 510 FData[I] := FData[I] xor $ff; 348 511 end; 349 512 … … 372 535 end; 373 536 537 function TBigInt.Copy: TBigInt; 538 begin 539 Result := Copy(Size); 540 end; 541 374 542 end. 375 543 -
branches/ByteArray/Cpu.pas
r52 r55 269 269 RegIndex := ReadRegIndex; 270 270 Address := Read(AddressWidth); 271 if Byte(Regs[RegIndex])<> 0 then271 if Regs[RegIndex] <> 0 then 272 272 PC := Address; 273 273 end; … … 284 284 RegIndex := ReadRegIndex; 285 285 Address := Read(AddressSize); 286 if Int64(Regs[RegIndex].Copy(DataSize)) <> 0 then286 if Regs[RegIndex].Copy(DataSize) <> 0 then 287 287 PC := Address; 288 288 end; … … 295 295 RegIndex := ReadRegIndex; 296 296 Address := Read(AddressWidth); 297 if Byte(Regs[RegIndex])= 0 then297 if Regs[RegIndex] = 0 then 298 298 PC := Address; 299 299 end; … … 310 310 RegIndex := ReadRegIndex; 311 311 Address := Read(AddressSize); 312 if Int64(Regs[RegIndex].Copy(DataSize)) = 0 then312 if Regs[RegIndex].Copy(DataSize) = 0 then 313 313 PC := Address; 314 314 end; -
branches/ByteArray/Devices/Memory.pas
r51 r55 12 12 TMemory = class(TDevice) 13 13 private 14 FSize: Integer; 14 15 FData: PByte; 15 16 function GetSize: Integer; … … 27 28 procedure SetChannel(Channel: TChannel); override; 28 29 procedure Clean; 29 property Size: Integer read GetSize write SetSize;30 property Size: Integer read FSize write SetSize; 30 31 destructor Destroy; override; 31 32 end; … … 35 36 36 37 resourcestring 37 SOutOfRange = ' Out of range';38 SOutOfRange = 'Address out of range.'; 38 39 39 40 { TMemory } … … 46 47 procedure TMemory.SetSize(AValue: Integer); 47 48 begin 49 FSize := AValue; 48 50 FData := ReAllocMem(FData, AValue); 49 51 end; … … 52 54 begin 53 55 Size := Source.Size; 54 if Size > 0 then55 Move(Source.FData[0], FData[0], Size);56 if FSize > 0 then 57 Move(Source.FData[0], FData[0], FSize); 56 58 Position := Source.Position; 57 59 end; … … 59 61 function TMemory.Read(Address: TBigInt; ASize: TBigIntSize): TBigInt; 60 62 begin 61 if Address + ASize > =Size then raise Exception.Create(SOutOfRange);63 if Address + ASize > FSize then raise Exception.Create(SOutOfRange); 62 64 case ASize of 63 65 1: Result := PByte(FData + Integer(Address))^; … … 76 78 procedure TMemory.Write(Address: TBigInt; ASize: TBigIntSize; Value: TBigInt); 77 79 begin 78 if Address + ASize > =Size then raise Exception.Create(SOutOfRange);80 if Address + ASize > FSize then raise Exception.Create(SOutOfRange); 79 81 case ASize of 80 82 1: PByte(FData + Integer(Address))^ := Value; … … 96 98 begin 97 99 if Length(Value) > 0 then begin 98 if Position + Length(Value) > Size then Size := Position + Length(Value);100 if Position + Length(Value) > FSize then Size := Position + Length(Value); 99 101 for I := 0 to Length(Value) - 1 do 100 102 Write(Position + I, 1, Ord(Value[I + 1])); … … 106 108 begin 107 109 if Memory.Size > 0 then begin 108 if Position + Memory.Size > Size then Size := Position + Memory.Size;110 if Position + Memory.Size > FSize then Size := Position + Memory.Size; 109 111 Move(Memory.FData[0], FData[Position], Memory.Size); 110 112 Inc(Position, Memory.Size); … … 114 116 function TMemory.GetAddressCount: Integer; 115 117 begin 116 Result := Size;118 Result := FSize; 117 119 end; 118 120 … … 125 127 procedure TMemory.Clean; 126 128 begin 127 FillChar(FData^, Size, 0);129 FillChar(FData^, FSize, 0); 128 130 end; 129 131 -
branches/ByteArray/Disassembler.pas
r52 r55 11 11 12 12 TDisassembler = class 13 private 14 function RegToStr(RegIndex: Byte): string; 15 public 13 16 InstructionSet: TInstructionSet; 14 17 Memory: TMemory; … … 25 28 BigInt; 26 29 27 function IntToHex(Value: TBigInt; Digits: Integer): string; overload;30 function TDisassembler.RegToStr(RegIndex: Byte): string; 28 31 begin 29 Result := IntToHex(UInt64(Value), Digits); 30 end; 31 32 function IntToStr(Value: TBigInt): string; overload; 33 begin 34 Result := IntToStr(UInt64(Value)); 32 Result := Chr(Ord('A') + RegIndex); 35 33 end; 36 34 … … 64 62 InstText := InstText + ' '; 65 63 if InstructionInfo.Params[J] = ptNumber then begin 66 InstText := InstText + IntToHex(Value , 8);64 InstText := InstText + IntToHex(Value) + 'h'; 67 65 end else 68 66 if InstructionInfo.Params[J] = ptReg then begin 69 InstText := InstText + 'R' + IntToStr(Value);67 InstText := InstText + RegToStr(Value); 70 68 end else 71 69 if InstructionInfo.Params[J] = ptRegIndirect then begin 72 InstText := InstText + '( R' + IntToStr(Value) + ')';70 InstText := InstText + '(' + RegToStr(Value) + ')'; 73 71 end else 74 72 if InstructionInfo.Params[J] = ptRegIndirectIndex then begin 75 InstText := InstText + '( R' + IntToStr(Value);73 InstText := InstText + '(' + RegToStr(Value); 76 74 Value := Memory.ReadPos(1); 77 75 InstBytes := InstBytes + IntToHex(Value, 2) + ' '; 78 76 InstText := InstText + ' + ' + IntToStr(Value) + ')'; 79 end else80 if InstructionInfo.Params[J] = ptRegIndirectGroup then begin81 InstText := InstText + '(R' + IntToStr(Value);82 Value := Memory.ReadPos(1);83 InstBytes := InstBytes + IntToHex(Value, 2) + ' ';84 InstText := InstText + ': R' + IntToStr(Value) + ')';85 77 end; 86 78 end; -
branches/ByteArray/Instructions.pas
r46 r55 7 7 8 8 type 9 TParamType = (ptNone, ptNumber, ptReg, ptRegIndirect, ptRegIndirectIndex, 10 ptRegIndirectGroup); 9 TParamType = (ptNone, ptNumber, ptReg, ptRegIndirect, ptRegIndirectIndex); 11 10 TParamTypeArray = array of TParamType; 12 11 … … 80 79 AddNew(inAdd, 'ADD', [ptReg, ptReg], 'Adds second register to first register.'); 81 80 AddNew(inSub, 'SUB', [ptReg, ptReg], 'Subtracts second register from first register.'); 82 AddNew(inInput, 'IN', [ptReg, ptRegIndirect Group], 'Reads value from input port to register.');83 AddNew(inOutput, 'OUT', [ptRegIndirect Group, ptReg], 'Writes value from register to output port.');81 AddNew(inInput, 'IN', [ptReg, ptRegIndirect], 'Reads value from input port to register.'); 82 AddNew(inOutput, 'OUT', [ptRegIndirect, ptReg], 'Writes value from register to output port.'); 84 83 AddNew(inJumpZero, 'JZ', [ptReg, ptNumber], 'Jumps to given address if value of register is zero'); 85 84 AddNew(inJumpNotZero, 'JNZ', [ptReg, ptNumber], 'Jumps to given address if value of register is not zero'); -
branches/ByteArray/Languages/ByteArray.cs.po
r51 r55 12 12 "X-Generator: Poedit 3.0.1\n" 13 13 14 #: bigint.soutofrange 15 msgctxt "bigint.soutofrange" 16 msgid "Out of range" 17 msgstr "Mimo rozsah" 18 14 19 #: bigint.sunsupportedbytesize 15 20 msgid "Unsupported byte size" … … 48 53 49 54 #: memory.soutofrange 50 msgid "Out of range" 51 msgstr "Mimo rozsah" 55 msgctxt "memory.soutofrange" 56 msgid "Address out of range." 57 msgstr "Adresa mimo rozsah." 52 58 53 59 #: parser.sunknowncharacter -
branches/ByteArray/Languages/ByteArray.pot
r51 r55 1 1 msgid "" 2 2 msgstr "Content-Type: text/plain; charset=UTF-8" 3 4 #: bigint.soutofrange 5 msgctxt "bigint.soutofrange" 6 msgid "Out of range" 7 msgstr "" 3 8 4 9 #: bigint.sunsupportedbytesize … … 38 43 39 44 #: memory.soutofrange 40 msgid "Out of range" 45 msgctxt "memory.soutofrange" 46 msgid "Address out of range." 41 47 msgstr "" 42 48
Note:
See TracChangeset
for help on using the changeset viewer.