// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rdi
movl $0x910, %edx            # imm = 0x910
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 0x8ff2(%rip), %xmm0     # 0x11870
movups %xmm0, 0x554(%rbx)
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x7983
testl %eax, %eax
je 0x89bb
movq 0x28(%rbx), %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x3170
movq %rbx, %rdi
movl $0x2, %esi
xorl %edx, %edx
callq 0x7983
testl %eax, %eax
je 0x89bb
movq 0x28(%rbx), %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x3170
movl $0x28, %r14d
cmpq $0x2c, %r14
jne 0x88df
xorl %eax, %eax
jmp 0x88e4
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 0x7983
testl %eax, %eax
je 0x89bb
incq %r14
cmpq $0x2d, %r14
jne 0x88d5
movq %rbx, %rdi
xorl %esi, %esi
callq 0x986f
testl %eax, %eax
je 0x89bb
leaq 0xfb76(%rip), %r14      # 0x184b0
movzwl (%r14), %eax
orw %ax, 0x15c(%rbx)
movb $0x1, 0x152(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x986f
testl %eax, %eax
je 0x89bb
movzwl 0x2(%r14), %eax
orw %ax, 0x15c(%rbx)
movb $0x1, 0x152(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x986f
testl %eax, %eax
je 0x89bb
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 0x993d
testl %eax, %eax
je 0x89bb
incl %r14d
jne 0x8994
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x7e7b
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
retq