#pragma once
#include <common.hpp>

#define RDRAM_SIZE 0x800000
#define RDRAM_DSIZE (RDRAM_SIZE - 1)
#define SRAM_SIZE 256_kb
#define SRAM_DSIZE (SRAM_SIZE - 1)
#define DMEM_SIZE 0x1000
#define DMEM_DSIZE (DMEM_SIZE - 1)
#define IMEM_SIZE 0x1000
#define IMEM_DSIZE (IMEM_SIZE - 1)
#define PIF_RAM_SIZE 0x40
#define PIF_RAM_DSIZE (PIF_RAM_SIZE - 1)
#define PIF_BOOTROM_SIZE 0x7C0
#define PIF_BOOTROM_DSIZE (PIF_BOOTROM_SIZE - 1)
#define ISVIEWER_SIZE (0x13FFFFFF - 0x13FF0020)
#define ISVIEWER_DSIZE (ISVIEWER_SIZE - 1)
#define CART_SIZE 0xFC00000
#define CART_DSIZE (CART_REGION_SIZE - 1)

#define RDRAM_REGION_START 0
#define RDRAM_REGION_END RDRAM_DSIZE
#define DMEM_REGION_START 0x4000000
#define DMEM_REGION_END (DMEM_REGION_START + DMEM_DSIZE)
#define IMEM_REGION_START 0x4001000
#define IMEM_REGION_END (IMEM_REGION_START + IMEM_DSIZE)
#define PIF_ROM_REGION_START 0x1FC00000
#define PIF_ROM_REGION_END 0x1FC007BF
#define PIF_RAM_REGION_START 0x1FC007C0
#define PIF_RAM_REGION_END 0x1FC007FF
#define CART_REGION_START_1_1 0x06000000
#define CART_REGION_START_1_2 0x10000000
#define CART_REGION_START_2_1 0x05000000
#define CART_REGION_START_2_2 0x08000000
#define CART_REGION_END_1_1 0x07FFFFFF
#define CART_REGION_END_1_2 0x1FBFFFFF
#define CART_REGION_END_2_1 0x05FFFFFF
#define CART_REGION_END_2_2 0x0FFFFFFF

#define RDRAM_REGION RDRAM_REGION_START ... RDRAM_REGION_END
#define RSP_MEM_REGION DMEM_REGION_START ... 0x0403FFFF
#define MMIO_REGION 0x04040000 ... 0x041FFFFF: case 0x04300000 ... 0x048FFFFF
#define SP_REGION 0x04040000 ... 0x040FFFFF
#define DP_CMD_REGION 0x04100000 ... 0x041FFFFF
#define RSP_REGION 0x04040000 ... 0x040FFFFF
#define RDP_REGION 0x04100000 ... 0x041FFFFF
#define MI_REGION 0x04300000 ... 0x043FFFFF
#define MI_REGION 0x04300000 ... 0x043FFFFF
#define VI_REGION 0x04400000 ... 0x044FFFFF
#define AI_REGION 0x04500000 ... 0x045FFFFF
#define PI_REGION 0x04600000 ... 0x046FFFFF
#define RI_REGION 0x04700000 ... 0x047FFFFF
#define SI_REGION 0x04800000 ... 0x048FFFFF
#define REGION_CART CART_REGION_START_2_1 ... CART_REGION_END_1_2
#define PIF_ROM_REGION PIF_ROM_REGION_START ... PIF_ROM_REGION_END
#define PIF_RAM_REGION PIF_RAM_REGION_START ... PIF_RAM_REGION_END

#define START_VREGION_KUSEG 0x00000000
#define START_VREGION_KSEG0 0x80000000
#define START_VREGION_KSEG1 0xA0000000
#define START_VREGION_KSSEG 0xC0000000
#define START_VREGION_KSEG3 0xE0000000

#define END_VREGION_KUSEG 0x7FFFFFFF
#define END_VREGION_KSEG0 0x9FFFFFFF
#define END_VREGION_KSEG1 0xBFFFFFFF
#define END_VREGION_KSSEG 0xDFFFFFFF
#define END_VREGION_KSEG3 0xFFFFFFFF

