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