// File: /workspace/github_cpp_makefile_star2_O2/QFSW[P]MIPS1-Simulator/src/MemoryMap.hpp
// Function: unsigned short Clarkitechture::MIPS::MemoryMap::read<unsigned short>(unsigned long)
T read(size_t address)
            {
                if (address % sizeof(T) > 0) { throw BadMemoryAccess(address, "read data address was misaligned"); }
                else if (address >= ADDR_GETC && address <= ADDR_GETC + 4 - sizeof(T))
                {
                    int input = std::getchar();
                    if (input == EOF || feof(stdin))
                    {
                        int error = ferror(stdin);
                        if (error) { throw BadIO("stdin", error); }
                        else { return -1; }
                    }
                    else if (address == ADDR_GETC + 4 - sizeof(T)) { return input; }
                    else { return 0; }
                }
                else if (address >= ADDR_DATA && address < ADDR_DATA + ADDR_DATA_LENGTH)
                {
                    T data = 0;
                    size_t memLocation = address - ADDR_DATA;
                    for (int i = 0; i < sizeof(T); i++)
                    {
                        int shamt = 8 * (sizeof(T) - i - 1);
                        data |= (memory[memLocation + i] << shamt);
                    }   

                    return data;
                }
                else if (address >= ADDR_INSTR && address < ADDR_INSTR + instrMemorySize)
                {
                    byte* memLocation = instrMemory + (address - ADDR_INSTR);
                    T data = *reinterpret_cast<T*>(memLocation);
                    return bswap(data);
                }
                else if (address >= ADDR_INSTR && address < ADDR_INSTR + ADDR_INSTR_LENGTH) { return 0; }
                else { throw BadMemoryAccess(address, "address was outside of the valid R/W memory range."); }
            }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x70, %rsp
movq %rsi, %r14
testb $0x1, %r14b
jne 0x6d71
leaq -0x30000000(%r14), %rax
cmpq $0x2, %rax
ja 0x6cc8
callq 0x52c0
cmpl $-0x1, %eax
je 0x6c59
movl %eax, %ebx
movq 0xa37f(%rip), %rax      # 0x10fc8
movq (%rax), %rdi
callq 0x5310
testl %eax, %eax
je 0x6d56
movq 0xa368(%rip), %rax      # 0x10fc8
movq (%rax), %rdi
callq 0x53c0
movw $0xffff, %bp            # imm = 0xFFFF
testl %eax, %eax
je 0x6d62
movl %eax, %r14d
pushq $0x50
popq %rdi
callq 0x50f0
movq %rax, %rbx
leaq 0x3778(%rip), %rsi      # 0xa401
leaq 0x30(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x67c8
movb $0x1, %bpl
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
movl %r14d, %edx
callq 0x5a62
xorl %ebp, %ebp
leaq 0x913c(%rip), %rsi      # 0xfdf0
leaq -0xcdd(%rip), %rdx      # 0x5fde
movq %rbx, %rdi
callq 0x5360
jmp 0x6e0b
movq %rdi, %rbx
movq %r14, %rax
andq $-0x4000000, %rax       # imm = 0xFC000000
cmpq $0x20000000, %rax       # imm = 0x20000000
jne 0x6d17
addl $0xe0000000, %r14d      # imm = 0xE0000000
pushq $0x8
popq %r15
xorl %ebp, %ebp
leaq 0xc(%rsp), %r12
cmpl $-0x8, %r15d
je 0x6d62
movl %r14d, 0xc(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x7440
movzbl (%rax), %eax
movl %r15d, %ecx
shll %cl, %eax
orl %eax, %ebp
addl $-0x8, %r15d
incl %r14d
jmp 0x6cee
cmpq $0x10000000, %r14       # imm = 0x10000000
jb 0x6d41
movl $0x10000000, %eax       # imm = 0x10000000
addq 0x40(%rbx), %rax
cmpq %r14, %rax
jbe 0x6d41
movq 0x38(%rbx), %rax
movzwl -0x10000000(%rax,%r14), %ebp
rolw $0x8, %bp
jmp 0x6d62
movq %r14, %rax
andq $-0x1000000, %rax       # imm = 0xFF000000
cmpq $0x10000000, %rax       # imm = 0x10000000
jne 0x6dbf
xorl %ebp, %ebp
jmp 0x6d62
xorl %ebp, %ebp
cmpq $0x30000002, %r14       # imm = 0x30000002
cmovel %ebx, %ebp
movl %ebp, %eax
addq $0x70, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
pushq $0x50
popq %rdi
callq 0x50f0
movq %rax, %rbx
leaq 0x365d(%rip), %rsi      # 0xa3e0
leaq 0x50(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x67c8
movb $0x1, %bpl
leaq 0x50(%rsp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5722
xorl %ebp, %ebp
leaq 0x8ffa(%rip), %rsi      # 0xfda8
leaq -0xe79(%rip), %rdx      # 0x5f3c
movq %rbx, %rdi
callq 0x5360
jmp 0x6e0b
pushq $0x50
popq %rdi
callq 0x50f0
movq %rax, %rbx
leaq 0x3636(%rip), %rsi      # 0xa407
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x67c8
movb $0x1, %bpl
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5722
xorl %ebp, %ebp
leaq 0x8fac(%rip), %rsi      # 0xfda8
leaq -0xec7(%rip), %rdx      # 0x5f3c
movq %rbx, %rdi
callq 0x5360
movq %rax, %r14
leaq 0x10(%rsp), %rdi
jmp 0x6e2b
jmp 0x6e37
movq %rax, %r14
leaq 0x30(%rsp), %rdi
jmp 0x6e2b
jmp 0x6e37
movq %rax, %r14
leaq 0x50(%rsp), %rdi
callq 0x50d0
testb %bpl, %bpl
jne 0x6e3a
jmp 0x6e42
movq %rax, %r14
movq %rbx, %rdi
callq 0x5170
movq %r14, %rdi
callq 0x5390