-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestavr.h
330 lines (263 loc) · 8.02 KB
/
testavr.h
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
This file is part of AVRtest -- A simple simulator for the
AVR family of 8-bit microcontrollers designed to test the compiler.
Copyright (C) 2001, 2002, 2003 Theodore A. Roth, Klaus Rudolph
Copyright (C) 2007 Paulo Marques
Copyright (C) 2008-2024 Free Software Foundation, Inc.
AVRtest 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.
AVRtest 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 AVRtest; see the file COPYING. If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#ifndef TESTAVR_H
#define TESTAVR_H
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <inttypes.h>
// ---------------------------------------------------------------------------
// configuration values (in bytes).
#ifdef ISA_XMEGA
#define MAX_RAM_SIZE (0x1000000) // 3-byte addresses due to RAMPx.
#else
#define MAX_RAM_SIZE (0x10000)
#endif // ISA_XMEGA
#define MAX_FLASH_SIZE (0x40000) // Must be at least 128KiB
#define MAX_EEPROM_SIZE (16 * 1024) // .eeprom is read from ELF but unused
#define REGX 26
#define REGY 28
#define REGZ 30
// PC is used as array index into decoded_flash[].
// If PC has bits outside the following mask, something went really wrong,
// e.g. in pop_PC.
#define PC_VALID_MASK (MAX_FLASH_SIZE/2 - 1)
typedef uint8_t byte;
typedef uint16_t word;
typedef uint32_t dword;
typedef struct
{
byte id;
byte op1;
word op2;
} decoded_t;
typedef struct
{
// Program entry byte address as of ELF header or set
// by -e ENTRY
unsigned entry_point;
// Size in bytes of program in flash (assuming it starts at 0x0).
unsigned size;
// Number of bytes read from file.
unsigned n_bytes;
// Range that covers executable code's byte addresses. That range might
// contain non-executable code like ELF headers that are part of PHDRs.
unsigned code_start, code_end;
// Max word address the PC can ever have. Anything bigger is bad_PC().
unsigned max_pc;
// A word mask to implement PC wrap-around for relative jumps.
unsigned pc_mask;
// Maximum number of instructions to be executed,
// used as a timeout. Can be set by -m CYCLES
uint64_t max_insns;
// Number of instructions simulated so far.
uint64_t n_insns;
// Cycles consumed by the program so far.
uint64_t n_cycles;
//
int leave_status, exit_value;
// filename of the file being executed
const char *name;
// ...and with directories stripped off
const char *short_name;
} program_t;
extern program_t program;
extern unsigned cpu_PC;
extern const int io_base;
extern const bool is_xmega;
extern const bool is_tiny;
extern const bool is_avrtest_log;
extern const unsigned invalid_opcode;
extern bool have_syscall[32];
#define INLINE inline __attribute__((always_inline))
#define NOINLINE __attribute__((noinline))
#define NORETURN __attribute__((noreturn))
#if defined (__i386__) || defined (__i868__)
#define FASTCALL __attribute__((fastcall))
#else
#define FASTCALL /* empty */
#endif
#if defined (__MINGW_PRINTF_FORMAT)
#define ATTR_PRINTF(N1, N2) \
__attribute__((__format__(__MINGW_PRINTF_FORMAT,N1,N2)))
#else
#define ATTR_PRINTF(N1, N2) \
__attribute__((__format__(printf,N1,N2)))
#endif
enum
{
LEAVE_EXIT,
LEAVE_ABORTED,
LEAVE_TIMEOUT,
LEAVE_ELF,
LEAVE_CODE,
LEAVE_SYMBOL,
LEAVE_HOSTIO,
// Something went badly wrong
LEAVE_USAGE,
LEAVE_MEMORY,
LEAVE_FOPEN,
LEAVE_FATAL
};
enum
{
AR_REG,
AR_RAM,
AR_FLASH,
AR_EEPROM
};
enum
{
IL_ILL,
IL_ARCH,
IL_TODO
};
ATTR_PRINTF(2,3)
extern void NOINLINE NORETURN leave (int status, const char *reason, ...);
ATTR_PRINTF(1,2)
extern void qprintf (const char *fmt, ...);
extern byte* cpu_address (int, int);
extern void* get_mem (unsigned, size_t, const char*);
extern const int addr_SREG;
extern const int addr_SPL;
extern byte* const pSP;
typedef struct
{
int addr;
const char *name;
// Points to a bool telling whether this address is special.
// NULL means "yes".
bool *pon;
} sfr_t;
extern const sfr_t named_sfr[];
#define OP_FUNC_TYPE void FASTCALL
typedef OP_FUNC_TYPE (*opcode_func)(int,int);
typedef struct
{
opcode_func func;
const char *mnemonic;
short size;
short cycles;
} opcode_t;
extern void log_va (const char*, va_list);
#ifndef AVRTEST_LOG
// empty placeholders to keep the rest of the code clean
#define log_init(...) (void) 0
#define log_append(...) (void) 0
#define log_append_va(...) (void) 0
#define log_add_instr(...) (void) 0
#define log_add_data_mov(...) (void) 0
#define log_add_flag_read(...) (void) 0
#define log_dump_line(...) (void) 0
#define do_syscall(...) (void) 0
#define log_set_func_symbol(...) (void) 0
#define log_set_string_table(...) (void) 0
#define log_finish_string_table(...) (void) 0
#define log_maybe_change_SP(...) (void) 0
#else
extern unsigned old_PC, old_old_PC;
extern bool log_unused;
extern void log_init (unsigned);
ATTR_PRINTF(1,2)
extern void log_append (const char *fmt, ...);
extern void log_append_va (const char *fmt, va_list);
extern void log_add_instr (const decoded_t *op);
extern void log_add_data_mov (const char *format, int addr, int value);
extern void log_add_flag_read (int mask, int value);
extern void log_add_reg_mov (const char *format, int regno, int value);
extern void log_dump_line (const decoded_t*);
extern void do_syscall (int x, int val);
extern void log_set_func_symbol (int, size_t, bool);
extern void log_set_string_table (char*, size_t, int);
extern void log_finish_string_table (void);
extern void log_maybe_change_SP (int);
typedef struct
{
bool perf;
bool logging;
bool graph, graph_cost;
bool call_depth;
} need_t;
typedef struct
{
// The ELF string table associated to the ELF symbol table
const char *data;
size_t size;
int n_entries;
// Number of ...
int n_strings; // ... usable items and indices (inclusive) in strings[]
int n_funcs; // ... that have STT_FUNC as symbol table type
int n_bad; // ... that are of no use for us, e.g. orphans
int n_vec; // ... unused vectors slots
bool *have;
} string_table_t;
// Some data shared by logging modules logging.c, perf.c, graph.c.
// Objects hosted by logging.c.
extern need_t need;
extern string_table_t string_table;
extern int get_nonglitch_SP (void);
#endif // AVRTEST_LOG
extern void load_to_flash (const char*, byte[], byte[], byte[]);
extern void decode_flash (decoded_t[], const byte[]);
extern void set_elf_string_table (char*, size_t, int);
extern void finish_elf_string_table (void);
extern void set_elf_function_symbol (int, size_t, bool);
extern void put_argv (int, byte*);
#include <string.h>
static INLINE bool
str_prefix (const char *prefix, const char *str)
{
return 0 == strncmp (prefix, str, strlen (prefix));
}
static INLINE bool
str_eq (const char *s1, const char *s2)
{
return 0 == strcmp (s1, s2);
}
static INLINE bool
str_in (const char *s, const char * const *arr)
{
for (; *arr; arr++)
if (str_eq (s, *arr))
return true;
return false;
}
// Returns n if x = 2^n, and -1 otherwise.
static INLINE int
exact_log2 (unsigned x)
{
if (x == 0 || (x & (x - 1)))
return -1;
int n = 0;
for (; x != 1; x >>= 1)
n = n + 1;
return n;
}
// ---------------------------------------------------------------------------
// auxiliary lookup tables
extern const opcode_t opcodes[];
enum
{
#define AVR_OPCODE(ID, N_WORDS, N_TICKS, NAME) \
ID_ ## ID,
#include "avr-opcode.def"
#undef AVR_OPCODE
};
#endif // TESTAVR_H