1 | unit Memory;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Generics.Collections;
|
---|
7 |
|
---|
8 | type
|
---|
9 |
|
---|
10 | { TMemory }
|
---|
11 |
|
---|
12 | TMemory = class
|
---|
13 | private
|
---|
14 | function GetSize: Integer; virtual;
|
---|
15 | procedure SetSize(AValue: Integer); virtual;
|
---|
16 | public
|
---|
17 | Position: Integer;
|
---|
18 | function Read(Address: Word): Byte; virtual;
|
---|
19 | procedure Write(Address: Word; Data: Byte); virtual;
|
---|
20 | function ReadByte: Byte;
|
---|
21 | function ReadWord: Word;
|
---|
22 | procedure LoadFromFile(FileName: string); virtual;
|
---|
23 | destructor Destroy; override;
|
---|
24 | property Size: Integer read GetSize write SetSize;
|
---|
25 | end;
|
---|
26 |
|
---|
27 | { TMemoryData }
|
---|
28 |
|
---|
29 | TMemoryData = class(TMemory)
|
---|
30 | DataPtr: PByte;
|
---|
31 | function GetSize: Integer; override;
|
---|
32 | procedure SetSize(AValue: Integer); override;
|
---|
33 | function Read(Address: Word): Byte; override;
|
---|
34 | procedure Write(Address: Word; Data: Byte); override;
|
---|
35 | procedure LoadFromFile(FileName: string); override;
|
---|
36 | end;
|
---|
37 |
|
---|
38 | { TMemoryMappedArea }
|
---|
39 |
|
---|
40 | TMemoryMappedArea = class
|
---|
41 | Base: Word;
|
---|
42 | Memory: TMemory;
|
---|
43 | function InRange(Address: Word): Boolean;
|
---|
44 | end;
|
---|
45 |
|
---|
46 | { TMemoryMappedAreas }
|
---|
47 |
|
---|
48 | TMemoryMappedAreas = class(TObjectList<TMemoryMappedArea>)
|
---|
49 | function AddNew(Base: Word; Memory: TMemory): TMemoryMappedArea;
|
---|
50 | end;
|
---|
51 |
|
---|
52 | { TMemoryMapped }
|
---|
53 |
|
---|
54 | TMemoryMapped = class(TMemory)
|
---|
55 | private
|
---|
56 | FSize: Integer;
|
---|
57 | public
|
---|
58 | Areas: TMemoryMappedAreas;
|
---|
59 | function GetSize: Integer; override;
|
---|
60 | procedure SetSize(AValue: Integer); override;
|
---|
61 | function Read(Address: Word): Byte; override;
|
---|
62 | procedure Write(Address: Word; Data: Byte); override;
|
---|
63 | constructor Create;
|
---|
64 | destructor Destroy; override;
|
---|
65 | end;
|
---|
66 |
|
---|
67 |
|
---|
68 | implementation
|
---|
69 |
|
---|
70 | { TMemoryMappedAreas }
|
---|
71 |
|
---|
72 | function TMemoryMappedAreas.AddNew(Base: Word; Memory: TMemory): TMemoryMappedArea;
|
---|
73 | begin
|
---|
74 | Result := TMemoryMappedArea.Create;
|
---|
75 | Result.Base := Base;
|
---|
76 | Result.Memory := Memory;
|
---|
77 | Add(Result);
|
---|
78 | end;
|
---|
79 |
|
---|
80 | { TMemoryMappedArea }
|
---|
81 |
|
---|
82 | function TMemoryMappedArea.InRange(Address: Word): Boolean;
|
---|
83 | begin
|
---|
84 | Result := (Address >= Base) and (Address < (Base + Memory.Size));
|
---|
85 | end;
|
---|
86 |
|
---|
87 | { TMemoryMapped }
|
---|
88 |
|
---|
89 | function TMemoryMapped.GetSize: Integer;
|
---|
90 | begin
|
---|
91 | Result := FSize;
|
---|
92 | end;
|
---|
93 |
|
---|
94 | procedure TMemoryMapped.SetSize(AValue: Integer);
|
---|
95 | begin
|
---|
96 | FSize := AValue;
|
---|
97 | end;
|
---|
98 |
|
---|
99 | function TMemoryMapped.Read(Address: Word): Byte;
|
---|
100 | var
|
---|
101 | I: Integer;
|
---|
102 | begin
|
---|
103 | for I := 0 to Areas.Count - 1 do
|
---|
104 | if Areas[I].InRange(Address) then begin
|
---|
105 | Result := Areas[I].Memory.Read(Address - Areas[I].Base);
|
---|
106 | Break;
|
---|
107 | end;
|
---|
108 | end;
|
---|
109 |
|
---|
110 | procedure TMemoryMapped.Write(Address: Word; Data: Byte);
|
---|
111 | var
|
---|
112 | I: Integer;
|
---|
113 | begin
|
---|
114 | for I := 0 to Areas.Count - 1 do
|
---|
115 | if Areas[I].InRange(Address) then begin
|
---|
116 | Areas[I].Memory.Write(Address - Areas[I].Base, Data);
|
---|
117 | Break;
|
---|
118 | end;
|
---|
119 | end;
|
---|
120 |
|
---|
121 | constructor TMemoryMapped.Create;
|
---|
122 | begin
|
---|
123 | inherited;
|
---|
124 | Areas := TMemoryMappedAreas.Create;
|
---|
125 | end;
|
---|
126 |
|
---|
127 | destructor TMemoryMapped.Destroy;
|
---|
128 | begin
|
---|
129 | FreeAndNil(Areas);
|
---|
130 | inherited;
|
---|
131 | end;
|
---|
132 |
|
---|
133 | { TMemoryData }
|
---|
134 |
|
---|
135 | function TMemoryData.GetSize: Integer;
|
---|
136 | begin
|
---|
137 | Result := MemSize(DataPtr);
|
---|
138 | end;
|
---|
139 |
|
---|
140 | procedure TMemoryData.SetSize(AValue: Integer);
|
---|
141 | begin
|
---|
142 | ReAllocMem(DataPtr, AValue);
|
---|
143 | end;
|
---|
144 |
|
---|
145 | function TMemoryData.Read(Address: Word): Byte;
|
---|
146 | begin
|
---|
147 | Result := PByte(DataPtr + Address)^;
|
---|
148 | end;
|
---|
149 |
|
---|
150 | procedure TMemoryData.Write(Address: Word; Data: Byte);
|
---|
151 | begin
|
---|
152 | PByte(DataPtr + Address)^ := Data;
|
---|
153 | end;
|
---|
154 |
|
---|
155 | procedure TMemoryData.LoadFromFile(FileName: string);
|
---|
156 | var
|
---|
157 | FileStream: TFileStream;
|
---|
158 | begin
|
---|
159 | FileStream := TFileStream.Create(FileName, fmOpenRead);
|
---|
160 | try
|
---|
161 | Size := FileStream.Size;
|
---|
162 | FileStream.Read(DataPtr^, FileStream.Size);
|
---|
163 | finally
|
---|
164 | FreeAndNil(FileStream);
|
---|
165 | end;
|
---|
166 | end;
|
---|
167 |
|
---|
168 | { TMemory }
|
---|
169 |
|
---|
170 | function TMemory.GetSize: Integer;
|
---|
171 | begin
|
---|
172 | Result := 0;
|
---|
173 | end;
|
---|
174 |
|
---|
175 | procedure TMemory.SetSize(AValue: Integer);
|
---|
176 | begin
|
---|
177 |
|
---|
178 | end;
|
---|
179 |
|
---|
180 | function TMemory.Read(Address: Word): Byte;
|
---|
181 | begin
|
---|
182 | Result := 0;
|
---|
183 | end;
|
---|
184 |
|
---|
185 | procedure TMemory.Write(Address: Word; Data: Byte);
|
---|
186 | begin
|
---|
187 | end;
|
---|
188 |
|
---|
189 | function TMemory.ReadByte: Byte;
|
---|
190 | begin
|
---|
191 | if Position < Size then Result := Read(Position)
|
---|
192 | else Result := 0;
|
---|
193 | Position := Position + SizeOf(Byte);
|
---|
194 | end;
|
---|
195 |
|
---|
196 | function TMemory.ReadWord: Word;
|
---|
197 | begin
|
---|
198 | Result := Read(Position) or (Read(Position + 1) shl 8);
|
---|
199 | Position := Position + SizeOf(Word);
|
---|
200 | end;
|
---|
201 |
|
---|
202 | procedure TMemory.LoadFromFile(FileName: string);
|
---|
203 | begin
|
---|
204 | end;
|
---|
205 |
|
---|
206 | destructor TMemory.Destroy;
|
---|
207 | begin
|
---|
208 | Size := 0;
|
---|
209 | inherited;
|
---|
210 | end;
|
---|
211 |
|
---|
212 | end.
|
---|
213 |
|
---|