// File: /workspace/github_cpp_makefile_star2_O2/khedoros[P]khednes/apu.cpp
// Function: apu::generate_arrays()
void apu::generate_arrays() {
    shift_reg val;
    val.val = 1;
    bool start = true;
    uint16_t count = 0;
    //generate the long noise values
    while (start || val.val != 1) {
        start = false;
        unsigned char bop = val.bit13 ^ val.bit14;
        noise_states[0][count] = ((val.bit13 ^ val.bit14)?0:1);
        val.val <<=(1);
        val.val |= (unsigned int)(bop);
        ++count;
    }
    //cout<<"Long count: "<<count<<endl;
    assert(count == 32767);
    val.val = 1;
    start = true;
    count = 0;
    //generate the short noise values
    while (start || val.val != 1) {
        start = false;
        uint8_t bop = val.bit8 ^ val.bit14;
        noise_states[1][count] = ((val.bit8 ^ val.bit14)?0:1);
        val.val <<=(1);
        val.val |= bop;
        ++count;
    }
    //cout<<"Short count: "<<count<<endl;
    assert(count == 93);

    uint16_t n_f_t[16] = { 0x002, 0x004, 0x008, 0x010,
                           0x020, 0x030, 0x040, 0x050,
                           0x065, 0x07F, 0x0BE, 0x0FE,
                           0x17D, 0x1FC, 0x3F9, 0x7F2 };

    uint8_t g_l_t[16] = { 0x7f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                          0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };

    int dmc_f[16] = { 0xD60, 0xBE0, 0xAA0, 0xA00, 0x8F0, 0x7F0, 0x710, 0x6B0,
                      0x5F0, 0x500, 0x470, 0x400, 0x350, 0x2A8, 0x240, 0x1B0 };

    for(uint8_t i=0;i<16;++i) {
        noise_freq_table[i] = n_f_t[i];
        general_len_table[i] = g_l_t[i];
        dmc_freq_table[i] = dmc_f[i];
    }

    uint8_t p_l_t[8] = { 0x05, 0x0A, 0x14, 0x28, 0x50, 0x1E, 0x07, 0x0D };
    uint8_t n_l_t[8] = { 0x06, 0x0C, 0x18, 0x30, 0x60, 0x24, 0x08, 0x10 };

    for(uint8_t i=0;i<8;++i) {
        pal_len_table[i] = p_l_t[i];
        ntsc_len_table[i] = n_l_t[i];
    }

    uint8_t t_s[TRI_LEN] = { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };

    for(uint8_t i=0;i<32;++i) TRI_STATES[i] = t_s[i];

    uint8_t s_s[4][SQ_LEN] = {{0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0},
                              {0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
                              {1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1}};

    for(uint8_t i=0;i<4;++i)
        for(uint8_t j=0;j<16;++j)
            SQ_STATES[i][j] = s_s[i][j];

    uint16_t n_l[2] = {32767, 93};
    NOISE_LEN[0] = n_l[0];
    NOISE_LEN[1] = n_l[1];
}

// Assembly:
pushq %rax
movw $0x1, %ax
movb $0x1, %sil
xorl %edx, %edx
leaq 0xe907(%rip), %rcx      # 0x1d510
testb $0x1, %sil
jne 0xec1d
movl %eax, %esi
andl $0x7fff, %esi           # imm = 0x7FFF
cmpw $0x1, %si
je 0xec4a
movzwl %ax, %esi
movl %esi, %r8d
shrl $0xd, %r8d
shrl $0xe, %esi
xorl %r8d, %esi
andl $0x1, %esi
andl $0x3fff, %eax           # imm = 0x3FFF
leal (%rsi,%rax,2), %eax
xorb $0x1, %sil
movzwl %dx, %r8d
movb %sil, (%r8,%rcx)
incl %edx
xorl %esi, %esi
jmp 0xec09
cmpw $0x7fff, %dx            # imm = 0x7FFF
jne 0xeda4
movb $0x1, %sil
xorl %edx, %edx
testb $0x1, %sil
jne 0xec6e
movl %eax, %esi
andl $0x7fff, %esi           # imm = 0x7FFF
cmpw $0x1, %si
je 0xec9f
movzwl %ax, %esi
movl %esi, %r8d
shrl $0x8, %r8d
shrl $0xe, %esi
xorl %r8d, %esi
andl $0x1, %esi
andl $0x3fff, %eax           # imm = 0x3FFF
leal (%rsi,%rax,2), %eax
xorb $0x1, %sil
movzwl %dx, %r8d
movb %sil, 0x7fff(%r8,%rcx)
incl %edx
xorl %esi, %esi
jmp 0xec5a
cmpw $0x5d, %dx
jne 0xedc3
pushq $-0x20
popq %rax
leaq 0x508d(%rip), %rcx      # 0x13d40
xorl %edx, %edx
leaq 0x5374(%rip), %rsi      # 0x14030
leaq 0x1e84d(%rip), %r8      # 0x2d510
leaq 0x5106(%rip), %r9       # 0x13dd0
leaq 0x1e86f(%rip), %r10     # 0x2d540
testq %rax, %rax
je 0xed02
movzwl 0x20(%rax,%rsi), %r11d
movw %r11w, 0x20(%rax,%r8)
movb (%rdx,%r9), %r11b
movb %r11b, (%rdx,%r10)
movl (%rcx), %r11d
movl %r11d, 0x220(%rdi,%rax,2)
incq %rdx
addq $0x2, %rax
addq $0x4, %rcx
jmp 0xecd1
xorl %eax, %eax
leaq 0x4fad(%rip), %rcx      # 0x13cb8
leaq 0x1e81e(%rip), %rdx     # 0x2d530
leaq 0x4fa7(%rip), %rsi      # 0x13cc0
leaq 0x1e818(%rip), %rdi     # 0x2d538
cmpq $0x8, %rax
je 0xed3b
movb (%rax,%rcx), %r8b
movb %r8b, (%rax,%rdx)
movb (%rax,%rsi), %r8b
movb %r8b, (%rax,%rdi)
incq %rax
jmp 0xed20
xorl %eax, %eax
leaq 0x530c(%rip), %rcx      # 0x14050
leaq 0x1e805(%rip), %rdx     # 0x2d550
cmpq $0x20, %rax
je 0xed5e
movb (%rax,%rcx), %sil
movb %sil, (%rax,%rdx)
incq %rax
jmp 0xed4b
xorl %eax, %eax
leaq 0x5019(%rip), %rcx      # 0x13d80
leaq 0x1e802(%rip), %rdx     # 0x2d570
xorl %esi, %esi
cmpq $0x4, %rsi
je 0xed98
xorl %edi, %edi
cmpq $0x10, %rdi
je 0xed8f
leaq (%rax,%rdi), %r8
movb (%rcx,%r8), %r9b
movb %r9b, (%rdx,%r8)
incq %rdi
jmp 0xed78
incq %rsi
addq $0x10, %rax
jmp 0xed70
movl $0x5d7fff, 0x1e80e(%rip) # imm = 0x5D7FFF
popq %rax
retq
leaq 0x51b8(%rip), %rdi      # 0x13f63
leaq 0x51c0(%rip), %rsi      # 0x13f72
leaq 0x51c1(%rip), %rcx      # 0x13f7a
movl $0x9b, %edx
callq 0x4250
leaq 0x51cc(%rip), %rdi      # 0x13f96
leaq 0x51a1(%rip), %rsi      # 0x13f72
leaq 0x51a2(%rip), %rcx      # 0x13f7a
movl $0xa9, %edx
callq 0x4250