// File: /workspace/github_c_makefile_star2_O0/ataradov[P]edbg/target_lattice_lcmxo2.c
// Function: parse_jed_file
static void parse_jed_file(jed_file_t *file, uint8_t *data, int size)
{
  static const char *start_text = "L000000";
  static const char *fr_text = "NOTE FEATURE_ROW*";
  uint8_t  *ptr;
  int bit_count = 0;
  int fr_bit_count = 0;
  int offset;

  // This is a very primitive parser. It expects a fixed format and will fail
  // if it finds something unexpected. It will also ignore EBR initialization data.

  if (!bitstream_valid(data, size))
    error_exit("malformed JED file: device signature not found");

  ptr = mem_find(data, size, (uint8_t *)start_text, strlen(start_text));

  if (NULL == ptr)
    error_exit("malformed JED file: no 'L000000' found");

  offset = ptr - data + strlen(start_text);

  memset(file->config, 0, file->config_size);

  for (; offset < size; offset++)
  {
    if (data[offset] == '*')
      break;

    if (data[offset] == '0' || data[offset] == '1')
    {
      file->config[bit_count / 8] |= ((data[offset] - '0') << (bit_count % 8));
      bit_count++;
      check(bit_count < (file->config_size*8), "malformed JED file: configuration data is too big");
    }
  }

  if (offset == size)
    error_exit("malformed JED file: no field terminator found");

  if (bit_count % FLASH_ROW_SIZE)
    error_exit("malformed JED file: size of the configuration data must be a multiple of 128");

  ptr = mem_find(data, size, (uint8_t *)fr_text, strlen(fr_text));

  if (NULL == ptr)
    error_exit("malformed JED file: no feature row found");

  offset = ptr - data + strlen(fr_text);

  file->feature = 0;
  file->feabits = 0;

  for (; offset < size; offset++)
  {
    if (data[offset] == '*')
      break;

    if (data[offset] == '0' || data[offset] == '1')
    {
      int bit = data[offset] - '0';

      check(fr_bit_count < (64 + 16), "malformed JED file: feature row data is too big");

      if (fr_bit_count < 64)
        file->feature |= ((uint64_t)bit << fr_bit_count);
      else
        file->feabits |= (bit << (fr_bit_count-64));

      fr_bit_count++;
    }
  }

  if (offset == size)
    error_exit("malformed JED file: no field terminator found");

  if (fr_bit_count != (64 + 16))
    error_exit("malformed JED file: invalid feature row size");

  file->config_size = bit_count;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x111a0
testb $0x1, %al
jne 0x10e82
leaq 0x6a7a(%rip), %rdi      # 0x178f5
movb $0x0, %al
callq 0x8970
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x3c(%rbp)
movq 0xe7c1(%rip), %rax      # 0x1f658
movq %rax, -0x38(%rbp)
movq 0xe7b6(%rip), %rdi      # 0x1f658
callq 0x60a0
movq -0x48(%rbp), %rdi
movl -0x3c(%rbp), %esi
movq -0x38(%rbp), %rdx
movl %eax, %ecx
callq 0x8cf0
movq %rax, -0x20(%rbp)
xorl %eax, %eax
cmpq -0x20(%rbp), %rax
jne 0x10ed3
leaq 0x6a58(%rip), %rdi      # 0x17924
movb $0x0, %al
callq 0x8970
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x50(%rbp)
movq 0xe76f(%rip), %rdi      # 0x1f658
callq 0x60a0
movq %rax, %rcx
movq -0x50(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movslq 0x8(%rax), %rdx
xorl %esi, %esi
callq 0x60f0
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x10fda
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x2a, %eax
jne 0x10f33
jmp 0x10fda
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x30, %eax
je 0x10f55
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x31, %eax
jne 0x10fca
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movzbl (%rax,%rcx), %esi
subl $0x30, %esi
movl -0x24(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %edx, %ecx
shll %cl, %esi
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movl -0x24(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x58(%rbp), %rax
movslq %ecx, %rcx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x8(%rbp), %rcx
movl 0x8(%rcx), %ecx
shll $0x3, %ecx
cmpl %ecx, %eax
setl %al
movzbl %al, %edi
andl $0x1, %edi
leaq 0x6988(%rip), %rsi      # 0x1794b
xorl %eax, %eax
callq 0x8880
jmp 0x10fcc
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x10f11
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0x10ff0
leaq 0x6994(%rip), %rdi      # 0x1797d
movb $0x0, %al
callq 0x8970
movl -0x24(%rbp), %eax
movl $0x80, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x1100e
leaq 0x69a4(%rip), %rdi      # 0x179ab
movb $0x0, %al
callq 0x8970
movq -0x10(%rbp), %rax
movq %rax, -0x70(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x64(%rbp)
movq 0xe63d(%rip), %rax      # 0x1f660
movq %rax, -0x60(%rbp)
movq 0xe632(%rip), %rdi      # 0x1f660
callq 0x60a0
movq -0x70(%rbp), %rdi
movl -0x64(%rbp), %esi
movq -0x60(%rbp), %rdx
movl %eax, %ecx
callq 0x8cf0
movq %rax, -0x20(%rbp)
xorl %eax, %eax
cmpq -0x20(%rbp), %rax
jne 0x1105f
leaq 0x69a0(%rip), %rdi      # 0x179f8
movb $0x0, %al
callq 0x8970
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x78(%rbp)
movq 0xe5eb(%rip), %rdi      # 0x1f660
callq 0x60a0
movq %rax, %rcx
movq -0x78(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x8(%rbp), %rax
movw $0x0, 0xc(%rax)
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x11161
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x2a, %eax
jne 0x110bf
jmp 0x11161
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x30, %eax
je 0x110e1
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x31, %eax
jne 0x11151
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
subl $0x30, %eax
movl %eax, -0x30(%rbp)
cmpl $0x50, -0x28(%rbp)
setl %al
movzbl %al, %edi
andl $0x1, %edi
leaq 0x691a(%rip), %rsi      # 0x17a21
xorl %eax, %eax
callq 0x8880
cmpl $0x40, -0x28(%rbp)
jge 0x1112f
movslq -0x30(%rbp), %rax
movl -0x28(%rbp), %ecx
shlq %cl, %rax
movq %rax, %rcx
movq -0x8(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
jmp 0x11148
movl -0x30(%rbp), %edx
movl -0x28(%rbp), %ecx
subl $0x40, %ecx
shll %cl, %edx
movq -0x8(%rbp), %rax
movzwl 0xc(%rax), %ecx
orl %edx, %ecx
movw %cx, 0xc(%rax)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x11153
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1109d
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0x11177
leaq 0x680d(%rip), %rdi      # 0x1797d
movb $0x0, %al
callq 0x8970
cmpl $0x50, -0x28(%rbp)
je 0x1118b
leaq 0x68cd(%rip), %rdi      # 0x17a51
movb $0x0, %al
callq 0x8970
movl -0x24(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x8(%rax)
addq $0x80, %rsp
popq %rbp
retq
nop