Subversion Repositories HomeAutomation

Rev

Rev 450 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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