File: register.c

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 (653 lines) | stat: -rw-r--r-- 14,668 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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*
 * $Id: register.c,v 1.35 2004/01/30 07:09:56 troth Exp $
 *
 ****************************************************************************
 *
 * simulavr - A simulator for the Atmel AVR family of microcontrollers.
 * Copyright (C) 2001, 2002, 2003, 2004  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
 *
 ****************************************************************************
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>

#include "avrerror.h"
#include "avrmalloc.h"
#include "avrclass.h"
#include "utils.h"
#include "callback.h"
#include "op_names.h"

#include "storage.h"
#include "flash.h"

#include "vdevs.h"
#include "memory.h"
#include "stack.h"
#include "register.h"
#include "sram.h"
#include "eeprom.h"
#include "timers.h"
#include "ports.h"

#include "avrcore.h"

#include "display.h"
#include "intvects.h"

/****************************************************************************\
 *
 * Status Register Methods.
 *
\****************************************************************************/

static inline uint8_t sreg_read (VDevice *dev, int addr);
static inline void sreg_write (VDevice *dev, int addr, uint8_t val);
static inline void sreg_reset (VDevice *dev);
static void sreg_add_addr (VDevice *dev, int addr, char *name, int rel_addr,
                           void *data);

VDevice *
sreg_create (int addr, char *name, int rel_addr, void *data)
{
    return (VDevice *)sreg_new ();
}

SREG *
sreg_new (void)
{
    SREG *sreg;

    sreg = avr_new (SREG, 1);
    sreg_construct (sreg);
    class_overload_destroy ((AvrClass *)sreg, sreg_destroy);

    return sreg;
}

void
sreg_construct (SREG *sreg)
{
    if (sreg == NULL)
        avr_error ("passed null ptr");

    vdev_construct ((VDevice *)sreg, sreg_read, sreg_write, sreg_reset,
                    sreg_add_addr);

    sreg->sreg.reg = 0;
}

void
sreg_destroy (void *sreg)
{
    if (sreg == NULL)
        return;

    vdev_destroy (sreg);
}

extern inline uint8_t sreg_get (SREG *sreg);

extern inline void sreg_set (SREG *sreg, uint8_t val);

extern inline uint8_t sreg_get_bit (SREG *sreg, int bit);

extern inline void sreg_set_bit (SREG *sreg, int bit, int val);

static inline uint8_t
sreg_read (VDevice *dev, int addr)
{
    return sreg_get ((SREG *)dev);
}

static inline void
sreg_write (VDevice *dev, int addr, uint8_t val)
{
    sreg_set ((SREG *)dev, val);
}

static inline void
sreg_reset (VDevice *dev)
{
    display_io_reg (SREG_IO_REG, 0);
    ((SREG *)dev)->sreg.reg = 0;
}

static void
sreg_add_addr (VDevice *dev, int addr, char *name, int rel_addr, void *data)
{
    /* Nothing to do here. */
}


/****************************************************************************\
 *
 * General Purpose Working Register (gpwr) Methods.
 *
\****************************************************************************/

static inline uint8_t gpwr_read (VDevice *dev, int addr);
static inline void gpwr_write (VDevice *dev, int addr, uint8_t val);
static inline void gpwr_reset (VDevice *dev);

GPWR *
gpwr_new (void)
{
    GPWR *gpwr;

    gpwr = avr_new (GPWR, 1);
    gpwr_construct (gpwr);
    class_overload_destroy ((AvrClass *)gpwr, gpwr_destroy);

    return gpwr;
}

void
gpwr_construct (GPWR *gpwr)
{
    if (gpwr == NULL)
        avr_error ("passed null ptr");

    vdev_construct ((VDevice *)gpwr, gpwr_read, gpwr_write, gpwr_reset, NULL);

    gpwr_reset ((VDevice *)gpwr);
}

void
gpwr_destroy (void *gpwr)
{
    if (gpwr == NULL)
        return;

    vdev_destroy (gpwr);
}

extern inline uint8_t gpwr_get (GPWR *gpwr, int reg);

extern inline void gpwr_set (GPWR *gpwr, int reg, uint8_t val);

static inline uint8_t
gpwr_read (VDevice *dev, int addr)
{
    return gpwr_get ((GPWR *)dev, addr);
}

