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