Skip to content

Commit 2700d89

Browse files
committed
Extend Binary writer by input and output range, extend hex2bin example
1 parent c0e8dad commit 2700d89

File tree

6 files changed

+1637
-117
lines changed

6 files changed

+1637
-117
lines changed

IntelHex.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<WarningLevel>4</WarningLevel>
1919
<Externalconsole>true</Externalconsole>
2020
<PlatformTarget>x86</PlatformTarget>
21+
<Commandlineparameters></Commandlineparameters>
2122
</PropertyGroup>
2223
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
2324
<DebugType>full</DebugType>
@@ -43,6 +44,7 @@
4344
<Compile Include="IntelHex\Listeners\RangeDetector.cs" />
4445
<Compile Include="IntelHex\Listeners\BinWriter.cs" />
4546
<Compile Include="Program.cs" />
47+
<Compile Include="Mono.Options\Options.cs" />
4648
</ItemGroup>
4749
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
4850
<ItemGroup>

IntelHex/Listeners/BinWriter.cs

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,52 +38,67 @@ namespace IntelHex.Listeners
3838
public class BinWriter : IDataListener
3939
{
4040

41+
Region inputRegion;
42+
Region outputRegion;
4143

42-
private Region outputRegion;
43-
private Stream destination;
44-
private byte[] buffer;
45-
private MemoryRegions regions;
46-
private uint maxAddress;
47-
private bool minimize;
44+
Stream destination;
45+
byte[] buffer;
46+
MemoryRegions writtenRegions;
47+
Region written;
4848

49-
public BinWriter (Region outputRegion, Stream destination, bool minimize)
49+
50+
public BinWriter (Region inputRegion, Region outputRegion, byte fill, Stream destination)
5051
{
52+
this.inputRegion = inputRegion;
5153
this.outputRegion = outputRegion;
5254
this.destination = destination;
53-
this.minimize = minimize;
54-
this.buffer = Enumerable.Repeat((byte)0xFF, (int)outputRegion.GetLength ()).ToArray();
55-
regions = new MemoryRegions ();
56-
maxAddress = outputRegion.GetAddressStart ();
55+
56+
buffer = Enumerable.Repeat(fill, (int)outputRegion.GetLength ()).ToArray();
57+
writtenRegions = new MemoryRegions ();
58+
written = new Region (0, 0);
59+
60+
if (!this.outputRegion.HasExactStart ()) {
61+
this.outputRegion.SetAddressStart (this.inputRegion.GetAddressStart ());
62+
}
63+
if (!this.outputRegion.HasExactEnd ()) {
64+
this.outputRegion.SetAddressEnd ((uint)this.inputRegion.GetAddressEnd ());
65+
}
5766
}
5867

5968
public void Data (uint address, byte[] data)
6069
{
61-
regions.Add (address, (uint)data.Length);
62-
63-
if ((address >= outputRegion.GetAddressStart ()) && (address <= outputRegion.GetAddressEnd ())) {
64-
int length = data.Length;
65-
if ((address + length) > outputRegion.GetAddressEnd ()) {
66-
length = (int)(outputRegion.GetAddressEnd () - address + 1);
67-
}
68-
Array.Copy (data, 0, buffer, (int)(address - outputRegion.GetAddressStart ()), length);
70+
var current = new Region (address, (uint)data.Length);
71+
current.Intersection (inputRegion);
72+
current.Intersection (outputRegion);
6973

70-
if (maxAddress < (address + data.Length - 1)) {
71-
maxAddress = address + (uint)data.Length - 1;
72-
}
74+
if (current.GetLength () > 0) {
75+
long sourceIndex = current.GetAddressStart () - address;
76+
long destinationIndex = address - outputRegion.GetAddressStart();
77+
Array.Copy (data, (int)sourceIndex, buffer, (int)destinationIndex, current.GetLength());
78+
writtenRegions.Add (current);
7379
}
7480
}
7581

7682
public void Eof ()
77-
{
78-
if (!minimize) {
79-
maxAddress = (uint)outputRegion.GetAddressEnd ();
83+
{
84+
UpdateWritten ();
85+
destination.Write (buffer, 0, (int)written.GetLength());
86+
}
87+
88+
void UpdateWritten ()
89+
{
90+
written = writtenRegions.GetFullRangeRegion ();
91+
if (outputRegion.HasExactStart ()) {
92+
written.SetAddressStart (outputRegion.GetAddressStart ());
93+
}
94+
if (outputRegion.HasExactEnd ()) {
95+
written.SetAddressEnd ((uint)outputRegion.GetAddressEnd ());
8096
}
81-
destination.Write (buffer, 0, (int)(maxAddress - outputRegion.GetAddressStart () + 1));
8297
}
8398

84-
public MemoryRegions GetMemoryRegions ()
99+
public Region GetWrittenRegion ()
85100
{
86-
return regions;
101+
return written;
87102
}
88103
}
89104
}

IntelHex/MemoryRegions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ public class MemoryRegions
3838

3939
private List<Region> regions = new List<Region> ();
4040

41+
/// <summary>
42+
/// Add region to list
43+
/// </summary>
44+
/// <param name="region">Region.</param>
45+
public void Add (Region region)
46+
{
47+
Add (region.GetAddressStart (), region.GetLength ());
48+
}
49+
4150
/// <summary>
4251
/// Add region to list
4352
/// </summary>

IntelHex/Region.cs

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,68 @@ namespace IntelHex
3434
public class Region : IComparable
3535
{
3636

37-
private uint addressStart;
38-
private uint addressEnd;
37+
uint addressStart;
38+
uint addressEnd;
39+
bool exactStart;
40+
bool exactEnd;
41+
42+
public Region (string range)
43+
{
44+
string [] region = range == null ? new string [0] : range.Split (':');
45+
if (region.Length == 0) {
46+
exactStart = ReadAddress ("min", out addressStart);
47+
exactEnd = ReadAddress ("max", out addressEnd);
48+
} else if (region.Length == 1) {
49+
exactStart = ReadAddress (region [0], out addressStart);
50+
exactEnd = ReadAddress ("max", out addressEnd);
51+
} else {
52+
exactStart = ReadAddress (region [0], out addressStart);
53+
exactEnd = ReadAddress (region [1] != null && region [1] != "" ? region [1] : "max", out addressEnd);
54+
}
55+
}
3956

4057
public Region (uint start, uint length)
4158
{
42-
this.addressStart = start;
43-
this.addressEnd = start + length - 1;
59+
addressStart = start;
60+
addressEnd = start + length - 1;
61+
exactStart = true;
62+
exactEnd = true;
4463
}
4564

65+
static bool ReadAddress (string s, out uint result)
66+
{
67+
if (s != null && s != "") {
68+
if (s == "min") {
69+
result = 0;
70+
return false;
71+
}
72+
if (s == "max") {
73+
result = 0xFFFFFFFF;
74+
return false;
75+
}
76+
if (s.StartsWith ("0x", StringComparison.InvariantCultureIgnoreCase)) {
77+
result = Convert.ToUInt32 (s.Substring (2), 16);
78+
} else {
79+
result = Convert.ToUInt32 (s);
80+
}
81+
return true;
82+
}
83+
result = 0;
84+
return false;
85+
}
86+
87+
4688
/// <summary>
4789
/// Get length of the region
4890
/// </summary>
4991
/// <returns>The length.</returns>
5092
public uint GetLength ()
5193
{
52-
return addressEnd - addressStart + 1;
94+
if (addressStart <= addressEnd) {
95+
return addressEnd - addressStart + 1;
96+
}
97+
98+
return 0;
5399
}
54100

55101
/// <summary>
@@ -68,6 +114,7 @@ public long GetAddressEnd ()
68114
public void SetAddressEnd (uint addressEnd)
69115
{
70116
this.addressEnd = addressEnd;
117+
this.exactEnd = true;
71118
}
72119

73120
/// <summary>
@@ -86,6 +133,7 @@ public uint GetAddressStart ()
86133
public void SetAddressStart (uint addressStart)
87134
{
88135
this.addressStart = addressStart;
136+
this.exactStart = true;
89137
}
90138

91139
/// <summary>
@@ -97,6 +145,39 @@ public void IncLength (uint value)
97145
addressEnd += value;
98146
}
99147

148+
149+
public bool HasExactStart ()
150+
{
151+
return exactStart;
152+
}
153+
154+
public void SetExactStart (bool value)
155+
{
156+
exactStart = value;
157+
}
158+
159+
/// <summary>
160+
/// Hases the exact end.
161+
/// </summary>
162+
/// <returns>Wether the end address was exactly defined or calculated</returns>
163+
public bool HasExactEnd ()
164+
{
165+
return exactEnd;
166+
}
167+
168+
public void SetExactEnd (bool value)
169+
{
170+
exactEnd = value;
171+
}
172+
173+
public void Intersection (Region other)
174+
{
175+
addressStart = addressStart > other.addressStart ? addressStart : other.addressStart;
176+
addressEnd = addressEnd < other.addressEnd ? addressEnd : other.addressEnd;
177+
//exactStart = exactStart | other.exactStart;
178+
//exactEnd = exactEnd | other.exactEnd;
179+
}
180+
100181
/// <summary>
101182
/// Convert region to string
102183
/// </summary>

0 commit comments

Comments
 (0)