// File: /workspace/github_c_makefile_star2_O0/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
subq $0x120, %rsp            # imm = 0x120
movb %dl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movb %al, -0x19(%rbp)
movq -0x10(%rbp), %rdi
movl $0x180, %esi            # imm = 0x180
movl $0x3c, %edx
movl $0x3f, %ecx
callq 0x1600
movb %al, -0x1a(%rbp)
movzbl -0x1a(%rbp), %eax
cmpl $0x0, %eax
je 0x3d4c
movq -0x10(%rbp), %rax
movzbl 0x8148(%rax), %eax
cmpl $0x3, %eax
jne 0x3d59
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x4302
movzbl -0x1a(%rbp), %eax
cmpl $0x8, %eax
je 0x3d74
movzbl -0x1a(%rbp), %esi
leaq 0xb662(%rip), %rdi      # 0xf3cf
movb $0x0, %al
callq 0xd690
jmp 0x3d76
movb $0x0, -0x32(%rbp)
movq -0x18(%rbp), %rdi
movl $0x27, %esi
movl $0x3f, %edx
callq 0x1310
movq %rax, -0x80(%rbp)
movq -0x18(%rbp), %rdi
movl $0x26, %edx
movq %rdx, %rsi
callq 0x1310
cmpq $0x1, %rax
jne 0x3dd0
movq -0x80(%rbp), %rax
movq %rax, -0x90(%rbp)
movl $0x19, %edi
callq 0x12b0
movq %rax, %rcx
movq -0x90(%rbp), %rax
cmpq %rcx, %rax
je 0x3dd0
movb $0x1, -0x32(%rbp)
movq -0x18(%rbp), %rdi
movl $0x26, %edx
movq %rdx, %rsi
callq 0x1310
cmpq $0x0, %rax
jne 0x3df2
cmpq $0x0, -0x80(%rbp)
je 0x3df2
movb $0x1, -0x32(%rbp)
testb $0x1, -0x32(%rbp)
je 0x3e12
movq -0x10(%rbp), %rdi
movzbl -0x19(%rbp), %esi
callq 0x3c70
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x4302
jmp 0x3e14
movq -0x10(%rbp), %rdi
movl $0x180, %esi            # imm = 0x180
xorl %edx, %edx
movl $0x2b, %ecx
callq 0x1600
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
shlq $0xc, %rax
movq %rax, -0x28(%rbp)
movq $0x2, -0x30(%rbp)
movq -0x18(%rbp), %rax
shrq $0xc, %rax
imulq $0x9, -0x30(%rbp), %rcx
shrq %cl, %rax
movq %rax, -0x98(%rbp)
movl $0x9, %edi
callq 0x12b0
movq %rax, %rcx
movq -0x98(%rbp), %rax
andq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq -0x48(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x3b70
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0xe0(%rbp)
movl $0x1, %edi
callq 0x12b0
movq %rax, %rcx
movq -0xe0(%rbp), %rax
andq %rcx, %rax
movb %al, -0x59(%rbp)
movq -0x58(%rbp), %rax
shrq %rax
movq %rax, -0xd8(%rbp)
movl $0x1, %edi
callq 0x12b0
movq %rax, %rcx
movq -0xd8(%rbp), %rax
andq %rcx, %rax
movb %al, -0x5a(%rbp)
movq -0x58(%rbp), %rax
shrq $0x2, %rax
movq %rax, -0xd0(%rbp)
movl $0x1, %edi
callq 0x12b0
movq %rax, %rcx
movq -0xd0(%rbp), %rax
andq %rcx, %rax
movb %al, -0x5b(%rbp)
movq -0x58(%rbp), %rax
shrq $0x3, %rax
movq %rax, -0xc8(%rbp)
movl $0x1, %edi
callq 0x12b0
movq %rax, %rcx
movq -0xc8(%rbp), %rax
andq %rcx, %rax
movb %al, -0x5c(%rbp)
movq -0x58(%rbp), %rax
shrq $0x4, %rax
movq %rax, -0xc0(%rbp)
movl $0x1, %edi
callq 0x12b0
movq %rax, %rcx
movq -0xc0(%rbp), %rax
andq %rcx, %rax
movb %al, -0x5d(%rbp)
movq -0x58(%rbp), %rax
shrq $0x5, %rax
movq %rax, -0xb8(%rbp)
movl $0x1, %edi
callq 0x12b0
movq %rax, %rcx
movq -0xb8(%rbp), %rax
andq %rcx, %rax
movb %al, -0x5e(%rbp)
movq -0x58(%rbp), %rax
shrq $0x6, %rax
movq %rax, -0xb0(%rbp)
movl $0x1, %edi
callq 0x12b0
movq %rax, %rcx
movq -0xb0(%rbp), %rax
andq %rcx, %rax
movb %al, -0x5f(%rbp)
movq -0x58(%rbp), %rax
shrq $0x7, %rax
movq %rax, -0xa8(%rbp)
movl $0x1, %edi
callq 0x12b0
movq %rax, %rcx
movq -0xa8(%rbp), %rax
andq %rcx, %rax
movb %al, -0x60(%rbp)
movq -0x58(%rbp), %rax
shrq $0xa, %rax
movq %rax, -0x70(%rbp)
movq -0x58(%rbp), %rax
shrq $0x8, %rax
movq %rax, -0xa0(%rbp)
movl $0x2, %edi
callq 0x12b0
movq %rax, %rcx
movq -0xa0(%rbp), %rax
andq %rcx, %rax
movb %al, -0x61(%rbp)
movzbl -0x59(%rbp), %eax
cmpl $0x0, %eax
je 0x402a
movzbl -0x5a(%rbp), %eax
cmpl $0x0, %eax
jne 0x4044
movzbl -0x5b(%rbp), %eax
cmpl $0x1, %eax
jne 0x4044
movq -0x10(%rbp), %rdi
movzbl -0x19(%rbp), %esi
callq 0x3c70
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x4302
jmp 0x4046
movzbl -0x5a(%rbp), %eax
cmpl $0x1, %eax
je 0x4058
movzbl -0x5c(%rbp), %eax
cmpl $0x1, %eax
jne 0x405a
jmp 0x4098
movq -0x30(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jge 0x4087
movq -0x10(%rbp), %rdi
movzbl -0x19(%rbp), %esi
callq 0x3c70
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x4302
movq -0x70(%rbp), %rax
shlq $0xc, %rax
movq %rax, -0x28(%rbp)
jmp 0x3e41
movb $0x0, -0x32(%rbp)
movzbl -0x19(%rbp), %eax
cmpl $0x0, %eax
jne 0x40c2
movzbl -0x5c(%rbp), %eax
cmpl $0x0, %eax
jne 0x40c0
leaq 0xb33d(%rip), %rdi      # 0xf3f2
movb $0x0, %al
callq 0x10a0
movb $0x1, -0x32(%rbp)
jmp 0x40c2
movzbl -0x19(%rbp), %eax
cmpl $0x1, %eax
jne 0x40e8
movzbl -0x5a(%rbp), %eax
cmpl $0x0, %eax
jne 0x40e6
leaq 0xb335(%rip), %rdi      # 0xf410
movb $0x0, %al
callq 0x10a0
movb $0x1, -0x32(%rbp)
jmp 0x40e8
movzbl -0x19(%rbp), %eax
cmpl $0x2, %eax
jne 0x410e
movzbl -0x5b(%rbp), %eax
cmpl $0x0, %eax
jne 0x410c
leaq 0xb326(%rip), %rdi      # 0xf427
movb $0x0, %al
callq 0x10a0
movb $0x1, -0x32(%rbp)
jmp 0x410e
testb $0x1, -0x32(%rbp)
je 0x413c
leaq 0xb324(%rip), %rdi      # 0xf43f
movb $0x0, %al
callq 0x10a0
movq -0x10(%rbp), %rdi
movzbl -0x19(%rbp), %esi
callq 0x3c70
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x4302
jmp 0x413e
cmpq $0x0, -0x30(%rbp)
jle 0x4175
movq -0x70(%rbp), %rax
movq %rax, -0xe8(%rbp)
imulq $0x9, -0x30(%rbp), %rax
movl %eax, %edi
callq 0x12b0
movq %rax, %rcx
movq -0xe8(%rbp), %rax
andq %rcx, %rax
cmpq $0x0, %rax
je 0x4173
movb $0x1, -0x32(%rbp)
jmp 0x4175
testb $0x1, -0x32(%rbp)
je 0x41a3
leaq 0xb2cf(%rip), %rdi      # 0xf451
movb $0x0, %al
callq 0x10a0
movq -0x10(%rbp), %rdi
movzbl -0x19(%rbp), %esi
callq 0x3c70
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x4302
jmp 0x41a5
movzbl -0x5f(%rbp), %eax
cmpl $0x0, %eax
je 0x41c4
movzbl -0x19(%rbp), %eax
cmpl $0x2, %eax
jne 0x4240
movzbl -0x60(%rbp), %eax
cmpl $0x0, %eax
jne 0x4240
movq -0x58(%rbp), %rax
movq %rax, -0xf8(%rbp)
movl $0x6, %edi
callq 0x12f0
movq %rax, %rcx
movq -0xf8(%rbp), %rax
orq %rcx, %rax
movq %rax, -0xf0(%rbp)
movzbl -0x19(%rbp), %eax
cmpl $0x2, %eax
jne 0x4209
movl $0x7, %edi
callq 0x12f0
movq %rax, -0x100(%rbp)
jmp 0x4214
xorl %eax, %eax
movq %rax, -0x100(%rbp)
jmp 0x4214
movq -0xf0(%rbp), %rax
movq -0x100(%rbp), %rcx
orq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x88(%rbp), %rdx
callq 0x38f0
jmp 0x4242
movq $0x0, -0x78(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x108(%rbp)
movl $0xc, %edi
callq 0x12b0
movq %rax, %rcx
movq -0x108(%rbp), %rax
andq %rcx, %rax
orq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x30(%rbp)
jle 0x42af
movq -0x18(%rbp), %rax
shrq $0xc, %rax
movq %rax, -0x110(%rbp)
imulq $0x9, -0x30(%rbp), %rax
movl %eax, %edi
callq 0x12b0
movq %rax, %rcx
movq -0x110(%rbp), %rax
andq %rcx, %rax
shlq $0xc, %rax
orq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x118(%rbp)
movl $0x2c, %edi
callq 0x12b0
movq %rax, -0x120(%rbp)
imulq $0x9, -0x30(%rbp), %rax
movl %eax, %edi
callq 0x12b0
movq -0x120(%rbp), %rcx
movq %rax, %rdx
movq -0x118(%rbp), %rax
xorq %rdx, %rcx
andq %rcx, %rax
shlq $0xc, %rax
orq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nop