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

Subversion Repositories altor32

[/] [altor32/] [trunk/] [rtl/] [sim/] [main.cpp] - Blame information for rev 27

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 ultra_embe
//-----------------------------------------------------------------
2
//                           AltOR32 
3
//                Alternative Lightweight OpenRisc 
4
//                            V2.0
5
//                     Ultra-Embedded.com
6
//                   Copyright 2011 - 2013
7
//
8
//               Email: admin@ultra-embedded.com
9
//
10
//                       License: LGPL
11
//-----------------------------------------------------------------
12
//
13
// Copyright (C) 2011 - 2013 Ultra-Embedded.com
14
//
15
// This source file may be used and distributed without         
16
// restriction provided that this copyright statement is not    
17
// removed from the file and that any derivative work contains  
18
// the original copyright notice and the associated disclaimer. 
19
//
20
// This source file is free software; you can redistribute it   
21
// and/or modify it under the terms of the GNU Lesser General   
22
// Public License as published by the Free Software Foundation; 
23
// either version 2.1 of the License, or (at your option) any   
24
// later version.
25
//
26
// This source is distributed in the hope that it will be       
27
// useful, but WITHOUT ANY WARRANTY; without even the implied   
28
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      
29
// PURPOSE.  See the GNU Lesser General Public License for more 
30
// details.
31
//
32
// You should have received a copy of the GNU Lesser General    
33
// Public License along with this source; if not, write to the 
34
// Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
35
// Boston, MA  02111-1307  USA
36
//-----------------------------------------------------------------
37
#include <stdio.h>
38
#include <unistd.h>
39
 
40
#include "top_if.h"
41
#include "verilated.h"
42
 
43
#ifdef INCLUDE_ELF_SUPPORT
44
#include <libelf.h>
45
#include <fcntl.h>
46
#include <gelf.h>
47
#endif
48
 
49
//-----------------------------------------------------------------
50
// Defines
51
//-----------------------------------------------------------------
52
#define MEM_BASE             0x10000000
53
#define MEM_EXEC_OFFSET      0x100
54
 
55
//-----------------------------------------------------------------
56
// Locals
57
//-----------------------------------------------------------------
58
 
59
//-----------------------------------------------------------------
60
// elf_load
61
//-----------------------------------------------------------------
62
#ifdef INCLUDE_ELF_SUPPORT
63
static int elf_load(const char *filename, unsigned int *startAddr)
64
{
65
    int fd;
66
    Elf * e;
67
    Elf_Kind ek;
68
    Elf_Scn *scn;
69
    Elf_Data *data;
70
    Elf32_Shdr *shdr;
71
    size_t shstrndx;
72
 
73
    if (elf_version ( EV_CURRENT ) == EV_NONE)
74
        return 0;
75
 
76
    if ((fd = open ( filename , O_RDONLY , 0)) < 0)
77
        return 0;
78
 
79
    if ((e = elf_begin ( fd , ELF_C_READ, NULL )) == NULL)
80
        return 0;
81
 
82
    ek = elf_kind ( e );
83
    if (ek != ELF_K_ELF)
84
        return 0;
85
 
86
    // Get section name header index
87
    if (elf_getshdrstrndx(e, &shstrndx)!=0)
88
        return 0;
89
 
90
    int section_idx = 0;
91
    while ((scn = elf_getscn(e, section_idx)) != NULL)
92
    {
93
        shdr = elf32_getshdr(scn);
94
 
95
        // Section which need loading (.text, .bss, .data, etc)
96
        if ((shdr->sh_type == SHT_PROGBITS || shdr->sh_type == SHT_NOBITS) &&
97
            (shdr->sh_flags & SHF_EXECINSTR || shdr->sh_flags & SHF_ALLOC)
98
           )
99
        {
100
            data = elf_getdata(scn, NULL);
101
 
102
            // .text section?
103
            if (elf_strptr(e, shstrndx, shdr->sh_name) && startAddr &&
104
                strcmp(elf_strptr(e, shstrndx, shdr->sh_name), ".text") == 0)
105
            {
106
                *startAddr = shdr->sh_addr + MEM_EXEC_OFFSET;
107
            }
108
 
109
            printf("Memory: 0x%x - 0x%x (Size=%dKB) [%s]\n", shdr->sh_addr, shdr->sh_addr + shdr->sh_size - 1, shdr->sh_size / 1024, elf_strptr(e, shstrndx, shdr->sh_name));
110
 
111
            if (shdr->sh_type == SHT_PROGBITS)
112
            {
113
                int i;
114
                for (i=0;i<shdr->sh_size;i++)
115
                    top_load(shdr->sh_addr + i, ((unsigned char*)data->d_buf)[i]);
116
            }
117
        }
118
 
119
        section_idx++;
120
    }
121
 
122
    elf_end ( e );
123
    close ( fd );
124
 
125
    return 1;
126
}
127
#endif
128
 