static inline void
gpwr_write (VDevice *dev, int addr, uint8_t val)
{
    gpwr_set ((GPWR *)dev, addr, val);
}

static void
gpwr_reset (VDevice *dev)
{
    int i;

    for (i = 0; i < GPWR_SIZE; i++)
        gpwr_set ((GPWR *)dev, i, 0);
}

/****************************************************************************\
 *
 * ACSR(VDevice) : Analog Comparator Control and Status Register Definition
 *
\****************************************************************************/

static uint8_t acsr_read (VDevice *dev, int addr);
static void acsr_write (VDevice *dev, int addr, uint8_t val);
static void acsr_reset (VDevice *dev);

ACSR *
acsr_new (uint8_t func_mask)
{
    ACSR *acsr;

    acsr = avr_new (ACSR, 1);
    acsr_construct (acsr, func_mask);
    class_overload_destroy ((AvrClass *)acsr, acsr_destroy);

    return acsr;
}

void
acsr_construct (ACSR *acsr, uint8_t func_mask)
{
    if (acsr == NULL)
        avr_error ("passed null ptr");

    vdev_construct ((VDevice *)acsr, acsr_read, acsr_write, acsr_reset,
                    vdev_def_AddAddr);

    acsr->func_mask = func_mask;
    acsr->acsr = 0;
}

void
acsr_destroy (void *acsr)
{
    if (acsr == NULL)
        return;

    vdev_destroy (acsr);
}

int
acsr_get_bit (ACSR *acsr, int bit)
{
    return !!(acsr->acsr & acsr->func_mask & (1 << bit));
}

void
acsr_set_bit (ACSR *acsr, int bit, int val)
{
    /* the ACO bit is read only */
    acsr->acsr =
        set_bit_in_byte (acsr->acsr, bit,
                         val) & acsr->func_mask & ~(mask_ACO);
}

static uint8_t
acsr_read (VDevice *dev, int addr)
{
    ACSR *reg = (ACSR *)dev;

    return (reg->acsr & reg->func_mask);
}

static void
acsr_write (VDevice *dev, int addr, uint8_t val)
{
    ACSR *reg = (ACSR *)dev;

    /* the ACO bit is read only */
    reg->acsr = (val & reg->func_mask & ~(mask_ACO));
}

static void
acsr_reset (VDevice *dev)
{
    ((ACSR *)dev)->acsr = 0;
}

/****************************************************************************\
 *
 * MCUCR(VDevice) : MCU general control register
 *
\****************************************************************************/

static uint8_t mcucr_read (VDevice *dev, int addr);
static void mcucr_write (VDevice *dev, int addr, uint8_t val);
static void mcucr_reset (VDevice *dev);

MCUCR *
mcucr_new (uint8_t func_mask)
{
    MCUCR *mcucr;

    mcucr = avr_new (MCUCR, 1);
    mcucr_construct (mcucr, func_mask);
    class_overload_destroy ((AvrClass *)mcucr, mcucr_destroy);

    return mcucr;
}

void
mcucr_construct (MCUCR *mcucr, uint8_t func_mask)
{
    if (mcucr == NULL)
        avr_error ("passed null ptr");

    vdev_construct ((VDevice *)mcucr, mcucr_read, mcucr_write, mcucr_reset,
                    vdev_def_AddAddr);

    mcucr->func_mask = func_mask;
    mcucr->mcucr = 0;
}

void
mcucr_destroy (void *mcucr)
{
    if (mcucr == NULL)
        return;

    vdev_destroy (mcucr);
}

int
mcucr_get_bit (MCUCR *reg, int bit)
{
    return !!(reg->mcucr & reg->func_mask & (1 << bit));
}

void
mcucr_set_bit (MCUCR *reg, int bit, int val)
{
    reg->mcucr = set_bit_in_byte (reg->mcucr, bit, val) & reg->func_mask;
}

static uint8_t
mcucr_read (VDevice *dev, int addr)
{
    MCUCR *reg = (MCUCR *)dev;

    return (reg->mcucr & reg->func_mask);
}

static void
mcucr_write (VDevice *dev, int addr, uint8_t val)
{
    MCUCR *reg = (MCUCR *)dev;

    reg->mcucr = (val & reg->func_mask);
}

