File: main.dox

package info (click to toggle)
simulavr 0.1.2.2-6.1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 2,756 kB
  • ctags: 3,179
  • sloc: ansic: 19,987; sh: 3,623; python: 3,528; makefile: 406; asm: 308; yacc: 145; lex: 48
file content (437 lines) | stat: -rw-r--r-- 12,891 bytes parent folder | download | duplicates (5)
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/* -*- mode: text -*-
 * $Id: main.dox,v 1.2 2003/11/11 08:14:19 troth Exp $
 *
 ****************************************************************************
 *
 * simulavr - A simulator for the Atmel AVR family of microcontrollers.
 * Copyright (C) 2001, 2002  Theodore A. Roth
 *
 * 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
 *
 ****************************************************************************
 */

/*
 * This file is only for documentation. There should never be any real code in
 * this file.
 *
 * Note: that each \page must be in it's own comment block. 
 *
 * Note: section and page names must only contain the characters [a-z0-9_].
 *
 * Note: You must use C++ style comments within code examples.
 */

/** \mainpage Simulavr Internals

\section intro Introduction
\addindex introduction

This chapter documents the internals of simulavr for those wishing to work
with the source code to fix bugs, add features, or to just see how it all
works. If you only wish to know how to use simulavr, you don't need to read
this.

Internals Topics:

- \link memory_management Memory Management \endlink
- \link object_system Objects \endlink
- \link insn_decoder Instruction Decoder \endlink
- \link interrupts Interrupts \endlink
- \link virtual_devs Virtual Devices \endlink
- \link ext_devs External Devices \endlink
- \link break_watch_pts Breakpoints and Watchpoints \endlink

*/

/** \page memory_management Memory Management
\addindex memory management

Every program ever written has had to deal with memory management.  Simulavr
is no exception. For portability and to potentially aid in memory debugging,
simulavr supplies it's own functions and macros for handling the allocation
and releasing of memory resources.

For memory which could be used by many differing parts of the simulator, an
object referencing system has been implemented (see \ref object_system).

\section memory_functions Memory Functions
\addindex memory functions

The following functions provide wrappers for all library functions that return
memory which simulavr must manage.

- avr_malloc()
- avr_malloc0()
- avr_realloc()
- avr_strdup()
- avr_free()

All functions which return allocated memory will only return if the allocation
was successful. If the allocation failed, an error message is issued and the
program is aborted. Thus, the developer does not need write any code to check
the returned value.

\section memory_macros Memory Macros
\addindex memory macros

The following C-preprocessor macro definitions are provided for convenience
and should be used instead of the underlying functions.  These macros relieve
the programmer from having to perform manual type-casting thus making the code
easier to read.

- avr_new()
- avr_new0()
- avr_renew()

*/

