From 9c45e9c7df1efa5bc1c8e73b6eaef4726b828d3e Mon Sep 17 00:00:00 2001 From: Hannes Schulze Date: Tue, 4 Feb 2025 19:59:47 +0100 Subject: add span type --- lib/nes/sys/cartridge.cc | 16 +++++++--------- lib/nes/sys/cartridge.hh | 18 +++++++----------- lib/nes/sys/mapper.cc | 18 +++++++++--------- lib/nes/sys/mapper.hh | 5 +++-- lib/nes/sys/nes.cc | 6 +++--- lib/nes/sys/nes.hh | 4 ++-- lib/nes/sys/ppu.cc | 2 +- lib/nes/sys/ppu.hh | 3 ++- 8 files changed, 34 insertions(+), 38 deletions(-) (limited to 'lib/nes/sys') diff --git a/lib/nes/sys/cartridge.cc b/lib/nes/sys/cartridge.cc index fcb7e84..14fff3d 100644 --- a/lib/nes/sys/cartridge.cc +++ b/lib/nes/sys/cartridge.cc @@ -5,15 +5,13 @@ namespace nes::sys { - cartridge::cartridge(u8 const* data, u32 const length) + cartridge::cartridge(span const data) { // See: https://www.nesdev.org/wiki/INES - auto const bytes = static_cast(data); - // Parse header. - if (length < header_length) { return; } - auto const h = header{ bytes }; + if (data.get_length() < header_length) { return; } + auto const h = header{ data.subspan(0) }; if (h.get_magic_0() != 'N' || h.get_magic_1() != 'E' || h.get_magic_2() != 'S' || h.get_magic_3() != 0x1A) { return; @@ -28,16 +26,16 @@ namespace nes::sys if (h.get_has_trainer()) { offset += 512; } prg_rom_size_ = h.get_prg_rom_banks() * prg_rom_bank_size; - if (length < offset + prg_rom_size_) { return; } + if (data.get_length() < offset + prg_rom_size_) { return; } if (prg_rom_size_ > max_prg_rom_size) { return; } - std::copy_n(&bytes[offset], prg_rom_size_, prg_rom_); + std::copy_n(&data[offset], prg_rom_size_, prg_rom_); offset += prg_rom_size_; chr_rom_size_ = h.get_chr_rom_banks() * chr_rom_bank_size; - if (length < offset + chr_rom_size_) { return; } + if (data.get_length() < offset + chr_rom_size_) { return; } if (chr_rom_size_ > max_chr_rom_size) { return; } - std::copy_n(&bytes[offset], chr_rom_size_, chr_rom_); + std::copy_n(&data[offset], chr_rom_size_, chr_rom_); ram_size_ = std::max(h.get_ram_banks(), 1u) * ram_bank_size; if (ram_size_ > max_ram_size) { return; } diff --git a/lib/nes/sys/cartridge.hh b/lib/nes/sys/cartridge.hh index 1934b8d..5383b56 100644 --- a/lib/nes/sys/cartridge.hh +++ b/lib/nes/sys/cartridge.hh @@ -3,8 +3,7 @@ #include "nes/sys/types/status.hh" #include "nes/sys/mapper.hh" #include "nes/common/types.hh" -#include -#include +#include "nes/common/span.hh" namespace nes::sys { @@ -42,7 +41,7 @@ namespace nes::sys u8 value[header_length]{}; - explicit header(u8 const* v) + explicit header(span const v) { for (auto i = u32{ 0 }; i < header_length; ++i) { @@ -64,16 +63,13 @@ namespace nes::sys public: static constexpr auto max_file_size = u32{ max_prg_rom_size + max_chr_rom_size + 512 + header_length }; - explicit cartridge(u8 const* data, u32 length); + explicit cartridge(span); auto get_status() const -> status { return status_; } - auto get_prg_rom() const -> u8 const* { return prg_rom_; } - auto get_prg_rom_length() const -> u32 { return prg_rom_size_; } - auto get_chr_rom() const -> u8 const* { return chr_rom_; } - auto get_chr_rom_length() const -> u32 { return chr_rom_size_; } - auto get_ram() const -> u8 const* { return ram_; } - auto get_ram_mut() -> u8* { return ram_; } - auto get_ram_length() const -> u32 { return ram_size_; } + auto get_prg_rom() const -> span { return span{ prg_rom_, prg_rom_size_ }; } + auto get_chr_rom() const -> span { return span{ chr_rom_, chr_rom_size_ }; } + auto get_ram() const -> span { return span{ ram_, ram_size_ }; } + auto ref_ram() -> span { return span{ ram_, ram_size_ }; } auto get_mapper() const -> mapper& { return *mapper_; } auto get_name_table_arrangement() const -> name_table_arrangement { return name_table_arrangement_; } }; diff --git a/lib/nes/sys/mapper.cc b/lib/nes/sys/mapper.cc index d779076..e5c9a2b 100644 --- a/lib/nes/sys/mapper.cc +++ b/lib/nes/sys/mapper.cc @@ -16,17 +16,17 @@ namespace nes::sys auto validate(cartridge& cartridge) -> status override { - if (cartridge.get_ram_length() != 0x2000) + if (cartridge.get_ram().get_length() != 0x2000) { return status::error_invalid_ines_data; } - if (cartridge.get_prg_rom_length() != 0x4000 && cartridge.get_prg_rom_length() != 0x8000) + if (cartridge.get_prg_rom().get_length() != 0x4000 && cartridge.get_prg_rom().get_length() != 0x8000) { return status::error_invalid_ines_data; } - if (cartridge.get_chr_rom_length() != 0x2000) + if (cartridge.get_chr_rom().get_length() != 0x2000) { return status::error_invalid_ines_data; } @@ -52,7 +52,7 @@ namespace nes::sys if (addr <= address{ 0xFFFF }) { auto rel = addr.get_absolute() - 0xC000u; - if (cartridge.get_prg_rom_length() > 0x4000) { rel += 0x4000; } + if (cartridge.get_prg_rom().get_length() > 0x4000) { rel += 0x4000; } return cartridge.get_prg_rom()[rel]; } @@ -67,12 +67,12 @@ namespace nes::sys } if (addr <= address{ 0x7FFF }) { - cartridge.get_ram_mut()[addr.get_absolute() - 0x6000] = value; + cartridge.ref_ram()[addr.get_absolute() - 0x6000] = value; return; } } - auto read_ppu(address const addr, cartridge& cartridge, u8 const* vram) -> u8 override + auto read_ppu(address const addr, cartridge& cartridge, span const vram) -> u8 override { if (addr <= address{ 0x1FFF }) { @@ -87,7 +87,7 @@ namespace nes::sys return 0x0; } - auto write_ppu(address const addr, u8 const value, cartridge& cartridge, u8* vram) -> void override + auto write_ppu(address const addr, u8 const value, cartridge& cartridge, span const vram) -> void override { if (addr <= address{ 0x1FFF }) { @@ -139,8 +139,8 @@ namespace nes::sys auto validate(cartridge&) -> status override { return status::error_unsupported_mapper; } auto read_cpu(address, cartridge&) -> u8 override { return 0; } auto write_cpu(address, u8, cartridge&) -> void override {} - auto read_ppu(address, cartridge&, u8 const*) -> u8 override { return 0; } - auto write_ppu(address, u8, cartridge&, u8*) -> void override {} + auto read_ppu(address, cartridge&, span) -> u8 override { return 0; } + auto write_ppu(address, u8, cartridge&, span) -> void override {} }; } // namespace diff --git a/lib/nes/sys/mapper.hh b/lib/nes/sys/mapper.hh index c7c9784..9ad88e2 100644 --- a/lib/nes/sys/mapper.hh +++ b/lib/nes/sys/mapper.hh @@ -3,6 +3,7 @@ #include "nes/sys/types/address.hh" #include "nes/sys/types/status.hh" #include "nes/common/types.hh" +#include "nes/common/span.hh" namespace nes::sys { @@ -24,8 +25,8 @@ namespace nes::sys virtual auto validate(cartridge&) -> status = 0; virtual auto read_cpu(address, cartridge&) -> u8 = 0; virtual auto write_cpu(address, u8, cartridge&) -> void = 0; - virtual auto read_ppu(address, cartridge&, u8 const* vram) -> u8 = 0; - virtual auto write_ppu(address, u8, cartridge&, u8* vram) -> void = 0; + virtual auto read_ppu(address, cartridge&, span vram) -> u8 = 0; + virtual auto write_ppu(address, u8, cartridge&, span vram) -> void = 0; protected: explicit mapper() = default; diff --git a/lib/nes/sys/nes.cc b/lib/nes/sys/nes.cc index 3195826..5cc47a4 100644 --- a/lib/nes/sys/nes.cc +++ b/lib/nes/sys/nes.cc @@ -3,8 +3,8 @@ namespace nes::sys { - nes::nes(display& display, u8 const* rom_data, u32 rom_length) - : cartridge_{ rom_data, rom_length } + nes::nes(display& display, span const rom_data) + : cartridge_{ rom_data } , display_{ display } , ppu_{ cpu_, cartridge_, display_ } , cpu_{ ppu_, cartridge_, controller_1_, controller_2_ } @@ -52,7 +52,7 @@ namespace nes::sys auto nes::get_snapshot() -> snapshot { auto res = snapshot{}; - res.sram = std::vector(cartridge_.get_ram(), cartridge_.get_ram() + cartridge_.get_ram_length()); + res.sram = std::vector(cartridge_.get_ram().begin(), cartridge_.get_ram().end()); cpu_.build_snapshot(res); ppu_.build_snapshot(res); return res; diff --git a/lib/nes/sys/nes.hh b/lib/nes/sys/nes.hh index 5e081b3..187b940 100644 --- a/lib/nes/sys/nes.hh +++ b/lib/nes/sys/nes.hh @@ -5,7 +5,7 @@ #include "nes/sys/controller.hh" #include "nes/sys/cpu.hh" #include "nes/sys/ppu.hh" -#include +#include "nes/common/span.hh" namespace nes { @@ -29,7 +29,7 @@ namespace nes::sys status status_{ status::error_invalid_ines_data }; public: - explicit nes(display&, u8 const* rom_data, u32 rom_length); + explicit nes(display&, span rom_data); nes(nes const&) = delete; nes(nes&&) = delete; diff --git a/lib/nes/sys/ppu.cc b/lib/nes/sys/ppu.cc index c9e6f11..2419030 100644 --- a/lib/nes/sys/ppu.cc +++ b/lib/nes/sys/ppu.cc @@ -239,7 +239,7 @@ namespace nes::sys sprite_count_ = 0; for (auto i = u32{ 0 }; i < sprite_max_count; ++i) { - auto const s = sprite{ &oam_[i * 4] }; + auto const s = sprite{ span{ oam_ }.subspan<4>(i * 4) }; auto const row = static_cast(scanline_) - static_cast(s.get_y()); if (row < 0 || static_cast(row) >= height) { continue; } diff --git a/lib/nes/sys/ppu.hh b/lib/nes/sys/ppu.hh index 013546a..7774669 100644 --- a/lib/nes/sys/ppu.hh +++ b/lib/nes/sys/ppu.hh @@ -2,6 +2,7 @@ #include "nes/sys/types/cycle-count.hh" #include "nes/common/types.hh" +#include "nes/common/span.hh" namespace nes { @@ -96,7 +97,7 @@ namespace nes::sys explicit sprite() = default; - explicit sprite(u8 const* data) + explicit sprite(span const data) : value{ data[0], data[1], data[2], data[3] } { } -- cgit v1.3.1