// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/converters/fsio/dos11.c
// Function: dos11Newfs
static int dos11Newfs(
  struct mountedFS *mount,
  size_t size
)
{
  struct DOS11data *data = &mount->dos11data;
  int i;

#define MFD2_BLOCK      2
#define MAP_BLOCK       3

  memset(data, 0, sizeof(*data));

  data->blocks = size / BLOCKSIZE_RK11;
  data->bitmaps = 5;
  data->bmblk[0] = MAP_BLOCK;
  data->bmblk[1] = MAP_BLOCK + 1;
  data->bmblk[2] = MAP_BLOCK + 2;
  data->bmblk[3] = MAP_BLOCK + 3;
  data->bmblk[4] = MAP_BLOCK + 4;
  data->bmindex = 0177777;

  /*
   * Build and write MFD Block #1:
   *
   *   - second MFD block is at block 2
   *   - 5 bitmaps starting at block (filesys->blocks - 5).
   */
  memset(data->buf, 0, mount->blocksz);
  data->buf[MFD1_MFD2BLOCK] = htole16(MFD2_BLOCK);
  data->buf[MFD1_INTERLEAVE] = htole16(1);
  data->buf[MFD1_BMSTART] = htole16(data->bmblk[0]);
  data->buf[MFD1_BMSTART + 1] = htole16(data->bmblk[1]);
  data->buf[MFD1_BMSTART + 2] = htole16(data->bmblk[2]);
  data->buf[MFD1_BMSTART + 3] = htole16(data->bmblk[3]);
  data->buf[MFD1_BMSTART + 4] = htole16(data->bmblk[4]);
  data->buf[MFD1_BMSTART + 5] = htole16(0);

  if (dos11WriteBlock(mount, MFD1_BLOCK, NULL) == 0)
    return 0;

  /*
   * Build and write MFD Block #2:
   *
   *   - no UFDs present
   */
  memset(data->buf, 0, mount->blocksz);

  if (dos11WriteBlock(mount, MFD2_BLOCK, NULL) == 0)
    return 0;

  /*
   * Build and write 5 bitmap blocks.
   */
  memset(data->buf, 0, mount->blocksz);

  for (i = 1; i < 6; i++) {
    data->buf[MAP_LINK] =
      i == 5 ? 0 : htole16(data->bmblk[i]);
    data->buf[MAP_MAP] = htole16(i);
    data->buf[MAP_WORDS] = htole16(MAP_LEN);
    data->buf[MAP_FIRST] = htole16(MAP_BLOCK);

    if (dos11WriteBlock(mount, data->bmblk[i - 1], NULL) == 0)
      return 0;
  }

  /*
   * Reserve used blocks in the bitmap(s).
   */
  if (bitmapSetBit(mount, BOOT_BLOCK) == 0)
    return 0;
  if (bitmapSetBit(mount, MFD1_BLOCK) == 0)
    return 0;
  if (bitmapSetBit(mount, MFD2_BLOCK) == 0)
    return 0;

  for (i = 1; i < 6; i++)
    if (bitmapSetBit(mount, MAP_BLOCK + i - 1) == 0)
      return 0;

  /*
   * Reserve all blocks past the end of the disk.
   */
  for (i = 4800; i < (5 * MAP_BLOCKS); i++)
    if (bitmapSetBit(mount, i) == 0)
    return 0;

  return bitmapFlush(mount);
}

// Assembly:
pushq %r14
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
addq $0x58, %rdi
movl $0x900, %edx            # imm = 0x900
xorl %esi, %esi
callq 0x3170
shrq $0x8, %r14
movl %r14d, 0x48(%rbx)
movabsq $0x5000400030005, %rax  # imm = 0x5000400030005
movq %rax, 0x4c(%rbx)
movl $0x70006, 0x54(%rbx)    # imm = 0x70006
movw $0xffff, 0x14e(%rbx)    # imm = 0xFFFF
leaq 0x554(%rbx), %r14
movq 0x28(%rbx), %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x3170
movaps 0x92cb(%rip), %xmm0     # 0x11870
movups %xmm0, 0x554(%rbx)
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x7700
testl %eax, %eax
je 0x86e2
movq 0x28(%rbx), %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x3170
movq %rbx, %rdi
movl $0x2, %esi
xorl %edx, %edx
callq 0x7700
testl %eax, %eax
je 0x86e2
movq 0x28(%rbx), %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x3170
movl $0x28, %r14d
cmpq $0x2c, %r14
jne 0x8606
xorl %eax, %eax
jmp 0x860b
movzwl (%rbx,%r14,2), %eax
movw %ax, 0x554(%rbx)
leal -0x27(%r14), %eax
movw %ax, 0x556(%rbx)
movl $0x3003c, 0x558(%rbx)   # imm = 0x3003C
movzwl -0x2(%rbx,%r14,2), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x7700
testl %eax, %eax
je 0x86e2
incq %r14
cmpq $0x2d, %r14
jne 0x85fc
movq %rbx, %rdi
xorl %esi, %esi
callq 0x9552
testl %eax, %eax
je 0x86e2
leaq 0xfe5f(%rip), %r14      # 0x184c0
movzwl (%r14), %eax
orw %ax, 0x15c(%rbx)
movb $0x1, 0x152(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x9552
testl %eax, %eax
je 0x86e2
movzwl 0x2(%r14), %eax
orw %ax, 0x15c(%rbx)
movb $0x1, 0x152(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x9552
testl %eax, %eax
je 0x86e2
movzwl 0x4(%r14), %eax
orw %ax, 0x15c(%rbx)
movb $0x1, 0x152(%rbx)
movl $0xfffffffb, %r14d      # imm = 0xFFFFFFFB
leal 0x8(%r14), %eax
movzwl %ax, %esi
movq %rbx, %rdi
callq 0x9620
testl %eax, %eax
je 0x86e2
incl %r14d
jne 0x86bb
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x7bbf
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
retq