Changeset 9 for branches/ByteArray


Ignore:
Timestamp:
Aug 6, 2024, 10:31:16 PM (4 months ago)
Author:
chronos
Message:
  • Modified: Optimized DeviceMapper read/write handlers calls without case statement.
  • Modified: Use TInt as base data type which can be redefined to different higher integer type. For start it is Int64.
Location:
branches/ByteArray
Files:
1 added
16 edited

Legend:

Unmodified
Added
Removed
  • branches/ByteArray

    • Property svn:ignore set to
      lib
      heaptrclog.trc
      ByteArray
      ByteArray.dbg
      ByteArray.lps
      ByteArray.res
  • branches/ByteArray/Assembler.pas

    r5 r9  
    44
    55uses
    6   Classes, SysUtils, Instructions, Cpu, Generics.Collections, BigInt,
     6  Classes, SysUtils, Instructions, Cpu, Generics.Collections, Int,
    77  Memory, Message, Parser;
    88
     
    2323    FOnError: TErrorEvent;
    2424    Parser: TParser;
    25     InstDataWidth: TBigIntSize;
    26     InstAddressWidth: TBigIntSize;
     25    InstDataWidth: TIntSize;
     26    InstAddressWidth: TIntSize;
    2727    function ParseVar: Boolean;
    2828    function ParseDb: Boolean;
     
    3232    function ParseLabel: Boolean;
    3333    procedure UpdateLabelRefs;
    34     function ParseNumParam(out Number: TBigInt): Boolean;
     34    function ParseNumParam(out Number: TInt): Boolean;
    3535    function ParseReg(out RegIndex: TRegIndex): Boolean;
    36     function ParseDataWidth(out Size: TBigIntSize): Boolean;
    37     function ParseAddressWidth(out Size: TBigIntSize): Boolean;
     36    function ParseDataWidth(out Size: TIntSize): Boolean;
     37    function ParseAddressWidth(out Size: TIntSize): Boolean;
    3838  public
    3939    InstructionSet: TInstructionInfos;
    4040    Memory: TMemory;
    41     Labels: TDictionary<string, TBigInt>;
     41    Labels: TDictionary<string, TInt>;
    4242    LabelRefs: TList<TLabelRef>;
    43     Variables: TDictionary<string, TBigInt>;
     43    Variables: TDictionary<string, TInt>;
    4444    Messages: TMessages;
    45     DataWidth: TBigIntSize;
    46     AddressWidth: TBigIntSize;
     45    DataWidth: TIntSize;
     46    AddressWidth: TIntSize;
    4747    procedure Error(Text: string; Pos: TPoint);
    4848    procedure Compile(Source: string);
     
    8686var
    8787  I: Integer;
    88   Addr: TBigInt;
     88  Addr: TInt;
    8989begin
    9090  for I := 0 to LabelRefs.Count - 1 do begin
     
    9696end;
    9797
    98 function TAssembler.ParseNumParam(out Number: TBigInt): Boolean;
     98function TAssembler.ParseNumParam(out Number: TInt): Boolean;
    9999var
    100100  Token: TToken;
     
    105105  Token := Parser.ReadNext;
    106106  if Token.Kind = tkNumber then begin
    107     if TryStrToBigInt(Token.Value, Number) then begin
     107    if TryStrToInt64(Token.Value, Number) then begin
    108108      Result := True;
    109109    end;
     
    142142end;
    143143
    144 function TAssembler.ParseDataWidth(out Size: TBigIntSize): Boolean;
     144function TAssembler.ParseDataWidth(out Size: TIntSize): Boolean;
    145145var
    146146  LastPos: TParserPos;
     
    160160end;
    161161
    162 function TAssembler.ParseAddressWidth(out Size: TBigIntSize): Boolean;
     162function TAssembler.ParseAddressWidth(out Size: TIntSize): Boolean;
    163163var
    164164  LastPos: TParserPos;
     
    218218  TokenName: TToken;
    219219  TokenValue: TToken;
    220   Number: TBigInt;
     220  Number: TInt;
    221221begin
    222222  Result := False;
     
    229229        if TokenValue.Kind = tkNumber then begin
    230230          if not Labels.ContainsKey(TokenName.Value) and not Variables.ContainsKey(TokenName.Value) then begin
    231             if TryStrToBigInt(TokenValue.Value, Number) then
     231            if TryStrToInt64(TokenValue.Value, Number) then
    232232              Variables.Add(TokenName.Value, Number)
    233233            else Error(SExpectedNumber, TokenValue.Pos);
     
    246246var
    247247  Token: TToken;
    248   Number: TBigInt;
     248  Number: TInt;
    249249begin
    250250  Result := False;
     
    384384  I: Integer;
    385385  RegIndex: TRegIndex;
    386   Number: TBigInt;
     386  Number: TInt;
    387387  Token: TToken;
    388388begin
     
    464464  LastPos: TParserPos;
    465465  Token: TToken;
    466   Addr: TBigInt;
     466  Addr: TInt;
    467467begin
    468468  Result := False;
     
    513513  InstructionSet := TInstructionInfos.Create;
    514514  InstructionSet.Init;
    515   Labels := TDictionary<string, TBigInt>.Create;
     515  Labels := TDictionary<string, TInt>.Create;
    516516  LabelRefs := TList<TLabelRef>.Create;
    517   Variables := TDictionary<string, TBigInt>.Create;
     517  Variables := TDictionary<string, TInt>.Create;
    518518end;
    519519
  • branches/ByteArray/ByteArray.lpi

    r5 r9  
    285285        <IsPartOfProject Value="True"/>
    286286      </Unit>
     287      <Unit>
     288        <Filename Value="Int.pas"/>
     289        <IsPartOfProject Value="True"/>
     290      </Unit>
     291      <Unit>
     292        <Filename Value="Sample.asm"/>
     293        <IsPartOfProject Value="True"/>
     294      </Unit>
    287295    </Units>
    288296  </ProjectOptions>
  • branches/ByteArray/ByteArray.lpr

    r5 r9  
    1313  Storage, DeviceMapper, Machine, Disassembler, Instructions, Parser, Message,
    1414  Assembler, Serial, Mouse, FormSourceEditor, FormMessages, FormMemory,
    15   FormStorage, Common, FormSettings, Core, FormDebugger, VarInt
     15  FormStorage, Common, FormSettings, Core, FormDebugger, VarInt, Int
    1616  { you can add units after this };
    1717
  • branches/ByteArray/Channel.pas

    r5 r9  
    44
    55uses
    6   Classes, SysUtils, BigInt;
     6  Classes, SysUtils, Int;
    77
    88type
    99  TChannel = class
    1010  type
    11     TRead = function (Address: TBigInt; DataSize: TBigIntSize): TBigInt of object;
    12     TWrite = procedure (Address: TBigInt; DataSize: TBigIntSize; Value: TBigInt) of object;
    13     TGetSize = function : TBigInt of object;
     11    TRead = function (Address: TInt; DataSize: TIntSize): TInt of object;
     12    TWrite = procedure (Address: TInt; DataSize: TIntSize; Value: TInt) of object;
     13    TGetSize = function : TInt of object;
    1414  var
    1515    Read: TRead;
    1616    Write: TWrite;
    1717    GetSize: TGetSize;
     18  end;
     19
     20  TChannelDevice = class
     21    procedure SetChannel(Channel: TChannel); virtual; abstract;
    1822  end;
    1923
  • branches/ByteArray/Core.pas

    r5 r9  
    4545    procedure DataModuleDestroy(Sender: TObject);
    4646  private
    47     procedure InitMachine;
    4847    procedure LoadConfig;
    4948    procedure SaveConfig;
     
    5958    StartUpForm: string;
    6059    AutoStartMachine: Boolean;
     60    ExecutableFileName: string;
    6161    Machine: TMachine;
    6262  end;
     
    7878begin
    7979  Machine := TMachine.Create;
    80   //InitMachine;
    8180  LoadConfig;
    8281
     
    9493    ADebugger.Execute;
    9594  end;
     95  if FileExists(ExecutableFileName) then
     96    Machine.Memory.LoadFromFile(ExecutableFileName);
    9697  if AutoStartMachine then
    9798    Machine.PowerOn;
     
    213214
    214215  FreeAndNil(Machine);
    215 end;
    216 
    217 procedure TCore.InitMachine;
    218 var
    219   Loop: TBigInt;
    220 begin
    221   with Machine.Cpu do begin
    222     Reset;
    223 
    224     // LD A, 'A'
    225     WriteInstruction(inLoadConst);
    226     WriteRegister(0);
    227     Write(DataWidth, Ord('A'));
    228 
    229     // LD B, 8
    230     WriteInstruction(inLoadConst);
    231     WriteRegister(1);
    232     Write(DataWidth, Machine.Serial.BaseAddress);
    233 
    234     // OUT (B), A
    235     WriteInstruction(inOutput);
    236     WriteRegister(1);
    237     WriteRegister(0);
    238 
    239     // LD B,
    240     WriteInstruction(inLoadConst);
    241     WriteRegister(1);
    242     Write(DataWidth, Machine.FrameBuffer.BaseAddress);
    243 
    244     WriteInstruction(inLoadConst);
    245     WriteRegister(2);
    246     Write(DataWidth, 10);
    247 
    248     Loop := PC;
    249 
    250     WriteInstruction(inOutput);
    251     WriteRegister(1);
    252     WriteRegister(0);
    253 
    254     WriteInstruction(inInc);
    255     WriteRegister(0);
    256 
    257     WriteInstruction(inDec);
    258     WriteRegister(2);
    259 
    260     WriteInstruction(inJumpNotZero);
    261     WriteRegister(2);
    262     Write(AddressWidth, Loop);
    263 
    264     WriteInstruction(inHalt);
    265   end;
    266216end;
    267217
     
    281231    StartUpForm := ReadStringWithDefault('StartUpForm', 'Screen');
    282232    AutoStartMachine := ReadBoolWithDefault('AutoStartMachine', True);
     233    ExecutableFileName := ReadStringWithDefault('ExecutableFileName', 'compiled.bin');
    283234  finally
    284235    Free;
     
    299250    WriteString('StartUpForm', StartUpForm);
    300251    WriteBool('AutoStartMachine', AutoStartMachine);
     252    WriteString('ExecutableFileName', ExecutableFileName);
    301253  finally
    302254    Free;
  • branches/ByteArray/Cpu.pas

    r5 r9  
    44
    55uses
    6   Classes, SysUtils, BigInt, Channel;
     6  Classes, SysUtils, Int, Channel;
    77
    88type
     
    5454    FInstructions: array[TInstruction] of TInstructionEvent;
    5555    FTicks: QWord;
    56     procedure Push(Value: TBigInt; Size: TBigIntSize);
    57     function Pop(Size: TBigIntSize): TBigInt;
     56    procedure Push(Value: TInt; Size: TIntSize);
     57    function Pop(Size: TIntSize): TInt;
    5858    procedure InstructionNop;
    5959    procedure InstructionHalt;
     
    109109    procedure Step;
    110110  public
    111     Regs: array[TRegIndex] of TBigInt;
    112     PC: TBigInt;
    113     SP: TBigInt;
    114     DataWidth: TBigIntSize;
    115     AddressWidth: TBigIntSize;
     111    Regs: array[TRegIndex] of TInt;
     112    PC: TInt;
     113    SP: TInt;
     114    DataWidth: TIntSize;
     115    AddressWidth: TIntSize;
    116116    Memory: TChannel;
    117117    IO: TChannel;
    118     function Read(Size: TBigIntSize): TBigInt;
    119     function ReadSize: TBigIntSize;
     118    function Read(Size: TIntSize): TInt;
     119    function ReadSize: TIntSize;
    120120    function ReadRegIndex: TRegIndex;
    121     procedure Write(Size: TBigIntSize; Value: TBigInt);
     121    procedure Write(Size: TIntSize; Value: TInt);
    122122    procedure WriteInstruction(Instruction: TInstruction);
    123123    procedure WriteRegister(Reg: TRegIndex);
     
    155155{ TCpu }
    156156
    157 procedure TCpu.Push(Value: TBigInt; Size: TBigIntSize);
     157procedure TCpu.Push(Value: TInt; Size: TIntSize);
    158158begin
    159159  SP := SP - Size;
     
    161161end;
    162162
    163 function TCpu.Pop(Size: TBigIntSize): TBigInt;
     163function TCpu.Pop(Size: TIntSize): TInt;
    164164begin
    165165  Result := Memory.Read(SP, Size);
     
    188188var
    189189  RegIndex: TRegIndex;
    190   DataSize: TBigIntSize;
     190  DataSize: TIntSize;
    191191begin
    192192  DataSize := ReadSize;
     
    207207procedure TCpu.InstructionLoadSize;
    208208var
    209   DataSize: TBigIntSize;
    210   RegIndex: TRegIndex;
    211   RegIndex2: TRegIndex;
    212 begin
    213   DataSize := ReadSize;
    214   RegIndex := ReadRegIndex;
    215   RegIndex2 := ReadRegIndex;
    216   Regs[RegIndex] := Regs[RegIndex2].Copy(DataSize);
     209  DataSize: TIntSize;
     210  RegIndex: TRegIndex;
     211  RegIndex2: TRegIndex;
     212begin
     213  DataSize := ReadSize;
     214  RegIndex := ReadRegIndex;
     215  RegIndex2 := ReadRegIndex;
     216  Regs[RegIndex] := LimitSize(Regs[RegIndex2], DataSize);
    217217end;
    218218
     
    229229procedure TCpu.InstructionLoadMemSize;
    230230var
    231   DataSize: TBigIntSize;
     231  DataSize: TIntSize;
    232232  RegIndex1: TRegIndex;
    233233  RegIndex2: TRegIndex;
     
    251251procedure TCpu.InstructionStoreMemSize;
    252252var
    253   DataSize: TBigIntSize;
     253  DataSize: TIntSize;
    254254  RegIndex1: TRegIndex;
    255255  RegIndex2: TRegIndex;
     
    265265  RegIndex1: TRegIndex;
    266266  RegIndex2: TRegIndex;
    267   RelativeAddress: TBigInt;
     267  RelativeAddress: TInt;
    268268begin
    269269  RegIndex1 := ReadRegIndex;
     
    275275procedure TCpu.InstructionLoadMemIndexSize;
    276276var
    277   DataSize: TBigIntSize;
     277  DataSize: TIntSize;
    278278  RegIndex1: TRegIndex;
    279279  RegIndex2: TRegIndex;
    280   RelativeAddress: TBigInt;
     280  RelativeAddress: TInt;
    281281begin
    282282  DataSize := ReadSize;
     
    291291  RegIndex1: TRegIndex;
    292292  RegIndex2: TRegIndex;
    293   RelativeAddress: TBigInt;
     293  RelativeAddress: TInt;
    294294begin
    295295  RegIndex1 := ReadRegIndex;
     
    301301procedure TCpu.InstructionStoreMemIndexSize;
    302302var
    303   DataSize: TBigIntSize;
     303  DataSize: TIntSize;
    304304  RegIndex1: TRegIndex;
    305305  RegIndex2: TRegIndex;
    306   RelativeAddress: TBigInt;
     306  RelativeAddress: TInt;
    307307begin
    308308  DataSize := ReadSize;
     
    320320procedure TCpu.InstructionJumpSize;
    321321var
    322   AddressSize: TBigIntSize;
    323 begin
    324   AddressSize := Read(SizeOf(TBigIntSize));
     322  AddressSize: TIntSize;
     323begin
     324  AddressSize := Read(SizeOf(TIntSize));
    325325  PC := Read(AddressSize);
    326326end;
     
    329329var
    330330  RegIndex: TRegIndex;
    331   Address: TBigInt;
     331  Address: TInt;
    332332begin
    333333  RegIndex := ReadRegIndex;
     
    340340var
    341341  RegIndex: TRegIndex;
    342   Address: TBigInt;
    343   DataSize: TBigIntSize;
    344   AddressSize: TBigIntSize;
    345 begin
    346   DataSize := ReadSize;
    347   AddressSize := Read(SizeOf(TBigIntSize));
     342  Address: TInt;
     343  DataSize: TIntSize;
     344  AddressSize: TIntSize;
     345begin
     346  DataSize := ReadSize;
     347  AddressSize := Read(SizeOf(TIntSize));
    348348  RegIndex := ReadRegIndex;
    349349  Address := Read(AddressSize);
    350   if Regs[RegIndex].Copy(DataSize) <> 0 then
     350  if LimitSize(Regs[RegIndex], DataSize) <> 0 then
    351351    PC := Address;
    352352end;
     
    355355var
    356356  RegIndex: TRegIndex;
    357   Address: TBigInt;
     357  Address: TInt;
    358358begin
    359359  RegIndex := ReadRegIndex;
     
    366366var
    367367  RegIndex: TRegIndex;
    368   Address: TBigInt;
    369   DataSize: TBigIntSize;
    370   AddressSize: TBigIntSize;
    371 begin
    372   DataSize := ReadSize;
    373   AddressSize := Read(SizeOf(TBigIntSize));
     368  Address: TInt;
     369  DataSize: TIntSize;
     370  AddressSize: TIntSize;
     371begin
     372  DataSize := ReadSize;
     373  AddressSize := Read(SizeOf(TIntSize));
    374374  RegIndex := ReadRegIndex;
    375375  Address := Read(AddressSize);
    376   if Regs[RegIndex].Copy(DataSize) = 0 then
     376  if LimitSize(Regs[RegIndex], DataSize) = 0 then
    377377    PC := Address;
    378378end;
     
    385385procedure TCpu.InstructionJumpRelSize;
    386386var
    387   AddressSize: TBigIntSize;
     387  AddressSize: TIntSize;
    388388begin
    389389  AddressSize := ReadSize;
     
    399399procedure TCpu.InstructionCallSize;
    400400var
    401   AddressSize: TBigIntSize;
     401  AddressSize: TIntSize;
    402402begin
    403403  AddressSize := ReadSize;
     
    413413procedure TCpu.InstructionRetSize;
    414414var
    415   AddressSize: TBigIntSize;
     415  AddressSize: TIntSize;
    416416begin
    417417  AddressSize := ReadSize;
     
    429429procedure TCpu.InstructionPushSize;
    430430var
    431   DataSize: TBigIntSize;
     431  DataSize: TIntSize;
    432432  RegIndex: TRegIndex;
    433433begin
     
    447447procedure TCpu.InstructionPopSize;
    448448var
    449   DataSize: TBigIntSize;
     449  DataSize: TIntSize;
    450450  RegIndex: TRegIndex;
    451451begin
     
    469469  RegIndex: TRegIndex;
    470470  RegIndex2: TRegIndex;
    471   DataSize: TBigIntSize;
     471  DataSize: TIntSize;
    472472begin
    473473  DataSize := ReadSize;
     
    491491  RegIndex: TRegIndex;
    492492  RegIndex2: TRegIndex;
    493   DataSize: TBigIntSize;
     493  DataSize: TIntSize;
    494494begin
    495495  DataSize := ReadSize;
     
    510510var
    511511  RegIndex: TRegIndex;
    512   DataSize: TBigIntSize;
    513 begin
    514   DataSize := ReadSize;
    515   RegIndex := ReadRegIndex;
    516   Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) + 1;
     512  DataSize: TIntSize;
     513begin
     514  DataSize := ReadSize;
     515  RegIndex := ReadRegIndex;
     516  Regs[RegIndex] := LimitSize(Regs[RegIndex], DataSize) + 1;
    517517end;
    518518
     
    528528var
    529529  RegIndex: TRegIndex;
    530   DataSize: TBigIntSize;
    531 begin
    532   DataSize := ReadSize;
    533   RegIndex := ReadRegIndex;
    534   Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) - 1;
     530  DataSize: TIntSize;
     531begin
     532  DataSize := ReadSize;
     533  RegIndex := ReadRegIndex;
     534  Regs[RegIndex] := LimitSize(Regs[RegIndex], DataSize) - 1;
    535535end;
    536536
     
    547547procedure TCpu.InstructionXorSize;
    548548var
    549   DataSize: TBigIntSize;
    550   RegIndex: TRegIndex;
    551   RegIndex2: TRegIndex;
    552 begin
    553   DataSize := ReadSize;
    554   RegIndex := ReadRegIndex;
    555   RegIndex2 := ReadRegIndex;
    556   Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) xor Regs[RegIndex2].Copy(DataSize);
     549  DataSize: TIntSize;
     550  RegIndex: TRegIndex;
     551  RegIndex2: TRegIndex;
     552begin
     553  DataSize := ReadSize;
     554  RegIndex := ReadRegIndex;
     555  RegIndex2 := ReadRegIndex;
     556  Regs[RegIndex] := LimitSize(Regs[RegIndex] xor Regs[RegIndex2], DataSize);
    557557end;
    558558
     
    569569procedure TCpu.InstructionAndSize;
    570570var
    571   DataSize: TBigIntSize;
    572   RegIndex: TRegIndex;
    573   RegIndex2: TRegIndex;
    574 begin
    575   DataSize := ReadSize;
    576   RegIndex := ReadRegIndex;
    577   RegIndex2 := ReadRegIndex;
    578   Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) and Regs[RegIndex2].Copy(DataSize);
     571  DataSize: TIntSize;
     572  RegIndex: TRegIndex;
     573  RegIndex2: TRegIndex;
     574begin
     575  DataSize := ReadSize;
     576  RegIndex := ReadRegIndex;
     577  RegIndex2 := ReadRegIndex;
     578  Regs[RegIndex] := LimitSize(Regs[RegIndex] and Regs[RegIndex2], DataSize);
    579579end;
    580580
     
    591591procedure TCpu.InstructionOrSize;
    592592var
    593   DataSize: TBigIntSize;
    594   RegIndex: TRegIndex;
    595   RegIndex2: TRegIndex;
    596 begin
    597   DataSize := ReadSize;
    598   RegIndex := ReadRegIndex;
    599   RegIndex2 := ReadRegIndex;
    600   Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) or Regs[RegIndex2].Copy(DataSize);
     593  DataSize: TIntSize;
     594  RegIndex: TRegIndex;
     595  RegIndex2: TRegIndex;
     596begin
     597  DataSize := ReadSize;
     598  RegIndex := ReadRegIndex;
     599  RegIndex2 := ReadRegIndex;
     600  Regs[RegIndex] := LimitSize(Regs[RegIndex] or Regs[RegIndex2], DataSize);
    601601end;
    602602
     
    613613procedure TCpu.InstructionAddSize;
    614614var
    615   DataSize: TBigIntSize;
    616   RegIndex: TRegIndex;
    617   RegIndex2: TRegIndex;
    618 begin
    619   DataSize := ReadSize;
    620   RegIndex := ReadRegIndex;
    621   RegIndex2 := ReadRegIndex;
    622   Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) + Regs[RegIndex2].Copy(DataSize);
     615  DataSize: TIntSize;
     616  RegIndex: TRegIndex;
     617  RegIndex2: TRegIndex;
     618begin
     619  DataSize := ReadSize;
     620  RegIndex := ReadRegIndex;
     621  RegIndex2 := ReadRegIndex;
     622  Regs[RegIndex] := LimitSize(Regs[RegIndex] + Regs[RegIndex2], DataSize);
    623623end;
    624624
     
    635635procedure TCpu.InstructionSubSize;
    636636var
    637   DataSize: TBigIntSize;
    638   RegIndex: TRegIndex;
    639   RegIndex2: TRegIndex;
    640 begin
    641   DataSize := ReadSize;
    642   RegIndex := ReadRegIndex;
    643   RegIndex2 := ReadRegIndex;
    644   Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) + Regs[RegIndex2].Copy(DataSize);
     637  DataSize: TIntSize;
     638  RegIndex: TRegIndex;
     639  RegIndex2: TRegIndex;
     640begin
     641  DataSize := ReadSize;
     642  RegIndex := ReadRegIndex;
     643  RegIndex2 := ReadRegIndex;
     644  Regs[RegIndex] := LimitSize(Regs[RegIndex] + Regs[RegIndex2], DataSize);
    645645end;
    646646
     
    704704end;
    705705
    706 function TCpu.Read(Size: TBigIntSize): TBigInt;
     706function TCpu.Read(Size: TIntSize): TInt;
    707707begin
    708708  Result := Memory.Read(PC, Size);
     
    710710end;
    711711
    712 function TCpu.ReadSize: TBigIntSize;
    713 begin
    714   Result := Read(SizeOf(TBigIntSize));
     712function TCpu.ReadSize: TIntSize;
     713begin
     714  Result := Read(SizeOf(TIntSize));
    715715end;
    716716
     
    720720end;
    721721
    722 procedure TCpu.Write(Size: TBigIntSize; Value: TBigInt);
     722procedure TCpu.Write(Size: TIntSize; Value: TInt);
    723723begin
    724724  Memory.Write(PC, Size, Value);
  • branches/ByteArray/Devices/Device.pas

    r5 r9  
    44
    55uses
    6   Classes, SysUtils, Channel, Forms, BigInt, Common.FormEx;
     6  Classes, SysUtils, Channel, Forms, Int, Common.FormEx, Generics.Collections;
    77
    88type
     
    1212  TFormDevice = class;
    1313
     14  TReadEvent = function (DataSize: TIntSize): TInt of object;
     15  TWriteEvent = procedure (DataSize: TIntSize; Value: TInt) of object;
     16  TReadEvents = TList<TReadEvent>;
     17  TWriteEvents = TList<TWriteEvent>;
     18
     19  { THandlers }
     20
     21  THandlers = class
     22    ReadHandlers: TReadEvents;
     23    WriteHandlers: TWriteEvents;
     24    constructor Create;
     25    destructor Destroy; override;
     26  end;
     27
    1428  { TDevice }
    1529
     
    1832    DeviceClass: TDeviceClass;
    1933    Form: TFormDevice;
    20     BaseAddress: Integer;
    21     function GetAddressCount: Integer; virtual;
    22     procedure SetChannel(Channel: TChannel); virtual;
     34    function GetHandlers: THandlers; virtual;
    2335  end;
    2436
     
    6072end;
    6173
     74{ THandlers }
     75
     76constructor THandlers.Create;
     77begin
     78  ReadHandlers := TReadEvents.Create;
     79  WriteHandlers := TWriteEvents.Create;
     80end;
     81
     82destructor THandlers.Destroy;
     83begin
     84  FreeAndNil(ReadHandlers);
     85  FreeAndNil(WriteHandlers);
     86  inherited;
     87end;
     88
    6289{ TDevice }
    6390
    64 function TDevice.GetAddressCount: Integer;
     91function TDevice.GetHandlers: THandlers;
    6592begin
    66   Result := 0;
    67 end;
    68 
    69 procedure TDevice.SetChannel(Channel: TChannel);
    70 begin
     93  Result := nil;
    7194end;
    7295
  • branches/ByteArray/Devices/DeviceMapper.pas

    r5 r9  
    44
    55uses
    6   Classes, SysUtils, Device, Channel, Generics.Collections, BigInt;
     6  Classes, SysUtils, Device, Channel, Generics.Collections, Int, Math;
    77
    88type
     
    1212  TMappedDevice = class
    1313    Device: TDevice;
    14     Count: Integer;
    15     Channel: TChannel;
     14    ReadCount: Integer;
     15    WriteCount: Integer;
     16    ReadBase: Integer;
     17    WriteBase: Integer;
    1618    constructor Create;
    1719    destructor Destroy; override;
     
    2022  { TDeviceMapper }
    2123
    22   TDeviceMapper = class
    23     FreeBaseAddress: Integer;
     24  TDeviceMapper = class(TChannelDevice)
     25  private
     26    function ChannelRead(Address: TInt; Size: TIntSize): TInt;
     27    procedure ChannelWrite(Address: TInt; Size: TIntSize; Value: TInt);
     28    function ChannelGetSize: TInt;
     29  public
     30    ReadHandlers: TList<TReadEvent>;
     31    WriteHandlers: TList<TWriteEvent>;
    2432    MappedDevices: TObjectList<TMappedDevice>;
    2533    procedure RegisterDevice(Device: TDevice);
    2634    procedure UnregisterDevice(Device: TDevice);
    27     function Read(Address: TBigInt; Size: TBigIntSize): TBigInt;
    28     procedure Write(Address: TBigInt; Size: TBigIntSize; Value: TBigInt);
    29     procedure SetChannel(Channel: TChannel);
     35    procedure SetChannel(Channel: TChannel); override;
    3036    constructor Create;
    3137    destructor Destroy; override;
     
    3945constructor TMappedDevice.Create;
    4046begin
    41   Channel := TChannel.Create;
    4247end;
    4348
    4449destructor TMappedDevice.Destroy;
    4550begin
    46   FreeAndNil(Channel);
    4751  inherited;
    4852end;
     
    5357var
    5458  NewMappedDevice: TMappedDevice;
     59  Handlers: THandlers;
    5560begin
     61  Handlers := Device.GetHandlers;
     62
    5663  NewMappedDevice := TMappedDevice.Create;
    5764  NewMappedDevice.Device := Device;
    58   NewMappedDevice.Device.BaseAddress := FreeBaseAddress;
    59   NewMappedDevice.Count := Device.GetAddressCount;
    60   Device.SetChannel(NewMappedDevice.Channel);
     65  NewMappedDevice.ReadBase := ReadHandlers.Count;
     66  NewMappedDevice.WriteBase := WriteHandlers.Count;
     67  NewMappedDevice.ReadCount := Handlers.ReadHandlers.Count;
     68  NewMappedDevice.WriteCount := Handlers.WriteHandlers.Count;
    6169  MappedDevices.Add(NewMappedDevice);
    62   Inc(FreeBaseAddress, NewMappedDevice.Count);
     70
     71  ReadHandlers.AddRange(Handlers.ReadHandlers);
     72  WriteHandlers.AddRange(Handlers.WriteHandlers);
     73  Handlers.Free;
    6374end;
    6475
     
    7485end;
    7586
    76 function TDeviceMapper.Read(Address: TBigInt; Size: TBigIntSize): TBigInt;
     87function TDeviceMapper.ChannelRead(Address: TInt; Size: TIntSize): TInt;
     88begin
     89  if Address < ReadHandlers.Count then
     90    Result := ReadHandlers[Address](Size)
     91    else Result := 0;
     92end;
     93
     94procedure TDeviceMapper.ChannelWrite(Address: TInt; Size: TIntSize; Value: TInt);
    7795var
    7896  I: Integer;
    7997begin
    80   for I := 0 to MappedDevices.Count - 1 do
    81     if (Integer(Address) >= MappedDevices[I].Device.BaseAddress) and (Integer(Address) >= MappedDevices[I].Device.BaseAddress + MappedDevices[I].Count) then begin
    82       Result := MappedDevices[I].Channel.Read(Integer(Address) - MappedDevices[I].Device.BaseAddress, Size);
    83       Break;
    84     end;
     98  if Address < WriteHandlers.Count then
     99    WriteHandlers[Address](Size, Value);
    85100end;
    86101
    87 procedure TDeviceMapper.Write(Address: TBigInt; Size: TBigIntSize; Value: TBigInt);
    88 var
    89   I: Integer;
     102function TDeviceMapper.ChannelGetSize: TInt;
    90103begin
    91   for I := 0 to MappedDevices.Count - 1 do
    92     if (Integer(Address) >= MappedDevices[I].Device.BaseAddress) and
    93     (Integer(Address) < MappedDevices[I].Device.BaseAddress + MappedDevices[I].Count) then begin
    94       MappedDevices[I].Channel.Write(Integer(Address) - MappedDevices[I].Device.BaseAddress, Size, Value);
    95       Break;
    96     end;
     104  Result := Max(ReadHandlers.Count, WriteHandlers.Count);
    97105end;
    98106
    99107procedure TDeviceMapper.SetChannel(Channel: TChannel);
    100108begin
    101   Channel.Read := Read;
    102   Channel.Write := Write;
     109  Channel.Read := ChannelRead;
     110  Channel.Write := ChannelWrite;
     111  Channel.GetSize := ChannelGetSize;
    103112end;
    104113
     
    106115begin
    107116  MappedDevices := TObjectList<TMappedDevice>.Create;
     117  ReadHandlers := TList<TReadEvent>.Create;
     118  WriteHandlers := TList<TWriteEvent>.Create;
    108119end;
    109120
    110121destructor TDeviceMapper.Destroy;
    111122begin
     123  FreeAndNil(ReadHandlers);
     124  FreeAndNil(WriteHandlers);
    112125  FreeAndNil(MappedDevices);
    113126  inherited;
  • branches/ByteArray/Devices/FrameBuffer.pas

    r5 r9  
    44
    55uses
    6   Classes, SysUtils, Device, Channel, Memory, BigInt;
     6  Classes, SysUtils, Device, Channel, Memory, Int;
    77
    88type
     
    1515    FOnChange: TNotifyEvent;
    1616    procedure DoChange;
     17    function ReadData(Size: TIntSize): TInt;
     18    function ReadPosition(Size: TIntSize): TInt;
     19    function ReadWidth(Size: TIntSize): TInt;
     20    function ReadHeight(Size: TIntSize): TInt;
     21    function ReadMode(Size: TIntSize): TInt;
     22    procedure WriteData(Size: TIntSize; Value: TInt);
     23    procedure WritePosition(Size: TIntSize; Value: TInt);
     24    procedure WriteWidth(Size: TIntSize; Value: TInt);
     25    procedure WriteHeight(Size: TIntSize; Value: TInt);
     26    procedure WriteMode(Size: TIntSize; Value: TInt);
    1727  public
    1828    Memory: TMemory;
     
    2232    Mode: TScreenMode;
    2333    procedure UpdateMode;
    24     function Read(Address: TBigInt; Size: TBigIntSize): TBigInt;
    25     procedure Write(Address: TBigInt; Size: TBigIntSize; Value: TBigInt);
    26     function GetAddressCount: Integer; override;
    27     procedure SetChannel(Channel: TChannel); override;
     34    function GetHandlers: THandlers; override;
    2835    constructor Create;
    2936    destructor Destroy; override;
     
    4148end;
    4249
     50function TFrameBuffer.ReadData(Size: TIntSize): TInt;
     51begin
     52  Result := Memory.Read(Position, Size);
     53  Inc(Position, Size);
     54end;
     55
     56function TFrameBuffer.ReadPosition(Size: TIntSize): TInt;
     57begin
     58  Result := Position;
     59end;
     60
     61function TFrameBuffer.ReadWidth(Size: TIntSize): TInt;
     62begin
     63  Result := Width;
     64end;
     65
     66function TFrameBuffer.ReadHeight(Size: TIntSize): TInt;
     67begin
     68  Result := Height;
     69end;
     70
     71function TFrameBuffer.ReadMode(Size: TIntSize): TInt;
     72begin
     73  Result := Byte(Mode);
     74end;
     75
     76procedure TFrameBuffer.WriteData(Size: TIntSize; Value: TInt);
     77begin
     78  Memory.Write(Position, Size, Value);
     79  Inc(Position, Size);
     80end;
     81
     82procedure TFrameBuffer.WritePosition(Size: TIntSize; Value: TInt);
     83begin
     84  Position := Value;
     85end;
     86
     87procedure TFrameBuffer.WriteWidth(Size: TIntSize; Value: TInt);
     88begin
     89  Width := Value;
     90  UpdateMode;
     91end;
     92
     93procedure TFrameBuffer.WriteHeight(Size: TIntSize; Value: TInt);
     94begin
     95  Height := Value;
     96  UpdateMode;
     97end;
     98
     99procedure TFrameBuffer.WriteMode(Size: TIntSize; Value: TInt);
     100begin
     101  Mode := TScreenMode(Integer(Value));
     102  UpdateMode;
     103end;
     104
    43105procedure TFrameBuffer.UpdateMode;
    44106begin
     
    47109end;
    48110
    49 function TFrameBuffer.Read(Address: TBigInt; Size: TBigIntSize): TBigInt;
     111function TFrameBuffer.GetHandlers: THandlers;
    50112begin
    51   case Integer(Address) of
    52     0: begin
    53       Result := Memory.Read(Position, Size);
    54       Inc(Position, Size);
    55     end;
    56     1: Result := Position;
    57     2: Result := Width;
    58     3: Result := Height;
    59     4: Result := Byte(Mode);
    60   end;
    61 end;
    62 
    63 procedure TFrameBuffer.Write(Address: TBigInt; Size: TBigIntSize; Value: TBigInt);
    64 begin
    65   case Integer(Address) of
    66     0: begin
    67       Memory.Write(Position, Size, Value);
    68       Inc(Position, Size);
    69     end;
    70     1: Position := Value;
    71     2: begin
    72       Width := Value;
    73       UpdateMode;
    74     end;
    75     3: begin
    76       Height := Value;
    77       UpdateMode;
    78     end;
    79     4: begin
    80       Mode := TScreenMode(Integer(Value));
    81       UpdateMode;
    82     end;
    83   end;
    84   DoChange;
    85 end;
    86 
    87 function TFrameBuffer.GetAddressCount: Integer;
    88 begin
    89   Result := 5;
    90 end;
    91 
    92 procedure TFrameBuffer.SetChannel(Channel: TChannel);
    93 begin
    94   Channel.Read := Read;
    95   Channel.Write := Write;
     113  Result := THandlers.Create;
     114  Result.ReadHandlers.Add(ReadData);
     115  Result.ReadHandlers.Add(ReadPosition);
     116  Result.ReadHandlers.Add(ReadWidth);
     117  Result.ReadHandlers.Add(ReadHeight);
     118  Result.ReadHandlers.Add(ReadMode);
     119  Result.WriteHandlers.Add(WriteData);
     120  Result.WriteHandlers.Add(WritePosition);
     121  Result.WriteHandlers.Add(WriteWidth);
     122  Result.WriteHandlers.Add(WriteHeight);
     123  Result.WriteHandlers.Add(WriteMode);
    96124end;
    97125
  • branches/ByteArray/Devices/Memory.pas

    r5 r9  
    44
    55uses
    6   Classes, SysUtils, BigInt, Channel, Device;
     6  Classes, SysUtils, Int, Channel, Device;
    77
    88type
     
    1010  { TMemory }
    1111
    12   TMemory = class(TDevice)
     12  TMemory = class(TChannelDevice)
    1313  private
    14     FSize: Integer;
     14    FSize: TInt;
    1515    FData: PByte;
    16     function GetSize: Integer;
    17     procedure SetSize(AValue: Integer);
     16    function GetSize: TInt;
     17    procedure SetSize(AValue: TInt);
    1818    procedure CheckGrow(Address: Integer);
    1919  public
    20     Position: Integer;
     20    Position: TInt;
    2121    Grow: Boolean;
    2222    procedure Assign(Source: TMemory);
    23     function Read(Address: TBigInt; ASize: TBigIntSize): TBigInt;
    24     function ReadPos(ASize: Byte): TBigInt;
    25     procedure Write(Address: TBigInt; ASize: TBigIntSize; Value: TBigInt);
    26     procedure WritePos(ASize: Byte; Value: TBigInt);
     23    function Read(Address: TInt; ASize: TIntSize): TInt;
     24    function ReadPos(ASize: Byte): TInt;
     25    procedure Write(Address: TInt; ASize: TIntSize; Value: TInt);
     26    procedure WritePos(ASize: Byte; Value: TInt);
    2727    procedure WriteStringPos(Value: string);
    2828    procedure WriteMemoryPos(Memory: TMemory);
    29     function GetAddressCount: Integer; override;
    3029    procedure SetChannel(Channel: TChannel); override;
    3130    procedure SaveToFile(FileName: string);
     31    procedure LoadFromFile(FileName: string);
    3232    procedure FillZero;
    3333    procedure Clear;
    34     property Size: Integer read FSize write SetSize;
     34    property Size: TInt read FSize write SetSize;
    3535    destructor Destroy; override;
    3636  end;
     
    4444{ TMemory }
    4545
    46 function TMemory.GetSize: Integer;
     46function TMemory.GetSize: TInt;
    4747begin
    4848  Result := MemSize(FData);
    4949end;
    5050
    51 procedure TMemory.SetSize(AValue: Integer);
     51procedure TMemory.SetSize(AValue: TInt);
    5252begin
    5353  FSize := AValue;
     
    6868end;
    6969
    70 function TMemory.Read(Address: TBigInt; ASize: TBigIntSize): TBigInt;
     70function TMemory.Read(Address: TInt; ASize: TIntSize): TInt;
    7171begin
    7272  if Address + ASize > FSize then raise Exception.Create(SOutOfRange);
     
    7979end;
    8080
    81 function TMemory.ReadPos(ASize: Byte): TBigInt;
     81function TMemory.ReadPos(ASize: Byte): TInt;
    8282begin
    8383  Result := Read(Position, ASize);
     
    8585end;
    8686
    87 procedure TMemory.Write(Address: TBigInt; ASize: TBigIntSize; Value: TBigInt);
     87procedure TMemory.Write(Address: TInt; ASize: TIntSize; Value: TInt);
    8888begin
    8989  if Address + ASize > FSize then raise Exception.Create(SOutOfRange);
     
    9696end;
    9797
    98 procedure TMemory.WritePos(ASize: Byte; Value: TBigInt);
     98procedure TMemory.WritePos(ASize: Byte; Value: TInt);
    9999begin
    100100  CheckGrow(Position + ASize);
     
    126126end;
    127127
    128 function TMemory.GetAddressCount: Integer;
    129 begin
    130   Result := FSize;
    131 end;
    132 
    133128procedure TMemory.SetChannel(Channel: TChannel);
    134129begin
    135130  Channel.Read := Read;
    136131  Channel.Write := Write;
     132  Channel.GetSize := GetSize;
    137133end;
    138134
     
    168164end;
    169165
     166procedure TMemory.LoadFromFile(FileName: string);
     167var
     168  F: TFileStream;
     169begin
     170  F := TFileStream.Create(FileName, fmOpenRead);
     171  try
     172    if FSize < F.Size then Size := F.Size;
     173    F.Read(FData[0], FSize);
     174  finally
     175    F.Free;
     176  end;
     177end;
     178
    170179end.
    171180
  • branches/ByteArray/Devices/Mouse.pas

    r5 r9  
    11unit Mouse;
    2 
    3 {$mode Delphi}
    42
    53interface
  • branches/ByteArray/Devices/Serial.pas

    r5 r9  
    44
    55uses
    6   Classes, SysUtils, Device, BigInt, Channel;
     6  Classes, SysUtils, Device, Int, Channel;
    77
    88type
     
    1717    FOnWrite: TWriteEvent;
    1818  public
    19     function Read(Address: TBigInt; Size: TBigIntSize): TBigInt;
    20     procedure Write(Address: TBigInt; Size: TBigIntSize; Value: TBigInt);
    21     function GetAddressCount: Integer; override;
    22     procedure SetChannel(Channel: TChannel); override;
     19    function ReadData(Size: TIntSize): TInt;
     20    procedure WriteData(Size: TIntSize; Value: TInt);
     21    function GetHandlers: THandlers; override;
    2322    property OnWrite: TWriteEvent read FOnWrite write FOnWrite;
    2423    property OnRead: TReadEvent read FOnRead write FOnRead;
     
    3029{ TSerial }
    3130
    32 function TSerial.Read(Address: TBigInt; Size: TBigIntSize): TBigInt;
     31function TSerial.ReadData(Size: TIntSize): TInt;
    3332begin
    34   case Integer(Address) of
    35     0: if Assigned(FOnRead) then Result := FOnRead;
    36   end;
     33  if Assigned(FOnRead) then Result := FOnRead;
    3734end;
    3835
    39 procedure TSerial.Write(Address: TBigInt; Size: TBigIntSize; Value: TBigInt);
     36procedure TSerial.WriteData(Size: TIntSize; Value: TInt);
    4037begin
    41   case Integer(Address) of
    42     0: if Assigned(FOnWrite) then FOnWrite(Value);
    43   end;
     38  if Assigned(FOnWrite) then FOnWrite(Value);
    4439end;
    4540
    46 function TSerial.GetAddressCount: Integer;
     41function TSerial.GetHandlers: THandlers;
    4742begin
    48   Result := 1;
    49 end;
    50 
    51 procedure TSerial.SetChannel(Channel: TChannel);
    52 begin
    53   Channel.Read := Read;
    54   Channel.Write := Write;
     43  Result := THandlers.Create;
     44  Result.ReadHandlers.Add(ReadData);
     45  Result.WriteHandlers.Add(WriteData);
    5546end;
    5647
  • branches/ByteArray/Devices/Storage.pas

    r5 r9  
    44
    55uses
    6   Classes, SysUtils, Device, Channel, BigInt;
     6  Classes, SysUtils, Device, Channel, Int;
    77
    88type
     
    1919    FFile: TFileStream;
    2020    Position: Integer;
    21     function ReadByte(Address: TBigInt): Byte;
    22     function Read(Address: TBigInt; Size: TBigIntSize): TBigInt;
    23     procedure Write(Address: TBigInt; Size: TBigIntSize; Value: TBigInt);
    24     function GetAddressCount: Integer; override;
    25     procedure SetChannel(Channel: TChannel); override;
     21    function ReadByte(Address: TInt): Byte;
     22    function ReadData(Size: TIntSize): TInt;
     23    function ReadPosition(Size: TIntSize): TInt;
     24    function ReadSize(Size: TIntSize): TInt;
     25    procedure WriteData(Size: TIntSize; Value: TInt);
     26    procedure WritePosition(Size: TIntSize; Value: TInt);
     27    procedure WriteSize(Size: TIntSize; Value: TInt);
     28    function GetHandlers: THandlers; override;
    2629    constructor Create;
    2730    destructor Destroy; override;
     
    5659end;
    5760
    58 function TStorage.ReadByte(Address: TBigInt): Byte;
     61function TStorage.ReadByte(Address: TInt): Byte;
    5962begin
    6063  Result := 0;
     
    6366end;
    6467
    65 function TStorage.Read(Address: TBigInt; Size: TBigIntSize): TBigInt;
    66 var
    67   Buffer: array of Byte;
     68function TStorage.ReadData(Size: TIntSize): TInt;
    6869begin
    69   case Integer(Address) of
    70     0: begin
    71       SetLength(Buffer, Size);
    72       FFile.Position := Position;
    73       FFile.Read(Buffer[0], Size);
    74       Result.SetByteArray(Buffer, Size);
    75       Inc(Position, Size);
    76     end;
    77     1: Result := Position;
    78     2: Result := FFile.Size;
    79   end;
     70  FFile.Position := Position;
     71  FFile.Read(Result, Size);
     72  Inc(Position, Size);
    8073end;
    8174
    82 procedure TStorage.Write(Address: TBigInt; Size: TBigIntSize; Value: TBigInt);
    83 var
    84   Buffer: array of Byte;
     75function TStorage.ReadPosition(Size: TIntSize): TInt;
    8576begin
    86   case Integer(Address) of
    87     0: begin
    88       SetLength(Buffer, Size);
    89       Value.GetByteArray(Buffer, Size);
    90       FFile.Position := Position;
    91       FFile.Write(Buffer[1], Size);
    92       Inc(Position, Size);
    93     end;
    94     1: Position := Value;
    95     2: FFile.Size := Value;
    96   end;
     77  Result := Position;
    9778end;
    9879
    99 function TStorage.GetAddressCount: Integer;
     80function TStorage.ReadSize(Size: TIntSize): TInt;
    10081begin
    101   Result := 3;
     82  Result := FFile.Size;
    10283end;
    10384
    104 procedure TStorage.SetChannel(Channel: TChannel);
     85procedure TStorage.WriteData(Size: TIntSize; Value: TInt);
    10586begin
    106   Channel.Read := Read;
    107   Channel.Write := Write;
     87  FFile.Position := Position;
     88  FFile.Write(Value, Size);
     89  Inc(Position, Size);
     90end;
     91
     92procedure TStorage.WritePosition(Size: TIntSize; Value: TInt);
     93begin
     94  Position := Value;
     95end;
     96
     97procedure TStorage.WriteSize(Size: TIntSize; Value: TInt);
     98begin
     99  FFile.Size := Value;
     100end;
     101function TStorage.GetHandlers: THandlers;
     102begin
     103  Result := THandlers.Create;
     104  Result.ReadHandlers.Add(ReadData);
     105  Result.ReadHandlers.Add(ReadPosition);
     106  Result.ReadHandlers.Add(ReadSize);
     107  Result.WriteHandlers.Add(WriteData);
     108  Result.WriteHandlers.Add(WritePosition);
     109  Result.WriteHandlers.Add(WriteSize);
    108110end;
    109111
  • branches/ByteArray/Machine.pas

    r5 r9  
    4747  Serial := TSerial.Create;
    4848  DeviceMapper := TDeviceMapper.Create;
     49  DeviceMapper.RegisterDevice(Serial);
    4950  DeviceMapper.RegisterDevice(FrameBuffer);
    5051  DeviceMapper.RegisterDevice(Storage);
    51   DeviceMapper.RegisterDevice(Serial);
    5252  Cpu := TCpu.Create;
    5353  Memory.SetChannel(Cpu.Memory);
  • branches/ByteArray/Packages/Common

    • Property svn:ignore set to
      lib
Note: See TracChangeset for help on using the changeset viewer.