static void
mcucr_reset (VDevice *dev)
{
    ((MCUCR *)dev)->mcucr = 0;
}

/****************************************************************************\
 *
 * WDTCR(VDevice) : Watchdog timer control register
 *
\****************************************************************************/

/*  static int    wdtcr_get_bit   ( WDTCR *wdtcr, int bit ); */
static void wdtcr_set_bit (WDTCR *wdtcr, int bit, int val);

static uint8_t wdtcr_read (VDevice *dev, int addr);
static void wdtcr_write (VDevice *dev, int addr, uint8_t val);
static void wdtcr_reset (VDevice *dev);

static int wdtcr_timer_cb (uint64_t time, AvrClass *data);
static int wdtcr_toe_clr_cb (uint64_t time, AvrClass *data);

WDTCR *
wdtcr_new (uint8_t func_mask)
{
    WDTCR *wdtcr;

    wdtcr = avr_new (WDTCR, 1);
    wdtcr_construct (wdtcr, func_mask);
    class_overload_destroy ((AvrClass *)wdtcr, wdtcr_destroy);

    return wdtcr;
}

void
wdtcr_construct (WDTCR *wdtcr, uint8_t func_mask)
{
    if (wdtcr == NULL)
        avr_error ("passed null ptr");

    vdev_construct ((VDevice *)wdtcr, wdtcr_read, wdtcr_write, wdtcr_reset,
                    vdev_def_AddAddr);

    wdtcr->func_mask = func_mask;

    wdtcr_reset ((VDevice *)wdtcr);
}

void
wdtcr_destroy (void *wdtcr)
{
    if (wdtcr == NULL)
        return;

    vdev_destroy (wdtcr);
}

/*
 * Function wdtcr_update: Called when the WDR instruction is issued
 */
void
wdtcr_update (WDTCR *wdtcr)
{
    wdtcr->last_WDR = get_program_time ();
}

#if 0                           /* This doesn't seem to be used anywhere. */
static int
wdtcr_get_bit (WDTCR *reg, int bit)
{
    return !!(reg->wdtcr & reg->func_mask & (1 << bit));
}
#endif

static void
wdtcr_set_bit (WDTCR *reg, int bit, int val)
{
    reg->wdtcr = set_bit_in_byte (reg->wdtcr, bit, val) & reg->func_mask;
}

static uint8_t
wdtcr_read (VDevice *dev, int addr)
{
    WDTCR *reg = (WDTCR *)dev;

    return (reg->wdtcr & reg->func_mask);
}

/*
 * FIXME: Should the wdtcr->toe_clk counter be reset to TOE_CLKS
 * every time a WDTOE is set 1? I.E. does the hw reset the 4 cycle
 * counter every time WDTOE is set? This code assumes it does.
 */
static void
wdtcr_write (VDevice *dev, int addr, uint8_t val)
{
    WDTCR *reg = (WDTCR *)dev;
    uint8_t wd_enabled = (reg->wdtcr & mask_WDE);

    CallBack *cb;

    if (reg->func_mask & mask_WDTOE)
    {                           /* Device has WDTOE functionality */

        if ((reg->wdtcr & mask_WDE) && !(reg->wdtcr & mask_WDTOE))
        {
            /* WDE can _NOT_ be cleared if WDTOE is zero */
            val |= mask_WDE;
        }

        if (val & mask_WDTOE)
        {                       /* program has set WDTOE */
            reg->toe_clk = TOE_CLKS;

            /* create and install the callback if it not already installed */
            if (reg->toe_cb == NULL)
            {
                cb = callback_new (wdtcr_toe_clr_cb, (AvrClass *)reg);
                reg->toe_cb = cb;
                avr_core_clk_cb_add ((AvrCore *)vdev_get_core (dev), cb);
            }
        }
    }

    reg->wdtcr = (val & reg->func_mask);

    if ((wd_enabled == 0) && (val & mask_WDE) && (reg->timer_cb == NULL))
    {
        /* install the WD timer callback */
        cb = callback_new (wdtcr_timer_cb, (AvrClass *)reg);
        reg->timer_cb = cb;
        avr_core_async_cb_add ((AvrCore *)vdev_get_core (dev), cb);
    }

    if (wd_enabled && ((val & mask_WDE) == 0) && (reg->timer_cb != NULL))
    {
        /* tell callback to remove itself */
        reg->timer_cb = NULL;
    }
}

