// File: sopanpatra160[P]OOO_Superscalar_Processor_Simulator/cache.cc
// Function: cache::read(unsigned int)
unsigned int cache::read(unsigned int addr){

	//printf("Entered read \n");
	unsigned int addr_ev;
	num_r++;
	//printf("Access no. %d\n",num_r);
	calc_tag_ind(addr);

	hit=0;
	miss=0;
		//printf("level = %d\n",lev);
	if(lev == 1){

		//#1 check for the element in all the blocks of that set
		for (unsigned int j=0; j<a; j++){
			if((tag_store[ind][j]==tag) && (valid_store[ind][j]==1)){
				if(rp==2)
					lru_update(ind,j);
				else if(rp==3)
					lfu_update_block(ind,j,1);
				hit=1;
				//printf("L1 Hit \n");				
				return 1;
			}
		}
		if(vptr != NULL){
			unsigned int tag_vc = vptr->calc_tag(addr); // tag reconstruction
			unsigned int found_vc=0;
			unsigned int found_loc = 0;
			for (unsigned int j=0; j<vptr->a; j++){
				if((vptr->tag_store[0][j]==tag_vc) && (vptr->valid_store[0][j]==1)){
					found_vc = 1;
					found_loc = j;
				}
			}
			if(found_vc==1){
				//unsigned int tag_vc = vptr->calc_tag(addr);
				num_swap+=1;
				swap(found_loc,0);
				return 1;
			}
		}
		num_miss_r++; // MISS in L1
		miss=1;
		//printf("L1 Miss \n");
		
		//#2 MISS in L1 but invalid case
		for (unsigned int j=0; j<a; j++){
			if(valid_store[ind][j]==0){
				if(next!=NULL)
					next->read(addr);
				tag_store[ind][j]=tag;
				valid_store[ind][j]=1;
				if(rp==2)
					lru_update(ind,j);
				else if(rp ==3) 
					lfu_update_block(ind,j,0);

				return 0;
			}
		}
		
		//#3 MISS IN L1 FOR ALL BITS VALID : EVICTION

		if(rp==2){
			ev_blk= lru_block2evict(ind);
		}
		
		else if(rp==3){
			ev_blk= lfu_block2evict(ind);
		}

		//See if victim cache is present
		if(vptr != NULL){
			addr_ev = ev_blk_addr(ind,tag_store[ind][ev_blk]);
			
			vptr->insert_vc(addr_ev,dirty_store[ind][ev_blk]);
			if(next!=NULL)
				next->read(addr);
			tag_store[ind][ev_blk]=tag;
			dirty_store[ind][ev_blk]=0;
		}
		
		else if(vptr == NULL){
			if(dirty_store[ind][ev_blk]==0){
				if(next!=NULL)
					next->read(addr);
				tag_store[ind][ev_blk]=tag;
			}
			if(dirty_store[ind][ev_blk]==1){
				num_wb+=1;
				addr_ev = ev_blk_addr(ind,tag_store[ind][ev_blk]);
				if(next!=NULL)
					next->write(addr_ev);
				if(next!=NULL)
					next->read(addr);
				tag_store[ind][ev_blk]=tag;
				dirty_store[ind][ev_blk]=0;
			}
		}

		//update LRU/LFU counters
		if(rp==2){
			lru_update(ind,ev_blk);
		}
		else if(rp==3){
			lfu_update_set(ind,ev_blk);
			lfu_update_block(ind,ev_blk,0);
		}
		return 0;
	}	

	else if(lev == 2){
		//#1 check for the element in all the blocks of that set
		for (unsigned int j=0; j<a; j++){
			if((tag_store[ind][j]==tag) && (valid_store[ind][j]==1)){
				if(rp==2)
					lru_update(ind,j);
				else if(rp==3)
					lfu_update_block(ind,j,1);
				hit=1;
				return 1;
			}
		}
		miss=1;	
		num_miss_r++;
		
		//#2 MISS in L1 but invalid case
		for (unsigned int j=0; j<a; j++){
			if(valid_store[ind][j]==0){
				tag_store[ind][j]=tag;
				valid_store[ind][j]=1;
				if(rp==2)
					lru_update(ind,j);
				else 
					lfu_update_block(ind,j,0);
			}
		}
		
		//#3 MISS IN L1 FOR ALL BITS VALID : EVICTION
		if(rp==2){
			ev_blk= lru_block2evict(ind);
		}
		//LFU policy
		else if(rp==3){
			ev_blk= lfu_block2evict(ind);
		}

		if(wp==0){
			if(dirty_store[ind][ev_blk]==0){
				tag_store[ind][ev_blk]=tag;
			}
			if(dirty_store[ind][ev_blk]==1){
				num_wb+=1;
				tag_store[ind][ev_blk]=tag;
				dirty_store[ind][ev_blk]=0;
			}
		}

		if(rp==2){
			lru_update(ind,ev_blk);
		}
		else if(rp==3){
			lfu_update_set(ind,ev_blk);
			lfu_update_block(ind,ev_blk,0);
		}
		return 0;
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x40(%rbp)
movl 0x80(%rdi), %eax
addl $0x1, %eax
movl %eax, 0x80(%rdi)
movl -0x14(%rbp), %esi
callq 0x2170
movq -0x40(%rbp), %rax
movl $0x0, 0x9c(%rax)
movl $0x0, 0xa0(%rax)
cmpl $0x1, 0x24(%rax)
jne 0x1ec4
movl $0x0, -0x1c(%rbp)
movq -0x40(%rbp), %rcx
movl -0x1c(%rbp), %eax
cmpl 0x18(%rcx), %eax
jae 0x1aee
movq -0x40(%rbp), %rcx
movq 0x28(%rcx), %rax
movl 0x68(%rcx), %edx
movq (%rax,%rdx,8), %rax
movl -0x1c(%rbp), %edx
movl (%rax,%rdx,4), %eax
cmpl 0x64(%rcx), %eax
jne 0x1ade
movq -0x40(%rbp), %rcx
movq 0x30(%rcx), %rax
movl 0x68(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movl -0x1c(%rbp), %ecx
cmpl $0x1, (%rax,%rcx,4)
jne 0x1ade
movq -0x40(%rbp), %rax
cmpl $0x2, 0x1c(%rax)
jne 0x1aa4
movq -0x40(%rbp), %rdi
movl 0x68(%rdi), %esi
movl -0x1c(%rbp), %edx
callq 0x21b0
jmp 0x1ac4
movq -0x40(%rbp), %rax
cmpl $0x3, 0x1c(%rax)
jne 0x1ac2
movq -0x40(%rbp), %rdi
movl 0x68(%rdi), %esi
movl -0x1c(%rbp), %edx
movl $0x1, %ecx
callq 0x2250
jmp 0x1ac4
movq -0x40(%rbp), %rax
movl $0x1, 0x9c(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x2158
jmp 0x1ae0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1a47
movq -0x40(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x1bac
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x22c0
movl %eax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x40(%rbp), %rcx
movl -0x2c(%rbp), %eax
movq 0x8(%rcx), %rcx
cmpl 0x18(%rcx), %eax
jae 0x1b81
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movl -0x2c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
cmpl -0x20(%rbp), %eax
jne 0x1b74
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x30(%rax), %rax
movq (%rax), %rax
movl -0x2c(%rbp), %ecx
cmpl $0x1, (%rax,%rcx,4)
jne 0x1b74
movl $0x1, -0x24(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x28(%rbp)
jmp 0x1b76
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1b25
cmpl $0x1, -0x24(%rbp)
jne 0x1baa
movq -0x40(%rbp), %rdi
movl 0x7c(%rdi), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rdi)
movl -0x28(%rbp), %esi
xorl %edx, %edx
callq 0x22f0
movl $0x1, -0x4(%rbp)
jmp 0x2158
jmp 0x1bac
movq -0x40(%rbp), %rax
movl 0x84(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x84(%rax)
movl $0x1, 0xa0(%rax)
movl $0x0, -0x30(%rbp)
movq -0x40(%rbp), %rcx
movl -0x30(%rbp), %eax
cmpl 0x18(%rcx), %eax
jae 0x1c92
movq -0x40(%rbp), %rcx
movq 0x30(%rcx), %rax
movl 0x68(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movl -0x30(%rbp), %ecx
cmpl $0x0, (%rax,%rcx,4)
jne 0x1c82
movq -0x40(%rbp), %rax
cmpq $0x0, (%rax)
je 0x1c15
movq -0x40(%rbp), %rax
movq (%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x19f0
movq -0x40(%rbp), %rax
movl 0x64(%rax), %esi
movq 0x28(%rax), %rcx
movl 0x68(%rax), %edx
movq (%rcx,%rdx,8), %rcx
movl -0x30(%rbp), %edx
movl %esi, (%rcx,%rdx,4)
movq 0x30(%rax), %rcx
movl 0x68(%rax), %edx
movq (%rcx,%rdx,8), %rcx
movl -0x30(%rbp), %edx
movl $0x1, (%rcx,%rdx,4)
cmpl $0x2, 0x1c(%rax)
jne 0x1c59
movq -0x40(%rbp), %rdi
movl 0x68(%rdi), %esi
movl -0x30(%rbp), %edx
callq 0x21b0
jmp 0x1c76
movq -0x40(%rbp), %rax
cmpl $0x3, 0x1c(%rax)
jne 0x1c74
movq -0x40(%rbp), %rdi
movl 0x68(%rdi), %esi
movl -0x30(%rbp), %edx
xorl %ecx, %ecx
callq 0x2250
jmp 0x1c76
movl $0x0, -0x4(%rbp)
jmp 0x2158
jmp 0x1c84
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1bd0
movq -0x40(%rbp), %rax
cmpl $0x2, 0x1c(%rax)
jne 0x1cb3
movq -0x40(%rbp), %rdi
movl 0x68(%rdi), %esi
callq 0x2490
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x6c(%rax)
jmp 0x1cd4
movq -0x40(%rbp), %rax
cmpl $0x3, 0x1c(%rax)
jne 0x1cd2
movq -0x40(%rbp), %rdi
movl 0x68(%rdi), %esi
callq 0x2500
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x6c(%rax)
jmp 0x1cd4
movq -0x40(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x1d6f
movq -0x40(%rbp), %rdi
movl 0x68(%rdi), %esi
movq 0x28(%rdi), %rax
movl 0x68(%rdi), %ecx
movq (%rax,%rcx,8), %rax
movl 0x6c(%rdi), %ecx
movl (%rax,%rcx,4), %edx
callq 0x2570
movq -0x40(%rbp), %rcx
movl %eax, -0x18(%rbp)
movq 0x8(%rcx), %rdi
movl -0x18(%rbp), %esi
movq 0x38(%rcx), %rax
movl 0x68(%rcx), %edx
movq (%rax,%rdx,8), %rax
movl 0x6c(%rcx), %ecx
movl (%rax,%rcx,4), %edx
callq 0x25b0
movq -0x40(%rbp), %rax
cmpq $0x0, (%rax)
je 0x1d3d
movq -0x40(%rbp), %rax
movq (%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x19f0
movq -0x40(%rbp), %rcx
movl 0x64(%rcx), %esi
movq 0x28(%rcx), %rax
movl 0x68(%rcx), %edx
movq (%rax,%rdx,8), %rax
movl 0x6c(%rcx), %edx
movl %esi, (%rax,%rdx,4)
movq 0x38(%rcx), %rax
movl 0x68(%rcx), %edx
movq (%rax,%rdx,8), %rax
movl 0x6c(%rcx), %ecx
movl $0x0, (%rax,%rcx,4)
jmp 0x1e71
movq -0x40(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x1e6f
movq -0x40(%rbp), %rcx
movq 0x38(%rcx), %rax
movl 0x68(%rcx), %edx
movq (%rax,%rdx,8), %rax
movl 0x6c(%rcx), %ecx
cmpl $0x0, (%rax,%rcx,4)
jne 0x1dc7
movq -0x40(%rbp), %rax
cmpq $0x0, (%rax)
je 0x1daf
movq -0x40(%rbp), %rax
movq (%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x19f0
movq -0x40(%rbp), %rcx
movl 0x64(%rcx), %edx
movq 0x28(%rcx), %rax
movl 0x68(%rcx), %esi
movq (%rax,%rsi,8), %rax
movl 0x6c(%rcx), %ecx
movl %edx, (%rax,%rcx,4)
movq -0x40(%rbp), %rcx
movq 0x38(%rcx), %rax
movl 0x68(%rcx), %edx
movq (%rax,%rdx,8), %rax
movl 0x6c(%rcx), %ecx
cmpl $0x1, (%rax,%rcx,4)
jne 0x1e6d
movq -0x40(%rbp), %rdi
movl 0x74(%rdi), %eax
addl $0x1, %eax
movl %eax, 0x74(%rdi)
movl 0x68(%rdi), %esi
movq 0x28(%rdi), %rax
movl 0x68(%rdi), %ecx
movq (%rax,%rcx,8), %rax
movl 0x6c(%rdi), %ecx
movl (%rax,%rcx,4), %edx
callq 0x2570
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, -0x18(%rbp)
cmpq $0x0, (%rax)
je 0x1e27
movq -0x40(%rbp), %rax
movq (%rax), %rdi
movl -0x18(%rbp), %esi
callq 0x27c0
movq -0x40(%rbp), %rax
cmpq $0x0, (%rax)
je 0x1e40
movq -0x40(%rbp), %rax
movq (%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x19f0
movq -0x40(%rbp), %rcx
movl 0x64(%rcx), %esi
movq 0x28(%rcx), %rax
movl 0x68(%rcx), %edx
movq (%rax,%rdx,8), %rax
movl 0x6c(%rcx), %edx
movl %esi, (%rax,%rdx,4)
movq 0x38(%rcx), %rax
movl 0x68(%rcx), %edx
movq (%rax,%rdx,8), %rax
movl 0x6c(%rcx), %ecx
movl $0x0, (%rax,%rcx,4)
jmp 0x1e6f
jmp 0x1e71
movq -0x40(%rbp), %rax
cmpl $0x2, 0x1c(%rax)
jne 0x1e8c
movq -0x40(%rbp), %rdi
movl 0x68(%rdi), %esi
movl 0x6c(%rdi), %edx
callq 0x21b0
jmp 0x1eb8
movq -0x40(%rbp), %rax
cmpl $0x3, 0x1c(%rax)
jne 0x1eb6
movq -0x40(%rbp), %rdi
movl 0x68(%rdi), %esi
movl 0x6c(%rdi), %edx
callq 0x2fe0
movq -0x40(%rbp), %rdi
movl 0x68(%rdi), %esi
movl 0x6c(%rdi), %edx
xorl %ecx, %ecx
callq 0x2250
jmp 0x1eb8
movl $0x0, -0x4(%rbp)
jmp 0x2158
movq -0x40(%rbp), %rax
cmpl $0x2, 0x24(%rax)
jne 0x214f
movl $0x0, -0x34(%rbp)
movq -0x40(%rbp), %rcx
movl -0x34(%rbp), %eax
cmpl 0x18(%rcx), %eax
jae 0x1f80
movq -0x40(%rbp), %rcx
movq 0x28(%rcx), %rax
movl 0x68(%rcx), %edx
movq (%rax,%rdx,8), %rax
movl -0x34(%rbp), %edx
movl (%rax,%rdx,4), %eax
cmpl 0x64(%rcx), %eax
jne 0x1f70
movq -0x40(%rbp), %rcx
movq 0x30(%rcx), %rax
movl 0x68(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movl -0x34(%rbp), %ecx
cmpl $0x1, (%rax,%rcx,4)
jne 0x1f70
movq -0x40(%rbp), %rax
cmpl $0x2, 0x1c(%rax)
jne 0x1f36
movq -0x40(%rbp), %rdi
movl 0x68(%rdi), %esi
movl -0x34(%rbp), %edx
callq 0x21b0
jmp 0x1f56
movq -0x40(%rbp), %rax
cmpl $0x3, 0x1c(%rax)
jne 0x1f54
movq -0x40(%rbp), %rdi
movl 0x68(%rdi), %esi
movl -0x34(%rbp), %edx
movl $0x1, %ecx
callq 0x2250
jmp 0x1f56
movq -0x40(%rbp), %rax
movl $0x1, 0x9c(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x2158
jmp 0x1f72
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1ed9
movq -0x40(%rbp), %rax
movl $0x1, 0xa0(%rax)
movl 0x84(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x84(%rax)
movl $0x0, -0x38(%rbp)
movq -0x40(%rbp), %rcx
movl -0x38(%rbp), %eax
cmpl 0x18(%rcx), %eax
jae 0x202f
movq -0x40(%rbp), %rcx
movq 0x30(%rcx), %rax
movl 0x68(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movl -0x38(%rbp), %ecx
cmpl $0x0, (%rax,%rcx,4)
jne 0x201f
movq -0x40(%rbp), %rax
movl 0x64(%rax), %esi
movq 0x28(%rax), %rcx
movl 0x68(%rax), %edx
movq (%rcx,%rdx,8), %rcx
movl -0x38(%rbp), %edx
movl %esi, (%rcx,%rdx,4)
movq 0x30(%rax), %rcx
movl 0x68(%rax), %edx
movq (%rcx,%rdx,8), %rcx
movl -0x38(%rbp), %edx
movl $0x1, (%rcx,%rdx,4)
cmpl $0x2, 0x1c(%rax)
jne 0x200c
movq -0x40(%rbp), %rdi
movl 0x68(%rdi), %esi
movl -0x38(%rbp), %edx
callq 0x21b0
jmp 0x201d
movq -0x40(%rbp), %rdi
movl 0x68(%rdi), %esi
movl -0x38(%rbp), %edx
xorl %ecx, %ecx
callq 0x2250
jmp 0x201f
jmp 0x2021
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x1fa4
movq -0x40(%rbp), %rax
cmpl $0x2, 0x1c(%rax)
jne 0x2050
movq -0x40(%rbp), %rdi
movl 0x68(%rdi), %esi
callq 0x2490
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x6c(%rax)
jmp 0x2071
movq -0x40(%rbp), %rax
cmpl $0x3, 0x1c(%rax)
jne 0x206f
movq -0x40(%rbp), %rdi
movl 0x68(%rdi), %esi
callq 0x2500
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x6c(%rax)
jmp 0x2071
movq -0x40(%rbp), %rax
cmpl $0x0, 0x20(%rax)
jne 0x20ff
movq -0x40(%rbp), %rcx
movq 0x38(%rcx), %rax
movl 0x68(%rcx), %edx
movq (%rax,%rdx,8), %rax
movl 0x6c(%rcx), %ecx
cmpl $0x0, (%rax,%rcx,4)
jne 0x20af
movq -0x40(%rbp), %rcx
movl 0x64(%rcx), %edx
movq 0x28(%rcx), %rax
movl 0x68(%rcx), %esi
movq (%rax,%rsi,8), %rax
movl 0x6c(%rcx), %ecx
movl %edx, (%rax,%rcx,4)
movq -0x40(%rbp), %rcx
movq 0x38(%rcx), %rax
movl 0x68(%rcx), %edx
movq (%rax,%rdx,8), %rax
movl 0x6c(%rcx), %ecx
cmpl $0x1, (%rax,%rcx,4)
jne 0x20fd
movq -0x40(%rbp), %rcx
movl 0x74(%rcx), %eax
addl $0x1, %eax
movl %eax, 0x74(%rcx)
movl 0x64(%rcx), %esi
movq 0x28(%rcx), %rax
movl 0x68(%rcx), %edx
movq (%rax,%rdx,8), %rax
movl 0x6c(%rcx), %edx
movl %esi, (%rax,%rdx,4)
movq 0x38(%rcx), %rax
movl 0x68(%rcx), %edx
movq (%rax,%rdx,8), %rax
movl 0x6c(%rcx), %ecx
movl $0x0, (%rax,%rcx,4)
jmp 0x20ff
movq -0x40(%rbp), %rax
cmpl $0x2, 0x1c(%rax)
jne 0x211a
movq -0x40(%rbp), %rdi
movl 0x68(%rdi), %esi
movl 0x6c(%rdi), %edx
callq 0x21b0
jmp 0x2146
movq -0x40(%rbp), %rax
cmpl $0x3, 0x1c(%rax)
jne 0x2144
movq -0x40(%rbp), %rdi
movl 0x68(%rdi), %esi
movl 0x6c(%rdi), %edx
callq 0x2fe0
movq -0x40(%rbp), %rdi
movl 0x68(%rdi), %esi
movl 0x6c(%rdi), %edx
xorl %ecx, %ecx
callq 0x2250
jmp 0x2146
movl $0x0, -0x4(%rbp)
jmp 0x2158
jmp 0x2151
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)