// File: CMU-SAFARI[P]MQSim/src/ssd/Address_Mapping_Unit_Page_Level.cpp
// Function: SSD_Components::AddressMappingDomain::AddressMappingDomain(unsigned int, unsigned int, unsigned int, SSD_Components::Cached_Mapping_Table*, SSD_Components::Flash_Plane_Allocation_Scheme_Type, unsigned int*, unsigned int, unsigned int*, unsigned int, unsigned int*, unsigned int, unsigned int*, unsigned int, unsigned long, unsigned long, unsigned int)
AddressMappingDomain::AddressMappingDomain(unsigned int cmt_capacity, unsigned int cmt_entry_size, unsigned int no_of_translation_entries_per_page,
		Cached_Mapping_Table* CMT,
		Flash_Plane_Allocation_Scheme_Type PlaneAllocationScheme,
		flash_channel_ID_type* channel_ids, unsigned int channel_no, flash_chip_ID_type* chip_ids, unsigned int chip_no,
		flash_die_ID_type* die_ids, unsigned int die_no, flash_plane_ID_type* plane_ids, unsigned int plane_no,
		PPA_type total_physical_sectors_no, LHA_type total_logical_sectors_no, unsigned int sectors_no_per_page) :
		CMT_entry_size(cmt_entry_size), Translation_entries_per_page(no_of_translation_entries_per_page), No_of_inserted_entries_in_preconditioning(0),
		PlaneAllocationScheme(PlaneAllocationScheme), Channel_no(channel_no), Chip_no(chip_no), Die_no(die_no), Plane_no(plane_no)
	{
		Total_physical_pages_no = total_physical_sectors_no / sectors_no_per_page;
		max_logical_sector_address = total_logical_sectors_no;
		Total_logical_pages_no = (max_logical_sector_address / sectors_no_per_page) + (max_logical_sector_address % sectors_no_per_page == 0? 0 : 1);
		
		Channel_ids = new flash_channel_ID_type[channel_no];
		for (flash_channel_ID_type cid = 0; cid < channel_no; cid++) {
			Channel_ids[cid] = channel_ids[cid];
		}
		
		Chip_ids = new flash_chip_ID_type[chip_no];
		for (flash_chip_ID_type cid = 0; cid < chip_no; cid++) {
			Chip_ids[cid] = chip_ids[cid];
		}
		
		Die_ids = new flash_die_ID_type[die_no];
		for (flash_die_ID_type did = 0; did < die_no; did++) {
			Die_ids[did] = die_ids[did];
		}
		
		Plane_ids = new flash_plane_ID_type[plane_no];
		for (flash_plane_ID_type pid = 0; pid < plane_no; pid++) {
			Plane_ids[pid] = plane_ids[pid];
		}

		GlobalMappingTable = new GMTEntryType[Total_logical_pages_no];
		for (unsigned int i = 0; i < Total_logical_pages_no; i++) {
			GlobalMappingTable[i].PPA = NO_PPA;
			GlobalMappingTable[i].WrittenStateBitmap = UNWRITTEN_LOGICAL_PAGE;
			GlobalMappingTable[i].TimeStamp = 0;
		}

		//If CMT is NULL, then each address mapping domain should create its own CMT
		if (CMT == NULL) {
			//Each flow (address mapping domain) has its own CMT, so CMT is create here in the constructor
			this->CMT = new Cached_Mapping_Table(cmt_capacity);
		} else {
			//The entire CMT space is shared among concurrently running flows (i.e., address mapping domains of all flow)
			this->CMT = CMT;
		}

		Total_translation_pages_no = MVPN_type(Total_logical_pages_no / Translation_entries_per_page);
		GlobalTranslationDirectory = new GTDEntryType[Total_translation_pages_no + 1];
		for (MVPN_type i = 0; i <= Total_translation_pages_no; i++) {
			GlobalTranslationDirectory[i].MPPN = (MPPN_type)NO_MPPN;
			GlobalTranslationDirectory[i].TimeStamp = INVALID_TIME_STAMP;
		}
	}

