// File: /workspace/github_c_makefile_star2_O2/hobo0xcc[P]rve/src/cpu.c
// Function: Translate
uint64_t Translate(State *state, uint64_t v_addr, uint8_t access_type) {
    uint8_t MODE = ReadCSR(state, SATP, 60, 63);

    if (MODE == Bare || state->mode == MACHINE) {
        return v_addr;
    }
    if (MODE != Sv39) {
        Error("Unimplemented Translation Mode: %d", MODE);
    }

    uint64_t a;
    int64_t i;
    bool access_fault;
    bool page_fault;
    uint64_t PPN;
    uint64_t va_vpn_i;
    uint64_t pte_addr;
    uint64_t pte;
    uint8_t pte_v;
    uint8_t pte_r;
    uint8_t pte_w;
    uint8_t pte_x;
    uint8_t pte_u;
    uint8_t pte_g;
    uint8_t pte_a;
    uint8_t pte_d;
    uint8_t pte_rsw;
    uint64_t pte_ppn;
    uint64_t pa;
step1:
    // printf("step1\n");
    page_fault = false;
    uint64_t va = GetRange(v_addr, VALEN, XLEN - 1);
    if (GetRange(v_addr, VALEN - 1, VALEN - 1) == 1 && va != SetNBits(XLEN - VALEN)) {
        page_fault = true;
    }
    if (GetRange(v_addr, VALEN - 1, VALEN - 1) == 0 && va != 0) {
        page_fault = true;
    }
    if (page_fault) {
        // printf("step1 page fault\n");
        PageFault(state, access_type);
        return v_addr;
    }

step2:
    // printf("step2\n");
    PPN = ReadCSR(state, SATP, 0, 43);
    a = PPN * PAGESIZE;
    i = LEVELS - 1;

step3:
    // printf("step3\n");
    va_vpn_i = ((v_addr >> 12) >> (i * 9)) & SetNBits(9);
    pte_addr = a + va_vpn_i * PTESIZE;
    pte = MemRead64(state, pte_addr);

step4:
    // printf("step4\n");
    pte_v = pte & SetNBits(1);
    pte_r = pte >> 1 & SetNBits(1);
    pte_w = pte >> 2 & SetNBits(1);
    pte_x = pte >> 3 & SetNBits(1);
    pte_u = pte >> 4 & SetNBits(1);
    pte_g = pte >> 5 & SetNBits(1);
    pte_a = pte >> 6 & SetNBits(1);
    pte_d = pte >> 7 & SetNBits(1);
    pte_ppn = pte >> 10;
    pte_rsw = pte >> 8 & SetNBits(2);
    if (pte_v == 0 || (pte_r == 0 && pte_w == 1)) {
        // printf("step4 page fault\n");
        PageFault(state, access_type);
        return v_addr;
    }

step5:
    // printf("step5\n");
    // If pte.r=1 or pte.x=1, go to step 6. 
    if (pte_r == 1 || pte_x == 1) {
        goto step6;
    } else {
        i--;
        if (i < 0) {
            // printf("step5 page fault\n");
            PageFault(state, access_type);
            return v_addr;
        } else {
            a =  pte_ppn * PAGESIZE;
            goto step3;
        }
    }

step6:
    // printf("step6\n");
    page_fault = false;
    // When SUM=0, S-mode memory accesses to pages that areaccessible by U-mode (U=1 in Figure 4.17) will fault. 
    // if (ReadCSR(state, MSTATUS, 18, 18) == 0 && pte_u == 1) {
    //     page_fault = true;
    // }
    if (access_type == AccessInstruction) {
        if (pte_x == 0) {
            printf("AccessInstruction Page Fault\n");
            page_fault = true;
        }
    }
    if (access_type == AccessLoad) {
        // if (ReadCSR(state, MSTATUS, 19, 19) == 0) {
        if (pte_r == 0) {
            printf("AccessLoad Page Fault\n");
            page_fault = true;
        }
        // } else if (ReadCSR(state, MSTATUS, 19, 19) == 1) {
        //     if (pte_r == 0 || pte_x == 0) {
        //         page_fault = true;
        //     }
        // }
    }
    if (access_type == AccessStore) {
        if (pte_w == 0) {
            printf("AccessStore Page Fault\n");
            page_fault = true;
        }
    }

    if (page_fault) {
        printf("step6 page fault\n");
        PageFault(state, access_type);
        return v_addr;
    }

step7:
    // printf("step7\n");
    if (i > 0) {
        if ((pte_ppn & SetNBits(9 * i)) != 0) {
            page_fault = true;
        }
    }
    if (page_fault) {
        printf("step7 page fault\n");
        PageFault(state, access_type);
        return v_addr;
    }

step8:
    // printf("step8\n");
    if (pte_a == 0 || (access_type == AccessStore && pte_d == 0)) {
        uint64_t new_pte = pte | SetOneBit(6) | (access_type == AccessStore ? SetOneBit(7) : 0);
        MemWrite64(state, pte_addr, new_pte);
    }

step9:
    // printf("step9\n");
    pa = 0;
    pa |= (v_addr & SetNBits(12));
    if (i > 0) {
        pa |= (v_addr >> 12 & SetNBits(9 * i)) << 12;
    }
    pa |= (pte_ppn & (SetNBits(44) ^ SetNBits(9 * i))) << 12;
    // printf("va: 0x%llx -> pa: 0x%llx\n", v_addr, pa);
    return pa;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq 0xc08(%rdi), %rsi
shrq $0x3c, %rsi
je 0x28f0
movq %rdi, %r14
cmpb $0x3, 0x8148(%rdi)
je 0x28f0
movl %edx, %r15d
cmpl $0x8, %esi
je 0x27d0
leaq 0x4ade(%rip), %rdi      # 0x72a7
xorl %eax, %eax
callq 0x5ca8
movq %rbx, %rcx
shrq $0x27, %rcx
cmpq $0x1ffffff, %rcx        # imm = 0x1FFFFFF
setb %al
btq $0x26, %rbx
setae %dl
testq %rcx, %rcx
setne %cl
testb %dl, %cl
jne 0x288d
movq %rbx, %rcx
shrq $0x26, %rcx
andb %al, %cl
jne 0x288d
movl %r15d, -0x2c(%rbp)
movabsq $0xfffffffffff, %r15    # imm = 0xFFFFFFFFFFF
andq 0xc08(%r14), %r15
movq %rbx, %rax
shrq $0xc, %rax
movq %rax, -0x38(%rbp)
pushq $0x12
popq %r12
movq -0x38(%rbp), %rax
movl %r12d, %ecx
shrq %cl, %rax
shlq $0xc, %r15
andl $0x1ff, %eax            # imm = 0x1FF
leaq (%r15,%rax,8), %r13
movq %r14, %rdi
movq %r13, %rsi
callq 0x2696
testb $0x1, %al
je 0x28e4
movl %eax, %ecx
andl $0x2, %ecx
movl %ecx, %edx
shrl %edx
btl $0x2, %eax
setae %sil
orb %dl, %sil
je 0x28e4
movl %eax, %edx
shrl $0x3, %edx
movq %rax, %r15
shrq $0xa, %r15
movl %eax, %esi
shrl %esi
orl %edx, %esi
testb $0x1, %sil
jne 0x2893
testq %r12, %r12
je 0x28e4
addq $-0x9, %r12
jmp 0x282a
movzbl %r15b, %esi
jmp 0x28e8
cmpb $0x0, -0x2c(%rbp)
jne 0x28a9
movl %eax, %edx
andl $0x8, %edx
jne 0x28a9
leaq 0x4c0f(%rip), %rdi      # 0x74b6
jmp 0x28d3
cmpb $0x1, -0x2c(%rbp)
jne 0x28bc
testl %ecx, %ecx
jne 0x28bc
leaq 0x4c19(%rip), %rdi      # 0x74d3
jmp 0x28d3
cmpb $0x2, -0x2c(%rbp)
jne 0x2902
movl %eax, %ecx
shrl $0x2, %ecx
andl $0x1, %ecx
jne 0x2902
leaq 0x4c16(%rip), %rdi      # 0x74e9
callq 0x1060
leaq 0x4c32(%rip), %rdi      # 0x7511
callq 0x1060
movzbl -0x2c(%rbp), %esi
movq %r14, %rdi
callq 0x2732
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $-0x1
popq %rsi
testq %r12, %r12
je 0x2924
movq %rsi, %rdx
movl %r12d, %ecx
shlq %cl, %rdx
notq %rdx
testq %rdx, %r15
je 0x2924
leaq 0x4bde(%rip), %rdi      # 0x7500
jmp 0x28df
cmpb $0x2, -0x2c(%rbp)
sete %cl
testb %al, %al
setns %dl
testb $0x40, %al
je 0x2938
andb %dl, %cl
je 0x295c
xorl %edx, %edx
cmpb $0x2, -0x2c(%rbp)
sete %dl
shll $0x7, %edx
orq %rax, %rdx
orq $0x40, %rdx
movq %r14, %rdi
movq %rsi, %r14
movq %r13, %rsi
callq 0x2515
movq %r14, %rsi
andl $0xfff, %ebx            # imm = 0xFFF
testq %r12, %r12
je 0x2990
movl %r12d, %ecx
shlq %cl, %rsi
movq %rsi, %rax
notq %rax
movq -0x38(%rbp), %rcx
andq %rax, %rcx
shlq $0xc, %rcx
orq %rcx, %rbx
movabsq $0xff00000000000, %rax  # imm = 0xFF00000000000
xorq %rsi, %rax
jmp 0x299a
movabsq $-0xff00000000001, %rax # imm = 0xFFF00FFFFFFFFFFF
andq %r15, %rax
shlq $0xc, %rax
orq %rax, %rbx
jmp 0x28f0