// File: /workspace/github_c_makefile_star2_O0/xiane[P]libvphevcodec/AML_HEVCEncoder.cpp
// Function: Wave4VpuCreateInstance(AMVHEVCEncHandle_s*, int)
AMVEnc_Status Wave4VpuCreateInstance(AMVHEVCEncHandle *Handle, int alloc) {
    AMVEnc_Status ret = AMVENC_SUCCESS;
    Uint32 temp;
    if (alloc) {
        memset((void*) &Handle->work_vb, 0, sizeof(vpu_buffer_t));
        Handle->work_vb.size = WAVE4ENC_WORKBUF_SIZE;
        if (vdi_allocate_dma_memory(Handle->instance_id, &Handle->work_vb) < 0) {
            VLOG(ERR, "Wave4VpuCreateInstance error alloc\n");
            return AMVENC_MEMORY_FAIL;
        }
    }

    vdi_clear_memory(Handle->instance_id, Handle->work_vb.phys_addr, Handle->work_vb.size);

    // instance information
    temp = 0;
    temp |= (0 << 24); // [27:24] codec_std_aux
    temp |= (1 << 16); // [23:16] codec_std
    temp |= (Handle->instance_id << 0);  // [15: 0] Handle->instance_id
    VpuWriteReg(Handle->instance_id, W4_INST_INDEX, temp);

    VpuWriteReg(Handle->instance_id, W4_ADDR_WORK_BASE, Handle->work_vb.phys_addr);
    VpuWriteReg(Handle->instance_id, W4_WORK_SIZE, Handle->work_vb.size);
    VpuWriteReg(Handle->instance_id, W4_WORK_PARAM, 0);

    VpuWriteReg(Handle->instance_id, W4_VPU_BUSY_STATUS, 1);
    VpuWriteReg(Handle->instance_id, W4_RET_SUCCESS, 0);  //for debug
    VpuWriteReg(Handle->instance_id, W4_CORE_INDEX, 0);
    //VpuWriteReg(Handle->instance_id, W4_INST_INDEX,  (Handle->instance_id&0xffff)|(1<<16));

    VpuWriteReg(Handle->instance_id, W4_COMMAND, CREATE_INSTANCE);
    VpuWriteReg(Handle->instance_id, W4_VPU_HOST_INT_REQ, 1);
    //Wave4BitIssueCommand(0, 4, CREATE_INSTANCE);
    temp = VpuReadReg(Handle->instance_id, W4_VPU_BUSY_STATUS);
    VLOG(DEBUG, "W4_VPU_BUSY_STATUS :0x%x\n", temp);

    if (vdi_wait_interrupt(Handle->instance_id, VPU_BUSY_CHECK_TIMEOUT) == -1) {
        //vdi_free_dma_memory(0, &Handle->work_vb);
        //     memset((void*)&Handle->work_vb, 0, sizeof(vpu_buffer_t));
        VLOG(ERR, "Wave4VpuCreateInstance error time out\n");
        return AMVENC_TIMEOUT;
    }
    //if (vdi_wait_vpu_busy(0, VPU_BUSY_CHECK_TIMEOUT, W4_VPU_BUSY_STATUS) == -1) {
    //    vdi_free_dma_memory(0, &vb);
    //    VLOG(ERR, "Wave4VpuCreateInstance error time out\n");
    //    return AMVENC_TIMEOUT;
    //}

    if (VpuReadReg(Handle->instance_id, W4_RET_SUCCESS) == 0) {
        uint32_t reasonCode = VpuReadReg(Handle->instance_id, W4_RET_FAIL_REASON);
        VLOG(ERR, "Wave4VpuCreateInstance failedREASON CODE(%08x)\n", reasonCode);
        //vdi_free_dma_memory(0, &Handle->work_vb);
        //    memset((void*)&Handle->work_vb, 0, sizeof(vpu_buffer_t));
        ret = AMVENC_HARDWARE;
    }
    temp = VpuReadReg(Handle->instance_id, W4_RET_SUCCESS);
    VLOG(DEBUG, "W4_RET_SUCCESS %x\n", temp);
    temp = VpuReadReg(Handle->instance_id, W4_ADDR_WORK_BASE);
    VLOG(DEBUG, "W4_ADDR_WORK_BASE %x\n", temp);
    temp = VpuReadReg(Handle->instance_id, W4_WORK_SIZE);
    VLOG(DEBUG, "W4_WORK_SIZE %x\n", temp);
    return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl $0x1, -0x18(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x597f
movq -0x10(%rbp), %rdi
addq $0x40, %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x20f0
movq -0x10(%rbp), %rax
movl $0x20000, 0x40(%rax)    # imm = 0x20000
movq -0x10(%rbp), %rax
movl (%rax), %edi
movq -0x10(%rbp), %rsi
addq $0x40, %rsi
callq 0x2260
cmpl $0x0, %eax
jge 0x597d
jmp 0x5955
leaq 0x2a58a(%rip), %rdi     # 0x2fee6
movb $0x0, %al
callq 0x2050
leaq 0x2a8fc(%rip), %rdi     # 0x30266
movb $0x0, %al
callq 0x2050
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5b65
jmp 0x597f
movq -0x10(%rbp), %rax
movl (%rax), %edi
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movl %eax, %esi
movq -0x10(%rbp), %rax
movl 0x40(%rax), %edx
callq 0x2170
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
orl $0x0, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
orl $0x10000, %eax          # imm = 0x10000
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
shll $0x0, %eax
orl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %edi
movl -0x1c(%rbp), %edx
movl $0x108, %esi            # imm = 0x108
callq 0x2160
movq -0x10(%rbp), %rax
movl (%rax), %edi
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movl %eax, %edx
movl $0x138, %esi            # imm = 0x138
callq 0x2160
movq -0x10(%rbp), %rax
movl (%rax), %edi
movq -0x10(%rbp), %rax
movl 0x40(%rax), %edx
movl $0x13c, %esi            # imm = 0x13C
callq 0x2160
movq -0x10(%rbp), %rax
movl (%rax), %edi
movl $0x140, %esi            # imm = 0x140
xorl %edx, %edx
callq 0x2160
movq -0x10(%rbp), %rax
movl (%rax), %edi
movl $0x70, %esi
movl $0x1, %edx
callq 0x2160
movq -0x10(%rbp), %rax
movl (%rax), %edi
movl $0x110, %esi            # imm = 0x110
xorl %edx, %edx
callq 0x2160
movq -0x10(%rbp), %rax
movl (%rax), %edi
movl $0x104, %esi            # imm = 0x104
xorl %edx, %edx
callq 0x2160
movq -0x10(%rbp), %rax
movl (%rax), %edi
movl $0x100, %esi            # imm = 0x100
movl $0x4000, %edx           # imm = 0x4000
callq 0x2160
movq -0x10(%rbp), %rax
movl (%rax), %edi
movl $0x38, %esi
movl $0x1, %edx
callq 0x2160
movq -0x10(%rbp), %rax
movl (%rax), %edi
movl $0x70, %esi
callq 0x2210
movl %eax, -0x1c(%rbp)
jmp 0x5a93
movq -0x10(%rbp), %rax
movl (%rax), %edi
movl $0x1388, %esi           # imm = 0x1388
callq 0x2340
cmpl $-0x1, %eax
jne 0x5ad2
jmp 0x5aaa
leaq 0x2a459(%rip), %rdi     # 0x2ff0a
movb $0x0, %al
callq 0x2050
leaq 0x2a7a7(%rip), %rdi     # 0x30266
movb $0x0, %al
callq 0x2050
movl $0xffffffdc, -0x4(%rbp) # imm = 0xFFFFFFDC
jmp 0x5b65
movq -0x10(%rbp), %rax
movl (%rax), %edi
movl $0x110, %esi            # imm = 0x110
callq 0x2210
cmpl $0x0, %eax
jne 0x5b20
movq -0x10(%rbp), %rax
movl (%rax), %edi
movl $0x114, %esi            # imm = 0x114
callq 0x2210
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %esi
leaq 0x2a42d(%rip), %rdi     # 0x2ff31
movb $0x0, %al
callq 0x2050
leaq 0x2a754(%rip), %rdi     # 0x30266
movb $0x0, %al
callq 0x2050
movl $0xffffffdb, -0x18(%rbp) # imm = 0xFFFFFFDB
movq -0x10(%rbp), %rax
movl (%rax), %edi
movl $0x110, %esi            # imm = 0x110
callq 0x2210
movl %eax, -0x1c(%rbp)
jmp 0x5b35
movq -0x10(%rbp), %rax
movl (%rax), %edi
movl $0x138, %esi            # imm = 0x138
callq 0x2210
movl %eax, -0x1c(%rbp)
jmp 0x5b4a
movq -0x10(%rbp), %rax
movl (%rax), %edi
movl $0x13c, %esi            # imm = 0x13C
callq 0x2210
movl %eax, -0x1c(%rbp)
jmp 0x5b5f
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nop