// File: sangwoojun[P]rv32emulator/emulator.cpp
// Function: mem_read(unsigned char*, unsigned int, instr_type)
uint32_t mem_read(uint8_t* mem, uint32_t addr, instr_type op) {
	uint32_t ret = 0;
	int bytes = 0;

	switch(op) {
		case LB: 
		case LBU: bytes = 1; break;
		case LH:
		case LHU: bytes = 2; break;
		case LW:  bytes = 4; break;
	}
	if ( addr + bytes <= MEM_BYTES ) {
		mem_read_reqs ++;
		int way = cache_peek(addr,bytes);
		if ( way < 0 ) {
			cache_flush(addr, mem);
			mem_flush_words += CACHE_LINE_WORD;
			uint32_t waddr = addr-(addr&((1<<(2+CACHE_LINE_WORD_SZ))-1));
			for ( int i = 0; i < CACHE_LINE_WORD; i++ ) {
				cache_update(waddr+(i*4), *(uint32_t*)&(mem[waddr+(i*4)]));
			}
		} else {
			cache_read_hits++;
			//printf( "%x %d--\n", addr,way);
		}
		uint32_t cr = cache_read(addr,bytes);
		switch(op) {
			case LB: ret = signextend(cr, 8); break;
			case LBU: ret = (cr&0xff); break;
			case LH: ret = signextend(cr,16); break;
			case LHU: ret = (cr&0xffff); break;
			case LW: ret = cr; break;
		}
	}
	//printf( "Reading %x from %d\n", ret, addr );

	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x10(%rbp), %eax
movl %eax, -0x2c(%rbp)
addl $-0xe, %eax
subl $0x2, %eax
jb 0x3a27
jmp 0x3a10
movl -0x2c(%rbp), %eax
addl $-0x10, %eax
subl $0x2, %eax
jb 0x3a30
jmp 0x3a1d
movl -0x2c(%rbp), %eax
subl $0x13, %eax
je 0x3a39
jmp 0x3a40
movl $0x1, -0x18(%rbp)
jmp 0x3a40
movl $0x2, -0x18(%rbp)
jmp 0x3a40
movl $0x4, -0x18(%rbp)
movl -0xc(%rbp), %eax
addl -0x18(%rbp), %eax
cmpl $0x10000, %eax          # imm = 0x10000
ja 0x3b63
movl 0xf465(%rip), %eax      # 0x12ebc
addl $0x1, %eax
movl %eax, 0xf45c(%rip)      # 0x12ebc
movl -0xc(%rbp), %edi
movl -0x18(%rbp), %esi
callq 0x26a0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0x3adb
movl -0xc(%rbp), %edi
movq -0x8(%rbp), %rsi
callq 0x2b00
movl 0xf42a(%rip), %eax      # 0x12eb0
addl $0x1, %eax
movl %eax, 0xf421(%rip)      # 0x12eb0
movl -0xc(%rbp), %eax
movl -0xc(%rbp), %ecx
andl $0x3, %ecx
subl %ecx, %eax
movl %eax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
cmpl $0x1, -0x24(%rbp)
jge 0x3ad9
movl -0x20(%rbp), %edi
movl -0x24(%rbp), %eax
shll $0x2, %eax
addl %eax, %edi
movq -0x8(%rbp), %rax
movl -0x20(%rbp), %ecx
movl -0x24(%rbp), %edx
shll $0x2, %edx
addl %edx, %ecx
movl %ecx, %ecx
movl (%rax,%rcx), %esi
callq 0x29f0
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x3aa4
jmp 0x3aea
movl 0xf3df(%rip), %eax      # 0x12ec0
addl $0x1, %eax
movl %eax, 0xf3d6(%rip)      # 0x12ec0
movl -0xc(%rbp), %edi
movl -0x18(%rbp), %esi
callq 0x28b0
movl %eax, -0x28(%rbp)
movl -0x10(%rbp), %eax
addl $-0xe, %eax
movl %eax, %ecx
movq %rcx, -0x38(%rbp)
subl $0x5, %eax
ja 0x3b61
movq -0x38(%rbp), %rax
leaq 0x7514(%rip), %rcx      # 0xb028
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0x28(%rbp), %edi
movl $0x8, %esi
callq 0x2c50
movl %eax, -0x14(%rbp)
jmp 0x3b61
movl -0x28(%rbp), %eax
andl $0xff, %eax
movl %eax, -0x14(%rbp)
jmp 0x3b61
movl -0x28(%rbp), %edi
movl $0x10, %esi
callq 0x2c50
movl %eax, -0x14(%rbp)
jmp 0x3b61
movl -0x28(%rbp), %eax
andl $0xffff, %eax           # imm = 0xFFFF
movl %eax, -0x14(%rbp)
jmp 0x3b61
movl -0x28(%rbp), %eax
movl %eax, -0x14(%rbp)
jmp 0x3b63
movl -0x14(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)