1 /* memory access abstraction layer for forth kernel
3 * Copyright (C) 2005 Stefan Reinauer
5 * See the file "COPYING" for further information about
6 * the copyright and warranty status of this work.
12 /* The forthstrap compiler has to abstract the underlying dictionary
13 * type: big/little endian, 32/64bit. All other binaries shall use
14 * unchanged memory access for performance.
19 #ifndef SWAP_ENDIANNESS
21 /* trivial case - we don't have to change anything */
22 #define read_ucell(addr) (*(ucell *)(addr))
23 #define read_cell(addr) (*(cell *)(addr))
24 #define read_long(addr) (*(u32 *)(addr))
25 #define read_word(addr) (*(u16 *)(addr))
26 #define read_byte(addr) (*(u8 *)(addr))
28 #define write_ucell(addr, value) {*(ucell *)(addr)=(value);}
29 #define write_cell(addr, value) {*(cell *)(addr)=(value);}
30 #define write_long(addr, value) {*(u32 *)(addr)=(value);}
31 #define write_word(addr, value) {*(u16 *)(addr)=(value);}
32 #define write_byte(addr, value) {*(u8 *)(addr)=(value);}
34 #define target_ucell(x) (x)
35 #define target_cell(x) (x)
36 #define target_long(x) (x)
37 #define target_ulong(x) (x)
39 #else /* SWAP_ENDIANNESS */
41 #define target_word(value) ( (((value)>>8)&0xff) | (((value)&0xff)<<8) )
42 #define target_long(value) ( (((value)&0xff000000)>>24)|(((value)&0x00ff0000)>>8)|(((value)&0xff00)<<8)|(((value)&0xff)<<24) )
43 #define target_ulong(value) (target_long(value))
46 #define target_ucell(value) ((ucell)target_long(value))
47 #define target_cell(value) ((cell)target_long(value))
49 #define target_ucell(value) \
50 ((((ucell)target_long((value) & 0xffffffff)) << 32) | \
51 ((ucell)target_long((value) >> 32)))
52 #define target_cell(value) \
53 ((((cell)target_long((value) & 0xffffffff)) << 32) | \
54 ((cell)target_long((value) >> 32)))
56 #error "Endianness not supported. Please report."
59 #define read_ucell(addr) target_ucell(*(ucell *)(addr))
60 #define read_cell(addr) target_cell(*(cell *)(addr))
61 #define read_long(addr) target_long(*(u32 *)(addr))
62 #define read_word(addr) target_word(*(u16 *)(addr))
63 #define read_byte(addr) (*(u8 *)(addr))
65 #define write_ucell(addr, value) {*(ucell *)(addr)=target_ucell(value);}
66 #define write_cell(addr, value) {*(cell *)(addr)=target_cell(value);}
67 #define write_long(addr, value) {*(u32 *)(addr)=target_long(value);}
68 #define write_word(addr, value) {*(u16 *)(addr)=target_word(value);}
69 #define write_byte(addr, value) {*(u8 *)(addr)=(value);}
72 #ifdef CONFIG_LITTLE_ENDIAN
73 #define unaligned_read_word(addr) \
74 (read_byte(addr)|(read_byte((u8 *)addr+1)<<8))
76 #define unaligned_read_long(addr) \
77 (unaligned_read_word(addr)|(unaligned_read_word((u8 *)addr+2)<<16))
79 #define unaligned_write_word(addr, value) \
80 write_byte(addr, (value & 0xff)); write_byte((u8 *)(addr+1), (value>>8))
82 #define unaligned_write_long(addr, value) \
83 unaligned_write_word(addr, (value & 0xffff)); \
84 unaligned_write_word((addr + 2), (value >> 16))
88 #ifdef CONFIG_BIG_ENDIAN
89 #define unaligned_read_word(addr) \
90 ((read_byte(addr)<<8)|read_byte((u8 *)addr+1))
92 #define unaligned_read_long(addr) \
93 ((unaligned_read_word(addr)<<16)|unaligned_read_word((u8 *)addr+2))
95 #define unaligned_write_word(addr, value) \
96 write_byte(addr, (value >> 8)); write_byte((u8 *)(addr+1), (value & 0xff))
98 #define unaligned_write_long(addr, value) \
99 unaligned_write_word(addr, (value >> 16)); \
100 unaligned_write_word((addr + 2), (value & 0xffff))
103 /* bit width handling */
105 #ifdef NATIVE_BITWIDTH_EQUALS_HOST_BITWIDTH
107 #define FMT_CELL_x "x"
108 #define FMT_CELL_d "d"
110 #define FMT_CELL_x "llx"
111 #define FMT_CELL_d "lld"
115 #ifdef NATIVE_BITWIDTH_SMALLER_THAN_HOST_BITWIDTH
116 extern unsigned long base_address
;
117 #define FMT_CELL_x "x"
118 #define FMT_CELL_d "d"
121 #ifdef NATIVE_BITWIDTH_LARGER_THAN_HOST_BITWIDTH
122 #define FMT_CELL_x "llx"
123 #define FMT_CELL_d "lld"