// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movq %rsi, %rbx
movq 0xc08(%rdi), %rsi
shrq $0x3c, %rsi
je 0x2bc5
movq %rdi, %r14
cmpb $0x3, 0x8148(%rdi)
je 0x2bc5
movl %edx, %r15d
cmpl $0x8, %esi
je 0x2ab0
leaq 0x57fe(%rip), %rdi      # 0x82a7
xorl %eax, %eax
callq 0x6b70
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 0x2bb9
movq %rbx, %rcx
shrq $0x26, %rcx
andb %al, %cl
jne 0x2bb9
movl %r15d, -0x2c(%rbp)
movabsq $0xfffffffffff, %r15    # imm = 0xFFFFFFFFFFF
andq 0xc08(%r14), %r15
movq %rbx, -0x38(%rbp)
shrq $0xc, %rbx
movq %rbx, -0x50(%rbp)
movl $0x2, %eax
shlq $0xc, %r15
movq %rax, -0x48(%rbp)
leaq (%rax,%rax,8), %rbx
movq -0x50(%rbp), %rax
movl %ebx, %ecx
shrq %cl, %rax
movq %rax, -0x58(%rbp)
andl $0x1ff, %eax            # imm = 0x1FF
movq %r15, -0x60(%rbp)
leaq (%r15,%rax,8), %r13
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %r14, %rdi
movq %r13, %rsi
callq 0x2841
movzbl %al, %eax
movl %r15d, %ecx
shlq %cl, %rax
orq %rax, %r12
addq $0x8, %r15
incq %r13
cmpq $0x40, %r15
jne 0x2b38
testb $0x1, %r12b
je 0x2ba7
movl %r12d, %r13d
shrl $0x2, %r13d
btl $0x1, %r12d
setae %al
movl %r13d, %ecx
andb %al, %cl
cmpb $0x1, %cl
je 0x2ba7
movq %rbx, -0x40(%rbp)
movl %r12d, %ecx
shrl $0x3, %ecx
movq %r12, %r15
shrq $0xa, %r15
movl %r12d, %ebx
shrl %ebx
movl %ebx, %eax
orl %ecx, %eax
testb $0x1, %al
jne 0x2bd7
movq -0x48(%rbp), %rax
subq $0x1, %rax
jae 0x2b0c
movzbl -0x2c(%rbp), %esi
movq %r14, %rdi
callq 0x2a12
movq -0x38(%rbp), %rbx
jmp 0x2bc5
movzbl %r15b, %esi
movq %r14, %rdi
callq 0x2a12
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
cmpb $0x0, -0x2c(%rbp)
jne 0x2bf2
andl $0x1, %ecx
jne 0x2bf2
leaq 0x58cb(%rip), %rdi      # 0x84b6
callq 0x1060
movb $0x1, %al
cmpb $0x1, -0x2c(%rbp)
jne 0x2c0b
andl $0x1, %ebx
jne 0x2c0b
leaq 0x58cf(%rip), %rdi      # 0x84d3
callq 0x1060
movb $0x1, %al
cmpb $0x2, -0x2c(%rbp)
jne 0x2c25
andl $0x1, %r13d
jne 0x2c25
leaq 0x58cb(%rip), %rdi      # 0x84e9
callq 0x1060
movb $0x1, %al
testb %al, %al
movl -0x2c(%rbp), %ebx
je 0x2c35
leaq 0x58de(%rip), %rdi      # 0x8511
jmp 0x2c66
cmpq $0x0, -0x48(%rbp)
jle 0x2c5b
movq $-0x1, %rdx
movq -0x40(%rbp), %rcx
shlq %cl, %rdx
notq %rdx
testq %rdx, %r15
movzbl %al, %ecx
movl $0x1, %eax
cmovel %ecx, %eax
testb %al, %al
je 0x2c73
leaq 0x589a(%rip), %rdi      # 0x8500
callq 0x1060
movzbl %bl, %esi
jmp 0x2bab
cmpb $0x2, %bl
sete %al
testb %r12b, %r12b
setns %cl
testb $0x40, %r12b
je 0x2c89
andb %cl, %al
je 0x2cd7
cmpb $0x2, %bl
movl $0x0, %ebx
sete %bl
shll $0x7, %ebx
orq %r12, %rbx
orq $0x40, %rbx
movq -0x58(%rbp), %rcx
andl $0x1ff, %ecx            # imm = 0x1FF
movq -0x60(%rbp), %rax
leaq (%rax,%rcx,8), %r12
xorl %r13d, %r13d
movq %rbx, %rax
movl %r13d, %ecx
shrq %cl, %rax
movzbl %al, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x26c6
addq $0x8, %r13
incq %r12
cmpq $0x40, %r13
jne 0x2cb3
movq -0x38(%rbp), %rbx
andl $0xfff, %ebx            # imm = 0xFFF
cmpq $0x0, -0x48(%rbp)
jle 0x2d09
movq $-0x1, %rax
movq -0x40(%rbp), %rcx
shlq %cl, %rax
notq %rax
movq -0x50(%rbp), %rdx
andq %rax, %rdx
shlq $0xc, %rdx
orq %rdx, %rbx
jmp 0x2d0d
movq -0x40(%rbp), %rcx
movq $-0x1, %rax
shlq %cl, %rax
movabsq $0xff00000000000, %rcx  # imm = 0xFF00000000000
xorq %rax, %rcx
andq %rcx, %r15
shlq $0xc, %r15
orq %r15, %rbx
jmp 0x2bc5