annotate Plugins/Input/console/Nsf_Emu.cpp @ 439:3c43e25ddc7d trunk

[svn] Add `Apply' button to the presets window. Patch via NhJm.
author nenolod
date Sun, 15 Jan 2006 00:34:41 -0800
parents 84aabc053b6e
children 0b9507985f0d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
90
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
1
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
2 // Game_Music_Emu 0.2.4. http://www.slack.net/~ant/libs/
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
3
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
4 #include "Nsf_Emu.h"
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
5
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
6 #include <string.h>
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
7 #include <stdio.h>
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
8
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
9 #include "blargg_endian.h"
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
10
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
11 /* Copyright (C) 2003-2005 Shay Green. This module is free software; you
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
12 can redistribute it and/or modify it under the terms of the GNU Lesser
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
13 General Public License as published by the Free Software Foundation; either
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
14 version 2.1 of the License, or (at your option) any later version. This
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
15 module is distributed in the hope that it will be useful, but WITHOUT ANY
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
17 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
18 more details. You should have received a copy of the GNU Lesser General
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
19 Public License along with this module; if not, write to the Free Software
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
21
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
22 #include BLARGG_SOURCE_BEGIN
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
23
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
24 const unsigned low_mem_size = 0x800;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
25 const unsigned page_size = 0x1000;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
26 const long ram_size = 0x10000;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
27 const nes_addr_t rom_begin = 0x8000;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
28 const nes_addr_t bank_select_addr = 0x5ff8;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
29 const nes_addr_t exram_addr = bank_select_addr - (bank_select_addr % Nes_Cpu::page_size);
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
30 const int master_clock_divisor = 12;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
31
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
32 const int vrc6_flag = 0x01;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
33 const int namco_flag = 0x10;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
34
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
35 // ROM
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
36
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
37 int Nsf_Emu::read_code( Nsf_Emu* emu, nes_addr_t addr )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
38 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
39 return *emu->cpu.get_code( addr );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
40 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
41
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
42 void Nsf_Emu::write_exram( Nsf_Emu* emu, nes_addr_t addr, int data )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
43 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
44 unsigned bank = addr - bank_select_addr;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
45 if ( bank < bank_count )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
46 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
47 if ( data < emu->total_banks ) {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
48 emu->cpu.map_code( (bank + 8) * page_size, page_size,
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
49 &emu->rom [data * page_size] );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
50 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
51 else {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
52 dprintf( "Bank %d out of range (%d banks total)\n",
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
53 data, (int) emu->total_banks );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
54 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
55 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
56 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
57
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
58 // APU
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
59
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
60 int Nsf_Emu::read_snd( Nsf_Emu* emu, nes_addr_t addr )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
61 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
62 if ( addr == Nes_Apu::status_addr )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
63 return emu->apu.read_status( emu->cpu.time() );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
64 return addr >> 8; // high byte of address stays on bus
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
65 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
66
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
67 void Nsf_Emu::write_snd( Nsf_Emu* emu, nes_addr_t addr, int data )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
68 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
69 if ( unsigned (addr - Nes_Apu::start_addr) <= Nes_Apu::end_addr - Nes_Apu::start_addr )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
70 emu->apu.write_register( emu->cpu.time(), addr, data );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
71 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
72
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
73 int Nsf_Emu::pcm_read( void* emu, nes_addr_t addr )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
74 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
75 return ((Nsf_Emu*) emu)->cpu.read( addr );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
76 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
77
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
78 // Low Mem
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
79
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
80 int Nsf_Emu::read_low_mem( Nsf_Emu* emu, nes_addr_t addr )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
81 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
82 return emu->cpu.low_mem [addr & (low_mem_size - 1)];
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
83 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
84
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
85 void Nsf_Emu::write_low_mem( Nsf_Emu* emu, nes_addr_t addr, int data )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
86 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
87 emu->cpu.low_mem [addr & (low_mem_size - 1)] = data;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
88 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
89
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
90 // SRAM
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
91
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
92 int Nsf_Emu::read_sram( Nsf_Emu* emu, nes_addr_t addr )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
93 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
94 return emu->sram [addr & (sram_size - 1)];
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
95 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
96
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
97 void Nsf_Emu::write_sram( Nsf_Emu* emu, nes_addr_t addr, int data )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
98 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
99 emu->sram [addr & (sram_size - 1)] = data;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
100 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
101
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
102 #if !NSF_EMU_APU_ONLY
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
103
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
104 // Namco
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
105 int Nsf_Emu::read_namco( Nsf_Emu* emu, nes_addr_t addr )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
106 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
107 if ( addr == Nes_Namco::data_reg_addr )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
108 return emu->namco.read_data();
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
109 return addr >> 8;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
110 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
111
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
112 void Nsf_Emu::write_namco( Nsf_Emu* emu, nes_addr_t addr, int data )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
113 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
114 if ( addr == Nes_Namco::data_reg_addr )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
115 emu->namco.write_data( emu->cpu.time(), data );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
116 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
117
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
118 void Nsf_Emu::write_namco_addr( Nsf_Emu* emu, nes_addr_t addr, int data )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
119 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
120 if ( addr == Nes_Namco::addr_reg_addr )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
121 emu->namco.write_addr( data );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
122 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
123
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
124 // VRC6
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
125 void Nsf_Emu::write_vrc6( Nsf_Emu* emu, nes_addr_t addr, int data )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
126 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
127 unsigned reg = addr & (Nes_Vrc6::addr_step - 1);
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
128 unsigned osc = unsigned (addr - Nes_Vrc6::base_addr) / Nes_Vrc6::addr_step;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
129 if ( osc < Nes_Vrc6::osc_count && reg < Nes_Vrc6::reg_count )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
130 emu->vrc6.write_osc( emu->cpu.time(), osc, reg, data );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
131 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
132
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
133 #endif
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
134
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
135 // Unmapped
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
136 int Nsf_Emu::read_unmapped( Nsf_Emu*, nes_addr_t addr )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
137 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
138 dprintf( "Read unmapped $%.4X\n", (unsigned) addr );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
139 return addr >> 8; // high byte of address stays on bus
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
140 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
141
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
142 void Nsf_Emu::write_unmapped( Nsf_Emu*, nes_addr_t addr, int )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
143 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
144 if (// some games write to $8000 and $8001 repeatedly
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
145 addr != 0x8000 && addr != 0x8001 &&
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
146
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
147 // probably namco sound mistakenly turned on in mck
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
148 addr != 0x4800 && addr != 0xF800 &&
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
149
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
150 // memory mapper?
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
151 addr != 0xFFF8 )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
152 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
153 dprintf( "Write unmapped $%.4X\n", (unsigned) addr );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
154 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
155 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
156
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
157 static BOOST::uint8_t unmapped_code [Nes_Cpu::page_size];
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
158
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
159 Nsf_Emu::Nsf_Emu( double gain_ )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
160 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
161 rom = NULL;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
162 play_addr = 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
163 clocks_per_msec = 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
164 gain = gain_;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
165 cpu.callback_data = this;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
166 set_equalizer( equalizer_t( -8.87, 8800, 110 ) );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
167 apu.dmc_reader( pcm_read, this );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
168 // set unmapped code to illegal instruction
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
169 memset( unmapped_code, 0x32, sizeof unmapped_code );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
170 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
171
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
172 Nsf_Emu::~Nsf_Emu()
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
173 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
174 unload();
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
175 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
176
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
177 void Nsf_Emu::unload()
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
178 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
179 delete [] rom;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
180 rom = NULL;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
181 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
182
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
183 const char** Nsf_Emu::voice_names() const
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
184 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
185 static const char* base_names [] = {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
186 "Square 1", "Square 2", "Triangle", "Noise", "DMC"
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
187 };
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
188 static const char* namco_names [] = {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
189 "Square 1", "Square 2", "Triangle", "Noise", "DMC",
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
190 "Namco 5&7", "Namco 4&6", "Namco 1-3"
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
191 };
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
192 static const char* vrc6_names [] = {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
193 "Square 1", "Square 2", "Triangle", "Noise", "DMC",
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
194 "VRC6 Square 1", "VRC6 Square 2", "VRC6 Saw"
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
195 };
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
196 if ( exp_flags & namco_flag )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
197 return namco_names;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
198 if ( exp_flags & vrc6_flag )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
199 return vrc6_names;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
200 return base_names;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
201 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
202
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
203 blargg_err_t Nsf_Emu::init_sound()
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
204 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
205 if ( exp_flags & ~(namco_flag | vrc6_flag) )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
206 return "NSF requires unsupported expansion audio hardware";
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
207
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
208 // map memory
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
209 cpu.reset( unmapped_code );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
210 cpu.map_memory( 0, ram_size, read_unmapped, write_unmapped ); // unmapped
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
211 cpu.map_memory( 0, low_mem_size, read_low_mem, write_low_mem ); // low mem
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
212 cpu.map_code( 0, low_mem_size, cpu.low_mem );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
213 cpu.map_memory( 0x4000, Nes_Cpu::page_size, read_snd, write_snd ); // apu
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
214 cpu.map_memory( exram_addr, Nes_Cpu::page_size, read_unmapped, write_exram ); // exram
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
215 cpu.map_memory( 0x6000, sram_size, read_sram, write_sram ); // sram
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
216 cpu.map_code ( 0x6000, sram_size, sram );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
217 cpu.map_memory( rom_begin, ram_size - rom_begin, read_code, write_unmapped ); // rom
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
218
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
219 voice_count_ = Nes_Apu::osc_count;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
220
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
221 double adjusted_gain = gain;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
222
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
223 #if NSF_EMU_APU_ONLY
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
224 if ( exp_flags )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
225 return "NSF requires expansion audio hardware";
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
226 #else
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
227 // namco
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
228 if ( exp_flags & namco_flag ) {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
229 adjusted_gain *= 0.75;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
230 voice_count_ += 3;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
231 cpu.map_memory( Nes_Namco::data_reg_addr, Nes_Cpu::page_size,
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
232 read_namco, write_namco );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
233 cpu.map_memory( Nes_Namco::addr_reg_addr, Nes_Cpu::page_size,
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
234 read_code, write_namco_addr );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
235 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
236
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
237 // vrc6
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
238 if ( exp_flags & vrc6_flag ) {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
239 adjusted_gain *= 0.75;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
240 voice_count_ += 3;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
241 for ( int i = 0; i < Nes_Vrc6::osc_count; i++ )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
242 cpu.map_memory( Nes_Vrc6::base_addr + i * Nes_Vrc6::addr_step,
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
243 Nes_Cpu::page_size, read_code, write_vrc6 );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
244 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
245
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
246 namco.volume( adjusted_gain );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
247 vrc6.volume( adjusted_gain );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
248 #endif
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
249
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
250 apu.volume( adjusted_gain );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
251
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
252 return blargg_success;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
253 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
254
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
255 void Nsf_Emu::update_eq( blip_eq_t const& eq )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
256 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
257 #if !NSF_EMU_APU_ONLY
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
258 vrc6.treble_eq( eq );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
259 namco.treble_eq( eq );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
260 #endif
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
261 apu.treble_eq( eq );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
262 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
263
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
264 blargg_err_t Nsf_Emu::load( const header_t& h, Emu_Reader& in )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
265 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
266 unload();
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
267
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
268 // check compatibility
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
269 if ( 0 != memcmp( h.tag, "NESM\x1A", 5 ) )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
270 return "Not an NSF file";
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
271 if ( h.vers != 1 )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
272 return "Unsupported NSF format";
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
273
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
274 // sound and memory
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
275 exp_flags = h.chip_flags;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
276 blargg_err_t err = init_sound();
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
277 if ( err )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
278 return err;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
279
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
280 // set up data
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
281 nes_addr_t load_addr = get_le16( h.load_addr );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
282 init_addr = get_le16( h.init_addr );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
283 play_addr = get_le16( h.play_addr );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
284 if ( !load_addr ) load_addr = rom_begin;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
285 if ( !init_addr ) init_addr = rom_begin;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
286 if ( !play_addr ) play_addr = rom_begin;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
287 if ( load_addr < rom_begin || init_addr < rom_begin )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
288 return "Invalid address in NSF";
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
289
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
290 // set up rom
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
291 total_banks = (in.remain() + load_addr % page_size + page_size - 1) / page_size;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
292 long rom_size = total_banks * page_size;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
293 rom = new byte [rom_size];
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
294 if ( !rom )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
295 return "Out of memory";
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
296 memset( rom, 0, rom_size );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
297 err = in.read( &rom [load_addr % page_size], in.remain() );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
298 if ( err ) {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
299 unload();
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
300 return err;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
301 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
302
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
303 // bank switching
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
304 int first_bank = (load_addr - rom_begin) / page_size;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
305 for ( int i = 0; i < bank_count; i++ )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
306 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
307 unsigned bank = i - first_bank;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
308 initial_banks [i] = (bank < total_banks) ? bank : 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
309
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
310 if ( h.banks [i] ) {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
311 // bank-switched
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
312 memcpy( initial_banks, h.banks, sizeof initial_banks );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
313 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
314 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
315 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
316
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
317 // playback rate
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
318 unsigned playback_rate = get_le16( h.ntsc_speed );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
319 unsigned standard_rate = 0x411A;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
320 double clock_rate = 1789772.72727;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
321 play_period = 262 * 341L * 4 + 2;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
322 pal_only = false;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
323
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
324 // use pal speed if there is no ntsc speed
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
325 if ( (h.speed_flags & 3) == 1 ) {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
326 pal_only = true;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
327 play_period = 33247 * master_clock_divisor;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
328 clock_rate = 1662607.125;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
329 standard_rate = 0x4E20;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
330 playback_rate = get_le16( h.pal_speed );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
331 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
332
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
333 clocks_per_msec = clock_rate * (1.0 / 1000.0);
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
334
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
335 // use custom playback rate if not the standard rate
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
336 if ( playback_rate && playback_rate != standard_rate )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
337 play_period = long (clock_rate * playback_rate * master_clock_divisor /
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
338 1000000.0);
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
339
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
340 // extra flags
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
341 int extra_flags = h.speed_flags;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
342 #if !NSF_EMU_EXTRA_FLAGS
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
343 extra_flags = 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
344 #endif
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
345 needs_long_frames = (extra_flags & 0x10) != 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
346 initial_pcm_dac = (extra_flags & 0x20) ? 0x3F : 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
347
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
348 track_count_ = h.track_count;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
349
256
84aabc053b6e [svn] C++ warning fixes by external contractor Stephen Bennett.
chainsaw
parents: 90
diff changeset
350 return setup_buffer( ( int) (clock_rate + 0.5) );
90
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
351 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
352
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
353 void Nsf_Emu::set_voice( int i, Blip_Buffer* buf, Blip_Buffer*, Blip_Buffer* )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
354 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
355 #if !NSF_EMU_APU_ONLY
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
356 if ( i >= Nes_Apu::osc_count ) {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
357 vrc6.osc_output( i - Nes_Apu::osc_count, buf );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
358 if ( i < 7 ) {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
359 i &= 1;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
360 namco.osc_output( i + 4, buf );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
361 namco.osc_output( i + 6, buf );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
362 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
363 else {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
364 for ( int n = 0; n < namco.osc_count / 2; n++ )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
365 namco.osc_output( n, buf );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
366 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
367 return;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
368 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
369 #endif
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
370 apu.osc_output( i, buf );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
371 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
372
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
373 blargg_err_t Nsf_Emu::start_track( int track )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
374 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
375 require( rom ); // file must be loaded
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
376
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
377 starting_track();
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
378
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
379 // clear memory
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
380 memset( cpu.low_mem, 0, sizeof cpu.low_mem );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
381 memset( sram, 0, sizeof sram );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
382
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
383 // initial rom banks
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
384 for ( int i = 0; i < bank_count; ++i )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
385 cpu.write( bank_select_addr + i, initial_banks [i] );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
386
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
387 // reset sound
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
388 apu.reset( pal_only, initial_pcm_dac );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
389 apu.write_register( 0, 0x4015, 0x0F );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
390 apu.write_register( 0, 0x4017, needs_long_frames ? 0x80 : 0 );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
391
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
392 #if !NSF_EMU_APU_ONLY
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
393 if ( exp_flags ) {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
394 namco.reset();
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
395 vrc6.reset();
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
396 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
397 #endif
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
398
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
399 // reset cpu
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
400 cpu.r.pc = exram_addr;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
401 cpu.r.a = track;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
402 cpu.r.x = pal_only;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
403 cpu.r.y = 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
404 cpu.r.sp = 0xFF;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
405 cpu.r.status = 0x04; // i flag
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
406
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
407 // first call
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
408 cpu_jsr( init_addr, -1 );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
409 next_play = 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
410 play_extra = 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
411
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
412 return blargg_success;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
413 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
414
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
415 void Nsf_Emu::cpu_jsr( nes_addr_t pc, int adj )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
416 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
417 unsigned ret_addr = cpu.r.pc + adj;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
418 cpu.r.pc = pc;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
419 cpu.low_mem [cpu.r.sp-- + 0x100] = ret_addr >> 8;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
420 cpu.low_mem [cpu.r.sp-- + 0x100] = ret_addr;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
421 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
422
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
423 blip_time_t Nsf_Emu::run( int msec, bool* )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
424 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
425 // run cpu
256
84aabc053b6e [svn] C++ warning fixes by external contractor Stephen Bennett.
chainsaw
parents: 90
diff changeset
426 blip_time_t duration = (blip_time_t) (clocks_per_msec * msec);
90
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
427 cpu.time( 0 );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
428 while ( cpu.time() < duration )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
429 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
430 // check for idle cpu
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
431 if ( cpu.r.pc == exram_addr )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
432 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
433 if ( next_play > duration ) {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
434 cpu.time( duration );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
435 break;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
436 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
437
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
438 if ( next_play > cpu.time() )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
439 cpu.time( next_play );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
440
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
441 nes_time_t period = (play_period + play_extra) / master_clock_divisor;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
442 play_extra = play_period - period * master_clock_divisor;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
443 next_play += period;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
444 cpu_jsr( play_addr, -1 );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
445 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
446
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
447 Nes_Cpu::result_t result = cpu.run( duration );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
448 if ( result == Nes_Cpu::result_badop && cpu.r.pc != exram_addr )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
449 {
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
450 dprintf( "Bad opcode $%.2x at $%.4x\n",
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
451 (int) cpu.read( cpu.r.pc ), (int) cpu.r.pc );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
452
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
453 return 0; // error
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
454 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
455 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
456
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
457 // end time frame
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
458 duration = cpu.time();
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
459 next_play -= duration;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
460 if ( next_play < 0 ) // could go negative if routine is taking too long to return
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
461 next_play = 0;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
462 apu.end_frame( duration );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
463
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
464 #if !NSF_EMU_APU_ONLY
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
465 if ( exp_flags & namco_flag )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
466 namco.end_frame( duration );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
467 if ( exp_flags & vrc6_flag )
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
468 vrc6.end_frame( duration );
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
469 #endif
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
470
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
471 return duration;
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
472 }
252843aac42f [svn] Import the initial sources for console music support.
nenolod
parents:
diff changeset
473