// File: /workspace/github_c_makefile_star2_O2/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 %r14
pushq %rbx
movq %rdi, %rbx
testl %esi, %esi
je 0x4a68
leaq 0x40(%rbx), %rsi
xorps %xmm0, %xmm0
movups %xmm0, 0x50(%rbx)
movups %xmm0, 0x44(%rbx)
movl $0x20000, 0x40(%rbx)    # imm = 0x20000
movl (%rbx), %edi
callq 0x2270
testl %eax, %eax
js 0x4b91
movl 0x48(%rbx), %esi
movl (%rbx), %edi
movl 0x40(%rbx), %edx
callq 0x2170
movl (%rbx), %edi
movl %edi, %edx
orl $0x10000, %edx          # imm = 0x10000
movl $0x108, %esi            # imm = 0x108
callq 0x2160
movl (%rbx), %edi
movl 0x48(%rbx), %edx
movl $0x138, %esi            # imm = 0x138
callq 0x2160
movl (%rbx), %edi
movl 0x40(%rbx), %edx
movl $0x13c, %esi            # imm = 0x13C
callq 0x2160
movl (%rbx), %edi
movl $0x140, %esi            # imm = 0x140
xorl %edx, %edx
callq 0x2160
movl (%rbx), %edi
pushq $0x70
popq %r14
pushq $0x1
popq %rbp
movl %r14d, %esi
movl %ebp, %edx
callq 0x2160
movl (%rbx), %edi
movl $0x110, %esi            # imm = 0x110
xorl %edx, %edx
callq 0x2160
movl (%rbx), %edi
movl $0x104, %esi            # imm = 0x104
xorl %edx, %edx
callq 0x2160
movl (%rbx), %edi
movl $0x100, %esi            # imm = 0x100
movl $0x4000, %edx           # imm = 0x4000
callq 0x2160
movl (%rbx), %edi
pushq $0x38
popq %rsi
movl %ebp, %edx
callq 0x2160
movl (%rbx), %edi
movl %r14d, %esi
callq 0x2210
movl (%rbx), %edi
movl $0x1388, %esi           # imm = 0x1388
callq 0x2360
cmpl $-0x1, %eax
je 0x4b79
movl (%rbx), %edi
movl $0x110, %esi            # imm = 0x110
callq 0x2210
testl %eax, %eax
jne 0x4b53
movl (%rbx), %edi
movl $0x114, %esi            # imm = 0x114
callq 0x2210
leaq 0x27303(%rip), %rdi     # 0x2be42
movl %eax, %esi
xorl %eax, %eax
callq 0x2050
pushq $0xa
popq %rdi
callq 0x2230
pushq $-0x25
popq %rbp
movl (%rbx), %edi
movl $0x110, %esi            # imm = 0x110
callq 0x2210
movl (%rbx), %edi
movl $0x138, %esi            # imm = 0x138
callq 0x2210
movl (%rbx), %edi
movl $0x13c, %esi            # imm = 0x13C
callq 0x2210
jmp 0x4ba8
leaq 0x274df(%rip), %rdi     # 0x2c05f
callq 0x2290
pushq $0xa
popq %rdi
callq 0x2230
pushq $-0x24
jmp 0x4ba7
leaq 0x274ed(%rip), %rdi     # 0x2c085
callq 0x2290
pushq $0xa
popq %rdi
callq 0x2230
pushq $-0x1
popq %rbp
movl %ebp, %eax
popq %rbx
popq %r14
popq %rbp
retq