├── LICENSE ├── Makefile ├── Makefile.3ds ├── Makefile.switch ├── README.md ├── icon ├── icon.jpg └── icon.png ├── romfs ├── file-dark.bmp ├── file-light.bmp ├── folder-dark.bmp ├── folder-light.bmp └── font.bmp └── src ├── 3ds ├── main.cpp └── mutex.cpp ├── apu.cpp ├── apu.h ├── config.cpp ├── config.h ├── core.cpp ├── core.h ├── cpu.cpp ├── cpu.h ├── desktop ├── main.cpp └── mutex.cpp ├── mapper.cpp ├── mapper.h ├── mutex.h ├── ppu.cpp ├── ppu.h └── switch ├── main.cpp ├── mutex.cpp ├── ui.cpp └── ui.h /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | NAME := noies 2 | SOURCES := src src/desktop 3 | LIBS := -lpthread -lglut -lGL -lportaudio 4 | 5 | CPPFILES := $(foreach dir,$(SOURCES),$(wildcard $(dir)/*.cpp)) 6 | HFILES := $(foreach dir,$(SOURCES),$(wildcard $(dir)/*.h)) 7 | 8 | $(NAME): $(CPPFILES) $(HFILES) 9 | g++ $(LIBS) -o $@ $(CPPFILES) 10 | 11 | clean: 12 | rm -f $(NAME) 13 | -------------------------------------------------------------------------------- /Makefile.3ds: -------------------------------------------------------------------------------- 1 | #--------------------------------------------------------------------------------- 2 | .SUFFIXES: 3 | #--------------------------------------------------------------------------------- 4 | 5 | ifeq ($(strip $(DEVKITARM)),) 6 | $(error "Please set DEVKITARM in your environment. export DEVKITARM=devkitARM") 7 | endif 8 | 9 | TOPDIR ?= $(CURDIR) 10 | include $(DEVKITARM)/3ds_rules 11 | 12 | #--------------------------------------------------------------------------------- 13 | # TARGET is the name of the output 14 | # BUILD is the directory where object files & intermediate files will be placed 15 | # SOURCES is a list of directories containing source code 16 | # DATA is a list of directories containing data files 17 | # INCLUDES is a list of directories containing header files 18 | # 19 | # NO_SMDH: if set to anything, no SMDH file is generated. 20 | # APP_TITLE is the name of the app stored in the SMDH file (Optional) 21 | # APP_DESCRIPTION is the description of the app stored in the SMDH file (Optional) 22 | # APP_AUTHOR is the author of the app stored in the SMDH file (Optional) 23 | # ICON is the filename of the icon (.png), relative to the project folder. 24 | # If not set, it attempts to use one of the following (in this order): 25 | # - .png 26 | # - icon.png 27 | # - /default_icon.png 28 | #--------------------------------------------------------------------------------- 29 | TARGET := noies 30 | BUILD := build_3ds 31 | SOURCES := src src/3ds 32 | DATA := data 33 | INCLUDES := src src/3ds 34 | GRAPHICS := gfx 35 | 36 | APP_TITLE := NoiES 37 | APP_DESCRIPTION := A noice NES emulator 38 | APP_AUTHOR := Hydr8gon 39 | ICON := icon/icon.png 40 | 41 | #--------------------------------------------------------------------------------- 42 | # options for code generation 43 | #--------------------------------------------------------------------------------- 44 | ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard 45 | 46 | CFLAGS := -g -Wall -Ofast -mword-relocations \ 47 | -fomit-frame-pointer \ 48 | $(ARCH) 49 | 50 | CFLAGS += $(INCLUDE) -DARM11 -D_3DS 51 | 52 | CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11 53 | 54 | ASFLAGS := -g $(ARCH) 55 | LDFLAGS = -specs=3dsx.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) 56 | 57 | LIBS := -lctru -lm 58 | 59 | #--------------------------------------------------------------------------------- 60 | # list of directories containing libraries, this must be the top level containing 61 | # include and lib 62 | #--------------------------------------------------------------------------------- 63 | LIBDIRS := $(CTRULIB) 64 | 65 | #--------------------------------------------------------------------------------- 66 | # no real need to edit anything past this point unless you need to add additional 67 | # rules for different file extensions 68 | #--------------------------------------------------------------------------------- 69 | ifneq ($(BUILD),$(notdir $(CURDIR))) 70 | #--------------------------------------------------------------------------------- 71 | 72 | export OUTPUT := $(CURDIR)/$(TARGET) 73 | export TOPDIR := $(CURDIR) 74 | 75 | export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ 76 | $(foreach dir,$(DATA),$(CURDIR)/$(dir)) \ 77 | $(foreach dir,$(GRAPHICS),$(CURDIR)/$(dir)) 78 | 79 | export DEPSDIR := $(CURDIR)/$(BUILD) 80 | 81 | CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) 82 | CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) 83 | SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) 84 | BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) 85 | PNGFILES := $(foreach dir,$(GRAPHICS),$(notdir $(wildcard $(dir)/*.png))) 86 | 87 | #--------------------------------------------------------------------------------- 88 | # use CXX for linking C++ projects, CC for standard C 89 | #--------------------------------------------------------------------------------- 90 | ifeq ($(strip $(CPPFILES)),) 91 | #--------------------------------------------------------------------------------- 92 | export LD := $(CC) 93 | #--------------------------------------------------------------------------------- 94 | else 95 | #--------------------------------------------------------------------------------- 96 | export LD := $(CXX) 97 | #--------------------------------------------------------------------------------- 98 | endif 99 | #--------------------------------------------------------------------------------- 100 | 101 | export OFILES_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) 102 | 103 | export OFILES_BIN := $(addsuffix .o,$(BINFILES)) \ 104 | $(PNGFILES:.png=.bgr.o) \ 105 | 106 | export OFILES := $(OFILES_BIN) $(OFILES_SOURCES) 107 | 108 | export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES))) $(PNGFILES:.png=_bgr.h) 109 | 110 | export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ 111 | $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ 112 | -I$(CURDIR)/$(BUILD) 113 | 114 | export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) 115 | 116 | ifeq ($(strip $(ICON)),) 117 | icons := $(wildcard *.png) 118 | ifneq (,$(findstring $(TARGET).png,$(icons))) 119 | export APP_ICON := $(TOPDIR)/$(TARGET).png 120 | else 121 | ifneq (,$(findstring icon.png,$(icons))) 122 | export APP_ICON := $(TOPDIR)/icon.png 123 | endif 124 | endif 125 | else 126 | export APP_ICON := $(TOPDIR)/$(ICON) 127 | endif 128 | 129 | IMAGEMAGICK := $(shell which convert) 130 | 131 | ifeq ($(strip $(NO_SMDH)),) 132 | export _3DSXFLAGS += --smdh=$(CURDIR)/$(TARGET).smdh 133 | endif 134 | 135 | .PHONY: $(BUILD) clean all 136 | 137 | #--------------------------------------------------------------------------------- 138 | ifneq ($(strip $(IMAGEMAGICK)),) 139 | ifeq ($(findstring System32,$(IMAGEMAGICK)),) 140 | 141 | HAVE_CONVERT := yes 142 | 143 | endif 144 | endif 145 | 146 | ifeq ($(strip $(HAVE_CONVERT)),yes) 147 | 148 | all: $(BUILD) 149 | 150 | else 151 | 152 | all: 153 | @echo "Image Magick not found!" 154 | @echo 155 | @echo "Please install Image Magick from http://www.imagemagick.org/ to build this example" 156 | 157 | endif 158 | 159 | #--------------------------------------------------------------------------------- 160 | $(BUILD): 161 | @[ -d $@ ] || mkdir -p $@ 162 | @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile.3ds 163 | 164 | #--------------------------------------------------------------------------------- 165 | clean: 166 | @echo clean ... 167 | @rm -fr $(BUILD) $(TARGET).3dsx $(OUTPUT).smdh $(TARGET).elf 168 | 169 | 170 | #--------------------------------------------------------------------------------- 171 | else 172 | 173 | DEPENDS := $(OFILES:.o=.d) 174 | 175 | #--------------------------------------------------------------------------------- 176 | # main targets 177 | #--------------------------------------------------------------------------------- 178 | ifeq ($(strip $(NO_SMDH)),) 179 | $(OUTPUT).3dsx : $(OUTPUT).elf $(OUTPUT).smdh 180 | else 181 | $(OUTPUT).3dsx : $(OUTPUT).elf 182 | endif 183 | 184 | $(OFILES_SOURCES) : $(HFILES) 185 | 186 | $(OUTPUT).elf : $(OFILES) 187 | 188 | #--------------------------------------------------------------------------------- 189 | # you need a rule like this for each extension you use as binary data 190 | #--------------------------------------------------------------------------------- 191 | %.bin.o : %.bin 192 | #--------------------------------------------------------------------------------- 193 | @echo $(notdir $<) 194 | @$(bin2o) 195 | 196 | 197 | 198 | #--------------------------------------------------------------------------------- 199 | %_bgr.h %.bgr.o: %.bgr 200 | #--------------------------------------------------------------------------------- 201 | @echo $(notdir $<) 202 | @$(bin2o) 203 | 204 | #--------------------------------------------------------------------------------- 205 | %.bgr: %.png 206 | #--------------------------------------------------------------------------------- 207 | @echo $(notdir $<) 208 | @convert $< -rotate 90 $@ 209 | 210 | -include $(DEPENDS) 211 | 212 | #--------------------------------------------------------------------------------------- 213 | endif 214 | #--------------------------------------------------------------------------------------- 215 | -------------------------------------------------------------------------------- /Makefile.switch: -------------------------------------------------------------------------------- 1 | #--------------------------------------------------------------------------------- 2 | .SUFFIXES: 3 | #--------------------------------------------------------------------------------- 4 | 5 | ifeq ($(strip $(DEVKITPRO)),) 6 | $(error "Please set DEVKITPRO in your environment. export DEVKITPRO=/devkitpro") 7 | endif 8 | 9 | TOPDIR ?= $(CURDIR) 10 | include $(DEVKITPRO)/libnx/switch_rules 11 | 12 | #--------------------------------------------------------------------------------- 13 | # TARGET is the name of the output 14 | # BUILD is the directory where object files & intermediate files will be placed 15 | # SOURCES is a list of directories containing source code 16 | # DATA is a list of directories containing data files 17 | # INCLUDES is a list of directories containing header files 18 | # ROMFS is the directory containing data to be added to RomFS, relative to the Makefile (Optional) 19 | # 20 | # NO_ICON: if set to anything, do not use icon. 21 | # NO_NACP: if set to anything, no .nacp file is generated. 22 | # APP_TITLE is the name of the app stored in the .nacp file (Optional) 23 | # APP_AUTHOR is the author of the app stored in the .nacp file (Optional) 24 | # APP_VERSION is the version of the app stored in the .nacp file (Optional) 25 | # APP_TITLEID is the titleID of the app stored in the .nacp file (Optional) 26 | # ICON is the filename of the icon (.jpg), relative to the project folder. 27 | # If not set, it attempts to use one of the following (in this order): 28 | # - .jpg 29 | # - icon.jpg 30 | # - /default_icon.jpg 31 | # 32 | # CONFIG_JSON is the filename of the NPDM config file (.json), relative to the project folder. 33 | # If not set, it attempts to use one of the following (in this order): 34 | # - .json 35 | # - config.json 36 | # If a JSON file is provided or autodetected, an ExeFS PFS0 (.nsp) is built instead 37 | # of a homebrew executable (.nro). This is intended to be used for sysmodules. 38 | # NACP building is skipped as well. 39 | #--------------------------------------------------------------------------------- 40 | TARGET := noies 41 | BUILD := build_switch 42 | SOURCES := src src/switch 43 | DATA := data 44 | INCLUDES := src src/switch 45 | ROMFS := romfs 46 | 47 | APP_TITLE := NoiES 48 | APP_AUTHOR := Hydr8gon 49 | APP_VERSION := 1.2 50 | ICON := icon/icon.jpg 51 | 52 | #--------------------------------------------------------------------------------- 53 | # options for code generation 54 | #--------------------------------------------------------------------------------- 55 | ARCH := -march=armv8-a+crc+crypto -mtune=cortex-a57 -mtp=soft -fPIE 56 | 57 | CFLAGS := -g -Wall -O2 -ffunction-sections \ 58 | $(ARCH) $(DEFINES) 59 | 60 | CFLAGS += $(INCLUDE) -D__SWITCH__ 61 | 62 | CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions 63 | 64 | ASFLAGS := -g $(ARCH) 65 | LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) 66 | 67 | LIBS := -lglad -lEGL -lglapi -ldrm_nouveau -lnx 68 | 69 | #--------------------------------------------------------------------------------- 70 | # list of directories containing libraries, this must be the top level containing 71 | # include and lib 72 | #--------------------------------------------------------------------------------- 73 | LIBDIRS := $(PORTLIBS) $(LIBNX) 74 | 75 | 76 | #--------------------------------------------------------------------------------- 77 | # no real need to edit anything past this point unless you need to add additional 78 | # rules for different file extensions 79 | #--------------------------------------------------------------------------------- 80 | ifneq ($(BUILD),$(notdir $(CURDIR))) 81 | #--------------------------------------------------------------------------------- 82 | 83 | export OUTPUT := $(CURDIR)/$(TARGET) 84 | export TOPDIR := $(CURDIR) 85 | 86 | export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ 87 | $(foreach dir,$(DATA),$(CURDIR)/$(dir)) 88 | 89 | export DEPSDIR := $(CURDIR)/$(BUILD) 90 | 91 | CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) 92 | CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) 93 | SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) 94 | BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) 95 | 96 | #--------------------------------------------------------------------------------- 97 | # use CXX for linking C++ projects, CC for standard C 98 | #--------------------------------------------------------------------------------- 99 | ifeq ($(strip $(CPPFILES)),) 100 | #--------------------------------------------------------------------------------- 101 | export LD := $(CC) 102 | #--------------------------------------------------------------------------------- 103 | else 104 | #--------------------------------------------------------------------------------- 105 | export LD := $(CXX) 106 | #--------------------------------------------------------------------------------- 107 | endif 108 | #--------------------------------------------------------------------------------- 109 | 110 | export OFILES_BIN := $(addsuffix .o,$(BINFILES)) 111 | export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) 112 | export OFILES := $(OFILES_BIN) $(OFILES_SRC) 113 | export HFILES_BIN := $(addsuffix .h,$(subst .,_,$(BINFILES))) 114 | 115 | export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ 116 | $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ 117 | -I$(CURDIR)/$(BUILD) 118 | 119 | export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) 120 | 121 | ifeq ($(strip $(CONFIG_JSON)),) 122 | jsons := $(wildcard *.json) 123 | ifneq (,$(findstring $(TARGET).json,$(jsons))) 124 | export APP_JSON := $(TOPDIR)/$(TARGET).json 125 | else 126 | ifneq (,$(findstring config.json,$(jsons))) 127 | export APP_JSON := $(TOPDIR)/config.json 128 | endif 129 | endif 130 | else 131 | export APP_JSON := $(TOPDIR)/$(CONFIG_JSON) 132 | endif 133 | 134 | ifeq ($(strip $(ICON)),) 135 | icons := $(wildcard *.jpg) 136 | ifneq (,$(findstring $(TARGET).jpg,$(icons))) 137 | export APP_ICON := $(TOPDIR)/$(TARGET).jpg 138 | else 139 | ifneq (,$(findstring icon.jpg,$(icons))) 140 | export APP_ICON := $(TOPDIR)/icon.jpg 141 | endif 142 | endif 143 | else 144 | export APP_ICON := $(TOPDIR)/$(ICON) 145 | endif 146 | 147 | ifeq ($(strip $(NO_ICON)),) 148 | export NROFLAGS += --icon=$(APP_ICON) 149 | endif 150 | 151 | ifeq ($(strip $(NO_NACP)),) 152 | export NROFLAGS += --nacp=$(CURDIR)/$(TARGET).nacp 153 | endif 154 | 155 | ifneq ($(APP_TITLEID),) 156 | export NACPFLAGS += --titleid=$(APP_TITLEID) 157 | endif 158 | 159 | ifneq ($(ROMFS),) 160 | export NROFLAGS += --romfsdir=$(CURDIR)/$(ROMFS) 161 | endif 162 | 163 | .PHONY: $(BUILD) clean all 164 | 165 | #--------------------------------------------------------------------------------- 166 | all: $(BUILD) 167 | 168 | $(BUILD): 169 | @[ -d $@ ] || mkdir -p $@ 170 | @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile.switch 171 | 172 | #--------------------------------------------------------------------------------- 173 | clean: 174 | @echo clean ... 175 | ifeq ($(strip $(APP_JSON)),) 176 | @rm -fr $(BUILD) $(TARGET).nro $(TARGET).nacp $(TARGET).elf 177 | else 178 | @rm -fr $(BUILD) $(TARGET).nsp $(TARGET).nso $(TARGET).npdm $(TARGET).elf 179 | endif 180 | 181 | 182 | #--------------------------------------------------------------------------------- 183 | else 184 | .PHONY: all 185 | 186 | DEPENDS := $(OFILES:.o=.d) 187 | 188 | #--------------------------------------------------------------------------------- 189 | # main targets 190 | #--------------------------------------------------------------------------------- 191 | ifeq ($(strip $(APP_JSON)),) 192 | 193 | all : $(OUTPUT).nro 194 | 195 | ifeq ($(strip $(NO_NACP)),) 196 | $(OUTPUT).nro : $(OUTPUT).elf $(OUTPUT).nacp 197 | else 198 | $(OUTPUT).nro : $(OUTPUT).elf 199 | endif 200 | 201 | else 202 | 203 | all : $(OUTPUT).nsp 204 | 205 | $(OUTPUT).nsp : $(OUTPUT).nso $(OUTPUT).npdm 206 | 207 | $(OUTPUT).nso : $(OUTPUT).elf 208 | 209 | endif 210 | 211 | $(OUTPUT).elf : $(OFILES) 212 | 213 | $(OFILES_SRC) : $(HFILES_BIN) 214 | 215 | #--------------------------------------------------------------------------------- 216 | # you need a rule like this for each extension you use as binary data 217 | #--------------------------------------------------------------------------------- 218 | %.bin.o %_bin.h : %.bin 219 | #--------------------------------------------------------------------------------- 220 | @echo $(notdir $<) 221 | @$(bin2o) 222 | 223 | -include $(DEPENDS) 224 | 225 | #--------------------------------------------------------------------------------------- 226 | endif 227 | #--------------------------------------------------------------------------------------- 228 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NoiES 2 | A "noice" NES emulator. 3 | 4 | Written to dive deeper into emulator programming. 5 | -------------------------------------------------------------------------------- /icon/icon.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hydr8gon/NoiES/3fbc4166dff28aa579bff8f053a47262cf724d21/icon/icon.jpg -------------------------------------------------------------------------------- /icon/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hydr8gon/NoiES/3fbc4166dff28aa579bff8f053a47262cf724d21/icon/icon.png -------------------------------------------------------------------------------- /romfs/file-dark.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hydr8gon/NoiES/3fbc4166dff28aa579bff8f053a47262cf724d21/romfs/file-dark.bmp -------------------------------------------------------------------------------- /romfs/file-light.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hydr8gon/NoiES/3fbc4166dff28aa579bff8f053a47262cf724d21/romfs/file-light.bmp -------------------------------------------------------------------------------- /romfs/folder-dark.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hydr8gon/NoiES/3fbc4166dff28aa579bff8f053a47262cf724d21/romfs/folder-dark.bmp -------------------------------------------------------------------------------- /romfs/folder-light.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hydr8gon/NoiES/3fbc4166dff28aa579bff8f053a47262cf724d21/romfs/folder-light.bmp -------------------------------------------------------------------------------- /romfs/font.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hydr8gon/NoiES/3fbc4166dff28aa579bff8f053a47262cf724d21/romfs/font.bmp -------------------------------------------------------------------------------- /src/3ds/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #include <3ds.h> 21 | #include 22 | 23 | #include "../core.h" 24 | #include "../ppu.h" 25 | #include "../apu.h" 26 | #include "../config.h" 27 | #include "../mutex.h" 28 | 29 | bool running = true; 30 | bool requestSave, requestLoad; 31 | 32 | u32 cropOverscan = 0; 33 | u32 keyMap[] = { KEY_A, KEY_B, KEY_SELECT, KEY_START, KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_L, KEY_R, KEY_TOUCH }; 34 | string romPath = "sdmc:/3ds/noies/game.nes"; 35 | 36 | const vector platformSettings = 37 | { 38 | { "cropOverscan", &cropOverscan, false }, 39 | { "keyA", &keyMap[0], false }, 40 | { "keyB", &keyMap[1], false }, 41 | { "keySelect", &keyMap[2], false }, 42 | { "keyStart", &keyMap[3], false }, 43 | { "keyUp", &keyMap[4], false }, 44 | { "keyDown", &keyMap[5], false }, 45 | { "keyLeft", &keyMap[6], false }, 46 | { "keyRight", &keyMap[7], false }, 47 | { "keySave", &keyMap[8], false }, 48 | { "keyLoad", &keyMap[9], false }, 49 | { "keyExit", &keyMap[10], false }, 50 | { "romPath", &romPath, true } 51 | }; 52 | 53 | void runCore(void *args) 54 | { 55 | while (running) 56 | { 57 | core::runCycle(); 58 | 59 | if (requestSave) 60 | { 61 | core::saveState(); 62 | requestSave = false; 63 | } 64 | else if (requestLoad) 65 | { 66 | core::loadState(); 67 | requestLoad = false; 68 | } 69 | } 70 | } 71 | 72 | int main(int argc, char **argv) 73 | { 74 | gfxInitDefault(); 75 | gfxSetDoubleBuffering(GFX_TOP, false); 76 | u8 *framebuffer = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL); 77 | consoleInit(GFX_BOTTOM, NULL); 78 | 79 | config::load(platformSettings); 80 | 81 | if (core::loadRom(romPath) != 0) 82 | { 83 | printf("The current ROM path is: %s\n", romPath.c_str()); 84 | printf("Press any button to exit.\n"); 85 | u32 pressed; 86 | while (!pressed) 87 | { 88 | hidScanInput(); 89 | pressed = hidKeysDown(); 90 | } 91 | config::save(); 92 | return 1; 93 | } 94 | 95 | u8 model; 96 | cfguInit(); 97 | CFGU_GetSystemModel(&model); 98 | cfguExit(); 99 | 100 | ndspInit(); 101 | ndspSetOutputMode(NDSP_OUTPUT_STEREO); 102 | ndspChnSetFormat(0, NDSP_FORMAT_STEREO_PCM16); 103 | ndspChnSetInterp(0, NDSP_INTERP_LINEAR); 104 | ndspChnSetRate(0, 48000); 105 | float mix[] = { 1.0f, 1.0f }; 106 | ndspChnSetMix(0, mix); 107 | 108 | u8 currentBuf = 0; 109 | ndspWaveBuf waveBuffers[2]; 110 | for (int i = 0; i < 2; i++) 111 | { 112 | memset(&waveBuffers[i], 0, sizeof(waveBuffers[i])); 113 | waveBuffers[i].data_vaddr = linearAlloc(1600 * 2 * sizeof(s16)); 114 | waveBuffers[i].nsamples = 1600; 115 | ndspChnWaveBufAdd(0, &waveBuffers[i]); 116 | } 117 | 118 | Thread core; 119 | if (model > 1 && model != 3) 120 | { 121 | ptmSysmInit(); 122 | PTMSYSM_ConfigureNew3DSCPU(0x03); 123 | ptmSysmExit(); 124 | core = threadCreate(runCore, NULL, 0x8000, 0x30, 2, false); 125 | } 126 | else 127 | { 128 | APT_SetAppCpuTimeLimit(30); 129 | core = threadCreate(runCore, NULL, 0x8000, 0x30, 1, false); 130 | } 131 | 132 | while (aptMainLoop()) 133 | { 134 | hidScanInput(); 135 | u32 pressed = hidKeysDown(); 136 | u32 released = hidKeysUp(); 137 | 138 | for (int i = 0; i < 8; i++) 139 | { 140 | if (pressed & keyMap[i]) 141 | core::pressKey(0, i); 142 | else if (released & keyMap[i]) 143 | core::releaseKey(0, i); 144 | } 145 | 146 | if (pressed & keyMap[8]) // Save state 147 | requestSave = true; 148 | else if (pressed & keyMap[9]) // Load state 149 | requestLoad = true; 150 | else if (pressed & keyMap[10]) // Exit 151 | break; 152 | 153 | if (waveBuffers[currentBuf].status == NDSP_WBUF_DONE) 154 | { 155 | for (unsigned int i = 0; i < waveBuffers[currentBuf].nsamples; i++) 156 | { 157 | s16 sample = apu::audioSample(2.3f); 158 | waveBuffers[currentBuf].data_pcm16[i * 2] = sample; 159 | waveBuffers[currentBuf].data_pcm16[i * 2 + 1] = sample; 160 | } 161 | ndspChnWaveBufAdd(0, &waveBuffers[currentBuf]); 162 | currentBuf = !currentBuf; 163 | } 164 | 165 | mutex::lock(ppu::displayMutex); 166 | for (int y = (cropOverscan ? 8 : 0); y < (cropOverscan ? 232 : 240); y++) 167 | { 168 | for (int x = 0; x < 256; x++) 169 | { 170 | framebuffer[((x + 72) * 240 + 239 - y) * 3] = ppu::displayBuffer[y * 256 + x] >> 8; 171 | framebuffer[((x + 72) * 240 + 239 - y) * 3 + 1] = ppu::displayBuffer[y * 256 + x] >> 16; 172 | framebuffer[((x + 72) * 240 + 239 - y) * 3 + 2] = ppu::displayBuffer[y * 256 + x] >> 24; 173 | } 174 | } 175 | mutex::unlock(ppu::displayMutex); 176 | 177 | gfxFlushBuffers(); 178 | gfxSwapBuffers(); 179 | gspWaitForVBlank(); 180 | } 181 | 182 | running = false; 183 | threadJoin(core, U64_MAX); 184 | threadFree(core); 185 | config::save(); 186 | ndspExit(); 187 | gfxExit(); 188 | return 0; 189 | } 190 | -------------------------------------------------------------------------------- /src/3ds/mutex.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #include <3ds.h> 21 | 22 | namespace mutex 23 | { 24 | 25 | void *create() 26 | { 27 | Handle *mutex = new Handle; 28 | svcCreateMutex(mutex, false); 29 | return mutex; 30 | } 31 | 32 | void lock(void *mutex) 33 | { 34 | svcWaitSynchronization(*(Handle*)mutex, U64_MAX); 35 | } 36 | 37 | void unlock(void *mutex) 38 | { 39 | svcReleaseMutex(*(Handle*)mutex); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/apu.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | #include "core.h" 24 | #include "cpu.h" 25 | 26 | namespace apu 27 | { 28 | 29 | float pulseWaves[2]; 30 | uint16_t pulseFreqs[2]; 31 | uint16_t pulseBaseFreqs[2]; 32 | uint8_t pulseLengths[2]; 33 | uint8_t pulseEnvPeriods[2]; 34 | uint8_t pulseEnvDividers[2]; 35 | uint8_t pulseEnvDecays[2]; 36 | uint8_t sweepPeriods[2]; 37 | uint8_t sweepDividers[2]; 38 | uint8_t sweepShifts[2]; 39 | uint8_t dutyCycles[2]; 40 | uint8_t pulseFlags[2]; 41 | 42 | float triangleWave; 43 | uint16_t triangleFreq; 44 | uint16_t triangleBaseFreq; 45 | uint8_t triangleLength; 46 | uint8_t linearCounter; 47 | uint8_t linearReload; 48 | uint8_t triangleFlags; 49 | 50 | float noiseWave; 51 | uint16_t noisePeriod; 52 | uint16_t noiseShift; 53 | uint8_t noiseLength; 54 | uint8_t noiseEnvPeriod; 55 | uint8_t noiseEnvDivider; 56 | uint8_t noiseEnvDecay; 57 | uint8_t noiseFlags; 58 | 59 | uint16_t frameCounter; 60 | uint8_t frameCounterFlags; 61 | uint8_t status; 62 | 63 | const uint8_t noteLengths[] = 64 | { 65 | 10, 254, 20, 2, 40, 4, 80, 6, 160, 8, 60, 10, 14, 12, 26, 14, 66 | 12, 16, 24, 18, 48, 20, 96, 22, 192, 24, 72, 26, 16, 28, 32, 30 67 | }; 68 | 69 | const uint8_t triangleSteps[] = 70 | { 71 | 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 72 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 73 | }; 74 | 75 | const uint16_t noisePeriods[] = 76 | { 77 | 4, 8, 16, 32, 64, 96, 128, 160, 202, 254, 380, 508, 762, 1016, 2034, 4068 78 | }; 79 | 80 | const vector stateItems = 81 | { 82 | { pulseWaves, sizeof(pulseWaves) }, 83 | { pulseFreqs, sizeof(pulseFreqs) }, 84 | { pulseBaseFreqs, sizeof(pulseBaseFreqs) }, 85 | { pulseLengths, sizeof(pulseLengths) }, 86 | { pulseEnvPeriods, sizeof(pulseEnvPeriods) }, 87 | { pulseEnvDividers, sizeof(pulseEnvDividers) }, 88 | { pulseEnvDecays, sizeof(pulseEnvDecays) }, 89 | { sweepPeriods, sizeof(sweepPeriods) }, 90 | { sweepDividers, sizeof(sweepDividers) }, 91 | { sweepShifts, sizeof(sweepShifts) }, 92 | { dutyCycles, sizeof(dutyCycles) }, 93 | { pulseFlags, sizeof(pulseFlags) }, 94 | { &triangleWave, sizeof(triangleWave) }, 95 | { &triangleFreq, sizeof(triangleFreq) }, 96 | { &triangleBaseFreq, sizeof(triangleBaseFreq) }, 97 | { &triangleLength, sizeof(triangleLength) }, 98 | { &linearCounter, sizeof(linearCounter) }, 99 | { &linearReload, sizeof(linearReload) }, 100 | { &triangleFlags, sizeof(triangleFlags) }, 101 | { &noiseWave, sizeof(noiseWave) }, 102 | { &noisePeriod, sizeof(noisePeriod) }, 103 | { &noiseShift, sizeof(noiseShift) }, 104 | { &noiseLength, sizeof(noiseLength) }, 105 | { &noiseEnvPeriod, sizeof(noiseEnvPeriod) }, 106 | { &noiseEnvDivider, sizeof(noiseEnvDivider) }, 107 | { &noiseEnvDecay, sizeof(noiseEnvDecay) }, 108 | { &noiseFlags, sizeof(noiseFlags) }, 109 | { &frameCounter, sizeof(frameCounter) }, 110 | { &frameCounterFlags, sizeof(frameCounterFlags) }, 111 | { &status, sizeof(status) } 112 | }; 113 | 114 | int16_t audioSample(float pitch) 115 | { 116 | int16_t out = 0; 117 | 118 | // Generate the pulse waves 119 | for (int i = 0; i < 2; i++) 120 | { 121 | pulseWaves[i] += pitch; 122 | if (pulseWaves[i] >= pulseFreqs[i]) 123 | pulseWaves[i] = 0; 124 | 125 | if ((dutyCycles[i] == 0 && pulseWaves[i] < pulseFreqs[i] / 8) || 126 | (dutyCycles[i] == 1 && pulseWaves[i] < pulseFreqs[i] / 4) || 127 | (dutyCycles[i] == 2 && pulseWaves[i] < pulseFreqs[i] / 2) || 128 | (dutyCycles[i] == 3 && pulseWaves[i] >= pulseFreqs[i] / 4)) 129 | out += 0x200 * ((pulseFlags[i] & 0x10) ? pulseEnvPeriods[i] : pulseEnvDecays[i]); 130 | } 131 | 132 | // Generate the triangle wave 133 | if (triangleLength != 0 && linearCounter != 0) 134 | triangleWave += pitch / 2; 135 | if (triangleWave >= triangleFreq + 1) 136 | { 137 | triangleWave = 0; 138 | triangleFreq = triangleBaseFreq; 139 | } 140 | uint8_t step = (triangleWave / (triangleFreq + 1)) * 32; 141 | out += 0x243 * triangleSteps[step]; 142 | 143 | // Generate the noise channel's pseudo-random 1-bit noise 144 | noiseWave += pitch; 145 | if (noiseWave >= noisePeriod) 146 | { 147 | uint8_t bit = (noiseFlags & 0x80) ? (noiseShift & 0x40) >> 6 : (noiseShift & 0x02) >> 1; 148 | noiseShift = (noiseShift >> 1) | (((noiseShift & 0x01) ^ bit) << 14); 149 | noiseWave = 0; 150 | } 151 | if (!(noiseShift & 0x01) && noiseLength != 0) 152 | out += 0x150 * ((noiseFlags & 0x10) ? noiseEnvPeriod : noiseEnvDecay); 153 | 154 | return out; 155 | } 156 | 157 | void reset() 158 | { 159 | // Clear the state items 160 | for (unsigned int i = 0; i < stateItems.size(); i++) 161 | memset(stateItems[i].pointer, 0, stateItems[i].size); 162 | 163 | // Set default values 164 | noiseShift = 1; 165 | } 166 | 167 | void quarterFrame() 168 | { 169 | // Clock the pulse envelopes 170 | for (int i = 0; i < 2; i++) 171 | { 172 | if (pulseFlags[i] & 0x01) 173 | { 174 | // Reload the divider and decay values 175 | pulseEnvDividers[i] = pulseEnvPeriods[i]; 176 | pulseEnvDecays[i] = 0x0F; 177 | pulseFlags[i] &= ~0x01; 178 | } 179 | else 180 | { 181 | // Clock the dividers 182 | if (pulseEnvDividers[i] == 0) 183 | { 184 | // Decay and reload the divider 185 | if (pulseEnvDecays[i] != 0) 186 | pulseEnvDecays[i]--; 187 | else if (pulseFlags[i] & 0x20) // Loop flag 188 | pulseEnvDecays[i] = 0x0F; 189 | pulseEnvDividers[i] = pulseEnvPeriods[i]; 190 | } 191 | else 192 | { 193 | pulseEnvDividers[i]--; 194 | } 195 | } 196 | } 197 | 198 | // Clock the triangle linear counter 199 | if (triangleFlags & 0x01) // Linear counter reload 200 | linearCounter = linearReload; 201 | else if (linearCounter != 0) 202 | linearCounter--; 203 | if (!(triangleFlags & 0x80)) // Control flag 204 | triangleFlags &= ~0x01; 205 | 206 | // Clock the noise envelope 207 | if (noiseFlags & 0x01) 208 | { 209 | // Reload the divider and decay values 210 | noiseEnvDivider = noiseEnvPeriod; 211 | noiseEnvDecay = 0x0F; 212 | noiseFlags &= ~0x01; 213 | } 214 | else 215 | { 216 | // Clock the dividers 217 | if (noiseEnvDivider == 0) 218 | { 219 | // Decay and reload the divider 220 | if (noiseEnvDecay != 0) 221 | noiseEnvDecay--; 222 | else if (noiseFlags & 0x20) // Loop flag 223 | noiseEnvDecay = 0x0F; 224 | noiseEnvDivider = noiseEnvPeriod; 225 | } 226 | else 227 | { 228 | noiseEnvDivider--; 229 | } 230 | } 231 | } 232 | 233 | void halfFrame() 234 | { 235 | // Clock the pulse length counters and sweeps 236 | for (int i = 0; i < 2; i++) 237 | { 238 | // Clock the length counters if they're not halted 239 | if (!(pulseFlags[i] & 0x20) && pulseLengths[i] != 0) 240 | pulseLengths[i]--; 241 | 242 | // Clock the sweep dividers 243 | if (sweepDividers[i] == 0 || (pulseFlags[i] & 0x02)) 244 | { 245 | // Sweep the frequencies if sweeps are enabled 246 | if (sweepDividers[i] == 0 && (pulseFlags[i] & 0x80)) 247 | { 248 | int16_t sweep = pulseBaseFreqs[i] >> sweepShifts[i]; 249 | if (pulseFlags[i] & 0x08) // Negation 250 | sweep -= 2 * sweep + !i; 251 | pulseFreqs[i] += sweep; 252 | } 253 | 254 | sweepDividers[i] = sweepPeriods[i]; 255 | pulseFlags[i] &= ~0x02; 256 | } 257 | else 258 | { 259 | sweepDividers[i]--; 260 | } 261 | } 262 | 263 | // Clock the triangle length counter if it's not halted 264 | if (!(triangleFlags & 0x80) && triangleLength != 0) 265 | triangleLength--; 266 | 267 | // Clock the noise length counter if it's not halted 268 | if (!(noiseFlags & 0x20) && noiseLength != 0) 269 | noiseLength--; 270 | } 271 | 272 | void runCycle() 273 | { 274 | // Only run on an APU cycle (6 global cycles) 275 | if (core::globalCycles % 6 != 0) 276 | return; 277 | 278 | // Advance the frame counter 279 | frameCounter++; 280 | if (frameCounter == 3729 || frameCounter == 7457 || frameCounter == 11186 || 281 | frameCounter == ((frameCounterFlags & 0x80) ? 18641 : 14915)) 282 | { 283 | quarterFrame(); 284 | if (frameCounter != 3729 && frameCounter != 11186) 285 | halfFrame(); 286 | 287 | // Trigger an optional IRQ at the end of the 4-step sequence 288 | if (frameCounter == 14915 && !(frameCounterFlags & 0x40)) 289 | { 290 | status |= 0x40; 291 | cpu::interrupts[2] = true; 292 | } 293 | 294 | if (frameCounter == 14915 || frameCounter == 18641) 295 | frameCounter = 0; 296 | } 297 | 298 | // Check if either of the pulse channels should be silenced 299 | for (int i = 0; i < 2; i++) 300 | { 301 | if (!(status & (1 << i))) 302 | pulseLengths[i] = 0; 303 | if (pulseFreqs[i] < 8 || pulseFreqs[i] > 0x7FF || pulseLengths[i] == 0) 304 | pulseFreqs[i] = 0; 305 | } 306 | 307 | // Check if the triangle channel should be silenced 308 | if (!(status & 0x04)) 309 | triangleLength = 0; 310 | 311 | // Check if the noise channel should be silenced 312 | if (!(status & 0x08)) 313 | noiseLength = 0; 314 | } 315 | 316 | uint8_t registerRead(uint16_t address) 317 | { 318 | uint8_t value = 0; 319 | 320 | switch (address) 321 | { 322 | case 0x4015: // APU status 323 | // Set bits if the corresponding length counters are greater than 0 324 | value = status & 0xC0; 325 | for (int i = 0; i < 2; i++) 326 | value |= (pulseLengths[i] > 0) << i; 327 | value |= (triangleLength > 0) << 2; 328 | value |= (noiseLength > 0) << 3; 329 | status &= ~0x40; 330 | break; 331 | } 332 | 333 | return value; 334 | } 335 | 336 | void registerWrite(uint16_t address, uint8_t value) 337 | { 338 | int i = (address - 0x4000) / 4; 339 | 340 | switch (address) 341 | { 342 | case 0x4000: case 0x4004: // Pulse channels 343 | dutyCycles[i] = (value & 0xC0) >> 6; 344 | pulseFlags[i] = (pulseFlags[i] & ~0x20) | (value & 0x20); // Length counter halt 345 | pulseFlags[i] = (pulseFlags[i] & ~0x10) | (value & 0x10); // Constant volume 346 | pulseEnvPeriods[i] = value & 0x0F; 347 | break; 348 | 349 | case 0x4001: case 0x4005: // Pulse channels 350 | sweepPeriods[i] = (value & 0x70) >> 4; 351 | pulseFlags[i] = (pulseFlags[i] & ~0x80) | (value & 0x80); // Sweep enable 352 | pulseFlags[i] = (pulseFlags[i] & ~0x08) | (value & 0x08); // Sweep negate 353 | sweepShifts[i] = value & 0x07; 354 | pulseFlags[i] |= 0x02; // Sweep reload 355 | break; 356 | 357 | case 0x4002: case 0x4006: // Pulse channels 358 | pulseFreqs[i] = pulseBaseFreqs[i] = (pulseBaseFreqs[i] & 0x700) | value; 359 | break; 360 | 361 | case 0x4003: case 0x4007: // Pulse channels 362 | pulseLengths[i] = noteLengths[(value & 0xF8) >> 3]; 363 | pulseFreqs[i] = pulseBaseFreqs[i] = ((value & 0x07) << 8) | (pulseBaseFreqs[i] & 0x0FF); 364 | pulseWaves[i] = 0; 365 | pulseFlags[i] |= 0x01; // Envelope reload 366 | break; 367 | 368 | case 0x4008: // Triangle channel 369 | triangleFlags = (triangleFlags & ~0x80) | (value & 0x80); // Length counter halt 370 | linearReload = value & 0x7F; 371 | triangleFlags |= 0x01; // Linear counter reload 372 | break; 373 | 374 | case 0x400A: // Triangle channel 375 | triangleBaseFreq = (triangleBaseFreq & 0x700) | value; 376 | break; 377 | 378 | case 0x400B: // Triangle channel 379 | triangleLength = noteLengths[(value & 0xF8) >> 3]; 380 | triangleBaseFreq = ((value & 0x07) << 8) | (triangleBaseFreq & 0x0FF); 381 | triangleFlags |= 0x01; // Linear counter reload 382 | break; 383 | 384 | case 0x400C: // Noise channel 385 | noiseFlags = (noiseFlags & ~0x20) | (value & 0x20); // Length counter halt 386 | noiseFlags = (noiseFlags & ~0x10) | (value & 0x10); // Constant volume 387 | noiseEnvPeriod = value & 0x0F; 388 | break; 389 | 390 | case 0x400E: // Noise channel 391 | noiseFlags = (noiseFlags & ~0x80) | (value & 0x80); // Feedback mode 392 | noisePeriod = noisePeriods[value & 0x0F]; 393 | break; 394 | 395 | case 0x400F: // Noise channel 396 | noiseLength = noteLengths[(value & 0xF8) >> 3]; 397 | noiseFlags |= 0x01; // Envelope reload 398 | break; 399 | 400 | case 0x4015: // Status 401 | // Set the channel enable bits without clearing the interrupt bits 402 | status = (status & 0xC0) | (value & 0x1F); 403 | break; 404 | 405 | case 0x4017: // Frame counter 406 | frameCounterFlags = value; 407 | if (frameCounterFlags & 0x40) // Interrupt inhibit 408 | status &= ~0x40; // Frame interrupt 409 | frameCounter = 0; 410 | break; 411 | } 412 | } 413 | 414 | void saveState(FILE *state) 415 | { 416 | for (unsigned int i = 0; i < stateItems.size(); i++) 417 | fwrite(stateItems[i].pointer, 1, stateItems[i].size, state); 418 | } 419 | 420 | void loadState(FILE *state) 421 | { 422 | for (unsigned int i = 0; i < stateItems.size(); i++) 423 | fread(stateItems[i].pointer, 1, stateItems[i].size, state); 424 | } 425 | 426 | } 427 | -------------------------------------------------------------------------------- /src/apu.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #ifndef APU_H 21 | #define APU_H 22 | 23 | namespace apu 24 | { 25 | 26 | int16_t audioSample(float pitch); 27 | 28 | void reset(); 29 | void runCycle(); 30 | 31 | uint8_t registerRead(uint16_t address); 32 | void registerWrite(uint16_t address, uint8_t value); 33 | 34 | void saveState(FILE *state); 35 | void loadState(FILE *state); 36 | 37 | } 38 | 39 | #endif // APU_H 40 | -------------------------------------------------------------------------------- /src/config.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include "config.h" 25 | 26 | namespace config 27 | { 28 | 29 | uint32_t frameLimiter = 1; 30 | uint32_t disableSpriteLimit = 0; 31 | 32 | vector settings = 33 | { 34 | { "frameLimiter", &frameLimiter, false }, 35 | { "disableSpriteLimit", &disableSpriteLimit, false } 36 | }; 37 | 38 | void load(vector platformSettings) 39 | { 40 | // Include any platform-specific settings 41 | settings.insert(settings.end(), platformSettings.begin(), platformSettings.end()); 42 | 43 | FILE *config = fopen("noies.ini", "r"); 44 | if (!config) 45 | return; 46 | 47 | // Search for setting names in the config file and load their values when found 48 | char read[256]; 49 | while (fgets(read, 256, config) != NULL) 50 | { 51 | string line = read; 52 | for (unsigned int i = 0; i < settings.size(); i++) 53 | { 54 | int split = line.rfind("="); 55 | string name = line.substr(0, split); 56 | if (name == settings[i].name) 57 | { 58 | string value = line.substr(split + 1, line.size() - split - 2); 59 | if (settings[i].isString) 60 | *((string*)settings[i].value) = value; 61 | else if (value[0] >= 0x30 && value[0] <= 0x39) 62 | *((uint32_t*)settings[i].value) = stoi(value); 63 | } 64 | } 65 | } 66 | 67 | fclose(config); 68 | } 69 | 70 | void save() 71 | { 72 | FILE *config = fopen("noies.ini", "w"); 73 | 74 | // Save all setting names and values to the config file 75 | for (unsigned int i = 0; i < settings.size(); i++) 76 | { 77 | string value; 78 | if (settings[i].isString) 79 | value = *(string*)settings[i].value; 80 | else 81 | value = to_string(*(uint32_t*)settings[i].value).c_str(); 82 | fputs((settings[i].name + '=' + value + '\n').c_str(), config); 83 | } 84 | 85 | fclose(config); 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /src/config.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #ifndef CONFIG_H 21 | #define CONFIG_H 22 | 23 | #include 24 | #include 25 | 26 | using namespace std; 27 | 28 | namespace config 29 | { 30 | 31 | typedef struct 32 | { 33 | string name; 34 | void *value; 35 | bool isString; 36 | } Setting; 37 | 38 | extern uint32_t frameLimiter; 39 | extern uint32_t disableSpriteLimit; 40 | 41 | void load(vector platformSettings); 42 | void save(); 43 | 44 | } 45 | 46 | #endif // CONFIG_H 47 | -------------------------------------------------------------------------------- /src/core.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | #include "core.h" 24 | #include "cpu.h" 25 | #include "ppu.h" 26 | #include "apu.h" 27 | #include "mapper.h" 28 | 29 | namespace core 30 | { 31 | 32 | uint8_t globalCycles; 33 | 34 | string romName; 35 | bool hasBattery; 36 | 37 | int loadRom(string filename) 38 | { 39 | // Open the file 40 | FILE *file = fopen(filename.c_str(), "rb"); 41 | if (!file) 42 | { 43 | printf("Failed to open ROM!\n"); 44 | return 1; 45 | } 46 | 47 | // Read the file header 48 | uint8_t header[0x10]; 49 | fread(header, 1, 0x10, file); 50 | 51 | // Verify that the file is an NES ROM 52 | char filetype[4]; 53 | memcpy(filetype, header, 3); 54 | filetype[3] = '\0'; 55 | if (strcmp(filetype, "NES") != 0 || header[3] != 0x1A) 56 | { 57 | printf("Invalid ROM format!\n"); 58 | return 2; 59 | } 60 | 61 | romName = filename.substr(0, filename.rfind(".")); 62 | 63 | // Reset the system 64 | cpu::reset(); 65 | ppu::reset(); 66 | apu::reset(); 67 | globalCycles = 0; 68 | 69 | // Load the trainer into memory if the ROM has one 70 | if (header[6] & 0x04) 71 | fread(&cpu::memory[0x7000], 1, 0x200, file); 72 | 73 | // Initialize the ROM mapper 74 | ppu::mirrorMode = (header[6] & 0x08) ? 4 : 3 - (header[6] & 0x01); 75 | uint8_t mapperType = header[7] | (header[6] >> 4); 76 | if (!mapper::load(file, header[4], mapperType)) 77 | { 78 | printf("Unknown mapper type: %d\n", mapperType); 79 | return mapperType; 80 | } 81 | 82 | // Attempt to load a savefile if the ROM has battery-backed SRAM 83 | if (header[6] & 0x02) 84 | { 85 | hasBattery = true; 86 | FILE *save = fopen((romName + ".sav").c_str(), "rb"); 87 | if (save) 88 | { 89 | fread(&cpu::memory[0x6000], 1, 0x2000, save); 90 | fclose(save); 91 | } 92 | } 93 | 94 | return 0; 95 | } 96 | 97 | void closeRom() 98 | { 99 | // Write a savefile if the ROM has battery-backed SRAM 100 | if (hasBattery) 101 | { 102 | FILE *save = fopen((romName + ".sav").c_str(), "wb"); 103 | fwrite(&cpu::memory[0x6000], 1, 0x2000, save); 104 | fclose(save); 105 | } 106 | } 107 | 108 | void runCycle() 109 | { 110 | // Run a global cycle 111 | cpu::runCycle(); 112 | ppu::runCycle(); 113 | apu::runCycle(); 114 | ++globalCycles %= 6; 115 | } 116 | 117 | void pressKey(uint8_t pad, uint8_t key) 118 | { 119 | // Set the bit corresponding to the pressed key 120 | cpu::inputMasks[pad] |= 1 << key; 121 | } 122 | 123 | void releaseKey(uint8_t pad, uint8_t key) 124 | { 125 | // Clear the bit corresponding to the released key 126 | cpu::inputMasks[pad] &= ~(1 << key); 127 | } 128 | 129 | void saveState() 130 | { 131 | // Write everything to a state file 132 | FILE *state = fopen((romName + ".noi").c_str(), "wb"); 133 | cpu::saveState(state); 134 | ppu::saveState(state); 135 | apu::saveState(state); 136 | mapper::saveState(state); 137 | fwrite(&globalCycles, 1, sizeof(globalCycles), state); 138 | fclose(state); 139 | } 140 | 141 | void loadState() 142 | { 143 | // Read everything from a state file if it exists 144 | FILE *state = fopen((romName + ".noi").c_str(), "rb"); 145 | if (state) 146 | { 147 | cpu::loadState(state); 148 | ppu::loadState(state); 149 | apu::loadState(state); 150 | mapper::loadState(state); 151 | fread(&globalCycles, 1, sizeof(globalCycles), state); 152 | fclose(state); 153 | } 154 | } 155 | 156 | } 157 | -------------------------------------------------------------------------------- /src/core.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #ifndef CORE_H 21 | #define CORE_H 22 | 23 | #include 24 | #include 25 | 26 | using namespace std; 27 | 28 | namespace core 29 | { 30 | 31 | typedef struct 32 | { 33 | void *pointer; 34 | uint32_t size; 35 | } StateItem; 36 | 37 | extern uint8_t globalCycles; 38 | 39 | int loadRom(string filename); 40 | void closeRom(); 41 | 42 | void runCycle(); 43 | 44 | void pressKey(uint8_t pad, uint8_t key); 45 | void releaseKey(uint8_t pad, uint8_t key); 46 | 47 | void saveState(); 48 | void loadState(); 49 | 50 | } 51 | 52 | #endif // CORE_H 53 | -------------------------------------------------------------------------------- /src/cpu.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | #include "core.h" 24 | #include "ppu.h" 25 | #include "apu.h" 26 | #include "mapper.h" 27 | 28 | namespace cpu 29 | { 30 | 31 | uint8_t memory[0x10000]; 32 | 33 | uint16_t cycles, targetCycles; 34 | uint16_t programCounter; 35 | uint8_t accumulator, registerX, registerY; 36 | uint8_t flags; // NVBBDIZC 37 | uint8_t stackPointer; 38 | bool interrupts[3]; // NMI, RST, IRQ 39 | 40 | uint8_t inputMasks[2]; 41 | uint8_t inputShifts[2]; 42 | 43 | const vector stateItems = 44 | { 45 | { memory, sizeof(memory) }, 46 | { &cycles, sizeof(cycles) }, 47 | { &targetCycles, sizeof(targetCycles) }, 48 | { &programCounter, sizeof(programCounter) }, 49 | { &accumulator, sizeof(accumulator) }, 50 | { ®isterX, sizeof(registerX) }, 51 | { ®isterY, sizeof(registerY) }, 52 | { &flags, sizeof(flags) }, 53 | { &stackPointer, sizeof(stackPointer) }, 54 | { interrupts, sizeof(interrupts) }, 55 | { inputShifts, sizeof(inputShifts) } 56 | }; 57 | 58 | void reset() 59 | { 60 | // Clear the state items 61 | for (unsigned int i = 0; i < stateItems.size(); i++) 62 | memset(stateItems[i].pointer, 0, stateItems[i].size); 63 | 64 | // Set default values 65 | flags = 0x24; 66 | stackPointer = 0xFF; 67 | interrupts[1] = true; 68 | inputMasks[0] = 0; 69 | inputMasks[1] = 0; 70 | } 71 | 72 | uint8_t memoryRead(uint8_t *src) 73 | { 74 | // Just read the value if it's not in memory; it's probably a register 75 | if (src < memory || src > memory + sizeof(memory)) 76 | return *src; 77 | 78 | uint16_t address = src - memory; 79 | 80 | // Get the real location of a mirrored address in memory 81 | if (address >= 0x0800 && address < 0x2000) 82 | address %= 0x0800; 83 | else if (address >= 0x2008 && address < 0x4000) 84 | address = 0x2000 + address % 8; 85 | 86 | if (address == 0x4016 || address == 0x4017) // JOYPAD1 or JOYPAD2 87 | { 88 | // Read button status 1 bit at a time 89 | uint8_t i = address - 0x4016; 90 | uint8_t value = (inputMasks[i] & (1 << inputShifts[i])) ? 0x41 : 0x40; 91 | ++inputShifts[i] %= 8; 92 | return value; 93 | } 94 | 95 | // Get a value from a memory-mapped register if needed 96 | if ((address >= 0x2000 && address < 0x2008) || address == 0x4014) 97 | return ppu::registerRead(address); 98 | else if (address >= 0x4000 && address < 0x4018) 99 | return apu::registerRead(address); 100 | else 101 | return memory[address]; 102 | } 103 | 104 | void memoryWrite(uint8_t *dst, uint8_t src) 105 | { 106 | // Just write the value if it's not in memory; it's probably a register 107 | if (dst < memory || dst > memory + sizeof(memory)) 108 | { 109 | *dst = src; 110 | return; 111 | } 112 | 113 | uint16_t address = dst - memory; 114 | 115 | // Get the real location of a mirrored address in memory 116 | if (address >= 0x0800 && address < 0x2000) 117 | address %= 0x0800; 118 | else if (address >= 0x2008 && address < 0x4000) 119 | address = 0x2000 + address % 8; 120 | 121 | if (address == 0x4016) // JOYPAD1 122 | { 123 | // Reset the input shifts when the strobe bit is set 124 | if (src & 0x01) 125 | inputShifts[0] = inputShifts[1] = 0; 126 | return; 127 | } 128 | 129 | // Pass the value to a memory-mapped register if needed 130 | if ((address >= 0x2000 && address < 0x2008) || address == 0x4014) 131 | ppu::registerWrite(address, src); 132 | else if (address >= 0x4000 && address < 0x4018) 133 | apu::registerWrite(address, src); 134 | else if (address >= 0x8000) 135 | mapper::registerWrite(address, src); 136 | else 137 | memory[address] = src; 138 | 139 | // Suspend the CPU on a DMA transfer with an extra cycle on odd CPU cycles 140 | if (address == 0x4014) 141 | targetCycles += (core::globalCycles == 3) ? 514 : 513; 142 | } 143 | 144 | uint8_t *zeroPage() 145 | { 146 | // Use the immediate value as a memory address 147 | programCounter++; 148 | return &memory[memory[programCounter]]; 149 | } 150 | 151 | uint8_t *zeroPageX() 152 | { 153 | // Use the immediate value plus the X register as a memory address in zero page 154 | programCounter++; 155 | return &memory[(memory[programCounter] + registerX) % 0x100]; 156 | } 157 | 158 | uint8_t *zeroPageY() 159 | { 160 | // Use the immediate value plus the Y register as a memory address in zero page 161 | programCounter++; 162 | return &memory[(memory[programCounter] + registerY) % 0x100]; 163 | } 164 | 165 | uint8_t *absolute() 166 | { 167 | // Use the immediate 2 values as a memory address 168 | programCounter += 2; 169 | return &memory[memory[programCounter - 1] | (memory[programCounter] << 8)]; 170 | } 171 | 172 | uint8_t *absoluteX(bool pageCycle) 173 | { 174 | // Use the absolute value plus the X register as a memory address 175 | programCounter += 2; 176 | uint16_t address = memory[programCounter - 1] | (memory[programCounter] << 8); 177 | if (pageCycle && (address & 0xFF) + registerX > 0xFF) // Page cross 178 | targetCycles++; 179 | return &memory[address + registerX]; 180 | } 181 | 182 | uint8_t *absoluteY(bool pageCycle) 183 | { 184 | // Use the absolute value plus the Y register as a memory address 185 | programCounter += 2; 186 | uint16_t address = memory[programCounter - 1] | (memory[programCounter] << 8); 187 | if (pageCycle && (address & 0xFF) + registerY > 0xFF) // Page cross 188 | targetCycles++; 189 | return &memory[address + registerY]; 190 | } 191 | 192 | uint8_t *indirect() 193 | { 194 | // Use the value stored at the absolute address as a memory address 195 | programCounter += 2; 196 | uint16_t addressLower = memory[programCounter - 1] | (memory[programCounter] << 8); 197 | uint16_t addressUpper = (addressLower & 0xFF00) | ((addressLower + 1) & 0x00FF); 198 | return &memory[(memory[addressUpper] << 8) | memory[addressLower]]; 199 | } 200 | 201 | uint8_t *indirectX() 202 | { 203 | // Use the value stored at the zero page X address as a memory address 204 | programCounter++; 205 | uint8_t addressLower = memory[(memory[programCounter] + registerX) % 0x100]; 206 | uint8_t addressUpper = memory[(memory[programCounter] + registerX + 1) % 0x100]; 207 | return &memory[(addressUpper << 8) | addressLower]; 208 | } 209 | 210 | uint8_t *indirectY(bool pageCycle) 211 | { 212 | // Use the value stored at the zero page address plus the Y register as a memory address 213 | programCounter++; 214 | uint8_t addressLower = memory[memory[programCounter]]; 215 | uint8_t addressUpper = memory[(memory[programCounter] + 1) % 0x100]; 216 | uint16_t address = (addressUpper << 8) | addressLower; 217 | if (pageCycle && (address & 0xFF) + registerY > 0xFF) // Page cross 218 | targetCycles++; 219 | return &memory[address + registerY]; 220 | } 221 | 222 | uint8_t *immediate() 223 | { 224 | // Get the value immediately after the current address 225 | programCounter++; 226 | return &memory[programCounter]; 227 | } 228 | 229 | void cl_(uint8_t flag) 230 | { 231 | // Clear a flag 232 | flags &= ~flag; 233 | } 234 | 235 | void se_(uint8_t flag) 236 | { 237 | // Set a flag 238 | flags |= flag; 239 | } 240 | 241 | void ph_(uint8_t src) 242 | { 243 | // Push a value to the stack 244 | memory[0x100 + stackPointer--] = src; 245 | } 246 | 247 | void pl_(uint8_t *dst) 248 | { 249 | // Pull a value from the stack 250 | *dst = memory[0x100 + ++stackPointer]; 251 | 252 | if (dst == &flags) 253 | { 254 | se_(0x20); // B 255 | cl_(0x10); // B 256 | return; 257 | } 258 | 259 | (*dst & 0x80) ? se_(0x80) : cl_(0x80); // N 260 | (*dst == 0) ? se_(0x02) : cl_(0x02); // Z 261 | } 262 | 263 | void adc(uint8_t *src) 264 | { 265 | // Add with carry 266 | uint8_t before = accumulator; 267 | uint8_t value = memoryRead(src); 268 | accumulator += value + (flags & 0x01); 269 | 270 | (accumulator & 0x80) ? se_(0x80) : cl_(0x80); // N 271 | (accumulator == 0) ? se_(0x02) : cl_(0x02); // Z 272 | ((value & 0x80) == (before & 0x80) && (flags & 0x80) != (value & 0x80)) ? se_(0x40) : cl_(0x40); // V 273 | (before > accumulator || value + (flags & 0x01) == 0x100) ? se_(0x01) : cl_(0x01); // C 274 | } 275 | 276 | void _and(uint8_t *src) 277 | { 278 | // Bitwise and 279 | accumulator &= memoryRead(src); 280 | 281 | (accumulator & 0x80) ? se_(0x80) : cl_(0x80); // N 282 | (accumulator == 0) ? se_(0x02) : cl_(0x02); // Z 283 | } 284 | 285 | void asl(uint8_t *dst) 286 | { 287 | // Arithmetic shift left 288 | uint8_t before = memoryRead(dst); 289 | uint8_t after = before << 1; 290 | memoryWrite(dst, after); 291 | 292 | (after & 0x80) ? se_(0x80) : cl_(0x80); // N 293 | (after == 0) ? se_(0x02) : cl_(0x02); // Z 294 | (before & 0x80) ? se_(0x01) : cl_(0x01); // C 295 | } 296 | 297 | void bit(uint8_t *src) 298 | { 299 | // Test bits 300 | uint8_t value = memoryRead(src); 301 | 302 | (value & 0x80) ? se_(0x80) : cl_(0x80); // N 303 | (value & 0x40) ? se_(0x40) : cl_(0x40); // V 304 | ((accumulator & value) == 0) ? se_(0x02) : cl_(0x02); // Z 305 | } 306 | 307 | void b__(bool condition) 308 | { 309 | // Branch on condition 310 | int8_t value = *immediate(); 311 | if (condition) 312 | { 313 | targetCycles++; 314 | uint16_t dst = programCounter + value + 1; 315 | if ((dst & 0xFF00) != ((programCounter + 1) & 0xFF00)) // Page cross 316 | targetCycles++; 317 | programCounter = dst - 1; 318 | } 319 | } 320 | 321 | void brk() 322 | { 323 | // Break 324 | programCounter += 2; 325 | ph_(programCounter >> 8); 326 | ph_(programCounter); 327 | se_(0x10); // B 328 | ph_(flags); 329 | cl_(0x10); // B 330 | se_(0x04); // I 331 | programCounter = ((memory[0xFFFF] << 8) | memory[0xFFFE]) - 1; 332 | } 333 | 334 | void cp_(uint8_t reg, uint8_t *src) 335 | { 336 | // Compare a register to a value 337 | uint8_t value = memoryRead(src); 338 | 339 | ((reg - value) & 0x80) ? se_(0x80) : cl_(0x80); // N 340 | (reg == value) ? se_(0x02) : cl_(0x02); // Z 341 | (reg >= value) ? se_(0x01) : cl_(0x01); // C 342 | } 343 | 344 | void de_(uint8_t *dst) 345 | { 346 | // Decrement a value 347 | uint8_t value = memoryRead(dst) - 1; 348 | memoryWrite(dst, value); 349 | 350 | (value & 0x80) ? se_(0x80) : cl_(0x80); // N 351 | (value == 0) ? se_(0x02) : cl_(0x02); // Z 352 | } 353 | 354 | void eor(uint8_t *src) 355 | { 356 | // Bitwise exclusive or 357 | accumulator ^= memoryRead(src); 358 | 359 | (accumulator & 0x80) ? se_(0x80) : cl_(0x80); // N 360 | (accumulator == 0) ? se_(0x02) : cl_(0x02); // Z 361 | } 362 | 363 | void in_(uint8_t *dst) 364 | { 365 | // Increment a value 366 | uint8_t value = memoryRead(dst) + 1; 367 | memoryWrite(dst, value); 368 | 369 | (value & 0x80) ? se_(0x80) : cl_(0x80); // N 370 | (value == 0) ? se_(0x02) : cl_(0x02); // Z 371 | } 372 | 373 | void jmp(uint8_t *location) 374 | { 375 | // Jump 376 | programCounter = location - memory - 1; 377 | } 378 | 379 | void jsr(uint8_t *location) 380 | { 381 | // Jump to subroutine 382 | ph_(programCounter >> 8); 383 | ph_(programCounter); 384 | jmp(location); 385 | } 386 | 387 | void ld_(uint8_t *reg, uint8_t *src) 388 | { 389 | // Load a register 390 | *reg = memoryRead(src); 391 | 392 | (*reg & 0x80) ? se_(0x80) : cl_(0x80); // N 393 | (*reg == 0) ? se_(0x02) : cl_(0x02); // Z 394 | } 395 | 396 | void lsr(uint8_t *dst) 397 | { 398 | // Logical shift right 399 | uint8_t before = memoryRead(dst); 400 | uint8_t after = before >> 1; 401 | memoryWrite(dst, after); 402 | 403 | (after & 0x80) ? se_(0x80) : cl_(0x80); // N 404 | (after == 0) ? se_(0x02) : cl_(0x02); // Z 405 | (before & 0x01) ? se_(0x01) : cl_(0x01); // C 406 | } 407 | 408 | void ora(uint8_t *src) 409 | { 410 | // Bitwise or 411 | accumulator |= memoryRead(src); 412 | 413 | (accumulator & 0x80) ? se_(0x80) : cl_(0x80); // N 414 | (accumulator == 0) ? se_(0x02) : cl_(0x02); // Z 415 | } 416 | 417 | void t__(uint8_t *src, uint8_t *dst) 418 | { 419 | // Transfer one register to another 420 | *dst = *src; 421 | 422 | // Don't set flags when transferring to the stack pointer 423 | if (dst == &stackPointer) 424 | return; 425 | 426 | (*dst & 0x80) ? se_(0x80) : cl_(0x80); // N 427 | (*dst == 0) ? se_(0x02) : cl_(0x02); // Z 428 | } 429 | 430 | void rol(uint8_t *dst) 431 | { 432 | // Rotate left 433 | uint8_t before = memoryRead(dst); 434 | uint8_t after = (before << 1) | (flags & 0x01); 435 | memoryWrite(dst, after); 436 | 437 | (after & 0x80) ? se_(0x80) : cl_(0x80); // N 438 | (after == 0) ? se_(0x02) : cl_(0x02); // Z 439 | (before & 0x80) ? se_(0x01) : cl_(0x01); // C 440 | } 441 | 442 | void ror(uint8_t *dst) 443 | { 444 | // Rotate right 445 | uint8_t before = memoryRead(dst); 446 | uint8_t after = (before >> 1) | ((flags & 0x01) << 7); 447 | memoryWrite(dst, after); 448 | 449 | (after & 0x80) ? se_(0x80) : cl_(0x80); // N 450 | (after == 0) ? se_(0x02) : cl_(0x02); // Z 451 | (before & 0x01) ? se_(0x01) : cl_(0x01); // C 452 | } 453 | 454 | void rts() 455 | { 456 | // Return from subroutine 457 | stackPointer += 2; 458 | programCounter = memory[0xFF + stackPointer] | (memory[0x100 + stackPointer] << 8); 459 | } 460 | 461 | void rti() 462 | { 463 | // Return from interrupt 464 | pl_(&flags); 465 | rts(); 466 | programCounter--; 467 | } 468 | 469 | void sbc(uint8_t *src) 470 | { 471 | // Subtract with carry 472 | uint8_t before = accumulator; 473 | uint8_t value = memoryRead(src); 474 | accumulator -= value + !(flags & 0x01); 475 | 476 | (accumulator & 0x80) ? se_(0x80) : cl_(0x80); // N 477 | (accumulator == 0) ? se_(0x02) : cl_(0x02); // Z 478 | ((value & 0x80) != (before & 0x80) && (flags & 0x80) == (value & 0x80)) ? se_(0x40) : cl_(0x40); // V 479 | (before >= accumulator && value + !(flags & 0x01) != 0x100) ? se_(0x01) : cl_(0x01); // C 480 | } 481 | 482 | void st_(uint8_t reg, uint8_t *dst) 483 | { 484 | // Store a register 485 | memoryWrite(dst, reg); 486 | } 487 | 488 | void ahx(uint8_t *dst) 489 | { 490 | // Store the bitwise and of the accumulator, the X register and the high byte of the address plus one 491 | memoryWrite(dst, accumulator & registerX & (memory[programCounter] + 1)); 492 | } 493 | 494 | void alr(uint8_t *src) 495 | { 496 | // Bitwise and and shift right 497 | _and(src); 498 | lsr(&accumulator); 499 | } 500 | 501 | void anc(uint8_t *src) 502 | { 503 | // Bitwise and and set carry flag 504 | _and(src); 505 | (accumulator & 0x80) ? se_(0x01) : cl_(0x01); // C 506 | } 507 | 508 | void arr(uint8_t *src) 509 | { 510 | // Bitwise and and rotate right 511 | _and(src); 512 | ror(&accumulator); 513 | 514 | ((accumulator & 0x40) ^ ((accumulator & 0x20) << 1)) ? se_(0x40) : cl_(0x40); // V 515 | (accumulator & 0x40) ? se_(0x01) : cl_(0x01); // C 516 | } 517 | 518 | void axs(uint8_t *src) 519 | { 520 | // Store the bitwise and with the X register minus a value in the X register 521 | registerX &= accumulator; 522 | uint8_t before = registerX; 523 | registerX -= *src; 524 | 525 | (registerX & 0x80) ? se_(0x80) : cl_(0x80); // N 526 | (registerX == 0) ? se_(0x02) : cl_(0x02); // Z 527 | (before >= registerX) ? se_(0x01) : cl_(0x01); // C 528 | } 529 | 530 | void dcp(uint8_t *src) 531 | { 532 | // Decrement and compare 533 | de_(src); 534 | cp_(accumulator, src); 535 | } 536 | 537 | void isc(uint8_t *src) 538 | { 539 | // Increment and subtract 540 | in_(src); 541 | sbc(src); 542 | } 543 | 544 | void las(uint8_t *src) 545 | { 546 | // Bitwise and with the stack pointer and load multiple registers 547 | uint8_t value = *src & stackPointer; 548 | accumulator = value; 549 | registerX = value; 550 | stackPointer = value; 551 | 552 | (value & 0x80) ? se_(0x80) : cl_(0x80); // N 553 | (value == 0) ? se_(0x02) : cl_(0x02); // Z 554 | } 555 | 556 | void lax(uint8_t *src) 557 | { 558 | // Load the accumulator and the X register 559 | ld_(&accumulator, src); 560 | ld_(®isterX, src); 561 | } 562 | 563 | void rla(uint8_t *src) 564 | { 565 | // Rotate left and bitwise and 566 | rol(src); 567 | _and(src); 568 | } 569 | 570 | void rra(uint8_t *src) 571 | { 572 | // Rotate right and add 573 | ror(src); 574 | adc(src); 575 | } 576 | 577 | void sax(uint8_t *dst) 578 | { 579 | // Bitwise and of the accumulator and the X register 580 | memoryWrite(dst, accumulator & registerX); 581 | } 582 | 583 | void shx(uint8_t *dst) 584 | { 585 | // Store the bitwise and of the X register and the high byte of the address plus one 586 | memoryWrite(dst, registerX & (memory[programCounter] + 1)); 587 | } 588 | 589 | void shy(uint8_t *dst) 590 | { 591 | // Store the bitwise and of the Y register and the high byte of the address plus one 592 | memoryWrite(dst, registerY & (memory[programCounter] + 1)); 593 | } 594 | 595 | void slo(uint8_t *src) 596 | { 597 | // Shift left and bitwise or 598 | asl(src); 599 | ora(src); 600 | } 601 | 602 | void sre(uint8_t *src) 603 | { 604 | // Shift right and bitwise exclusive or 605 | lsr(src); 606 | eor(src); 607 | } 608 | 609 | void tas(uint8_t *dst) 610 | { 611 | // Store the bitwise and of the accumulator, the X register and the high byte of the address plus one 612 | stackPointer = (accumulator & registerX); 613 | memoryWrite(dst, stackPointer & (memory[programCounter] + 1)); 614 | } 615 | 616 | void xaa(uint8_t *src) 617 | { 618 | // Transfer the X register to the accumulator and bitwise and 619 | t__(®isterX, &accumulator); 620 | _and(src); 621 | } 622 | 623 | void runCycle() 624 | { 625 | // Only run on a CPU cycle (3 global cycles) 626 | if (core::globalCycles % 3 != 0) 627 | return; 628 | 629 | // Wait until the previous instruction's cycles have finished 630 | if (++cycles < targetCycles) 631 | return; 632 | 633 | cycles = targetCycles = 0; 634 | 635 | // Disable IRQs if the inhibit flag is set 636 | if (interrupts[2] && (flags & 0x04)) 637 | interrupts[2] = false; 638 | 639 | // Handle interrupts 640 | for (int i = 0; i < 3; i++) 641 | { 642 | if (interrupts[i]) 643 | { 644 | ph_(programCounter >> 8); 645 | ph_(programCounter); 646 | ph_(flags); 647 | se_(0x04); // I 648 | programCounter = (memory[0xFFFB + i * 2] << 8) | memory[0xFFFA + i * 2]; 649 | targetCycles += 7; 650 | interrupts[i] = false; 651 | return; 652 | } 653 | } 654 | 655 | // Decode opcode 656 | switch (memory[programCounter]) 657 | { 658 | case 0x00: brk(); targetCycles += 7; break; // BRK 659 | case 0x04: zeroPage(); targetCycles += 3; break; // NOP d 660 | case 0x08: ph_(flags | 0x10); targetCycles += 3; break; // PHP 661 | case 0x0C: absolute(); targetCycles += 4; break; // NOP a 662 | case 0x10: b__(!(flags & 0x80)); targetCycles += 2; break; // BPL *+d 663 | case 0x14: zeroPageX(); targetCycles += 4; break; // NOP d,x 664 | case 0x18: cl_(0x01); targetCycles += 2; break; // CLC 665 | case 0x1C: absoluteX(true); targetCycles += 4; break; // NOP a,x 666 | 667 | case 0x01: ora(indirectX()); targetCycles += 6; break; // ORA (d,x) 668 | case 0x05: ora(zeroPage()); targetCycles += 3; break; // ORA d 669 | case 0x09: ora(immediate()); targetCycles += 2; break; // ORA #i 670 | case 0x0D: ora(absolute()); targetCycles += 4; break; // ORA a 671 | case 0x11: ora(indirectY(true)); targetCycles += 5; break; // ORA (d),y 672 | case 0x15: ora(zeroPageX()); targetCycles += 4; break; // ORA d,x 673 | case 0x19: ora(absoluteY(true)); targetCycles += 4; break; // ORA a,y 674 | case 0x1D: ora(absoluteX(true)); targetCycles += 4; break; // ORA a,x 675 | 676 | case 0x02: return; // STP 677 | case 0x06: asl(zeroPage()); targetCycles += 5; break; // ASL d 678 | case 0x0A: asl(&accumulator); targetCycles += 2; break; // ASL 679 | case 0x0E: asl(absolute()); targetCycles += 6; break; // ASL a 680 | case 0x12: return; // STP 681 | case 0x16: asl(zeroPageX()); targetCycles += 6; break; // ASL d,x 682 | case 0x1A: targetCycles += 2; break; // NOP 683 | case 0x1E: asl(absoluteX(false)); targetCycles += 7; break; // ASL a,x 684 | 685 | case 0x03: slo(indirectX()); targetCycles += 8; break; // SLO (d,x) 686 | case 0x07: slo(zeroPage()); targetCycles += 5; break; // SLO d 687 | case 0x0B: anc(immediate()); targetCycles += 2; break; // ANC #i 688 | case 0x0F: slo(absolute()); targetCycles += 6; break; // SLO a 689 | case 0x13: slo(indirectY(false)); targetCycles += 8; break; // SLO (d),y 690 | case 0x17: slo(zeroPageX()); targetCycles += 6; break; // SLO d,x 691 | case 0x1B: slo(absoluteY(false)); targetCycles += 7; break; // SLO a,y 692 | case 0x1F: slo(absoluteX(false)); targetCycles += 7; break; // SLO a,x 693 | 694 | case 0x20: jsr(absolute()); targetCycles += 6; break; // JSR a 695 | case 0x24: bit(zeroPage()); targetCycles += 3; break; // BIT d 696 | case 0x28: pl_(&flags); targetCycles += 4; break; // PLP 697 | case 0x2C: bit(absolute()); targetCycles += 4; break; // BIT a 698 | case 0x30: b__( (flags & 0x80)); targetCycles += 2; break; // BMI *+d 699 | case 0x34: zeroPageX(); targetCycles += 4; break; // NOP d,x 700 | case 0x38: se_(0x01); targetCycles += 2; break; // SEC 701 | case 0x3C: absoluteX(true); targetCycles += 4; break; // NOP a,x 702 | 703 | case 0x21: _and(indirectX()); targetCycles += 6; break; // AND (d,x) 704 | case 0x25: _and(zeroPage()); targetCycles += 3; break; // AND d 705 | case 0x29: _and(immediate()); targetCycles += 2; break; // AND #i 706 | case 0x2D: _and(absolute()); targetCycles += 4; break; // AND a 707 | case 0x31: _and(indirectY(true)); targetCycles += 5; break; // AND (d),y 708 | case 0x35: _and(zeroPageX()); targetCycles += 4; break; // AND d,x 709 | case 0x39: _and(absoluteY(true)); targetCycles += 4; break; // AND a,y 710 | case 0x3D: _and(absoluteX(true)); targetCycles += 4; break; // AND a,x 711 | 712 | case 0x22: return; // STP 713 | case 0x26: rol(zeroPage()); targetCycles += 5; break; // ROL d 714 | case 0x2A: rol(&accumulator); targetCycles += 2; break; // ROL 715 | case 0x2E: rol(absolute()); targetCycles += 6; break; // ROL a 716 | case 0x32: return; // STP 717 | case 0x36: rol(zeroPageX()); targetCycles += 6; break; // ROL d,x 718 | case 0x3A: targetCycles += 2; break; // NOP 719 | case 0x3E: rol(absoluteX(false)); targetCycles += 7; break; // ROL a,x 720 | 721 | case 0x23: rla(indirectX()); targetCycles += 8; break; // RLA (d,x) 722 | case 0x27: rla(zeroPage()); targetCycles += 5; break; // RLA d 723 | case 0x2B: anc(immediate()); targetCycles += 2; break; // ANC #i 724 | case 0x2F: rla(absolute()); targetCycles += 6; break; // RLA a 725 | case 0x33: rla(indirectY(false)); targetCycles += 8; break; // RLA (d),y 726 | case 0x37: rla(zeroPageX()); targetCycles += 6; break; // RLA d,x 727 | case 0x3B: rla(absoluteY(false)); targetCycles += 7; break; // RLA a,y 728 | case 0x3F: rla(absoluteX(false)); targetCycles += 7; break; // RLA a,x 729 | 730 | case 0x40: rti(); targetCycles += 6; break; // RTI 731 | case 0x44: zeroPage(); targetCycles += 3; break; // NOP d 732 | case 0x48: ph_(accumulator); targetCycles += 3; break; // PHA 733 | case 0x4C: jmp(absolute()); targetCycles += 3; break; // JMP a 734 | case 0x50: b__(!(flags & 0x40)); targetCycles += 2; break; // BVC *+d 735 | case 0x54: zeroPageX(); targetCycles += 4; break; // NOP d,x 736 | case 0x58: cl_(0x04); targetCycles += 2; break; // CLI 737 | case 0x5C: absoluteX(true); targetCycles += 4; break; // NOP a,x 738 | 739 | case 0x41: eor(indirectX()); targetCycles += 6; break; // EOR (d,x) 740 | case 0x45: eor(zeroPage()); targetCycles += 3; break; // EOR d 741 | case 0x49: eor(immediate()); targetCycles += 2; break; // EOR #i 742 | case 0x4D: eor(absolute()); targetCycles += 4; break; // EOR a 743 | case 0x51: eor(indirectY(true)); targetCycles += 5; break; // EOR (d),y 744 | case 0x55: eor(zeroPageX()); targetCycles += 4; break; // EOR d,x 745 | case 0x59: eor(absoluteY(true)); targetCycles += 4; break; // EOR a,y 746 | case 0x5D: eor(absoluteX(true)); targetCycles += 4; break; // EOR a,x 747 | 748 | case 0x42: return; // STP 749 | case 0x46: lsr(zeroPage()); targetCycles += 5; break; // LSR d 750 | case 0x4A: lsr(&accumulator); targetCycles += 2; break; // LSR 751 | case 0x4E: lsr(absolute()); targetCycles += 6; break; // LSR a 752 | case 0x52: return; // STP 753 | case 0x56: lsr(zeroPageX()); targetCycles += 6; break; // LSR d,x 754 | case 0x5A: targetCycles += 2; break; // NOP 755 | case 0x5E: lsr(absoluteX(false)); targetCycles += 7; break; // LSR a,x 756 | 757 | case 0x43: sre(indirectX()); targetCycles += 8; break; // SRE (d,x) 758 | case 0x47: sre(zeroPage()); targetCycles += 5; break; // SRE d 759 | case 0x4B: alr(immediate()); targetCycles += 2; break; // ALR #i 760 | case 0x4F: sre(absolute()); targetCycles += 6; break; // SRE a 761 | case 0x53: sre(indirectY(false)); targetCycles += 8; break; // SRE (d),y 762 | case 0x57: sre(zeroPageX()); targetCycles += 6; break; // SRE d,x 763 | case 0x5B: sre(absoluteY(false)); targetCycles += 7; break; // SRE a,y 764 | case 0x5F: sre(absoluteX(false)); targetCycles += 7; break; // SRE a,x 765 | 766 | case 0x60: rts(); targetCycles += 6; break; // RTS 767 | case 0x64: zeroPage(); targetCycles += 3; break; // NOP d 768 | case 0x68: pl_(&accumulator); targetCycles += 4; break; // PLA 769 | case 0x6C: jmp(indirect()); targetCycles += 5; break; // JMP (a) 770 | case 0x70: b__( (flags & 0x40)); targetCycles += 2; break; // BVS *+d 771 | case 0x74: zeroPageX(); targetCycles += 4; break; // NOP d,x 772 | case 0x78: se_(0x04); targetCycles += 2; break; // SEI 773 | case 0x7C: absoluteX(true); targetCycles += 4; break; // NOP a,x 774 | 775 | case 0x61: adc(indirectX()); targetCycles += 6; break; // ADC (d,x) 776 | case 0x65: adc(zeroPage()); targetCycles += 3; break; // ADC d 777 | case 0x69: adc(immediate()); targetCycles += 2; break; // ADC #i 778 | case 0x6D: adc(absolute()); targetCycles += 4; break; // ADC a 779 | case 0x71: adc(indirectY(true)); targetCycles += 5; break; // ADC (d),y 780 | case 0x75: adc(zeroPageX()); targetCycles += 4; break; // ADC d,x 781 | case 0x79: adc(absoluteY(true)); targetCycles += 4; break; // ADC a,y 782 | case 0x7D: adc(absoluteX(true)); targetCycles += 4; break; // ADC a,x 783 | 784 | case 0x62: return; // STP 785 | case 0x66: ror(zeroPage()); targetCycles += 5; break; // ROR d 786 | case 0x6A: ror(&accumulator); targetCycles += 2; break; // ROR 787 | case 0x6E: ror(absolute()); targetCycles += 6; break; // ROR a 788 | case 0x72: return; // STP 789 | case 0x76: ror(zeroPageX()); targetCycles += 6; break; // ROR d,x 790 | case 0x7A: targetCycles += 2; break; // NOP 791 | case 0x7E: ror(absoluteX(false)); targetCycles += 7; break; // ROR a,x 792 | 793 | case 0x63: rra(indirectX()); targetCycles += 8; break; // RRA (d,x) 794 | case 0x67: rra(zeroPage()); targetCycles += 5; break; // RRA d 795 | case 0x6B: arr(immediate()); targetCycles += 2; break; // ARR #i 796 | case 0x6F: rra(absolute()); targetCycles += 6; break; // RRA a 797 | case 0x73: rra(indirectY(false)); targetCycles += 8; break; // RRA (d),y 798 | case 0x77: rra(zeroPageX()); targetCycles += 6; break; // RRA d,x 799 | case 0x7B: rra(absoluteY(false)); targetCycles += 7; break; // RRA a,y 800 | case 0x7F: rra(absoluteX(false)); targetCycles += 7; break; // RRA a,x 801 | 802 | case 0x80: immediate(); targetCycles += 2; break; // NOP #i 803 | case 0x84: st_(registerY, zeroPage()); targetCycles += 3; break; // STY d 804 | case 0x88: de_(®isterY); targetCycles += 2; break; // DEY 805 | case 0x8C: st_(registerY, absolute()); targetCycles += 4; break; // STY a 806 | case 0x90: b__(!(flags & 0x01)); targetCycles += 2; break; // BCC *+d 807 | case 0x94: st_(registerY, zeroPageX()); targetCycles += 4; break; // STY d,x 808 | case 0x98: t__(®isterY, &accumulator); targetCycles += 2; break; // TYA 809 | case 0x9C: shy(absoluteX(false)); targetCycles += 5; break; // SHY a,x 810 | 811 | case 0x81: st_(accumulator, indirectX()); targetCycles += 6; break; // STA (d,x) 812 | case 0x85: st_(accumulator, zeroPage()); targetCycles += 3; break; // STA d 813 | case 0x89: immediate(); targetCycles += 2; break; // NOP #i 814 | case 0x8D: st_(accumulator, absolute()); targetCycles += 4; break; // STA a 815 | case 0x91: st_(accumulator, indirectY(false)); targetCycles += 6; break; // STA (d),y 816 | case 0x95: st_(accumulator, zeroPageX()); targetCycles += 4; break; // STA d,x 817 | case 0x99: st_(accumulator, absoluteY(false)); targetCycles += 5; break; // STA a,y 818 | case 0x9D: st_(accumulator, absoluteX(false)); targetCycles += 5; break; // STA a,x 819 | 820 | case 0x82: immediate(); targetCycles += 2; break; // NOP #i 821 | case 0x86: st_(registerX, zeroPage()); targetCycles += 3; break; // STX d 822 | case 0x8A: t__(®isterX, &accumulator); targetCycles += 2; break; // TXA 823 | case 0x8E: st_(registerX, absolute()); targetCycles += 4; break; // STX a 824 | case 0x92: return; // STP 825 | case 0x96: st_(registerX, zeroPageY()); targetCycles += 4; break; // STX d,y 826 | case 0x9A: t__(®isterX, &stackPointer); targetCycles += 2; break; // TXS 827 | case 0x9E: shx(absoluteY(false)); targetCycles += 5; break; // SHX a,y 828 | 829 | case 0x83: sax(indirectX()); targetCycles += 6; break; // SAX (d,x) 830 | case 0x87: sax(zeroPage()); targetCycles += 3; break; // SAX d 831 | case 0x8B: xaa(immediate()); targetCycles += 2; break; // XAA #i 832 | case 0x8F: sax(absolute()); targetCycles += 4; break; // SAX a 833 | case 0x93: ahx(indirectY(false)); targetCycles += 6; break; // AHX (d),y 834 | case 0x97: sax(zeroPageY()); targetCycles += 4; break; // SAX d,y 835 | case 0x9B: tas(absoluteY(false)); targetCycles += 5; break; // TAS a,y 836 | case 0x9F: ahx(absoluteY(false)); targetCycles += 5; break; // AHX a,y 837 | 838 | case 0xA0: ld_(®isterY, immediate()); targetCycles += 2; break; // LDY #i 839 | case 0xA4: ld_(®isterY, zeroPage()); targetCycles += 3; break; // LDY d 840 | case 0xA8: t__(&accumulator, ®isterY); targetCycles += 2; break; // TAY 841 | case 0xAC: ld_(®isterY, absolute()); targetCycles += 4; break; // LDY a 842 | case 0xB0: b__( (flags & 0x01)); targetCycles += 2; break; // BCS *+d 843 | case 0xB4: ld_(®isterY, zeroPageX()); targetCycles += 4; break; // LDY d,x 844 | case 0xBC: ld_(®isterY, absoluteX(true)); targetCycles += 4; break; // LDY a,x 845 | case 0xB8: cl_(0x40); targetCycles += 2; break; // CLV 846 | 847 | case 0xA1: ld_(&accumulator, indirectX()); targetCycles += 6; break; // LDA (d,x) 848 | case 0xA5: ld_(&accumulator, zeroPage()); targetCycles += 3; break; // LDA d 849 | case 0xA9: ld_(&accumulator, immediate()); targetCycles += 2; break; // LDA #i 850 | case 0xAD: ld_(&accumulator, absolute()); targetCycles += 4; break; // LDA a 851 | case 0xB1: ld_(&accumulator, indirectY(true)); targetCycles += 5; break; // LDA (d),y 852 | case 0xB5: ld_(&accumulator, zeroPageX()); targetCycles += 4; break; // LDA d,x 853 | case 0xB9: ld_(&accumulator, absoluteY(true)); targetCycles += 4; break; // LDA a,y 854 | case 0xBD: ld_(&accumulator, absoluteX(true)); targetCycles += 4; break; // LDA a,x 855 | 856 | case 0xA2: ld_(®isterX, immediate()); targetCycles += 2; break; // LDX #i 857 | case 0xA6: ld_(®isterX, zeroPage()); targetCycles += 3; break; // LDX d 858 | case 0xAA: t__(&accumulator, ®isterX); targetCycles += 2; break; // TAX 859 | case 0xAE: ld_(®isterX, absolute()); targetCycles += 4; break; // LDX a 860 | case 0xB2: return; // STP 861 | case 0xBA: t__(&stackPointer, ®isterX); targetCycles += 2; break; // TSX 862 | case 0xB6: ld_(®isterX, zeroPageY()); targetCycles += 4; break; // LDX d,y 863 | case 0xBE: ld_(®isterX, absoluteY(true)); targetCycles += 4; break; // LDX a,y 864 | 865 | case 0xA3: lax(indirectX()); targetCycles += 6; break; // LAX (d,x) 866 | case 0xA7: lax(zeroPage()); targetCycles += 3; break; // LAX d 867 | case 0xAB: lax(immediate()); targetCycles += 2; break; // LAX #i 868 | case 0xAF: lax(absolute()); targetCycles += 4; break; // LAX a 869 | case 0xB3: lax(indirectY(true)); targetCycles += 5; break; // LAX (d),y 870 | case 0xB7: lax(zeroPageY()); targetCycles += 4; break; // LAX d,y 871 | case 0xBB: las(absoluteY(true)); targetCycles += 4; break; // LAS a,y 872 | case 0xBF: lax(absoluteY(true)); targetCycles += 4; break; // LAX a,y 873 | 874 | case 0xC0: cp_(registerY, immediate()); targetCycles += 2; break; // CPY #i 875 | case 0xC4: cp_(registerY, zeroPage()); targetCycles += 3; break; // CPY d 876 | case 0xC8: in_(®isterY); targetCycles += 2; break; // INY 877 | case 0xCC: cp_(registerY, absolute()); targetCycles += 4; break; // CPY a 878 | case 0xD0: b__(!(flags & 0x02)); targetCycles += 2; break; // BNE *+d 879 | case 0xD4: zeroPageX(); targetCycles += 4; break; // NOP d,x 880 | case 0xD8: cl_(0x08); targetCycles += 2; break; // CLD 881 | case 0xDC: absoluteX(true); targetCycles += 4; break; // NOP a,x 882 | 883 | case 0xC1: cp_(accumulator, indirectX()); targetCycles += 6; break; // CMP (d,x) 884 | case 0xC5: cp_(accumulator, zeroPage()); targetCycles += 3; break; // CMP d 885 | case 0xC9: cp_(accumulator, immediate()); targetCycles += 2; break; // CMP #i 886 | case 0xCD: cp_(accumulator, absolute()); targetCycles += 4; break; // CMP a 887 | case 0xD1: cp_(accumulator, indirectY(true)); targetCycles += 5; break; // CMP (d),y 888 | case 0xD5: cp_(accumulator, zeroPageX()); targetCycles += 4; break; // CMP d,x 889 | case 0xD9: cp_(accumulator, absoluteY(true)); targetCycles += 4; break; // CMP a,y 890 | case 0xDD: cp_(accumulator, absoluteX(true)); targetCycles += 4; break; // CMP a,x 891 | 892 | case 0xC2: immediate(); targetCycles += 2; break; // NOP #i 893 | case 0xC6: de_(zeroPage()); targetCycles += 5; break; // DEC d 894 | case 0xCA: de_(®isterX); targetCycles += 2; break; // DEX 895 | case 0xCE: de_(absolute()); targetCycles += 6; break; // DEC a 896 | case 0xD2: return; // STP 897 | case 0xD6: de_(zeroPageX()); targetCycles += 6; break; // DEC d,x 898 | case 0xDA: targetCycles += 2; break; // NOP 899 | case 0xDE: de_(absoluteX(false)); targetCycles += 7; break; // DEC a,x 900 | 901 | case 0xC3: dcp(indirectX()); targetCycles += 8; break; // DCP (d,x) 902 | case 0xC7: dcp(zeroPage()); targetCycles += 5; break; // DCP d 903 | case 0xCB: axs(immediate()); targetCycles += 2; break; // AXS #i 904 | case 0xCF: dcp(absolute()); targetCycles += 6; break; // DCP a 905 | case 0xD3: dcp(indirectY(false)); targetCycles += 8; break; // DCP (d),y 906 | case 0xD7: dcp(zeroPageX()); targetCycles += 6; break; // DCP d,x 907 | case 0xDB: dcp(absoluteY(false)); targetCycles += 7; break; // DCP a,y 908 | case 0xDF: dcp(absoluteX(false)); targetCycles += 7; break; // DCP a,x 909 | 910 | case 0xE0: cp_(registerX, immediate()); targetCycles += 2; break; // CPX #i 911 | case 0xE4: cp_(registerX, zeroPage()); targetCycles += 3; break; // CPX d 912 | case 0xE8: in_(®isterX); targetCycles += 2; break; // INX 913 | case 0xEC: cp_(registerX, absolute()); targetCycles += 4; break; // CPX a 914 | case 0xF0: b__( (flags & 0x02)); targetCycles += 2; break; // BEQ *+d 915 | case 0xF4: zeroPageX(); targetCycles += 4; break; // NOP d,x 916 | case 0xF8: se_(0x08); targetCycles += 2; break; // SED 917 | case 0xFC: absoluteX(true); targetCycles += 4; break; // NOP a,x 918 | 919 | case 0xE1: sbc(indirectX()); targetCycles += 6; break; // SBC (d,x) 920 | case 0xE5: sbc(zeroPage()); targetCycles += 3; break; // SBC d 921 | case 0xE9: sbc(immediate()); targetCycles += 2; break; // SBC #i 922 | case 0xED: sbc(absolute()); targetCycles += 4; break; // SBC a 923 | case 0xF1: sbc(indirectY(true)); targetCycles += 5; break; // SBC (d),y 924 | case 0xF5: sbc(zeroPageX()); targetCycles += 4; break; // SBC d,x 925 | case 0xF9: sbc(absoluteY(true)); targetCycles += 4; break; // SBC a,y 926 | case 0xFD: sbc(absoluteX(true)); targetCycles += 4; break; // SBC a,x 927 | 928 | case 0xE2: immediate(); targetCycles += 2; break; // NOP #i 929 | case 0xE6: in_(zeroPage()); targetCycles += 5; break; // INC d 930 | case 0xEA: targetCycles += 2; break; // NOP 931 | case 0xEE: in_(absolute()); targetCycles += 6; break; // INC a 932 | case 0xF2: return; // STP 933 | case 0xF6: in_(zeroPageX()); targetCycles += 6; break; // INC d,x 934 | case 0xFA: targetCycles += 2; break; // NOP 935 | case 0xFE: in_(absoluteX(false)); targetCycles += 7; break; // INC a,x 936 | 937 | case 0xE3: isc(indirectX()); targetCycles += 8; break; // ISC (d,x) 938 | case 0xE7: isc(zeroPage()); targetCycles += 5; break; // ISC d 939 | case 0xEB: sbc(immediate()); targetCycles += 2; break; // SBC #i 940 | case 0xEF: isc(absolute()); targetCycles += 6; break; // ISC a 941 | case 0xF3: isc(indirectY(false)); targetCycles += 8; break; // ISC (d),y 942 | case 0xF7: isc(zeroPageX()); targetCycles += 6; break; // ISC d,x 943 | case 0xFB: isc(absoluteY(false)); targetCycles += 7; break; // ISC a,y 944 | case 0xFF: isc(absoluteX(false)); targetCycles += 7; break; // ISC a,x 945 | } 946 | 947 | programCounter++; 948 | } 949 | 950 | void saveState(FILE *state) 951 | { 952 | for (unsigned int i = 0; i < stateItems.size(); i++) 953 | fwrite(stateItems[i].pointer, 1, stateItems[i].size, state); 954 | } 955 | 956 | void loadState(FILE *state) 957 | { 958 | for (unsigned int i = 0; i < stateItems.size(); i++) 959 | fread(stateItems[i].pointer, 1, stateItems[i].size, state); 960 | } 961 | 962 | } 963 | -------------------------------------------------------------------------------- /src/cpu.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #ifndef CPU_H 21 | #define CPU_H 22 | 23 | namespace cpu 24 | { 25 | 26 | extern uint8_t memory[0x10000]; 27 | extern bool interrupts[3]; 28 | 29 | extern uint8_t inputMasks[2]; 30 | 31 | void reset(); 32 | void runCycle(); 33 | 34 | void saveState(FILE *state); 35 | void loadState(FILE *state); 36 | 37 | } 38 | 39 | #endif // CPU_H 40 | -------------------------------------------------------------------------------- /src/desktop/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #include 21 | #include "GL/glut.h" 22 | #include "portaudio.h" 23 | 24 | #include "../core.h" 25 | #include "../ppu.h" 26 | #include "../apu.h" 27 | #include "../config.h" 28 | #include "../mutex.h" 29 | 30 | bool requestSave, requestLoad; 31 | 32 | uint32_t screenFiltering = 0; 33 | uint32_t cropOverscan = 0; 34 | string keyMap[] = { "l", "k", "g", "h", "w", "s", "a", "d" }; 35 | 36 | const vector platformSettings = 37 | { 38 | { "screenFiltering", &screenFiltering, false }, 39 | { "cropOverscan", &cropOverscan, false }, 40 | { "keyA", &keyMap[0], true }, 41 | { "keyB", &keyMap[1], true }, 42 | { "keySelect", &keyMap[2], true }, 43 | { "keyStart", &keyMap[3], true }, 44 | { "keyUp", &keyMap[4], true }, 45 | { "keyDown", &keyMap[5], true }, 46 | { "keyLeft", &keyMap[6], true }, 47 | { "keyRight", &keyMap[7], true } 48 | }; 49 | 50 | void runCore() 51 | { 52 | while (true) 53 | { 54 | core::runCycle(); 55 | 56 | if (requestSave) 57 | { 58 | core::saveState(); 59 | requestSave = false; 60 | } 61 | else if (requestLoad) 62 | { 63 | core::loadState(); 64 | requestLoad = false; 65 | } 66 | } 67 | } 68 | 69 | void draw() 70 | { 71 | mutex::lock(ppu::displayMutex); 72 | if (cropOverscan) 73 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 224, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &ppu::displayBuffer[256 * 8]); 74 | else 75 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 240, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, ppu::displayBuffer); 76 | mutex::unlock(ppu::displayMutex); 77 | glBegin(GL_QUADS); 78 | glTexCoord2i(1, 1); glVertex2f( 1, -1); 79 | glTexCoord2i(0, 1); glVertex2f(-1, -1); 80 | glTexCoord2i(0, 0); glVertex2f(-1, 1); 81 | glTexCoord2i(1, 0); glVertex2f( 1, 1); 82 | glEnd(); 83 | glFlush(); 84 | glutPostRedisplay(); 85 | } 86 | 87 | void keyDown(unsigned char key, int x, int y) 88 | { 89 | for (int i = 0; i < 8; i++) 90 | { 91 | if (key == keyMap[i][0]) 92 | core::pressKey(0, i); 93 | } 94 | } 95 | 96 | void keyUp(unsigned char key, int x, int y) 97 | { 98 | for (int i = 0; i < 8; i++) 99 | { 100 | if (key == keyMap[i][0]) 101 | core::releaseKey(0, i); 102 | } 103 | } 104 | 105 | int audioCallback(const void *in, void *out, unsigned long frames, 106 | const PaStreamCallbackTimeInfo *info, PaStreamCallbackFlags flags, void *data) 107 | { 108 | int16_t *curOut = (int16_t*)out; 109 | for (int i = 0; i < frames; i++) 110 | *curOut++ = apu::audioSample(2.5f); 111 | return 0; 112 | } 113 | 114 | void onMenuSelect(int selection) 115 | { 116 | if (selection == 0) // Save State 117 | requestSave = true; 118 | else // Load State 119 | requestLoad = true; 120 | } 121 | 122 | void onExit() 123 | { 124 | core::closeRom(); 125 | config::save(); 126 | } 127 | 128 | int main(int argc, char **argv) 129 | { 130 | config::load(platformSettings); 131 | 132 | if (argc < 2) 133 | { 134 | printf("Please specify a ROM to load.\n"); 135 | return 1; 136 | } 137 | 138 | if (core::loadRom(argv[1]) != 0) 139 | return 1; 140 | 141 | glutInit(&argc, argv); 142 | glutInitWindowSize(256, (cropOverscan ? 224 : 240)); 143 | glutCreateWindow("NoiES"); 144 | glEnable(GL_TEXTURE_2D); 145 | 146 | GLuint texture; 147 | glGenTextures(1, &texture); 148 | glBindTexture(GL_TEXTURE_2D, texture); 149 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, screenFiltering ? GL_LINEAR : GL_NEAREST); 150 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, screenFiltering ? GL_LINEAR : GL_NEAREST); 151 | 152 | PaStream *stream; 153 | Pa_Initialize(); 154 | Pa_OpenDefaultStream(&stream, 0, 1, paInt16, 44100, 256, audioCallback, NULL); 155 | Pa_StartStream(stream); 156 | 157 | glutCreateMenu(onMenuSelect); 158 | glutAddMenuEntry("Save State", 0); 159 | glutAddMenuEntry("Load State", 1); 160 | glutAttachMenu(GLUT_RIGHT_BUTTON); 161 | 162 | atexit(onExit); 163 | glutDisplayFunc(draw); 164 | glutKeyboardFunc(keyDown); 165 | glutKeyboardUpFunc(keyUp); 166 | 167 | std::thread core(runCore); 168 | glutMainLoop(); 169 | 170 | return 0; 171 | } 172 | -------------------------------------------------------------------------------- /src/desktop/mutex.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #include 21 | 22 | namespace mutex 23 | { 24 | 25 | void *create() 26 | { 27 | std::mutex *mutex = new std::mutex; 28 | return mutex; 29 | } 30 | 31 | void lock(void *mutex) 32 | { 33 | ((std::mutex*)mutex)->lock(); 34 | } 35 | 36 | void unlock(void *mutex) 37 | { 38 | ((std::mutex*)mutex)->unlock(); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/mapper.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include "core.h" 25 | #include "cpu.h" 26 | #include "ppu.h" 27 | 28 | namespace mapper 29 | { 30 | 31 | uint8_t *rom; 32 | uint32_t vromAddress; 33 | 34 | uint8_t type; 35 | uint8_t bankSelect, latch, shift; 36 | uint8_t mmc2VromBanks[4]; 37 | uint8_t irqCount, irqLatch; 38 | bool irqEnable, irqReload; 39 | 40 | const vector stateItems = 41 | { 42 | { &bankSelect, sizeof(bankSelect) }, 43 | { &latch, sizeof(latch) }, 44 | { &shift, sizeof(shift) }, 45 | { mmc2VromBanks, sizeof(mmc2VromBanks) }, 46 | { &irqCount, sizeof(irqCount) }, 47 | { &irqLatch, sizeof(irqLatch) }, 48 | { &irqEnable, sizeof(irqEnable) }, 49 | { &irqReload, sizeof(irqReload) } 50 | }; 51 | 52 | bool load(FILE *romFile, uint8_t numBanks, uint8_t mapperType) 53 | { 54 | // Check if the mapper type is supported 55 | if (mapperType > 4 && mapperType != 7 && mapperType != 9 && mapperType != 15) 56 | return false; 57 | 58 | vromAddress = numBanks * 0x4000; 59 | type = mapperType; 60 | 61 | // Clear the state items 62 | for (unsigned int i = 0; i < stateItems.size(); i++) 63 | memset(stateItems[i].pointer, 0, stateItems[i].size); 64 | 65 | // Move the ROM into its own memory, with an extra 8 KB for VRAM if no VROM is present 66 | uint16_t start = ftell(romFile); 67 | fseek(romFile, 0, SEEK_END); 68 | uint32_t size = ftell(romFile) - start; 69 | delete[] rom; 70 | rom = new uint8_t[size + ((vromAddress == size) ? 0x2000 : 0)]; 71 | fseek(romFile, start, SEEK_SET); 72 | fread(rom, 1, size, romFile); 73 | fclose(romFile); 74 | 75 | // Load the initial banks into system memory 76 | uint16_t lastSize = (type == 9) ? 0x6000 : 0x4000; 77 | memcpy(&cpu::memory[0x8000], rom, 0x8000 - lastSize); 78 | memcpy(&cpu::memory[0x10000 - lastSize], &rom[vromAddress - lastSize], lastSize); 79 | memcpy(ppu::memory, &rom[vromAddress], 0x2000); 80 | 81 | return true; 82 | } 83 | 84 | void mmc1(uint16_t address, uint8_t value) 85 | { 86 | if (value & 0x80) 87 | { 88 | // Reset the shift register 89 | bankSelect |= 0x0C; 90 | latch = 0; 91 | shift = 0; 92 | } 93 | else 94 | { 95 | // Write a bit to the latch 96 | latch |= (value & 0x01) << shift++; 97 | } 98 | 99 | // Pass the 5-bit value to a register 100 | if (shift == 5) 101 | { 102 | if (address >= 0x8000 && address < 0xA000) // Control 103 | { 104 | bankSelect = latch; 105 | ppu::mirrorMode = latch & 0x03; 106 | } 107 | else if (address >= 0xA000 && address < 0xC000) // Swap VROM bank 0 108 | { 109 | if (bankSelect & 0x10) // 4 KB 110 | memcpy(ppu::memory, &rom[vromAddress + 0x1000 * latch], 0x1000); 111 | else // 8 KB 112 | memcpy(ppu::memory, &rom[vromAddress + 0x1000 * (latch & ~0x01)], 0x2000); 113 | } 114 | else if (address >= 0xC000 && address < 0xE000) // Swap VROM bank 1 115 | { 116 | if (bankSelect & 0x10) // 4 KB 117 | memcpy(&ppu::memory[0x1000], &rom[vromAddress + 0x1000 * latch], 0x1000); 118 | } 119 | else // Swap ROM banks 120 | { 121 | if (!(bankSelect & 0x08)) // 32 KB 122 | { 123 | memcpy(&cpu::memory[0x8000], &rom[0x4000 * (latch & ~0x01)], 0x8000); 124 | } 125 | else if (bankSelect & 0x04) // 16 KB, bank 1 fixed 126 | { 127 | memcpy(&cpu::memory[0x8000], &rom[0x4000 * latch], 0x4000); 128 | memcpy(&cpu::memory[0xC000], &rom[vromAddress - 0x4000], 0x4000); 129 | } 130 | else // 16 KB, bank 0 fixed 131 | { 132 | memcpy(&cpu::memory[0xC000], &rom[0x4000 * latch], 0x4000); 133 | memcpy(&cpu::memory[0x8000], rom, 0x4000); 134 | } 135 | } 136 | 137 | latch = 0; 138 | shift = 0; 139 | } 140 | } 141 | 142 | void unrom(uint16_t address, uint8_t value) 143 | { 144 | // Swap the first 16 KB ROM bank 145 | if (address >= 0x8000) 146 | memcpy(&cpu::memory[0x8000], &rom[0x4000 * value], 0x4000); 147 | } 148 | 149 | void cnrom(uint16_t address, uint8_t value) 150 | { 151 | // Swap the 8 KB VROM bank 152 | if (address >= 0x8000) 153 | memcpy(ppu::memory, &rom[vromAddress + 0x2000 * (value & 0x03)], 0x2000); 154 | } 155 | 156 | void mmc3(uint16_t address, uint8_t value) 157 | { 158 | if (address >= 0x8000 && address < 0xA000) 159 | { 160 | if (address % 2 == 0) // Select banks 161 | { 162 | bankSelect = value; 163 | memcpy(&cpu::memory[(value & 0x40) ? 0x8000 : 0xC000], &rom[vromAddress - 0x4000], 0x2000); 164 | } 165 | else // Swap banks 166 | { 167 | uint8_t bank = bankSelect & 0x07; 168 | if (bank < 2) // 2 KB VROM banks 169 | { 170 | memcpy(&ppu::memory[((bankSelect & 0x80) ? 0x1000 : 0) + 0x800 * bank], 171 | &rom[vromAddress + 0x400 * (value & ~0x01)], 0x800); 172 | } 173 | else if (bank >= 2 && bank < 6) // 1 KB VROM banks 174 | { 175 | memcpy(&ppu::memory[((bankSelect & 0x80) ? 0 : 0x1000) + 0x400 * (bank - 2)], 176 | &rom[vromAddress + 0x400 * value], 0x400); 177 | } 178 | else if (bank == 6) // Swappable/fixed 8 KB ROM bank 179 | { 180 | memcpy(&cpu::memory[(bankSelect & 0x40) ? 0xC000 : 0x8000], &rom[0x2000 * value], 0x2000); 181 | } 182 | else // Swappable 8 KB ROM bank 183 | { 184 | memcpy(&cpu::memory[0xA000], &rom[0x2000 * value], 0x2000); 185 | } 186 | } 187 | } 188 | else if (address >= 0xA000 && address < 0xC000) 189 | { 190 | if (address % 2 == 0) // Mirroring 191 | { 192 | if (ppu::mirrorMode != 4) 193 | ppu::mirrorMode = 2 + value; 194 | } 195 | } 196 | else if (address >= 0xC000 && address < 0xE000) 197 | { 198 | if (address % 2 == 0) // IRQ latch 199 | irqLatch = value; 200 | else // IRQ reload 201 | irqReload = true; 202 | } 203 | else // IRQ toggle 204 | { 205 | irqEnable = (address % 2 == 1); 206 | } 207 | } 208 | 209 | void axrom(uint16_t address, uint8_t value) 210 | { 211 | // Swap the 32 KB ROM bank and select a nametable for 1-screen mirroring 212 | if (address >= 0x8000) 213 | { 214 | memcpy(&cpu::memory[0x8000], &rom[0x8000 * (value & 0x07)], 0x8000); 215 | ppu::mirrorMode = (value & 0x10) ? 1 : 0; 216 | } 217 | } 218 | 219 | void mmc2(uint16_t address, uint8_t value) 220 | { 221 | if (address >= 0xA000 && address < 0xB000) // Swap first 8 KB ROM bank 222 | { 223 | memcpy(&cpu::memory[0x8000], &rom[0x2000 * value], 0x2000); 224 | } 225 | else if (address < 0xF000) // Select VROM banks 226 | { 227 | mmc2VromBanks[(address - 0xB000) / 0x1000] = value; 228 | } 229 | else // Mirroring 230 | { 231 | if (ppu::mirrorMode != 4) 232 | ppu::mirrorMode = 2 + value; 233 | } 234 | } 235 | 236 | void map15(uint16_t address, uint8_t value) 237 | { 238 | if (address >= 0x8000) 239 | { 240 | uint8_t bank = value & 0x3F; 241 | switch (address & 0x03) 242 | { 243 | case 0: // Swap the 32 KB bank (if bit 0 is set, acts like 16 KB mode) 244 | memcpy(&cpu::memory[0x8000], &rom[0x4000 * bank], 0x4000); 245 | memcpy(&cpu::memory[0xC000], &rom[0x4000 * (bank | 0x01)], 0x4000); 246 | break; 247 | 248 | case 1: // Swap the first 16 KB bank and fix the last bank to the last of a 128 KB block 249 | memcpy(&cpu::memory[0x8000], &rom[0x4000 * bank], 0x4000); 250 | memcpy(&cpu::memory[0xC000], &rom[0x4000 * (bank | 0x07)], 0x4000); 251 | break; 252 | 253 | case 2: // Swap a single 8 KB bank and mirror it 254 | memcpy(&cpu::memory[0x8000], &rom[0x4000 * bank + ((value & 0x80) ? 0x2000 : 0)], 0x2000); 255 | for (int i = 0; i < 3; i++) 256 | memcpy(&cpu::memory[0xA000 + i * 0x2000], &cpu::memory[0x8000], 0x2000); 257 | break; 258 | 259 | case 3: // Swap a single 16 KB bank and mirror it 260 | memcpy(&cpu::memory[0x8000], &rom[0x4000 * bank], 0x4000); 261 | memcpy(&cpu::memory[0xC000], &cpu::memory[0x8000], 0x4000); 262 | break; 263 | } 264 | 265 | // Set mirroring mode 266 | ppu::mirrorMode = (value & 0x40) ? 3 : 2; 267 | } 268 | } 269 | 270 | void registerWrite(uint16_t address, uint8_t value) 271 | { 272 | switch (type) 273 | { 274 | case 1: mmc1(address, value); break; 275 | case 2: unrom(address, value); break; 276 | case 3: cnrom(address, value); break; 277 | case 4: mmc3(address, value); break; 278 | case 7: axrom(address, value); break; 279 | case 9: mmc2(address, value); break; 280 | case 15: map15(address, value); break; 281 | } 282 | } 283 | 284 | void mmc3Counter() 285 | { 286 | if (type != 4) 287 | return; 288 | 289 | // Clock the MMC3 IRQ counter 290 | if (irqCount == 0 || irqReload) 291 | { 292 | // Trigger an IRQ if they're enabled 293 | if (irqEnable && !irqReload) 294 | cpu::interrupts[2] = true; 295 | 296 | irqCount = irqLatch; 297 | irqReload = false; 298 | } 299 | else 300 | { 301 | irqCount--; 302 | } 303 | } 304 | 305 | void mmc2SetLatch(uint8_t latch, bool value) 306 | { 307 | if (type != 9) 308 | return; 309 | 310 | if (latch == 0) 311 | memcpy(ppu::memory, &rom[vromAddress + 0x1000 * mmc2VromBanks[value]], 0x1000); 312 | else 313 | memcpy(&ppu::memory[0x1000], &rom[vromAddress + 0x1000 * mmc2VromBanks[2 + value]], 0x1000); 314 | } 315 | 316 | void saveState(FILE *state) 317 | { 318 | for (unsigned int i = 0; i < stateItems.size(); i++) 319 | fwrite(stateItems[i].pointer, 1, stateItems[i].size, state); 320 | } 321 | 322 | void loadState(FILE *state) 323 | { 324 | for (unsigned int i = 0; i < stateItems.size(); i++) 325 | fread(stateItems[i].pointer, 1, stateItems[i].size, state); 326 | } 327 | 328 | } 329 | -------------------------------------------------------------------------------- /src/mapper.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #ifndef MAPPER_H 21 | #define MAPPER_H 22 | 23 | #include 24 | 25 | namespace mapper 26 | { 27 | 28 | bool load(FILE *romFile, uint8_t numBanks, uint8_t mapperType); 29 | void registerWrite(uint16_t address, uint8_t value); 30 | 31 | void mmc3Counter(); 32 | void mmc2SetLatch(uint8_t latch, bool value); 33 | 34 | void saveState(FILE *state); 35 | void loadState(FILE *state); 36 | 37 | } 38 | 39 | #endif // MAPPER_H 40 | -------------------------------------------------------------------------------- /src/mutex.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #ifndef MUTEX_H 21 | #define MUTEX_H 22 | 23 | namespace mutex 24 | { 25 | 26 | void *create(); 27 | 28 | void lock(void *mutex); 29 | void unlock(void *mutex); 30 | 31 | } 32 | 33 | #endif // MUTEX_H 34 | -------------------------------------------------------------------------------- /src/ppu.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #include "core.h" 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include "config.h" 29 | #include "cpu.h" 30 | #include "mapper.h" 31 | #include "mutex.h" 32 | 33 | namespace ppu 34 | { 35 | 36 | chrono::steady_clock::time_point timer; 37 | uint32_t framebuffer[256 * 240]; 38 | uint32_t displayBuffer[256 * 240]; 39 | void *displayMutex; 40 | 41 | uint8_t memory[0x4000]; 42 | uint8_t sprMemory[0x100]; 43 | uint8_t pixelBuffer[0x10]; 44 | 45 | uint16_t scanline, scanlineDot; 46 | uint16_t ppuAddress, ppuTempAddr; 47 | uint8_t scrollX; 48 | uint8_t control, mask, status; 49 | uint8_t oamAddress; 50 | uint8_t readBuffer; 51 | uint8_t spriteCount; 52 | bool writeToggle; 53 | 54 | uint8_t mirrorMode; 55 | 56 | const vector stateItems = 57 | { 58 | { memory, sizeof(memory) }, 59 | { sprMemory, sizeof(sprMemory) }, 60 | { &scanline, sizeof(scanline) }, 61 | { &scanlineDot, sizeof(scanlineDot) }, 62 | { &ppuAddress, sizeof(ppuAddress) }, 63 | { &ppuTempAddr, sizeof(ppuTempAddr) }, 64 | { &scrollX, sizeof(scrollX) }, 65 | { &control, sizeof(control) }, 66 | { &mask, sizeof(mask) }, 67 | { &status, sizeof(status) }, 68 | { &oamAddress, sizeof(oamAddress) }, 69 | { &readBuffer, sizeof(readBuffer) }, 70 | { &spriteCount, sizeof(spriteCount) }, 71 | { &writeToggle, sizeof(writeToggle) } 72 | }; 73 | 74 | const uint32_t palette[] = 75 | { 76 | 0x757575FF, 0x271B8FFF, 0x0000ABFF, 0x47009FFF, 77 | 0x8F0077FF, 0xAB0013FF, 0xA70000FF, 0x7F0B00FF, 78 | 0x432F00FF, 0x004700FF, 0x005100FF, 0x003F17FF, 79 | 0x1B3F5FFF, 0x000000FF, 0x000000FF, 0x000000FF, 80 | 0xBCBCBCFF, 0x0073EFFF, 0x233BEFFF, 0x8300F3FF, 81 | 0xBF00BFFF, 0xE7005BFF, 0xDB2B00FF, 0xCB4F0FFF, 82 | 0x8B7300FF, 0x009700FF, 0x00AB00FF, 0x00933BFF, 83 | 0x00838BFF, 0x000000FF, 0x000000FF, 0x000000FF, 84 | 0xFFFFFFFF, 0x3FBFFFFF, 0x5F97FFFF, 0xA78BFDFF, 85 | 0xF77BFFFF, 0xFF77B7FF, 0xFF7763FF, 0xFF9B3BFF, 86 | 0xF3BF3FFF, 0x83D313FF, 0x4FDF4BFF, 0x58F898FF, 87 | 0x00EBDBFF, 0x000000FF, 0x000000FF, 0x000000FF, 88 | 0xFFFFFFFF, 0xABE7FFFF, 0xC7D7FFFF, 0xD7CBFFFF, 89 | 0xFFC7FFFF, 0xFFC7DBFF, 0xFFBFB3FF, 0xFFDBABFF, 90 | 0xFFE7A3FF, 0xE3FFA3FF, 0xABF3BFFF, 0xB3FFFCFF, 91 | 0x9FFFF3FF, 0x000000FF, 0x000000FF, 0x000000FF 92 | }; 93 | 94 | void reset() 95 | { 96 | // Clear the state items 97 | for (unsigned int i = 0; i < stateItems.size(); i++) 98 | memset(stateItems[i].pointer, 0, stateItems[i].size); 99 | 100 | displayMutex = mutex::create(); 101 | } 102 | 103 | uint16_t memoryMirror(uint16_t address) 104 | { 105 | // Get the real location of a mirrored address in memory 106 | address %= 0x4000; 107 | if (address >= 0x3000 && address < 0x3F00) 108 | address -= 0x1000; 109 | else if (address >= 0x3F20 && address < 0x4000) 110 | address = 0x3F00 + address % 0x20; 111 | else if (address >= 0x3F10 && address < 0x3F20 && address % 4 == 0) 112 | address -= 0x10; 113 | 114 | // Nametable mirroring 115 | switch (mirrorMode) 116 | { 117 | case 0: // 1-screen A 118 | if (address >= 0x2400 && address < 0x3000) 119 | address -= ((address - 0x2000) / 0x0400) * 0x0400; 120 | break; 121 | 122 | case 1: // 1-screen B 123 | if (address >= 0x2000 && address < 0x2400) 124 | address += 0x0400; 125 | else if (address >= 0x2800 && address < 0x3000) 126 | address -= ((address - 0x2400) / 0x0400) * 0x0400; 127 | break; 128 | 129 | case 2: // Vertical 130 | if (address >= 0x2800 && address < 0x3000) 131 | address -= 0x0800; 132 | break; 133 | 134 | case 3: // Horizontal 135 | if (address >= 0x2400 && address < 0x2800) 136 | address -= 0x0400; 137 | else if (address >= 0x2800 && address < 0x3000) 138 | address -= ((address - 0x2400) / 0x0400) * 0x0400; 139 | break; 140 | } 141 | 142 | return address; 143 | } 144 | 145 | void fetchPixels() 146 | { 147 | uint16_t xOffset = ((ppuAddress & 0x001F) << 3); 148 | uint16_t yOffset = ((ppuAddress & 0x03E0) >> 2) + (ppuAddress >> 12); 149 | uint16_t tableOffset = memoryMirror(0x2000 | (ppuAddress & 0x0C00)); 150 | uint16_t tile = ((control & 0x10) << 8) + memory[tableOffset + (yOffset / 8) * 32 + xOffset / 8] * 16; 151 | 152 | // Get the upper 2 bits of the palette index from the attribute table 153 | uint8_t upperBits = memory[tableOffset + 0x03C0 + (yOffset / 32) * 8 + xOffset / 32]; 154 | if ((xOffset / 16) % 2 == 0 && (yOffset / 16) % 2 == 0) // Top left 155 | upperBits = (upperBits & 0x03) << 2; 156 | else if ((xOffset / 16) % 2 == 1 && (yOffset / 16) % 2 == 0) // Top right 157 | upperBits = (upperBits & 0x0C) << 0; 158 | else if ((xOffset / 16) % 2 == 0 && (yOffset / 16) % 2 == 1) // Bottom left 159 | upperBits = (upperBits & 0x30) >> 2; 160 | else // Bottom right 161 | upperBits = (upperBits & 0xC0) >> 4; 162 | 163 | for (int i = 0; i < 8; i++) 164 | { 165 | // Get the lower 2 bits of the palette index from the pattern table 166 | uint8_t lowerBits = memory[tile + yOffset % 8] & (0x80 >> ((xOffset + i) % 8)) ? 0x01 : 0x00; 167 | lowerBits |= memory[tile + yOffset % 8 + 8] & (0x80 >> ((xOffset + i) % 8)) ? 0x02 : 0x00; 168 | 169 | // Shift the pixel buffer and store the new data 170 | pixelBuffer[i] = pixelBuffer[i + 8]; 171 | pixelBuffer[i + 8] = upperBits | lowerBits; 172 | } 173 | 174 | // Set the MMC2 latches 175 | if (tile == 0x1FD0) 176 | mapper::mmc2SetLatch(1, false); 177 | else if (tile == 0x1FE0) 178 | mapper::mmc2SetLatch(1, true); 179 | 180 | // Increment the coarse X coordinate 181 | if ((ppuAddress & 0x001F) == 0x1F) 182 | ppuAddress = (ppuAddress & ~0x001F) ^ 0x0400; 183 | else 184 | ppuAddress++; 185 | } 186 | 187 | void runCycle() 188 | { 189 | if (scanline < 240 && (mask & 0x18)) // Visible lines 190 | { 191 | if (scanlineDot >= 1 && scanlineDot <= 256 && (mask & 0x08)) // Background drawing 192 | { 193 | uint8_t x = scanlineDot - 1; 194 | 195 | // Update the pixel buffer 196 | if (x != 0 && x % 8 == 0) 197 | fetchPixels(); 198 | uint8_t color = pixelBuffer[x % 8 + scrollX]; 199 | 200 | // Get the pixel type 201 | uint32_t *pixel = &framebuffer[scanline * 256 + x]; 202 | uint8_t type = *pixel; 203 | *pixel |= 0xFF; 204 | 205 | if ((x >= 8 || (mask & 0x02)) && (color & 0x03) != 0) 206 | { 207 | // Check for a sprite 0 hit 208 | if (type < 0xFD) 209 | status |= 0x40; 210 | 211 | // Draw a pixel 212 | if (type % 2 == 1) 213 | *pixel = palette[memory[0x3F00 | color]]; 214 | } 215 | } 216 | else if (scanlineDot >= 257 && scanlineDot <= 320 && (mask & 0x10)) // Sprite drawing 217 | { 218 | uint8_t *sprite = &sprMemory[(scanlineDot - 257) * 4]; 219 | uint8_t height = (control & 0x20) ? 16 : 8; 220 | uint8_t y = scanline; 221 | 222 | if (*sprite <= y && *sprite + height > y) 223 | { 224 | if (spriteCount < 8) 225 | { 226 | uint8_t spriteX = *(sprite + 3); 227 | uint8_t spriteY = ((y - *sprite) / 8) * 16 + (y - *sprite) % 8; 228 | uint8_t upperBits = (*(sprite + 2) & 0x03) << 2; 229 | 230 | uint16_t tile; 231 | if (height == 8) 232 | tile = ((control & 0x08) << 9) + *(sprite + 1) * 16; 233 | else 234 | tile = ((*(sprite + 1) & 0x01) << 12) + (*(sprite + 1) & ~0x01) * 16; 235 | 236 | // Flip the sprite vertically if needed 237 | if (*(sprite + 2) & 0x80) 238 | { 239 | if (height == 16 && spriteY < 8) 240 | tile += 16; 241 | spriteY = 7 - (spriteY % 8); 242 | } 243 | 244 | // Draw a sprite line on the next scanline 245 | y++; 246 | for (int i = 0; i < 8; i++) 247 | { 248 | uint16_t xOffset = spriteX + ((*(sprite + 2) & 0x40) ? 7 - i : i); 249 | uint8_t lowerBits = memory[tile + spriteY] & (0x80 >> i) ? 0x01 : 0x00; 250 | lowerBits |= memory[tile + spriteY + 8] & (0x80 >> i) ? 0x02 : 0x00; 251 | 252 | if ((xOffset >= 8 || (mask & 0x04)) && xOffset < 256 && lowerBits != 0) 253 | { 254 | uint32_t *pixel = &framebuffer[y * 256 + xOffset]; 255 | uint8_t type = *pixel; 256 | 257 | if (type == 0xFF) 258 | { 259 | // Draw a pixel 260 | *pixel = palette[memory[0x3F10 | upperBits | lowerBits]]; 261 | 262 | // Mark opaque pixels 263 | (*pixel)--; 264 | if (*(sprite + 2) & 0x20) // Behind background 265 | (*pixel)--; 266 | } 267 | 268 | // Mark sprite 0 269 | if (scanlineDot == 257) 270 | *pixel -= 2; 271 | } 272 | } 273 | 274 | // Set the MMC2 latches 275 | if (tile == 0x0FD0) 276 | mapper::mmc2SetLatch(0, false); 277 | else if (tile == 0x0FE0) 278 | mapper::mmc2SetLatch(0, true); 279 | 280 | if (!config::disableSpriteLimit) 281 | spriteCount++; 282 | } 283 | else 284 | { 285 | // Set the sprite overflow flag 286 | status |= 0x20; 287 | } 288 | } 289 | } 290 | 291 | if (scanlineDot == 256) 292 | { 293 | // Increment the Y coordinate 294 | if ((ppuAddress & 0x7000) == 0x7000) 295 | { 296 | if ((ppuAddress & 0x03E0) == 0x03A0) 297 | ppuAddress = (ppuAddress & ~0x73E0) ^ 0x0800; 298 | else 299 | ppuAddress = (ppuAddress & ~0x73E0) | ((ppuAddress + 0x0020) & 0x03E0); 300 | } 301 | else 302 | { 303 | ppuAddress += 0x1000; 304 | } 305 | } 306 | } 307 | else if (scanline == 241 && scanlineDot == 1) // Start of V-blank 308 | { 309 | // Trigger an NMI if enabled 310 | status |= 0x80; 311 | if (control & 0x80) 312 | cpu::interrupts[0] = true; 313 | } 314 | else if (scanline == 261) // Pre-render line 315 | { 316 | // Clear the bits for the next frame 317 | if (scanlineDot == 1) 318 | status &= ~0xE0; 319 | 320 | // Reload the vertical scroll data if rendering is enabled 321 | if (scanlineDot >= 280 && scanlineDot <= 304 && (mask & 0x18)) 322 | ppuAddress = (ppuAddress & ~0x7BE0) | (ppuTempAddr & 0x7BE0); 323 | } 324 | 325 | if ((scanline < 240 || scanline == 261) && (mask & 0x18)) 326 | { 327 | // Perform updates that happen on both visible lines and the pre-render line 328 | if (scanlineDot == 257) // Horizontal scroll data 329 | ppuAddress = (ppuAddress & ~0x041F) | (ppuTempAddr & 0x041F); 330 | else if (scanlineDot == 260) // MMC3 IRQ counter 331 | mapper::mmc3Counter(); 332 | else if (scanlineDot == 328 || scanlineDot == 336) // Pixel buffer 333 | fetchPixels(); 334 | } 335 | 336 | // Update the scanline position 337 | scanlineDot++; 338 | if (scanlineDot == 341) // End of scanline 339 | { 340 | scanlineDot = spriteCount = 0; 341 | scanline++; 342 | 343 | if (scanline == 262) // End of frame 344 | { 345 | // Copy the finished frame to the display 346 | mutex::lock(displayMutex); 347 | memcpy(displayBuffer, framebuffer, sizeof(displayBuffer)); 348 | mutex::unlock(displayMutex); 349 | 350 | // Clear the framebuffer 351 | for (int i = 0; i < 256 * 240; i++) 352 | framebuffer[i] = palette[memory[0x3F00]]; 353 | 354 | // Limit the FPS to 60 if enabled 355 | if (config::frameLimiter) 356 | { 357 | chrono::duration elapsed = chrono::steady_clock::now() - timer; 358 | if (elapsed.count() < 1.0f / 60) 359 | usleep((1.0f / 60 - elapsed.count()) * 1000000); 360 | timer = chrono::steady_clock::now(); 361 | } 362 | 363 | scanline = 0; 364 | } 365 | } 366 | } 367 | 368 | uint8_t registerRead(uint16_t address) 369 | { 370 | uint8_t value = 0; 371 | 372 | // Handle reads from memory-mapped registers 373 | switch (address) 374 | { 375 | case 0x2002: // PPUSTATUS 376 | // Clear the V-blank bit and write toggle 377 | value = status; 378 | status &= ~0x80; 379 | writeToggle = false; 380 | break; 381 | 382 | case 0x2004: // OAMDATA 383 | // Read from sprite memory 384 | value = sprMemory[oamAddress]; 385 | break; 386 | 387 | case 0x2007: // PPUDATA 388 | // Read from PPU memory, buffering non-palette reads 389 | value = (ppuAddress < 0x3F00) ? readBuffer : memory[memoryMirror(ppuAddress)]; 390 | readBuffer = memory[memoryMirror(ppuAddress)]; 391 | ppuAddress += (control & 0x04) ? 32 : 1; 392 | break; 393 | } 394 | 395 | return value; 396 | } 397 | 398 | void registerWrite(uint16_t address, uint8_t value) 399 | { 400 | // Handle writes to memory-mapped registers 401 | switch (address) 402 | { 403 | case 0x2000: // PPUCTRL 404 | control = value; 405 | ppuTempAddr = (ppuTempAddr & ~0x0C00) | ((value & 0x03) << 10); 406 | break; 407 | 408 | case 0x2001: // PPUMASK 409 | mask = value; 410 | break; 411 | 412 | case 0x2003: // OAMADDR 413 | oamAddress = value; 414 | break; 415 | 416 | case 0x2004: // OAMDATA 417 | // Write a value to sprite memory 418 | sprMemory[oamAddress] = value; 419 | oamAddress++; 420 | break; 421 | 422 | case 0x2005: // PPUSCROLL 423 | // Set the scroll positions 424 | writeToggle = !writeToggle; 425 | if (writeToggle) 426 | { 427 | ppuTempAddr = (ppuTempAddr & ~0x001F) | ((value & 0xF8) >> 3); 428 | scrollX = value & 0x07; 429 | } 430 | else 431 | { 432 | ppuTempAddr = (ppuTempAddr & ~0x73E0) | ((value & 0xF8) << 2) | ((value & 0x07) << 12); 433 | } 434 | break; 435 | 436 | case 0x2006: // PPUADDR 437 | // Set the PPU address 438 | writeToggle = !writeToggle; 439 | if (writeToggle) 440 | ppuTempAddr = (ppuTempAddr & ~0xFF00) | ((value & 0x7F) << 8); 441 | else 442 | ppuAddress = ppuTempAddr = (ppuTempAddr & ~0x00FF) | value; 443 | break; 444 | 445 | case 0x2007: // PPUDATA 446 | // Write a value to PPU memory 447 | memory[memoryMirror(ppuAddress)] = (ppuAddress < 0x3F00) ? value : value % 0x40; 448 | ppuAddress += (control & 0x04) ? 32 : 1; 449 | break; 450 | 451 | case 0x4014: // OAMDMA 452 | // DMA transfer to sprite memory 453 | memcpy(sprMemory, &cpu::memory[value * 0x100], 0x100); 454 | break; 455 | } 456 | } 457 | 458 | void saveState(FILE *state) 459 | { 460 | for (unsigned int i = 0; i < stateItems.size(); i++) 461 | fwrite(stateItems[i].pointer, 1, stateItems[i].size, state); 462 | } 463 | 464 | void loadState(FILE *state) 465 | { 466 | for (unsigned int i = 0; i < stateItems.size(); i++) 467 | fread(stateItems[i].pointer, 1, stateItems[i].size, state); 468 | } 469 | 470 | } 471 | -------------------------------------------------------------------------------- /src/ppu.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #ifndef PPU_H 21 | #define PPU_H 22 | 23 | namespace ppu 24 | { 25 | 26 | extern uint32_t displayBuffer[256 * 240]; 27 | extern void *displayMutex; 28 | 29 | extern uint8_t memory[0x4000]; 30 | extern uint8_t mirrorMode; 31 | 32 | void reset(); 33 | void runCycle(); 34 | 35 | uint8_t registerRead(uint16_t address); 36 | void registerWrite(uint16_t address, uint8_t value); 37 | 38 | void saveState(FILE *state); 39 | void loadState(FILE *state); 40 | 41 | } 42 | 43 | #endif // PPU_H 44 | -------------------------------------------------------------------------------- /src/switch/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #include "ui.h" 21 | #include "../core.h" 22 | #include "../ppu.h" 23 | #include "../apu.h" 24 | #include "../config.h" 25 | #include "../mutex.h" 26 | 27 | bool paused; 28 | Thread coreThread, audioThread; 29 | 30 | u32 *bufferPointer; 31 | int bufferHeight, screenWidth, screenOffsetX; 32 | 33 | u32 screenFiltering = 0; 34 | u32 cropOverscan = 0; 35 | u32 aspectRatio = 0; 36 | string lastPath = "sdmc:/"; 37 | 38 | u32 keyMap[] = 39 | { 40 | KEY_A, KEY_B, KEY_MINUS, KEY_PLUS, 41 | (KEY_DUP | KEY_LSTICK_UP), (KEY_DDOWN | KEY_LSTICK_DOWN), 42 | (KEY_DLEFT | KEY_LSTICK_LEFT), (KEY_DRIGHT | KEY_LSTICK_RIGHT), 43 | (KEY_L | KEY_R) 44 | }; 45 | 46 | const vector platformSettings = 47 | { 48 | { "screenFiltering", &screenFiltering, false }, 49 | { "cropOverscan", &cropOverscan, false }, 50 | { "aspectRatio", &aspectRatio, false }, 51 | { "keyA", &keyMap[0], false }, 52 | { "keyB", &keyMap[1], false }, 53 | { "keySelect", &keyMap[2], false }, 54 | { "keyStart", &keyMap[3], false }, 55 | { "keyUp", &keyMap[4], false }, 56 | { "keyDown", &keyMap[5], false }, 57 | { "keyLeft", &keyMap[6], false }, 58 | { "keyRight", &keyMap[7], false }, 59 | { "keyMenu", &keyMap[8], false }, 60 | { "lastPath", &lastPath, true } 61 | }; 62 | 63 | const vector controlNames = 64 | { 65 | "A Button", 66 | "B Button", 67 | "Select Button", 68 | "Start Button", 69 | "D-Pad Up", 70 | "D-Pad Down", 71 | "D-Pad Left", 72 | "D-Pad Right", 73 | "Pause Menu" 74 | }; 75 | 76 | const vector controlSubnames = 77 | { 78 | "A Button", "B Button", "X Button", "Y Button", 79 | "Left Stick Click", "Right Stick Click", 80 | "L Button", "R Button", "ZL Button", "ZR Button", 81 | "Plus Button", "Minus Button", 82 | "D-Pad Left", "D-Pad Up", "D-Pad Right", "D-Pad Down", 83 | "Left Stick Left", "Left Stick Up", "Left Stick Right", "Left Stick Down", 84 | "Right Stick Left", "Right Stick Up", "Right Stick Right", "Right Stick Down" 85 | }; 86 | 87 | const vector settingNames = 88 | { 89 | "Frame Limiter", 90 | "Disable Sprite Limit", 91 | "Screen Filtering", 92 | "Crop Overscan", 93 | "Aspect Ratio" 94 | }; 95 | 96 | const vector> settingSubnames = 97 | { 98 | { "Off", "On" }, 99 | { "Off", "On" }, 100 | { "Off", "On" }, 101 | { "Off", "On" }, 102 | { "Pixel Perfect", "4:3", "16:9" } 103 | }; 104 | 105 | const vector settingValues = 106 | { 107 | &config::frameLimiter, 108 | &config::disableSpriteLimit, 109 | &screenFiltering, 110 | &cropOverscan, 111 | &aspectRatio 112 | }; 113 | 114 | const vector pauseNames = 115 | { 116 | "Resume", 117 | "Save State", 118 | "Load State", 119 | "Settings", 120 | "File Browser" 121 | }; 122 | 123 | void runCore(void *args) 124 | { 125 | while (!paused) 126 | core::runCycle(); 127 | } 128 | 129 | void audioOutput(void *args) 130 | { 131 | AudioOutBuffer *audioBuffer; 132 | u32 count; 133 | 134 | while (!paused) 135 | { 136 | audoutWaitPlayFinish(&audioBuffer, &count, U64_MAX); 137 | for (int i = 0; i < 1024; i++) 138 | { 139 | s16 sample = apu::audioSample(2.3f); 140 | ((s16*)audioBuffer->buffer)[i * 2] = sample; 141 | ((s16*)audioBuffer->buffer)[i * 2 + 1] = sample; 142 | } 143 | audoutAppendAudioOutBuffer(audioBuffer); 144 | } 145 | } 146 | 147 | void setScreenLayout() 148 | { 149 | if (cropOverscan) 150 | { 151 | bufferPointer = &ppu::displayBuffer[256 * 8]; 152 | bufferHeight = 224; 153 | } 154 | else 155 | { 156 | bufferPointer = ppu::displayBuffer; 157 | bufferHeight = 240; 158 | } 159 | 160 | if (aspectRatio == 0) // Pixel Perfect 161 | screenWidth = (cropOverscan ? 823 : 768); 162 | else if (aspectRatio == 1) // 4:3 163 | screenWidth = 960; 164 | else // 16:9 165 | screenWidth = 1280; 166 | 167 | screenOffsetX = (1280 - screenWidth) / 2; 168 | setTextureFiltering(screenFiltering); 169 | } 170 | 171 | void startCore() 172 | { 173 | paused = false; 174 | appletLockExit(); 175 | audoutInitialize(); 176 | audoutStartAudioOut(); 177 | setupAudioBuffer(); 178 | setScreenLayout(); 179 | threadCreate(&coreThread, runCore, NULL, NULL, 0x8000, 0x30, 1); 180 | threadStart(&coreThread); 181 | threadCreate(&audioThread, audioOutput, NULL, NULL, 0x8000, 0x30, 0); 182 | threadStart(&audioThread); 183 | } 184 | 185 | void stopCore() 186 | { 187 | paused = true; 188 | threadWaitForExit(&coreThread); 189 | threadClose(&coreThread); 190 | threadWaitForExit(&audioThread); 191 | threadClose(&audioThread); 192 | audoutStopAudioOut(); 193 | audoutExit(); 194 | appletUnlockExit(); 195 | } 196 | 197 | void controlsMenu() 198 | { 199 | int selection = 0; 200 | 201 | while (true) 202 | { 203 | vector controlSubitems; 204 | for (unsigned int i = 0; i < controlNames.size(); i++) 205 | { 206 | if (keyMap[i] == 0) 207 | { 208 | controlSubitems.push_back("None"); 209 | } 210 | else 211 | { 212 | string subitem; 213 | int count = 0; 214 | for (unsigned int j = 0; j < controlSubnames.size(); j++) 215 | { 216 | if (keyMap[i] & BIT(j)) 217 | { 218 | count++; 219 | if (count < 5) 220 | { 221 | subitem += controlSubnames[j] + ", "; 222 | } 223 | else 224 | { 225 | subitem += "..."; 226 | break; 227 | } 228 | } 229 | } 230 | controlSubitems.push_back(subitem.substr(0, subitem.size() - ((count == 5) ? 0 : 2))); 231 | } 232 | } 233 | 234 | u32 pressed = menuScreen("Controls", "", "Clear", {}, controlNames, controlSubitems, &selection); 235 | 236 | if (pressed & KEY_A) 237 | { 238 | pressed = 0; 239 | while (pressed == 0 || pressed > KEY_RSTICK_DOWN) 240 | pressed = messageScreen("Controls", {"Press a button to add a mapping to: " + controlNames[selection]}, false); 241 | keyMap[selection] |= pressed; 242 | } 243 | else if (pressed & KEY_B) 244 | { 245 | return; 246 | } 247 | else if ((pressed & KEY_X) && !(pressed & KEY_TOUCH)) 248 | { 249 | keyMap[selection] = 0; 250 | } 251 | } 252 | } 253 | 254 | void settingsMenu() 255 | { 256 | int selection = 0; 257 | 258 | while (true) 259 | { 260 | vector settingSubitems; 261 | for (unsigned int i = 0; i < settingNames.size(); i++) 262 | settingSubitems.push_back(settingSubnames[i][*settingValues[i]]); 263 | 264 | u32 pressed = menuScreen("Settings", "", "Controls", {}, settingNames, settingSubitems, &selection); 265 | 266 | if (pressed & KEY_A) 267 | { 268 | (*settingValues[selection])++; 269 | if (*settingValues[selection] >= settingSubnames[selection].size()) 270 | *settingValues[selection] = 0; 271 | } 272 | else if (pressed & KEY_B) 273 | { 274 | config::save(); 275 | return; 276 | } 277 | else if (pressed & KEY_X) 278 | { 279 | controlsMenu(); 280 | } 281 | } 282 | } 283 | 284 | bool fileBrowser() 285 | { 286 | string romPath = lastPath; 287 | int selection = 0; 288 | 289 | while (true) 290 | { 291 | vector files = dirContents(romPath, ".nes"); 292 | vector icons; 293 | 294 | for (unsigned int i = 0; i < files.size(); i++) 295 | { 296 | if (files[i].find(".nes", (files[i].length() - 4)) != string::npos) 297 | icons.push_back({fileIcon, 64}); 298 | else 299 | icons.push_back({folderIcon, 64}); 300 | } 301 | 302 | u32 pressed = menuScreen("NoiES", "Exit", "Settings", icons, files, {}, &selection); 303 | 304 | if (pressed & KEY_A && files.size() > 0) 305 | { 306 | romPath += "/" + files[selection]; 307 | selection = 0; 308 | 309 | if (romPath.find(".nes", romPath.length() - 4) != string::npos) 310 | { 311 | int result = core::loadRom(romPath); 312 | if (result != 0) 313 | { 314 | vector message = { "The ROM couldn't be loaded." }; 315 | if (result > 2) 316 | message.push_back("It uses an unsupported mapper: " + to_string(result)); 317 | else 318 | message.push_back("The file format is invalid."); 319 | 320 | messageScreen("Unable to load ROM", message, true); 321 | return false; 322 | } 323 | 324 | lastPath = romPath.substr(0, romPath.rfind("/")); 325 | config::save(); 326 | return true; 327 | } 328 | } 329 | else if (pressed & KEY_B && romPath != "sdmc:/") 330 | { 331 | romPath = romPath.substr(0, romPath.rfind("/")); 332 | selection = 0; 333 | } 334 | else if (pressed & KEY_X) 335 | { 336 | settingsMenu(); 337 | } 338 | else if (pressed & KEY_PLUS) 339 | { 340 | return false; 341 | } 342 | } 343 | } 344 | 345 | bool pauseMenu() 346 | { 347 | stopCore(); 348 | 349 | int selection = 0; 350 | 351 | while (paused) 352 | { 353 | u32 pressed = menuScreen("NoiES", "", "", {}, pauseNames, {}, &selection); 354 | 355 | if (pressed & KEY_A) 356 | { 357 | if (selection == 1) // Save State 358 | { 359 | core::saveState(); 360 | } 361 | else if (selection == 2) // Load State 362 | { 363 | core::loadState(); 364 | } 365 | else if (selection == 3) // Settings 366 | { 367 | settingsMenu(); 368 | } 369 | else if (selection == 4) // File Browser 370 | { 371 | core::closeRom(); 372 | if (!fileBrowser()) 373 | return false; 374 | } 375 | } 376 | 377 | if ((pressed & KEY_A && selection != 3) || pressed & KEY_B) 378 | startCore(); 379 | } 380 | 381 | return true; 382 | } 383 | 384 | int main(int argc, char **argv) 385 | { 386 | initRenderer(); 387 | config::load(platformSettings); 388 | 389 | if (!fileBrowser()) 390 | { 391 | deinitRenderer(); 392 | return 0; 393 | } 394 | 395 | startCore(); 396 | 397 | while (appletMainLoop()) 398 | { 399 | hidScanInput(); 400 | u64 pressed[] = { hidKeysDown(CONTROLLER_P1_AUTO), hidKeysDown(CONTROLLER_PLAYER_2) }; 401 | u64 released[] = { hidKeysUp(CONTROLLER_P1_AUTO), hidKeysUp(CONTROLLER_PLAYER_2) }; 402 | 403 | for (int i = 0; i < 2; i++) 404 | { 405 | for (int j = 0; j < 8; j++) 406 | { 407 | if (pressed[i] & keyMap[j]) 408 | core::pressKey(i, j); 409 | else if (released[i] & keyMap[j]) 410 | core::releaseKey(i, j); 411 | } 412 | } 413 | 414 | if (pressed[0] & keyMap[8]) 415 | { 416 | if (!pauseMenu()) 417 | break; 418 | } 419 | 420 | clearDisplay(0); 421 | mutex::lock(ppu::displayMutex); 422 | drawImage(bufferPointer, 256, bufferHeight, false, screenOffsetX, 0, screenWidth, 720, 0); 423 | mutex::unlock(ppu::displayMutex); 424 | refreshDisplay(); 425 | } 426 | 427 | core::closeRom(); 428 | stopCore(); 429 | deinitRenderer(); 430 | return 0; 431 | } 432 | -------------------------------------------------------------------------------- /src/switch/mutex.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #include 21 | 22 | namespace mutex 23 | { 24 | 25 | void *create() 26 | { 27 | Mutex *mutex = new Mutex; 28 | mutexInit(mutex); 29 | return mutex; 30 | } 31 | 32 | void lock(void *mutex) 33 | { 34 | mutexLock((Mutex*)mutex); 35 | } 36 | 37 | void unlock(void *mutex) 38 | { 39 | mutexUnlock((Mutex*)mutex); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/switch/ui.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | #include "ui.h" 31 | 32 | ColorSetId systemTheme; 33 | u32 *font, *fileIcon, *folderIcon; 34 | u32 uiPalette[6]; 35 | 36 | EGLDisplay display; 37 | EGLContext context; 38 | EGLSurface surface; 39 | GLuint program, vbo, texture; 40 | 41 | bool showSelector = true; 42 | 43 | AudioOutBuffer audioBuffers[2]; 44 | s16 *audioData[2]; 45 | 46 | const int charWidths[] = 47 | { 48 | 11, 9, 11, 20, 18, 28, 24, 7, 12, 12, 49 | 14, 24, 9, 12, 9, 16, 21, 21, 21, 21, 50 | 21, 21, 21, 21, 21, 21, 9, 9, 26, 24, 51 | 26, 18, 28, 24, 21, 24, 26, 20, 20, 27, 52 | 23, 9, 17, 21, 16, 31, 27, 29, 19, 29, 53 | 20, 18, 21, 26, 24, 37, 21, 21, 24, 12, 54 | 16, 12, 18, 16, 9, 20, 21, 18, 21, 20, 55 | 10, 20, 20, 8, 12, 19, 9, 30, 20, 21, 56 | 21, 21, 12, 16, 12, 20, 17, 29, 17, 17, 57 | 16, 9, 8, 9, 12, 0, 40, 40, 40, 40 58 | }; 59 | 60 | typedef struct 61 | { 62 | float position[2]; 63 | float texCoord[2]; 64 | } Vertex; 65 | 66 | const char *vertexShader = R"( 67 | #version 330 core 68 | precision mediump float; 69 | 70 | layout (location = 0) in vec2 inPos; 71 | layout (location = 1) in vec2 inTexCoord; 72 | out vec2 vtxTexCoord; 73 | 74 | void main() 75 | { 76 | gl_Position = vec4(-1.0 + inPos.x / 640, 1.0 - inPos.y / 360, 0.0, 1.0); 77 | vtxTexCoord = inTexCoord; 78 | } 79 | )"; 80 | 81 | const char *fragmentShader = R"( 82 | #version 330 core 83 | precision mediump float; 84 | 85 | in vec2 vtxTexCoord; 86 | out vec4 fragColor; 87 | uniform sampler2D texDiffuse; 88 | 89 | void main() 90 | { 91 | fragColor = texture(texDiffuse, vtxTexCoord); 92 | }; 93 | )"; 94 | 95 | u32 rgbaToU32(u8 r, u8 g, u8 b, u8 a) 96 | { 97 | return (r << 24) | (g << 16) | (b << 8) | a; 98 | } 99 | 100 | u32 *bmpToTexture(string filename) 101 | { 102 | FILE *bmp = fopen(filename.c_str(), "rb"); 103 | if (!bmp) 104 | return NULL; 105 | 106 | // Read the header 107 | u8 header[70]; 108 | fread(header, sizeof(u8), 70, bmp); 109 | int width = *(int*)&header[18]; 110 | int height = *(int*)&header[22]; 111 | 112 | // Convert the bitmap data to RGBA format 113 | u32 *tex = new u32[width * height]; 114 | for (int y = 1; y <= height; y++) 115 | { 116 | for (int x = 0; x < width; x++) 117 | { 118 | u8 b; fread(&b, sizeof(u8), 1, bmp); 119 | u8 g; fread(&g, sizeof(u8), 1, bmp); 120 | u8 r; fread(&r, sizeof(u8), 1, bmp); 121 | u8 a; fread(&a, sizeof(u8), 1, bmp); 122 | tex[(height - y) * width + x] = rgbaToU32(r, g, b, a); 123 | } 124 | } 125 | 126 | fclose(bmp); 127 | return tex; 128 | } 129 | 130 | int stringWidth(string str) 131 | { 132 | int width = 0; 133 | for (unsigned int i = 0; i < str.size(); i++) 134 | width += charWidths[str[i] - 32]; 135 | return width; 136 | } 137 | 138 | void loadTheme() 139 | { 140 | // Get the current system theme 141 | setsysInitialize(); 142 | setsysGetColorSetId(&systemTheme); 143 | setsysExit(); 144 | 145 | // Load the font bitmaps corresponding to the theme 146 | romfsInit(); 147 | string theme = (systemTheme == ColorSetId_Light) ? "light" : "dark"; 148 | font = bmpToTexture("romfs:/font.bmp"); 149 | fileIcon = bmpToTexture("romfs:/file-" + theme + ".bmp"); 150 | folderIcon = bmpToTexture("romfs:/folder-" + theme + ".bmp"); 151 | romfsExit(); 152 | 153 | // Set the theme colors 154 | if (systemTheme == ColorSetId_Light) 155 | { 156 | uiPalette[0] = rgbaToU32(235, 235, 235, 255); 157 | uiPalette[1] = rgbaToU32( 45, 45, 45, 255); 158 | uiPalette[2] = rgbaToU32(205, 205, 205, 255); 159 | uiPalette[3] = rgbaToU32(255, 255, 255, 255); 160 | uiPalette[4] = rgbaToU32( 50, 215, 210, 255); 161 | uiPalette[5] = rgbaToU32( 50, 80, 240, 255); 162 | } 163 | else 164 | { 165 | uiPalette[0] = rgbaToU32( 45, 45, 45, 255); 166 | uiPalette[1] = rgbaToU32(255, 255, 255, 255); 167 | uiPalette[2] = rgbaToU32( 75, 75, 75, 255); 168 | uiPalette[3] = rgbaToU32( 35, 35, 35, 255); 169 | uiPalette[4] = rgbaToU32( 85, 185, 225, 255); 170 | uiPalette[5] = rgbaToU32( 0, 255, 200, 255); 171 | } 172 | } 173 | 174 | void initRenderer() 175 | { 176 | // Initialize EGL 177 | display = eglGetDisplay(EGL_DEFAULT_DISPLAY); 178 | eglInitialize(display, NULL, NULL); 179 | eglBindAPI(EGL_OPENGL_API); 180 | EGLConfig config; 181 | EGLint numConfigs; 182 | eglChooseConfig(display, {}, &config, 1, &numConfigs); 183 | surface = eglCreateWindowSurface(display, config, nwindowGetDefault(), NULL); 184 | context = eglCreateContext(display, config, EGL_NO_CONTEXT, {}); 185 | eglMakeCurrent(display, surface, surface, context); 186 | 187 | gladLoadGL(); 188 | 189 | GLint vertShader = glCreateShader(GL_VERTEX_SHADER); 190 | glShaderSource(vertShader, 1, &vertexShader, NULL); 191 | glCompileShader(vertShader); 192 | 193 | GLint fragShader = glCreateShader(GL_FRAGMENT_SHADER); 194 | glShaderSource(fragShader, 1, &fragmentShader, NULL); 195 | glCompileShader(fragShader); 196 | 197 | program = glCreateProgram(); 198 | glAttachShader(program, vertShader); 199 | glAttachShader(program, fragShader); 200 | glLinkProgram(program); 201 | 202 | glDeleteShader(vertShader); 203 | glDeleteShader(fragShader); 204 | 205 | glGenBuffers(1, &vbo); 206 | glBindBuffer(GL_ARRAY_BUFFER, vbo); 207 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position)); 208 | glEnableVertexAttribArray(0); 209 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texCoord)); 210 | glEnableVertexAttribArray(1); 211 | 212 | glGenTextures(1, &texture); 213 | glBindTexture(GL_TEXTURE_2D, texture); 214 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 215 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 216 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 217 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 218 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 219 | 220 | glEnable(GL_BLEND); 221 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 222 | 223 | loadTheme(); 224 | } 225 | 226 | void deinitRenderer() 227 | { 228 | glDeleteProgram(program); 229 | glDeleteTextures(1, &texture); 230 | glDeleteBuffers(1, &vbo); 231 | 232 | // Deinitialize EGL 233 | eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); 234 | eglDestroyContext(display, context); 235 | eglDestroySurface(display, surface); 236 | eglTerminate(display); 237 | } 238 | 239 | void setSurface() 240 | { 241 | glUseProgram(program); 242 | glBindFramebuffer(GL_FRAMEBUFFER, 0); 243 | glViewport(0, 0, 1280, 720); 244 | glBindVertexArray(0); 245 | } 246 | 247 | void drawImage(u32 *image, int imageWidth, int imageHeight, bool reverse, float x, float y, float width, float height, int rotation) 248 | { 249 | Vertex dimensions[] = 250 | { 251 | { { x + width, y + height }, { 1.0f, 1.0f } }, 252 | { { x, y + height }, { 0.0f, 1.0f } }, 253 | { { x, y }, { 0.0f, 0.0f } }, 254 | { { x + width, y }, { 1.0f, 0.0f } } 255 | }; 256 | 257 | // Rotate the image 90 degrees clockwise for every rotation 258 | for (int i = 0; i < rotation; i++) 259 | { 260 | int size = sizeof(dimensions[0].position); 261 | Vertex *copy = new Vertex[sizeof(dimensions) / sizeof(Vertex)]; 262 | memcpy(copy, dimensions, sizeof(dimensions)); 263 | for (int k = 0; k < 8; k += 4) 264 | { 265 | memcpy(dimensions[k ].position, copy[k + 1].position, size); 266 | memcpy(dimensions[k + 1].position, copy[k + 2].position, size); 267 | memcpy(dimensions[k + 2].position, copy[k + 3].position, size); 268 | memcpy(dimensions[k + 3].position, copy[k ].position, size); 269 | } 270 | delete[] copy; 271 | } 272 | 273 | glBindBuffer(GL_ARRAY_BUFFER, vbo); 274 | glBufferData(GL_ARRAY_BUFFER, sizeof(dimensions), dimensions, GL_DYNAMIC_DRAW); 275 | 276 | GLenum format = reverse ? GL_BGRA : GL_RGBA; 277 | GLenum type = reverse ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_INT_8_8_8_8; 278 | glBindTexture(GL_TEXTURE_2D, texture); 279 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 0, format, type, image); 280 | 281 | setSurface(); 282 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 283 | } 284 | 285 | void drawString(string str, float x, float y, int size, bool right, u32 color) 286 | { 287 | int width = stringWidth(str); 288 | u32 *tex = new u32[width * 48]; 289 | int currentX = 0; 290 | 291 | // Copy the characters from the font bitmap into the string texture 292 | for (unsigned int i = 0; i < str.size(); i++) 293 | { 294 | int col = (str[i] - 32) % 10; 295 | int row = (str[i] - 32) / 10; 296 | 297 | for (int j = 0; j < 48; j++) 298 | for (int k = 0; k < charWidths[str[i] - 32]; k++) 299 | tex[j * width + currentX + k] = (color & ~0xFF) | (font[(row * 512 + col) * 48 + j * 512 + k] & 0xFF); 300 | 301 | currentX += charWidths[str[i] - 32]; 302 | } 303 | 304 | if (right) // Align the string to the right 305 | x -= width * size / 48; 306 | 307 | drawImage(tex, width, 48, false, x, y, width * size / 48, size, 0); 308 | delete[] tex; 309 | } 310 | 311 | void drawLine(float x1, float y1, float x2, float y2, u32 color) 312 | { 313 | Vertex dimensions[] = 314 | { 315 | { { x1, y1 }, { 0.0f, 0.0f } }, 316 | { { x2, y2 }, { 0.0f, 0.0f } } 317 | }; 318 | 319 | glBindBuffer(GL_ARRAY_BUFFER, vbo); 320 | glBufferData(GL_ARRAY_BUFFER, sizeof(dimensions), dimensions, GL_DYNAMIC_DRAW); 321 | 322 | glBindTexture(GL_TEXTURE_2D, texture); 323 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color); 324 | 325 | setSurface(); 326 | glDrawArrays(GL_LINES, 0, 2); 327 | } 328 | 329 | void setTextureFiltering(bool enabled) 330 | { 331 | glBindTexture(GL_TEXTURE_2D, texture); 332 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, enabled ? GL_LINEAR : GL_NEAREST); 333 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, enabled ? GL_LINEAR : GL_NEAREST); 334 | } 335 | 336 | void clearDisplay(u8 color) 337 | { 338 | setSurface(); 339 | float clear = (float)color / 255; 340 | glClearColor(clear, clear, clear, 1.0f); 341 | glClear(GL_COLOR_BUFFER_BIT); 342 | } 343 | 344 | void refreshDisplay() 345 | { 346 | eglSwapBuffers(display, surface); 347 | } 348 | 349 | u32 menuScreen(string title, string actionPlus, string actionX, vector icons, vector items, vector subitems, int *selection) 350 | { 351 | if (actionPlus != "") 352 | actionPlus = "\x83 " + actionPlus + " "; 353 | if (actionX != "") 354 | actionX = "\x82 " + actionX + " "; 355 | string actionB = "\x81 Back "; 356 | string actionA = "\x80 OK"; 357 | 358 | unsigned int boundsAB = 1218 - (stringWidth(actionA) + charWidths[0] * 2.5) * 34 / 48; 359 | unsigned int boundsBX = boundsAB - stringWidth(actionB) * 34 / 48; 360 | unsigned int boundsXPlus = boundsBX - stringWidth(actionX) * 34 / 48; 361 | unsigned int boundsPlus = boundsXPlus - stringWidth(actionPlus) * 34 / 48; 362 | 363 | unsigned int position = *selection; 364 | bool upHeld = false; 365 | bool downHeld = false; 366 | bool scroll = false; 367 | chrono::steady_clock::time_point timeHeld; 368 | 369 | bool touchStarted = false; 370 | bool touchScroll = false; 371 | touchPosition touch, touchMove; 372 | 373 | setTextureFiltering(true); 374 | 375 | while (true) 376 | { 377 | clearDisplay((systemTheme == ColorSetId_Light) ? 235 : 45); 378 | 379 | drawString(title, 72, 30, 42, false, uiPalette[1]); 380 | drawLine(30, 88, 1250, 88, uiPalette[1]); 381 | drawLine(30, 648, 1250, 648, uiPalette[1]); 382 | drawString(actionPlus + actionX + actionB + actionA, 1218, 667, 34, true, uiPalette[1]); 383 | 384 | hidScanInput(); 385 | u32 pressed = hidKeysDown(CONTROLLER_P1_AUTO); 386 | u32 released = hidKeysUp(CONTROLLER_P1_AUTO); 387 | 388 | if (pressed & KEY_UP && position > 0) 389 | { 390 | if (!showSelector) 391 | showSelector = true; 392 | else 393 | position--; 394 | upHeld = true; 395 | timeHeld = chrono::steady_clock::now(); 396 | } 397 | else if (pressed & KEY_DOWN && position < items.size() - 1) 398 | { 399 | if (!showSelector) 400 | showSelector = true; 401 | else 402 | position++; 403 | downHeld = true; 404 | timeHeld = chrono::steady_clock::now(); 405 | } 406 | else if ((pressed & (KEY_A | KEY_B)) || (actionX != "" && (pressed & KEY_X)) || (actionPlus != "" && (pressed & KEY_PLUS))) 407 | { 408 | if (!(pressed & KEY_A) || showSelector) 409 | { 410 | showSelector = true; 411 | *selection = position; 412 | return pressed; 413 | } 414 | showSelector = true; 415 | } 416 | 417 | if (released & KEY_UP) 418 | { 419 | upHeld = false; 420 | scroll = false; 421 | } 422 | if (released & KEY_DOWN) 423 | { 424 | downHeld = false; 425 | scroll = false; 426 | } 427 | 428 | // Scroll continuously when up or down is held 429 | if ((upHeld && position > 0) || (downHeld && position < items.size() - 1)) 430 | { 431 | chrono::duration elapsed = chrono::steady_clock::now() - timeHeld; 432 | if (!scroll && elapsed.count() > 0.5f) 433 | scroll = true; 434 | if (scroll && elapsed.count() > 0.1f) 435 | { 436 | position += (upHeld && position > 0) ? -1 : 1; 437 | timeHeld = chrono::steady_clock::now(); 438 | } 439 | } 440 | 441 | if (hidTouchCount() > 0) 442 | { 443 | if (!touchStarted) 444 | { 445 | hidTouchRead(&touch, 0); 446 | touchStarted = true; 447 | touchScroll = false; 448 | showSelector = false; 449 | } 450 | hidTouchRead(&touchMove, 0); 451 | 452 | if (touchScroll) 453 | { 454 | // Scroll with a dragged touch 455 | int newPos = *selection + (int)(touch.py - touchMove.py) / 70; 456 | if (items.size() <= 7) 457 | position = 0; 458 | else if (newPos > (int)items.size() - 4) 459 | position = items.size() - 4; 460 | else if (newPos < 3) 461 | position = 3; 462 | else 463 | position = newPos; 464 | } 465 | else if (touchMove.px > touch.px + 25 || touchMove.px < touch.px - 25 || touchMove.py > touch.py + 25 || touchMove.py < touch.py - 25) 466 | { 467 | // Prepare to scroll with a dragged touch 468 | touchScroll = true; 469 | if (items.size() <= 7) 470 | *selection = position; 471 | if (position > items.size() - 4) 472 | *selection = items.size() - 4; 473 | else if (position < 3) 474 | *selection = 3; 475 | else 476 | *selection = position; 477 | } 478 | } 479 | else 480 | { 481 | // Simulate a button press if the button text is touched 482 | if (!touchScroll && touch.py >= 650) 483 | { 484 | if (touch.px >= boundsBX && touch.px < boundsAB) 485 | return KEY_B | KEY_TOUCH; 486 | else if (touch.px >= boundsXPlus && touch.px < boundsBX) 487 | return KEY_X | KEY_TOUCH; 488 | else if (touch.px >= boundsPlus && touch.px < boundsXPlus) 489 | return KEY_PLUS | KEY_TOUCH; 490 | } 491 | touchStarted = false; 492 | } 493 | 494 | if (items.size() > 0) 495 | drawLine(90, 124, 1190, 124, uiPalette[2]); 496 | 497 | for (unsigned int i = 0; i < 7; i++) 498 | { 499 | if (i < items.size()) 500 | { 501 | unsigned int row; 502 | if (position < 4 || items.size() <= 7) 503 | row = i; 504 | else if (position > items.size() - 4) 505 | row = items.size() - 7 + i; 506 | else 507 | row = i + position - 3; 508 | 509 | // Simulate an A press on a selection if its row is touched 510 | if (!touchStarted && !touchScroll && touch.px >= 90 && touch.px < 1190 && touch.py >= 124 + i * 70 && touch.py < 194 + i * 70) 511 | { 512 | *selection = row; 513 | return KEY_A | KEY_TOUCH; 514 | } 515 | 516 | // Draw the selection box and row lines 517 | if (row == position && showSelector) 518 | { 519 | drawImage(&uiPalette[3], 1, 1, false, 90, 125 + i * 70, 1100, 69, 0); 520 | drawImage(&uiPalette[4], 1, 1, false, 89, 121 + i * 70, 1103, 5, 0); 521 | drawImage(&uiPalette[4], 1, 1, false, 89, 191 + i * 70, 1103, 5, 0); 522 | drawImage(&uiPalette[4], 1, 1, false, 88, 122 + i * 70, 5, 73, 0); 523 | drawImage(&uiPalette[4], 1, 1, false, 1188, 122 + i * 70, 5, 73, 0); 524 | } 525 | else 526 | { 527 | drawLine(90, 194 + i * 70, 1190, 194 + i * 70, uiPalette[2]); 528 | } 529 | 530 | // Draw the rows 531 | if (icons.size() > row) 532 | { 533 | drawImage(icons[row].texture, icons[row].size, icons[row].size, false, 105, 126 + i * 70, 64, 64, 0); 534 | drawString(items[row], 184, 140 + i * 70, 38, false, uiPalette[1]); 535 | } 536 | else 537 | { 538 | drawString(items[row], 105, 140 + i * 70, 38, false, uiPalette[1]); 539 | } 540 | if (subitems.size() > row) 541 | drawString(subitems[row], 1175, 143 + i * 70, 32, true, uiPalette[5]); 542 | } 543 | } 544 | 545 | refreshDisplay(); 546 | } 547 | } 548 | 549 | u32 messageScreen(string title, vector text, bool exit) 550 | { 551 | clearDisplay((systemTheme == ColorSetId_Light) ? 235 : 45); 552 | 553 | drawString(title, 72, 30, 42, false, uiPalette[1]); 554 | drawLine(30, 88, 1250, 88, uiPalette[1]); 555 | drawLine(30, 648, 1250, 648, uiPalette[1]); 556 | 557 | if (exit) 558 | drawString("\x83 Exit", 1218, 667, 34, true, uiPalette[1]); 559 | 560 | for (unsigned int i = 0; i < text.size(); i++) 561 | drawString(text[i], 90, 124 + i * 38, 38, false, uiPalette[1]); 562 | 563 | refreshDisplay(); 564 | 565 | while (true) 566 | { 567 | hidScanInput(); 568 | u32 pressed = hidKeysDown(CONTROLLER_P1_AUTO); 569 | if ((!exit && pressed) || (pressed & KEY_PLUS)) 570 | return pressed; 571 | } 572 | } 573 | 574 | void setupAudioBuffer() 575 | { 576 | for (int i = 0; i < 2; i++) 577 | { 578 | int size = 1024 * 2 * sizeof(s16); 579 | int alignedSize = (size + 0xFFF) & ~0xFFF; 580 | audioData[i] = (s16*)memalign(0x1000, size); 581 | memset(audioData[i], 0, alignedSize); 582 | audioBuffers[i].next = NULL; 583 | audioBuffers[i].buffer = audioData[i]; 584 | audioBuffers[i].buffer_size = alignedSize; 585 | audioBuffers[i].data_size = size; 586 | audioBuffers[i].data_offset = 0; 587 | audoutAppendAudioOutBuffer(&audioBuffers[i]); 588 | } 589 | } 590 | 591 | vector dirContents(string directory, string extension) 592 | { 593 | vector contents; 594 | DIR *dir = opendir(directory.c_str()); 595 | dirent *entry; 596 | 597 | while ((entry = readdir(dir))) 598 | { 599 | string name = entry->d_name; 600 | if (entry->d_type == DT_DIR || name.find(extension, (name.length() - extension.length())) != string::npos) 601 | contents.push_back(name); 602 | } 603 | 604 | closedir(dir); 605 | sort(contents.begin(), contents.end()); 606 | return contents; 607 | } 608 | -------------------------------------------------------------------------------- /src/switch/ui.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 Hydr8gon 3 | 4 | This file is part of NoiES. 5 | 6 | NoiES is free software: you can redistribute it and/or modify it 7 | under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | NoiES is distributed in the hope that it will be useful, but 12 | WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with NoiES. If not, see . 18 | */ 19 | 20 | #ifndef UI_H 21 | #define UI_H 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | using namespace std; 28 | 29 | typedef struct 30 | { 31 | u32 *texture; 32 | int size; 33 | } Icon; 34 | 35 | extern ColorSetId systemTheme; 36 | extern u32 *fileIcon; 37 | extern u32 *folderIcon; 38 | 39 | u32 rgbaToU32(u8 r, u8 g, u8 b, u8 a); 40 | 41 | void initRenderer(); 42 | void deinitRenderer(); 43 | 44 | void drawImage(u32 *image, int imageWidth, int imageHeight, bool reverse, float x, float y, float width, float height, int rotation); 45 | void drawString(string str, float x, float y, int size, bool right, u32 color); 46 | void drawLine(float x1, float y1, float x2, float y2, u32 color); 47 | void setTextureFiltering(bool enabled); 48 | void clearDisplay(u8 color); 49 | void refreshDisplay(); 50 | 51 | u32 menuScreen(string title, string actionPlus, string actionX, vector icons, vector items, vector subitems, int *selection); 52 | u32 messageScreen(string title, vector text, bool exit); 53 | 54 | void setupAudioBuffer(); 55 | 56 | vector dirContents(string directory, string extension); 57 | 58 | #endif // UI_H 59 | --------------------------------------------------------------------------------