src/memory.h

changeset 40
239bc48590ba
child 70
5bbe76e71698
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/memory.h	Thu Dec 02 23:03:13 2010 +0000
     1.3 @@ -0,0 +1,72 @@
     1.4 +#ifndef _MEMORY_H
     1.5 +#define _MEMORY_H
     1.6 +
     1.7 +/***********************************
     1.8 + * Array read/write utility macros
     1.9 + * "Don't Repeat Yourself" :)
    1.10 + ***********************************/
    1.11 +
    1.12 +/// Array read, 32-bit
    1.13 +#define RD32(array, address, andmask)							\
    1.14 +	(((uint32_t)array[(address + 0) & (andmask)] << 24) |		\
    1.15 +	 ((uint32_t)array[(address + 1) & (andmask)] << 16) |		\
    1.16 +	 ((uint32_t)array[(address + 2) & (andmask)] << 8)  |		\
    1.17 +	 ((uint32_t)array[(address + 3) & (andmask)]))
    1.18 +
    1.19 +/// Array read, 16-bit
    1.20 +#define RD16(array, address, andmask)							\
    1.21 +	(((uint32_t)array[(address + 0) & (andmask)] << 8)  |		\
    1.22 +	 ((uint32_t)array[(address + 1) & (andmask)]))
    1.23 +
    1.24 +/// Array read, 8-bit
    1.25 +#define RD8(array, address, andmask)							\
    1.26 +	((uint32_t)array[(address + 0) & (andmask)])
    1.27 +
    1.28 +/// Array write, 32-bit
    1.29 +#define WR32(array, address, andmask, value) {					\
    1.30 +	array[(address + 0) & (andmask)] = (value >> 24) & 0xff;	\
    1.31 +	array[(address + 1) & (andmask)] = (value >> 16) & 0xff;	\
    1.32 +	array[(address + 2) & (andmask)] = (value >> 8)  & 0xff;	\
    1.33 +	array[(address + 3) & (andmask)] =  value        & 0xff;	\
    1.34 +}
    1.35 +
    1.36 +/// Array write, 16-bit
    1.37 +#define WR16(array, address, andmask, value) {					\
    1.38 +	array[(address + 0) & (andmask)] = (value >> 8)  & 0xff;	\
    1.39 +	array[(address + 1) & (andmask)] =  value        & 0xff;	\
    1.40 +}
    1.41 +
    1.42 +/// Array write, 8-bit
    1.43 +#define WR8(array, address, andmask, value)						\
    1.44 +	array[(address + 0) & (andmask)] =  value        & 0xff;
    1.45 +
    1.46 +/******************
    1.47 + * Memory mapping
    1.48 + ******************/
    1.49 +
    1.50 +typedef enum {
    1.51 +	MEM_ALLOWED = 0,
    1.52 +	MEM_PAGEFAULT,		// Page fault -- page not present
    1.53 +	MEM_PAGE_NO_WE,		// Page not write enabled
    1.54 +	MEM_KERNEL,			// User attempted to access kernel memory
    1.55 +	MEM_UIE				// User Nonmemory Location Access
    1.56 +} MEM_STATUS;
    1.57 +
    1.58 +/**
    1.59 + * @brief 	Check memory access permissions for a given address.
    1.60 + * @param	addr		Address.
    1.61 + * @param	writing		true if writing to memory, false if reading.
    1.62 + * @return	One of the MEM_STATUS constants, specifying whether the access is
    1.63 + * 			permitted, or what error occurred.
    1.64 + */
    1.65 +MEM_STATUS checkMemoryAccess(uint32_t addr, bool writing);
    1.66 +
    1.67 +/**
    1.68 + * @brief	Map a CPU memory address into physical memory space.
    1.69 + * @param	addr		Address.
    1.70 + * @param	writing		true if writing to memory, false if reading.
    1.71 + * @return	Address, remapped into physical memory.
    1.72 + */
    1.73 +uint32_t mapAddr(uint32_t addr, bool writing);
    1.74 +
    1.75 +#endif