static void
wdtcr_reset (VDevice *dev)
{
    WDTCR *wdtcr = (WDTCR *)dev;

    wdtcr->wdtcr = 0;

    wdtcr->last_WDR = get_program_time (); /* FIXME: This might not be the
                                              right thing to do */
    wdtcr->timer_cb = NULL;

    wdtcr->toe_clk = TOE_CLKS;
    wdtcr->toe_cb = NULL;
}

/*
 * Timer callback will remove itself if wdtcr->timer_cb is set NULL.
 */
static int
wdtcr_timer_cb (uint64_t time, AvrClass *data)
{
    WDTCR *wdtcr = (WDTCR *)data;
    uint64_t time_diff;
    uint64_t time_out;

    if (wdtcr->timer_cb == NULL)
        return CB_RET_REMOVE;

    time_diff = time - wdtcr->last_WDR;
    time_out = TIMEOUT_BASE * (1 << (wdtcr->wdtcr & mask_WDP));

    if (time_diff > time_out)
    {
        avr_warning ("watchdog reset: time %lld\n", time_diff);

        /* reset the device, we timed out */
        avr_core_irq_raise ((AvrCore *)vdev_get_core ((VDevice *)wdtcr),
                            irq_vect_table_index (RESET));
    }

    return CB_RET_RETAIN;
}

/*
 * The WDTOE is cleared by hardware after TOE_CLKS clock cycles.
 */
static int
wdtcr_toe_clr_cb (uint64_t time, AvrClass *data)
{
    WDTCR *wdtcr = (WDTCR *)data;

    if (wdtcr->toe_cb == NULL)
        return CB_RET_REMOVE;

    if (wdtcr->toe_clk > 0)
    {
        wdtcr->toe_clk--;
    }
    else
    {
        wdtcr_set_bit (wdtcr, bit_WDTOE, 0);
        wdtcr->toe_cb = NULL;   /* So we know that cb is not installed */
        return CB_RET_REMOVE;
    }

    return CB_RET_RETAIN;
}

/****************************************************************************\
 *
 * RAMPZ(VDevice) : The RAMPZ register used by ELPM and ESPM instructions.
 *
 * Even though the rampz register is not available to all devices, we will
 * install it for all in the simulator. It just so much easier that way and
 * we're already assuming that the compiler generated the correct code in
 * many places anyways. Let's see if we get bit.
 *
\****************************************************************************/

static uint8_t rampz_read (VDevice *dev, int addr);
static void rampz_write (VDevice *dev, int addr, uint8_t val);
static void rampz_reset (VDevice *dev);

VDevice *
rampz_create (int addr, char *name, int rel_addr, void *data)
{
    return (VDevice *)rampz_new ();
}

RAMPZ *
rampz_new (void)
{
    RAMPZ *rampz;

    rampz = avr_new (RAMPZ, 1);
    rampz_construct (rampz);
    class_overload_destroy ((AvrClass *)rampz, rampz_destroy);

    return rampz;
}

void
rampz_construct (RAMPZ *rampz)
{
    if (rampz == NULL)
        avr_error ("passed null ptr");

    vdev_construct ((VDevice *)rampz, rampz_read, rampz_write, rampz_reset,
                    vdev_def_AddAddr);

    rampz->reg = 0;
}

void
rampz_destroy (void *rampz)
{
    if (rampz == NULL)
        return;

    vdev_destroy (rampz);
}

uint8_t
rampz_get (RAMPZ *rampz)
{
    return rampz->reg;
}

void
rampz_set (RAMPZ *rampz, uint8_t val)
{
    rampz->reg = val;
}

static uint8_t
rampz_read (VDevice *dev, int addr)
{
    return rampz_get ((RAMPZ *)dev);
}

static void
rampz_write (VDevice *dev, int addr, uint8_t val)
{
    rampz_set ((RAMPZ *)dev, val);
}

static void
rampz_reset (VDevice *dev)
{
    display_io_reg (RAMPZ_IO_REG, 0);
    ((RAMPZ *)dev)->reg = 0;
}