Initial commit

This commit is contained in:
Anthony Wang 2020-05-18 14:42:11 -05:00
parent 9a85d09f29
commit 42328080e6
12 changed files with 93 additions and 29 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
.sconsign.dblite .sconsign.dblite
laines laines
*.o *.o
roms
.nes

View file

@ -5,46 +5,46 @@
namespace APU { namespace APU {
Nes_Apu apu; //Nes_Apu apu;
Blip_Buffer buf; //Blip_Buffer buf;
const int OUT_SIZE = 4096; const int OUT_SIZE = 4096;
blip_sample_t outBuf[OUT_SIZE]; //blip_sample_t outBuf[OUT_SIZE];
void init() /*void init()
{ {
buf.sample_rate(96000); buf.sample_rate(96000);
buf.clock_rate(1789773); buf.clock_rate(1789773);
apu.output(&buf); apu.output(&buf);
apu.dmc_reader(CPU::dmc_read); apu.dmc_reader(CPU::dmc_read);
} }*/
void reset() /*void reset()
{ {
apu.reset(); apu.reset();
buf.clear(); buf.clear();
} }*/
template <bool write> u8 access(int elapsed, u16 addr, u8 v) template <bool write> u8 access(int elapsed, u16 addr, u8 v)
{ {
if (write) /*if (write)
apu.write_register(elapsed, addr, v); apu.write_register(elapsed, addr, v);
else if (addr == apu.status_addr) else if (addr == apu.status_addr)
v = apu.read_status(elapsed); v = apu.read_status(elapsed);*/
return v; return v;
} }
template u8 access<0>(int, u16, u8); template u8 access<1>(int, u16, u8); template u8 access<0>(int, u16, u8); template u8 access<1>(int, u16, u8);
void run_frame(int elapsed) /*void run_frame(int elapsed)
{ {
apu.end_frame(elapsed); apu.end_frame(elapsed);
buf.end_frame(elapsed); buf.end_frame(elapsed);
if (buf.samples_avail() >= OUT_SIZE) if (buf.samples_avail() >= OUT_SIZE)
GUI::new_samples(outBuf, buf.read_samples(outBuf, OUT_SIZE)); GUI::new_samples(outBuf, buf.read_samples(outBuf, OUT_SIZE));
} }*/
} }

View file

@ -6,6 +6,7 @@
#include "mappers/mapper2.hpp" #include "mappers/mapper2.hpp"
#include "mappers/mapper3.hpp" #include "mappers/mapper3.hpp"
#include "mappers/mapper4.hpp" #include "mappers/mapper4.hpp"
#include "mappers/mapper7.hpp"
#include "ppu.hpp" #include "ppu.hpp"
#include "cartridge.hpp" #include "cartridge.hpp"
@ -57,11 +58,12 @@ void load(const char* fileName)
case 2: mapper = new Mapper2(rom); break; case 2: mapper = new Mapper2(rom); break;
case 3: mapper = new Mapper3(rom); break; case 3: mapper = new Mapper3(rom); break;
case 4: mapper = new Mapper4(rom); break; case 4: mapper = new Mapper4(rom); break;
case 7: mapper = new Mapper7(rom); break;
} }
CPU::power(); CPU::power();
PPU::reset(); PPU::reset();
APU::reset(); //APU::reset();
} }
bool loaded() bool loaded()

View file

@ -275,7 +275,7 @@ void run_frame()
exec(); exec();
} }
APU::run_frame(elapsed()); //APU::run_frame(elapsed());
} }

View file

@ -1,7 +1,6 @@
#include <csignal> #include <csignal>
#include <SDL2/SDL_image.h> #include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h> #include <SDL2/SDL_ttf.h>
#include "Sound_Queue.h"
#include "apu.hpp" #include "apu.hpp"
#include "cartridge.hpp" #include "cartridge.hpp"
#include "cpu.hpp" #include "cpu.hpp"
@ -18,7 +17,7 @@ SDL_Texture* gameTexture;
SDL_Texture* background; SDL_Texture* background;
TTF_Font* font; TTF_Font* font;
u8 const* keys; u8 const* keys;
Sound_Queue* soundQueue; //Sound_Queue* soundQueue;
SDL_Joystick* joystick[] = { nullptr, nullptr }; SDL_Joystick* joystick[] = { nullptr, nullptr };
// Menus: // Menus:
@ -51,9 +50,9 @@ void init()
for (int i = 0; i < SDL_NumJoysticks(); i++) for (int i = 0; i < SDL_NumJoysticks(); i++)
joystick[i] = SDL_JoystickOpen(i); joystick[i] = SDL_JoystickOpen(i);
APU::init(); //APU::init();
soundQueue = new Sound_Queue; //soundQueue = new Sound_Queue;
soundQueue->init(96000); //soundQueue->init(96000);
// Initialize graphics structures: // Initialize graphics structures:
window = SDL_CreateWindow ("LaiNES", window = SDL_CreateWindow ("LaiNES",
@ -203,10 +202,10 @@ void new_frame(u32* pixels)
SDL_UpdateTexture(gameTexture, NULL, pixels, WIDTH * sizeof(u32)); SDL_UpdateTexture(gameTexture, NULL, pixels, WIDTH * sizeof(u32));
} }
void new_samples(const blip_sample_t* samples, size_t count) /*void new_samples(const blip_sample_t* samples, size_t count)
{ {
soundQueue->write(samples, count); soundQueue->write(samples, count);
} }*/
/* Render the screen */ /* Render the screen */
void render() void render()

