// File: 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:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x130(%rsp)
movq 0x130(%rsp), %rax
movq %rax, 0x10(%rsp)
movw 0x12c(%rsp), %ax
andw $0x8000, %ax            # imm = 0x8000
orw $0x1, %ax
movw %ax, 0x12c(%rsp)
movb $0x1, 0x12b(%rsp)
movw $0x0, 0x128(%rsp)
movb $0x1, %al
testb $0x1, 0x12b(%rsp)
movb %al, 0xf(%rsp)
jne 0x227bf
movw 0x12c(%rsp), %ax
andw $0x7fff, %ax            # imm = 0x7FFF
movzwl %ax, %eax
cmpl $0x1, %eax
setne %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x227cc
jmp 0x228cf
movb $0x0, 0x12b(%rsp)
movw 0x12c(%rsp), %ax
shrw $0xd, %ax
andw $0x1, %ax
movzwl %ax, %eax
movw 0x12c(%rsp), %cx
shrw $0xe, %cx
andw $0x1, %cx
movzwl %cx, %ecx
xorl %ecx, %eax
movb %al, 0x127(%rsp)
movw 0x12c(%rsp), %ax
shrw $0xd, %ax
andw $0x1, %ax
movzwl %ax, %edx
movw 0x12c(%rsp), %ax
shrw $0xe, %ax
andw $0x1, %ax
movzwl %ax, %eax
xorl %eax, %edx
movl $0x1, %eax
xorl %ecx, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movb %al, %dl
movzwl 0x128(%rsp), %eax
movl %eax, %ecx
leaq 0x16cf5(%rip), %rax     # 0x39540
movb %dl, (%rax,%rcx)
movw 0x12c(%rsp), %ax
andw $0x7fff, %ax            # imm = 0x7FFF
movzwl %ax, %eax
shll %eax
movw %ax, %cx
movw 0x12c(%rsp), %ax
andw $0x7fff, %cx            # imm = 0x7FFF
andw $0x8000, %ax            # imm = 0x8000
orw %cx, %ax
movw %ax, 0x12c(%rsp)
movzbl 0x127(%rsp), %ecx
movw 0x12c(%rsp), %ax
andw $0x7fff, %ax            # imm = 0x7FFF
movzwl %ax, %eax
orl %ecx, %eax
movw %ax, %cx
movw 0x12c(%rsp), %ax
andw $0x7fff, %cx            # imm = 0x7FFF
andw $0x8000, %ax            # imm = 0x8000
orw %cx, %ax
movw %ax, 0x12c(%rsp)
movw 0x128(%rsp), %ax
addw $0x1, %ax
movw %ax, 0x128(%rsp)
jmp 0x22796
movzwl 0x128(%rsp), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x228e0
jmp 0x228ff
leaq 0xa742(%rip), %rdi      # 0x2d029
leaq 0xa74a(%rip), %rsi      # 0x2d038
movl $0x9b, %edx
leaq 0xa746(%rip), %rcx      # 0x2d040
callq 0x4270
movw 0x12c(%rsp), %ax
andw $0x8000, %ax            # imm = 0x8000
orw $0x1, %ax
movw %ax, 0x12c(%rsp)
movb $0x1, 0x12b(%rsp)
movw $0x0, 0x128(%rsp)
movb $0x1, %al
testb $0x1, 0x12b(%rsp)
movb %al, 0xe(%rsp)
jne 0x22952
movw 0x12c(%rsp), %ax
andw $0x7fff, %ax            # imm = 0x7FFF
movzwl %ax, %eax
cmpl $0x1, %eax
setne %al
movb %al, 0xe(%rsp)
movb 0xe(%rsp), %al
testb $0x1, %al
jne 0x2295f
jmp 0x22a66
movb $0x0, 0x12b(%rsp)
movw 0x12c(%rsp), %ax
shrw $0x8, %ax
andw $0x1, %ax
movzwl %ax, %eax
movw 0x12c(%rsp), %cx
shrw $0xe, %cx
andw $0x1, %cx
movzwl %cx, %ecx
xorl %ecx, %eax
movb %al, 0x126(%rsp)
movw 0x12c(%rsp), %ax
shrw $0x8, %ax
andw $0x1, %ax
movzwl %ax, %edx
movw 0x12c(%rsp), %ax
shrw $0xe, %ax
andw $0x1, %ax
movzwl %ax, %eax
xorl %eax, %edx
movl $0x1, %eax
xorl %ecx, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movb %al, %dl
movzwl 0x128(%rsp), %eax
movl %eax, %ecx
leaq 0x16b62(%rip), %rax     # 0x39540
movb %dl, 0x7fff(%rax,%rcx)
movw 0x12c(%rsp), %ax
andw $0x7fff, %ax            # imm = 0x7FFF
movzwl %ax, %eax
shll %eax
movw %ax, %cx
movw 0x12c(%rsp), %ax
andw $0x7fff, %cx            # imm = 0x7FFF
andw $0x8000, %ax            # imm = 0x8000
orw %cx, %ax
movw %ax, 0x12c(%rsp)
movzbl 0x126(%rsp), %ecx
movw 0x12c(%rsp), %ax
andw $0x7fff, %ax            # imm = 0x7FFF
movzwl %ax, %eax
orl %ecx, %eax
movw %ax, %cx
movw 0x12c(%rsp), %ax
andw $0x7fff, %cx            # imm = 0x7FFF
andw $0x8000, %ax            # imm = 0x8000
orw %cx, %ax
movw %ax, 0x12c(%rsp)
movw 0x128(%rsp), %ax
addw $0x1, %ax
movw %ax, 0x128(%rsp)
jmp 0x22929
movzwl 0x128(%rsp), %eax
cmpl $0x5d, %eax
jne 0x22a75
jmp 0x22a94
leaq 0xa5e0(%rip), %rdi      # 0x2d05c
leaq 0xa5b5(%rip), %rsi      # 0x2d038
movl $0xa9, %edx
leaq 0xa5b1(%rip), %rcx      # 0x2d040
callq 0x4270
movq 0xa625(%rip), %rax      # 0x2d0c0
movq %rax, 0x100(%rsp)
movq 0xa61e(%rip), %rax      # 0x2d0c8
movq %rax, 0x108(%rsp)
movq 0xa617(%rip), %rax      # 0x2d0d0
movq %rax, 0x110(%rsp)
movq 0xa610(%rip), %rax      # 0x2d0d8
movq %rax, 0x118(%rsp)
movq 0xa629(%rip), %rax      # 0x2d100
movq %rax, 0xf0(%rsp)
movq 0xa622(%rip), %rax      # 0x2d108
movq %rax, 0xf8(%rsp)
leaq 0xb0(%rsp), %rdi
leaq 0xa2e3(%rip), %rsi      # 0x2cde0
movl $0x40, %edx
callq 0x4330
movb $0x0, 0xaf(%rsp)
movzbl 0xaf(%rsp), %eax
cmpl $0x10, %eax
jge 0x22ba2
movq 0x10(%rsp), %rax
movzbl 0xaf(%rsp), %ecx
movw 0x100(%rsp,%rcx,2), %si
movzbl 0xaf(%rsp), %ecx
movl %ecx, %edx
leaq 0x269fa(%rip), %rcx     # 0x49540
movw %si, (%rcx,%rdx,2)
movzbl 0xaf(%rsp), %ecx
movb 0xf0(%rsp,%rcx), %sil
movzbl 0xaf(%rsp), %ecx
movl %ecx, %edx
leaq 0x26a05(%rip), %rcx     # 0x49570
movb %sil, (%rcx,%rdx)
movzbl 0xaf(%rsp), %ecx
movl 0xb0(%rsp,%rcx,4), %edx
movzbl 0xaf(%rsp), %ecx
movl %edx, 0x1e0(%rax,%rcx,4)
movb 0xaf(%rsp), %al
addb $0x1, %al
movb %al, 0xaf(%rsp)
jmp 0x22b0f
movq 0xa1b7(%rip), %rax      # 0x2cd60
movq %rax, 0xa7(%rsp)
movq 0xa1b0(%rip), %rax      # 0x2cd68
movq %rax, 0x9f(%rsp)
movb $0x0, 0x9e(%rsp)
movzbl 0x9e(%rsp), %eax
cmpl $0x8, %eax
jge 0x22c2d
movzbl 0x9e(%rsp), %eax
movb 0xa7(%rsp,%rax), %dl
movzbl 0x9e(%rsp), %eax
movl %eax, %ecx
leaq 0x2696b(%rip), %rax     # 0x49560
movb %dl, (%rax,%rcx)
movzbl 0x9e(%rsp), %eax
movb 0x9f(%rsp,%rax), %dl
movzbl 0x9e(%rsp), %eax
movl %eax, %ecx
leaq 0x26950(%rip), %rax     # 0x49568
movb %dl, (%rax,%rcx)
movb 0x9e(%rsp), %al
addb $0x1, %al
movb %al, 0x9e(%rsp)
jmp 0x22bc8
movq 0xa4ac(%rip), %rax      # 0x2d0e0
movq %rax, 0x70(%rsp)
movq 0xa4a8(%rip), %rax      # 0x2d0e8
movq %rax, 0x78(%rsp)
movq 0xa4a4(%rip), %rax      # 0x2d0f0
movq %rax, 0x80(%rsp)
movq 0xa49d(%rip), %rax      # 0x2d0f8
movq %rax, 0x88(%rsp)
movb $0x0, 0x6f(%rsp)
movzbl 0x6f(%rsp), %eax
cmpl $0x20, %eax
jge 0x22c98
movzbl 0x6f(%rsp), %eax
movb 0x70(%rsp,%rax), %dl
movzbl 0x6f(%rsp), %eax
movl %eax, %ecx
leaq 0x268f7(%rip), %rax     # 0x49580
movb %dl, (%rax,%rcx)
movb 0x6f(%rsp), %al
addb $0x1, %al
movb %al, 0x6f(%rsp)
jmp 0x22c68
leaq 0x20(%rsp), %rdi
leaq 0xa17c(%rip), %rsi      # 0x2ce20
movl $0x40, %edx
callq 0x4330
movb $0x0, 0x1f(%rsp)
movzbl 0x1f(%rsp), %eax
cmpl $0x4, %eax
jge 0x22d1e
movb $0x0, 0x1e(%rsp)
movzbl 0x1e(%rsp), %eax
cmpl $0x10, %eax
jge 0x22d10
movzbl 0x1f(%rsp), %eax
movl %eax, %ecx
leaq 0x20(%rsp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movzbl 0x1e(%rsp), %ecx
movb (%rax,%rcx), %dl
movzbl 0x1f(%rsp), %eax
movl %eax, %ecx
leaq 0x268ab(%rip), %rax     # 0x495a0
shlq $0x4, %rcx
addq %rcx, %rax
movzbl 0x1e(%rsp), %ecx
movb %dl, (%rax,%rcx)
movb 0x1e(%rsp), %al
addb $0x1, %al
movb %al, 0x1e(%rsp)
jmp 0x22cc2
jmp 0x22d12
movb 0x1f(%rsp), %al
addb $0x1, %al
movb %al, 0x1f(%rsp)
jmp 0x22cb3
movl 0xa140(%rip), %eax      # 0x2ce64
movl %eax, 0x1a(%rsp)
movw 0x1a(%rsp), %ax
movw %ax, 0x268ac(%rip)      # 0x495e0
movw 0x1c(%rsp), %ax
movw %ax, 0x268a2(%rip)      # 0x495e2
addq $0x138, %rsp            # imm = 0x138
retq
nopl (%rax,%rax)