Changeset 9 for branches/ByteArray/Devices/DeviceMapper.pas
- Timestamp:
- Aug 6, 2024, 10:31:16 PM (2 months ago)
- Location:
- branches/ByteArray
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ByteArray
-
Property svn:ignore
set to
lib
heaptrclog.trc
ByteArray
ByteArray.dbg
ByteArray.lps
ByteArray.res
-
Property svn:ignore
set to
-
branches/ByteArray/Devices/DeviceMapper.pas
r5 r9 4 4 5 5 uses 6 Classes, SysUtils, Device, Channel, Generics.Collections, BigInt;6 Classes, SysUtils, Device, Channel, Generics.Collections, Int, Math; 7 7 8 8 type … … 12 12 TMappedDevice = class 13 13 Device: TDevice; 14 Count: Integer; 15 Channel: TChannel; 14 ReadCount: Integer; 15 WriteCount: Integer; 16 ReadBase: Integer; 17 WriteBase: Integer; 16 18 constructor Create; 17 19 destructor Destroy; override; … … 20 22 { TDeviceMapper } 21 23 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>; 24 32 MappedDevices: TObjectList<TMappedDevice>; 25 33 procedure RegisterDevice(Device: TDevice); 26 34 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; 30 36 constructor Create; 31 37 destructor Destroy; override; … … 39 45 constructor TMappedDevice.Create; 40 46 begin 41 Channel := TChannel.Create;42 47 end; 43 48 44 49 destructor TMappedDevice.Destroy; 45 50 begin 46 FreeAndNil(Channel);47 51 inherited; 48 52 end; … … 53 57 var 54 58 NewMappedDevice: TMappedDevice; 59 Handlers: THandlers; 55 60 begin 61 Handlers := Device.GetHandlers; 62 56 63 NewMappedDevice := TMappedDevice.Create; 57 64 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; 61 69 MappedDevices.Add(NewMappedDevice); 62 Inc(FreeBaseAddress, NewMappedDevice.Count); 70 71 ReadHandlers.AddRange(Handlers.ReadHandlers); 72 WriteHandlers.AddRange(Handlers.WriteHandlers); 73 Handlers.Free; 63 74 end; 64 75 … … 74 85 end; 75 86 76 function TDeviceMapper.Read(Address: TBigInt; Size: TBigIntSize): TBigInt; 87 function TDeviceMapper.ChannelRead(Address: TInt; Size: TIntSize): TInt; 88 begin 89 if Address < ReadHandlers.Count then 90 Result := ReadHandlers[Address](Size) 91 else Result := 0; 92 end; 93 94 procedure TDeviceMapper.ChannelWrite(Address: TInt; Size: TIntSize; Value: TInt); 77 95 var 78 96 I: Integer; 79 97 begin 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); 85 100 end; 86 101 87 procedure TDeviceMapper.Write(Address: TBigInt; Size: TBigIntSize; Value: TBigInt); 88 var 89 I: Integer; 102 function TDeviceMapper.ChannelGetSize: TInt; 90 103 begin 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); 97 105 end; 98 106 99 107 procedure TDeviceMapper.SetChannel(Channel: TChannel); 100 108 begin 101 Channel.Read := Read; 102 Channel.Write := Write; 109 Channel.Read := ChannelRead; 110 Channel.Write := ChannelWrite; 111 Channel.GetSize := ChannelGetSize; 103 112 end; 104 113 … … 106 115 begin 107 116 MappedDevices := TObjectList<TMappedDevice>.Create; 117 ReadHandlers := TList<TReadEvent>.Create; 118 WriteHandlers := TList<TWriteEvent>.Create; 108 119 end; 109 120 110 121 destructor TDeviceMapper.Destroy; 111 122 begin 123 FreeAndNil(ReadHandlers); 124 FreeAndNil(WriteHandlers); 112 125 FreeAndNil(MappedDevices); 113 126 inherited;
Note:
See TracChangeset
for help on using the changeset viewer.