129
//-----------------------------------------------------------------
130
// main
131
//-----------------------------------------------------------------
132
int main(int argc, char **argv, char **env)
133
{
134
    int c;
135
    int err;
136
    unsigned int loadAddr = MEM_BASE;
137
    unsigned int execAddr = MEM_BASE + MEM_EXEC_OFFSET;
138
    unsigned int breakAddr = 0xFFFFFFFF;
139
    char *filename = NULL;
140
    int help = 0;
141
    int exitcode = 0;
142
    int cycles = -1;
143
    int intr_after = -1;
144
 
145
    Verilated::commandArgs(argc, argv);
146
 
147
    while ((c = getopt (argc, argv, "f:l:c:i:b:e:")) != -1)
148
    {
149
        switch(c)
150
        {
151
            case 'l':
152
                 loadAddr = strtoul(optarg, NULL, 0);
153
                 break;
154
            case 'f':
155
                 filename = optarg;
156
                 break;
157
            case 'c':
158
                 cycles = strtoul(optarg, NULL, 0);
159
                 break;
160
            case 'i':
161
                 intr_after = strtoul(optarg, NULL, 0);
162
                 break;
163
            case 'b':
164
                 breakAddr = strtoul(optarg, NULL, 0);
165
                 break;
166
            case '?':
167
            default:
168
                help = 1;
169
                break;
170
        }
171
    }
172
 
173
    if (loadAddr < MEM_BASE)
174
    {
175
        fprintf (stderr,"Load address incorrect (0x%x)\n", loadAddr);
176
        exit(-1);
177
    }
178
 
179
    if (help || filename == NULL)
180
    {
181
        fprintf (stderr,"Usage:\n");
182
        fprintf (stderr,"-t          = Enable program trace\n");
183
        fprintf (stderr,"-l 0xnnnn   = Executable load address\n");
184
        fprintf (stderr,"-f filename = Executable to load\n");
185
        fprintf (stderr,"-c num      = Max number of cycles\n");
186
        fprintf (stderr,"-i num      = Generate interrupt after num cycles\n");
187
        fprintf (stderr,"-b addr     = Break at address\n");
188
 
189
        exit(0);
190
    }
191
 
192
    top_init();
193
 
194
    if (strstr(filename, ".elf"))
195
    {
196
#ifdef INCLUDE_ELF_SUPPORT        
197
        if (!elf_load(filename, &execAddr))
198
        {
199
            fprintf (stderr,"Error: Could not open ELF file %s\n", filename);
200
            exit(-1);
201
        }
202
#else
203
        fprintf (stderr,"Error: ELF files not supported\n");
204
        exit(-1);
205
#endif
206
    }
207
    else
208
    {
209
        FILE *f;
210
 
211
        printf("Opening %s\n", filename);
212
        f = fopen(filename, "rb");
213
        if (f)
214
        {
215
            long size;
216
            char *buf;
217
 
218
            // Get size
219
            fseek(f, 0, SEEK_END);
220
            size = ftell(f);
221
            rewind(f);
222
 
223
            buf = (char*)malloc(size+1);
224
            if (buf)
225
            {
226
                unsigned int addr;
227
 
228
                // Read file data in
229
                int len = fread(buf, 1, size, f);
230
                buf[len] = 0;
231
 
232
                printf("Loading to 0x%x\n", loadAddr);
233
                for (addr=0;addr<len;addr++)
234
                    top_load(loadAddr + addr, buf[addr]);
235
 
236
                free(buf);
237
                fclose(f);
238
            }
239
        }
240
        else
241
        {
242
            printf("Could not read file!\n");
243
            exit(-1);
244
        }
245
    }
246
 
247
    // Setup breakpoint to stop at (or 0xFFFFFFFF if none)
248
    top_setbreakpoint(0, breakAddr);
249
 
250
    // Run
251
    err = top_run(execAddr, cycles, intr_after);
252
 
253
    // Cleanup
254
    top_done();
255
 
256
    printf("Exit\n");
257
    exit((err == TOP_RES_FAULT) ? 1 : 0);
258
}

powered by: WebSVN 2.1.0

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