-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvmfile.c
257 lines (204 loc) · 5.95 KB
/
nvmfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//
// NanoVM, a tiny java VM for the Atmel AVR family
// Copyright (C) 2005 by Till Harbaum <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//
// nvmfile.c
//
// routines to store and access the NanoVM internal nvm file
// format as generated by NanoVMTool
//
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "9p_pgm.h"
#include "error.h"
#include "nvmfile.h"
#include "nvmtypes.h"
#include "nvmfeatures.h"
#include "vm.h"
#include <avr/io.h>
#include <util/atomic.h>
#include <avr/pgmspace.h>
uint8_t nvmfile_constant_count;
PGM_VOID_P pgm_mem = NULL;
PGM_VOID_P nvmfile_get_base(void) {
return pgm_mem;
}
void nvmfile_read(void *dst, const void *src, uint16_t len)
{
src = NVMFILE_ADDR(src); // remove marker (if present)
memcpy_P(dst, (PGM_P)src, len);
}
uint8_t nvmfile_read08(const void *addr)
{
uint8_t val;
addr = NVMFILE_ADDR(addr); // remove marker (if present)
memcpy_P((uint8_t*)&val, (PGM_P)addr, sizeof(val));
return val;
}
uint16_t nvmfile_read16(const void *addr)
{
uint16_t val;
addr = NVMFILE_ADDR(addr); // remove marker (if present)
memcpy_P((uint8_t*)&val, (PGM_P)addr, sizeof(val));
return val;
}
uint32_t nvmfile_read32(const void *addr)
{
uint32_t val;
addr = NVMFILE_ADDR(addr); // remove marker (if present)
memcpy_P((uint8_t*)&val, (PGM_P)addr, sizeof(val));
return val;
}
void nvmfile_write08(void *addr, uint8_t data)
{
}
void nvmfile_write_initialize()
{
}
void nvmfile_write_finalize()
{
}
void nvmfile_store(uint16_t index, uint8_t *buffer, uint16_t size)
{
}
bool_t nvmfile_init(void)
{
uint16_t t;
nvm_header_t * nvm_header;
PGM_VOID_P targetcopy;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
targetcopy = pgm_target;
}
if (targetcopy == NULL)
{
return FALSE;
}
else
{
pgm_mem = targetcopy;
}
nvm_header = (nvm_header_t *)pgm_mem;
uint32_t features = nvmfile_read32(&nvm_header->magic_feature);
DEBUGF("NVM_MAGIC_FEAUTURE[file] = %lx\n", features);
DEBUGF("NVM_MAGIC_FEAUTURE[vm] = %lx\n", NVM_MAGIC_FEAUTURE);
if ((features&NVM_MAGIC_FEAUTURE)!=(features|NVMFILE_MAGIC))
{
error(ERROR_NVMFILE_MAGIC);
return FALSE;
}
if(nvmfile_read08(&nvm_header->version) != NVMFILE_VERSION) {
error(ERROR_NVMFILE_VERSION);
return FALSE;
}
t = nvmfile_read16(&nvm_header->string_offset);
t -= nvmfile_read16(&nvm_header->constant_offset);
nvmfile_constant_count = t/4;
return TRUE;
}
nvm_method_hdr_t *nvmfile_get_method_hdr(uint16_t index)
{
// get pointer to method header
nvm_header_t * nvm_header = (nvm_header_t *)pgm_mem;
nvm_method_hdr_t *hdrs =
((nvm_method_hdr_t*)(pgm_mem +
nvmfile_read16(&nvm_header->method_offset)))+index;
return(hdrs);
}
uint32_t nvmfile_get_constant(uint8_t index)
{
nvm_ref_t res;
if (index<nvmfile_constant_count)
{
nvm_header_t * nvm_header = (nvm_header_t *)pgm_mem;
uint16_t addr = nvmfile_read16(&nvm_header->constant_offset);
uint32_t result = nvmfile_read32(pgm_mem+addr+4*index);
DEBUGF(" constant = 0x%08lx\n", result);
return result;
}
// it's a string!
DEBUGF(" constant string index = %i\n", index);
res = NVM_TYPE_CONST | (index-nvmfile_constant_count);
return res;
}
void nvmfile_call_main(void)
{
uint8_t i;
nvm_header_t * nvm_header = (nvm_header_t *)pgm_mem;
for(i=0;i<nvmfile_read08(&nvm_header->methods);i++) {
// is this a clinit method?
if(nvmfile_read08(&nvmfile_get_method_hdr(i)->flags) & FLAG_CLINIT) {
DEBUGF("calling clinit %d\n", i);
vm_run(i);
}
}
// determine method description address and code
vm_run(nvmfile_read16(&nvm_header->main));
}
void *nvmfile_get_addr(uint16_t ref)
{
// get pointer to string
nvm_header_t * nvm_header = (nvm_header_t *)pgm_mem;
uint16_t *refs =
(uint16_t*)(pgm_mem +
nvmfile_read16(&nvm_header->string_offset));
return((uint8_t*)refs + nvmfile_read16(refs+ref));
}
uint8_t nvmfile_get_class_fields(uint8_t index)
{
nvm_header_t * nvm_header = (nvm_header_t *)pgm_mem;
return nvmfile_read08(&nvm_header->class_hdr[index].fields);
}
uint8_t nvmfile_get_static_fields(void)
{
nvm_header_t * nvm_header = (nvm_header_t *)pgm_mem;
return nvmfile_read08(&nvm_header->static_fields);
}
uint8_t nvmfile_get_method_by_fixed_class_and_id(uint8_t class, uint8_t id) {
uint8_t i;
nvm_method_hdr_t mhdr, *mhdr_ptr;
DEBUGF("Searching for class "DBG8", method "DBG8"\n", class, id);
nvm_header_t * nvm_header = (nvm_header_t *)pgm_mem;
for(i=0;i<nvmfile_read08(&nvm_header->methods);i++) {
DEBUGF("Method %d ", i);
// load new method header into ram
mhdr_ptr = nvmfile_get_method_hdr(i);
nvmfile_read(&mhdr, mhdr_ptr, sizeof(nvm_method_hdr_t));
DEBUGF("id = #"DBG16"\n", mhdr.id);
if(((mhdr.id >> 8) == class) && ((mhdr.id & 0xff) == id)) {
DEBUGF("Match!\n");
return i;
}
}
DEBUGF("No matching method in this class\n");
return 0xff;
}
uint8_t nvmfile_get_method_by_class_and_id(uint8_t class, uint8_t id) {
uint8_t mref;
nvm_header_t * nvm_header = (nvm_header_t *)pgm_mem;
for(;;) {
if((mref = nvmfile_get_method_by_fixed_class_and_id(class, id)) != 0xff)
return mref;
DEBUGF("Getting super class of %d ", class);
class = nvmfile_read08(&nvm_header->class_hdr[class].super);
DEBUGF("-> %d\n", class);
}
return 0;
}