Changeset 47


Ignore:
Timestamp:
Oct 25, 2023, 12:33:07 AM (13 months ago)
Author:
chronos
Message:
  • Added: Common package.
  • Modified: Improved BigInt class.
Location:
branches/ByteArray
Files:
78 added
11 edited

Legend:

Unmodified
Added
Removed
  • branches/ByteArray/BigInt.pas

    r46 r47  
    99  TArrayOfByte = array of Byte;
    1010
     11  TBigIntSize = Byte;
     12
    1113  { TBigInt }
    1214
    1315  TBigInt = record
    1416  private
    15     function GetSize: Byte;
    16     procedure SetSize(AValue: Byte);
     17    FData: array of Byte;
     18    function GetData(Index: TBigIntSize): Byte;
     19    function GetSize: TBigIntSize;
     20    procedure SetData(Index: TBigIntSize; AValue: Byte);
     21    procedure SetSize(AValue: TBigIntSize);
    1722  public
    18     Data: array of Byte;
    1923    class operator Implicit(A: Byte): TBigInt;
    2024    class operator Implicit(A: Word): TBigInt;
     
    3337    class operator Implicit(A: TBigInt): PByte;
    3438    class operator Inc(A: TBigInt) : TBigInt;
     39    class operator Dec(A: TBigInt) : TBigInt;
    3540    class operator Add(A, B: TBigInt): TBigInt;
    3641    class operator Subtract(A, B: TBigInt): TBigInt;
     
    4247    class operator GreaterThanOrEqual(A, B: TBigInt): Boolean;
    4348    procedure Shrink;
    44     procedure SetByteArray(AData: TArrayOfByte; ASize: Byte);
    45     procedure GetByteArray(var AData: TArrayOfByte; ASize: Byte);
    46     function Copy(Size: Byte): TBigInt;
     49    procedure SetByteArray(AData: TArrayOfByte; ASize: TBigIntSize);
     50    procedure GetByteArray(var AData: TArrayOfByte; ASize: TBigIntSize);
     51    function Copy(Size: TBigIntSize): TBigInt;
    4752    property Size: Byte read GetSize write SetSize;
    48   end;
    49 
    50 function TryStrToBigInt(const S: string; Out I: TBigInt): Boolean;
     53    property Data[Index: TBigIntSize]: Byte read GetData write SetData;
     54  end;
     55
     56function TryStrToBigInt(const S: string; out I: TBigInt): Boolean;
     57
    5158
    5259implementation
    5360
    54 function TryStrToBigInt(const S: string; Out I: TBigInt): Boolean;
     61resourcestring
     62  SUnsupportedByteSize = 'Unsupported byte size';
     63
     64function TryStrToBigInt(const S: string; out I: TBigInt): Boolean;
    5565var
    5666  Value: Int64;
     
    6272{ TBigInt }
    6373
    64 function TBigInt.GetSize: Byte;
    65 begin
    66   Result := Length(Data);
    67 end;
    68 
    69 procedure TBigInt.SetSize(AValue: Byte);
    70 var
    71   LastSize: Byte;
     74function TBigInt.GetSize: TBigIntSize;
     75begin
     76  Result := Length(FData);
     77end;
     78
     79function TBigInt.GetData(Index: TBigIntSize): Byte;
     80begin
     81  if Index < Size then Result := FData[Index]
     82    else Result := 0;
     83end;
     84
     85procedure TBigInt.SetData(Index: TBigIntSize; AValue: Byte);
     86begin
     87  if Index > Size - 1 then Size := Index + 1;
     88  FData[Index] := AValue;
     89end;
     90
     91procedure TBigInt.SetSize(AValue: TBigIntSize);
     92var
     93  LastSize: TBigIntSize;
    7294begin
    7395  LastSize := GetSize;
    74   SetLength(Data, AValue);
     96  SetLength(FData, AValue);
    7597  if AValue > LastSize then
    76     FillChar(Data[LastSize], AValue - LastSize, 0);
     98    FillChar(FData[LastSize], AValue - LastSize, 0);
    7799end;
    78100
     
    80102begin
    81103  Result.Size := 1;
    82   Result.Data[0] := A;
     104  Result.FData[0] := A;
    83105end;
    84106
     
    86108begin
    87109  Result.Size := 2;
    88   PWord(@Result.Data[0])^ := A;
     110  PWord(@Result.FData[0])^ := A;
    89111  Result.Shrink;
    90112end;
     
    93115begin
    94116  Result.Size := 1;
    95   PByte(@Result.Data[0])^ := A;
     117  PByte(@Result.FData[0])^ := A;
    96118  Result.Shrink;
    97119end;
     
    100122begin
    101123  Result.Size := 4;
    102   PInteger(@Result.Data[0])^ := A;
     124  PInteger(@Result.FData[0])^ := A;
    103125  Result.Shrink;
    104126end;
     
    107129begin
    108130  Result.Size := 8;
    109   PQWord(@Result.Data[0])^ := A;
     131  PQWord(@Result.FData[0])^ := A;
    110132  Result.Shrink;
    111133end;
     
    114136begin
    115137  Result.Size := 8;
    116   PInt64(@Result.Data[0])^ := A;
     138  PInt64(@Result.FData[0])^ := A;
    117139  Result.Shrink;
    118140end;
     
    120142class operator TBigInt.Implicit(A: TBigInt): UInt8;
    121143begin
    122   if A.Size = 1 then Result := A.Data[0]
    123     else raise Exception.Create('x');
     144  if A.Size = 1 then Result := A.FData[0]
     145    else raise Exception.Create(SUnsupportedByteSize);
    124146end;
    125147
    126148class operator TBigInt.Implicit(A: TBigInt): Int8;
    127149begin
    128   if A.Size = 1 then Result := A.Data[0]
    129     else raise Exception.Create('x');
     150  if A.Size = 1 then Result := A.FData[0]
     151    else raise Exception.Create(SUnsupportedByteSize);
    130152end;
    131153
    132154class operator TBigInt.Implicit(A: TBigInt): UInt16;
    133155begin
    134   if A.Size = 2 then Result := PUInt16(@A.Data[0])^
    135     else raise Exception.Create('x');
     156  if A.Size = 2 then Result := PUInt16(@A.FData[0])^
     157    else raise Exception.Create(SUnsupportedByteSize);
    136158end;
    137159
    138160class operator TBigInt.Implicit(A: TBigInt): Int16;
    139161begin
    140   if A.Size = 2 then Result := PInt16(@A.Data[0])^
    141     else raise Exception.Create('x');
     162  if A.Size = 2 then Result := PInt16(@A.FData[0])^
     163    else raise Exception.Create(SUnsupportedByteSize);
    142164end;
    143165
     
    145167begin
    146168  case A.Size of
    147     1: Result := PByte(@A.Data[0])^;
    148     2: Result := PWord(@A.Data[0])^;
    149     4: Result := PDWord(@A.Data[0])^;
    150     8: Result := PQWord(@A.Data[0])^;
    151     else raise Exception.Create('x');
     169    1: Result := PByte(@A.FData[0])^;
     170    2: Result := PWord(@A.FData[0])^;
     171    3: Result := PWord(@A.FData[0])^ or (PByte(@A.FData[2])^ shl 16);
     172    4: Result := PDWord(@A.FData[0])^;
     173    8: Result := PQWord(@A.FData[0])^;
     174    else raise Exception.Create(SUnsupportedByteSize);
    152175  end;
    153176end;
     
    155178class operator TBigInt.Implicit(A: TBigInt): UInt32;
    156179begin
    157   if A.Size = 4 then Result := PInt32(@A.Data[0])^
    158     else raise Exception.Create('x');
     180  if A.Size = 4 then Result := PInt32(@A.FData[0])^
     181    else raise Exception.Create(SUnsupportedByteSize);
    159182end;
    160183
    161184class operator TBigInt.Implicit(A: TBigInt): UInt64;
    162185begin
    163   if A.Size = 8 then Result := PUInt64(@A.Data[0])^
    164     else raise Exception.Create('x');
     186  if A.Size = 8 then Result := PUInt64(@A.FData[0])^
     187    else raise Exception.Create(SUnsupportedByteSize);
    165188end;
    166189
    167190class operator TBigInt.Implicit(A: TBigInt): Int64;
    168191begin
    169   if A.Size = 8 then Result := PInt64(@A.Data[0])^
    170     else raise Exception.Create('x');
     192  if A.Size = 8 then Result := PInt64(@A.FData[0])^
     193    else raise Exception.Create(SUnsupportedByteSize);
    171194end;
    172195
    173196class operator TBigInt.Implicit(A: TBigInt): PByte;
    174197begin
    175   if A.Size = 4 then Result := PByte(@PInteger(@A.Data[0])^)
    176     else raise Exception.Create('x');
     198  if A.Size = 4 then Result := PByte(@PInteger(@A.FData[0])^)
     199    else raise Exception.Create(SUnsupportedByteSize);
    177200end;
    178201
    179202class operator TBigInt.Inc(A: TBigInt): TBigInt;
    180203begin
     204  Result := A + 1;
     205end;
     206
     207class operator TBigInt.Dec(A: TBigInt): TBigInt;
     208begin
     209  Result := A - 1;
     210end;
     211
     212class operator TBigInt.Add(A, B: TBigInt): TBigInt;
     213var
     214  I: Integer;
     215  V: SmallInt;
     216  Carry: Byte;
     217begin
     218  Result.Size := Max(A.Size, B.Size);
     219  Carry := 0;
     220  I := 0;
     221  while (I < Result.Size) or (Carry > 0) do begin
     222    V := A.Data[I] + B.Data[I] + Carry;
     223    if V >= 256 then begin
     224      Carry := 1;
     225    end else Carry := 0;
     226    Result.Data[I] := V and $ff;
     227    Inc(I);
     228  end;
     229end;
     230
     231class operator TBigInt.Subtract(A, B: TBigInt): TBigInt;
     232var
     233  I: Integer;
     234  V: SmallInt;
     235  Carry: Byte;
     236begin
    181237  Result.Size := A.Size;
    182   case Result.Size of
    183     1: PByte(@Result.Data[0])^ := PByte(@A.Data[0])^ + 1;
    184     2: PWord(@Result.Data[0])^ := PWord(@A.Data[0])^ + 1;
    185     4: PDWord(@Result.Data[0])^ := PDWord(@A.Data[0])^ + 1;
    186     8: PQWord(@Result.Data[0])^ := PQWord(@A.Data[0])^ + 1;
    187   end;
    188 end;
    189 
    190 class operator TBigInt.Add(A, B: TBigInt): TBigInt;
    191 var
    192   CommonSize: Byte;
    193 begin
    194   CommonSize := Max(A.Size, B.Size);
    195   A.Size := CommonSize;
    196   B.Size := CommonSize;
    197   Result.Size := CommonSize;
    198   case Result.Size of
    199     1: PByte(@Result.Data[0])^ := PByte(@A.Data[0])^ + PByte(@B.Data[0])^;
    200     2: PWord(@Result.Data[0])^ := PWord(@A.Data[0])^ + PWord(@B.Data[0])^;
    201     4: PDWord(@Result.Data[0])^ := PDWord(@A.Data[0])^ + PDWord(@B.Data[0])^;
    202     8: PQWord(@Result.Data[0])^ := PQWord(@A.Data[0])^ + PQWord(@B.Data[0])^;
    203   end;
    204 end;
    205 
    206 class operator TBigInt.Subtract(A, B: TBigInt): TBigInt;
    207 var
    208   CommonSize: Byte;
    209 begin
    210   CommonSize := Max(A.Size, B.Size);
    211   A.Size := CommonSize;
    212   B.Size := CommonSize;
    213   Result.Size := CommonSize;
    214   case Result.Size of
    215     1: PByte(@Result.Data[0])^ := PByte(@A.Data[0])^ - PByte(@B.Data[0])^;
    216     2: PWord(@Result.Data[0])^ := PWord(@A.Data[0])^ - PWord(@B.Data[0])^;
    217     4: PDWord(@Result.Data[0])^ := PDWord(@A.Data[0])^ - PDWord(@B.Data[0])^;
    218     8: PQWord(@Result.Data[0])^ := PQWord(@A.Data[0])^ - PQWord(@B.Data[0])^;
     238  Carry := 0;
     239  I := 0;
     240  while (I < Result.Size) or (Carry > 0) do begin
     241    V := A.Data[I] - B.Data[I] - Carry;
     242    if V < 0 then begin
     243      Carry := 1;
     244    end else Carry := 0;
     245    Result.Data[I] := V and $ff;
     246    Inc(I);
    219247  end;
    220248end;
    221249
    222250class operator TBigInt.Equal(A, B: TBigInt): Boolean;
    223 begin
    224 
     251var
     252  I: Byte;
     253begin
     254  Result := A.Size = B.Size;
     255  for I := 0 to A.Size - 1 do
     256    if A.FData[I] <> B.FData[I] then begin
     257      Result := False;
     258      Break;
     259    end;
    225260end;
    226261
    227262class operator TBigInt.NotEqual(A, B: TBigInt): Boolean;
    228 begin
    229 
     263var
     264  I: Byte;
     265begin
     266  Result := A.Size = B.Size;
     267  for I := 0 to A.Size - 1 do
     268    if A.FData[I] <> B.FData[I] then begin
     269      Result := False;
     270      Break;
     271    end;
     272  Result := not Result;
    230273end;
    231274
     
    255298begin
    256299  I := Size - 1;
    257   while (I > 0) and (Data[I] = 0) do
     300  while (I > 0) and (FData[I] = 0) do
    258301    Dec(I);
    259302  Size := I + 1;
    260303end;
    261304
    262 procedure TBigInt.SetByteArray(AData: TArrayOfByte; ASize: Byte);
     305procedure TBigInt.SetByteArray(AData: TArrayOfByte; ASize: TBigIntSize);
    263306begin
    264307  Size := ASize;
    265   Move(AData[0], Data[0], Size);
    266 end;
    267 
    268 procedure TBigInt.GetByteArray(var AData: TArrayOfByte; ASize: Byte);
     308  Move(AData[0], FData[0], Size);
     309end;
     310
     311procedure TBigInt.GetByteArray(var AData: TArrayOfByte; ASize: TBigIntSize);
    269312begin
    270313  SetLength(AData, ASize);
    271   Move(Data[0], AData[0], Size);
    272 end;
    273 
    274 function TBigInt.Copy(Size: Byte): TBigInt;
     314  Move(FData[0], AData[0], Size);
     315end;
     316
     317function TBigInt.Copy(Size: TBigIntSize): TBigInt;
    275318begin
    276319  Result.Size := Size;
    277320  case Result.Size of
    278     1: PByte(@Result.Data[0])^ := PByte(@Data[0])^;
    279     2: PWord(@Result.Data[0])^ := PWord(@Data[0])^;
    280     4: PDWord(@Result.Data[0])^ := PDWord(@Data[0])^;
    281     8: PQWord(@Result.Data[0])^ := PQWord(@Data[0])^;
     321    1: PByte(@Result.FData[0])^ := PByte(@FData[0])^;
     322    2: PWord(@Result.FData[0])^ := PWord(@FData[0])^;
     323    4: PDWord(@Result.FData[0])^ := PDWord(@FData[0])^;
     324    8: PQWord(@Result.FData[0])^ := PQWord(@FData[0])^;
    282325  end;
    283326end;
  • branches/ByteArray/ByteArray.lpi

    r46 r47  
    2626    <RequiredPackages>
    2727      <Item>
     28        <PackageName Value="Common"/>
     29        <DefaultFilename Value="Packages/Common/Common.lpk" Prefer="True"/>
     30      </Item>
     31      <Item>
    2832        <PackageName Value="SynEdit"/>
    2933      </Item>
     
    163167        <HasResources Value="True"/>
    164168        <ResourceBaseClass Value="Form"/>
     169      </Unit>
     170      <Unit>
     171        <Filename Value="Forms/Project.pas"/>
     172        <IsPartOfProject Value="True"/>
    165173      </Unit>
    166174    </Units>
  • branches/ByteArray/Channel.pas

    r45 r47  
    99  TChannel = class
    1010  type
    11     TRead = function (Address: TBigInt; DataSize: Byte): TBigInt of object;
    12     TWrite = procedure (Address: TBigInt; DataSize: Byte; Value: TBigInt) of object;
     11    TRead = function (Address: TBigInt; DataSize: TBigIntSize): TBigInt of object;
     12    TWrite = procedure (Address: TBigInt; DataSize: TBigIntSize; Value: TBigInt) of object;
    1313    TGetSize = function : TBigInt of object;
    1414  var
  • branches/ByteArray/Cpu.pas

    r46 r47  
    77
    88type
    9   TBitWidth = Byte;
    10 
    119  TInstruction = (inNop, inHalt,
    1210    inLoadConst, inLoadConstSize,
     
    4947  private
    5048    Instructions: array[TInstruction] of TInstructionEvent;
    51     procedure Push(Value: TBigInt; Size: Byte);
    52     function Pop(Size: Byte): TBigInt;
     49    procedure Push(Value: TBigInt; Size: TBigIntSize);
     50    function Pop(Size: TBigIntSize): TBigInt;
    5351    procedure InstructionNop;
    5452    procedure InstructionHalt;
     
    9189    SP: TBigInt;
    9290    Terminated: Boolean;
    93     DataWidth: TBitWidth;
    94     AddressWidth: TBitWidth;
     91    DataWidth: TBigIntSize;
     92    AddressWidth: TBigIntSize;
    9593    Memory: TChannel;
    9694    IO: TChannel;
    97     function Read(Size: Byte): TBigInt;
     95    function Read(Size: TBigIntSize): TBigInt;
    9896    function ReadRegIndex: TRegIndex;
    99     procedure Write(Size: Byte; Value: TBigInt);
     97    procedure Write(Size: TBigIntSize; Value: TBigInt);
    10098    procedure WriteInstruction(Instruction: TInstruction);
    10199    procedure WriteRegister(Reg: TRegIndex);
     
    111109{ TCpu }
    112110
    113 procedure TCpu.Push(Value: TBigInt; Size: Byte);
     111procedure TCpu.Push(Value: TBigInt; Size: TBigIntSize);
    114112begin
    115113  SP := SP - Size;
     
    117115end;
    118116
    119 function TCpu.Pop(Size: Byte): TBigInt;
     117function TCpu.Pop(Size: TBigIntSize): TBigInt;
    120118begin
    121119  Result := Memory.Read(SP, Size);
     
    144142var
    145143  RegIndex: TRegIndex;
    146   DataSize: Byte;
    147 begin
    148   DataSize := Read(1);
     144  DataSize: TBigIntSize;
     145begin
     146  DataSize := Read(SizeOf(TBigIntSize));
    149147  RegIndex := ReadRegIndex;
    150148  Regs[RegIndex] := Read(DataSize);
     
    163161procedure TCpu.InstructionLoadSize;
    164162var
    165   DataSize: Byte;
     163  DataSize: TBigIntSize;
    166164  RegIndex: TRegIndex;
    167165  RegIndex2: TRegIndex;
    168166begin
    169   DataSize := Read(1);
     167  DataSize := Read(SizeOf(TBigIntSize));
    170168  RegIndex := ReadRegIndex;
    171169  RegIndex2 := ReadRegIndex;
     
    185183procedure TCpu.InstructionLoadMemSize;
    186184var
    187   DataSize: Byte;
    188   AddressSize: Byte;
    189   Address: TBigInt;
    190   RegIndex: TRegIndex;
    191 begin
    192   DataSize := Read(1);
    193   AddressSize := Read(1);
     185  DataSize: TBigIntSize;
     186  AddressSize: TBigIntSize;
     187  Address: TBigInt;
     188  RegIndex: TRegIndex;
     189begin
     190  DataSize := Read(SizeOf(TBigIntSize));
     191  AddressSize := Read(SizeOf(TBigIntSize));
    194192  RegIndex := ReadRegIndex;
    195193  Address := Read(AddressSize);
     
    209207procedure TCpu.InstructionStoreMemSize;
    210208var
    211   DataSize: Byte;
    212   AddressSize: Byte;
    213   Address: TBigInt;
    214   RegIndex: TRegIndex;
    215 begin
    216   DataSize := Read(1);
    217   AddressSize := Read(1);
     209  DataSize: TBigIntSize;
     210  AddressSize: TBigIntSize;
     211  Address: TBigInt;
     212  RegIndex: TRegIndex;
     213begin
     214  DataSize := Read(SizeOf(TBigIntSize));
     215  AddressSize := Read(SizeOf(TBigIntSize));
    218216  Address := Read(AddressSize);
    219217  RegIndex := ReadRegIndex;
     
    228226procedure TCpu.InstructionJumpSize;
    229227var
    230   AddressSize: Byte;
    231 begin
    232   AddressSize := Read(1);
     228  AddressSize: TBigIntSize;
     229begin
     230  AddressSize := Read(SizeOf(TBigIntSize));
    233231  PC := Read(AddressSize);
    234232end;
     
    249247  RegIndex: TRegIndex;
    250248  Address: TBigInt;
    251   DataSize: Byte;
    252   AddressSize: Byte;
    253 begin
    254   DataSize := Read(1);
    255   AddressSize := Read(1);
     249  DataSize: TBigIntSize;
     250  AddressSize: TBigIntSize;
     251begin
     252  DataSize := Read(SizeOf(TBigIntSize));
     253  AddressSize := Read(SizeOf(TBigIntSize));
    256254  RegIndex := ReadRegIndex;
    257255  Address := Read(AddressSize);
     
    275273  RegIndex: TRegIndex;
    276274  Address: TBigInt;
    277   DataSize: Byte;
    278   AddressSize: Byte;
    279 begin
    280   DataSize := Read(1);
    281   AddressSize := Read(1);
     275  DataSize: TBigIntSize;
     276  AddressSize: TBigIntSize;
     277begin
     278  DataSize := Read(SizeOf(TBigIntSize));
     279  AddressSize := Read(SizeOf(TBigIntSize));
    282280  RegIndex := ReadRegIndex;
    283281  Address := Read(AddressSize);
     
    293291procedure TCpu.InstructionJumpRelSize;
    294292var
    295   AddressSize: Byte;
    296 begin
    297   AddressSize := Read(1);
     293  AddressSize: TBigIntSize;
     294begin
     295  AddressSize := Read(SizeOf(TBigIntSize));
    298296  PC := PC + Read(AddressSize);
    299297end;
     
    307305procedure TCpu.InstructionCallSize;
    308306var
    309   AddressSize: Byte;
    310 begin
    311   AddressSize := Read(1);
     307  AddressSize: TBigIntSize;
     308begin
     309  AddressSize := Read(SizeOf(TBigIntSize));
    312310  Push(PC, AddressSize);
    313311  PC := Read(AddressSize);
     
    321319procedure TCpu.InstructionRetSize;
    322320var
    323   AddressSize: Byte;
    324 begin
    325   AddressSize := Read(1);
     321  AddressSize: TBigIntSize;
     322begin
     323  AddressSize := Read(SizeOf(TBigIntSize));
    326324  PC := Pop(AddressSize);
    327325end;
     
    337335procedure TCpu.InstructionPushSize;
    338336var
    339   DataSize: Byte;
    340   RegIndex: TRegIndex;
    341 begin
    342   DataSize := Read(1);
     337  DataSize: TBigIntSize;
     338  RegIndex: TRegIndex;
     339begin
     340  DataSize := Read(SizeOf(TBigIntSize));
    343341  RegIndex := ReadRegIndex;
    344342  Push(Regs[RegIndex], DataSize);
     
    355353procedure TCpu.InstructionPopSize;
    356354var
    357   DataSize: Byte;
    358   RegIndex: TRegIndex;
    359 begin
    360   DataSize := Read(1);
     355  DataSize: TBigIntSize;
     356  RegIndex: TRegIndex;
     357begin
     358  DataSize := Read(SizeOf(TBigIntSize));
    361359  RegIndex := ReadRegIndex;
    362360  Regs[RegIndex] := Pop(DataSize);
     
    377375  RegIndex: TRegIndex;
    378376  RegIndex2: TRegIndex;
    379   DataSize: Byte;
    380 begin
    381   DataSize := Read(1);
     377  DataSize: TBigIntSize;
     378begin
     379  DataSize := Read(SizeOf(TBigIntSize));
    382380  RegIndex := ReadRegIndex;
    383381  RegIndex2 := ReadRegIndex;
     
    399397  RegIndex: TRegIndex;
    400398  RegIndex2: TRegIndex;
    401   DataSize: Byte;
    402 begin
    403   DataSize := Read(1);
     399  DataSize: TBigIntSize;
     400begin
     401  DataSize := Read(SizeOf(TBigIntSize));
    404402  RegIndex := ReadRegIndex;
    405403  RegIndex2 := ReadRegIndex;
     
    418416var
    419417  RegIndex: TRegIndex;
    420   DataSize: Byte;
    421 begin
    422   DataSize := Read(1);
     418  DataSize: TBigIntSize;
     419begin
     420  DataSize := Read(SizeOf(TBigIntSize));
    423421  RegIndex := ReadRegIndex;
    424422  Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) + 1;
     
    436434var
    437435  RegIndex: TRegIndex;
    438   DataSize: Byte;
    439 begin
    440   DataSize := Read(1);
     436  DataSize: TBigIntSize;
     437begin
     438  DataSize := Read(SizeOf(TBigIntSize));
    441439  RegIndex := ReadRegIndex;
    442440  Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) - 1;
     
    481479end;
    482480
    483 function TCpu.Read(Size: Byte): TBigInt;
     481function TCpu.Read(Size: TBigIntSize): TBigInt;
    484482begin
    485483  Result := Memory.Read(PC, Size);
     
    492490end;
    493491
    494 procedure TCpu.Write(Size: Byte; Value: TBigInt);
     492procedure TCpu.Write(Size: TBigIntSize; Value: TBigInt);
    495493begin
    496494  Memory.Write(PC, Size, Value);
     
    517515    Regs[I] := 0;
    518516  PC := 0;
     517  SP := 0;
    519518  Terminated := False;
    520519end;
  • branches/ByteArray/Devices/DeviceMapper.pas

    r45 r47  
    2525    procedure RegisterDevice(Device: TDevice);
    2626    procedure UnregisterDevice(Device: TDevice);
    27     function Read(Address: TBigInt; Size: Byte): TBigInt;
    28     procedure Write(Address: TBigInt; Size: Byte; Value: TBigInt);
     27    function Read(Address: TBigInt; Size: TBigIntSize): TBigInt;
     28    procedure Write(Address: TBigInt; Size: TBigIntSize; Value: TBigInt);
    2929    procedure SetChannel(Channel: TChannel);
    3030    constructor Create;
     
    7474end;
    7575
    76 function TDeviceMapper.Read(Address: TBigInt; Size: Byte): TBigInt;
     76function TDeviceMapper.Read(Address: TBigInt; Size: TBigIntSize): TBigInt;
    7777var
    7878  I: Integer;
     
    8585end;
    8686
    87 procedure TDeviceMapper.Write(Address: TBigInt; Size: Byte; Value: TBigInt);
     87procedure TDeviceMapper.Write(Address: TBigInt; Size: TBigIntSize; Value: TBigInt);
    8888var
    8989  I: Integer;
  • branches/ByteArray/Devices/FrameBuffer.pas

    r45 r47  
    2222    Mode: TScreenMode;
    2323    procedure UpdateMode;
    24     function Read(Address: TBigInt; Size: Byte): TBigInt;
    25     procedure Write(Address: TBigInt; Size: Byte; Value: TBigInt);
     24    function Read(Address: TBigInt; Size: TBigIntSize): TBigInt;
     25    procedure Write(Address: TBigInt; Size: TBigIntSize; Value: TBigInt);
    2626    function GetAddressCount: Integer; override;
    2727    procedure SetChannel(Channel: TChannel); override;
     
    4747end;
    4848
    49 function TFrameBuffer.Read(Address: TBigInt; Size: Byte): TBigInt;
     49function TFrameBuffer.Read(Address: TBigInt; Size: TBigIntSize): TBigInt;
    5050begin
    5151  case Integer(Address) of
     
    6161end;
    6262
    63 procedure TFrameBuffer.Write(Address: TBigInt; Size: Byte; Value: TBigInt);
     63procedure TFrameBuffer.Write(Address: TBigInt; Size: TBigIntSize; Value: TBigInt);
    6464begin
    6565  case Integer(Address) of
  • branches/ByteArray/Devices/Memory.pas

    r46 r47  
    1717  public
    1818    Position: Integer;
    19     function Read(Address: TBigInt; Size: Byte): TBigInt;
    20     function ReadPos(Size: Byte): TBigInt;
    21     procedure Write(Address: TBigInt; Size: Byte; Value: TBigInt);
    22     procedure WritePos(Size: Byte; Value: TBigInt);
     19    function Read(Address: TBigInt; ASize: TBigIntSize): TBigInt;
     20    function ReadPos(ASize: Byte): TBigInt;
     21    procedure Write(Address: TBigInt; ASize: TBigIntSize; Value: TBigInt);
     22    procedure WritePos(ASize: Byte; Value: TBigInt);
    2323    procedure WriteStringPos(Value: string);
    2424    function GetAddressCount: Integer; override;
     
    3030
    3131implementation
     32
     33resourcestring
     34  SOutOfRange = 'Out of range';
    3235
    3336{ TMemory }
     
    4346end;
    4447
    45 function TMemory.Read(Address: TBigInt; Size: Byte): TBigInt;
     48function TMemory.Read(Address: TBigInt; ASize: TBigIntSize): TBigInt;
    4649begin
    47   case Size of
     50  if Address + ASize >= Size then raise Exception.Create(SOutOfRange);
     51  case ASize of
    4852    1: Result := PByte(FData + Integer(Address))^;
    4953    2: Result := PWord(FData + Integer(Address))^;
     
    5357end;
    5458
    55 function TMemory.ReadPos(Size: Byte): TBigInt;
     59function TMemory.ReadPos(ASize: Byte): TBigInt;
    5660begin
    57   Result := Read(Position, Size);
     61  Result := Read(Position, ASize);
    5862end;
    5963
    60 procedure TMemory.Write(Address: TBigInt; Size: Byte; Value: TBigInt);
     64procedure TMemory.Write(Address: TBigInt; ASize: TBigIntSize; Value: TBigInt);
    6165begin
    62   case Size of
     66  if Address + ASize >= Size then raise Exception.Create(SOutOfRange);
     67  case ASize of
    6368    1: PByte(FData + Integer(Address))^ := Value;
    6469    2: PWord(FData + Integer(Address))^ := Value;
     
    6873end;
    6974
    70 procedure TMemory.WritePos(Size: Byte; Value: TBigInt);
     75procedure TMemory.WritePos(ASize: Byte; Value: TBigInt);
    7176begin
    72   Write(Position, Size, Value);
     77  Write(Position, ASize, Value);
    7378end;
    7479
  • branches/ByteArray/Devices/Serial.pas

    r45 r47  
    1717    FOnWrite: TWriteEvent;
    1818  public
    19     function Read(Address: TBigInt; Size: Byte): TBigInt;
    20     procedure Write(Address: TBigInt; Size: Byte; Value: TBigInt);
     19    function Read(Address: TBigInt; Size: TBigIntSize): TBigInt;
     20    procedure Write(Address: TBigInt; Size: TBigIntSize; Value: TBigInt);
    2121    function GetAddressCount: Integer; override;
    2222    procedure SetChannel(Channel: TChannel); override;
     
    3030{ TSerial }
    3131
    32 function TSerial.Read(Address: TBigInt; Size: Byte): TBigInt;
     32function TSerial.Read(Address: TBigInt; Size: TBigIntSize): TBigInt;
    3333begin
    3434  case Integer(Address) of
     
    3737end;
    3838
    39 procedure TSerial.Write(Address: TBigInt; Size: Byte; Value: TBigInt);
     39procedure TSerial.Write(Address: TBigInt; Size: TBigIntSize; Value: TBigInt);
    4040begin
    4141  case Integer(Address) of
  • branches/ByteArray/Devices/Storage.pas

    r45 r47  
    1717    FFile: TFileStream;
    1818    Position: Integer;
    19     function Read(Address: TBigInt; Size: Byte): TBigInt;
    20     procedure Write(Address: TBigInt; Size: Byte; Value: TBigInt);
     19    function Read(Address: TBigInt; Size: TBigIntSize): TBigInt;
     20    procedure Write(Address: TBigInt; Size: TBigIntSize; Value: TBigInt);
    2121    function GetAddressCount: Integer; override;
    2222    procedure SetChannel(Channel: TChannel); override;
     
    4242end;
    4343
    44 function TStorage.Read(Address: TBigInt; Size: Byte): TBigInt;
     44function TStorage.Read(Address: TBigInt; Size: TBigIntSize): TBigInt;
    4545var
    4646  Buffer: array of Byte;
     
    5959end;
    6060
    61 procedure TStorage.Write(Address: TBigInt; Size: Byte; Value: TBigInt);
     61procedure TStorage.Write(Address: TBigInt; Size: TBigIntSize; Value: TBigInt);
    6262var
    6363  Buffer: array of Byte;
  • branches/ByteArray/Forms/FormSourceEditor.lfm

    r46 r47  
    55  Width = 926
    66  Caption = 'Source editor'
    7   ClientHeight = 681
     7  ClientHeight = 647
    88  ClientWidth = 926
    99  DesignTimePPI = 144
     10  Menu = MainMenu1
    1011  OnCreate = FormCreate
    1112  OnDestroy = FormDestroy
     
    1415  object PanelCenter: TPanel
    1516    Left = 0
    16     Height = 513
     17    Height = 479
    1718    Top = 0
    1819    Width = 926
     
    2526    Left = 0
    2627    Height = 8
    27     Top = 513
     28    Top = 479
    2829    Width = 926
    2930    Align = alBottom
     
    3334    Left = 0
    3435    Height = 160
    35     Top = 521
     36    Top = 487
    3637    Width = 926
    3738    Align = alBottom
     
    3940    TabOrder = 2
    4041  end
     42  object MainMenu1: TMainMenu
     43    Left = 472
     44    Top = 144
     45    object MenuItem1: TMenuItem
     46      Caption = 'File'
     47      object MenuItem2: TMenuItem
     48        Action = ANew
     49      end
     50      object MenuItem3: TMenuItem
     51        Action = AOpen
     52      end
     53      object MenuItem4: TMenuItem
     54        Action = AOpenRecent
     55      end
     56      object MenuItem5: TMenuItem
     57        Action = ASave
     58      end
     59      object MenuItem6: TMenuItem
     60        Action = ASaveAs
     61      end
     62      object MenuItem7: TMenuItem
     63        Action = AClose
     64      end
     65      object Separator1: TMenuItem
     66        Caption = '-'
     67      end
     68      object MenuItem8: TMenuItem
     69        Action = AExit
     70      end
     71    end
     72  end
     73  object ActionList1: TActionList
     74    Left = 618
     75    Top = 147
     76    object AExit: TAction
     77      Caption = 'Exit'
     78      OnExecute = AExitExecute
     79    end
     80    object ANew: TAction
     81      Caption = 'New'
     82      OnExecute = ANewExecute
     83    end
     84    object AOpen: TAction
     85      Caption = 'Open'
     86      OnExecute = AOpenExecute
     87      ShortCut = 16463
     88    end
     89    object AOpenRecent: TAction
     90      Caption = 'Open recent'
     91    end
     92    object ASave: TAction
     93      Caption = 'Save'
     94      OnExecute = ASaveExecute
     95      ShortCut = 16467
     96    end
     97    object ASaveAs: TAction
     98      Caption = 'Save as...'
     99      OnExecute = ASaveAsExecute
     100    end
     101    object AClose: TAction
     102      Caption = 'Close'
     103      OnExecute = ACloseExecute
     104    end
     105  end
    41106end
  • branches/ByteArray/Forms/FormSourceEditor.pas

    r46 r47  
    44
    55uses
    6   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, FormMessages,
    7   FormAssembler;
     6  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Menus,
     7  ActnList, FormMessages, FormAssembler, Project;
    88
    99type
     
    1212
    1313  TFormSourceEditor = class(TForm)
     14    AClose: TAction;
     15    ANew: TAction;
     16    ASaveAs: TAction;
     17    ASave: TAction;
     18    AOpenRecent: TAction;
     19    AOpen: TAction;
     20    AExit: TAction;
     21    ActionList1: TActionList;
     22    MainMenu1: TMainMenu;
     23    MenuItem1: TMenuItem;
     24    MenuItem2: TMenuItem;
     25    MenuItem3: TMenuItem;
     26    MenuItem4: TMenuItem;
     27    MenuItem5: TMenuItem;
     28    MenuItem6: TMenuItem;
     29    MenuItem7: TMenuItem;
     30    MenuItem8: TMenuItem;
     31    Separator1: TMenuItem;
    1432    PanelBottom: TPanel;
    1533    PanelCenter: TPanel;
    1634    Splitter1: TSplitter;
     35    procedure ACloseExecute(Sender: TObject);
     36    procedure AExitExecute(Sender: TObject);
     37    procedure ANewExecute(Sender: TObject);
     38    procedure AOpenExecute(Sender: TObject);
     39    procedure ASaveAsExecute(Sender: TObject);
     40    procedure ASaveExecute(Sender: TObject);
    1741    procedure FormCreate(Sender: TObject);
    1842    procedure FormDestroy(Sender: TObject);
    1943    procedure FormShow(Sender: TObject);
    20   private
    21 
    2244  public
    2345    FormMessages: TFormMessages;
    2446    FormAssembler: TFormAssembler;
     47    Project: TProject;
    2548    procedure DockInit;
     49    procedure UpdateInterface;
    2650  end;
    2751
     
    4165  FormAssembler := TFormAssembler.Create(nil);
    4266  FormMessages.OnSelect := FormAssembler.Select;
     67end;
     68
     69procedure TFormSourceEditor.AExitExecute(Sender: TObject);
     70begin
     71  Close;
     72end;
     73
     74procedure TFormSourceEditor.ANewExecute(Sender: TObject);
     75begin
     76  Project.New;
     77end;
     78
     79procedure TFormSourceEditor.ACloseExecute(Sender: TObject);
     80begin
     81  Project.Close;
     82end;
     83
     84procedure TFormSourceEditor.AOpenExecute(Sender: TObject);
     85var
     86  OpenDialog: TOpenDialog;
     87begin
     88  OpenDialog := TOpenDialog.Create(nil);
     89  try
     90    if OpenDialog.Execute then
     91      Project.Open(OpenDialog.FileName);
     92  finally
     93    OpenDialog.Free;
     94  end;
     95end;
     96
     97procedure TFormSourceEditor.ASaveAsExecute(Sender: TObject);
     98var
     99  SaveDialog: TSaveDialog;
     100begin
     101  SaveDialog := TSaveDialog.Create(nil);
     102  try
     103    if SaveDialog.Execute then
     104      Project.SaveAs(SaveDialog.FileName);
     105  finally
     106    SaveDialog.Free;
     107  end;
     108end;
     109
     110procedure TFormSourceEditor.ASaveExecute(Sender: TObject);
     111begin
     112  Project.Save;
    43113end;
    44114
     
    62132end;
    63133
     134procedure TFormSourceEditor.UpdateInterface;
     135begin
     136
     137end;
     138
    64139end.
    65140
Note: See TracChangeset for help on using the changeset viewer.