import { expect, test } from 'bun:test';
import CS from '../../capstone';

test('CS.ARCH_M680X', () => {
  const buffer = new Uint8Array([
    0x00, 0x04, 0x01, 0x00, 0x0c, 0x00, 0x80, 0x0e, 0x00, 0x80, 0x00, 0x11,
    0x1e, 0x10, 0x00, 0x80, 0x00, 0x3b, 0x4a, 0x10, 0x00, 0x04, 0x4b, 0x01,
    0x04, 0x4f, 0x7f, 0x80, 0x00, 0x8f, 0x10, 0x00, 0xb7, 0x52, 0xb7, 0xb1,
    0xa6, 0x67, 0xa6, 0xfe, 0xa6, 0xf7, 0x18, 0x02, 0xe2, 0x30, 0x39, 0xe2,
    0x10, 0x00, 0x18, 0x0c, 0x30, 0x39, 0x10, 0x00, 0x18, 0x11, 0x18, 0x12,
    0x10, 0x00, 0x18, 0x19, 0x00, 0x18, 0x1e, 0x00, 0x18, 0x3e, 0x18, 0x3f,
    0x00,
  ]);
  const disassembler = new CS.CAPSTONE(CS.ARCH_M680X, CS.MODE_M680X_CPU12);
  disassembler.option(CS.OPT_DETAIL, true);
  const insns = disassembler.disasm(buffer, 0x1000);

  expect(
    insns.map(({ id, address, size, mnemonic, op_str, bytes }) => ({
      id,
      address,
      size,
      mnemonic,
      op_str,
      bytes,
    })),
  ).toEqual([
    {
      id: 42,
      address: 4096,
      size: 1,
      mnemonic: 'bgnd',
      op_str: '',
      bytes: new Uint8Array([0]),
    },
    {
      id: 118,
      address: 4097,
      size: 3,
      mnemonic: 'dbeq',
      op_str: 'b, $1004',
      bytes: new Uint8Array([4, 1, 0]),
    },
    {
      id: 70,
      address: 4100,
      size: 3,
      mnemonic: 'bset',
      op_str: '0, x; #-128',
      bytes: new Uint8Array([12, 0, 128]),
    },
    {
      id: 67,
      address: 4103,
      size: 4,
      mnemonic: 'brset',
      op_str: '0, x; #-128; $100b',
      bytes: new Uint8Array([14, 0, 128, 0]),
    },
    {
      id: 137,
      address: 4107,
      size: 1,
      mnemonic: 'ediv',
      op_str: '',
      bytes: new Uint8Array([17]),
    },
    {
      id: 67,
      address: 4108,
      size: 5,
      mnemonic: 'brset',
      op_str: '$1000, #-128, $1011',
      bytes: new Uint8Array([30, 16, 0, 128, 0]),
    },
    {
      id: 249,
      address: 4113,
      size: 1,
      mnemonic: 'pshd',
      op_str: '',
      bytes: new Uint8Array([59]),
    },
    {
      id: 74,
      address: 4114,
      size: 4,
      mnemonic: 'call',
      op_str: '$1000, 4',
      bytes: new Uint8Array([74, 16, 0, 4]),
    },
    {
      id: 74,
      address: 4118,
      size: 3,
      mnemonic: 'call',
      op_str: '1, x; 4',
      bytes: new Uint8Array([75, 1, 4]),
    },
    {
      id: 66,
      address: 4121,
      size: 4,
      mnemonic: 'brclr',
      op_str: '$7f, #-128, $101d',
      bytes: new Uint8Array([79, 127, 128, 0]),
    },
    {
      id: 113,
      address: 4125,
      size: 3,
      mnemonic: 'cps',
      op_str: '#4096',
      bytes: new Uint8Array([143, 16, 0]),
    },
    {
      id: 335,
      address: 4128,
      size: 2,
      mnemonic: 'tfr',
      op_str: 'x, cc',
      bytes: new Uint8Array([183, 82]),
    },
    {
      id: 335,
      address: 4130,
      size: 2,
      mnemonic: 'exg',
      op_str: 'tmp3, b',
      bytes: new Uint8Array([183, 177]),
    },
    {
      id: 191,
      address: 4132,
      size: 2,
      mnemonic: 'ldaa',
      op_str: '8, +y',
      bytes: new Uint8Array([166, 103]),
    },
    {
      id: 191,
      address: 4134,
      size: 2,
      mnemonic: 'ldaa',
      op_str: 'd, pc',
      bytes: new Uint8Array([166, 254]),
    },
    {
      id: 191,
      address: 4136,
      size: 2,
      mnemonic: 'ldaa',
      op_str: '[d, s]',
      bytes: new Uint8Array([166, 247]),
    },
    {
      id: 228,
      address: 4138,
      size: 8,
      mnemonic: 'movw',
      op_str: '12345, x; 4096, x',
      bytes: new Uint8Array([24, 2, 226, 48, 57, 226, 16, 0]),
    },
    {
      id: 227,
      address: 4146,
      size: 6,
      mnemonic: 'movb',
      op_str: '$3039, $1000',
      bytes: new Uint8Array([24, 12, 48, 57, 16, 0]),
    },
    {
      id: 154,
      address: 4152,
      size: 2,
      mnemonic: 'fdiv',
      op_str: '',
      bytes: new Uint8Array([24, 17]),
    },
    {
      id: 140,
      address: 4154,
      size: 4,
      mnemonic: 'emacs',
      op_str: '$1000',
      bytes: new Uint8Array([24, 18, 16, 0]),
    },
    {
      id: 224,
      address: 4158,
      size: 3,
      mnemonic: 'mina',
      op_str: '0, x',
      bytes: new Uint8Array([24, 25, 0]),
    },
    {
      id: 142,
      address: 4161,
      size: 3,
      mnemonic: 'emaxm',
      op_str: '0, x',
      bytes: new Uint8Array([24, 30, 0]),
    },
    {
      id: 306,
      address: 4164,
      size: 2,
      mnemonic: 'stop',
      op_str: '',
      bytes: new Uint8Array([24, 62]),
    },
    {
      id: 152,
      address: 4166,
      size: 3,
      mnemonic: 'etbl',
      op_str: '0, x',
      bytes: new Uint8Array([24, 63, 0]),
    },
  ]);

  disassembler.close();
});