// Assembly:
subq $0xe8, %rsp
movl 0x140(%rsp), %eax
movq 0x138(%rsp), %rax
movq 0x130(%rsp), %rax
movl 0x128(%rsp), %eax
movq 0x120(%rsp), %rax
movl 0x118(%rsp), %eax
movq 0x110(%rsp), %rax
movl 0x108(%rsp), %eax
movq 0x100(%rsp), %rax
movl 0xf8(%rsp), %eax
movq 0xf0(%rsp), %rax
movq %rdi, 0xe0(%rsp)
movl %esi, 0xdc(%rsp)
movl %edx, 0xd8(%rsp)
movl %ecx, 0xd4(%rsp)
movq %r8, 0xc8(%rsp)
movl %r9d, 0xc4(%rsp)
movq 0xe0(%rsp), %rdi
movq %rdi, 0x88(%rsp)
movl 0xd8(%rsp), %eax
movl %eax, 0x8(%rdi)
movl 0xd4(%rsp), %eax
movl %eax, 0xc(%rdi)
movl $0x0, 0x18(%rdi)
addq $0x28, %rdi
movq %rdi, 0x30(%rsp)
callq 0x58db0
movq 0x88(%rsp), %rdi
addq $0x58, %rdi
movq %rdi, 0x38(%rsp)
callq 0x58db0
movq 0x88(%rsp), %rdi
addq $0x88, %rdi
movq %rdi, 0x40(%rsp)
callq 0x58dc0
movq 0x88(%rsp), %rdi
addq $0xb8, %rdi
movq %rdi, 0x48(%rsp)
callq 0x58dd0
movq 0x88(%rsp), %rdi
addq $0xe8, %rdi
movq %rdi, 0x50(%rsp)
callq 0x21570
movq 0x88(%rsp), %rdi
addq $0x118, %rdi            # imm = 0x118
movq %rdi, 0x58(%rsp)
callq 0x58dd0
movq 0x88(%rsp), %rdi
addq $0x148, %rdi            # imm = 0x148
movq %rdi, 0x60(%rsp)
callq 0x58db0
movq 0x88(%rsp), %rdi
addq $0x178, %rdi            # imm = 0x178
movq %rdi, 0x68(%rsp)
callq 0x58db0
movq 0x88(%rsp), %rdi
addq $0x1a8, %rdi            # imm = 0x1A8
movq %rdi, 0x70(%rsp)
callq 0x58dd0
movq 0x88(%rsp), %rdi
addq $0x1d8, %rdi            # imm = 0x1D8
movq %rdi, 0x78(%rsp)
callq 0x58dd0
movq 0x88(%rsp), %rax
movl 0xc4(%rsp), %ecx
movl %ecx, 0x208(%rax)
movl 0xf8(%rsp), %ecx
movl %ecx, 0x218(%rax)
movl 0x108(%rsp), %ecx
movl %ecx, 0x228(%rax)
movl 0x118(%rsp), %ecx
movl %ecx, 0x238(%rax)
movl 0x128(%rsp), %ecx
movl %ecx, 0x248(%rax)
movq 0x130(%rsp), %rax
movl 0x140(%rsp), %ecx
xorl %edx, %edx
movq %rdx, 0x80(%rsp)
divq %rcx
movq 0x80(%rsp), %rdx
movq %rax, %rcx
movq 0x88(%rsp), %rax
movq %rcx, 0x260(%rax)
movq 0x138(%rsp), %rcx
movq %rcx, 0x250(%rax)
movq 0x250(%rax), %rax
movl 0x140(%rsp), %ecx
divq %rcx
movq %rax, %rcx
movq 0x88(%rsp), %rax
subq $0x1, %rdx
sbbq $-0x1, %rcx
movq %rcx, 0x258(%rax)
movl 0xf8(%rsp), %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x8030
movq %rax, 0x90(%rsp)
jmp 0x4b0c3
movq 0x88(%rsp), %rax
movq 0x90(%rsp), %rcx
movq %rcx, 0x210(%rax)
movl $0x0, 0xb0(%rsp)
movl 0xb0(%rsp), %eax
cmpl 0xf8(%rsp), %eax
jae 0x4b14c
movq 0x88(%rsp), %rax
movq 0xf0(%rsp), %rcx
movl 0xb0(%rsp), %edx
movl (%rcx,%rdx,4), %edx
movq 0x210(%rax), %rax
movl 0xb0(%rsp), %ecx
movl %edx, (%rax,%rcx,4)
movl 0xb0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb0(%rsp)
jmp 0x4b0e5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
jmp 0x4b4e9
movl 0x108(%rsp), %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x8030
movq %rax, 0x28(%rsp)
jmp 0x4b165
movq 0x88(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, 0x220(%rax)
movl $0x0, 0xac(%rsp)
movl 0xac(%rsp), %eax
cmpl 0x108(%rsp), %eax
jae 0x4b1d2
movq 0x88(%rsp), %rax
movq 0x100(%rsp), %rcx
movl 0xac(%rsp), %edx
movl (%rcx,%rdx,4), %edx
movq 0x220(%rax), %rax
movl 0xac(%rsp), %ecx
movl %edx, (%rax,%rcx,4)
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x4b184
movl 0x118(%rsp), %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x8030
movq %rax, 0x20(%rsp)
jmp 0x4b1eb
movq 0x88(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x230(%rax)
movl $0x0, 0xa8(%rsp)
movl 0xa8(%rsp), %eax
cmpl 0x118(%rsp), %eax
jae 0x4b258
movq 0x88(%rsp), %rax
movq 0x110(%rsp), %rcx
movl 0xa8(%rsp), %edx
movl (%rcx,%rdx,4), %edx
movq 0x230(%rax), %rax
movl 0xa8(%rsp), %ecx
movl %edx, (%rax,%rcx,4)
movl 0xa8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa8(%rsp)
jmp 0x4b20a
movl 0x128(%rsp), %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x8030
movq %rax, 0x18(%rsp)
jmp 0x4b271
movq 0x88(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0x240(%rax)
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
cmpl 0x128(%rsp), %eax
jae 0x4b2de
movq 0x88(%rsp), %rax
movq 0x120(%rsp), %rcx
movl 0xa4(%rsp), %edx
movl (%rcx,%rdx,4), %edx
movq 0x240(%rax), %rax
movl 0xa4(%rsp), %ecx
movl %edx, (%rax,%rcx,4)
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x4b290
movq 0x88(%rsp), %rax
movq 0x258(%rax), %rax
movl $0x18, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x8030
movq %rax, 0x10(%rsp)
jmp 0x4b30f
movq 0x88(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, 0x20(%rax)
movl $0x0, 0xa0(%rsp)
movq 0x88(%rsp), %rcx
movl 0xa0(%rsp), %eax
cmpq 0x258(%rcx), %rax
jae 0x4b3ab
movq 0x88(%rsp), %rax
movq 0x20(%rax), %rcx
movl 0xa0(%rsp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq $-0x1, (%rcx)
movq 0x20(%rax), %rcx
movl 0xa0(%rsp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq $0x0, 0x8(%rcx)
movq 0x20(%rax), %rax
movl 0xa0(%rsp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq $0x0, 0x10(%rax)
movl 0xa0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa0(%rsp)
jmp 0x4b32b
cmpq $0x0, 0xc8(%rsp)
jne 0x4b410
movl $0x58, %edi
callq 0x8450
movq %rax, 0x8(%rsp)
jmp 0x4b3c7
movq 0x8(%rsp), %rdi
movl 0xdc(%rsp), %esi
callq 0x4a2b0
jmp 0x4b3da
movq 0x88(%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rcx, 0x10(%rax)
jmp 0x4b424
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
callq 0x8410
jmp 0x4b4e9
movq 0x88(%rsp), %rax
movq 0xc8(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq 0x88(%rsp), %rcx
movq 0x258(%rcx), %rax
movl 0xc(%rcx), %ecx
xorl %edx, %edx
divq %rcx
movq %rax, %rcx
movq 0x88(%rsp), %rax
movl %ecx, 0x268(%rax)
movl 0x268(%rax), %eax
incl %eax
movl %eax, %edi
shlq $0x4, %rdi
callq 0x8030
movq %rax, (%rsp)
jmp 0x4b465
movq 0x88(%rsp), %rax
movq (%rsp), %rcx
movq %rcx, (%rax)
movl $0x0, 0x9c(%rsp)
movq 0x88(%rsp), %rcx
movl 0x9c(%rsp), %eax
cmpl 0x268(%rcx), %eax
ja 0x4b4e1
movq 0x88(%rsp), %rax
movq (%rax), %rcx
movl 0x9c(%rsp), %edx
shlq $0x4, %rdx
addq %rdx, %rcx
movl $0xffffffff, (%rcx)     # imm = 0xFFFFFFFF
movq (%rax), %rax
movl 0x9c(%rsp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq $-0x1, 0x8(%rax)
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0x4b47f
addq $0xe8, %rsp
retq
movq 0x78(%rsp), %rdi
callq 0x58de0
movq 0x70(%rsp), %rdi
callq 0x58de0
movq 0x68(%rsp), %rdi
callq 0x58df0
movq 0x60(%rsp), %rdi
callq 0x58df0
movq 0x58(%rsp), %rdi
callq 0x58de0
movq 0x50(%rsp), %rdi
callq 0x20b60
movq 0x48(%rsp), %rdi
callq 0x58de0
movq 0x40(%rsp), %rdi
callq 0x58e00
movq 0x38(%rsp), %rdi
callq 0x58df0
movq 0x30(%rsp), %rdi
callq 0x58df0
movq 0xb8(%rsp), %rdi
callq 0x8770
nopw (%rax,%rax)