source: trunk/Memory.pas

Last change on this file was 2, checked in by chronos, 16 months ago
  • Added: Initial development version.
File size: 4.1 KB
Line 
1unit Memory;
2
3interface
4
5uses
6 Classes, SysUtils, Generics.Collections;
7
8type
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
68implementation
69
70{ TMemoryMappedAreas }
71
72function TMemoryMappedAreas.AddNew(Base: Word; Memory: TMemory): TMemoryMappedArea;
73begin
74 Result := TMemoryMappedArea.Create;
75 Result.Base := Base;
76 Result.Memory := Memory;
77 Add(Result);
78end;
79
80{ TMemoryMappedArea }
81
82function TMemoryMappedArea.InRange(Address: Word): Boolean;
83begin
84 Result := (Address >= Base) and (Address < (Base + Memory.Size));
85end;
86
87{ TMemoryMapped }
88
89function TMemoryMapped.GetSize: Integer;
90begin
91 Result := FSize;
92end;
93
94procedure TMemoryMapped.SetSize(AValue: Integer);
95begin
96 FSize := AValue;
97end;
98
99function TMemoryMapped.Read(Address: Word): Byte;
100var
101 I: Integer;
102begin
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;
108end;
109
110procedure TMemoryMapped.Write(Address: Word; Data: Byte);
111var
112 I: Integer;
113begin
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;
119end;
120
121constructor TMemoryMapped.Create;
122begin
123 inherited;
124 Areas := TMemoryMappedAreas.Create;
125end;
126
127destructor TMemoryMapped.Destroy;
128begin
129 FreeAndNil(Areas);
130 inherited;
131end;
132
133{ TMemoryData }
134
135function TMemoryData.GetSize: Integer;
136begin
137 Result := MemSize(DataPtr);
138end;
139
140procedure TMemoryData.SetSize(AValue: Integer);
141begin
142 ReAllocMem(DataPtr, AValue);
143end;
144
145function TMemoryData.Read(Address: Word): Byte;
146begin
147 Result := PByte(DataPtr + Address)^;
148end;
149
150procedure TMemoryData.Write(Address: Word; Data: Byte);
151begin
152 PByte(DataPtr + Address)^ := Data;
153end;
154
155procedure TMemoryData.LoadFromFile(FileName: string);
156var
157 FileStream: TFileStream;
158begin
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;
166end;
167
168{ TMemory }
169
170function TMemory.GetSize: Integer;
171begin
172 Result := 0;
173end;
174
175procedure TMemory.SetSize(AValue: Integer);
176begin
177
178end;
179
180function TMemory.Read(Address: Word): Byte;
181begin
182 Result := 0;
183end;
184
185procedure TMemory.Write(Address: Word; Data: Byte);
186begin
187end;
188
189function TMemory.ReadByte: Byte;
190begin
191 if Position < Size then Result := Read(Position)
192 else Result := 0;
193 Position := Position + SizeOf(Byte);
194end;
195
196function TMemory.ReadWord: Word;
197begin
198 Result := Read(Position) or (Read(Position + 1) shl 8);
199 Position := Position + SizeOf(Word);
200end;
201
202procedure TMemory.LoadFromFile(FileName: string);
203begin
204end;
205
206destructor TMemory.Destroy;
207begin
208 Size := 0;
209 inherited;
210end;
211
212end.
213
Note: See TracBrowser for help on using the repository browser.