// File: /workspace/github_c_makefile_star2_O3/Edilbert[P]BS9/bs9.c
// Function: SetPostByte
int SetPostByte(char *p, int *v)
{
   int inc,dec,reg,amo,off,ind,opl;

   ind =  0;  // indirect bit
   inc =  0;  // auto increment counter
   dec =  0;  // auto decrement counter
   reg = -1;  // index  register
   amo = -1;  // address mode bits
   off =  0;  // constant offset
   opl = strlen(p);

   // indirect

   if (df) fprintf(df,"indirect check %c %c\n",p[0],p[opl-1]);
   if (p[0] == '[' && p[opl-1] == ']')
   {
      ind = 0x10;
      p[opl-1] = 0;
      opl-=2;
      ++p;
      if (df) fprintf(df,"is indirect <%s>\n",p);
   }

   if (df && strlen(p) > 2)
      fprintf(df,"Check R,R: %c%c%c\n",toupper(p[0]),p[1],toupper(p[2]));

   // A,R

   if (toupper(p[0]) == 'A' && p[1] == ',')
   {
      reg = PostIndexReg(reg,p+2);
      ql = 0;
      return (0x80 | reg | ind | 0x06);
   }

   // B,R

   if (toupper(p[0]) == 'B' && p[1] == ',')
   {
      reg = PostIndexReg(reg,p+2);
      ql = 0;
      return (0x80 | reg | ind | 0x05);
   }

   // D,R

   if (toupper(p[0]) == 'D' && p[1] == ',')
   {
      reg = PostIndexReg(reg,p+2);
      ql = 0;
      return (0x80 | reg | ind | 0x0b);
   }

   // E,R

   if (toupper(p[0]) == 'E' && p[1] == ',')
   {
      reg = PostIndexReg(reg,p+2);
      ql = 0;
      return (0x80 | reg | ind | 0x07);
   }

   // F,R

   if (toupper(p[0]) == 'F' && p[1] == ',')
   {
      reg = PostIndexReg(reg,p+2);
      ql = 0;
      return (0x80 | reg | ind | 0x0a);
   }

   // W,R

   if (toupper(p[0]) == 'W' && p[1] == ',')
   {
      reg = PostIndexReg(reg,p+2);
      ql = 0;
      return (0x80 | reg | ind | 0x0e);
   }

   // PC relative

   if (df) fprintf(df,"check PC relative %d [%s],<%s>\n",opl,p,p+opl-3);
   if ((opl > 4 && StrNCaseCmp(p+opl-4,",PCR",4) == 0) ||
       (opl > 3 && StrNCaseCmp(p+opl-3,",PC" ,3) == 0))
   {
      if (df) fprintf(df,"check PC relative %s\n",p);
      p = EvalOperand(p,&off,0);
      off -= pc+3;
      if (ForcedMode < 0 || (off >= -128 && off < 128 && ROM[pc] != 0x8d))
      {
         ql = 1;
         *v = off;
         return (0x8c | ind);
      }
      else
      {
         ql = 2;
         *v = off-1;
         return (0x8d | ind);
      }
   }

   // offset

   if (*p != ',')
   {
      p = EvalOperand(p,&off,0);
   }

   // zero offset

   if (*p == ',' && off == 0)
   {
      while (*(++p) == '-') ++dec;
      reg = PostIndexW(reg,p);
      if (df) fprintf(df,"zero offset reg=%2.2x\n",reg);
      while (*(++p) == '+') ++inc;
      if (df) fprintf(df, "Pre - (%d) and Post + (%d)\n",dec,inc);
      if (reg <  0) OperandError(p);
           if (inc == 1 && dec == 0) amo = 0x00;
      else if (inc == 2 && dec == 0) amo = 0x01;
      else if (inc == 0 && dec == 1) amo = 0x02;
      else if (inc == 0 && dec == 2) amo = 0x03;
      else if (inc == 0 && dec == 0) amo = 0x04;
      else OperandError(p);
      ql = 0; // no address
      if (reg == 0xf) // W register
      {
              if (amo == 4) reg = 0x8f; // ,W
         else if (amo == 1) reg = 0xcf; // ,W++
         else if (amo == 3) reg = 0xef; // ,--W
         else OperandError(p);
         if (ind) reg +=1;
         if (df) fprintf(df,"W pb = %2.2x ind = %2.2x\n",reg,ind);
         return reg;
      }
      if (*p) OperandError(p);
      return (0x80 | reg | ind | amo);
   }

   // constant offset

   if (*p == ',')
   {
      if (df) fprintf(df,"constant off = %x\n",off);
      *v = off;
      reg = PostIndexW(reg,++p);

      if (reg == 0xf) // W register
      {
         ql = 2;
         if (ind) return 0xb0;
         else     return 0xaf;
      }
                                             // 5 bit offset
      if (ForcedMode <= 0 && off >= -16 && off < 16 && ind == 0)
      {
         ql = 0; // no following bytes
         return (reg | (off & 0x1f));
      }                                      // 8 bit offset

      if (ForcedMode <  0 || (off >= -128 && off < 128))
      {
         ql = 1; // one following byte
         return (0x80 | reg | ind | 0x08);
      }

      // 16 bit offset

      ql = 2; // two following bytes
      return (0x80 | reg | ind | 0x09);
   }

   OperandError(p);
   return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r15
movq %rdi, %r14
movabsq $-0x100000000, %r13     # imm = 0xFFFFFFFF00000000
movl $0x0, 0x4(%rsp)
callq 0x2110
movq %rax, %r12
movq 0x1e834(%rip), %rdi     # 0x25a10
testq %rdi, %rdi
je 0x7206
movsbl (%r14), %edx
movq %r12, %rax
shlq $0x20, %rax
addq %r13, %rax
sarq $0x20, %rax
movsbl (%r14,%rax), %ecx
leaq 0x5ca4(%rip), %rsi      # 0xcea3
xorl %eax, %eax
callq 0x2210
xorl %ebx, %ebx
movb $0x1, %bpl
cmpb $0x5b, (%r14)
jne 0x7256
movq %r12, %rax
shlq $0x20, %rax
addq %r13, %rax
sarq $0x20, %rax
cmpb $0x5d, (%r14,%rax)
jne 0x7256
movb $0x0, (%r14,%rax)
addl $-0x2, %r12d
incq %r14
movq 0x1e7d7(%rip), %rdi     # 0x25a10
testq %rdi, %rdi
je 0x72a0
leaq 0x5c74(%rip), %rsi      # 0xceb9
xorl %ebp, %ebp
movq %r14, %rdx
xorl %eax, %eax
callq 0x2210
movl $0x10, %ebx
movq 0x1e7b3(%rip), %r13     # 0x25a10
testq %r13, %r13
je 0x72a7
movq %r14, %rdi
callq 0x2110
cmpq $0x3, %rax
jb 0x72a7
callq 0x2030
movq (%rax), %rax
movsbq (%r14), %rcx
movl (%rax,%rcx,4), %edx
movsbl 0x1(%r14), %ecx
movsbq 0x2(%r14), %rsi
movl (%rax,%rsi,4), %r8d
leaq 0x5c37(%rip), %rsi      # 0xcecb
movq %r13, %rdi
xorl %eax, %eax
callq 0x2210
jmp 0x72a7
movl $0x10, %ebx
xorl %ebp, %ebp
callq 0x2030
movq (%rax), %rax
movsbq (%r14), %rcx
movl (%rax,%rcx,4), %ecx
leal -0x41(%rcx), %eax
cmpl $0x5, %eax
ja 0x7301
leaq 0x4e83(%rip), %rcx      # 0xc148
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpb $0x2c, 0x1(%r14)
jne 0x7401
addq $0x2, %r14
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r14, %rsi
callq 0x7142
movl $0x0, 0x1e730(%rip)     # 0x25a24
orl %eax, %ebx
orl $0x86, %ebx
jmp 0x7608
cmpl $0x57, %ecx
jne 0x7401
cmpb $0x2c, 0x1(%r14)
jne 0x7401
addq $0x2, %r14
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r14, %rsi
callq 0x7142
movl $0x0, 0x1e6f4(%rip)     # 0x25a24
orl %eax, %ebx
orl $0x8e, %ebx
jmp 0x7608
cmpb $0x2c, 0x1(%r14)
jne 0x7401
addq $0x2, %r14
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r14, %rsi
callq 0x7142
movl $0x0, 0x1e6c1(%rip)     # 0x25a24
orl %eax, %ebx
orl $0x8a, %ebx
jmp 0x7608
cmpb $0x2c, 0x1(%r14)
jne 0x7401
addq $0x2, %r14
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r14, %rsi
callq 0x7142
movl $0x0, 0x1e68e(%rip)     # 0x25a24
orl %eax, %ebx
orl $0x8b, %ebx
jmp 0x7608
cmpb $0x2c, 0x1(%r14)
jne 0x7401
addq $0x2, %r14
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r14, %rsi
callq 0x7142
movl $0x0, 0x1e65f(%rip)     # 0x25a24
orl %eax, %ebx
orl $0x85, %ebx
jmp 0x7608
cmpb $0x2c, 0x1(%r14)
jne 0x7401
addq $0x2, %r14
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r14, %rsi
callq 0x7142
movl $0x0, 0x1e630(%rip)     # 0x25a24
orl %eax, %ebx
orl $0x87, %ebx
jmp 0x7608
movq 0x1e608(%rip), %rdi     # 0x25a10
testq %rdi, %rdi
je 0x7429
movslq %r12d, %rdx
leaq (%r14,%rdx), %r8
addq $-0x3, %r8
leaq 0x5abf(%rip), %rsi      # 0xcede
movq %r14, %rcx
xorl %eax, %eax
callq 0x2210
cmpl $0x5, %r12d
jl 0x7454
movl %r12d, %r13d
leaq (%r14,%r13), %rdi
addq $-0x4, %rdi
leaq 0x5abd(%rip), %rsi      # 0xcefe
movl $0x4, %edx
callq 0x2c53
testl %eax, %eax
jne 0x7460
jmp 0x757d
movl $0x4, %r13d
cmpl $0x4, %r12d
jne 0x7481
leaq (%r14,%r13), %rdi
addq $-0x3, %rdi
leaq 0x5a94(%rip), %rsi      # 0xcf03
movl $0x3, %edx
callq 0x2c53
testl %eax, %eax
je 0x757d
cmpb $0x2c, (%r14)
je 0x74b8
leaq 0x4(%rsp), %r12
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x3e17
movq %rax, %r14
movb (%rax), %al
cmpb $0x2c, %al
sete %cl
movl (%r12), %r12d
testl %r12d, %r12d
sete %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x761d
xorl %r15d, %r15d
cmpb $0x2d, 0x1(%r14,%r15)
leaq 0x1(%r15), %r15
je 0x74bb
leaq (%r14,%r15), %rsi
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x715e
movl %eax, %r12d
movq 0x1e531(%rip), %rdi     # 0x25a10
testq %rdi, %rdi
je 0x74f5
leaq 0x5a32(%rip), %rsi      # 0xcf1d
movl %r12d, %edx
xorl %eax, %eax
callq 0x2210
leaq -0x1(%r15), %rdx
xorl %r13d, %r13d
leaq (%r14,%r13), %rax
incq %r13
cmpb $0x2b, 0x1(%r15,%rax)
je 0x74fc
movq 0x1e4fe(%rip), %rdi     # 0x25a10
addq %r13, %r14
testq %rdi, %rdi
je 0x752c
leaq -0x1(%r13), %rcx
leaq 0x5a0f(%rip), %rsi      # 0xcf34
xorl %eax, %eax
callq 0x2210
leaq (%r14,%r15), %rdi
testl %r12d, %r12d
js 0x77c4
movl %r13d, %ecx
xorl $0x2, %ecx
movl %r15d, %r8d
xorl $0x1, %r8d
xorl %eax, %eax
orl %r8d, %ecx
movl $0x0, %edx
movl $0x0, %esi
movl $0x0, %ecx
je 0x7732
movl %r13d, %eax
xorl $0x3, %eax
orl %r8d, %eax
jne 0x7673
movl $0x1, %ecx
movb $0x1, %dl
xorl %eax, %eax
jmp 0x7730
movq 0x1e48c(%rip), %rdi     # 0x25a10
testq %rdi, %rdi
je 0x759a
leaq 0x5977(%rip), %rsi      # 0xcf07
movq %r14, %rdx
xorl %eax, %eax
callq 0x2210
leaq 0x4(%rsp), %r12
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x3e17
movslq 0xd181(%rip), %rcx      # 0x14734
movl (%r12), %eax
subl %ecx, %eax
cmpl $0x0, 0x7d990(%rip)     # 0x84f50
js 0x75f2
leal 0x7d(%rax), %edx
cmpl $0xff, %edx
ja 0x75da
leaq 0xd67c(%rip), %rdx      # 0x14c50
cmpb $-0x73, (%rcx,%rdx)
jne 0x75f2
movl $0x2, 0x1e440(%rip)     # 0x25a24
addl $-0x4, %eax
movl %eax, (%r15)
orl $0x8d, %ebx
jmp 0x7608
addl $-0x3, %eax
movl $0x1, 0x1e425(%rip)     # 0x25a24
movl %eax, (%r15)
orl $0x8c, %ebx
movl %ebx, %r12d
movl %r12d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x2c, %al
jne 0x77c9
movq 0x1e3e4(%rip), %rdi     # 0x25a10
testq %rdi, %rdi
je 0x7642
leaq 0x5932(%rip), %rsi      # 0xcf6a
movl %r12d, %edx
xorl %eax, %eax
callq 0x2210
movl %r12d, (%r15)
incq %r14
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r14, %rsi
callq 0x715e
cmpl $0xf, %eax
jne 0x7691
movl $0x2, 0x1e3c0(%rip)     # 0x25a24
movzbl %bpl, %eax
movl $0xb0, %r12d
subl %eax, %r12d
jmp 0x760b
movl %r13d, %eax
xorl $0x1, %eax
movl %r15d, %ecx
xorl $0x2, %ecx
orl %eax, %ecx
jne 0x76ca
movl $0x2, %ecx
xorl %eax, %eax
xorl %edx, %edx
jmp 0x7730
movl 0x7d8b9(%rip), %ecx     # 0x84f50
testl %ecx, %ecx
setle %dl
leal 0x10(%r12), %esi
cmpl $0x20, %esi
setb %sil
andb %dl, %bpl
andb %sil, %bpl
cmpb $0x1, %bpl
jne 0x76e2
movl $0x0, 0x1e366(%rip)     # 0x25a24
andl $0x1f, %r12d
orl %eax, %r12d
jmp 0x760b
movl %r15d, %ecx
xorl $0x3, %ecx
orl %ecx, %eax
jne 0x7713
movl $0x3, %ecx
movb $0x1, %sil
xorl %eax, %eax
xorl %edx, %edx
jmp 0x7732
testl %ecx, %ecx
sets %cl
movsbl %r12b, %edx
cmpl %r12d, %edx
sete %dl
orb %cl, %dl
orl %eax, %ebx
cmpb $0x1, %dl
jne 0x77af
movl $0x1, 0x1e31c(%rip)     # 0x25a24
orl $0x88, %ebx
jmp 0x7608
cmpl $0x1, %r13d
jne 0x77c4
cmpl $0x1, %r15d
jne 0x77c4
xorl %edx, %edx
movl $0x4, %ecx
movb $0x1, %al
xorl %esi, %esi
movl $0x0, 0x1e2e8(%rip)     # 0x25a24
cmpl $0xf, %r12d
jne 0x7796
orb %dl, %sil
orb %al, %sil
je 0x77c4
xorb $0x1, %dl
movzbl %dl, %ecx
shll $0x5, %ecx
orl $0xcf, %ecx
testb %al, %al
movl $0x8f, %eax
cmovel %ecx, %eax
xorb $0x1, %bpl
movzbl %bpl, %r12d
addl %eax, %r12d
movq 0x1e29b(%rip), %rdi     # 0x25a10
testq %rdi, %rdi
je 0x760b
leaq 0x57cb(%rip), %rsi      # 0xcf50
movl %r12d, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x2210
jmp 0x760b
cmpb $0x0, (%r14,%r15)
jne 0x77c4
orl %ecx, %r12d
orl %ebx, %r12d
orl $0x80, %r12d
jmp 0x760b
movl $0x2, 0x1e26b(%rip)     # 0x25a24
orl $0x89, %ebx
jmp 0x7608
callq 0x70c5
movq %r14, %rdi
callq 0x70c5