/*
    MemoryLayout.h, Copyright  (c) by Silvio Dragone & David Reist,
    Swiss Federal Institute of Technology,
    Computer Engineering and Networks Laboratory.
 
    TOPSY -- A Teachable Operating System.
             Implementation of a tiny and simple
             micro kernel for teaching purposes.
 
    For further information, please visit http://www.tik.ee.ethz.ch/~topsy
 
    This software is provided under the terms of the GNU General Public Licence.
 
    A full copy of the GNU GPL is provided in the file COPYING found in the 
    development root of Topsy.
*/

#ifndef _MEMORYLAYOUT_H_
#define _MEMORYLAYOUT_H_

#include "cpu.h"

/* we use ARM small pages (4KB) in a coarse second-level table */

/* PAGESIZE is the core's real page size */
#define REGIONBITS          20
#define REGIONSIZE          (1 << REGIONBITS)  /* 1MB */

#define PAGEBITS            12
#define PAGESIZE            (1 << PAGEBITS)    /* 4KB */
#define PAGEMASK            (PAGESIZE - 1)
#define PAGEFRAMEMASK       (~PAGEMASK)
#define PAGENUMBER(addr)    (((unsigned long)addr) >> PAGEBITS)
#define PAGEREMAINDER(addr) (((unsigned long)addr) & PAGEMASK)
#define PAGETOADDR(addr)    ((void *)(((unsigned long)addr) << PAGEBITS))
#define PAGEROUNDUP(addr)   ((((unsigned long)addr) + PAGEMASK) & PAGEFRAMEMASK )
#define PAGEROUNDDOWN(addr) (((unsigned long)addr) & PAGEFRAMEMASK )
#define PAGETODMAPPED(pagenr)	PAGETOADDR(pagenr)
#define DMAPPEDTOPAGE(addr)		PAGENUMBER(addr)
#define DMAPPEDTOPHYS(addr)		(addr)

/* logical page size is the granularity of vm regions
 * this may be smaller on a direct mapped memory system. 
 * if paging is used, LOGICAL and PHYSICAL have to be the same size 
 */

/* we use same size for logical and physical
 */

#define LOGICALPAGEBITS            12
#define LOGICALPAGESIZE            (1 << LOGICALPAGEBITS)  /* 4KB */
#define LOGICALPAGEFRAMEMASK       (0xffffffff << LOGICALPAGEBITS)
#define LOGICALPAGEMASK            (~LOGICALPAGEFRAMEMASK)
#define LOGICALPAGENUMBER(addr)    (((unsigned long)addr) >> LOGICALPAGEBITS)
#define LOGICALPAGEREMAINDER(addr) (((unsigned long)addr) & LOGICALPAGEMASK)

/* used as a boot and later as an exception stack */
#define BOOTSTACKSIZE	           2*PAGESIZE
#define BOOTSTACKBOTTOM	         (BOOTSTACK + EXCSEG_SIZE)
#define BOOTSTACKTOP	           (BOOTSTACKBOTTOM+BOOTSTACKSIZE-4)

/* the steal region is in the kernel mapable region and has a virtual size of 4MB */
#define STEAL_START STEALALLOC
#define STEAL_SIZE  (4 * 1024 * 1024)

#endif
