// File: sopanpatra160[P]OOO_Superscalar_Processor_Simulator/cache.cc
// Function: cache::cache(unsigned int, unsigned int, unsigned int, double, unsigned int, unsigned int)
cache::cache(unsigned int bs, unsigned int cs, unsigned int a, double rp, unsigned int wp, unsigned int lev){


	//printf("Initialization done\n");
	this->cs = cs;							//IF CACHE SIZE = 0 , SIMPLY RETURN TO NEXT INSTANTIATION AS THAT LEVEL IS UNDEFINED
	num_r=0;
	num_miss_r=0;
	num_w=0;
	num_miss_w=0;
	num_wb=0;
	num_wb_v=0;
	num_swap=0;
	mr=0;
	hit_time=0;
	mp=0;
	next = NULL;
	vptr = NULL;
	hit=0;
	miss=0;
	if(cs==0)return ;


	num_set = cs/(bs*a);
	num_bo= log2 (bs);
	num_ind= log2 (num_set);
	num_tag= 32-num_bo-num_ind; 
	
	this->bs = bs;
	this->a = a;
	this->rp = rp;
	this->wp = wp;
	this->lev= lev;

	//printf("num_set:%d",num_set);	
	//MEMORY ALLOCATION
	//(1)TAG
	tag_store = new unsigned int *[num_set];
	for (unsigned int i=0; i<num_set; i++){
		tag_store[i] = new unsigned int [a];			
	}
	for (unsigned int i=0; i<num_set; i++){
		for (unsigned int j=0; j<a; j++){
			tag_store[i][j]= 0;
		}
	}
	//(2)VALID
	valid_store = new unsigned int *[num_set];
	for (unsigned int i=0; i<num_set; i++){
		valid_store[i] = new unsigned int [a];			
	}
	for (unsigned int i=0; i<num_set; i++){
		for (unsigned int j=0; j<a; j++){
			valid_store[i][j]= 0;
		}
	}
	//LRU_STORE
	if(rp==2){
		lru_store = new unsigned int *[num_set];
		for (unsigned int i=0; i<num_set; i++){
			lru_store[i] = new unsigned int [a];			
		}
		for (unsigned int i=0; i<num_set; i++){
			for (unsigned int j=0; j<a; j++){
				lru_store[i][j]= a-j-1;
			}
		}
	}

	//LFU_BLOCK_STORE & LFU_SET_STORE
	if (rp==3){
		lfu_block_store = new unsigned int *[num_set];
		for (unsigned int i=0; i<num_set; i++){
			lfu_block_store[i] = new unsigned int [a];			
		}
		for (unsigned int i=0; i<num_set; i++){
			for (unsigned int j=0; j<a; j++){
				lfu_block_store[i][j]= 0;
			}
		}
		lfu_set_store = new unsigned int [num_set];
		for (unsigned int i=0; i<num_set; i++){
				lfu_set_store[i]= 0;
		}
	}
	//DIRTY_STORE
	if(wp==0){
		num_wb=0;
		dirty_store = new unsigned int *[num_set];
		for (unsigned int i=0; i<num_set; i++){
			dirty_store[i] = new unsigned int [a];			
		}
		for (unsigned int i=0; i<num_set; i++){
			for (unsigned int j=0; j<a; j++){
				dirty_store[i][j]= 0;
			}
		}
	}

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movsd %xmm0, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x70(%rbp)
movl -0x10(%rbp), %ecx
movl %ecx, 0x14(%rax)
movl $0x0, 0x80(%rax)
movl $0x0, 0x84(%rax)
movl $0x0, 0x88(%rax)
movl $0x0, 0x8c(%rax)
movl $0x0, 0x74(%rax)
movl $0x0, 0x78(%rax)
movl $0x0, 0x7c(%rax)
xorps %xmm0, %xmm0
movss %xmm0, 0x90(%rax)
xorps %xmm0, %xmm0
movss %xmm0, 0x94(%rax)
xorps %xmm0, %xmm0
movss %xmm0, 0x98(%rax)
movq $0x0, (%rax)
movq $0x0, 0x8(%rax)
movl $0x0, 0x9c(%rax)
movl $0x0, 0xa0(%rax)
cmpl $0x0, -0x10(%rbp)
jne 0x147c
jmp 0x19e7
movl -0x10(%rbp), %eax
movl -0xc(%rbp), %ecx
movl -0x14(%rbp), %edx
imull %edx, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x70(%rax)
movl -0xc(%rbp), %edi
callq 0x3360
movq -0x70(%rbp), %rax
cvttsd2si %xmm0, %rcx
movl %ecx, 0x58(%rax)
movl 0x70(%rax), %edi
callq 0x3360
movq -0x70(%rbp), %rax
cvttsd2si %xmm0, %rcx
movl %ecx, 0x5c(%rax)
movl 0x58(%rax), %edx
movl 0x5c(%rax), %ecx
addl %ecx, %edx
movl $0x20, %ecx
subl %edx, %ecx
movl %ecx, 0x60(%rax)
movl -0xc(%rbp), %ecx
movl %ecx, 0x10(%rax)
movl -0x14(%rbp), %ecx
movl %ecx, 0x18(%rax)
cvttsd2si -0x20(%rbp), %rcx
movl %ecx, 0x1c(%rax)
movl -0x24(%rbp), %ecx
movl %ecx, 0x20(%rax)
movl -0x28(%rbp), %ecx
movl %ecx, 0x24(%rax)
movl 0x70(%rax), %eax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x28(%rax)
movl $0x0, -0x2c(%rbp)
movq -0x70(%rbp), %rcx
movl -0x2c(%rbp), %eax
cmpl 0x70(%rcx), %eax
jae 0x1573
movl -0x14(%rbp), %eax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, %rdx
movq -0x70(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x2c(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1526
movl $0x0, -0x30(%rbp)
movq -0x70(%rbp), %rcx
movl -0x30(%rbp), %eax
cmpl 0x70(%rcx), %eax
jae 0x15c6
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x15b9
movq -0x70(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x30(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0x34(%rbp), %ecx
movl $0x0, (%rax,%rcx,4)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x158d
jmp 0x15bb
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x157a
movq -0x70(%rbp), %rax
movl 0x70(%rax), %eax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x30(%rax)
movl $0x0, -0x38(%rbp)
movq -0x70(%rbp), %rcx
movl -0x38(%rbp), %eax
cmpl 0x70(%rcx), %eax
jae 0x164d
movl -0x14(%rbp), %eax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, %rdx
movq -0x70(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x38(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x1600
movl $0x0, -0x3c(%rbp)
movq -0x70(%rbp), %rcx
movl -0x3c(%rbp), %eax
cmpl 0x70(%rcx), %eax
jae 0x16a0
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x1693
movq -0x70(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0x40(%rbp), %ecx
movl $0x0, (%rax,%rcx,4)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x1667
jmp 0x1695
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1654
movsd -0x20(%rbp), %xmm0
movsd 0x595b(%rip), %xmm1     # 0x7008
ucomisd %xmm1, %xmm0
jne 0x179e
jp 0x179e
movq -0x70(%rbp), %rax
movl 0x70(%rax), %eax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x40(%rax)
movl $0x0, -0x44(%rbp)
movq -0x70(%rbp), %rcx
movl -0x44(%rbp), %eax
cmpl 0x70(%rcx), %eax
jae 0x1744
movl -0x14(%rbp), %eax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, %rdx
movq -0x70(%rbp), %rax
movq 0x40(%rax), %rax
movl -0x44(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x16f7
movl $0x0, -0x48(%rbp)
movq -0x70(%rbp), %rcx
movl -0x48(%rbp), %eax
cmpl 0x70(%rcx), %eax
jae 0x179c
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x178f
movq -0x70(%rbp), %rax
movl -0x14(%rbp), %edx
subl -0x4c(%rbp), %edx
subl $0x1, %edx
movq 0x40(%rax), %rax
movl -0x48(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0x4c(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x175e
jmp 0x1791
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x174b
jmp 0x179e
movsd -0x20(%rbp), %xmm0
movsd 0x5865(%rip), %xmm1     # 0x7010
ucomisd %xmm1, %xmm0
jne 0x18fa
jp 0x18fa
movq -0x70(%rbp), %rax
movl 0x70(%rax), %eax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x48(%rax)
movl $0x0, -0x50(%rbp)
movq -0x70(%rbp), %rcx
movl -0x50(%rbp), %eax
cmpl 0x70(%rcx), %eax
jae 0x1842
movl -0x14(%rbp), %eax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, %rdx
movq -0x70(%rbp), %rax
movq 0x48(%rax), %rax
movl -0x50(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x17f5
movl $0x0, -0x54(%rbp)
movq -0x70(%rbp), %rcx
movl -0x54(%rbp), %eax
cmpl 0x70(%rcx), %eax
jae 0x1895
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x1888
movq -0x70(%rbp), %rax
movq 0x48(%rax), %rax
movl -0x54(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0x58(%rbp), %ecx
movl $0x0, (%rax,%rcx,4)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x185c
jmp 0x188a
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1849
movq -0x70(%rbp), %rax
movl 0x70(%rax), %eax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x50(%rax)
movl $0x0, -0x5c(%rbp)
movq -0x70(%rbp), %rcx
movl -0x5c(%rbp), %eax
cmpl 0x70(%rcx), %eax
jae 0x18f8
movq -0x70(%rbp), %rax
movq 0x50(%rax), %rax
movl -0x5c(%rbp), %ecx
movl $0x0, (%rax,%rcx,4)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x18cf
jmp 0x18fa
cmpl $0x0, -0x24(%rbp)
jne 0x19e7
movq -0x70(%rbp), %rax
movl $0x0, 0x74(%rax)
movl 0x70(%rax), %eax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x38(%rax)
movl $0x0, -0x60(%rbp)
movq -0x70(%rbp), %rcx
movl -0x60(%rbp), %eax
cmpl 0x70(%rcx), %eax
jae 0x1992
movl -0x14(%rbp), %eax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, %rdx
movq -0x70(%rbp), %rax
movq 0x38(%rax), %rax
movl -0x60(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x1945
movl $0x0, -0x64(%rbp)
movq -0x70(%rbp), %rcx
movl -0x64(%rbp), %eax
cmpl 0x70(%rcx), %eax
jae 0x19e5
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x19d8
movq -0x70(%rbp), %rax
movq 0x38(%rax), %rax
movl -0x64(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0x68(%rbp), %ecx
movl $0x0, (%rax,%rcx,4)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x19ac
jmp 0x19da
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x1999
jmp 0x19e7
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)