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

Subversion Repositories yacc

[/] [yacc/] [trunk/] [tool_source_by_tak/] [convert_mips.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 tak.sugawa
//convert.c by Steve Rhoads 4/26/01
2
//Now uses the ELF format (get gccmips_elf.zip) 
3
//set $gp and zero .sbss and .bss
4
//May.15.2004 add code0.hex code1.hex code2.hex code3.hex 8bitIntelHex File Generation by Tak.Sugawara
5
//Jan.6.2005 add Xilinx *.coe file code0.coe, code1.coe,code2.coe,code3.coe
6
//Apr.3.2005 -sp16k flag addition
7
//Apr.5.2005 -mif file addition
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <string.h>
11
 
12
#define BUF_SIZE (1024*1024)
13
/*Assumes running on PC little endian*/
14
#define ntohl(A) (((A)>>24)|(((A)&0x00ff0000)>>8)|(((A)&0xff00)<<8)|((A)<<24))
15
#define ntohs(A) ((((A)&0xff00)>>8)|((A)<<8))
16
 
17
#define EI_NIDENT 16
18
#define SHT_PROGBITS 1
19
#define SHT_STRTAB 3
20
#define SHT_NOBITS 8
21
 
22
typedef struct {
23
   unsigned char  e_ident[EI_NIDENT];
24
   unsigned short e_e_type;
25
   unsigned short e_machine;
26
   unsigned long  e_version;
27
   unsigned long  e_entry;
28
   unsigned long  e_phoff;
29
   unsigned long  e_shoff;
30
   unsigned long  e_flags;
31
   unsigned short e_ehsize;
32
   unsigned short e_phentsize;
33
   unsigned short e_phnum;
34
   unsigned short e_shentsize;
35
   unsigned short e_shnum;
36
   unsigned short e_shstrndx;
37
} ElfHeader;
38
 
39
typedef struct {
40
   unsigned long p_type;
41
   unsigned long p_offset;
42
   unsigned long p_vaddr;
43
   unsigned long p_paddr;
44
   unsigned long p_filesz;
45
   unsigned long p_memsz;
46
   unsigned long p_flags;
47
   unsigned long p_align;
48
} Elf32_Phdr;
49
 
50
typedef struct {
51
   unsigned long sh_name;
52
   unsigned long sh_type;
53
   unsigned long sh_flags;
54
   unsigned long sh_addr;
55
   unsigned long sh_offset;
56
   unsigned long sh_size;
57
   unsigned long sh_link;
58
   unsigned long sh_info;
59
   unsigned long sh_addralign;
60
   unsigned long sh_entsize;
61
} Elf32_Shdr;
62
 
63
#if 0
64
unsigned long load(unsigned char *ptr,unsigned long address)
65
{
66
   unsigned long value;
67
   value=*(unsigned long*)(ptr+address);
68
   value=ntohl(value);
69
   return value;
70
}
71
 
72
unsigned short load_short(unsigned char *ptr,unsigned long address)
73
{
74
   return (ptr[address]<<8)+ptr[address+1];
75
}
76
#endif
77
 
78
void set_low(char *ptr,unsigned long address,unsigned long value)
79
{
80
   unsigned long opcode;
81
   opcode=*(unsigned long*)(ptr+address);
82
   opcode=ntohl(opcode);
83
   opcode=(opcode&0xffff0000)|(value&0xffff);
84
   opcode=ntohl(opcode);
85
   *(unsigned long*)(ptr+address)=opcode;
86
}
87
 
88
int main(int argc,char *argv[])
89
{
90
   FILE *infile,*outfile,*txtfile;
91
   unsigned char *buf,*code;
92
   long size,stack_pointer;
93
   unsigned long length,d,i,gp_ptr=0;
94
   unsigned long bss_start=0,bss_end=0;
95
 char filename[80];//TAK May.10.2004
96
 char filename_coe[80];//TAK Jan.6.2005
97
 char filename_mif[80];//TAK Apr.5.2005
98
   FILE *file;//TAK May.10.2004
99
   FILE *file_coe;//TAK Jan.6.2005
100
   FILE *file_mif;
101
   unsigned long j,k,sum;
102
   int m;
103
   ElfHeader *elfHeader;
104
   Elf32_Phdr *elfProgram;
105
   Elf32_Shdr *elfSection;
106
   int stack_ptr16KB_flag=0;
107
   char* ptr=argv[1];
108
   if (argc==2){
109
                if(!strcmp( argv[1],"-sp16k")){
110
                                stack_ptr16KB_flag=1;
111
                }
112
   }
113
   printf("test.exe -> code.txt & test2.exe\n");
114
   infile=fopen("test.exe","rb");
115
   if(infile==NULL) {
116
      printf("Can't open test.exe");
117
      return 0;
118
   }
119
   buf=(unsigned char*)malloc(BUF_SIZE);
120
   size=fread(buf,1,BUF_SIZE,infile);
121
   fclose(infile);
122
   code=(unsigned char*)malloc(BUF_SIZE);
123
   memset(code,0,BUF_SIZE);
124
 
125
   elfHeader=(ElfHeader*)buf;
126
   if(strncmp(elfHeader->e_ident+1,"ELF",3)) {
127
      printf("Error:  Not an ELF file!\n");
128
      printf("Use the gccmips_elf.zip from opencores/projects/plasma!\n");
129
      return -1;
130
   }
131
 
132
   elfHeader->e_entry=ntohl(elfHeader->e_entry);
133
   elfHeader->e_phoff=ntohl(elfHeader->e_phoff);
134
   elfHeader->e_shoff=ntohl(elfHeader->e_shoff);
135
   elfHeader->e_phentsize=ntohs(elfHeader->e_phentsize);
136
   elfHeader->e_phnum=ntohs(elfHeader->e_phnum);
137
   elfHeader->e_shentsize=ntohs(elfHeader->e_shentsize);
138
   elfHeader->e_shnum=ntohs(elfHeader->e_shnum);
139
   length=0;
140
 
141
   for(i=0;i<elfHeader->e_phnum;++i) {
142
      elfProgram=(Elf32_Phdr*)(buf+elfHeader->e_phoff+elfHeader->e_phentsize*i);
143
      elfProgram->p_offset=ntohl(elfProgram->p_offset);
144
      elfProgram->p_vaddr=ntohl(elfProgram->p_vaddr);
145
      elfProgram->p_filesz=ntohl(elfProgram->p_filesz);
146
      elfProgram->p_memsz=ntohl(elfProgram->p_memsz);
147
//      printf("[0x%x,0x%x,0x%x]\n",elfProgram->p_vaddr,elfProgram->p_offset,elfProgram->p_filesz);
148
      memcpy(code+elfProgram->p_vaddr,buf+elfProgram->p_offset,elfProgram->p_filesz);
149
      length=elfProgram->p_vaddr+elfProgram->p_memsz;
150
   }
151
 
152
   for(i=0;i<elfHeader->e_shnum;++i) {
153
      elfSection=(Elf32_Shdr*)(buf+elfHeader->e_shoff+elfHeader->e_shentsize*i);
154
      elfSection->sh_name=ntohl(elfSection->sh_name);
155
      elfSection->sh_type=ntohl(elfSection->sh_type);
156
      elfSection->sh_addr=ntohl(elfSection->sh_addr);
157
      elfSection->sh_offset=ntohl(elfSection->sh_offset);
158
      elfSection->sh_size=ntohl(elfSection->sh_size);
159
#if 0
160
      printf("{0x%x,0x%x:0x%x,0x%x,0x%x}\n",
161
         elfSection->sh_name,elfSection->sh_type,elfSection->sh_addr,
162
         elfSection->sh_offset,elfSection->sh_size);
163
#endif
164
#if 0
165
      if(elfSection->sh_type==SHT_PROGBITS||elfSection->sh_type==SHT_STRTAB) {
166
//         memcpy(code+elfSection->sh_addr,buf+elfSection->sh_offset,elfSection->sh_size);
167
         length=elfSection->sh_addr+elfSection->sh_size;
168
         bss_start=length;
169
      }
170
#endif
171
      if(elfSection->sh_type==SHT_PROGBITS) {
172
         gp_ptr=elfSection->sh_addr;
173
      }
174
      if(elfSection->sh_type==SHT_NOBITS) {
175
         if(bss_start==0) {
176
            bss_start=elfSection->sh_addr;
177
         }
178
         bss_end=elfSection->sh_addr+elfSection->sh_size;
179
      }
180
   }
181
 
182
   if(bss_start==length) {
183
      bss_start=length;
184
      bss_end=length+4;
185
   }
186
 
187
   /*Initialize the $gp register for sdata and sbss*/
188
   gp_ptr+=0x7ff0;
189
   printf("gp_ptr=0x%x ",gp_ptr);
190
   /*modify the first opcodes in boot.asm*/
191
   /*modify the lui opcode*/
192
   set_low(code,0,gp_ptr>>16);
193
   /*modify the ori opcode*/
194
   set_low(code,4,gp_ptr&0xffff);
195
 
196
   /*Clear .sbss and .bss*/
197
   printf(".sbss=0x%x .bss_end=0x%x\n",bss_start,bss_end);
198
   set_low(code,8,bss_start>>16);
199
   set_low(code,12,bss_start&0xffff);
200
   set_low(code,16,bss_end>>16);
201
   set_low(code,20,bss_end&0xffff);
202
   /*Set stack pointer*/
203
   stack_pointer=bss_end+512;
204
   if(stack_ptr16KB_flag) stack_pointer=0x3f80;//Apr.4.2005
205
   printf("Stack pointer=0x%x\n",stack_pointer);
206
 
207
   set_low(code,24,stack_pointer>>16);//TAK
208
   set_low(code,28,stack_pointer&0xffff);//TAK
209
   /*write out code.txt*/
210
   outfile=fopen("test2.exe","wb");
211
   fwrite(code,length,1,outfile);
212
   fclose(outfile);
213
 
214
   txtfile=fopen("code.txt","w");
215
   for(i=0;i<=length;i+=4) {
216
      d=ntohl(*(unsigned long*)(code+i));
217
      fprintf(txtfile,"%8.8x\n",d);
218
   }
219
   fclose(txtfile);
220
   //TAK May.10.2004 
221
    strcpy(filename,"codeX.hex");
222
    strcpy(filename_coe,"codeX.coe");
223
        strcpy(filename_mif,"ram1kX.mif");
224
   for(i=0;i<4;++i) {
225
      filename[4]='0'+i;
226
      filename_coe[4]='0'+i;//Jan.6.2005
227
          filename_mif[5]='0'+i;
228
      file=fopen(filename,"wb");
229
      file_coe=fopen(filename_coe,"wb");//Jan.6.2005
230
          file_mif=fopen(filename_mif,"wb");
231
          fprintf(file_coe,"memory_initialization_radix=16;\n");//Jan.6.2005
232
          fprintf(file_coe,"memory_initialization_vector=\n");//Jan.6.2005
233
 
234
      for(j=0;i+j*4*16<length;++j) {
235
         k=j*16;
236
         fprintf(file,":10%4.4x00",k);
237
         sum=0x10+(k>>8)+(k&0xff);
238
         for(k=0;k<16;++k) {
239
            fprintf(file,"%2.2x",code[i+j*4*16+k*4]);
240
            fprintf(file_coe,"%2.2x",code[i+j*4*16+k*4]);//Jan.6.2005
241
                        for (m=7;m>=0;m--){
242
                                fprintf(file_mif,"%1x",(code[i+j*4*16+k*4] >> m) & 1);
243
                        }
244
                        fprintf(file_mif,"\n");
245
                        if (k==15 && (j+1)*4*16>=length) fprintf(file_coe,";\n");//Jan.6.2005
246
                        else fprintf(file_coe,",\n");//Jan.6.2005
247
            sum+=code[i+j*4*16+k*4];
248
 
249
         }
250
         sum&=0xff;
251
         sum=0x100-sum;
252
         sum&=0xff;
253
         fprintf(file,"%2.2x\n",sum);
254
      }
255
      fprintf(file,":00000001ff\n");
256
      fclose(file);
257
      fclose(file_coe);//Jan.6.2005
258
          fclose(file_mif);
259
   }
260
 
261
 
262
 
263
 
264
 
265
 
266
 
267
 
268
 
269
 
270
   free(buf);
271
 
272
   return 0;
273
}
274
 

powered by: WebSVN 2.1.0

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