// File: /workspace/github_c_makefile_star2_O1/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
pushq %rbx
pushq %rax
movq %rdi, %rbx
testl %esi, %esi
je 0x4c86
leaq 0x40(%rbx), %rsi
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rbx)
movups %xmm0, 0x50(%rbx)
movl $0x20000, 0x40(%rbx)    # imm = 0x20000
movl (%rbx), %edi
callq 0x2260
testl %eax, %eax
js 0x4dc2
movl 0x48(%rbx), %esi
movl (%rbx), %edi
movl 0x40(%rbx), %edx
callq 0x2160
movl (%rbx), %edi
movl %edi, %edx
orl $0x10000, %edx          # imm = 0x10000
movl $0x108, %esi            # imm = 0x108
callq 0x2150
movl (%rbx), %edi
movl 0x48(%rbx), %edx
movl $0x138, %esi            # imm = 0x138
callq 0x2150
movl (%rbx), %edi
movl 0x40(%rbx), %edx
movl $0x13c, %esi            # imm = 0x13C
callq 0x2150
movl (%rbx), %edi
movl $0x140, %esi            # imm = 0x140
xorl %edx, %edx
callq 0x2150
movl (%rbx), %edi
movl $0x1, %ebp
movl $0x70, %esi
movl $0x1, %edx
callq 0x2150
movl (%rbx), %edi
movl $0x110, %esi            # imm = 0x110
xorl %edx, %edx
callq 0x2150
movl (%rbx), %edi
movl $0x104, %esi            # imm = 0x104
xorl %edx, %edx
callq 0x2150
movl (%rbx), %edi
movl $0x100, %esi            # imm = 0x100
movl $0x4000, %edx           # imm = 0x4000
callq 0x2150
movl (%rbx), %edi
movl $0x38, %esi
movl $0x1, %edx
callq 0x2150
movl (%rbx), %edi
movl $0x70, %esi
callq 0x2200
movl (%rbx), %edi
movl $0x1388, %esi           # imm = 0x1388
callq 0x2340
cmpl $-0x1, %eax
je 0x4da5
movl (%rbx), %edi
movl $0x110, %esi            # imm = 0x110
callq 0x2200
testl %eax, %eax
jne 0x4d7f
movl (%rbx), %edi
movl $0x114, %esi            # imm = 0x114
callq 0x2200
leaq 0x280c3(%rip), %rdi     # 0x2ce2a
movl %eax, %esi
xorl %eax, %eax
callq 0x2050
movl $0xa, %edi
callq 0x2220
movl $0xffffffdb, %ebp       # imm = 0xFFFFFFDB
movl (%rbx), %edi
movl $0x110, %esi            # imm = 0x110
callq 0x2200
movl (%rbx), %edi
movl $0x138, %esi            # imm = 0x138
callq 0x2200
movl (%rbx), %edi
movl $0x13c, %esi            # imm = 0x13C
callq 0x2200
jmp 0x4ddd
leaq 0x2829b(%rip), %rdi     # 0x2d047
callq 0x2280
movl $0xa, %edi
callq 0x2220
movl $0xffffffdc, %ebp       # imm = 0xFFFFFFDC
jmp 0x4ddd
leaq 0x282a4(%rip), %rdi     # 0x2d06d
callq 0x2280
movl $0xa, %edi
callq 0x2220
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %rbp
retq