// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movapd %xmm0, %xmm1
movq %rcx, (%rsp)
movl %edx, 0x14(%rdi)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rdi)
movupd %xmm0, 0x74(%rdi)
movupd %xmm0, 0x84(%rdi)
movupd %xmm0, 0x94(%rdi)
testl %edx, %edx
je 0x16f3
movl %r9d, %r15d
movl %r8d, %ebp
movl %esi, %r12d
movq %rdi, %r14
movq (%rsp), %rbx
movl %ebx, %ecx
imull %esi, %ecx
movl %edx, %eax
xorl %edx, %edx
divl %ecx
movl %eax, 0x70(%rdi)
movl %esi, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd %xmm1, 0x8(%rsp)
callq 0x1150
cvttsd2si %xmm0, %rax
movl %eax, 0x58(%r14)
movl 0x70(%r14), %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
callq 0x1150
cvttsd2si %xmm0, %rax
movl %eax, 0x5c(%r14)
addl 0x58(%r14), %eax
movl $0x20, %ecx
subl %eax, %ecx
movl %ecx, 0x60(%r14)
movl %r12d, 0x10(%r14)
movl %ebx, 0x18(%r14)
cvttsd2si 0x8(%rsp), %rax
movl %eax, 0x1c(%r14)
movl %ebp, 0x14(%rsp)
movl %ebp, 0x20(%r14)
movl %r15d, 0x24(%r14)
movl 0x70(%r14), %r13d
leaq (,%r13,8), %rdi
callq 0x1030
movq %rax, 0x28(%r14)
movl %ebx, %r15d
testq %r13, %r13
je 0x148d
leaq (,%r15,4), %r12
xorl %ebx, %ebx
movq %r12, %rdi
callq 0x1030
movq 0x28(%r14), %rcx
movq %rax, (%rcx,%rbx,8)
incq %rbx
cmpq %rbx, %r13
jne 0x1475
movl 0x70(%r14), %ebx
testl %ebx, %ebx
je 0x14c4
movq 0x28(%r14), %r13
leaq (,%r15,4), %r12
xorl %ebp, %ebp
cmpl $0x0, (%rsp)
je 0x14b8
movq (%r13,%rbp,8), %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x1070
incq %rbp
movl 0x70(%r14), %ebx
cmpq %rbx, %rbp
jb 0x14a3
movl %ebx, %r13d
leaq (,%r13,8), %rdi
callq 0x1030
movq %rax, 0x30(%r14)
testl %ebx, %ebx
je 0x14fe
leaq (,%r15,4), %r12
xorl %ebx, %ebx
movq %r12, %rdi
callq 0x1030
movq 0x30(%r14), %rcx
movq %rax, (%rcx,%rbx,8)
incq %rbx
cmpq %rbx, %r13
jne 0x14e6
movl 0x70(%r14), %ebp
testl %ebp, %ebp
movsd 0x8(%rsp), %xmm0
je 0x1541
movq 0x30(%r14), %r13
leaq (,%r15,4), %r12
xorl %ebx, %ebx
cmpl $0x0, (%rsp)
je 0x1535
movq (%r13,%rbx,8), %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x1070
movsd 0x8(%rsp), %xmm0
incq %rbx
movl 0x70(%r14), %ebp
cmpq %rbp, %rbx
jb 0x151a
ucomisd 0x2abf(%rip), %xmm0     # 0x4008
jne 0x15c9
jp 0x15c9
movl %ebp, %r13d
leaq (,%r13,8), %rdi
callq 0x1030
movq %rax, 0x40(%r14)
testl %ebp, %ebp
je 0x1587
leaq (,%r15,4), %r12
xorl %ebx, %ebx
movq %r12, %rdi
callq 0x1030
movq 0x40(%r14), %rcx
movq %rax, (%rcx,%rbx,8)
incq %rbx
cmpq %rbx, %r13
jne 0x156f
cmpl $0x0, 0x70(%r14)
movsd 0x8(%rsp), %xmm0
je 0x15c9
movq 0x40(%r14), %rax
movq (%rsp), %rcx
decl %ecx
xorl %edx, %edx
cmpl $0x0, (%rsp)
je 0x15bd
movq (%rax,%rdx,8), %rsi
movl %ecx, %edi
xorl %r8d, %r8d
movl %edi, (%rsi,%r8,4)
incq %r8
decl %edi
cmpq %r8, %r15
jne 0x15af
incq %rdx
movl 0x70(%r14), %esi
cmpq %rsi, %rdx
jb 0x15a0
ucomisd 0x2a3f(%rip), %xmm0     # 0x4010
movl 0x14(%rsp), %ebp
jne 0x167a
jp 0x167a
movl 0x70(%r14), %r13d
leaq (,%r13,8), %rdi
callq 0x1030
movq %rax, 0x48(%r14)
testq %r13, %r13
je 0x161d
leaq (,%r15,4), %r12
xorl %ebx, %ebx
movq %r12, %rdi
callq 0x1030
movq 0x48(%r14), %rcx
movq %rax, (%rcx,%rbx,8)
incq %rbx
cmpq %rbx, %r13
jne 0x1605
movl 0x70(%r14), %eax
testl %eax, %eax
je 0x1654
movq 0x48(%r14), %r13
leaq (,%r15,4), %r12
xorl %ebx, %ebx
cmpl $0x0, (%rsp)
je 0x1648
movq (%r13,%rbx,8), %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x1070
incq %rbx
movl 0x70(%r14), %eax
cmpq %rax, %rbx
jb 0x1633
movl %eax, %edi
shlq $0x2, %rdi
callq 0x1030
movq %rax, 0x50(%r14)
movl 0x70(%r14), %edx
testq %rdx, %rdx
je 0x167a
shlq $0x2, %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x1070
testl %ebp, %ebp
jne 0x16f3
movl $0x0, 0x74(%r14)
movl 0x70(%r14), %r13d
leaq (,%r13,8), %rdi
callq 0x1030
movq %rax, 0x38(%r14)
testq %r13, %r13
je 0x16c2
leaq (,%r15,4), %r12
xorl %ebx, %ebx
movq %r12, %rdi
callq 0x1030
movq 0x38(%r14), %rcx
movq %rax, (%rcx,%rbx,8)
incq %rbx
cmpq %rbx, %r13
jne 0x16aa
cmpl $0x0, 0x70(%r14)
je 0x16f3
movq 0x38(%r14), %r12
shlq $0x2, %r15
xorl %ebx, %ebx
cmpl $0x0, (%rsp)
je 0x16e7
movq (%r12,%rbx,8), %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x1070
incq %rbx
movl 0x70(%r14), %eax
cmpq %rax, %rbx
jb 0x16d3
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq