From 4e752fceb85b011115c17ed34640df374d0d1b4f Mon Sep 17 00:00:00 2001 From: dborth Date: Tue, 6 Apr 2010 02:13:53 +0000 Subject: [PATCH] upstream changes. change sound defaults --- source/preferences.cpp | 3 +- source/snes9x/apu/apu.cpp | 45 +- source/snes9x/apu/hermite_resampler.h | 144 ++++ source/snes9x/apu/license.txt | 1008 ++++++++++++------------- source/snes9x/apu/linear_resampler.h | 115 +++ source/snes9x/apu/resampler.h | 144 +--- source/snes9x/cpu.cpp | 32 +- source/snes9x/cpuexec.cpp | 60 +- source/snes9x/cpuops.cpp | 6 +- source/snes9x/dma.cpp | 110 ++- source/snes9x/getset.h | 335 ++++++-- source/snes9x/memmap.cpp | 70 +- source/snes9x/memmap.h | 3 - source/snes9x/ppu.cpp | 6 - source/snes9x/sa1.cpp | 30 +- source/snes9x/screenshot.cpp | 6 +- source/snes9x/snapshot.cpp | 1 - source/snes9x/snes9x.h | 4 +- 18 files changed, 1253 insertions(+), 869 deletions(-) create mode 100644 source/snes9x/apu/hermite_resampler.h create mode 100644 source/snes9x/apu/linear_resampler.h diff --git a/source/preferences.cpp b/source/preferences.cpp index 25c3a6b..4f4e6ea 100644 --- a/source/preferences.cpp +++ b/source/preferences.cpp @@ -450,11 +450,12 @@ DefaultSettings () Settings.HDMATimingHack = 100; // Sound defaults. On Wii this is 32Khz/16bit/Stereo + Settings.SoundSync = true; Settings.SixteenBitSound = true; Settings.Stereo = true; Settings.ReverseStereo = true; Settings.SoundPlaybackRate = 32000; - Settings.SoundInputRate = 31950; + Settings.SoundInputRate = 32000; // Graphics Settings.Transparency = true; diff --git a/source/snes9x/apu/apu.cpp b/source/snes9x/apu/apu.cpp index e5143ff..5dbd931 100644 --- a/source/snes9x/apu/apu.cpp +++ b/source/snes9x/apu/apu.cpp @@ -180,15 +180,17 @@ #include "apu.h" #include "snapshot.h" #include "display.h" -#include "resampler.h" +#include "linear_resampler.h" +#include "hermite_resampler.h" #define APU_DEFAULT_INPUT_RATE 32000 #define APU_MINIMUM_SAMPLE_COUNT 512 #define APU_MINIMUM_SAMPLE_BLOCK 128 -#define APU_NUMERATOR_NTSC 5632 -#define APU_DENOMINATOR_NTSC 118125 -#define APU_NUMERATOR_PAL 102400 -#define APU_DENOMINATOR_PAL 2128137 +#define APU_NUMERATOR_NTSC 15664 +#define APU_DENOMINATOR_NTSC 328125 +#define APU_NUMERATOR_PAL 34176 +#define APU_DENOMINATOR_PAL 709379 +#define APU_DEFAULT_RESAMPLER HermiteResampler SNES_SPC *spc_core = NULL; @@ -224,8 +226,12 @@ namespace spc static int32 reference_time; static uint32 remainder; - static const int32 timing_hack_numerator = SNES_SPC::tempo_unit; - static int32 timing_hack_denominator = SNES_SPC::tempo_unit; + static const int timing_hack_numerator = SNES_SPC::tempo_unit; + static int timing_hack_denominator = SNES_SPC::tempo_unit; + /* Set these to NTSC for now. Will change to PAL in S9xAPUTimingSetSpeedup + if necessary on game load. */ + static uint32 ratio_numerator = APU_NUMERATOR_NTSC; + static uint32 ratio_denominator = APU_DENOMINATOR_NTSC; } static void EightBitize (uint8 *, int); @@ -445,7 +451,7 @@ bool8 S9xInitSound (int buffer_ms, int lag_ms) arguments. Use 2x in the resampler for buffer leveling with SoundSync */ if (!spc::resampler) { - spc::resampler = new Resampler(spc::buffer_size >> (Settings.SoundSync ? 0 : 1)); + spc::resampler = new APU_DEFAULT_RESAMPLER(spc::buffer_size >> (Settings.SoundSync ? 0 : 1)); if (!spc::resampler) { delete[] spc::landing_buffer; @@ -534,22 +540,14 @@ void S9xDeinitAPU (void) static inline int S9xAPUGetClock (int32 cpucycles) { - if (Settings.PAL) - return ((int) floor(((double) APU_NUMERATOR_PAL * spc::timing_hack_numerator * (cpucycles - spc::reference_time) + spc::remainder) / - ((double) APU_DENOMINATOR_PAL * spc::timing_hack_denominator))); - else - return (APU_NUMERATOR_NTSC * spc::timing_hack_numerator * (cpucycles - spc::reference_time) + spc::remainder) / - (APU_DENOMINATOR_NTSC * spc::timing_hack_denominator); + return (spc::ratio_numerator * (cpucycles - spc::reference_time) + spc::remainder) / + spc::ratio_denominator; } static inline int S9xAPUGetClockRemainder (int32 cpucycles) { - if (Settings.PAL) - return ((int) fmod (((double) APU_NUMERATOR_PAL * spc::timing_hack_numerator * (cpucycles - spc::reference_time) + spc::remainder), - ((double) APU_DENOMINATOR_PAL * spc::timing_hack_denominator))); - else - return (APU_NUMERATOR_NTSC * spc::timing_hack_numerator * (cpucycles - spc::reference_time) + spc::remainder) % - (APU_DENOMINATOR_NTSC * spc::timing_hack_denominator); + return (spc::ratio_numerator * (cpucycles - spc::reference_time) + spc::remainder) % + spc::ratio_denominator; } uint8 S9xAPUReadPort (int port) @@ -590,9 +588,12 @@ void S9xAPUTimingSetSpeedup (int ticks) if (ticks != 0) printf("APU speedup hack: %d\n", ticks); - spc_core->set_tempo(SNES_SPC::tempo_unit - ticks); - spc::timing_hack_denominator = SNES_SPC::tempo_unit - ticks; + spc_core->set_tempo(spc::timing_hack_denominator); + + spc::ratio_numerator = Settings.PAL ? APU_NUMERATOR_PAL : APU_NUMERATOR_NTSC; + spc::ratio_denominator = Settings.PAL ? APU_DENOMINATOR_PAL : APU_DENOMINATOR_NTSC; + spc::ratio_denominator = spc::ratio_denominator * spc::timing_hack_denominator / spc::timing_hack_numerator; UpdatePlaybackRate(); } diff --git a/source/snes9x/apu/hermite_resampler.h b/source/snes9x/apu/hermite_resampler.h new file mode 100644 index 0000000..30ee103 --- /dev/null +++ b/source/snes9x/apu/hermite_resampler.h @@ -0,0 +1,144 @@ +/* Simple resampler based on bsnes's ruby audio library */ + +#ifndef __HERMITE_RESAMPLER_H +#define __HERMITE_RESAMPLER_H + +#include "resampler.h" + +#undef CLAMP +#undef SHORT_CLAMP +#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) +#define SHORT_CLAMP(n) ((short) CLAMP((n), -32768, 32767)) + +class HermiteResampler : public Resampler +{ + protected: + + double r_step; + double r_frac; + int r_left[4], r_right[4]; + + double + hermite (double mu1, double a, double b, double c, double d) + { + const double tension = 0.0; //-1 = low, 0 = normal, 1 = high + const double bias = 0.0; //-1 = left, 0 = even, 1 = right + + double mu2, mu3, m0, m1, a0, a1, a2, a3; + + mu2 = mu1 * mu1; + mu3 = mu2 * mu1; + + m0 = (b - a) * (1 + bias) * (1 - tension) / 2; + m0 += (c - b) * (1 - bias) * (1 - tension) / 2; + m1 = (c - b) * (1 + bias) * (1 - tension) / 2; + m1 += (d - c) * (1 - bias) * (1 - tension) / 2; + + a0 = +2 * mu3 - 3 * mu2 + 1; + a1 = mu3 - 2 * mu2 + mu1; + a2 = mu3 - mu2; + a3 = -2 * mu3 + 3 * mu2; + + return (a0 * b) + (a1 * m0) + (a2 * m1) + (a3 * c); + } + + public: + HermiteResampler (int num_samples) : Resampler (num_samples) + { + clear (); + } + + ~HermiteResampler () + { + } + + void + time_ratio (double ratio) + { + r_step = ratio; + clear (); + } + + void + clear (void) + { + ring_buffer::clear (); + r_frac = 1.0; + r_left [0] = r_left [1] = r_left [2] = r_left [3] = 0; + r_right[0] = r_right[1] = r_right[2] = r_right[3] = 0; + } + + void + read (short *data, int num_samples) + { + int i_position = start >> 1; + short *internal_buffer = (short *) buffer; + int o_position = 0; + int consumed = 0; + + while (o_position < num_samples && consumed < buffer_size) + { + int s_left = internal_buffer[i_position]; + int s_right = internal_buffer[i_position + 1]; + int max_samples = buffer_size >> 1; + const double margin_of_error = 1.0e-10; + + if (fabs(r_step - 1.0) < margin_of_error) + { + data[o_position] = (short) s_left; + data[o_position + 1] = (short) s_right; + + o_position += 2; + i_position += 2; + if (i_position >= max_samples) + i_position -= max_samples; + consumed += 2; + + continue; + } + + while (r_frac <= 1.0 && o_position < num_samples) + { + data[o_position] = SHORT_CLAMP (hermite (r_frac, r_left [0], r_left [1], r_left [2], r_left [3])); + data[o_position + 1] = SHORT_CLAMP (hermite (r_frac, r_right[0], r_right[1], r_right[2], r_right[3])); + + o_position += 2; + + r_frac += r_step; + } + + if (r_frac > 1.0) + { + r_left [0] = r_left [1]; + r_left [1] = r_left [2]; + r_left [2] = r_left [3]; + r_left [3] = s_left; + + r_right[0] = r_right[1]; + r_right[1] = r_right[2]; + r_right[2] = r_right[3]; + r_right[3] = s_right; + + r_frac -= 1.0; + + i_position += 2; + if (i_position >= max_samples) + i_position -= max_samples; + consumed += 2; + } + } + + size -= consumed << 1; + start += consumed << 1; + if (start >= buffer_size) + start -= buffer_size; + } + + inline int + avail (void) + { + return (int) floor (((size >> 2) - r_frac) / r_step) * 2; + } +}; + +#endif /* __HERMITE_RESAMPLER_H */ diff --git a/source/snes9x/apu/license.txt b/source/snes9x/apu/license.txt index 5ab7695..5faba9d 100644 --- a/source/snes9x/apu/license.txt +++ b/source/snes9x/apu/license.txt @@ -1,504 +1,504 @@ - GNU LESSER GENERAL PUBLIC LICENSE - Version 2.1, February 1999 - - Copyright (C) 1991, 1999 Free Software Foundation, Inc. - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - -[This is the first released version of the Lesser GPL. It also counts - as the successor of the GNU Library Public License, version 2, hence - the version number 2.1.] - - Preamble - - The licenses for most software are designed to take away your -freedom to share and change it. By contrast, the GNU General Public -Licenses are intended to guarantee your freedom to share and change -free software--to make sure the software is free for all its users. - - This license, the Lesser General Public License, applies to some -specially designated software packages--typically libraries--of the -Free Software Foundation and other authors who decide to use it. You -can use it too, but we suggest you first think carefully about whether -this license or the ordinary General Public License is the better -strategy to use in any particular case, based on the explanations below. - - When we speak of free software, we are referring to freedom of use, -not price. Our General Public Licenses are designed to make sure that -you have the freedom to distribute copies of free software (and charge -for this service if you wish); that you receive source code or can get -it if you want it; that you can change the software and use pieces of -it in new free programs; and that you are informed that you can do -these things. - - To protect your rights, we need to make restrictions that forbid -distributors to deny you these rights or to ask you to surrender these -rights. These restrictions translate to certain responsibilities for -you if you distribute copies of the library or if you modify it. - - For example, if you distribute copies of the library, whether gratis -or for a fee, you must give the recipients all the rights that we gave -you. You must make sure that they, too, receive or can get the source -code. If you link other code with the library, you must provide -complete object files to the recipients, so that they can relink them -with the library after making changes to the library and recompiling -it. And you must show them these terms so they know their rights. - - We protect your rights with a two-step method: (1) we copyright the -library, and (2) we offer you this license, which gives you legal -permission to copy, distribute and/or modify the library. - - To protect each distributor, we want to make it very clear that -there is no warranty for the free library. Also, if the library is -modified by someone else and passed on, the recipients should know -that what they have is not the original version, so that the original -author's reputation will not be affected by problems that might be -introduced by others. - - Finally, software patents pose a constant threat to the existence of -any free program. We wish to make sure that a company cannot -effectively restrict the users of a free program by obtaining a -restrictive license from a patent holder. Therefore, we insist that -any patent license obtained for a version of the library must be -consistent with the full freedom of use specified in this license. - - Most GNU software, including some libraries, is covered by the -ordinary GNU General Public License. This license, the GNU Lesser -General Public License, applies to certain designated libraries, and -is quite different from the ordinary General Public License. We use -this license for certain libraries in order to permit linking those -libraries into non-free programs. - - When a program is linked with a library, whether statically or using -a shared library, the combination of the two is legally speaking a -combined work, a derivative of the original library. The ordinary -General Public License therefore permits such linking only if the -entire combination fits its criteria of freedom. The Lesser General -Public License permits more lax criteria for linking other code with -the library. - - We call this license the "Lesser" General Public License because it -does Less to protect the user's freedom than the ordinary General -Public License. It also provides other free software developers Less -of an advantage over competing non-free programs. These disadvantages -are the reason we use the ordinary General Public License for many -libraries. However, the Lesser license provides advantages in certain -special circumstances. - - For example, on rare occasions, there may be a special need to -encourage the widest possible use of a certain library, so that it becomes -a de-facto standard. To achieve this, non-free programs must be -allowed to use the library. A more frequent case is that a free -library does the same job as widely used non-free libraries. In this -case, there is little to gain by limiting the free library to free -software only, so we use the Lesser General Public License. - - In other cases, permission to use a particular library in non-free -programs enables a greater number of people to use a large body of -free software. For example, permission to use the GNU C Library in -non-free programs enables many more people to use the whole GNU -operating system, as well as its variant, the GNU/Linux operating -system. - - Although the Lesser General Public License is Less protective of the -users' freedom, it does ensure that the user of a program that is -linked with the Library has the freedom and the wherewithal to run -that program using a modified version of the Library. - - The precise terms and conditions for copying, distribution and -modification follow. Pay close attention to the difference between a -"work based on the library" and a "work that uses the library". The -former contains code derived from the library, whereas the latter must -be combined with the library in order to run. - - GNU LESSER GENERAL PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. This License Agreement applies to any software library or other -program which contains a notice placed by the copyright holder or -other authorized party saying it may be distributed under the terms of -this Lesser General Public License (also called "this License"). -Each licensee is addressed as "you". - - A "library" means a collection of software functions and/or data -prepared so as to be conveniently linked with application programs -(which use some of those functions and data) to form executables. - - The "Library", below, refers to any such software library or work -which has been distributed under these terms. A "work based on the -Library" means either the Library or any derivative work under -copyright law: that is to say, a work containing the Library or a -portion of it, either verbatim or with modifications and/or translated -straightforwardly into another language. (Hereinafter, translation is -included without limitation in the term "modification".) - - "Source code" for a work means the preferred form of the work for -making modifications to it. For a library, complete source code means -all the source code for all modules it contains, plus any associated -interface definition files, plus the scripts used to control compilation -and installation of the library. - - Activities other than copying, distribution and modification are not -covered by this License; they are outside its scope. The act of -running a program using the Library is not restricted, and output from -such a program is covered only if its contents constitute a work based -on the Library (independent of the use of the Library in a tool for -writing it). Whether that is true depends on what the Library does -and what the program that uses the Library does. - - 1. You may copy and distribute verbatim copies of the Library's -complete source code as you receive it, in any medium, provided that -you conspicuously and appropriately publish on each copy an -appropriate copyright notice and disclaimer of warranty; keep intact -all the notices that refer to this License and to the absence of any -warranty; and distribute a copy of this License along with the -Library. - - You may charge a fee for the physical act of transferring a copy, -and you may at your option offer warranty protection in exchange for a -fee. - - 2. You may modify your copy or copies of the Library or any portion -of it, thus forming a work based on the Library, and copy and -distribute such modifications or work under the terms of Section 1 -above, provided that you also meet all of these conditions: - - a) The modified work must itself be a software library. - - b) You must cause the files modified to carry prominent notices - stating that you changed the files and the date of any change. - - c) You must cause the whole of the work to be licensed at no - charge to all third parties under the terms of this License. - - d) If a facility in the modified Library refers to a function or a - table of data to be supplied by an application program that uses - the facility, other than as an argument passed when the facility - is invoked, then you must make a good faith effort to ensure that, - in the event an application does not supply such function or - table, the facility still operates, and performs whatever part of - its purpose remains meaningful. - - (For example, a function in a library to compute square roots has - a purpose that is entirely well-defined independent of the - application. Therefore, Subsection 2d requires that any - application-supplied function or table used by this function must - be optional: if the application does not supply it, the square - root function must still compute square roots.) - -These requirements apply to the modified work as a whole. If -identifiable sections of that work are not derived from the Library, -and can be reasonably considered independent and separate works in -themselves, then this License, and its terms, do not apply to those -sections when you distribute them as separate works. But when you -distribute the same sections as part of a whole which is a work based -on the Library, the distribution of the whole must be on the terms of -this License, whose permissions for other licensees extend to the -entire whole, and thus to each and every part regardless of who wrote -it. - -Thus, it is not the intent of this section to claim rights or contest -your rights to work written entirely by you; rather, the intent is to -exercise the right to control the distribution of derivative or -collective works based on the Library. - -In addition, mere aggregation of another work not based on the Library -with the Library (or with a work based on the Library) on a volume of -a storage or distribution medium does not bring the other work under -the scope of this License. - - 3. You may opt to apply the terms of the ordinary GNU General Public -License instead of this License to a given copy of the Library. To do -this, you must alter all the notices that refer to this License, so -that they refer to the ordinary GNU General Public License, version 2, -instead of to this License. (If a newer version than version 2 of the -ordinary GNU General Public License has appeared, then you can specify -that version instead if you wish.) Do not make any other change in -these notices. - - Once this change is made in a given copy, it is irreversible for -that copy, so the ordinary GNU General Public License applies to all -subsequent copies and derivative works made from that copy. - - This option is useful when you wish to copy part of the code of -the Library into a program that is not a library. - - 4. You may copy and distribute the Library (or a portion or -derivative of it, under Section 2) in object code or executable form -under the terms of Sections 1 and 2 above provided that you accompany -it with the complete corresponding machine-readable source code, which -must be distributed under the terms of Sections 1 and 2 above on a -medium customarily used for software interchange. - - If distribution of object code is made by offering access to copy -from a designated place, then offering equivalent access to copy the -source code from the same place satisfies the requirement to -distribute the source code, even though third parties are not -compelled to copy the source along with the object code. - - 5. A program that contains no derivative of any portion of the -Library, but is designed to work with the Library by being compiled or -linked with it, is called a "work that uses the Library". Such a -work, in isolation, is not a derivative work of the Library, and -therefore falls outside the scope of this License. - - However, linking a "work that uses the Library" with the Library -creates an executable that is a derivative of the Library (because it -contains portions of the Library), rather than a "work that uses the -library". The executable is therefore covered by this License. -Section 6 states terms for distribution of such executables. - - When a "work that uses the Library" uses material from a header file -that is part of the Library, the object code for the work may be a -derivative work of the Library even though the source code is not. -Whether this is true is especially significant if the work can be -linked without the Library, or if the work is itself a library. The -threshold for this to be true is not precisely defined by law. - - If such an object file uses only numerical parameters, data -structure layouts and accessors, and small macros and small inline -functions (ten lines or less in length), then the use of the object -file is unrestricted, regardless of whether it is legally a derivative -work. (Executables containing this object code plus portions of the -Library will still fall under Section 6.) - - Otherwise, if the work is a derivative of the Library, you may -distribute the object code for the work under the terms of Section 6. -Any executables containing that work also fall under Section 6, -whether or not they are linked directly with the Library itself. - - 6. As an exception to the Sections above, you may also combine or -link a "work that uses the Library" with the Library to produce a -work containing portions of the Library, and distribute that work -under terms of your choice, provided that the terms permit -modification of the work for the customer's own use and reverse -engineering for debugging such modifications. - - You must give prominent notice with each copy of the work that the -Library is used in it and that the Library and its use are covered by -this License. You must supply a copy of this License. If the work -during execution displays copyright notices, you must include the -copyright notice for the Library among them, as well as a reference -directing the user to the copy of this License. Also, you must do one -of these things: - - a) Accompany the work with the complete corresponding - machine-readable source code for the Library including whatever - changes were used in the work (which must be distributed under - Sections 1 and 2 above); and, if the work is an executable linked - with the Library, with the complete machine-readable "work that - uses the Library", as object code and/or source code, so that the - user can modify the Library and then relink to produce a modified - executable containing the modified Library. (It is understood - that the user who changes the contents of definitions files in the - Library will not necessarily be able to recompile the application - to use the modified definitions.) - - b) Use a suitable shared library mechanism for linking with the - Library. A suitable mechanism is one that (1) uses at run time a - copy of the library already present on the user's computer system, - rather than copying library functions into the executable, and (2) - will operate properly with a modified version of the library, if - the user installs one, as long as the modified version is - interface-compatible with the version that the work was made with. - - c) Accompany the work with a written offer, valid for at - least three years, to give the same user the materials - specified in Subsection 6a, above, for a charge no more - than the cost of performing this distribution. - - d) If distribution of the work is made by offering access to copy - from a designated place, offer equivalent access to copy the above - specified materials from the same place. - - e) Verify that the user has already received a copy of these - materials or that you have already sent this user a copy. - - For an executable, the required form of the "work that uses the -Library" must include any data and utility programs needed for -reproducing the executable from it. However, as a special exception, -the materials to be distributed need not include anything that is -normally distributed (in either source or binary form) with the major -components (compiler, kernel, and so on) of the operating system on -which the executable runs, unless that component itself accompanies -the executable. - - It may happen that this requirement contradicts the license -restrictions of other proprietary libraries that do not normally -accompany the operating system. Such a contradiction means you cannot -use both them and the Library together in an executable that you -distribute. - - 7. You may place library facilities that are a work based on the -Library side-by-side in a single library together with other library -facilities not covered by this License, and distribute such a combined -library, provided that the separate distribution of the work based on -the Library and of the other library facilities is otherwise -permitted, and provided that you do these two things: - - a) Accompany the combined library with a copy of the same work - based on the Library, uncombined with any other library - facilities. This must be distributed under the terms of the - Sections above. - - b) Give prominent notice with the combined library of the fact - that part of it is a work based on the Library, and explaining - where to find the accompanying uncombined form of the same work. - - 8. You may not copy, modify, sublicense, link with, or distribute -the Library except as expressly provided under this License. Any -attempt otherwise to copy, modify, sublicense, link with, or -distribute the Library is void, and will automatically terminate your -rights under this License. However, parties who have received copies, -or rights, from you under this License will not have their licenses -terminated so long as such parties remain in full compliance. - - 9. You are not required to accept this License, since you have not -signed it. However, nothing else grants you permission to modify or -distribute the Library or its derivative works. These actions are -prohibited by law if you do not accept this License. Therefore, by -modifying or distributing the Library (or any work based on the -Library), you indicate your acceptance of this License to do so, and -all its terms and conditions for copying, distributing or modifying -the Library or works based on it. - - 10. Each time you redistribute the Library (or any work based on the -Library), the recipient automatically receives a license from the -original licensor to copy, distribute, link with or modify the Library -subject to these terms and conditions. You may not impose any further -restrictions on the recipients' exercise of the rights granted herein. -You are not responsible for enforcing compliance by third parties with -this License. - - 11. If, as a consequence of a court judgment or allegation of patent -infringement or for any other reason (not limited to patent issues), -conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot -distribute so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you -may not distribute the Library at all. For example, if a patent -license would not permit royalty-free redistribution of the Library by -all those who receive copies directly or indirectly through you, then -the only way you could satisfy both it and this License would be to -refrain entirely from distribution of the Library. - -If any portion of this section is held invalid or unenforceable under any -particular circumstance, the balance of the section is intended to apply, -and the section as a whole is intended to apply in other circumstances. - -It is not the purpose of this section to induce you to infringe any -patents or other property right claims or to contest validity of any -such claims; this section has the sole purpose of protecting the -integrity of the free software distribution system which is -implemented by public license practices. Many people have made -generous contributions to the wide range of software distributed -through that system in reliance on consistent application of that -system; it is up to the author/donor to decide if he or she is willing -to distribute software through any other system and a licensee cannot -impose that choice. - -This section is intended to make thoroughly clear what is believed to -be a consequence of the rest of this License. - - 12. If the distribution and/or use of the Library is restricted in -certain countries either by patents or by copyrighted interfaces, the -original copyright holder who places the Library under this License may add -an explicit geographical distribution limitation excluding those countries, -so that distribution is permitted only in or among countries not thus -excluded. In such case, this License incorporates the limitation as if -written in the body of this License. - - 13. The Free Software Foundation may publish revised and/or new -versions of the Lesser General Public License from time to time. -Such new versions will be similar in spirit to the present version, -but may differ in detail to address new problems or concerns. - -Each version is given a distinguishing version number. If the Library -specifies a version number of this License which applies to it and -"any later version", you have the option of following the terms and -conditions either of that version or of any later version published by -the Free Software Foundation. If the Library does not specify a -license version number, you may choose any version ever published by -the Free Software Foundation. - - 14. If you wish to incorporate parts of the Library into other free -programs whose distribution conditions are incompatible with these, -write to the author to ask for permission. For software which is -copyrighted by the Free Software Foundation, write to the Free -Software Foundation; we sometimes make exceptions for this. Our -decision will be guided by the two goals of preserving the free status -of all derivatives of our free software and of promoting the sharing -and reuse of software generally. - - NO WARRANTY - - 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO -WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. -EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR -OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY -KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE -LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME -THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN -WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY -AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU -FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR -CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE -LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING -RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A -FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF -SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH -DAMAGES. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Libraries - - If you develop a new library, and you want it to be of the greatest -possible use to the public, we recommend making it free software that -everyone can redistribute and change. You can do so by permitting -redistribution under these terms (or, alternatively, under the terms of the -ordinary General Public License). - - To apply these terms, attach the following notices to the library. It is -safest to attach them to the start of each source file to most effectively -convey the exclusion of warranty; and each file should have at least the -"copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - -Also add information on how to contact you by electronic and paper mail. - -You should also get your employer (if you work as a programmer) or your -school, if any, to sign a "copyright disclaimer" for the library, if -necessary. Here is a sample; alter the names: - - Yoyodyne, Inc., hereby disclaims all copyright interest in the - library `Frob' (a library for tweaking knobs) written by James Random Hacker. - - , 1 April 1990 - Ty Coon, President of Vice - -That's all there is to it! - - + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! + + diff --git a/source/snes9x/apu/linear_resampler.h b/source/snes9x/apu/linear_resampler.h new file mode 100644 index 0000000..35789db --- /dev/null +++ b/source/snes9x/apu/linear_resampler.h @@ -0,0 +1,115 @@ +/* Simple fixed-point linear resampler by BearOso*/ + +#ifndef __LINEAR_RESAMPLER_H +#define __LINEAR_RESAMPLER_H + +#include "resampler.h" +#include "snes9x.h" + +static const int f_prec = 15; +static const uint32 f__one = (1 << f_prec); + +#define lerp(t, a, b) (((((b) - (a)) * (t)) >> f_prec) + (a)) + +class LinearResampler : public Resampler +{ + protected: + uint32 f__r_step; + uint32 f__inv_r_step; + uint32 f__r_frac; + int r_left, r_right; + + public: + LinearResampler (int num_samples) : Resampler (num_samples) + { + f__r_frac = 0; + } + + ~LinearResampler () + { + } + + void + time_ratio (double ratio) + { + if (ratio == 0.0) + ratio = 1.0; + f__r_step = (uint32) (ratio * f__one); + f__inv_r_step = (uint32) (f__one / ratio); + clear (); + } + + void + clear (void) + { + ring_buffer::clear (); + f__r_frac = 0; + r_left = 0; + r_right = 0; + } + + void + read (short *data, int num_samples) + { + int i_position = start >> 1; + short *internal_buffer = (short *) buffer; + int o_position = 0; + int consumed = 0; + int max_samples = (buffer_size >> 1); + + while (o_position < num_samples && consumed < buffer_size) + { + if (f__r_step == f__one) + { + data[o_position] = internal_buffer[i_position]; + data[o_position + 1] = internal_buffer[i_position + 1]; + + o_position += 2; + i_position += 2; + if (i_position >= max_samples) + i_position -= max_samples; + consumed += 2; + + continue; + } + + while (f__r_frac <= f__one && o_position < num_samples) + { + data[o_position] = lerp (f__r_frac, + r_left, + internal_buffer[i_position]); + data[o_position + 1] = lerp (f__r_frac, + r_right, + internal_buffer[i_position + 1]); + + o_position += 2; + + f__r_frac += f__r_step; + } + + if (f__r_frac > f__one) + { + f__r_frac -= f__one; + r_left = internal_buffer[i_position]; + r_right = internal_buffer[i_position + 1]; + i_position += 2; + if (i_position >= max_samples) + i_position -= max_samples; + consumed += 2; + } + } + + size -= consumed << 1; + start += consumed << 1; + if (start >= buffer_size) + start -= buffer_size; + } + + inline int + avail (void) + { + return (((size >> 2) * f__inv_r_step) - ((f__r_frac * f__inv_r_step) >> f_prec)) >> (f_prec - 1); + } +}; + +#endif /* __LINEAR_RESAMPLER_H */ diff --git a/source/snes9x/apu/resampler.h b/source/snes9x/apu/resampler.h index 51a3226..2e62add 100644 --- a/source/snes9x/apu/resampler.h +++ b/source/snes9x/apu/resampler.h @@ -5,158 +5,56 @@ #include "ring_buffer.h" -#undef MIN -#define MIN(a, b) ((a) < (b) ? (a) : (b)) - -#undef CLAMP -#undef short_clamp -#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) -#define short_clamp(n) ((short) CLAMP((n), -32768, 32767)) - class Resampler : public ring_buffer { - protected: - - double r_step; - double r_frac; - int r_left[4], r_right[4]; - - double - hermite (double mu1, double a, double b, double c, double d) - { - const double tension = 0.0; //-1 = low, 0 = normal, 1 = high - const double bias = 0.0; //-1 = left, 0 = even, 1 = right - - double mu2, mu3, m0, m1, a0, a1, a2, a3; - - mu2 = mu1 * mu1; - mu3 = mu2 * mu1; - - m0 = (b - a) * (1 + bias) * (1 - tension) / 2; - m0 += (c - b) * (1 - bias) * (1 - tension) / 2; - m1 = (c - b) * (1 + bias) * (1 - tension) / 2; - m1 += (d - c) * (1 - bias) * (1 - tension) / 2; - - a0 = +2 * mu3 - 3 * mu2 + 1; - a1 = mu3 - 2 * mu2 + mu1; - a2 = mu3 - mu2; - a3 = -2 * mu3 + 3 * mu2; - - return (a0 * b) + (a1 * m0) + (a2 * m1) + (a3 * c); - } - public: + virtual void clear (void) = 0; + virtual void time_ratio (double) = 0; + virtual void read (short *, int) = 0; + virtual int avail (void) = 0; + Resampler (int num_samples) : ring_buffer (num_samples << 1) { - r_frac = 0.0; } ~Resampler () { } - void - time_ratio (double ratio) - { - r_step = ratio; - clear (); - } - - void - clear (void) - { - ring_buffer::clear (); - r_frac = 0; - r_left [0] = r_left [1] = r_left [2] = r_left [3] = 0; - r_right[0] = r_right[1] = r_right[2] = r_right[3] = 0; - } - - void - read (short *data, int num_samples) - { - int i_position = start >> 1; - short *internal_buffer = (short *) buffer; - int o_position = 0; - int consumed = 0; - - while (o_position < num_samples && consumed < buffer_size) - { - int s_left = internal_buffer[i_position]; - int s_right = internal_buffer[i_position + 1]; - const double margin_of_error = 1.0e-10; - - if (fabs(r_step - 1.0) < margin_of_error) - { - data[o_position] = (short) s_left; - data[o_position + 1] = (short) s_right; - - o_position += 2; - i_position = (i_position + 2) % (buffer_size >> 1); - consumed += 2; - - continue; - } - - r_left [0] = r_left [1]; - r_left [1] = r_left [2]; - r_left [2] = r_left [3]; - r_left [3] = s_left; - - r_right[0] = r_right[1]; - r_right[1] = r_right[2]; - r_right[2] = r_right[3]; - r_right[3] = s_right; - - while (r_frac <= 1.0 && o_position < num_samples) - { - data[o_position] = short_clamp (hermite (r_frac, r_left [0], r_left [1], r_left [2], r_left [3])); - data[o_position + 1] = short_clamp (hermite (r_frac, r_right[0], r_right[1], r_right[2], r_right[3])); - - o_position += 2; - - r_frac += r_step; - } - - if (r_frac > 1.0) - { - r_frac -= 1.0; - i_position = (i_position + 2) % (buffer_size >> 1); - consumed += 2; - } - } - - size -= consumed << 1; - start = (start + (consumed << 1)) % buffer_size; - } - - bool + inline bool push (short *src, int num_samples) { if (max_write () < num_samples) return false; - ring_buffer::push ((unsigned char *) src, num_samples << 1); + !num_samples || ring_buffer::push ((unsigned char *) src, num_samples << 1); return true; } - int + inline int + space_empty (void) + { + return buffer_size - size; + } + + inline int + space_filled (void) + { + return size; + } + + inline int max_write (void) { return space_empty () >> 1; } - void + inline void resize (int num_samples) { ring_buffer::resize (num_samples << 1); } - - int - avail (void) - { - return (int) floor (((size >> 2) - r_frac) / r_step) * 2; - } }; #endif /* __RESAMPLER_H */ diff --git a/source/snes9x/cpu.cpp b/source/snes9x/cpu.cpp index 91c986c..3e249cc 100644 --- a/source/snes9x/cpu.cpp +++ b/source/snes9x/cpu.cpp @@ -207,22 +207,6 @@ static void S9xResetCPU (void) static void S9xSoftResetCPU (void) { - Registers.PBPC = 0; - Registers.PB = 0; - Registers.PCw = S9xGetWord(0xfffc); - OpenBus = Registers.PCh; - Registers.D.W = 0; - Registers.DB = 0; - Registers.SH = 1; - Registers.SL -= 3; - Registers.XH = 0; - Registers.YH = 0; - - ICPU.ShiftedPB = 0; - ICPU.ShiftedDB = 0; - SetFlags(MemoryFlag | IndexFlag | IRQ | Emulation); - ClearFlags(Decimal); - CPU.Cycles = 182; // Or 188. This is the cycle count just after the jump to the Reset Vector. CPU.PrevCycles = -1; CPU.V_Counter = 0; @@ -248,6 +232,22 @@ static void S9xSoftResetCPU (void) CPU.AutoSaveTimer = 0; CPU.SRAMModified = FALSE; + Registers.PBPC = 0; + Registers.PB = 0; + Registers.PCw = S9xGetWord(0xfffc); + OpenBus = Registers.PCh; + Registers.D.W = 0; + Registers.DB = 0; + Registers.SH = 1; + Registers.SL -= 3; + Registers.XH = 0; + Registers.YH = 0; + + ICPU.ShiftedPB = 0; + ICPU.ShiftedDB = 0; + SetFlags(MemoryFlag | IndexFlag | IRQ | Emulation); + ClearFlags(Decimal); + Timings.InterlaceField = FALSE; Timings.H_Max = Timings.H_Max_Master; Timings.V_Max = Timings.V_Max_Master; diff --git a/source/snes9x/cpuexec.cpp b/source/snes9x/cpuexec.cpp index d7f22db..1b9d892 100644 --- a/source/snes9x/cpuexec.cpp +++ b/source/snes9x/cpuexec.cpp @@ -307,8 +307,10 @@ void S9xMainLoop (void) if (SA1.Executing) S9xSA1MainLoop(); + #if (S9X_ACCURACY_LEVEL <= 2) while (CPU.Cycles >= CPU.NextEvent) S9xDoHEventProcessing(); + #endif } S9xPackStatus(); @@ -355,10 +357,29 @@ void S9xClearIRQ (uint32 source) void S9xDoHEventProcessing (void) { +#ifdef DEBUGGER + static char eventname[13][32] = + { + "", + "HC_HBLANK_START_EVENT", + "HC_IRQ_1_3_EVENT ", + "HC_HDMA_START_EVENT ", + "HC_IRQ_3_5_EVENT ", + "HC_HCOUNTER_MAX_EVENT", + "HC_IRQ_5_7_EVENT ", + "HC_HDMA_INIT_EVENT ", + "HC_IRQ_7_9_EVENT ", + "HC_RENDER_EVENT ", + "HC_IRQ_9_A_EVENT ", + "HC_WRAM_REFRESH_EVENT", + "HC_IRQ_A_1_EVENT " + }; +#endif + #ifdef DEBUGGER if (Settings.TraceHCEvent) - S9xTraceFormattedMessage("--- HC event processing (%02d) expected HC:%04d executed HC:%04d", - CPU.WhichEvent, CPU.NextEvent, CPU.Cycles); + S9xTraceFormattedMessage("--- HC event processing (%s) expected HC:%04d executed HC:%04d", + eventname[CPU.WhichEvent], CPU.NextEvent, CPU.Cycles); #endif #ifdef CPU_SHUTDOWN @@ -366,13 +387,16 @@ void S9xDoHEventProcessing (void) #endif switch (CPU.WhichEvent) - { + { case HC_HBLANK_START_EVENT: S9xCheckMissingHTimerPosition(Timings.HBlankStart); - + S9xReschedule(); break; case HC_HDMA_START_EVENT: + S9xCheckMissingHTimerPosition(Timings.HDMAStart); + S9xReschedule(); + if (PPU.HDMA && CPU.V_Counter <= PPU.ScreenHeight) { #ifdef DEBUGGER @@ -381,8 +405,6 @@ void S9xDoHEventProcessing (void) PPU.HDMA = S9xDoHDMA(PPU.HDMA); } - S9xCheckMissingHTimerPosition(Timings.HDMAStart); - break; case HC_HCOUNTER_MAX_EVENT: @@ -470,7 +492,7 @@ void S9xDoHEventProcessing (void) missing.dma_this_frame = 0; #endif IPPU.MaxBrightness = PPU.Brightness; - PPU.ForcedBlanking = (Memory.FillRAM [0x2100] >> 7) & 1; + PPU.ForcedBlanking = (Memory.FillRAM[0x2100] >> 7) & 1; if (!PPU.ForcedBlanking) { @@ -511,14 +533,21 @@ void S9xDoHEventProcessing (void) S9xStartScreenRefresh(); CPU.NextEvent = -1; + S9xReschedule(); break; case HC_HDMA_INIT_EVENT: - if (CPU.V_Counter == 0) - S9xStartHDMA(); - S9xCheckMissingHTimerPosition(Timings.HDMAInit); + S9xReschedule(); + + if (CPU.V_Counter == 0) + { + #ifdef DEBUGGER + S9xTraceFormattedMessage("*** HDMA Init HC:%04d, Channel:%02x", CPU.Cycles, PPU.HDMA); + #endif + S9xStartHDMA(); + } break; @@ -527,6 +556,7 @@ void S9xDoHEventProcessing (void) RenderLine((uint8) (CPU.V_Counter - FIRST_VISIBLE_LINE)); S9xCheckMissingHTimerPosition(Timings.RenderPos); + S9xReschedule(); break; @@ -534,10 +564,12 @@ void S9xDoHEventProcessing (void) #ifdef DEBUGGER S9xTraceFormattedMessage("*** WRAM Refresh HC:%04d", CPU.Cycles); #endif + S9xCheckMissingHTimerHalt(Timings.WRAMRefreshPos, SNES_WRAM_REFRESH_CYCLES); CPU.Cycles += SNES_WRAM_REFRESH_CYCLES; S9xCheckMissingHTimerPosition(Timings.WRAMRefreshPos); + S9xReschedule(); break; @@ -553,13 +585,13 @@ void S9xDoHEventProcessing (void) if (PPU.VTimerEnabled && (CPU.V_Counter == PPU.VTimerPosition)) S9xSetIRQ(PPU_IRQ_SOURCE); + S9xReschedule(); break; - } - - S9xReschedule(); + } #ifdef DEBUGGER if (Settings.TraceHCEvent) - S9xTraceFormattedMessage("--- HC event rescheduled (%02d) expected HC:%04d", CPU.WhichEvent, CPU.NextEvent); + S9xTraceFormattedMessage("--- HC event rescheduled (%s) expected HC:%04d current HC:%04d", + eventname[CPU.WhichEvent], CPU.NextEvent, CPU.Cycles); #endif } diff --git a/source/snes9x/cpuops.cpp b/source/snes9x/cpuops.cpp index c10a0c0..ea8d865 100644 --- a/source/snes9x/cpuops.cpp +++ b/source/snes9x/cpuops.cpp @@ -190,7 +190,11 @@ #ifdef SA1_OPCODES #define AddCycles(n) { } #else -#define AddCycles(n) CPU.Cycles += n +#if (S9X_ACCURACY_LEVEL >= 3) +#define AddCycles(n) { CPU.Cycles += (n); while (CPU.Cycles >= CPU.NextEvent) S9xDoHEventProcessing(); } +#else +#define AddCycles(n) { CPU.Cycles += (n); } +#endif #endif #include "cpuaddr.h" diff --git a/source/snes9x/dma.cpp b/source/snes9x/dma.cpp index ef8d737..bd2c6f2 100644 --- a/source/snes9x/dma.cpp +++ b/source/snes9x/dma.cpp @@ -185,6 +185,8 @@ #include "missing.h" #endif +#define ADD_CYCLES(n) CPU.Cycles += (n) + extern uint8 *HDMAMemPointers[8]; extern int HDMA_ModeByteCounts[8]; extern SPC7110 s7emu; @@ -199,7 +201,7 @@ static inline bool8 addCyclesInDMA (uint8 dma_channel) { // Add 8 cycles per byte, sync APU, and do HC related events. // If HDMA was done in S9xDoHEventProcessing(), check if it used the same channel as DMA. - CPU.Cycles += SLOW_ONE_CYCLE; + ADD_CYCLES(SLOW_ONE_CYCLE); while (CPU.Cycles >= CPU.NextEvent) S9xDoHEventProcessing(); @@ -245,7 +247,7 @@ bool8 S9xDoDMA (uint8 Channel) c = 0x10000; // 8 cycles per channel - CPU.Cycles += SLOW_ONE_CYCLE; + ADD_CYCLES(SLOW_ONE_CYCLE); // 8 cycles per byte while (c) { @@ -510,7 +512,7 @@ bool8 S9xDoDMA (uint8 Channel) uint8 Work; // 8 cycles per channel - CPU.Cycles += SLOW_ONE_CYCLE; + ADD_CYCLES(SLOW_ONE_CYCLE); if (!d->ReverseTransfer) { @@ -1315,7 +1317,7 @@ static inline bool8 HDMAReadLineCount (int d) uint8 line; line = S9xGetByte((DMA[d].ABank << 16) + DMA[d].Address); - CPU.Cycles += SLOW_ONE_CYCLE; + ADD_CYCLES(SLOW_ONE_CYCLE); if (!line) { @@ -1327,10 +1329,10 @@ static inline bool8 HDMAReadLineCount (int d) if (PPU.HDMA & (0xfe << d)) { DMA[d].Address++; - CPU.Cycles += SLOW_ONE_CYCLE * 2; + ADD_CYCLES(SLOW_ONE_CYCLE << 1); } else - CPU.Cycles += SLOW_ONE_CYCLE; + ADD_CYCLES(SLOW_ONE_CYCLE); DMA[d].IndirectAddress = S9xGetWord((DMA[d].ABank << 16) + DMA[d].Address); DMA[d].Address++; @@ -1358,7 +1360,7 @@ static inline bool8 HDMAReadLineCount (int d) if (DMA[d].HDMAIndirectAddressing) { - CPU.Cycles += SLOW_ONE_CYCLE << 1; + ADD_CYCLES(SLOW_ONE_CYCLE << 1); DMA[d].IndirectAddress = S9xGetWord((DMA[d].ABank << 16) + DMA[d].Address); DMA[d].Address += 2; HDMAMemPointers[d] = S9xGetMemPointer((DMA[d].IndirectBank << 16) + DMA[d].IndirectAddress); @@ -1390,7 +1392,7 @@ void S9xStartHDMA (void) // XXX: Not quite right... if (PPU.HDMA != 0) - CPU.Cycles += Timings.DMACPUSync; + ADD_CYCLES(Timings.DMACPUSync); for (uint8 i = 0; i < 8; i++) { @@ -1433,7 +1435,7 @@ uint8 S9xDoHDMA (uint8 byte) tmpch = CPU.CurrentDMAorHDMAChannel; // XXX: Not quite right... - CPU.Cycles += Timings.DMACPUSync; + ADD_CYCLES(Timings.DMACPUSync); for (uint8 mask = 1; mask; mask <<= 1, p++, d++) { @@ -1498,46 +1500,57 @@ uint8 S9xDoHDMA (uint8 byte) switch (p->TransferMode) { case 0: - CPU.Cycles += SLOW_ONE_CYCLE; DOBYTE(IAddr, 0); + ADD_CYCLES(SLOW_ONE_CYCLE); break; case 5: - CPU.Cycles += 4 * SLOW_ONE_CYCLE; DOBYTE(IAddr + 0, 0); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 1, 1); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 2, 0); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 3, 1); + ADD_CYCLES(SLOW_ONE_CYCLE); break; case 1: - CPU.Cycles += 2 * SLOW_ONE_CYCLE; DOBYTE(IAddr + 0, 0); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 1, 1); + ADD_CYCLES(SLOW_ONE_CYCLE); break; case 2: case 6: - CPU.Cycles += 2 * SLOW_ONE_CYCLE; DOBYTE(IAddr + 0, 0); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 1, 0); + ADD_CYCLES(SLOW_ONE_CYCLE); break; case 3: case 7: - CPU.Cycles += 4 * SLOW_ONE_CYCLE; DOBYTE(IAddr + 0, 0); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 1, 0); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 2, 1); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 3, 1); + ADD_CYCLES(SLOW_ONE_CYCLE); break; case 4: - CPU.Cycles += 4 * SLOW_ONE_CYCLE; DOBYTE(IAddr + 0, 0); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 1, 1); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 2, 2); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 3, 3); + ADD_CYCLES(SLOW_ONE_CYCLE); break; } @@ -1556,44 +1569,53 @@ uint8 S9xDoHDMA (uint8 byte) switch (p->TransferMode) { case 0: - CPU.Cycles += SLOW_ONE_CYCLE; S9xSetPPU(S9xGetByte(Addr), 0x2100 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); break; case 5: - CPU.Cycles += 2 * SLOW_ONE_CYCLE; S9xSetPPU(S9xGetByte(Addr + 0), 0x2100 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); S9xSetPPU(S9xGetByte(Addr + 1), 0x2101 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); Addr += 2; /* fall through */ case 1: - CPU.Cycles += 2 * SLOW_ONE_CYCLE; S9xSetPPU(S9xGetByte(Addr + 0), 0x2100 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); S9xSetPPU(S9xGetByte(Addr + 1), 0x2101 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); break; case 2: case 6: - CPU.Cycles += 2 * SLOW_ONE_CYCLE; S9xSetPPU(S9xGetByte(Addr + 0), 0x2100 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); S9xSetPPU(S9xGetByte(Addr + 1), 0x2100 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); break; case 3: case 7: - CPU.Cycles += 4 * SLOW_ONE_CYCLE; S9xSetPPU(S9xGetByte(Addr + 0), 0x2100 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); S9xSetPPU(S9xGetByte(Addr + 1), 0x2100 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); S9xSetPPU(S9xGetByte(Addr + 2), 0x2101 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); S9xSetPPU(S9xGetByte(Addr + 3), 0x2101 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); break; case 4: - CPU.Cycles += 4 * SLOW_ONE_CYCLE; S9xSetPPU(S9xGetByte(Addr + 0), 0x2100 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); S9xSetPPU(S9xGetByte(Addr + 1), 0x2101 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); S9xSetPPU(S9xGetByte(Addr + 2), 0x2102 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); S9xSetPPU(S9xGetByte(Addr + 3), 0x2103 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); break; } } @@ -1603,47 +1625,56 @@ uint8 S9xDoHDMA (uint8 byte) switch (p->TransferMode) { case 0: - CPU.Cycles += SLOW_ONE_CYCLE; S9xSetPPU(*HDMAMemPointers[d]++, 0x2100 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); break; case 5: - CPU.Cycles += 2 * SLOW_ONE_CYCLE; S9xSetPPU(*(HDMAMemPointers[d] + 0), 0x2100 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); S9xSetPPU(*(HDMAMemPointers[d] + 1), 0x2101 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); HDMAMemPointers[d] += 2; /* fall through */ case 1: - CPU.Cycles += 2 * SLOW_ONE_CYCLE; S9xSetPPU(*(HDMAMemPointers[d] + 0), 0x2100 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); S9xSetPPU(*(HDMAMemPointers[d] + 1), 0x2101 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); HDMAMemPointers[d] += 2; break; case 2: case 6: - CPU.Cycles += 2 * SLOW_ONE_CYCLE; S9xSetPPU(*(HDMAMemPointers[d] + 0), 0x2100 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); S9xSetPPU(*(HDMAMemPointers[d] + 1), 0x2100 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); HDMAMemPointers[d] += 2; break; case 3: case 7: - CPU.Cycles += 4 * SLOW_ONE_CYCLE; S9xSetPPU(*(HDMAMemPointers[d] + 0), 0x2100 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); S9xSetPPU(*(HDMAMemPointers[d] + 1), 0x2100 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); S9xSetPPU(*(HDMAMemPointers[d] + 2), 0x2101 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); S9xSetPPU(*(HDMAMemPointers[d] + 3), 0x2101 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); HDMAMemPointers[d] += 4; break; case 4: - CPU.Cycles += 4 * SLOW_ONE_CYCLE; S9xSetPPU(*(HDMAMemPointers[d] + 0), 0x2100 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); S9xSetPPU(*(HDMAMemPointers[d] + 1), 0x2101 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); S9xSetPPU(*(HDMAMemPointers[d] + 2), 0x2102 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); S9xSetPPU(*(HDMAMemPointers[d] + 3), 0x2103 + p->BAddress); + ADD_CYCLES(SLOW_ONE_CYCLE); HDMAMemPointers[d] += 4; break; } @@ -1665,46 +1696,57 @@ uint8 S9xDoHDMA (uint8 byte) switch (p->TransferMode) { case 0: - CPU.Cycles += SLOW_ONE_CYCLE; DOBYTE(IAddr, 0); + ADD_CYCLES(SLOW_ONE_CYCLE); break; case 5: - CPU.Cycles += 4 * SLOW_ONE_CYCLE; DOBYTE(IAddr + 0, 0); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 1, 1); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 2, 0); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 3, 1); + ADD_CYCLES(SLOW_ONE_CYCLE); break; case 1: - CPU.Cycles += 2 * SLOW_ONE_CYCLE; DOBYTE(IAddr + 0, 0); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 1, 1); + ADD_CYCLES(SLOW_ONE_CYCLE); break; case 2: case 6: - CPU.Cycles += 2 * SLOW_ONE_CYCLE; DOBYTE(IAddr + 0, 0); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 1, 0); + ADD_CYCLES(SLOW_ONE_CYCLE); break; case 3: case 7: - CPU.Cycles += 4 * SLOW_ONE_CYCLE; DOBYTE(IAddr + 0, 0); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 1, 0); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 2, 1); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 3, 1); + ADD_CYCLES(SLOW_ONE_CYCLE); break; case 4: - CPU.Cycles += 4 * SLOW_ONE_CYCLE; DOBYTE(IAddr + 0, 0); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 1, 1); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 2, 2); + ADD_CYCLES(SLOW_ONE_CYCLE); DOBYTE(IAddr + 3, 3); + ADD_CYCLES(SLOW_ONE_CYCLE); break; } @@ -1730,7 +1772,7 @@ uint8 S9xDoHDMA (uint8 byte) } } else - CPU.Cycles += SLOW_ONE_CYCLE; + ADD_CYCLES(SLOW_ONE_CYCLE); } } diff --git a/source/snes9x/getset.h b/source/snes9x/getset.h index 6d2b645..bf5557b 100644 --- a/source/snes9x/getset.h +++ b/source/snes9x/getset.h @@ -187,15 +187,63 @@ #include "seta.h" #include "bsx.h" +#if (S9X_ACCURACY_LEVEL >= 2) + +#define addCyclesInMemoryAccess \ + if (!CPU.InDMAorHDMA) \ + { \ + CPU.Cycles += speed; \ + while (CPU.Cycles >= CPU.NextEvent) \ + S9xDoHEventProcessing(); \ + } + +#define addCyclesInMemoryAccess_x2 \ + if (!CPU.InDMAorHDMA) \ + { \ + CPU.Cycles += speed << 1; \ + while (CPU.Cycles >= CPU.NextEvent) \ + S9xDoHEventProcessing(); \ + } + +#else + +#define addCyclesInMemoryAccess \ + if (!CPU.InDMAorHDMA) \ + CPU.Cycles += speed; + +#define addCyclesInMemoryAccess_x2 \ + if (!CPU.InDMAorHDMA) \ + CPU.Cycles += speed << 1; + +#endif + extern uint8 OpenBus; +static inline int32 memory_speed (uint32 address) +{ + if (address & 0x408000) + { + if (address & 0x800000) + return (CPU.FastROMSpeed); + + return (SLOW_ONE_CYCLE); + } + + if ((address + 0x6000) & 0x4000) + return (SLOW_ONE_CYCLE); + + if ((address - 0x4000) & 0x7e00) + return (ONE_CYCLE); + + return (TWO_CYCLES); +} + inline uint8 S9xGetByte (uint32 Address) { - int block; - uint8 *GetAddress = Memory.Map[block = ((Address & 0xffffff) >> MEMMAP_SHIFT)]; - - if (!CPU.InDMAorHDMA) - CPU.Cycles += Memory.MemorySpeed[block]; + int block = (Address & 0xffffff) >> MEMMAP_SHIFT; + uint8 *GetAddress = Memory.Map[block]; + int32 speed = memory_speed(Address); + uint8 byte; if (GetAddress >= (uint8 *) CMemory::MAP_LAST) { @@ -203,18 +251,25 @@ inline uint8 S9xGetByte (uint32 Address) if (Memory.BlockIsRAM[block]) CPU.WaitAddress = CPU.PBPCAtOpcodeStart; #endif - return (*(GetAddress + (Address & 0xffff))); + byte = *(GetAddress + (Address & 0xffff)); + addCyclesInMemoryAccess; + return (byte); } switch ((pint) GetAddress) { case CMemory::MAP_CPU: - return (S9xGetCPU(Address & 0xffff)); + byte = S9xGetCPU(Address & 0xffff); + addCyclesInMemoryAccess; + return (byte); case CMemory::MAP_PPU: if (CPU.InDMAorHDMA && (Address & 0xff00) == 0x2100) return (OpenBus); - return (S9xGetPPU(Address & 0xffff)); + + byte = S9xGetPPU(Address & 0xffff); + addCyclesInMemoryAccess; + return (byte); case CMemory::MAP_LOROM_SRAM: case CMemory::MAP_SA1RAM: @@ -222,45 +277,71 @@ inline uint8 S9xGetByte (uint32 Address) // Address & 0xff0000 : bank // bank >> 1 | offset : SRAM address, unbound // unbound & SRAMMask : SRAM offset - return (*(Memory.SRAM + ((((Address & 0xff0000) >> 1) | (Address & 0x7fff)) & Memory.SRAMMask))); + byte = *(Memory.SRAM + ((((Address & 0xff0000) >> 1) | (Address & 0x7fff)) & Memory.SRAMMask)); + addCyclesInMemoryAccess; + return (byte); case CMemory::MAP_LOROM_SRAM_B: - return (*(Multi.sramB + ((((Address & 0xff0000) >> 1) | (Address & 0x7fff)) & Multi.sramMaskB))); + byte = *(Multi.sramB + ((((Address & 0xff0000) >> 1) | (Address & 0x7fff)) & Multi.sramMaskB)); + addCyclesInMemoryAccess; + return (byte); case CMemory::MAP_HIROM_SRAM: case CMemory::MAP_RONLY_SRAM: - return (*(Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0xf0000) >> 3)) & Memory.SRAMMask))); + byte = *(Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0xf0000) >> 3)) & Memory.SRAMMask)); + addCyclesInMemoryAccess; + return (byte); case CMemory::MAP_BWRAM: - return (*(Memory.BWRAM + ((Address & 0x7fff) - 0x6000))); + byte = *(Memory.BWRAM + ((Address & 0x7fff) - 0x6000)); + addCyclesInMemoryAccess; + return (byte); case CMemory::MAP_DSP: - return (S9xGetDSP(Address & 0xffff)); + byte = S9xGetDSP(Address & 0xffff); + addCyclesInMemoryAccess; + return (byte); case CMemory::MAP_SPC7110_ROM: - return (S9xGetSPC7110Byte(Address)); + byte = S9xGetSPC7110Byte(Address); + addCyclesInMemoryAccess; + return (byte); case CMemory::MAP_SPC7110_DRAM: - return (S9xGetSPC7110(0x4800)); + byte = S9xGetSPC7110(0x4800); + addCyclesInMemoryAccess; + return (byte); case CMemory::MAP_C4: - return (S9xGetC4(Address & 0xffff)); + byte = S9xGetC4(Address & 0xffff); + addCyclesInMemoryAccess; + return (byte); case CMemory::MAP_OBC_RAM: - return (S9xGetOBC1(Address & 0xffff)); + byte = S9xGetOBC1(Address & 0xffff); + addCyclesInMemoryAccess; + return (byte); case CMemory::MAP_SETA_DSP: - return (S9xGetSetaDSP(Address)); + byte = S9xGetSetaDSP(Address); + addCyclesInMemoryAccess; + return (byte); case CMemory::MAP_SETA_RISC: - return (S9xGetST018(Address)); + byte = S9xGetST018(Address); + addCyclesInMemoryAccess; + return (byte); case CMemory::MAP_BSX: - return (S9xGetBSX(Address)); + byte = S9xGetBSX(Address); + addCyclesInMemoryAccess; + return (byte); case CMemory::MAP_NONE: default: - return (OpenBus); + byte = OpenBus; + addCyclesInMemoryAccess; + return (byte); } } @@ -291,11 +372,10 @@ inline uint16 S9xGetWord (uint32 Address, enum s9xwrap_t w = WRAP_NONE) } } - int block; - uint8 *GetAddress = Memory.Map[block = ((Address & 0xffffff) >> MEMMAP_SHIFT)]; - - if (!CPU.InDMAorHDMA) - CPU.Cycles += (Memory.MemorySpeed[block] << 1); + int block = (Address & 0xffffff) >> MEMMAP_SHIFT; + uint8 *GetAddress = Memory.Map[block]; + int32 speed = memory_speed(Address); + uint16 word; if (GetAddress >= (uint8 *) CMemory::MAP_LAST) { @@ -303,13 +383,19 @@ inline uint16 S9xGetWord (uint32 Address, enum s9xwrap_t w = WRAP_NONE) if (Memory.BlockIsRAM[block]) CPU.WaitAddress = CPU.PBPCAtOpcodeStart; #endif - return (READ_WORD(GetAddress + (Address & 0xffff))); + word = READ_WORD(GetAddress + (Address & 0xffff)); + addCyclesInMemoryAccess_x2; + return (word); } switch ((pint) GetAddress) { case CMemory::MAP_CPU: - return (S9xGetCPU(Address & 0xffff) | (S9xGetCPU((Address + 1) & 0xffff) << 8)); + word = S9xGetCPU(Address & 0xffff); + addCyclesInMemoryAccess; + word |= S9xGetCPU((Address + 1) & 0xffff) << 8; + addCyclesInMemoryAccess; + return (word); case CMemory::MAP_PPU: if (CPU.InDMAorHDMA) @@ -318,61 +404,107 @@ inline uint16 S9xGetWord (uint32 Address, enum s9xwrap_t w = WRAP_NONE) return (OpenBus | (S9xGetByte(Address + 1) << 8)); } - return (S9xGetPPU(Address & 0xffff) | (S9xGetPPU((Address + 1) & 0xffff) << 8)); + word = S9xGetPPU(Address & 0xffff); + addCyclesInMemoryAccess; + word |= S9xGetPPU((Address + 1) & 0xffff) << 8; + addCyclesInMemoryAccess; + return (word); case CMemory::MAP_LOROM_SRAM: case CMemory::MAP_SA1RAM: if (Memory.SRAMMask >= MEMMAP_MASK) - return (READ_WORD(Memory.SRAM + ((((Address & 0xff0000) >> 1) | (Address & 0x7fff)) & Memory.SRAMMask))); + word = READ_WORD(Memory.SRAM + ((((Address & 0xff0000) >> 1) | (Address & 0x7fff)) & Memory.SRAMMask)); else - return ((*(Memory.SRAM + ((((Address & 0xff0000) >> 1) | (Address & 0x7fff)) & Memory.SRAMMask))) | - ((*(Memory.SRAM + (((((Address + 1) & 0xff0000) >> 1) | ((Address + 1) & 0x7fff)) & Memory.SRAMMask))) << 8)); + word = (*(Memory.SRAM + ((((Address & 0xff0000) >> 1) | (Address & 0x7fff)) & Memory.SRAMMask))) | + ((*(Memory.SRAM + (((((Address + 1) & 0xff0000) >> 1) | ((Address + 1) & 0x7fff)) & Memory.SRAMMask))) << 8); + addCyclesInMemoryAccess_x2; + return (word); case CMemory::MAP_LOROM_SRAM_B: if (Multi.sramMaskB >= MEMMAP_MASK) - return (READ_WORD(Multi.sramB + ((((Address & 0xff0000) >> 1) | (Address & 0x7fff)) & Multi.sramMaskB))); + word = READ_WORD(Multi.sramB + ((((Address & 0xff0000) >> 1) | (Address & 0x7fff)) & Multi.sramMaskB)); else - return ((*(Multi.sramB + ((((Address & 0xff0000) >> 1) | (Address & 0x7fff)) & Multi.sramMaskB))) | - ((*(Multi.sramB + (((((Address + 1) & 0xff0000) >> 1) | ((Address + 1) & 0x7fff)) & Multi.sramMaskB))) << 8)); + word = (*(Multi.sramB + ((((Address & 0xff0000) >> 1) | (Address & 0x7fff)) & Multi.sramMaskB))) | + ((*(Multi.sramB + (((((Address + 1) & 0xff0000) >> 1) | ((Address + 1) & 0x7fff)) & Multi.sramMaskB))) << 8); + addCyclesInMemoryAccess_x2; + return (word); case CMemory::MAP_HIROM_SRAM: case CMemory::MAP_RONLY_SRAM: if (Memory.SRAMMask >= MEMMAP_MASK) - return (READ_WORD(Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0xf0000) >> 3)) & Memory.SRAMMask))); + word = READ_WORD(Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0xf0000) >> 3)) & Memory.SRAMMask)); else - return ((*(Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0xf0000) >> 3)) & Memory.SRAMMask)) | - (*(Memory.SRAM + ((((Address + 1) & 0x7fff) - 0x6000 + (((Address + 1) & 0xf0000) >> 3)) & Memory.SRAMMask)) << 8))); + word = (*(Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0xf0000) >> 3)) & Memory.SRAMMask)) | + (*(Memory.SRAM + ((((Address + 1) & 0x7fff) - 0x6000 + (((Address + 1) & 0xf0000) >> 3)) & Memory.SRAMMask)) << 8)); + addCyclesInMemoryAccess_x2; + return (word); case CMemory::MAP_BWRAM: - return (READ_WORD(Memory.BWRAM + ((Address & 0x7fff) - 0x6000))); + word = READ_WORD(Memory.BWRAM + ((Address & 0x7fff) - 0x6000)); + addCyclesInMemoryAccess_x2; + return (word); case CMemory::MAP_DSP: - return (S9xGetDSP(Address & 0xffff) | (S9xGetDSP((Address + 1) & 0xffff) << 8)); + word = S9xGetDSP(Address & 0xffff); + addCyclesInMemoryAccess; + word |= S9xGetDSP((Address + 1) & 0xffff) << 8; + addCyclesInMemoryAccess; + return (word); case CMemory::MAP_SPC7110_ROM: - return (S9xGetSPC7110Byte(Address) | (S9xGetSPC7110Byte(Address + 1) << 8)); + word = S9xGetSPC7110Byte(Address); + addCyclesInMemoryAccess; + word |= S9xGetSPC7110Byte(Address + 1) << 8; + addCyclesInMemoryAccess; + return (word); case CMemory::MAP_SPC7110_DRAM: - return (S9xGetSPC7110(0x4800) | (S9xGetSPC7110(0x4800) << 8)); + word = S9xGetSPC7110(0x4800); + addCyclesInMemoryAccess; + word |= S9xGetSPC7110(0x4800) << 8; + addCyclesInMemoryAccess; + return (word); case CMemory::MAP_C4: - return (S9xGetC4(Address & 0xffff) | (S9xGetC4((Address + 1) & 0xffff) << 8)); + word = S9xGetC4(Address & 0xffff); + addCyclesInMemoryAccess; + word |= S9xGetC4((Address + 1) & 0xffff) << 8; + addCyclesInMemoryAccess; + return (word); case CMemory::MAP_OBC_RAM: - return (S9xGetOBC1(Address & 0xffff) | (S9xGetOBC1((Address + 1) & 0xffff) << 8)); + word = S9xGetOBC1(Address & 0xffff); + addCyclesInMemoryAccess; + word |= S9xGetOBC1((Address + 1) & 0xffff) << 8; + addCyclesInMemoryAccess; + return (word); case CMemory::MAP_SETA_DSP: - return (S9xGetSetaDSP(Address) | (S9xGetSetaDSP(Address + 1) << 8)); + word = S9xGetSetaDSP(Address); + addCyclesInMemoryAccess; + word |= S9xGetSetaDSP(Address + 1) << 8; + addCyclesInMemoryAccess; + return (word); case CMemory::MAP_SETA_RISC: - return (S9xGetST018(Address) | (S9xGetST018(Address + 1) << 8)); + word = S9xGetST018(Address); + addCyclesInMemoryAccess; + word |= S9xGetST018(Address + 1) << 8; + addCyclesInMemoryAccess; + return (word); case CMemory::MAP_BSX: - return (S9xGetBSX(Address) | (S9xGetBSX(Address + 1) << 8)); + word = S9xGetBSX(Address); + addCyclesInMemoryAccess; + word |= S9xGetBSX(Address + 1) << 8; + addCyclesInMemoryAccess; + return (word); case CMemory::MAP_NONE: default: - return (OpenBus | (OpenBus << 8)); + word = OpenBus | (OpenBus << 8); + addCyclesInMemoryAccess_x2; + return (word); } } @@ -382,17 +514,16 @@ inline void S9xSetByte (uint8 Byte, uint32 Address) CPU.WaitAddress = 0xffffffff; #endif - int block; - uint8 *SetAddress = Memory.WriteMap[block = ((Address & 0xffffff) >> MEMMAP_SHIFT)]; - - if (!CPU.InDMAorHDMA) - CPU.Cycles += Memory.MemorySpeed[block]; + int block = (Address & 0xffffff) >> MEMMAP_SHIFT; + uint8 *SetAddress = Memory.WriteMap[block]; + int32 speed = memory_speed(Address); if (SetAddress >= (uint8 *) CMemory::MAP_LAST) { #ifdef CPU_SHUTDOWN SetAddress += (Address & 0xffff); *SetAddress = Byte; + addCyclesInMemoryAccess; if (Settings.SA1) { @@ -404,6 +535,7 @@ inline void S9xSetByte (uint8 Byte, uint32 Address) } #else *(SetAddress + (Address & 0xffff)) = Byte; + addCyclesInMemoryAccess; #endif return; } @@ -412,12 +544,15 @@ inline void S9xSetByte (uint8 Byte, uint32 Address) { case CMemory::MAP_CPU: S9xSetCPU(Byte, Address & 0xffff); + addCyclesInMemoryAccess; return; case CMemory::MAP_PPU: if (CPU.InDMAorHDMA && (Address & 0xff00) == 0x2100) return; + S9xSetPPU(Byte, Address & 0xffff); + addCyclesInMemoryAccess; return; case CMemory::MAP_LOROM_SRAM: @@ -427,6 +562,7 @@ inline void S9xSetByte (uint8 Byte, uint32 Address) CPU.SRAMModified = TRUE; } + addCyclesInMemoryAccess; return; case CMemory::MAP_LOROM_SRAM_B: @@ -436,6 +572,7 @@ inline void S9xSetByte (uint8 Byte, uint32 Address) CPU.SRAMModified = TRUE; } + addCyclesInMemoryAccess; return; case CMemory::MAP_HIROM_SRAM: @@ -445,44 +582,54 @@ inline void S9xSetByte (uint8 Byte, uint32 Address) CPU.SRAMModified = TRUE; } + addCyclesInMemoryAccess; return; case CMemory::MAP_BWRAM: *(Memory.BWRAM + ((Address & 0x7fff) - 0x6000)) = Byte; CPU.SRAMModified = TRUE; + addCyclesInMemoryAccess; return; case CMemory::MAP_SA1RAM: *(Memory.SRAM + (Address & 0xffff)) = Byte; SA1.Executing = !SA1.Waiting; + addCyclesInMemoryAccess; return; case CMemory::MAP_DSP: S9xSetDSP(Byte, Address & 0xffff); + addCyclesInMemoryAccess; return; case CMemory::MAP_C4: S9xSetC4(Byte, Address & 0xffff); + addCyclesInMemoryAccess; return; case CMemory::MAP_OBC_RAM: S9xSetOBC1(Byte, Address & 0xffff); + addCyclesInMemoryAccess; return; case CMemory::MAP_SETA_DSP: S9xSetSetaDSP(Byte, Address); + addCyclesInMemoryAccess; return; case CMemory::MAP_SETA_RISC: S9xSetST018(Byte, Address); + addCyclesInMemoryAccess; return; case CMemory::MAP_BSX: S9xSetBSX(Byte, Address); + addCyclesInMemoryAccess; return; case CMemory::MAP_NONE: default: + addCyclesInMemoryAccess; return; } } @@ -527,17 +674,16 @@ inline void S9xSetWord (uint16 Word, uint32 Address, enum s9xwrap_t w = WRAP_NON CPU.WaitAddress = 0xffffffff; #endif - int block; - uint8 *SetAddress = Memory.WriteMap[block = ((Address & 0xffffff) >> MEMMAP_SHIFT)]; - - if (!CPU.InDMAorHDMA) - CPU.Cycles += (Memory.MemorySpeed[block] << 1); + int block = (Address & 0xffffff) >> MEMMAP_SHIFT; + uint8 *SetAddress = Memory.WriteMap[block]; + int32 speed = memory_speed(Address); if (SetAddress >= (uint8 *) CMemory::MAP_LAST) { #ifdef CPU_SHUTDOWN SetAddress += (Address & 0xffff); WRITE_WORD(SetAddress, Word); + addCyclesInMemoryAccess_x2; if (Settings.SA1) { @@ -549,6 +695,7 @@ inline void S9xSetWord (uint16 Word, uint32 Address, enum s9xwrap_t w = WRAP_NON } #else WRITE_WORD(SetAddress + (Address & 0xffff), Word); + addCyclesInMemoryAccess_x2; #endif return; } @@ -559,16 +706,20 @@ inline void S9xSetWord (uint16 Word, uint32 Address, enum s9xwrap_t w = WRAP_NON if (o) { S9xSetCPU(Word >> 8, (Address + 1) & 0xffff); + addCyclesInMemoryAccess; S9xSetCPU((uint8) Word, Address & 0xffff); + addCyclesInMemoryAccess; + return; } else { S9xSetCPU((uint8) Word, Address & 0xffff); + addCyclesInMemoryAccess; S9xSetCPU(Word >> 8, (Address + 1) & 0xffff); + addCyclesInMemoryAccess; + return; } - return; - case CMemory::MAP_PPU: if (CPU.InDMAorHDMA) { @@ -582,16 +733,20 @@ inline void S9xSetWord (uint16 Word, uint32 Address, enum s9xwrap_t w = WRAP_NON if (o) { S9xSetPPU(Word >> 8, (Address + 1) & 0xffff); + addCyclesInMemoryAccess; S9xSetPPU((uint8) Word, Address & 0xffff); + addCyclesInMemoryAccess; + return; } else { S9xSetPPU((uint8) Word, Address & 0xffff); + addCyclesInMemoryAccess; S9xSetPPU(Word >> 8, (Address + 1) & 0xffff); + addCyclesInMemoryAccess; + return; } - return; - case CMemory::MAP_LOROM_SRAM: if (Memory.SRAMMask) { @@ -606,6 +761,7 @@ inline void S9xSetWord (uint16 Word, uint32 Address, enum s9xwrap_t w = WRAP_NON CPU.SRAMModified = TRUE; } + addCyclesInMemoryAccess_x2; return; case CMemory::MAP_LOROM_SRAM_B: @@ -622,6 +778,7 @@ inline void S9xSetWord (uint16 Word, uint32 Address, enum s9xwrap_t w = WRAP_NON CPU.SRAMModified = TRUE; } + addCyclesInMemoryAccess_x2; return; case CMemory::MAP_HIROM_SRAM: @@ -638,104 +795,132 @@ inline void S9xSetWord (uint16 Word, uint32 Address, enum s9xwrap_t w = WRAP_NON CPU.SRAMModified = TRUE; } + addCyclesInMemoryAccess_x2; return; case CMemory::MAP_BWRAM: WRITE_WORD(Memory.BWRAM + ((Address & 0x7fff) - 0x6000), Word); CPU.SRAMModified = TRUE; + addCyclesInMemoryAccess_x2; return; case CMemory::MAP_SA1RAM: WRITE_WORD(Memory.SRAM + (Address & 0xffff), Word); SA1.Executing = !SA1.Waiting; + addCyclesInMemoryAccess_x2; return; case CMemory::MAP_DSP: if (o) { S9xSetDSP(Word >> 8, (Address + 1) & 0xffff); + addCyclesInMemoryAccess; S9xSetDSP((uint8) Word, Address & 0xffff); + addCyclesInMemoryAccess; + return; } else { S9xSetDSP((uint8) Word, Address & 0xffff); + addCyclesInMemoryAccess; S9xSetDSP(Word >> 8, (Address + 1) & 0xffff); + addCyclesInMemoryAccess; + return; } - return; - case CMemory::MAP_C4: if (o) { S9xSetC4(Word >> 8, (Address + 1) & 0xffff); + addCyclesInMemoryAccess; S9xSetC4((uint8) Word, Address & 0xffff); + addCyclesInMemoryAccess; + return; } else { S9xSetC4((uint8) Word, Address & 0xffff); + addCyclesInMemoryAccess; S9xSetC4(Word >> 8, (Address + 1) & 0xffff); + addCyclesInMemoryAccess; + return; } - return; - case CMemory::MAP_OBC_RAM: if (o) { S9xSetOBC1(Word >> 8, (Address + 1) & 0xffff); + addCyclesInMemoryAccess; S9xSetOBC1((uint8) Word, Address & 0xffff); + addCyclesInMemoryAccess; + return; } else { S9xSetOBC1((uint8) Word, Address & 0xffff); + addCyclesInMemoryAccess; S9xSetOBC1(Word >> 8, (Address + 1) & 0xffff); + addCyclesInMemoryAccess; + return; } - return; - case CMemory::MAP_SETA_DSP: if (o) { S9xSetSetaDSP(Word >> 8, Address + 1); + addCyclesInMemoryAccess; S9xSetSetaDSP((uint8) Word, Address); + addCyclesInMemoryAccess; + return; } else { S9xSetSetaDSP((uint8) Word, Address); + addCyclesInMemoryAccess; S9xSetSetaDSP(Word >> 8, Address + 1); + addCyclesInMemoryAccess; + return; } - return; - case CMemory::MAP_SETA_RISC: if (o) { S9xSetST018(Word >> 8, Address + 1); + addCyclesInMemoryAccess; S9xSetST018((uint8) Word, Address); + addCyclesInMemoryAccess; + return; } else { S9xSetST018((uint8) Word, Address); + addCyclesInMemoryAccess; S9xSetST018(Word >> 8, Address + 1); + addCyclesInMemoryAccess; + return; } - return; - case CMemory::MAP_BSX: if (o) { S9xSetBSX(Word >> 8, Address + 1); + addCyclesInMemoryAccess; S9xSetBSX((uint8) Word, Address); + addCyclesInMemoryAccess; + return; } else { S9xSetBSX((uint8) Word, Address); + addCyclesInMemoryAccess; S9xSetBSX(Word >> 8, Address + 1); + addCyclesInMemoryAccess; + return; } - return; - case CMemory::MAP_NONE: default: + addCyclesInMemoryAccess_x2; return; } } @@ -748,7 +933,7 @@ inline void S9xSetPCBase (uint32 Address) int block; uint8 *GetAddress = Memory.Map[block = ((Address & 0xffffff) >> MEMMAP_SHIFT)]; - CPU.MemSpeed = Memory.MemorySpeed[block]; + CPU.MemSpeed = memory_speed(Address); CPU.MemSpeedx2 = CPU.MemSpeed << 1; if (GetAddress >= (uint8 *) CMemory::MAP_LAST) diff --git a/source/snes9x/memmap.cpp b/source/snes9x/memmap.cpp index b517413..7d72f62 100644 --- a/source/snes9x/memmap.cpp +++ b/source/snes9x/memmap.cpp @@ -2632,9 +2632,6 @@ void CMemory::InitROM (void) Timings.NMIDMADelay = 24; Timings.IRQPendCount = 0; - CPU.FastROMSpeed = 0; - ResetSpeedMap(); - IPPU.TotalEmulatedFrames = 0; //// Hack games @@ -2676,36 +2673,6 @@ void CMemory::InitROM (void) S9xVerifyControllers(); } -void CMemory::FixROMSpeed (void) -{ - if (CPU.FastROMSpeed == 0) - CPU.FastROMSpeed = SLOW_ONE_CYCLE; - - // [80-bf]:[8000-ffff], [c0-ff]:[0000-ffff] - for (int c = 0x800; c < 0x1000; c++) - { - if (c & 0x8 || c & 0x400) - MemorySpeed[c] = (uint8) CPU.FastROMSpeed; - } -} - -void CMemory::ResetSpeedMap (void) -{ - memset(MemorySpeed, SLOW_ONE_CYCLE, 0x1000); - - // Fast - [00-3f|80-bf]:[2000-3fff|4200-5fff] - // XSlow - [00-3f|80-bf]:[4000-41ff] see also S9xGet/SetCPU() - for (int i = 0; i < 0x400; i += 0x10) - { - MemorySpeed[i + 2] = MemorySpeed[0x800 + i + 2] = ONE_CYCLE; - MemorySpeed[i + 3] = MemorySpeed[0x800 + i + 3] = ONE_CYCLE; - MemorySpeed[i + 4] = MemorySpeed[0x800 + i + 4] = ONE_CYCLE; - MemorySpeed[i + 5] = MemorySpeed[0x800 + i + 5] = ONE_CYCLE; - } - - FixROMSpeed(); -} - // memory map uint32 CMemory::map_mirror (uint32 size, uint32 pos) @@ -3617,37 +3584,6 @@ void CMemory::ApplyROMFixes (void) Timings.HDMAStart = SNES_HDMA_START_HC + Settings.HDMATimingHack - 100; Timings.HBlankStart = SNES_HBLANK_START_HC + Timings.HDMAStart - SNES_HDMA_START_HC; - if (!Settings.DisableGameSpecificHacks) - { - // The HC counter (CPU.Cycles for snes9x) passes over the WRAM refresh point (HC~536) - // while preparing to jump to the IRQ vector address. - // That is to say, the WRAM refresh point is passed over in S9xOpcode_IRQ(). - // Then, HDMA starts just after $210e is half updated, and it causes the flicker of the ground. - // IRQ timing is bad? HDMA timing is bad? else? - if (match_na("GUNDAMW ENDLESSDUEL")) // Shin Kidou Senki Gundam W - Endless Duel - { - Timings.HDMAStart -= 10; - Timings.HBlankStart -= 10; - printf("HDMA timing hack: %d\n", Timings.HDMAStart); - } - - // Due to Snes9x's very inaccurate timings, - // HDMA transfer to $210D-$2114 between the first and second writings to the same addresses. - if (match_na("POWER RANGERS FIGHT")) // Mighty Morphin Power Rangers - The Fighting Edition - { - Timings.HDMAStart -= 10; - Timings.HBlankStart -= 10; - printf("HDMA timing hack: %d\n", Timings.HDMAStart); - } - - if (match_na("SFX SUPERBUTOUDEN2")) // Dragon Ball Z - Super Butouden 2 - { - Timings.HDMAStart += 20; - Timings.HBlankStart += 20; - printf("HDMA timing hack: %d\n", Timings.HDMAStart); - } - } - if (!Settings.DisableGameSpecificHacks) { // The delay to sync CPU and DMA which Snes9x cannot emulate. @@ -3678,6 +3614,12 @@ void CMemory::ApplyROMFixes (void) Timings.IRQPendCount = 2; printf("IRQ count hack: %d\n", Timings.IRQPendCount); } + + if (match_na("BATTLE BLAZE")) + { + Timings.IRQPendCount = 1; + printf("IRQ count hack: %d\n", Timings.IRQPendCount); + } } if (!Settings.DisableGameSpecificHacks) diff --git a/source/snes9x/memmap.h b/source/snes9x/memmap.h index 88c34f8..ac02b82 100644 --- a/source/snes9x/memmap.h +++ b/source/snes9x/memmap.h @@ -242,7 +242,6 @@ struct CMemory uint8 *WriteMap[MEMMAP_NUM_BLOCKS]; uint8 BlockIsRAM[MEMMAP_NUM_BLOCKS]; uint8 BlockIsROM[MEMMAP_NUM_BLOCKS]; - uint8 MemorySpeed[MEMMAP_NUM_BLOCKS]; uint8 ExtendedFormat; char ROMFilename[PATH_MAX + 1]; @@ -290,8 +289,6 @@ struct CMemory char * SafeANK (const char *); void ParseSNESHeader (uint8 *); void InitROM (void); - void FixROMSpeed (void); - void ResetSpeedMap (void); uint32 map_mirror (uint32, uint32); void map_lorom (uint32, uint32, uint32, uint32, uint32); diff --git a/source/snes9x/ppu.cpp b/source/snes9x/ppu.cpp index 6872f6b..e1d5c39 100644 --- a/source/snes9x/ppu.cpp +++ b/source/snes9x/ppu.cpp @@ -1565,8 +1565,6 @@ void S9xSetCPU (uint8 Byte, uint16 Address) { if (Address < 0x4200) { - CPU.Cycles += ONE_CYCLE; // XSlow - switch (Address) { case 0x4016: // JOYSER0 @@ -1851,8 +1849,6 @@ void S9xSetCPU (uint8 Byte, uint16 Address) } else CPU.FastROMSpeed = SLOW_ONE_CYCLE; - - Memory.FixROMSpeed(); } break; @@ -1911,8 +1907,6 @@ uint8 S9xGetCPU (uint16 Address) } #endif - CPU.Cycles += ONE_CYCLE; // XSlow - switch (Address) { case 0x4016: // JOYSER0 diff --git a/source/snes9x/sa1.cpp b/source/snes9x/sa1.cpp index 8ec6904..f8d8892 100644 --- a/source/snes9x/sa1.cpp +++ b/source/snes9x/sa1.cpp @@ -788,13 +788,41 @@ static void S9xSA1CharConv2 (void) uint32 offset = (SA1.in_char_dma & 7) ? 0 : 1; int depth = (Memory.FillRAM[0x2231] & 3) == 0 ? 8 : (Memory.FillRAM[0x2231] & 3) == 1 ? 4 : 2; int bytes_per_char = 8 * depth; - uint8 *p = &Memory.FillRAM[0x3000] + dest + offset * bytes_per_char; + uint8 *p = &Memory.FillRAM[0x3000] + (dest & 0x7ff) + offset * bytes_per_char; uint8 *q = &Memory.ROM[CMemory::MAX_ROM_SIZE - 0x10000] + offset * 64; switch (depth) { case 2: + for (int l = 0; l < 8; l++, q += 8) + { + for (int b = 0; b < 8; b++) + { + uint8 r = *(q + b); + *(p + 0) = (*(p + 0) << 1) | ((r >> 0) & 1); + *(p + 1) = (*(p + 1) << 1) | ((r >> 1) & 1); + } + + p += 2; + } + + break; + case 4: + for (int l = 0; l < 8; l++, q += 8) + { + for (int b = 0; b < 8; b++) + { + uint8 r = *(q + b); + *(p + 0) = (*(p + 0) << 1) | ((r >> 0) & 1); + *(p + 1) = (*(p + 1) << 1) | ((r >> 1) & 1); + *(p + 16) = (*(p + 16) << 1) | ((r >> 2) & 1); + *(p + 17) = (*(p + 17) << 1) | ((r >> 3) & 1); + } + + p += 2; + } + break; case 8: diff --git a/source/snes9x/screenshot.cpp b/source/snes9x/screenshot.cpp index 6c0aec7..ec8f750 100644 --- a/source/snes9x/screenshot.cpp +++ b/source/snes9x/screenshot.cpp @@ -209,7 +209,7 @@ bool8 S9xDoScreenshot (int width, int height) if (!png_ptr) { fclose(fp); - unlink(fname); + remove(fname); S9xMessage(S9X_ERROR, 0, "Failed to take screenshot."); return (FALSE); } @@ -219,7 +219,7 @@ bool8 S9xDoScreenshot (int width, int height) { png_destroy_write_struct(&png_ptr, (png_infopp) NULL); fclose(fp); - unlink(fname); + remove(fname); S9xMessage(S9X_ERROR, 0, "Failed to take screenshot."); return (FALSE); } @@ -228,7 +228,7 @@ bool8 S9xDoScreenshot (int width, int height) { png_destroy_write_struct(&png_ptr, &info_ptr); fclose(fp); - unlink(fname); + remove(fname); S9xMessage(S9X_ERROR, 0, "Failed to take screenshot."); return (FALSE); } diff --git a/source/snes9x/snapshot.cpp b/source/snes9x/snapshot.cpp index ae6396e..2f18685 100644 --- a/source/snes9x/snapshot.cpp +++ b/source/snes9x/snapshot.cpp @@ -1692,7 +1692,6 @@ int S9xUnfreezeFromStream (STREAM stream) S9xSetPCBase(Registers.PBPC); S9xUnpackStatus(); S9xFixCycles(); - Memory.FixROMSpeed(); for (int d = 0; d < 8; d++) DMA[d] = dma_snap.dma[d]; diff --git a/source/snes9x/snes9x.h b/source/snes9x/snes9x.h index 2b75bd7..692d01a 100644 --- a/source/snes9x/snes9x.h +++ b/source/snes9x/snes9x.h @@ -179,13 +179,15 @@ #define _SNES9X_H_ #ifndef VERSION -#define VERSION "1.52" +#define VERSION "1.53" #endif #include "port.h" #include "65c816.h" #include "messages.h" +#define S9X_ACCURACY_LEVEL 3 + #ifdef ZLIB #include #define STREAM gzFile