OpenCores
URL https://opencores.org/ocsvn/usb_fpga_1_2/usb_fpga_1_2/trunk

Subversion Repositories usb_fpga_1_2

[/] [usb_fpga_1_2/] [trunk/] [java/] [ztex/] [IhxFile.java] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*!
2
   Java Driver API for the ZTEX Firmware Kit
3
   Copyright (C) 2008-2009 ZTEX e.K.
4
   http://www.ztex.de
5
 
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License version 3 as
8
   published by the Free Software Foundation.
9
 
10
   This program is distributed in the hope that it will be useful, but
11
   WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
   General Public License for more details.
14
 
15
   You should have received a copy of the GNU General Public License
16
   along with this program; if not, see http://www.gnu.org/licenses/.
17
!*/
18
 
19
/*
20
    Reads an ihx file
21
*/
22
package ztex;
23
 
24
import java.io.*;
25
import java.util.*;
26
import java.net.*;
27
 
28
public class IhxFile {
29
    public short ihxData[] = new short[65536];
30
 
31
// ******* readHexDigit ********************************************************
32
    private static final int readHexDigit( InputStream in ) throws IOException, IhxParseException {
33
        int b = in.read();
34
        if ( b>=(byte) '0' && b<=(byte) '9' )
35
            return b-(byte) '0';
36
        if ( b>=(byte) 'a' && b<=(byte) 'f' )
37
            return 10+b-(byte) 'a';
38
        if ( b>=(byte) 'A' && b<=(byte) 'F' )
39
            return 10+b-(byte) 'A';
40
        if ( b == -1 )
41
            throw new IhxParseException( "Inexpected end of file" );
42
        throw new IhxParseException( "Hex digit expected: " + (char) b );
43
    }
44
 
45
// ******* readHexByte *********************************************************
46
    private static final int readHexByte(InputStream in) throws IOException, IhxParseException {
47
        return (readHexDigit(in) << 4) | readHexDigit(in);
48
    }
49
 
50
// ******* IhxFile *************************************************************
51
    public IhxFile ( String fileName ) throws IOException, IhxFileDamagedException {
52
        InputStream in = JInputStream.getInputStream( fileName );
53
        int b, len, cs, addr;
54
        byte buf[] = new byte[255];
55
        boolean eof = false;
56
        int line = 0;
57
 
58
        for ( int i=0; i<ihxData.length; i++ )
59
            ihxData[i] = -1;
60
 
61
        try {
62
            while ( ! eof ) {
63
                do      {
64
                    b = in.read();
65
                    if ( b<0 )
66
                        throw new IhxParseException( "Inexpected end of file" );
67
                } while ( b != (byte) ':' );
68
 
69
                line ++;
70
 
71
                len = readHexByte(in);          // length field 
72
                cs = len;
73
 
74
                b = readHexByte(in);            // address field 
75
                cs += b;
76
                addr = b << 8;
77
                b = readHexByte(in);
78
                cs += b;
79
                addr |= b;
80
 
81
                b = readHexByte(in);            // record type field
82
                cs += b;
83
 
84
                for ( int i=0; i<len; i++ ) {    // data
85
                    buf[i] = (byte) readHexByte(in);
86
                    cs+=buf[i];
87
                }
88
 
89
                cs += readHexByte(in);          // checksum
90
                if ( (cs & 0xff) != 0 ) {
91
                    throw new IhxParseException( "Checksum error" );
92
                }
93
 
94
                if ( b == 0 ) {                  // data record
95
                    for (int i=0; i<len; i++ ) {
96
                        if ( ihxData[addr+i]>=0 ) System.err.println ( "Warning: Memory at position " + Integer.toHexString(i) + " overwritten" );
97
                        ihxData[addr+i] = (short) (buf[i] & 255);
98
                    }
99
                }
100
                else if (b == 1 ) {             // eof record
101
                    eof = true;
102
                }
103
                else {
104
                    throw new IhxParseException( "Invalid record type: " + b );
105
                }
106
            }
107
        }
108
        catch ( IhxParseException e ) {
109
            throw new IhxFileDamagedException ( fileName, line, e.getLocalizedMessage() );
110
        }
111
 
112
        try {
113
            in.close();
114
        }
115
        catch ( Exception e ) {
116
            System.err.println( "Warning: Error closing file " + fileName + ": " + e.getLocalizedMessage() );
117
        }
118
    }
119
 
120
// ******* dataInfo ************************************************************
121
    public void dataInfo( PrintStream out ) {
122
        int addr=-1;
123
        for ( int i=0; i<=65536; i++ ) { // data
124
            if ( (i==65536 || ihxData[i]<0) && addr>=0 ) {
125
                System.out.println( i-addr + " Bytes from " + Integer.toHexString(addr) + " to " + Integer.toHexString(i-1) );
126
                addr = -1;
127
            }
128
            if ( i<65536 && ihxData[i]>=0 && addr<0 )
129
                addr = i;
130
        }
131
    }
132
 
133
}
134
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.