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

Subversion Repositories igor

[/] [igor/] [trunk/] [simulator/] [memtool.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 atypic
#include <stdio.h>
2
#include <err.h>
3
#include <string.h>
4
#include <errno.h>
5
 
6
#include "types.h"
7
#include "object.h"
8
#include "memory.h"
9
#include "debug.h"
10
 
11
#define LINE_LEN 64
12
 
13
int memtool_size = 0;
14
int memtool_start = 0;
15
 
16
void
17
memory_load_from_file_s(char *memfile)
18
{
19
        char line[LINE_LEN];
20
        char cmd[LINE_LEN];
21
        char *args;
22
        FILE *f;
23
        int linenr, i, c, addr, intarg, addrarg, store_obj;
24
        reg_t obj = 0;
25
 
26
        addr = 0;
27
 
28
        f = fopen(memfile, "r");
29
        if (f == NULL)
30
                errx(1, "could not open memory file %s for reading: %s",
31
                     memfile, strerror(errno));
32
 
33
        for (linenr = 1; !feof(f); linenr++) {
34
                for (i = 0, c = fgetc(f);
35
                     i < LINE_LEN-1 && c != '\n' && c != EOF;
36
                     i++, c = fgetc(f))
37
                        line[i] = c;
38
                if (c == EOF)
39
                        break;
40
                line[i] = '\0';
41
                while (c != '\n' && c != EOF)
42
                        c = fgetc(f);
43
 
44
                if (line[0] == '#' || i == 0)
45
                        continue;
46
 
47
                for (i = 0; i < LINE_LEN-2 && line[i] != ' '; i++)
48
                        cmd[i] = line[i];
49
                cmd[i] = '\0';
50
                args = &line[i+1];
51
 
52
                sscanf(args, "%X", &intarg);
53
                addrarg = intarg;
54
                if (args[0]=='+' || args[0]=='-')
55
                        addrarg += addr;
56
 
57
                store_obj = 1;
58
                if (strcmp(cmd, "addr")==0) {
59
                        addr = intarg;
60
                        store_obj = 0;
61
                } else if (strcmp(cmd, "size")==0) {
62
                        memtool_size = intarg;
63
                        store_obj = 0;
64
                } else if (strcmp(cmd, "start")==0) {
65
                        memtool_start = intarg;
66
                        store_obj = 0;
67
                } else if (strcmp(cmd, "nil")==0) {
68
                        obj = object_make(TYPE_NIL, 0);
69
                } else if (strcmp(cmd, "t")==0) {
70
                        obj = object_make(TYPE_T, 0);
71
                } else if (strcmp(cmd, "int")==0) {
72
                        obj = object_make(TYPE_INT, intarg);
73
                } else if (strcmp(cmd, "char")==0) {
74
                        obj = object_make(TYPE_CHAR, intarg);
75
                } else if (strcmp(cmd, "array")==0) {
76
                        obj = object_make(TYPE_ARRAY, intarg);
77
                } else if (strcmp(cmd, "ptr")==0) {
78
                        obj = object_make(TYPE_PTR, addrarg);
79
                } else if (strcmp(cmd, "symbol")==0) {
80
                        obj = object_make(TYPE_SYMBOL, addrarg);
81
                } else if (strcmp(cmd, "builtin")==0) {
82
                        obj = object_make(TYPE_BUILTIN, addrarg);
83
                } else if (strcmp(cmd, "cons")==0) {
84
                        obj = object_make(TYPE_CONS, addrarg);
85
                } else if (strcmp(cmd, "snoc")==0) {
86
                        obj = object_make(TYPE_SNOC, addrarg);
87
                } else if (strcmp(cmd, "none")==0) {
88
                        obj = object_make(TYPE_NONE, intarg);
89
                } else {
90
                        warnx("I don't know what %s is", cmd);
91
                        store_obj = 0;
92
                }
93
 
94
                if (store_obj) {
95
                        if (object_get_type(memory_get(addr)) != TYPE_NONE)
96
                                errx(1, "overwriting memory at address 0x%07X", addr);
97
                        if ((memtool_size>0 && addr >= memtool_size+memtool_start) ||
98
                            (memtool_start>0 && addr < memtool_start))
99
                                errx(1, "storing at address 0x%07X, which is outside "
100
                                     "used region 0x%07X-0x%07X",
101
                                     addr, memtool_start, memtool_size+memtool_start);
102
                        memory_set(addr++, obj);
103
                }
104
        }
105
}
106
 
107
int
108
memory_used_amount(void)
109
{
110
        int i;
111
        for (i = memory_size()-1; i >= 0; i--) {
112
                if (memory_get(i))
113
                        return i+1;
114
        }
115
        return 0;
116
}
117
 
118
void
119
create_memory_file(char *infile, char *outfile)
120
{
121
        int size;
122
        memory_init(DEFAULT_MEMORY_SIZE, NULL);
123
        memory_load_from_file_s(infile);
124
        size = memtool_size>0 ? memtool_size : (memory_used_amount()-memtool_start);
125
        memory_write_part_to_file(outfile, memtool_start, size);
126
}
127
 
128
extern int verify_written_memory;
129
 
130
int
131
main(int argc, char **argv)
132
{
133
        if (argc < 3) {
134
                fprintf(stderr,
135
                        "usage: %s INFILE OUTFILE\n"
136
                        "Translates textual description of memory in INFILE to\n"
137
                        "binary representation of memory (suitable for loading\n"
138
                        "into the emulator) in OUTFILE\n",
139
                        argv[0]);
140
                return 1;
141
        }
142
 
143
        verify_written_memory = 0;
144
        debuginfo = NULL;
145
        create_memory_file(argv[1], argv[2]);
146
        return 0;
147
}
148
 

powered by: WebSVN 2.1.0

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