View file

@ -5,9 +5,9 @@ namespace APU {
template <bool write> u8 access(int elapsed, u16 addr, u8 v = 0); template <bool write> u8 access(int elapsed, u16 addr, u8 v = 0);
void run_frame(int elapsed); //void run_frame(int elapsed);
void reset(); //void reset();
void init(); //void init();
} }

View file

@ -1,7 +1,6 @@
#pragma once #pragma once
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <string> #include <string>
#include <Nes_Apu.h>
#include "common.hpp" #include "common.hpp"
namespace GUI { namespace GUI {
@ -24,7 +23,7 @@ void render_texture(SDL_Texture* texture, int x, int y);
u8 get_joypad_state(int n); u8 get_joypad_state(int n);
void new_frame(u32* pixels); void new_frame(u32* pixels);
void new_samples(const blip_sample_t* samples, size_t count); //void new_samples(const blip_sample_t* samples, size_t count);
void set_size(int mul); void set_size(int mul);
} }

View file

@ -0,0 +1,19 @@
#pragma once
#include "mapper.hpp"
#include "ppu.hpp"
class Mapper7 : public Mapper
{
u8 regs[1];
void apply();
public:
Mapper7(u8* rom) : Mapper(rom)
{
regs[0] = 0;
apply();
}
u8 write(u16 addr, u8 v);
u8 chr_write(u16 addr, u8 v);
};

View file

@ -5,8 +5,7 @@ namespace PPU {
enum Scanline { VISIBLE, POST, NMI, PRE }; enum Scanline { VISIBLE, POST, NMI, PRE };
enum Mirroring { VERTICAL, HORIZONTAL }; enum Mirroring { VERTICAL, HORIZONTAL, ONE_SCREEN_HI, ONE_SCREEN_LO, FOUR_SCREEN };
/* Sprite buffer */ /* Sprite buffer */
struct Sprite struct Sprite
{ {

View file

@ -38,8 +38,10 @@ void Mapper1::apply()
// Set mirroring: // Set mirroring:
switch (regs[0] & 0b11) switch (regs[0] & 0b11)
{ {
case 2: set_mirroring(PPU::VERTICAL); break; case 0: set_mirroring(PPU::ONE_SCREEN_LO); break;
case 3: set_mirroring(PPU::HORIZONTAL); break; case 1: set_mirroring(PPU::ONE_SCREEN_HI); break;
case 2: set_mirroring(PPU::VERTICAL); break;
case 3: set_mirroring(PPU::HORIZONTAL); break;
} }
} }

39
src/mappers/mapper7.cpp Normal file
View file

@ -0,0 +1,39 @@
#include "ppu.hpp"
#include "mappers/mapper7.hpp"
/* Based off of https://wiki.nesdev.com/w/index.php/AxROM */
/* Apply the registers state */
void Mapper7::apply()
{
/*
* 32 kb PRG ROM Banks
* 0x8000 - 0xFFFF swappable
*/
map_prg<32>(0, regs[0] & 0b00001111);
/* 8k of CHR (ram) */
map_chr<8>(0, 0);
/* Mirroring based on bit 5 */
set_mirroring((regs[0] & 0b00010000) ? PPU::ONE_SCREEN_HI : PPU::ONE_SCREEN_LO);
}
u8 Mapper7::write(u16 addr, u8 v)
{
/* check for bus contingency? (addr & 0x8000 == v?)
* Seems not neccesary */
/* bank switching */
if (addr & 0x8000)
{
regs[0] = v;
apply();
}
return v;
}
u8 Mapper7::chr_write(u16 addr, u8 v)
{
return chr[addr] = v;
}

View file

@ -42,6 +42,9 @@ u16 nt_mirror(u16 addr)
{ {
case VERTICAL: return addr % 0x800; case VERTICAL: return addr % 0x800;
case HORIZONTAL: return ((addr / 2) & 0x400) + (addr % 0x400); case HORIZONTAL: return ((addr / 2) & 0x400) + (addr % 0x400);
case ONE_SCREEN_LO:
case ONE_SCREEN_HI:
return ((addr & 0x3ff) + ((mirroring == ONE_SCREEN_HI) ? 0x400 : 0x0)) - 0x2000;
default: return addr - 0x2000; default: return addr - 0x2000;
} }
} }