// File: rfotino[P]consolite-emulator/src/processor.cpp
// Function: EmuProcessor::_setFlags(unsigned int const&, unsigned int const&, unsigned int const&, unsigned char const&)
void EmuProcessor::_setFlags(const uint32_t& dest,
                             const uint32_t& src,
                             const uint32_t& result,
                             const uint8_t& opcode) {
  // Overflow set if we added two positives and got a negative,
  // added two negatives and got a positive, subtracted a positive
  // from a negative and got a positive, or subtracted a negative
  // from a positive and got a negative
  if (OPCODE_ADD == opcode) {
    _overflowFlag =
      ((0x8000 & dest) && (0x8000 & src) && !(0x8000 & result)) ||
      (!(0x8000 & dest) && !(0x8000 & src) && (0x8000 & result));
  } else if (OPCODE_SUB == opcode || OPCODE_CMP == opcode) {
    _overflowFlag =
      ((0x8000 & dest) && !(0x8000 & src) && !(0x8000 & result)) ||
      (!(0x8000 & dest) && (0x8000 & src) && (0x8000 & result));
  } else {
    _overflowFlag = false;
  }
  // Carry set if c is too large to fit into 16 bits.
  _carryFlag = 0xffff < result;
  // Zero set if the result was zero.
  _zeroFlag = !(0xffff & result);
  // Sign flag set if the sign bit of the result is set.
  _signFlag = 0x8000 & result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
movzbl (%rax), %ecx
movl $0xa, %eax
cmpl %ecx, %eax
jne 0x8b65
movq -0x10(%rbp), %rax
movl (%rax), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
je 0x8b0a
movq -0x18(%rbp), %rax
movl (%rax), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
je 0x8b0a
movq -0x20(%rbp), %rax
movl (%rax), %ecx
andl $0x8000, %ecx           # imm = 0x8000
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0x31(%rbp)
je 0x8b50
movq -0x10(%rbp), %rax
movl (%rax), %ecx
andl $0x8000, %ecx           # imm = 0x8000
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x32(%rbp)
jne 0x8b4a
movq -0x18(%rbp), %rax
movl (%rax), %ecx
andl $0x8000, %ecx           # imm = 0x8000
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x32(%rbp)
jne 0x8b4a
movq -0x20(%rbp), %rax
movl (%rax), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
setne %al
movb %al, -0x32(%rbp)
movb -0x32(%rbp), %al
movb %al, -0x31(%rbp)
movq -0x30(%rbp), %rax
movb -0x31(%rbp), %cl
andb $0x1, %cl
movb %cl, 0x1002b(%rax)
jmp 0x8c24
movq -0x28(%rbp), %rax
movzbl (%rax), %ecx
movl $0xb, %eax
cmpl %ecx, %eax
je 0x8b89
movq -0x28(%rbp), %rax
movzbl (%rax), %ecx
movl $0x14, %eax
cmpl %ecx, %eax
jne 0x8c17
movq -0x10(%rbp), %rax
movl (%rax), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
je 0x8bbf
movq -0x18(%rbp), %rax
movl (%rax), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
jne 0x8bbf
movq -0x20(%rbp), %rax
movl (%rax), %ecx
andl $0x8000, %ecx           # imm = 0x8000
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0x33(%rbp)
je 0x8c05
movq -0x10(%rbp), %rax
movl (%rax), %ecx
andl $0x8000, %ecx           # imm = 0x8000
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x34(%rbp)
jne 0x8bff
movq -0x18(%rbp), %rax
movl (%rax), %ecx
andl $0x8000, %ecx           # imm = 0x8000
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x34(%rbp)
je 0x8bff
movq -0x20(%rbp), %rax
movl (%rax), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
setne %al
movb %al, -0x34(%rbp)
movb -0x34(%rbp), %al
movb %al, -0x33(%rbp)
movq -0x30(%rbp), %rax
movb -0x33(%rbp), %cl
andb $0x1, %cl
movb %cl, 0x1002b(%rax)
jmp 0x8c22
movq -0x30(%rbp), %rax
movb $0x0, 0x1002b(%rax)
jmp 0x8c24
movq -0x30(%rbp), %rax
movq -0x20(%rbp), %rdx
movl $0xffff, %ecx           # imm = 0xFFFF
cmpl (%rdx), %ecx
setb %cl
andb $0x1, %cl
movb %cl, 0x1002c(%rax)
movq -0x20(%rbp), %rcx
movl (%rcx), %ecx
andl $0xffff, %ecx           # imm = 0xFFFF
cmpl $0x0, %ecx
setne %cl
xorb $-0x1, %cl
andb $0x1, %cl
movb %cl, 0x1002d(%rax)
movq -0x20(%rbp), %rcx
movl (%rcx), %ecx
andl $0x8000, %ecx           # imm = 0x8000
cmpl $0x0, %ecx
setne %cl
andb $0x1, %cl
movb %cl, 0x1002e(%rax)
popq %rbp
retq
nopw (%rax,%rax)