Some PPU code
This commit is contained in:
parent
4abf87e278
commit
7a954c705e
@ -16,7 +16,9 @@ add_executable(nes
|
|||||||
src/Mapper/Mapper.cpp
|
src/Mapper/Mapper.cpp
|
||||||
src/Mapper/Mapper.h
|
src/Mapper/Mapper.h
|
||||||
src/Mapper/Mapper0.cpp
|
src/Mapper/Mapper0.cpp
|
||||||
src/Mapper/Mapper0.h src/Ppu.cpp src/Ppu.h)
|
src/Mapper/Mapper0.h src/Ppu.cpp src/Ppu.h
|
||||||
|
src/Window.cpp
|
||||||
|
src/Window.h)
|
||||||
|
|
||||||
find_package(SDL2 CONFIG REQUIRED)
|
find_package(SDL2 CONFIG REQUIRED)
|
||||||
target_link_libraries(nes PRIVATE SDL2::SDL2)
|
target_link_libraries(nes PRIVATE SDL2::SDL2)
|
||||||
12
src/Bus.cpp
12
src/Bus.cpp
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include "Bus.h"
|
#include "Bus.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
namespace nes {
|
namespace nes {
|
||||||
|
|
||||||
@ -17,7 +18,7 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
else if(address >= 0x2000 && address < 0x4000) {
|
else if(address >= 0x2000 && address < 0x4000) {
|
||||||
std::cout << "PPU read at address: " << address << std::endl;
|
std::cout << "PPU read at address: " << address << std::endl;
|
||||||
return 0;
|
return _ppu->read(address & 0x0007);
|
||||||
}
|
}
|
||||||
else if(address >= 0x8000) {
|
else if(address >= 0x8000) {
|
||||||
return _cartridge->readPrg(address);
|
return _cartridge->readPrg(address);
|
||||||
@ -32,13 +33,18 @@ namespace nes {
|
|||||||
}
|
}
|
||||||
else if(address >= 0x2000 && address < 0x4000) {
|
else if(address >= 0x2000 && address < 0x4000) {
|
||||||
std::cout << "PPU write at address: " << address << std::endl;
|
std::cout << "PPU write at address: " << address << std::endl;
|
||||||
|
_ppu->write(address & 0x0007, value);
|
||||||
}
|
}
|
||||||
else if(address >= 0x8000) {
|
else if(address >= 0x8000) {
|
||||||
std::cout << "Cartridge write at address: " << address << std::endl;
|
std::cout << "Cartridge write at address: " << address << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bus::connect(Cartridge *cartridge) {
|
void Bus::connect(std::shared_ptr<Cartridge> cartridge) {
|
||||||
_cartridge = cartridge;
|
_cartridge = std::move(cartridge);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Bus::connect(std::shared_ptr<Ppu> ppu) {
|
||||||
|
_ppu = std::move(ppu);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -6,6 +6,7 @@
|
|||||||
#define NES_BUS_H
|
#define NES_BUS_H
|
||||||
|
|
||||||
#include "Cartridge.h"
|
#include "Cartridge.h"
|
||||||
|
#include "Ppu.h"
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@ -17,11 +18,13 @@ namespace nes {
|
|||||||
Bus();
|
Bus();
|
||||||
uint8_t read(uint16_t address);
|
uint8_t read(uint16_t address);
|
||||||
void write(uint16_t address, uint8_t value);
|
void write(uint16_t address, uint8_t value);
|
||||||
void connect(Cartridge* cartridge);
|
void connect(std::shared_ptr<Cartridge> cartridge);
|
||||||
|
void connect(std::shared_ptr<Ppu> ppu);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<uint8_t[]> _ram;
|
std::unique_ptr<uint8_t[]> _ram;
|
||||||
Cartridge* _cartridge;
|
std::shared_ptr<Cartridge> _cartridge;
|
||||||
|
std::shared_ptr<Ppu> _ppu;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,7 +16,8 @@ namespace nes {
|
|||||||
|
|
||||||
void Nes::runRom(const fs::path &path, std::optional<uint16_t> address) {
|
void Nes::runRom(const fs::path &path, std::optional<uint16_t> address) {
|
||||||
_cartridge = std::make_shared<Cartridge>(path);
|
_cartridge = std::make_shared<Cartridge>(path);
|
||||||
_cpu->bus()->connect(_cartridge.get());
|
_cpu->bus()->connect(_cartridge);
|
||||||
|
_cpu->bus()->connect(_ppu);
|
||||||
reset(address);
|
reset(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
17
src/Ppu.cpp
17
src/Ppu.cpp
@ -7,11 +7,12 @@
|
|||||||
namespace nes {
|
namespace nes {
|
||||||
|
|
||||||
Ppu::Ppu(): _column{}, _scanline{} {
|
Ppu::Ppu(): _column{}, _scanline{} {
|
||||||
|
_frameBuffer = std::make_unique<Pixel[]>(SCREEN_WIDTH*SCREEN_HEIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ppu::tick() {
|
void Ppu::tick() {
|
||||||
|
|
||||||
|
setPixel(_scanline, _column, {});
|
||||||
|
|
||||||
_column++;
|
_column++;
|
||||||
if(_column >= 341) {
|
if(_column >= 341) {
|
||||||
@ -20,7 +21,21 @@ namespace nes {
|
|||||||
|
|
||||||
if(_scanline >= 261) {
|
if(_scanline >= 261) {
|
||||||
_scanline = -1;
|
_scanline = -1;
|
||||||
|
onNewFrame(_frameBuffer.get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Ppu::write(uint16_t address, uint8_t value) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Ppu::read(uint16_t address) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Ppu::setPixel(uint16_t row, uint16_t column, Pixel pixel) {
|
||||||
|
size_t index = row*SCREEN_WIDTH + column;
|
||||||
|
_frameBuffer[index] = pixel;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
20
src/Ppu.h
20
src/Ppu.h
@ -6,17 +6,37 @@
|
|||||||
#define NES_PPU_H
|
#define NES_PPU_H
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <memory>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
namespace nes {
|
namespace nes {
|
||||||
|
|
||||||
|
struct Pixel {
|
||||||
|
uint8_t R;
|
||||||
|
uint8_t G;
|
||||||
|
uint8_t B;
|
||||||
|
uint8_t A;
|
||||||
|
};
|
||||||
|
|
||||||
class Ppu {
|
class Ppu {
|
||||||
|
public:
|
||||||
|
static constexpr uint16_t SCREEN_WIDTH = 256;
|
||||||
|
static constexpr uint16_t SCREEN_HEIGHT = 240;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Ppu();
|
Ppu();
|
||||||
void tick();
|
void tick();
|
||||||
|
void write(uint16_t address, uint8_t value);
|
||||||
|
uint8_t read(uint16_t address);
|
||||||
|
void setPixel(uint16_t row, uint16_t column, Pixel pixel);
|
||||||
|
|
||||||
|
public:
|
||||||
|
std::function<void(const Pixel*)> onNewFrame;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int16_t _column;
|
int16_t _column;
|
||||||
int16_t _scanline;
|
int16_t _scanline;
|
||||||
|
std::unique_ptr<Pixel[]> _frameBuffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
9
src/Window.cpp
Normal file
9
src/Window.cpp
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
//
|
||||||
|
// Created by Мустафаев Селим Мустафаевич on 24.08.2023.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "Window.h"
|
||||||
|
|
||||||
|
namespace nes {
|
||||||
|
|
||||||
|
}
|
||||||
18
src/Window.h
Normal file
18
src/Window.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
//
|
||||||
|
// Created by Мустафаев Селим Мустафаевич on 24.08.2023.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef NES_WINDOW_H
|
||||||
|
#define NES_WINDOW_H
|
||||||
|
|
||||||
|
#include <SDL.h>
|
||||||
|
|
||||||
|
namespace nes {
|
||||||
|
|
||||||
|
class Window {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //NES_WINDOW_H
|
||||||
Loading…
Reference in New Issue
Block a user