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

Subversion Repositories neo430

[/] [neo430/] [trunk/] [neo430/] [sw/] [tools/] [image_gen/] [main.cpp] - Blame information for rev 198

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 198 zero_gravi
// #################################################################################################
2
// #  < NEO430 Boot image generator >                                                              #
3
// # ********************************************************************************************* #
4
// # BSD 3-Clause License                                                                          #
5
// #                                                                                               #
6
// # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
7
// #                                                                                               #
8
// # Redistribution and use in source and binary forms, with or without modification, are          #
9
// # permitted provided that the following conditions are met:                                     #
10
// #                                                                                               #
11
// # 1. Redistributions of source code must retain the above copyright notice, this list of        #
12
// #    conditions and the following disclaimer.                                                   #
13
// #                                                                                               #
14
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
15
// #    conditions and the following disclaimer in the documentation and/or other materials        #
16
// #    provided with the distribution.                                                            #
17
// #                                                                                               #
18
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
19
// #    endorse or promote products derived from this software without specific prior written      #
20
// #    permission.                                                                                #
21
// #                                                                                               #
22
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
23
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
24
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
25
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
26
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
27
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
28
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
29
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
30
// # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
31
// # ********************************************************************************************* #
32
// # The NEO430 Processor - https://github.com/stnolting/neo430                                    #
33
// #################################################################################################
34
 
35
#include <stdint.h>
36
#include <stdio.h>
37
#include <stdlib.h>
38
#include <string.h>
39
 
