// File: /workspace/github_cpp_makefile_star2_O3/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
xorl %edx, %edx
leaq 0xd928(%rip), %rcx      # 0x1e500
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
cmpw $0x1, %ax
jne 0x10bd8
movzwl %dx, %edx
cmpl $0x7fff, %edx           # imm = 0x7FFF
jne 0x10d23
xorl %edx, %edx
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
cmpw $0x1, %ax
jne 0x10c18
cmpw $0x5d, %dx
jne 0x10d42
movaps 0x5424(%rip), %xmm0     # 0x16080
movaps %xmm0, 0x1d8ad(%rip)    # 0x2e510
movaps 0x5406(%rip), %xmm0     # 0x16070
movaps %xmm0, 0x1d88f(%rip)    # 0x2e500
movaps 0x5198(%rip), %xmm0     # 0x15e10
movaps %xmm0, 0x1d8b1(%rip)    # 0x2e530
movaps 0x50fa(%rip), %xmm0     # 0x15d80
movups %xmm0, 0x1e0(%rdi)
movaps 0x50fc(%rip), %xmm0     # 0x15d90
movups %xmm0, 0x1f0(%rdi)
movaps 0x50fe(%rip), %xmm0     # 0x15da0
movups %xmm0, 0x200(%rdi)
movaps 0x5100(%rip), %xmm0     # 0x15db0
movups %xmm0, 0x210(%rdi)
movabsq $0xd071e5028140a05, %rax # imm = 0xD071E5028140A05
movq %rax, 0x1d858(%rip)     # 0x2e520
movabsq $0x1008246030180c06, %rax # imm = 0x1008246030180C06
movq %rax, 0x1d84f(%rip)     # 0x2e528
movaps 0x53b0(%rip), %xmm0     # 0x16090
movaps %xmm0, 0x1d859(%rip)    # 0x2e540
movaps 0x53b2(%rip), %xmm0     # 0x160a0
movaps %xmm0, 0x1d85b(%rip)    # 0x2e550
xorl %eax, %eax
leaq 0x1d862(%rip), %rcx     # 0x2e560
leaq 0x50bb(%rip), %rdx      # 0x15dc0
movaps (%rax,%rdx), %xmm0
movaps %xmm0, (%rax,%rcx)
addq $0x10, %rax
cmpq $0x40, %rax
jne 0x10d05
movl $0x5d7fff, 0x1d87f(%rip) # imm = 0x5D7FFF
popq %rax
retq
leaq 0x5279(%rip), %rdi      # 0x15fa3
leaq 0x5281(%rip), %rsi      # 0x15fb2
leaq 0x5282(%rip), %rcx      # 0x15fba
movl $0x9b, %edx
callq 0x4260
leaq 0x528d(%rip), %rdi      # 0x15fd6
leaq 0x5262(%rip), %rsi      # 0x15fb2
leaq 0x5263(%rip), %rcx      # 0x15fba
movl $0xa9, %edx
callq 0x4260
nop