1 | #ifndef SEGMENT_H |
---|
2 | #define SEGMENT_H |
---|
3 | |
---|
4 | #include <stdint.h> |
---|
5 | #include <iomanip> |
---|
6 | #include <iostream> |
---|
7 | #include "type_target.h" |
---|
8 | #include "shared/soclib_segment_table.h" |
---|
9 | |
---|
10 | using namespace std; |
---|
11 | |
---|
12 | extern "C" void loadexec(void **emem, int *esize, int *eoffset, const char *file, const char ** sections); |
---|
13 | |
---|
14 | |
---|
15 | namespace hierarchy_memory { |
---|
16 | namespace data { |
---|
17 | |
---|
18 | class segment_t |
---|
19 | { |
---|
20 | protected : uint32_t base; |
---|
21 | protected : uint32_t size; |
---|
22 | protected : bool uncached; |
---|
23 | protected : char * name; |
---|
24 | protected : char * data; |
---|
25 | protected : type_target_t type; |
---|
26 | protected : uint32_t index; |
---|
27 | |
---|
28 | public : segment_t () |
---|
29 | { |
---|
30 | base = 0; |
---|
31 | size = 0; |
---|
32 | uncached = false; |
---|
33 | name = NULL; |
---|
34 | data = NULL; |
---|
35 | type = TYPE_MEMORY; |
---|
36 | index = 0; |
---|
37 | } |
---|
38 | |
---|
39 | public : segment_t (SEGMENT_TABLE_ENTRY segment) |
---|
40 | { |
---|
41 | uint32_t size_name = strlen (segment.getName())+1; |
---|
42 | |
---|
43 | base = segment.getBase(); |
---|
44 | size = segment.getSize(); |
---|
45 | uncached = segment.getUncached(); |
---|
46 | name = new char [size_name]; |
---|
47 | name = strncpy (name, segment.getName(),size_name); |
---|
48 | data = new char [size]; |
---|
49 | type = TYPE_MEMORY; |
---|
50 | index = 0; |
---|
51 | }; |
---|
52 | |
---|
53 | //*****[ init ]***** |
---|
54 | public : bool init (const char * filename,const char ** sections) |
---|
55 | { |
---|
56 | void * ptab; |
---|
57 | int size = this->size; |
---|
58 | int offset = 0; |
---|
59 | |
---|
60 | loadexec(&ptab,&size,&offset,filename,sections); |
---|
61 | |
---|
62 | cout << " - size : " << size << endl; |
---|
63 | cout << " - offset : " << offset << endl; |
---|
64 | |
---|
65 | if (size > (int)this->size) |
---|
66 | { |
---|
67 | cerr << "<segment.init> : segment " << name << " is to small : size is " << this->size << " and requiert is " << size << endl; |
---|
68 | return false; |
---|
69 | } |
---|
70 | |
---|
71 | memcpy(data, ptab, size); |
---|
72 | |
---|
73 | free (ptab); |
---|
74 | |
---|
75 | return true; |
---|
76 | } |
---|
77 | |
---|
78 | //*****[ define_target ]***** |
---|
79 | public : void define_target (type_target_t type, uint32_t index) |
---|
80 | { |
---|
81 | this->type = type; |
---|
82 | this->index = index; |
---|
83 | } |
---|
84 | |
---|
85 | //*****[ getType ]***** |
---|
86 | public : type_target_t getType () |
---|
87 | { |
---|
88 | return type; |
---|
89 | } |
---|
90 | |
---|
91 | //*****[ getIndex ]***** |
---|
92 | public : uint32_t getIndex () |
---|
93 | { |
---|
94 | return index; |
---|
95 | } |
---|
96 | |
---|
97 | //*****[ getBase ]***** |
---|
98 | public : uint32_t getBase () |
---|
99 | { |
---|
100 | return base; |
---|
101 | } |
---|
102 | |
---|
103 | //*****[ getSize ]***** |
---|
104 | public : uint32_t getSize () |
---|
105 | { |
---|
106 | return size; |
---|
107 | } |
---|
108 | |
---|
109 | //*****[ getUncached ]***** |
---|
110 | public : uint32_t getUncached () |
---|
111 | { |
---|
112 | return uncached; |
---|
113 | } |
---|
114 | |
---|
115 | //*****[ data_addr ]***** |
---|
116 | // Return the data address |
---|
117 | public : char * data_addr () |
---|
118 | { |
---|
119 | return data; |
---|
120 | } |
---|
121 | |
---|
122 | //*****[ test ]***** |
---|
123 | public : bool test (char * name) |
---|
124 | { |
---|
125 | return (strcmp(this->name,name) == 0); |
---|
126 | } |
---|
127 | |
---|
128 | public : bool test (uint32_t address, uint32_t size) |
---|
129 | { |
---|
130 | bool res = (((address ) >= this->base ) && |
---|
131 | ((address+size) <= (this->base+this->size))); |
---|
132 | |
---|
133 | return res; |
---|
134 | } |
---|
135 | |
---|
136 | //*****[ read ]***** |
---|
137 | public : void read (uint32_t address, uint32_t size, char * & data_dest) |
---|
138 | { |
---|
139 | for (uint32_t it = 0; it < size ; it ++) |
---|
140 | data_dest [it] = data[address-base+it]; |
---|
141 | } |
---|
142 | |
---|
143 | //*****[ write ]***** |
---|
144 | public : void write (uint32_t address, uint32_t size, char * & data_src) |
---|
145 | { |
---|
146 | for (uint32_t it = 0; it < size ; it ++) |
---|
147 | data[address-base+it] = data_src [it]; |
---|
148 | } |
---|
149 | |
---|
150 | friend ostream& operator<< (ostream& output_stream, const segment_t & x) |
---|
151 | { |
---|
152 | output_stream << "Segment \"" << x.name << "\"" << endl |
---|
153 | << hex |
---|
154 | << " * base : " << setw(8) << setfill('0') << x.base << endl |
---|
155 | << " * size : " << setw(8) << setfill('0') << x.size << endl |
---|
156 | << dec |
---|
157 | << " * uncached : " << x.uncached << endl |
---|
158 | << " * type : " << x.type << endl; |
---|
159 | |
---|
160 | return output_stream; |
---|
161 | }; |
---|
162 | }; |
---|
163 | |
---|
164 | };}; //end namespace |
---|
165 | #endif //!SEGMENT_H |
---|