/** \page object_system Objects
\addindex objects

Simulavr uses a simple object oriented system for handling the data structure
creation and destruction. Since simulavr is written in C, a class system must be
manually implemented and the basis for this class system is the AvrClass
structure. All higher level structures are ultimately based on the AvrClass
structure.

How the AvrClass structure is defined is not as import as how it is used as a
base or parent class structure. A concrete example of simulavr's object system
will be discussed (see \ref example_derived_class), but before jumping into the
example, the AvrClass method functions will be introduced.

\section avrclass AvrClass Methods

The following functions provide the user interfaces to the AvrClass structure.

- class_new()
- class_construct()
- class_destroy()
- class_overload_destroy()
- class_ref()
- class_unref()

All classes must provide their own creation function, \<klass\>_new(). The
purpose of the creation function is to:

- Allocate memory for the class's data structure.
- Call class_overload_destroy() to install the class's own destroy method.
- Call the class's constructor method to fill in the data structure information.

\section example_derived_class Derived Class Example
\addindex example, derived class

Simulavr's inheritance mechanism is a little more complicated than that of
C++, but is still relatively easy to use once it is understood. An example
should make it clear how the system works.

First we need to create some objects. Assume that we need to add two new
objects to simulavr, \c foo and \c bar. To keep things simple, they are both
integers. Another requirement is that any time we need to access a \c foo, we'll
also need to access a \c bar, but sometimes we only need a \c bar without a \c
foo. Thus, we will have a class hierarchy \c FooClass->BarClass->AvrClass, or
\c FooClass derives from \c BarClass which derives from \c AvrClass. To achieve
this, we create the following two data structures:

\addindex BarClass structure definition
\addindex BarClass
\addindex FooClass structure definition
\addindex FooClass
\code

// Define BarClass with AvrClass as parent 

typedef struct _BarClass BarClass;
struct _BarClass {
    AvrClass parent;
    int      bar;
};

// Define FooClass with BarClass as parent 

typedef struct _FooClass FooClass;
struct _FooClass {
    BarClass parent;
    int      foo;
};

\endcode

Notice that in both struct definitions, the parent element is not a
pointer. When you allocate memory for a \c BarClass, you automatically
allocate memory for an \c AvrClass at the same time. It's important that the
parent is always the first element of any derived class structure.

The trick here is that once we have a class object, we can get at any object
in it's class hierarchy with a simple type-cast.

\code

void func( void )
{
    int num;
    FooClass *Foo = foo_new( 12, 21 );

    // get foo from FooClass 
    num = Foo->foo;

    // get bar from BarClass 
    num = ((BarClass *)Foo)->bar;

    class_unref( (AvrClass *)Foo );
}

\endcode

Although the example above works, it assumes that the programmer knows what
the \c FooClass and \c BarClass structures look like. The programmer has
broken the encapsulation of both \c FooClass and \c BarClass objects. To solve
this problem, we need to write method functions for both classes.

Here's the methods for \c BarClass:

\addindex BarClass
\code

// BarClass allocator 
BarClass *bar_new( int bar )
{
    BarClass *bc;

    bc = avr_new( BarClass, 1 );
    bar_construct( bc, bar );
    class_overload_destroy( (AvrClass *)bc, bar_destroy );

    return bc;
}

// BarClass constructor 
void bar_construct( BarClass *bc, int bar )
{
    class_construct( (AvrClass *)bc );
    bc->bar = bar;
}

// BarClass destructor 
void bar_destroy( void *bc )
{
    if (bc == NULL)
        return;

    class_destroy( bc );
}

// BarClass public data access methods 
int  bar_get_bar( BarClass *bc )          { return bc->bar; }
void bar_set_bar( BarClass *bc, int val ) { bc->bar = val;  }

\endcode

And here's the methods for \c FooClass:

\addindex FooClass
\code

// FooClass allocator 
FooClass *foo_new( int foo, int bar )
{
    FooClass *fc;

    fc = avr_new( FooClass, 1 );
    foo_construct( fc, foo, bar );
    class_overload_destroy( (AvrClass *)fc, foo_destroy );

    return fc;
}

// FooClass constructor 
void foo_construct( FooClass *fc, int foo, bar )
{
    bar_construct( (BarClass *)fc, bar );
    fc->foo = foo;
}

// FooClass destructor 
void foo_destroy( void *fc )
{
    if (fc == NULL)
        return;

    class_destroy( fc );
}

// FooClass public data access methods 

int  foo_get_foo( FooClass *fc )          { return fc->foo; }
void foo_set_foo( FooClass *fc, int val ) { fc->foo = val;  }

int  foo_get_bar( FooClass *fc )
{
    return bar_get_bar( (BarClass *)fc );
}

void foo_set_bar( FooClass *fc, int val )
{
    bar_set_bar( (BarClass *)fc, val );
}

\endcode

Take a good look at the \c *_new(), \c *_construct() and \c *_destroy()
functions in the above examples and make sure you understand what's going
on. Of particluar importance is how the constructor and destructor functions
are chained up along the various classes. This pattern is used extensively
throughout the simulavr source code and once understood, makes some
complicated concepts incredibly easy to implement.

Now that we have the method functions, we can rewrite our original example
function without the broken encapsulation.

\code

void func( void )
{
    int num;
    FooClass *Foo = foo_new( 12, 21 );

    num = foo_get_foo( Foo );
    num = foo_get_bar( Foo );

    class_unref( (AvrClass *)Foo );
}

\endcode

Now that's better, but you might think that we are breaking encapsulation when
we cast \c Foo to \c AvrClass. Well, in a way we are, but since \em all class
objects \em must be derived from \c AvrClass either directly or indirectly,
this is acceptable.

\section object_refencing Object Referencing
\addindex object referencing

You may have noticed by this point that we haven't called avr_free() to free
the memory we allocated for our objects. We called class_unref() instead. This
mechanism allows us to store many references to a single object without having
to keep track of all of them.

The only thing we must do when we store a reference to an object in a new
variable, is call class_ref() on the object. Then, when that stored reference
is no longer needed, we simply call class_unref() on the object. Once the
reference count reaches zero, the object's destroy method is automatically
called for us. The only hard part for us is knowing when to ref and unref the
object.

Here's an example from the simulavr code for callbacks:

\code

void callback_construct( CallBack *cb,
                         CallBack_FP func,
                         AvrClass *data )
{
    if (cb == NULL)
        avr_error( "passed null ptr");

    class_construct( (AvrClass *)cb );

    cb->func = func;

    cb->data = data;
    class_ref( data );
}

void callback_destroy( void *cb )
{
    CallBack *_cb = (CallBack *)cb;

    if (cb == NULL)
        return;

    class_unref( _cb->data );

    class_destroy( cb );
}

\endcode

Notice that \c data is a pointer to \c AvrClass and thus can be any class
defined by simulavr. \c CallBack is another class which happens to store a
reference to \c data and must therefore call class_ref() on the \c data
object. When the callback is destroyed (because the reference count reached
zero), the callback destroy method calls class_unref() on the \c data
object. It is assumed that the original reference to \c data still exists when
the callback is created, but may or may not exist when the callback is
destroyed.

*/

/** \page insn_decoder Instruction Decoder
\addindex instruction decoder

Instruction decoding and processing is implemented in the \c decode.c file.

The heart of the instruction decoder is the decode_opcode() function.

The decode_opcode() function examines the given opcode to determine which
instruction applies and returns a pointer to a function to handle performing
the instruction's operation. If the given opcode does not map to an
instruction handler, \c NULL is returned indicating an invalid instruction.

Nearly every instruction in Atmel's Instruction Set Data Sheet will have a
handler function defined. Each handler will perform all the operations
described in the data sheet for a given instruction. A few instructions have
synonyms. For example, \c CBR is a synonym for \c ANDI.

This should all be fairly straight forward.

*/

/** \page interrupts Interrupts
\addindex interrupts

\b FIXME: empty place holder

*/

/** \page virtual_devs Virtual Devices
\addindex virtual devices

\b FIXME: empty place holder

*/

/** \page ext_devs External Devices
\addindex external devices

\b FIXME: empty place holder

*/

/** \page break_watch_pts Breakpoints and Watchpoints
\addindex breakpoints
\addindex watchpoints

Using gdb, it is possible to set breakpoints.

Watch points are not currently implemented.

\b This \b is \b only \b an \b idea \b right \b now: The way breakpoints are
implemented within simulavr, it would be possible at init time to read a file
containing breakpoint information. Then, as breakpoints are reached, have
something happen which allows the user to check the state of the
program. Anyone interested in implementing this, please step forward.

*/