-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.cs
242 lines (181 loc) · 5.89 KB
/
memory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
internal class Memory
{
private readonly byte[] _m;
public Memory(uint size)
{
_m = new byte[size];
}
public byte ReadByte(uint address)
{
if (address >= _m.Length)
{
#if DEBUG
Log.DoLog($"Memory::ReadByte: {address} > {_m.Length}");
#endif
return 0xee;
}
return _m[address];
}
public void WriteByte(uint address, byte v)
{
if (address < _m.Length)
_m[address] = v;
}
}
internal class Rom
{
private readonly byte[] _contents;
private Dictionary <uint, string> _annotations = new();
private Dictionary <uint, string> _scripts = new();
public Rom(string filename)
{
_contents = File.ReadAllBytes(filename);
string annotations_file = filename + ".ann";
if (File.Exists(annotations_file))
{
Console.WriteLine($"Loading annotations file {annotations_file}");
int n = 0;
using(StreamReader sr = File.OpenText(annotations_file))
{
for(;;)
{
string s = sr.ReadLine();
if (s == null)
break;
int pipe = s.IndexOf("|");
if (pipe == -1)
continue;
string key = s.Substring(0, pipe);
string val = s.Substring(pipe + 1);
string scr = null;
uint key_uint = Convert.ToUInt32(key, 16);
int val_pipe = val.IndexOf("|");
if (val_pipe != -1)
{
scr = val.Substring(val_pipe + 1);
_scripts[key_uint] = scr;
val = val.Substring(0, val_pipe);
}
_annotations[key_uint] = val;
n++;
// Console.WriteLine($"{n} {key_uint} {val}");
}
}
Console.WriteLine($"Loaded {n} annotations for {filename}");
}
}
public uint GetSize()
{
return (uint)_contents.Length;
}
public string GetAnnotation(uint address)
{
if (_annotations.ContainsKey(address))
return _annotations[address];
return null;
}
public string GetScript(uint address)
{
if (_scripts.ContainsKey(address))
return _scripts[address];
return null;
}
public byte ReadByte(uint address)
{
if (address < _contents.Length)
return _contents[address];
#if DEBUG
Log.DoLog($"Rom::ReadByte: {address} > {_contents.Length}");
#endif
return 0xee;
}
}
class Bus
{
private Memory _m;
// private readonly Rom _bios = new("roms/BIOS_5160_16AUG82_U18_5000026.BIN");
private readonly Rom _bios = new("roms/glabios/GLaBIOS/src/GLABIOS.ROM");
// private readonly Rom _bios = new("roms/ruuds_diagnostic_rom_32kb_2022-12-12.bin");
// private readonly Rom _bios = new("roms/DiagROM/DiagROM");
// private readonly Rom _bios = new("roms/diagrom201212/DIAGROMV2.bin");
// private readonly Rom _bios = new("roms/Supersoft_PCXT_8KB.bin");
private readonly Rom _basic = new("roms/BIOS_5160_16AUG82_U19_5000027.BIN");
// private readonly Rom _bios = new("roms/GLABIOS_0.2.4_8T.ROM");
// private readonly Rom _bios = new("roms/GLABIOS_0.2.4_8P.ROM");
// private readonly Rom _bios = new("roms/BIOS_5160_09MAY86_U18_59X7268_62X0890_27256_F800.BIN");
// private readonly Rom _basic = new("roms/BIOS_5160_09MAY86_U19_62X0819_68X4370_27256_F000.BIN");
private List<Device> _devices;
private bool _use_bios;
private uint _size;
private uint _bios_base;
public Bus(uint size, bool use_bios, ref List<Device> devices)
{
_size = size;
_m = new Memory(size);
_use_bios = use_bios;
_bios_base = 0x00100000 - _bios.GetSize();
_devices = devices;
}
public void ClearMemory()
{
_m = new Memory(_size);
}
public string GetAnnotation(uint address)
{
address &= 0x000fffff;
if (address < 640 * 1024 || _use_bios == false)
return null;
if (address >= _bios_base && address <= 0x000fffff)
return _bios.GetAnnotation(address);
if (address is >= 0x000f0000 and <= 0x000f7fff)
return _basic.GetAnnotation(address);
return null;
}
public string GetScript(uint address)
{
address &= 0x000fffff;
if (address < 640 * 1024 || _use_bios == false)
return null;
if (address >= _bios_base && address <= 0x000fffff)
return _bios.GetScript(address);
if (address is >= 0x000f0000 and <= 0x000f7fff)
return _basic.GetScript(address);
return null;
}
public byte ReadByte(uint address)
{
address &= 0x000fffff;
if (_use_bios)
{
if (address >= _bios_base && address <= 0x000fffff)
return _bios.ReadByte(address - _bios_base);
if (address is >= 0x000f0000 and <= 0x000f7fff)
return _basic.ReadByte(address - 0x000f0000);
}
foreach(var device in _devices)
{
if (device.HasAddress(address))
return device.ReadByte(address);
}
if (address < 1024 * 1024)
return _m.ReadByte(address);
#if DEBUG
Log.DoLog($"Bus::ReadByte: {address} > {_size}");
#endif
return 0xee;
}
public void WriteByte(uint address, byte v)
{
address &= 0x000fffff;
foreach(var device in _devices)
{
if (device.HasAddress(address))
{
device.WriteByte(address, v);
return;
}
}
if (address < 1024 * 1024)
_m.WriteByte(address, v);
}
}