summaryrefslogtreecommitdiff
path: root/digital/avr/modules/utils/test/test_byte_methods.c
blob: adf6d05ed1b9e6492d5159bd818c04cada235fbe (plain)
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
/* test_byte.c */
/* n.avr.utils - AVR utilities module. {{{
 *
 * Copyright (C) 2005 Nicolas Schodet
 *
 * Robot APB Team/Efrei 2005.
 *        Web: http://assos.efrei.fr/robot/
 *      Email: robot AT efrei DOT fr
 *
 * 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 "common.h"
#include "io.h"

/* +AutoDec */
/* -AutoDec */

#define nop() asm volatile ("nop" ::)

/* Union for byte access. */
union _utils_byte_access
{
    uint8_t v8[4];
    uint16_t v16[2];
    uint32_t v32;
};

int
main (void)
{
    union _utils_byte_access ba;
    volatile uint32_t d = 0;
    volatile uint8_t b0 = 1, b1 = 2, b2 = 3, b3 = 4;
    nop (); // join union method.
    ba.v8[0] = b0; ba.v8[1] = b1; ba.v8[2] = b2; ba.v8[3] = b3;
    d = ba.v32;
    nop (); // join asm method.
    asm ("mov %A0, %1"	"\r\n"
	 "mov %B0, %2"	"\r\n"
	 "mov %C0, %3"	"\r\n"
	 "mov %D0, %4"	"\r\n"
	 : "=d" (d) : "r" (b0), "r" (b1), "r" (b2), "r" (b3));
    nop (); // split union method.
    ba.v32 = d;
    b2 = ba.v8[2];
    nop (); // split asm method.
    asm ("mov %0, %A1" : "=r" (b2) : "d" (d));
    nop (); // b2 = d >> 16;
    b2 = d >> 16;
    nop (); // b2 = ((uint8_t *) &d)[2];
    b2 = ((uint8_t *) &d)[2];
    nop ();
    return 0;
}