#define VREGION_KUSEG START_VREGION_KUSEG ... END_VREGION_KUSEG
#define VREGION_KSEG0 START_VREGION_KSEG0 ... END_VREGION_KSEG0
#define VREGION_KSEG1 START_VREGION_KSEG1 ... END_VREGION_KSEG1
#define VREGION_KSSEG START_VREGION_KSSEG ... END_VREGION_KSSEG
#define VREGION_KSEG3 START_VREGION_KSEG3 ... END_VREGION_KSEG3

#define DIRECT_MAP_MASK 0x1FFFFFFF

#define VREGION_XKUSEG 0x0000000000000000 ... 0x000000FFFFFFFFFF
#define VREGION_XBAD1  0x0000010000000000 ... 0x3FFFFFFFFFFFFFFF
#define VREGION_XKSSEG 0x4000000000000000 ... 0x400000FFFFFFFFFF
#define VREGION_XBAD2  0x4000010000000000 ... 0x7FFFFFFFFFFFFFFF
#define VREGION_XKPHYS 0x8000000000000000 ... 0xBFFFFFFFFFFFFFFF
#define VREGION_XKSEG  0xC000000000000000 ... 0xC00000FF7FFFFFFF
#define VREGION_XBAD3  0xC00000FF80000000 ... 0xFFFFFFFF7FFFFFFF
#define VREGION_CKSEG0 0xFFFFFFFF80000000 ... 0xFFFFFFFF9FFFFFFF
#define VREGION_CKSEG1 0xFFFFFFFFA0000000 ... 0xFFFFFFFFBFFFFFFF
#define VREGION_CKSSEG 0xFFFFFFFFC0000000 ... 0xFFFFFFFFDFFFFFFF
#define VREGION_CKSEG3 0xFFFFFFFFE0000000 ... 0xFFFFFFFFFFFFFFFF

#define SREGION_PI_UNKNOWN  0x00000000
#define SREGION_PI_64DD_REG 0x05000000
#define SREGION_PI_64DD_ROM 0x06000000
#define SREGION_PI_SRAM     0x08000000
#define SREGION_PI_ROM      0x10000000

#define EREGION_PI_UNKNOWN  0x04FFFFFF
#define EREGION_PI_64DD_REG 0x05FFFFFF
#define EREGION_PI_64DD_ROM 0x07FFFFFF
#define EREGION_PI_SRAM     0x0FFFFFFF
#define EREGION_PI_ROM      0xFFFFFFFF

#define REGION_PI_UNKNOWN  SREGION_PI_UNKNOWN  ... EREGION_PI_UNKNOWN
#define REGION_PI_64DD_REG SREGION_PI_64DD_REG ... EREGION_PI_64DD_REG
#define REGION_PI_64DD_ROM SREGION_PI_64DD_ROM ... EREGION_PI_64DD_ROM
#define REGION_PI_SRAM     SREGION_PI_SRAM     ... EREGION_PI_SRAM
#define REGION_PI_ROM      SREGION_PI_ROM      ... EREGION_PI_ROM

#define CART_ISVIEWER_FLUSH         0x13FF0014
#define SREGION_CART_ISVIEWER_BUFFER 0x13FF0020
#define EREGION_CART_ISVIEWER_BUFFER 0x13FFFFFF
#define CART_ISVIEWER_SIZE (EREGION_CART_ISVIEWER_BUFFER - SREGION_CART_ISVIEWER_BUFFER)
#define REGION_CART_ISVIEWER_BUFFER SREGION_CART_ISVIEWER_BUFFER ... EREGION_CART_ISVIEWER_BUFFER

constexpr u64 operator""_kb(unsigned long long int x) {
  return 1024ULL * x;
}

constexpr u64 operator""_mb(unsigned long long int x) {
  return 1024_kb * x;
}

constexpr u64 operator""_gb(unsigned long long int x) {
  return 1024_mb * x;
}

#define ADDRESS_RANGE_SIZE 0x80000000ull
#define PAGE_SIZE 4_kb
#define PAGE_COUNT ((ADDRESS_RANGE_SIZE) / (PAGE_SIZE))