40
int main(int argc, char *argv[]) {
41
 
42
  if (argc != 4) {
43
        printf("<<< NEO430 executable image generator >>>\n"
44
               "Three arguments are required.\n"
45
               "1st: Option\n"
46
               " -app_bin : Generate application executable binary (with header!) \n"
47
               " -app_img : Generate application raw executable memory image (text file, no header!)\n"
48
               " -bld_img : Generate bootloader raw executable memory image (text file, no header!)\n"
49
                     "2nd: Input file (raw binary image)\n"
50
                     "3rd: Output file\n");
51
        return 0;
52
  }
53
 
54
  FILE *input, *output;
55
  unsigned char buffer[2];
56
  char tmp_string[512];
57
  uint16_t tmp = 0, size = 0, checksum = 0;
58
  int i = 0;
59
  int option = 0;
60
 
61
  if (strcmp(argv[1], "-app_bin") == 0)
62
    option = 1;
63
  else if (strcmp(argv[1], "-app_img") == 0)
64
    option = 2;
65
  else if (strcmp(argv[1], "-bld_img") == 0)
66
    option = 3;
67
  else {
68
        printf("Invalid option!");
69
        return 2;
70
  }
71
 
72
  // open input file
73
  input = fopen(argv[2], "rb");
74
  if(input == NULL){
75
    printf("Input file error!");
76
    return 3;
77
  }
78
 
79
  // open output file
80
  output = fopen(argv[3], "wb");
81
  if(output == NULL){
82
    printf("Output file error!");
83
    return 4;
84
  }
85
 
86
 
87
// ------------------------------------------------------------
88
// Generate BINARY executable (with header!!!) for bootloader upload
89
// ------------------------------------------------------------
90
  if (option == 1) {
91
    // reserve space for header
92
    fputc(char(0), output);
93
    fputc(char(0), output);
94
    fputc(char(0), output);
95
    fputc(char(0), output);
96
    fputc(char(0), output);
97
    fputc(char(0), output);
98
 
99
    buffer[0] = 0;
100
    buffer[1] = 0;
101
 
102
    checksum = 0;
103
    size = 0;
104
    rewind(input);
105
    while(fread(&buffer, sizeof(unsigned char), 2, input) != 0) {
106
      tmp = (((uint16_t)buffer[1] << 8) | buffer[0]);
107
      checksum = checksum ^ tmp;
108
      fputc(buffer[1], output);
109
      fputc(buffer[0], output);
110
      size += 2;
111
    }
112
 
113
    // build header
114
    rewind(output);
115
    fputc((unsigned char)(0xCA), output);
116
    fputc((unsigned char)(0xFE), output);
117
    fputc((unsigned char)((size & 0xFF00) >> 8), output);
118
    fputc((unsigned char)((size & 0x00FF) >> 0), output);
119
    fputc((unsigned char)((checksum & 0xFF00) >> 8), output);
120
    fputc((unsigned char)((checksum & 0x00FF) >> 0), output);
121
  }
122
 
123
 
124
// ------------------------------------------------------------
125
// Generate APPLICATION's executable memory init file (no header!!!)
126
// ------------------------------------------------------------
127
  if (option == 2) {
128
 
129
        // header
130
    sprintf(tmp_string, "-- The NEO430 Processor Project, by Stephan Nolting\r\n"
131
                                                "-- Auto-generated memory init file (for APPLICATION)\r\n"
132
                                                "\r\n"
133
                                                "library ieee;\r\n"
134
                                                "use ieee.std_logic_1164.all;\r\n"
135
                                                "\r\n"
136
                                                "package neo430_application_image is\r\n"
137
                                                "\r\n"
138
                                                "  type application_init_image_t is array (0 to 65535) of std_ulogic_vector(15 downto 0);\r\n"
139
                                                "  constant application_init_image : application_init_image_t := (\r\n");
140
    fputs(tmp_string, output);
141
 
142
        // data
143
    buffer[0] = 0;
144
    buffer[1] = 0;
145
    i = 0;
146
    while(fread(&buffer, sizeof(unsigned char), 2, input) != 0) {
147
      tmp = (((uint16_t)buffer[1] << 8) | buffer[0]);
148
      sprintf(tmp_string, "    %06d => x\"%04x\",\r\n", i, tmp);
149
      fputs(tmp_string, output);
150
      buffer[0] = 0;
151
      buffer[1] = 0;
152
      i++;
153
    }
154
 
155
    sprintf(tmp_string, "    others => x\"0000\"\r\n");
156
    fputs(tmp_string, output);
157
 
158
        // end
159
    sprintf(tmp_string, "  );\r\n"
160
                                                "\r\n"
161
                                                "end neo430_application_image;\r\n");
162
    fputs(tmp_string, output);
163
  }
164
 
165
 
166
// ------------------------------------------------------------
167
// Generate BOOTLOADER's executable memory init file (no header!!!)
168
// ------------------------------------------------------------
169
  if (option == 3) {
170
 
171
        // header
172
    sprintf(tmp_string, "-- The NEO430 Processor Project, by Stephan Nolting\r\n"
173
                                                "-- Auto-generated memory init file (for BOOTLOADER)\r\n"
174
                                                "\r\n"
175
                                                "library ieee;\r\n"
176
                                                "use ieee.std_logic_1164.all;\r\n"
177
                                                "\r\n"
178
                                                "package neo430_bootloader_image is\r\n"
179
                                                "\r\n"
180
                                                "  type bootloader_init_image_t is array (0 to 65535) of std_ulogic_vector(15 downto 0);\r\n"
181
                                                "  constant bootloader_init_image : bootloader_init_image_t := (\r\n");
182
    fputs(tmp_string, output);
183
 
184
        // data
185
    buffer[0] = 0;
186
    buffer[1] = 0;
187
    i = 0;
188
    while(fread(&buffer, sizeof(unsigned char), 2, input) != 0) {
189
      tmp = (((uint16_t)buffer[1] << 8) | buffer[0]);
190
      sprintf(tmp_string, "    %06d => x\"%04x\",\r\n", i, tmp);
191
      fputs(tmp_string, output);
192
      buffer[0] = 0;
193
      buffer[1] = 0;
194
      i++;
195
    }
196
 
197
    sprintf(tmp_string, "    others => x\"0000\"\r\n");
198
    fputs(tmp_string, output);
199
 
200
        // end
201
    sprintf(tmp_string, "  );\r\n"
202
                                                "\r\n"
203
                                                "end neo430_bootloader_image;\r\n");
204
    fputs(tmp_string, output);
205
  }
206
 
207
 
208
  fclose(input);
209
  fclose(output);
210
 
211
  return 0;
212
}
213
 

powered by: WebSVN 2.1.0

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