Subversion Repositories HomeAutomation

Rev

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