Skip to content

Commit 8e7c12a

Browse files
authored
Add files via upload
1 parent 4402366 commit 8e7c12a

File tree

6 files changed

+1134
-0
lines changed

6 files changed

+1134
-0
lines changed

xLoader2/DataBuffer.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
3+
struct DataBuffer {
4+
private byte[] hexStringM;
5+
6+
public int Size {
7+
get { return hexStringM.Length; }
8+
}
9+
10+
public byte this[int index] {
11+
get { return hexStringM[index]; }
12+
}
13+
14+
public byte[] ToByteArray(int startIndex = 0, int length = -1) {
15+
if (length < 0) {
16+
length = hexStringM.Length;
17+
}
18+
19+
byte[] array = new byte[length];
20+
for (int i = 0; i < length; i++) {
21+
array[i] = hexStringM[startIndex+i];
22+
}
23+
24+
return array;
25+
}
26+
27+
static public implicit operator DataBuffer(string text) {
28+
return new DataBuffer(text);
29+
}
30+
31+
public DataBuffer(string text) {
32+
int length = text.Length;
33+
34+
hexStringM = new byte[length/2];
35+
36+
for (int i = 0; i < length; i++) {
37+
int c = text[i];
38+
if (c >= 'A' && c <= 'F') {
39+
c -= ('A'-10);
40+
} else if (c >= 'a' && c <= 'f') {
41+
c -= ('a'-10);
42+
} else if (c >= '0' && c <= '9') {
43+
c -= '0';
44+
} else {
45+
throw new FormatException("Could not find any recognizable digits.");
46+
}
47+
48+
if ((i % 2) == 0) {
49+
hexStringM[i/2] = (byte)(c << 4);
50+
} else {
51+
hexStringM[i/2] |= (byte)c;
52+
}
53+
}
54+
}
55+
}

xLoader2/MemoryChunk.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
3+
struct MemoryChunk {
4+
private byte[] hexStringM;
5+
private int addressM;
6+
7+
public int StartAddress {
8+
get { return addressM; }
9+
}
10+
public int EndAddress {
11+
get { return (addressM + hexStringM.Length - 1); }
12+
}
13+
14+
public byte this[int addr] {
15+
get { return hexStringM[addr-addressM]; }
16+
}
17+
18+
public bool In(int addr) {
19+
return (addr >= addressM && addr < (addressM + hexStringM.Length));
20+
}
21+
22+
public MemoryChunk(int addr, byte[] array) {
23+
int length = array.Length;
24+
25+
hexStringM = new byte[length];
26+
addressM = addr;
27+
28+
for (int i = 0; i < length; i++) {
29+
hexStringM[i] = array[i];
30+
}
31+
}
32+
}

xLoader2/MemoryMap.cs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text;
5+
6+
class MemoryMap {
7+
private List<MemoryChunk> memoryChunkVector = new List<MemoryChunk>();
8+
private int loLimitM = 0x7FFFFFFF;
9+
private int hiLimitM = 0x00000000;
10+
11+
//public int LowestAddress {
12+
//get { return loLimitM; }
13+
//}
14+
//public int HighestAddress {
15+
//get { return hiLimitM; }
16+
//}
17+
public int Size {
18+
get { return (hiLimitM-loLimitM+1); }
19+
}
20+
21+
public byte this[int index] {
22+
get
23+
{
24+
int addr = (loLimitM+index);
25+
foreach (MemoryChunk chunk in memoryChunkVector) {
26+
if (chunk.In(addr)) {
27+
return chunk[addr];
28+
}
29+
}
30+
return 0xFF;
31+
}
32+
}
33+
34+
public MemoryMap(string filename) {
35+
StreamReader inFile;
36+
try {
37+
inFile = new StreamReader(filename, Encoding.ASCII, false);
38+
} catch {
39+
throw new Exception("Couldn't find '" + filename + "'.");
40+
}
41+
42+
try {
43+
int highAddress = 0;
44+
while (!inFile.EndOfStream) {
45+
string s = inFile.ReadLine();
46+
if (s == "") {
47+
continue;
48+
}
49+
if (s[0] != ':') {
50+
throw new Exception("'" + filename + "' is not an Intel HEX formatted file.");
51+
}
52+
53+
DataBuffer line = s.Remove(0, 1);
54+
55+
byte chksum = 0;
56+
for (int i = (line.Size-1); i >= 0; i--) {
57+
chksum += line[i];
58+
}
59+
60+
if (chksum != 0) {
61+
throw new Exception("'" + filename + "' is damaged.");
62+
}
63+
64+
byte length = line[0];
65+
int address = ((line[1] << 8) | line[2]);
66+
byte type = line[3];
67+
68+
switch (type) {
69+
case 0:
70+
MemoryChunk chunk = new MemoryChunk((highAddress+address), line.ToByteArray(4, length));
71+
for (int i = chunk.StartAddress; i <= chunk.EndAddress; i++) {
72+
foreach (MemoryChunk chunk2 in memoryChunkVector) {
73+
if (chunk2.In(i)) {
74+
throw new Exception("Overlapping memory assignments in file '" + filename + "'.");
75+
}
76+
}
77+
}
78+
memoryChunkVector.Add(chunk);
79+
if (loLimitM > chunk.StartAddress) {
80+
loLimitM = chunk.StartAddress;
81+
}
82+
if (hiLimitM < chunk.EndAddress) {
83+
hiLimitM = chunk.EndAddress;
84+
}
85+
break;
86+
87+
case 1: return;
88+
case 2: highAddress = ((line[4] << 12) | (line[5] << 4)); break;
89+
case 3: break;
90+
case 4: highAddress = ((line[4] << 24) | (line[5] << 16)); break;
91+
case 5: break;
92+
default: throw new Exception("'" + filename + "' is damaged.");
93+
}
94+
}
95+
} finally {
96+
inFile.Close();
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)