// File: /workspace/github_c_makefile_star2_O0/ataradov[P]edbg/target_mchp_cm23.c
// Function: target_fuse_write
static void target_fuse_write(int section, uint8_t *data)
{
  uint32_t addr = 0;

  if (0 == section)
  {
    uint32_t crc = crc32(&data[8], target_device.crc_offset-8);

    data[target_device.crc_offset + 0] = crc;
    data[target_device.crc_offset + 1] = crc >> 8;
    data[target_device.crc_offset + 2] = crc >> 16;
    data[target_device.crc_offset + 3] = crc >> 24;

    addr = USER_ROW_ADDR;
  }
  else if (1 == section)
  {
    uint32_t crc = crc32(data, 8);

    data[8]  = crc;
    data[9]  = crc >> 8;
    data[10] = crc >> 16;
    data[11] = crc >> 24;

    sha256(data, FLASH_ROW_SIZE-32, &data[FLASH_ROW_SIZE-32]);

    addr = BOCOR_ROW_ADDR;
  }
  else if (2 == section)
  {
    addr = USER_ROW_ADDR;
  }
  else if (3 == section)
  {
    addr = BOCOR_ROW_ADDR;
  }
  else
    error_exit("internal: incorrect section index in target_fuse_write()");

  bootrom_park();

  dap_write_byte(NVMCTRL_CTRLC, 0);
  dap_write_word(NVMCTRL_ADDR, addr);
  dap_write_half(NVMCTRL_CTRLA, NVMCTRL_CMD_ER);
  while (0 == (dap_read_byte(NVMCTRL_STATUS) & NVMCTRL_STATUS_READY));

  dap_write_block(addr, data, FLASH_ROW_SIZE);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
xorl %eax, %eax
cmpl -0x4(%rbp), %eax
jne 0xe11f
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
movl 0x742f9(%rip), %esi     # 0x82398
subl $0x8, %esi
callq 0xa250
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movl 0x742df(%rip), %ecx     # 0x82398
addl $0x0, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x18(%rbp), %eax
shrl $0x8, %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movl 0x742c4(%rip), %ecx     # 0x82398
addl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x18(%rbp), %eax
shrl $0x10, %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movl 0x742a9(%rip), %ecx     # 0x82398
addl $0x2, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x18(%rbp), %eax
shrl $0x18, %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movl 0x7428e(%rip), %ecx     # 0x82398
addl $0x3, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl $0x804000, -0x14(%rbp)  # imm = 0x804000
jmp 0xe1cf
movl $0x1, %eax
cmpl -0x4(%rbp), %eax
jne 0xe195
movq -0x10(%rbp), %rdi
movl $0x8, %esi
callq 0xa250
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x8(%rax)
movl -0x1c(%rbp), %eax
shrl $0x8, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x9(%rax)
movl -0x1c(%rbp), %eax
shrl $0x10, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0xa(%rax)
movl -0x1c(%rbp), %eax
shrl $0x18, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0xb(%rax)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rdx
addq $0xe0, %rdx
movl $0xe0, %esi
callq 0x9c20
movl $0x80c000, -0x14(%rbp)  # imm = 0x80C000
jmp 0xe1cd
movl $0x2, %eax
cmpl -0x4(%rbp), %eax
jne 0xe1a8
movl $0x804000, -0x14(%rbp)  # imm = 0x804000
jmp 0xe1cb
movl $0x3, %eax
cmpl -0x4(%rbp), %eax
jne 0xe1bb
movl $0x80c000, -0x14(%rbp)  # imm = 0x80C000
jmp 0xe1c9
leaq 0x86da(%rip), %rdi      # 0x1689c
movb $0x0, %al
callq 0x8970
jmp 0xe1cb
jmp 0xe1cd
jmp 0xe1cf
callq 0xe3e0
movl 0x74206(%rip), %edi     # 0x823e0
xorl %esi, %esi
callq 0x7770
movl 0x74201(%rip), %edi     # 0x823e8
movl -0x14(%rbp), %esi
callq 0x77d0
movl 0x741e3(%rip), %edi     # 0x823d8
movl $0xa502, %esi           # imm = 0xA502
callq 0x77a0
movl 0x741df(%rip), %edi     # 0x823e4
callq 0x76e0
movzbl %al, %ecx
andl $0x4, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0xe218
jmp 0xe1ff
movl -0x14(%rbp), %edi
movq -0x10(%rbp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x79c0
addq $0x20, %rsp
popq %rbp
retq
nop