// File: /workspace/github_c_makefile_star2_O2/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
andl $0x0, 0x4(%rsp)
callq 0x2110
movq %rax, %r12
movq 0x1e581(%rip), %rdi     # 0x24a10
testq %rdi, %rdi
je 0x64b9
movsbl (%r14), %edx
movq %r12, %rax
shlq $0x20, %rax
addq %r13, %rax
sarq $0x20, %rax
movsbl (%r14,%rax), %ecx
leaq 0x5981(%rip), %rsi      # 0xbe33
xorl %eax, %eax
callq 0x2210
xorl %ebx, %ebx
movb $0x1, %bpl
cmpb $0x5b, (%r14)
jne 0x6507
movq %r12, %rax
shlq $0x20, %rax
addq %r13, %rax
sarq $0x20, %rax
cmpb $0x5d, (%r14,%rax)
jne 0x6507
movb $0x0, (%r14,%rax)
addl $-0x2, %r12d
incq %r14
movq 0x1e524(%rip), %rdi     # 0x24a10
testq %rdi, %rdi
je 0x6551
leaq 0x5951(%rip), %rsi      # 0xbe49
xorl %ebp, %ebp
movq %r14, %rdx
xorl %eax, %eax
callq 0x2210
pushq $0x10
popq %rbx
movq 0x1e502(%rip), %r13     # 0x24a10
testq %r13, %r13
je 0x6556
movq %r14, %rdi
callq 0x2110
cmpq $0x3, %rax
jb 0x6556
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 0x5916(%rip), %rsi      # 0xbe5b
movq %r13, %rdi
xorl %eax, %eax
callq 0x2210
jmp 0x6556
pushq $0x10
popq %rbx
xorl %ebp, %ebp
callq 0x2030
movq (%rax), %rax
movsbq (%r14), %rcx
movl (%rax,%rcx,4), %ecx
leal -0x41(%rcx), %eax
cmpl $0x5, %eax
ja 0x65ab
leaq 0x4b64(%rip), %rcx      # 0xb0d8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpb $0x2c, 0x1(%r14)
jne 0x668e
addq $0x2, %r14
pushq $-0x1
popq %rdi
movq %r14, %rsi
callq 0x63fa
andl $0x0, 0x1e486(%rip)     # 0x24a24
orl %eax, %ebx
orl $0x86, %ebx
jmp 0x688b
cmpl $0x57, %ecx
jne 0x668e
cmpb $0x2c, 0x1(%r14)
jne 0x668e
addq $0x2, %r14
pushq $-0x1
popq %rdi
movq %r14, %rsi
callq 0x63fa
andl $0x0, 0x1e44f(%rip)     # 0x24a24
orl %eax, %ebx
orl $0x8e, %ebx
jmp 0x688b
cmpb $0x2c, 0x1(%r14)
jne 0x668e
addq $0x2, %r14
pushq $-0x1
popq %rdi
movq %r14, %rsi
callq 0x63fa
andl $0x0, 0x1e421(%rip)     # 0x24a24
orl %eax, %ebx
orl $0x8a, %ebx
jmp 0x688b
cmpb $0x2c, 0x1(%r14)
jne 0x668e
addq $0x2, %r14
pushq $-0x1
popq %rdi
movq %r14, %rsi
callq 0x63fa
andl $0x0, 0x1e3f7(%rip)     # 0x24a24
orl %eax, %ebx
orl $0x8b, %ebx
jmp 0x688b
cmpb $0x2c, 0x1(%r14)
jne 0x668e
addq $0x2, %r14
pushq $-0x1
popq %rdi
movq %r14, %rsi
callq 0x63fa
andl $0x0, 0x1e3cd(%rip)     # 0x24a24
orl %eax, %ebx
orl $0x85, %ebx
jmp 0x688b
cmpb $0x2c, 0x1(%r14)
jne 0x668e
addq $0x2, %r14
pushq $-0x1
popq %rdi
movq %r14, %rsi
callq 0x63fa
andl $0x0, 0x1e3a3(%rip)     # 0x24a24
orl %eax, %ebx
orl $0x87, %ebx
jmp 0x688b
movq 0x1e37b(%rip), %rdi     # 0x24a10
testq %rdi, %rdi
je 0x66b6
movslq %r12d, %rdx
leaq (%r14,%rdx), %r8
addq $-0x3, %r8
leaq 0x57c2(%rip), %rsi      # 0xbe6e
movq %r14, %rcx
xorl %eax, %eax
callq 0x2210
cmpl $0x5, %r12d
jl 0x66df
movl %r12d, %r12d
leaq (%r14,%r12), %rdi
addq $-0x4, %rdi
leaq 0x57c0(%rip), %rsi      # 0xbe8e
pushq $0x4
popq %rdx
callq 0x291a
testl %eax, %eax
jne 0x66e9
jmp 0x6800
cmpl $0x4, %r12d
jne 0x6708
pushq $0x4
popq %r12
leaq (%r14,%r12), %rdi
addq $-0x3, %rdi
leaq 0x579b(%rip), %rsi      # 0xbe93
pushq $0x3
popq %rdx
callq 0x291a
testl %eax, %eax
je 0x6800
cmpb $0x2c, (%r14)
je 0x673f
leaq 0x4(%rsp), %r12
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x37d5
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 0x68a0
xorl %r15d, %r15d
cmpb $0x2d, 0x1(%r14,%r15)
leaq 0x1(%r15), %r15
je 0x6742
leaq (%r14,%r15), %rsi
pushq $-0x1
popq %rdi
callq 0x6416
movl %eax, %r12d
movq 0x1e2ac(%rip), %rdi     # 0x24a10
testq %rdi, %rdi
je 0x677a
leaq 0x573d(%rip), %rsi      # 0xbead
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 0x6781
movq 0x1e279(%rip), %rdi     # 0x24a10
addq %r13, %r14
testq %rdi, %rdi
je 0x67b1
leaq -0x1(%r13), %rcx
leaq 0x571a(%rip), %rsi      # 0xbec4
xorl %eax, %eax
callq 0x2210
leaq (%r14,%r15), %rdi
testl %r12d, %r12d
js 0x6a39
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 0x69aa
movl %r13d, %eax
xorl $0x3, %eax
orl %r8d, %eax
jne 0x68f4
pushq $0x1
popq %rcx
movb $0x1, %dl
xorl %eax, %eax
jmp 0x69a8
movq 0x1e209(%rip), %rdi     # 0x24a10
testq %rdi, %rdi
je 0x681d
leaq 0x5684(%rip), %rsi      # 0xbe97
movq %r14, %rdx
xorl %eax, %eax
callq 0x2210
leaq 0x4(%rsp), %r12
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x37d5
movslq 0xcefe(%rip), %rcx      # 0x13734
movl (%r12), %eax
subl %ecx, %eax
cmpl $0x0, 0x7d70d(%rip)     # 0x83f50
js 0x6875
leal 0x7d(%rax), %edx
cmpl $0xff, %edx
ja 0x685d
leaq 0xd3f9(%rip), %rdx      # 0x13c50
cmpb $-0x73, (%rcx,%rdx)
jne 0x6875
movl $0x2, 0x1e1bd(%rip)     # 0x24a24
addl $-0x4, %eax
movl %eax, (%r15)
orl $0x8d, %ebx
jmp 0x688b
addl $-0x3, %eax
movl $0x1, 0x1e1a2(%rip)     # 0x24a24
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 0x6a3e
movq 0x1e161(%rip), %rdi     # 0x24a10
testq %rdi, %rdi
je 0x68c5
leaq 0x563f(%rip), %rsi      # 0xbefa
movl %r12d, %edx
xorl %eax, %eax
callq 0x2210
movl %r12d, (%r15)
incq %r14
pushq $-0x1
popq %rdi
movq %r14, %rsi
callq 0x6416
cmpl $0xf, %eax
jne 0x6910
movl $0x2, 0x1e13f(%rip)     # 0x24a24
movzbl %bpl, %eax
movl $0xb0, %r12d
subl %eax, %r12d
jmp 0x688e
movl %r13d, %eax
xorl $0x1, %eax
movl %r15d, %ecx
xorl $0x2, %ecx
orl %eax, %ecx
jne 0x6946
pushq $0x2
popq %rcx
xorl %eax, %eax
xorl %edx, %edx
jmp 0x69a8
movl 0x7d63a(%rip), %ecx     # 0x83f50
testl %ecx, %ecx
setle %dl
leal 0x10(%r12), %esi
cmpl $0x20, %esi
setb %sil
andb %dl, %bpl
andb %sil, %bpl
cmpb $0x1, %bpl
jne 0x695c
andl $0x0, 0x1e0ea(%rip)     # 0x24a24
andl $0x1f, %r12d
orl %eax, %r12d
jmp 0x688e
movl %r15d, %ecx
xorl $0x3, %ecx
orl %ecx, %eax
jne 0x698d
pushq $0x3
popq %rcx
movb $0x1, %sil
xorl %eax, %eax
xorl %edx, %edx
jmp 0x69aa
testl %ecx, %ecx
sets %cl
movsbl %r12b, %edx
cmpl %r12d, %edx
sete %dl
orb %cl, %dl
orl %eax, %ebx
cmpb $0x1, %dl
jne 0x6a24
movl $0x1, 0x1e0a2(%rip)     # 0x24a24
orl $0x88, %ebx
jmp 0x688b
cmpl $0x1, %r13d
jne 0x6a39
cmpl $0x1, %r15d
jne 0x6a39
xorl %edx, %edx
pushq $0x4
popq %rcx
movb $0x1, %al
xorl %esi, %esi
andl $0x0, 0x1e073(%rip)     # 0x24a24
cmpl $0xf, %r12d
jne 0x6a0b
orb %dl, %sil
orb %al, %sil
je 0x6a39
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 0x1e026(%rip), %rdi     # 0x24a10
testq %rdi, %rdi
je 0x688e
leaq 0x54e6(%rip), %rsi      # 0xbee0
movl %r12d, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x2210
jmp 0x688e
cmpb $0x0, (%r14,%r15)
jne 0x6a39
orl %ecx, %r12d
orl %ebx, %r12d
orl $0x80, %r12d
jmp 0x688e
movl $0x2, 0x1dff6(%rip)     # 0x24a24
orl $0x89, %ebx
jmp 0x688b
callq 0x637b
movq %r14, %rdi
callq 0x637b