/*
		==     =                    *  =      =
		| -    |                        -    - 
		|  -   |  =   =  =--=  =    =    ====  
		|   -  |  |   |  |     |    |    ====  
		|    - |  |   |  |     |    |   -    - 
		=     ==  =---=  =--=  =--= =  =      =
*/

#ifndef _PAGING_H
#define _PAGING_H

#include "../Kernel.h"
#include "../GRUB.h"
#include "../Defines.h"

struct Heap;

/* A few useful defines. */
#define PAGE_SIZE				0x1000				/* 0x1000 = 4096 */

#define HEAP_STARTADDRESS		0xC0000000
#define HEAP_START_SIZE			0x100000
#define HEAP_INDEX_SIZE			0x20000
#define HEAP_MINIMUM_SIZE		0x70000
#define HEAP_MAGIC_NUMBER		0x123890AB

/* Enumeration representing access modes for Page::Access. */
enum PageAccessMode
{
	PAGE_ACCESS_READONLY =		0x0,
	PAGE_ACCESS_READWRITE =		0x1
};

/* Bitfield structure representing a page. */
struct Page
{
	unsigned Present		: 1;		/* Indicicates whether the Page is present. */
	unsigned Access			: 1;		/* In what ways is this Page accessible? See PageAccessMode for values. */
	unsigned UserMode		: 1;		/* Indicates whether the Page is supervisor mode only (Cleared) or not (Set). */
	unsigned WriteThrough	: 1;		/* Controls whether write-through caching is enabled (set to 0 for write-back caching). */
	unsigned DisableCache	: 1;		/* Can the page be cached? */
	unsigned Accessed		: 1;		/* Indicates whether the Page has been accessed (since the last refresh). */
	unsigned Dirty			: 1;		/* Indicates whether the Page is dirty (has been written to [since the last refresh]). */
	unsigned Size			: 1;		/* Size of the page. If 0, the size is 4 kB (PAGE_SIZE). */
	unsigned Global			: 1;		/* Is this page global (can be used to prevent flushing when i.e. task-switching occurs). */
	unsigned Unused			: 3;		/* 3 Unused bits, can be used for other purposes. */
	unsigned FrameAddress	: 20;		/* 20 Bits representing the frame address of the page (shifted 12 bits to the right). */
};

/* Represents a page table (table of 1024 PageS). */
struct PageTable
{
	Page Table[1024];
};

/* Represent an entire page directory. */
struct PageDirectory
{
	PageTable *Tables[1024];			/* Holds the page tables. */
	unsigned TablesPhysAddr[1024];		/* Holds the physical addresses of the page tables. */
	unsigned PhysAddr;					/* The physical address of the 'TablesPhysAddr' variable. */
};

bool AllocFrame(Page *ThePage, bool Kernel, bool Writable);		/* Allocates a frame for the specified Page. */
void FreeFrame(Page *ThePage);									/* Frees the specified Page's Frame. */

void SwitchPageDirectory(PageDirectory *NewPageDir);			/* Switches page directories. */
Page *GetPage(unsigned Address, bool Make, PageDirectory *Dir);	/* Retrieves an existing page. */

void InitPaging(/*unsigned ReserveVideoMem*/);					/* Initializes paging. ReserveVideoMem stands for the memory needed for VBE. */

/* Because of my spaghetti code, I have to make this function. */
void *AllocExtFromHeap(size_t Length, bool Align, unsigned *PhysAddr, Heap *_Heap);

#endif //_PAGING_H
