Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
450 arune 1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.IO;
5
using System.Globalization;
6
 
7
public class HexFile {
8
	private const ulong USER_RESET_ADDR = 0; //0x1000;	//0x1000 för PIC??
9
	private const ulong BUFFER_SIZE = 5200000;
10
	private const ulong LINE_MAX_SIZE = 1024;
11
	private const ulong ERROR_BUFFER_SIZE = 256;
12
	private const ulong FILE_PATH_SIZE = 256;
13
 
14
	private ulong length = 0;
15
	private ulong addrLower = 0;
16
	private ulong addrUpper = 0;
17
	private string filepath = "";
18
	private bool valid = false;
19
	private byte[] buff = new byte[BUFFER_SIZE];
20
 
21
	public HexFile() { }
22
 
23
	public bool isValid() { return valid; }
24
 
25
	public bool loadHex(string filepath) {
26
		// Populate buffer
27
 
28
		byte lineDataCount = 0;
29
		ulong lineAddrHigh = 0;
30
		ulong lineAddr = 0;
31
		byte lineCode = 0;
32
		ulong fullAddr = 0;
33
 
34
		this.filepath = filepath;
35
		this.length = 0;
36
		this.addrLower = 0x7FFFFFFF;
37
		this.addrUpper = 0;
38
		this.valid = false;
39
 
40
		// Prime the buffer
41
		for (ulong i = 0; i < BUFFER_SIZE; i++) buff[i] = 0xFF;
42
 
43
 
44
		// Try opening file
45
		if (!File.Exists(this.filepath)) {
46
			// The file could not be opened
47
			Console.WriteLine("Error in HexFile: Unable to load file.");
48
			return false;
49
		}
50
		else {
51
			FileStream file = new FileStream(this.filepath, FileMode.Open, FileAccess.Read);
52
			StreamReader sr = new StreamReader(file);
53
 
54
			// Safely use the file stream
55
 
56
			// For each line, read until eof.
57
			while (!sr.EndOfStream) {
58
				// read one line
59
				string oneLine = sr.ReadLine();
60
 
61
				// If valid line
62
				if (!oneLine.Equals("") && oneLine[0]==':') {
63
					lineDataCount = byte.Parse(oneLine.Substring(1,2), NumberStyles.HexNumber);
64
					lineAddr =  ulong.Parse(oneLine.Substring(3,4),NumberStyles.HexNumber) & 65535;	 
65
					lineCode = byte.Parse(oneLine.Substring(7,2), NumberStyles.HexNumber);   
66
					fullAddr = (lineAddrHigh * 65536) + lineAddr;
67
 
68
					switch(lineCode) {
69
							case 0:
70
								this.length +=lineDataCount;
71
								if ((fullAddr + lineDataCount - 1) > this.addrUpper) this.addrUpper = fullAddr + lineDataCount - 1;
72
								if (lineCode == 0 && fullAddr < this.addrLower) this.addrLower = fullAddr;
73
								for (int j = 0; j < lineDataCount; j++) {
74
									buff[fullAddr] = byte.Parse(oneLine.Substring(j * 2 + 9, 2), System.Globalization.NumberStyles.HexNumber);
75
									buff[fullAddr] &= (byte)255;
76
									fullAddr++;
77
								}
78
								break;
79
							case 4:
80
								lineAddrHigh = ulong.Parse(oneLine.Substring(9, 4), System.Globalization.NumberStyles.HexNumber) & 65535;
81
								break;
82
							case 1: break; 
83
 
84
					}
85
					//Console.WriteLine("lineDataCount: " + lineCode + " lineAddr: " + lineAddr + " lineCode: " + lineCode + " addrLower: " + this.addrLower);
86
				}
87
			}
88
 
89
			this.valid = true;
90
			sr.Close();
91
		}
92
 
93
		// Force lower addr to start
94
		//this.addrLower = USER_RESET_ADDR;
95
 
96
		return true;
97
	}
98
 
99
 
100
	public ulong getAddrUpper() { return addrUpper; }
101
 
102
	public ulong getAddrLower() { return addrLower; }
103
 
104
	public ulong getLength() { return length; }
105
 
106
	public byte getByte(ulong addr)
107
	{
108
		if (addr>=BUFFER_SIZE) return 0xFF;
109
		return buff[addr];
110
	}
111
 
112
}