// File: /workspace/github_c_makefile_star2_O2/simh[P]simtools/converters/fsio/os8.c
// Function: rx02ReadBlock
static int rx02ReadBlock(
  struct mountedFS *mount,
  uint8_t unit,
  unsigned int block,
  void *buf
)
{
  unsigned int base = (block / RX02REPEAT) * RX02REPEAT;
  unsigned int offset = block % RX02REPEAT;
  uint16_t *buffer = buf;
  int i, j, k, status;

#ifdef DEBUG
  if ((mount->flags & FS_DEBUG) != 0)
    fprintf(DEBUGout, "  >>%s%o: rx02ReadBlock, base %u, offset %u\n",
            mount->name, unit, base, offset);
#endif

  /*
   * Convert to sector number
   */
  base *= OS8_BLOCKSIZE / OS8_RX02SS_W;

  /*
   * Read 2 sectors into the buffer
   */
  for (i = 0; i < 2; i++) {
    unsigned int sector = base + rx02interleave[(offset * 2) + i];
    unsigned char temp[OS8_RX02SS];

#ifdef DEBUG
    if ((mount->flags & FS_DEBUG) != 0)
      fprintf(DEBUGout, "  >> %s%o: (os8) Reading %u, track %u sector %u\n",
              mount->name, unit, sector,
              base + ((rx02interleave[(offset * 2) + i] + 1) / OS8_RX02SS),
              (rx02interleave[(offset * 2) + i] + 1) % OS8_RX02SS);
#endif

    if ((status = FSioReadSector(mount, sector, OS8_RX02SS, temp)) == 0) {
      ERROR("I/O error on \"%s%o:\"\n", mount->name, unit);
      return 0;
    }

    /*
     * Unpack 128 words from the temporary buffer
     */
    for (j = 0, k = 0; j < OS8_RX02SS_W; j += 2, k += 3) {
      buffer[j] = ((temp[k] << 4) | ((temp[k + 1] >> 4) & 0xF)) & 0xFFF;
      buffer[j + 1] = (((temp[k + 1] << 8) & 0xF00) | temp[k + 2]) & 0xFFF;
    }
    buffer += OS8_RX02SS_W;
  }
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rcx, %r14
movl %edx, %r15d
movq %rdi, %r12
pushq $0xd
popq %rcx
movl %edx, %eax
xorl %edx, %edx
divl %ecx
movl %edx, %r13d
subl %edx, %r15d
testb $0x2, 0x30(%rdi)
jne 0xc2b3
movzbl %sil, %eax
movl %eax, 0x14(%rsp)
jmp 0xc2de
leaq 0xbd0e(%rip), %rax      # 0x17fc8
movq (%rax), %rdi
leaq 0x8(%r12), %rdx
movzbl %sil, %ecx
leaq 0x5759(%rip), %rsi      # 0x11a26
movl %ecx, 0x14(%rsp)
movl %r15d, %r8d
movl %r13d, %r9d
xorl %eax, %eax
callq 0x31e0
addl %r15d, %r15d
addl %r13d, %r13d
leaq 0x8(%r12), %rax
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
cmpq $0x2, %rbx
je 0xc3b3
movq %rbx, %rax
orq %r13, %rax
leaq 0x54e9(%rip), %rcx      # 0x117f0
movzwl (%rcx,%rax,2), %eax
leal (%r15,%rax), %ebp
testb $0x2, 0x30(%r12)
je 0xc34d
leaq 0xbcaa(%rip), %rcx      # 0x17fc8
movq (%rcx), %rdi
incl %eax
movl %eax, %r9d
shrl $0x8, %r9d
addl %r15d, %r9d
movzbl %al, %eax
movl %eax, (%rsp)
leaq 0x565c(%rip), %rsi      # 0x11996
movq 0x18(%rsp), %rdx
movl 0x14(%rsp), %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x31e0
movq %r12, %rdi
movl %ebp, %esi
movl $0x100, %edx            # imm = 0x100
leaq 0x20(%rsp), %rcx
callq 0x4cb1
testl %eax, %eax
je 0xc3ca
leaq 0x22(%rsp), %rax
xorl %ecx, %ecx
cmpq $0x7f, %rcx
ja 0xc3a4
movzbl -0x2(%rax), %edx
shll $0x4, %edx
movzbl -0x1(%rax), %esi
movl %esi, %edi
shrl $0x4, %edi
orl %edx, %edi
movw %di, (%r14,%rcx,2)
movzbl (%rax), %edx
andl $0xf, %esi
shll $0x8, %esi
orl %edx, %esi
movw %si, 0x2(%r14,%rcx,2)
addq $0x2, %rcx
addq $0x3, %rax
jmp 0xc36c
addq $0x100, %r14            # imm = 0x100
incq %rbx
jmp 0xc2f0
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xac27(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
leaq 0x4bfb(%rip), %rsi      # 0x10fd6
xorl %ebx, %ebx
movq 0x18(%rsp), %rdx
movl 0x14(%rsp), %ecx
xorl %eax, %eax
callq 0x31e0
leaq 0xbbd4(%rip), %rax      # 0x17fc8
movq (%rax), %rdi
testq %rdi, %rdi
je 0xc3b6
movq 0xabbd(%rip), %rax      # 0x16fc0
cmpq (%rax), %rdi
je 0xc3b6
leaq 0x4bc7(%rip), %rsi      # 0x10fd6
movq 0x18(%rsp), %rdx
movl 0x14(%rsp), %ecx
xorl %eax, %eax
callq 0x31e0
jmp 0xc3b6