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

Subversion Repositories System09

[/] [System09/] [trunk/] [Tools/] [Mot2Hex/] [mot2hex.c] - Blame information for rev 170

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

Line No. Rev Author Line
1 62 davidgb
/*
2
 * Motorola S1 format to Intel hex format
3
 * Usage
4
 * mot2hex <file_name>
5
 */
6
 
7
#include <stdio.h>
8
#include <string.h>
9
 
10
 
11
int gethex( FILE *fp_in )
12
{
13
    int hex;
14
 
15
    hex = fgetc( fp_in );
16
    if( (hex >= '0') && (hex <= '9' ))
17
        hex -= '0';
18
    else if( (hex >= 'A') && (hex <= 'F'))
19
        hex = hex - 'A' + 10;
20
    else
21
        hex = -1;
22
    return hex;
23
}
24
 
25
int get2hex( FILE *fp_in )
26
{
27
    int hexhi, hexlo, byte;
28
 
29
    hexhi = gethex( fp_in );
30
    if( hexhi != -1 )
31
    {
32
        hexlo = gethex( fp_in );
33
        if( hexlo != -1 )
34
        {
35
            byte = hexhi * 16 + hexlo;
36
            return byte;
37
        }
38
    }
39
    return -1;
40
}
41
 
42
int get4hex( FILE *fp_in )
43
{
44
    int bytehi, bytelo, addr;
45
 
46
    bytehi = get2hex( fp_in );
47
    if( bytehi != -1 )
48
    {
49
        bytelo = get2hex( fp_in );
50
        if( bytelo != -1 )
51
        {
52
            addr = (bytehi * 256) + bytelo;
53
            return addr;
54
        }
55
    }
56
    return -1;
57
}
58
 
59
 
60
main( int argc, char *argv[] )
61
{
62
        FILE *fp_in, *fp_out;
63
        char fname_in[32];
64
        char fname_out[32];
65
        int byte, addr, start, i;
66
        int motorola_check, intel_check;
67
        int motorola_count, intel_count;
68
 
69
        if( argc != 2 )
70
        {
71
            printf( "\n usage: mot2hex <file_name> \n" );
72
            exit(0);
73
        }
74
        sprintf( fname_in, "%s.s19", argv[1] );
75
        fp_in = fopen( fname_in, "r" );
76
        if( !fp_in )
77
        {
78
            sprintf( fname_in, "%s.s1", argv[1] );
79
            fp_in = fopen( fname_in, "r" );
80
            if( !fp_in )
81
            {
82
                printf( "Can't open %s", fname_in );
83
                exit(0);
84
            }
85
        }
86
        sprintf( fname_out, "%s.hex", argv[1] );
87
        fp_out = fopen( fname_out, "w" );
88
        if( !fp_out )
89
        {
90
            printf( "Can't open %s", fname_out );
91
            exit(0);
92
        }
93
        byte = 0;
94
        addr = 0;
95
        start = -1;
96
 
97
        while( byte != -1 )
98
        {
99
            /*
100
             * Motorola 8 bit record starts with "S1"
101
             */
102
            do {
103
                byte = fgetc( fp_in);
104
            } while( (byte != 'S') && (byte != -1) );
105
 
106
            byte = fgetc( fp_in );
107
            if( byte == '1' )
108
            {
109
                /*
110
                 * get byte count from Motorola record
111
                 */
112
                motorola_count = get2hex( fp_in );
113
                motorola_check = motorola_count;
114
                /*
115
                 * Intel byte count is for data field only
116
                 * round up to even byte boundary
117
                 */
118
                intel_count = motorola_count - 3;
119
                intel_check = intel_count;
120
                /*
121
                 * Get two byte motorola address field
122
                 */
123
                addr = get4hex( fp_in );
124
                motorola_check += (addr & 0xff);
125
                motorola_check += (addr & 0xff00) >> 8;
126
                /*
127
                 * output intel start of record
128
                 * ":" <data count> <address> <record type>
129
                 */
130
                intel_check += (addr & 0xff);
131
                intel_check += (addr & 0xff00) >> 8;
132
                fprintf( fp_out, ":%02x%04x00", intel_count, addr );
133
                /*
134
                 * Input Motorola data field
135
                 */
136
                for( i=0; i<intel_count; i++ )
137
                {
138
                    byte = get2hex( fp_in );
139
                    motorola_check += byte;
140
                    /*
141
                     * Output Intel data field
142
                     */
143
                    intel_check += byte;
144
                    fprintf( fp_out, "%02x", byte );
145
                    /*
146
                     * Get the start of record from the reset vector
147
                     */
148
                    if( addr == 0xfffe )
149
                        start = byte * 256;
150
                    if( addr == 0xffff )
151
                        start += byte;
152
                    addr ++;
153
                }
154
                /*
155
                 * Display Motorola cehcksum for current record
156
                 * should be 0xff
157
                 */
158
                byte = get2hex( fp_in) & 0xff;
159
                motorola_check += byte;
160
                motorola_check &= 0xff;
161
                printf( " Motorola checksum = %02x   ", motorola_check );
162
                /*
163
                 * Display Intel checksum
164
                 */
165
                intel_check &= 0xff;
166
                intel_check = 0x100 - intel_check;
167
                intel_check &= 0xff;
168
                printf( "Intel checksum = %02x \n", intel_check );
169
                /*
170
                 * Output Intel checksum
171
                 */
172
                fprintf( fp_out, "%02x\n", intel_check);
173
                if( addr > 0xffff )
174
                   byte = -1;
175
             }
176
        }
177
        /*
178
         * output Intel trasfer address
179
         */
180
        if( start != -1 )
181
        {
182
            fprintf( fp_out, ":00%04x01", start );
183
        }
184
        /*
185
         * Close input and output files and exit
186
         */
187
        close( fp_in );
188
        close( fp_out );
189
}

powered by: WebSVN 2.1.0

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