Compare commits

...

10 Commits

Author SHA1 Message Date
44ea8baea8 Fixed memory leak in spm package 2023-09-30 17:56:48 +03:00
2e7b1bb480 Fixed bug in OAM 2023-09-30 15:31:30 +03:00
e2c33d36f7 Some fixes for iOS 2023-09-28 22:40:54 +03:00
Selim Mustafaev
be7d432f6d Adding some logic to Objective-C++ wrapper 2023-09-28 18:26:39 +03:00
83bb16b28b Fixing SPM targets. Adding example project 2023-09-27 23:38:52 +03:00
cb12225d5e Adding SPM package 2023-09-25 23:03:06 +03:00
Selim Mustafaev
d3eaacbe8a Changing palette container 2023-09-25 17:16:25 +03:00
352c1588ae Finised sprite rendering 2023-09-24 23:14:34 +03:00
c1bd4c863f Adding DMA for OAM 2023-09-23 14:26:27 +03:00
cf0a1a5cfe Refactoring 2023-09-23 11:55:35 +03:00
52 changed files with 1794 additions and 517 deletions

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:">
</FileRef>
</Workspace>

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>NesKit.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
<key>nes-Package.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>NesKit</key>
<dict>
<key>primary</key>
<true/>
</dict>
<key>nes</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>

View File

@ -6,29 +6,29 @@ set(CMAKE_CXX_STANDARD 23)
add_compile_definitions(SDL_MAIN_HANDLED) add_compile_definitions(SDL_MAIN_HANDLED)
add_executable(nes add_executable(nes
main.cpp examples/sdl/main.cpp
src/Cartridge.cpp src/Cartridge.cpp
src/Cartridge.h src/Cartridge.h
src/Nes.cpp src/System.cpp
src/Nes.h src/System.h
src/Cpu.cpp src/Cpu.cpp
src/Cpu.h src/Cpu.h
src/Bus.cpp
src/Bus.h
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 examples/sdl/Window.cpp
src/Window.h examples/sdl/Window.h
src/Shifter.cpp src/Shifter.cpp
src/Shifter.h src/Shifter.h
src/Logger.cpp src/Logger.cpp
src/Logger.h src/Logger.h
src/Controller.cpp src/Controller.cpp
src/Controller.h src/Controller.h
src/SdlKeyboardController.cpp examples/sdl/SdlKeyboardController.cpp
src/SdlKeyboardController.h) examples/sdl/SdlKeyboardController.h src/Dma.cpp src/Dma.h
src/Oam.cpp
src/Oam.h)
find_package(SDL2 CONFIG REQUIRED) find_package(SDL2 CONFIG REQUIRED)
find_package(fmt REQUIRED) find_package(fmt REQUIRED)

24
NesKit/NesLayer.h Normal file
View File

@ -0,0 +1,24 @@
//
// NesLayer.h
//
//
// Created by Мустафаев Селим Мустафаевич on 28.09.2023.
//
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import "NesSystem.h"
NS_ASSUME_NONNULL_BEGIN
@interface NesLayer : CALayer
@property NesSystem* system;
- (instancetype)initWithNesSystem:(NesSystem*)system;
@end
NS_ASSUME_NONNULL_END

31
NesKit/NesLayer.m Normal file
View File

@ -0,0 +1,31 @@
//
// NesLayer.m
//
//
// Created by Мустафаев Селим Мустафаевич on 28.09.2023.
//
#import "NesLayer.h"
@implementation NesLayer {
NSTimer* _timer;
}
- (instancetype)initWithNesSystem:(NesSystem*)system {
if(self = [super init]) {
self.system = system;
self.contentsGravity = kCAGravityResizeAspect;
_timer = [NSTimer timerWithTimeInterval:1.0/60.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
if(self.system) {
self.contents = (__bridge id _Nullable)(self.system.frame);
[self.system stepToNextFrame];
}
}];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
}
return self;
}
@end

23
NesKit/NesSystem.h Normal file
View File

@ -0,0 +1,23 @@
//
// NesSystem.h
//
//
// Created by Selim Mustafaev on 27.09.2023.
//
#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
NS_ASSUME_NONNULL_BEGIN
@interface NesSystem : NSObject
@property CGImageRef frame;
- (instancetype)init;
- (void)runRom:(NSString*)path;
- (void)stepToNextFrame;
@end
NS_ASSUME_NONNULL_END

85
NesKit/NesSystem.mm Normal file
View File

@ -0,0 +1,85 @@
//
// NesSystem.mm
//
//
// Created by Selim Mustafaev on 27.09.2023.
//
#import "NesSystem.h"
#import <NesKitCpp.h>
#import <memory>
@interface NesSystem()
@end
@implementation NesSystem {
std::unique_ptr<nes::System> _system;
NSCondition* _condition;
BOOL _runEmulation;
}
- (instancetype)init {
if(self = [super init]) {
_system = std::make_unique<nes::System>();
_condition = [NSCondition new];
_runEmulation = YES;
size_t frameBufferSize = nes::Ppu::SCREEN_WIDTH * nes::Ppu::SCREEN_HEIGHT * sizeof(nes::Pixel);
_system->setNewFrameCallback([frameBufferSize, self](auto frameBuffer) {
_runEmulation = NO;
CGImageRelease(_frame);
CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, (void*)frameBuffer, frameBufferSize, NULL);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGImageRef image = CGImageCreate(nes::Ppu::SCREEN_WIDTH,
nes::Ppu::SCREEN_HEIGHT,
8,
sizeof(nes::Pixel)*8,
nes::Ppu::SCREEN_WIDTH * sizeof(nes::Pixel),
colorspace,
kCGImageAlphaPremultipliedLast,
dataProvider,
NULL,
false,
kCGRenderingIntentDefault);
CGDataProviderRelease(dataProvider);
CGColorSpaceRelease(colorspace);
dispatch_async(dispatch_get_main_queue(), ^{
_frame = image;
});
});
}
return self;
}
- (void)runRom:(NSURL*)url {
_system->insertCartridge([url fileSystemRepresentation]);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while(TRUE) {
[_condition lock];
while (!_runEmulation) {
[_condition wait];
}
while(_runEmulation) {
_system->tick();
}
[_condition unlock];
}
});
}
- (void)stepToNextFrame {
[_condition lock];
_runEmulation = YES;
[_condition signal];
[_condition unlock];
}
@end

14
NesKit/include/NesKit.h Normal file
View File

@ -0,0 +1,14 @@
//
// Header.h
//
//
// Created by Selim Mustafaev on 27.09.2023.
//
#ifndef NesKit_h
#define NesKit_h
#import "../NesSystem.h"
#import "../NesLayer.h"
#endif /* Header_h */

26
Package.swift Normal file
View File

@ -0,0 +1,26 @@
// swift-tools-version: 5.8
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "NesKit",
platforms: [
.macOS(.v13),
.iOS(.v16)
],
products: [
.library(name: "NesKit", targets: ["NesKit"]),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(name: "NesKitCpp",
path: "src",
exclude: ["Logger.cpp", "Logger.h"]),
.target(name: "NesKit",
dependencies: [.target(name: "NesKitCpp")],
path: "NesKit")
],
cxxLanguageStandard: .cxx20
)

View File

@ -0,0 +1,390 @@
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 60;
objects = {
/* Begin PBXBuildFile section */
7AF4D40F2AC4A97B00717C81 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AF4D40E2AC4A97B00717C81 /* AppDelegate.swift */; };
7AF4D4112AC4A97B00717C81 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AF4D4102AC4A97B00717C81 /* SceneDelegate.swift */; };
7AF4D4132AC4A97B00717C81 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AF4D4122AC4A97B00717C81 /* ViewController.swift */; };
7AF4D4162AC4A97B00717C81 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 7AF4D4142AC4A97B00717C81 /* Main.storyboard */; };
7AF4D4182AC4A97D00717C81 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7AF4D4172AC4A97D00717C81 /* Assets.xcassets */; };
7AF4D41B2AC4A97D00717C81 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 7AF4D4192AC4A97D00717C81 /* LaunchScreen.storyboard */; };
7AF4D4242AC4A9D300717C81 /* NesKit in Frameworks */ = {isa = PBXBuildFile; productRef = 7AF4D4232AC4A9D300717C81 /* NesKit */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
7AF4D40B2AC4A97B00717C81 /* NesApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = NesApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
7AF4D40E2AC4A97B00717C81 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
7AF4D4102AC4A97B00717C81 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
7AF4D4122AC4A97B00717C81 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
7AF4D4152AC4A97B00717C81 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
7AF4D4172AC4A97D00717C81 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
7AF4D41A2AC4A97D00717C81 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
7AF4D41C2AC4A97D00717C81 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
7AF4D4082AC4A97B00717C81 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
7AF4D4242AC4A9D300717C81 /* NesKit in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
7AF4D4022AC4A97B00717C81 = {
isa = PBXGroup;
children = (
7AF4D40D2AC4A97B00717C81 /* NesApp */,
7AF4D40C2AC4A97B00717C81 /* Products */,
);
sourceTree = "<group>";
};
7AF4D40C2AC4A97B00717C81 /* Products */ = {
isa = PBXGroup;
children = (
7AF4D40B2AC4A97B00717C81 /* NesApp.app */,
);
name = Products;
sourceTree = "<group>";
};
7AF4D40D2AC4A97B00717C81 /* NesApp */ = {
isa = PBXGroup;
children = (
7AF4D40E2AC4A97B00717C81 /* AppDelegate.swift */,
7AF4D4102AC4A97B00717C81 /* SceneDelegate.swift */,
7AF4D4122AC4A97B00717C81 /* ViewController.swift */,
7AF4D4142AC4A97B00717C81 /* Main.storyboard */,
7AF4D4172AC4A97D00717C81 /* Assets.xcassets */,
7AF4D4192AC4A97D00717C81 /* LaunchScreen.storyboard */,
7AF4D41C2AC4A97D00717C81 /* Info.plist */,
);
path = NesApp;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
7AF4D40A2AC4A97B00717C81 /* NesApp */ = {
isa = PBXNativeTarget;
buildConfigurationList = 7AF4D41F2AC4A97D00717C81 /* Build configuration list for PBXNativeTarget "NesApp" */;
buildPhases = (
7AF4D4072AC4A97B00717C81 /* Sources */,
7AF4D4082AC4A97B00717C81 /* Frameworks */,
7AF4D4092AC4A97B00717C81 /* Resources */,
);
buildRules = (
);
dependencies = (
);
name = NesApp;
packageProductDependencies = (
7AF4D4232AC4A9D300717C81 /* NesKit */,
);
productName = NesApp;
productReference = 7AF4D40B2AC4A97B00717C81 /* NesApp.app */;
productType = "com.apple.product-type.application";
};
/* End PBXNativeTarget section */
/* Begin PBXProject section */
7AF4D4032AC4A97B00717C81 /* Project object */ = {
isa = PBXProject;
attributes = {
BuildIndependentTargetsInParallel = 1;
LastSwiftUpdateCheck = 1500;
LastUpgradeCheck = 1500;
TargetAttributes = {
7AF4D40A2AC4A97B00717C81 = {
CreatedOnToolsVersion = 15.0;
};
};
};
buildConfigurationList = 7AF4D4062AC4A97B00717C81 /* Build configuration list for PBXProject "NesApp" */;
compatibilityVersion = "Xcode 14.0";
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = 7AF4D4022AC4A97B00717C81;
packageReferences = (
7AF4D4222AC4A9D300717C81 /* XCLocalSwiftPackageReference "../.." */,
);
productRefGroup = 7AF4D40C2AC4A97B00717C81 /* Products */;
projectDirPath = "";
projectRoot = "";
targets = (
7AF4D40A2AC4A97B00717C81 /* NesApp */,
);
};
/* End PBXProject section */
/* Begin PBXResourcesBuildPhase section */
7AF4D4092AC4A97B00717C81 /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
7AF4D41B2AC4A97D00717C81 /* LaunchScreen.storyboard in Resources */,
7AF4D4182AC4A97D00717C81 /* Assets.xcassets in Resources */,
7AF4D4162AC4A97B00717C81 /* Main.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXResourcesBuildPhase section */
/* Begin PBXSourcesBuildPhase section */
7AF4D4072AC4A97B00717C81 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
7AF4D4132AC4A97B00717C81 /* ViewController.swift in Sources */,
7AF4D40F2AC4A97B00717C81 /* AppDelegate.swift in Sources */,
7AF4D4112AC4A97B00717C81 /* SceneDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */
/* Begin PBXVariantGroup section */
7AF4D4142AC4A97B00717C81 /* Main.storyboard */ = {
isa = PBXVariantGroup;
children = (
7AF4D4152AC4A97B00717C81 /* Base */,
);
name = Main.storyboard;
sourceTree = "<group>";
};
7AF4D4192AC4A97D00717C81 /* LaunchScreen.storyboard */ = {
isa = PBXVariantGroup;
children = (
7AF4D41A2AC4A97D00717C81 /* Base */,
);
name = LaunchScreen.storyboard;
sourceTree = "<group>";
};
/* End PBXVariantGroup section */
/* Begin XCBuildConfiguration section */
7AF4D41D2AC4A97D00717C81 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu17;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 17.0;
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
};
name = Debug;
};
7AF4D41E2AC4A97D00717C81 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu17;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 17.0;
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = iphoneos;
SWIFT_COMPILATION_MODE = wholemodule;
VALIDATE_PRODUCT = YES;
};
name = Release;
};
7AF4D4202AC4A97D00717C81 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 46DTTB8X4S;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = NesApp/Info.plist;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen;
INFOPLIST_KEY_UIMainStoryboardFile = Main;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = pro.aliencat.NesApp;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
};
7AF4D4212AC4A97D00717C81 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 46DTTB8X4S;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = NesApp/Info.plist;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen;
INFOPLIST_KEY_UIMainStoryboardFile = Main;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = pro.aliencat.NesApp;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
};
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
7AF4D4062AC4A97B00717C81 /* Build configuration list for PBXProject "NesApp" */ = {
isa = XCConfigurationList;
buildConfigurations = (
7AF4D41D2AC4A97D00717C81 /* Debug */,
7AF4D41E2AC4A97D00717C81 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
7AF4D41F2AC4A97D00717C81 /* Build configuration list for PBXNativeTarget "NesApp" */ = {
isa = XCConfigurationList;
buildConfigurations = (
7AF4D4202AC4A97D00717C81 /* Debug */,
7AF4D4212AC4A97D00717C81 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
/* Begin XCLocalSwiftPackageReference section */
7AF4D4222AC4A9D300717C81 /* XCLocalSwiftPackageReference "../.." */ = {
isa = XCLocalSwiftPackageReference;
relativePath = ../..;
};
/* End XCLocalSwiftPackageReference section */
/* Begin XCSwiftPackageProductDependency section */
7AF4D4232AC4A9D300717C81 /* NesKit */ = {
isa = XCSwiftPackageProductDependency;
productName = NesKit;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = 7AF4D4032AC4A97B00717C81 /* Project object */;
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:">
</FileRef>
</Workspace>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>NesApp.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>

View File

@ -0,0 +1,36 @@
//
// AppDelegate.swift
// NesApp
//
// Created by Selim Mustafaev on 27.09.2023.
//
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
}

View File

@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@ -0,0 +1,13 @@
{
"images" : [
{
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="EHf-IW-A2E">
<objects>
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" xcode11CocoaTouchSystemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53" y="375"/>
</scene>
</scenes>
</document>

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" xcode11CocoaTouchSystemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
</scene>
</scenes>
</document>

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<false/>
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UISceneConfigurationName</key>
<string>Default Configuration</string>
<key>UISceneDelegateClassName</key>
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
<key>UISceneStoryboardFile</key>
<string>Main</string>
</dict>
</array>
</dict>
</dict>
</dict>
</plist>

View File

@ -0,0 +1,52 @@
//
// SceneDelegate.swift
// NesApp
//
// Created by Selim Mustafaev on 27.09.2023.
//
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else { return }
}
func sceneDidDisconnect(_ scene: UIScene) {
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
}
func sceneDidBecomeActive(_ scene: UIScene) {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}
func sceneWillResignActive(_ scene: UIScene) {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
}
func sceneWillEnterForeground(_ scene: UIScene) {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}
func sceneDidEnterBackground(_ scene: UIScene) {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
}
}

View File

@ -0,0 +1,72 @@
//
// ViewController.swift
// NesApp
//
// Created by Selim Mustafaev on 27.09.2023.
//
import UIKit
import NesKit
class NesView: UIView {
let nesLayer: NesLayer
init(system: NesSystem) {
self.nesLayer = NesLayer(nesSystem: system)
super.init(frame: .zero)
self.layer.addSublayer(self.nesLayer)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
self.nesLayer.frame = self.layer.frame
}
}
class ViewController: UIViewController {
let system = NesSystem()
lazy var nesView = NesView(system: system)
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(nesView)
nesView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
nesView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
nesView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
nesView.topAnchor.constraint(equalTo: view.topAnchor),
nesView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.openFilePicker()
}
}
func openFilePicker() {
let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [.data])
documentPicker.delegate = self
documentPicker.modalPresentationStyle = .overFullScreen
documentPicker.allowsMultipleSelection = false
present(documentPicker, animated: true)
}
}
extension ViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
print("Did pick document at: ", url)
system.runRom(url.path(percentEncoded: false))
}
}

View File

@ -5,7 +5,7 @@
#ifndef NES_SDLKEYBOARDCONTROLLER_H #ifndef NES_SDLKEYBOARDCONTROLLER_H
#define NES_SDLKEYBOARDCONTROLLER_H #define NES_SDLKEYBOARDCONTROLLER_H
#include "Controller.h" #include "../../src/Controller.h"
class SdlKeyboardController: public nes::Controller { class SdlKeyboardController: public nes::Controller {
public: public:

View File

@ -5,7 +5,7 @@
#ifndef NES_WINDOW_H #ifndef NES_WINDOW_H
#define NES_WINDOW_H #define NES_WINDOW_H
#include "Ppu.h" #include "../../src/Ppu.h"
#include <SDL.h> #include <SDL.h>
#include <memory> #include <memory>

45
examples/sdl/main.cpp Normal file
View File

@ -0,0 +1,45 @@
#include "../../src/System.h"
#include "Window.h"
#include "SdlKeyboardController.h"
#include <functional>
#include <thread>
int main() {
using namespace std::placeholders;
nes::System device;
nes::SdlWindow window(nes::Ppu::SCREEN_WIDTH, nes::Ppu::SCREEN_HEIGHT);
window.setSize(nes::Ppu::SCREEN_WIDTH * 4, nes::Ppu::SCREEN_HEIGHT * 4);
SDL_Event e;
bool frameRendered = false;
device.setNewFrameCallback([&window, &e, &frameRendered](auto buffer){
window.drawFrame(buffer);
while(SDL_PollEvent(&e));
frameRendered = true;
});
device.connect(std::make_shared<SdlKeyboardController>());
//device.insertCartridge("/home/selim/Downloads/dk.nes");
device.insertCartridge("/Users/selim/Documents/smb.nes");
//device.insertCartridge("C:\\Users\\selim\\Documents\\nestest.nes");
auto frameStart = std::chrono::steady_clock::now();
while (true) {
device.tick();
if(frameRendered) {
auto renderingTime = std::chrono::steady_clock::now();
auto elapsedTime = std::chrono::duration_cast<std::chrono::nanoseconds>(renderingTime - frameStart).count();
int64_t waitTime = 1000000000/60 - elapsedTime;
if(waitTime > 0) {
std::this_thread::sleep_for(std::chrono::nanoseconds(waitTime));
}
frameStart = std::chrono::steady_clock::now();
frameRendered = false;
}
}
return 0;
}

View File

@ -1,37 +0,0 @@
#include "src/Nes.h"
#include "src/Window.h"
#include "src/SdlKeyboardController.h"
#include <functional>
#include <chrono>
#include <thread>
int main() {
using namespace std::placeholders;
nes::Nes device;
nes::SdlWindow window(nes::Ppu::SCREEN_WIDTH, nes::Ppu::SCREEN_HEIGHT);
window.setSize(nes::Ppu::SCREEN_WIDTH * 4, nes::Ppu::SCREEN_HEIGHT * 4);
SDL_Event e;
device.setNewFrameCallback([&window, &e](auto buffer){
window.drawFrame(buffer);
while(SDL_PollEvent(&e));
});
device.connect(std::make_shared<SdlKeyboardController>());
device.insertCartridge("/home/selim/Downloads/smb.nes");
//device.insertCartridge("/Users/selim/Documents/nestest.nes");
//device.insertCartridge("C:\\Users\\selim\\Documents\\nestest.nes");
uint64_t cycles = 0;
while (cycles < 1000000000) {
device.tick();
cycles++;
//int64_t us = static_cast<int64_t>(1000000000.0/(60*nes::Ppu::SCREEN_WIDTH*nes::Ppu::SCREEN_HEIGHT));
//std::this_thread::sleep_for(std::chrono::nanoseconds(1));
}
return 0;
}

View File

@ -1,79 +0,0 @@
//
// Created by Selim Mustafaev on 12.08.2023.
//
#include "Bus.h"
#include <iostream>
#include <utility>
namespace nes {
Bus::Bus(): _cartridge{nullptr} {
_ram = std::make_unique<uint8_t[]>(2*1024);
}
uint8_t Bus::read(uint16_t address) {
if(address < 0x2000) {
return _ram[address & 0x07FF];
}
else if(address >= 0x2000 && address < 0x4000) {
return _ppu->read(address & 0x0007);
}
else if(address >= 0x8000) {
return _cartridge->readPrg(address);
}
else if(address == 0x4016) {
return _controller1->read();
}
else if(address == 0x4017) {
// Controller 2
return 0;
}
return 0;
}
void Bus::write(uint16_t address, uint8_t value) {
if(address < 0x2000) {
_ram[address & 0x07FF] = value;
}
else if(address >= 0x2000 && address < 0x4000) {
_ppu->write(address & 0x0007, value);
}
else if(address >= 0x8000) {
std::cout << "Cartridge write at address: " << address << std::endl;
}
else if(address == 0x4014) {
// DMA
}
else if(address == 0x4016) {
_controller1->poll();
}
else if(address == 0x4017) {
// Controller 2
}
}
void Bus::connect(std::shared_ptr<Cartridge> cartridge) {
_cartridge = std::move(cartridge);
}
void Bus::connect(std::shared_ptr<Ppu> ppu) {
_ppu = std::move(ppu);
}
void Bus::connect(std::shared_ptr<Controller> controller) {
_controller1 = std::move(controller);
}
// For debug
// Calc "hash" - just sum of the bytes of first page
// Can be useful for detecting change in ram
uint32_t Bus::zpHash() const {
uint32_t sum = 0;
for(size_t i = 0; i < 2048; ++i) {
sum += _ram[i];
}
return sum;
}
}

View File

@ -1,36 +0,0 @@
//
// Created by Selim Mustafaev on 12.08.2023.
//
#ifndef NES_BUS_H
#define NES_BUS_H
#include "Cartridge.h"
#include "Ppu.h"
#include "Controller.h"
#include <cstdint>
#include <memory>
namespace nes {
class Bus {
public:
Bus();
uint8_t read(uint16_t address);
void write(uint16_t address, uint8_t value);
void connect(std::shared_ptr<Cartridge> cartridge);
void connect(std::shared_ptr<Ppu> ppu);
void connect(std::shared_ptr<Controller> controller);
[[nodiscard]] uint32_t zpHash() const;
private:
std::unique_ptr<uint8_t[]> _ram;
std::shared_ptr<Cartridge> _cartridge;
std::shared_ptr<Ppu> _ppu;
std::shared_ptr<Controller> _controller1;
};
}
#endif //NES_BUS_H

View File

@ -28,6 +28,7 @@ namespace nes {
_chrRom = std::span<uint8_t>(_romData.get() + sizeof(RomHeader) + prgSize, chrSize); _chrRom = std::span<uint8_t>(_romData.get() + sizeof(RomHeader) + prgSize, chrSize);
_mapper = std::make_unique<Mapper0>(_header->prgChunks, _header->chrChunks); _mapper = std::make_unique<Mapper0>(_header->prgChunks, _header->chrChunks);
_mirroring = _header->flags.mirroring == 0 ? Mirroring::Horizontal : Mirroring::Vertical;
} }
uint8_t Cartridge::readPrg(uint16_t address) { uint8_t Cartridge::readPrg(uint16_t address) {
@ -40,4 +41,8 @@ namespace nes {
return _chrRom[mappedAddress]; return _chrRom[mappedAddress];
} }
Cartridge::Mirroring Cartridge::mirroring() const {
return _mirroring;
}
} }

View File

@ -19,20 +19,33 @@ namespace nes {
constexpr size_t PRG_CHUNK_SIZE = 16*1024; constexpr size_t PRG_CHUNK_SIZE = 16*1024;
constexpr size_t CHR_CHUNK_SIZE = 8*1024; constexpr size_t CHR_CHUNK_SIZE = 8*1024;
struct RomHeader {
uint32_t magic;
uint8_t prgChunks;
uint8_t chrChunks;
uint8_t reserved[10];
};
class Cartridge { class Cartridge {
public:
enum Mirroring {
Horizontal,
Vertical
};
struct Flags {
uint8_t mirroring: 1;
uint8_t reserved: 7;
};
struct RomHeader {
uint32_t magic;
uint8_t prgChunks;
uint8_t chrChunks;
Flags flags;
uint8_t reserved[9];
};
public: public:
explicit Cartridge(const fs::path& path); explicit Cartridge(const fs::path& path);
public: public:
uint8_t readPrg(uint16_t address); uint8_t readPrg(uint16_t address);
uint8_t readChr(uint16_t address); uint8_t readChr(uint16_t address);
Mirroring mirroring() const;
private: private:
std::unique_ptr<uint8_t[]> _romData; std::unique_ptr<uint8_t[]> _romData;
@ -40,6 +53,9 @@ namespace nes {
RomHeader* _header; RomHeader* _header;
std::span<uint8_t> _prgRom; std::span<uint8_t> _prgRom;
std::span<uint8_t> _chrRom; std::span<uint8_t> _chrRom;
private:
Mirroring _mirroring;
}; };
} }

View File

@ -15,4 +15,8 @@ namespace nes {
_data <<= 1; _data <<= 1;
return result; return result;
} }
void Controller::poll() {
}
} }

View File

@ -25,7 +25,7 @@ namespace nes {
public: public:
Controller(); Controller();
uint8_t read(); uint8_t read();
virtual void poll() = 0; virtual void poll();
protected: protected:
uint8_t _data; uint8_t _data;

View File

@ -3,32 +3,36 @@
// //
#include "Cpu.h" #include "Cpu.h"
#include "System.h"
#include <iostream> #include <iostream>
#include <format>
#ifdef NES_LOGGING
#include <fmt/format.h>
#endif
namespace nes { namespace nes {
Cpu::Cpu(): _ticks{}, A{}, X{}, Y{}, PC{}, SP{}, flags{} { Cpu::Cpu(System* system): _ticks{}, A{}, X{}, Y{}, PC{}, SP{}, flags{} {
_bus = std::make_unique<Bus>(); _system = system;
_instructions = std::vector<Instruction>(256); _instructions = std::vector<Instruction>(256);
_instructions[0x00] = {"BRK", &Cpu::BRK, &Cpu::IMP, 7, false}; _instructions[0x00] = {"BRK", &Cpu::BRK, &Cpu::IMPL, 7, false};
_instructions[0x01] = {"ORA", &Cpu::ORA, &Cpu::IZX, 6, false}; _instructions[0x01] = {"ORA", &Cpu::ORA, &Cpu::IZX, 6, false};
_instructions[0x05] = {"ORA", &Cpu::ORA, &Cpu::ZP0, 3, false}; _instructions[0x05] = {"ORA", &Cpu::ORA, &Cpu::ZP0, 3, false};
_instructions[0x06] = {"ASL", &Cpu::ASL, &Cpu::ZP0, 5, false}; _instructions[0x06] = {"ASL", &Cpu::ASL, &Cpu::ZP0, 5, false};
_instructions[0x09] = {"ORA", &Cpu::ORA, &Cpu::IMM, 2, false}; _instructions[0x09] = {"ORA", &Cpu::ORA, &Cpu::IMM, 2, false};
_instructions[0x0A] = {"ASL", &Cpu::ASL_ACC, &Cpu::IMP, 2, false}; _instructions[0x0A] = {"ASL", &Cpu::ASL_ACC, &Cpu::IMPL, 2, false};
_instructions[0x0D] = {"ORA", &Cpu::ORA, &Cpu::ABS, 4, false}; _instructions[0x0D] = {"ORA", &Cpu::ORA, &Cpu::ABSL, 4, false};
_instructions[0x11] = {"ORA", &Cpu::ORA, &Cpu::IZY, 5, true}; _instructions[0x11] = {"ORA", &Cpu::ORA, &Cpu::IZY, 5, true};
_instructions[0x19] = {"ORA", &Cpu::ORA, &Cpu::ABY, 4, true}; _instructions[0x19] = {"ORA", &Cpu::ORA, &Cpu::ABY, 4, true};
_instructions[0x15] = {"ORA", &Cpu::ORA, &Cpu::ZPX, 4, false}; _instructions[0x15] = {"ORA", &Cpu::ORA, &Cpu::ZPX, 4, false};
_instructions[0x1D] = {"ORA", &Cpu::ORA, &Cpu::ABX, 4, true}; _instructions[0x1D] = {"ORA", &Cpu::ORA, &Cpu::ABX, 4, true};
_instructions[0x0E] = {"ASL", &Cpu::ASL, &Cpu::ABS, 6, false}; _instructions[0x0E] = {"ASL", &Cpu::ASL, &Cpu::ABSL, 6, false};
_instructions[0x16] = {"ASL", &Cpu::ASL, &Cpu::ZPX, 6, false}; _instructions[0x16] = {"ASL", &Cpu::ASL, &Cpu::ZPX, 6, false};
_instructions[0x1E] = {"ASL", &Cpu::ASL, &Cpu::ABX, 7, false}; _instructions[0x1E] = {"ASL", &Cpu::ASL, &Cpu::ABX, 7, false};
_instructions[0x21] = {"AND", &Cpu::AND, &Cpu::IZX, 6, false}; _instructions[0x21] = {"AND", &Cpu::AND, &Cpu::IZX, 6, false};
_instructions[0x25] = {"AND", &Cpu::AND, &Cpu::ZP0, 3, false}; _instructions[0x25] = {"AND", &Cpu::AND, &Cpu::ZP0, 3, false};
_instructions[0x29] = {"AND", &Cpu::AND, &Cpu::IMM, 2, false}; _instructions[0x29] = {"AND", &Cpu::AND, &Cpu::IMM, 2, false};
_instructions[0x2D] = {"AND", &Cpu::AND, &Cpu::ABS, 4, false}; _instructions[0x2D] = {"AND", &Cpu::AND, &Cpu::ABSL, 4, false};
_instructions[0x31] = {"AND", &Cpu::AND, &Cpu::IZY, 5, true}; _instructions[0x31] = {"AND", &Cpu::AND, &Cpu::IZY, 5, true};
_instructions[0x35] = {"AND", &Cpu::AND, &Cpu::ZPX, 4, false}; _instructions[0x35] = {"AND", &Cpu::AND, &Cpu::ZPX, 4, false};
_instructions[0x39] = {"AND", &Cpu::AND, &Cpu::ABY, 4, true}; _instructions[0x39] = {"AND", &Cpu::AND, &Cpu::ABY, 4, true};
@ -36,14 +40,14 @@ namespace nes {
_instructions[0x41] = {"EOR", &Cpu::EOR, &Cpu::IZX, 6, false}; _instructions[0x41] = {"EOR", &Cpu::EOR, &Cpu::IZX, 6, false};
_instructions[0x45] = {"EOR", &Cpu::EOR, &Cpu::ZP0, 3, false}; _instructions[0x45] = {"EOR", &Cpu::EOR, &Cpu::ZP0, 3, false};
_instructions[0x49] = {"EOR", &Cpu::EOR, &Cpu::IMM, 2, false}; _instructions[0x49] = {"EOR", &Cpu::EOR, &Cpu::IMM, 2, false};
_instructions[0x4D] = {"EOR", &Cpu::EOR, &Cpu::ABS, 4, false}; _instructions[0x4D] = {"EOR", &Cpu::EOR, &Cpu::ABSL, 4, false};
_instructions[0x51] = {"EOR", &Cpu::EOR, &Cpu::IZY, 5, true}; _instructions[0x51] = {"EOR", &Cpu::EOR, &Cpu::IZY, 5, true};
_instructions[0x55] = {"EOR", &Cpu::EOR, &Cpu::ZPX, 4, false}; _instructions[0x55] = {"EOR", &Cpu::EOR, &Cpu::ZPX, 4, false};
_instructions[0x59] = {"EOR", &Cpu::EOR, &Cpu::ABY, 4, true}; _instructions[0x59] = {"EOR", &Cpu::EOR, &Cpu::ABY, 4, true};
_instructions[0x5D] = {"EOR", &Cpu::EOR, &Cpu::ABX, 4, true}; _instructions[0x5D] = {"EOR", &Cpu::EOR, &Cpu::ABX, 4, true};
_instructions[0x61] = {"ADC", &Cpu::ADC, &Cpu::IZX, 6, false}; _instructions[0x61] = {"ADC", &Cpu::ADC, &Cpu::IZX, 6, false};
_instructions[0x65] = {"ADC", &Cpu::ADC, &Cpu::ZP0, 3, false}; _instructions[0x65] = {"ADC", &Cpu::ADC, &Cpu::ZP0, 3, false};
_instructions[0x6D] = {"ADC", &Cpu::ADC, &Cpu::ABS, 4, false}; _instructions[0x6D] = {"ADC", &Cpu::ADC, &Cpu::ABSL, 4, false};
_instructions[0x69] = {"ADC", &Cpu::ADC, &Cpu::IMM, 2, false}; _instructions[0x69] = {"ADC", &Cpu::ADC, &Cpu::IMM, 2, false};
_instructions[0x71] = {"ADC", &Cpu::ADC, &Cpu::IZY, 5, true}; _instructions[0x71] = {"ADC", &Cpu::ADC, &Cpu::IZY, 5, true};
_instructions[0x75] = {"ADC", &Cpu::ADC, &Cpu::ZPX, 4, false}; _instructions[0x75] = {"ADC", &Cpu::ADC, &Cpu::ZPX, 4, false};
@ -51,114 +55,114 @@ namespace nes {
_instructions[0x7D] = {"ADC", &Cpu::ADC, &Cpu::ABX, 4, true}; _instructions[0x7D] = {"ADC", &Cpu::ADC, &Cpu::ABX, 4, true};
_instructions[0xA2] = {"LDX", &Cpu::LDX, &Cpu::IMM, 2, false}; _instructions[0xA2] = {"LDX", &Cpu::LDX, &Cpu::IMM, 2, false};
_instructions[0xA6] = {"LDX", &Cpu::LDX, &Cpu::ZP0, 3, false}; _instructions[0xA6] = {"LDX", &Cpu::LDX, &Cpu::ZP0, 3, false};
_instructions[0xAE] = {"LDX", &Cpu::LDX, &Cpu::ABS, 4, false}; _instructions[0xAE] = {"LDX", &Cpu::LDX, &Cpu::ABSL, 4, false};
_instructions[0xB6] = {"LDX", &Cpu::LDX, &Cpu::ZPY, 4, false}; _instructions[0xB6] = {"LDX", &Cpu::LDX, &Cpu::ZPY, 4, false};
_instructions[0xBE] = {"LDX", &Cpu::LDX, &Cpu::ABY, 4, true}; _instructions[0xBE] = {"LDX", &Cpu::LDX, &Cpu::ABY, 4, true};
_instructions[0x86] = {"STX", &Cpu::STX, &Cpu::ZP0, 3, false}; _instructions[0x86] = {"STX", &Cpu::STX, &Cpu::ZP0, 3, false};
_instructions[0x8E] = {"STX", &Cpu::STX, &Cpu::ABS, 4, false}; _instructions[0x8E] = {"STX", &Cpu::STX, &Cpu::ABSL, 4, false};
_instructions[0x96] = {"STX", &Cpu::STX, &Cpu::ZPY, 4, false}; _instructions[0x96] = {"STX", &Cpu::STX, &Cpu::ZPY, 4, false};
_instructions[0xA4] = {"LDY", &Cpu::LDY, &Cpu::ZP0, 3, false}; _instructions[0xA4] = {"LDY", &Cpu::LDY, &Cpu::ZP0, 3, false};
_instructions[0xAC] = {"LDY", &Cpu::LDY, &Cpu::ABS, 4, false}; _instructions[0xAC] = {"LDY", &Cpu::LDY, &Cpu::ABSL, 4, false};
_instructions[0xA0] = {"LDY", &Cpu::LDY, &Cpu::IMM, 2, false}; _instructions[0xA0] = {"LDY", &Cpu::LDY, &Cpu::IMM, 2, false};
_instructions[0xB4] = {"LDY", &Cpu::LDY, &Cpu::ZPX, 4, false}; _instructions[0xB4] = {"LDY", &Cpu::LDY, &Cpu::ZPX, 4, false};
_instructions[0xBC] = {"LDY", &Cpu::LDY, &Cpu::ABX, 4, true}; _instructions[0xBC] = {"LDY", &Cpu::LDY, &Cpu::ABX, 4, true};
_instructions[0xA1] = {"LDA", &Cpu::LDA, &Cpu::IZX, 6, false}; _instructions[0xA1] = {"LDA", &Cpu::LDA, &Cpu::IZX, 6, false};
_instructions[0xA5] = {"LDA", &Cpu::LDA, &Cpu::ZP0, 3, false}; _instructions[0xA5] = {"LDA", &Cpu::LDA, &Cpu::ZP0, 3, false};
_instructions[0xA9] = {"LDA", &Cpu::LDA, &Cpu::IMM, 2, false}; _instructions[0xA9] = {"LDA", &Cpu::LDA, &Cpu::IMM, 2, false};
_instructions[0xAD] = {"LDA", &Cpu::LDA, &Cpu::ABS, 4, false}; _instructions[0xAD] = {"LDA", &Cpu::LDA, &Cpu::ABSL, 4, false};
_instructions[0xB1] = {"LDA", &Cpu::LDA, &Cpu::IZY, 5, true}; _instructions[0xB1] = {"LDA", &Cpu::LDA, &Cpu::IZY, 5, true};
_instructions[0xB5] = {"LDA", &Cpu::LDA, &Cpu::ZPX, 4, false}; _instructions[0xB5] = {"LDA", &Cpu::LDA, &Cpu::ZPX, 4, false};
_instructions[0xB9] = {"LDA", &Cpu::LDA, &Cpu::ABY, 4, true}; _instructions[0xB9] = {"LDA", &Cpu::LDA, &Cpu::ABY, 4, true};
_instructions[0xBD] = {"LDA", &Cpu::LDA, &Cpu::ABX, 4, true}; _instructions[0xBD] = {"LDA", &Cpu::LDA, &Cpu::ABX, 4, true};
_instructions[0x18] = {"CLC", &Cpu::CLC, &Cpu::IMP, 2, false}; _instructions[0x18] = {"CLC", &Cpu::CLC, &Cpu::IMPL, 2, false};
_instructions[0x88] = {"DEY", &Cpu::DEY, &Cpu::IMP, 2, false}; _instructions[0x88] = {"DEY", &Cpu::DEY, &Cpu::IMPL, 2, false};
_instructions[0xD0] = {"BNE", &Cpu::BNE, &Cpu::REL, 2, false}; _instructions[0xD0] = {"BNE", &Cpu::BNE, &Cpu::REL, 2, false};
_instructions[0x81] = {"STA", &Cpu::STA, &Cpu::IZX, 6, false}; _instructions[0x81] = {"STA", &Cpu::STA, &Cpu::IZX, 6, false};
_instructions[0x8D] = {"STA", &Cpu::STA, &Cpu::ABS, 4, false}; _instructions[0x8D] = {"STA", &Cpu::STA, &Cpu::ABSL, 4, false};
_instructions[0x91] = {"STA", &Cpu::STA, &Cpu::IZY, 6, false}; _instructions[0x91] = {"STA", &Cpu::STA, &Cpu::IZY, 6, false};
_instructions[0x95] = {"STA", &Cpu::STA, &Cpu::ZPX, 4, false}; _instructions[0x95] = {"STA", &Cpu::STA, &Cpu::ZPX, 4, false};
_instructions[0x99] = {"STA", &Cpu::STA, &Cpu::ABY, 5, false}; _instructions[0x99] = {"STA", &Cpu::STA, &Cpu::ABY, 5, false};
_instructions[0x9D] = {"STA", &Cpu::STA, &Cpu::ABX, 5, false}; _instructions[0x9D] = {"STA", &Cpu::STA, &Cpu::ABX, 5, false};
_instructions[0xEA] = {"NOP", &Cpu::NOP, &Cpu::IMP, 2, false}; _instructions[0xEA] = {"NOP", &Cpu::NOP, &Cpu::IMPL, 2, false};
_instructions[0x78] = {"SEI", &Cpu::SEI, &Cpu::IMP, 2, false}; _instructions[0x78] = {"SEI", &Cpu::SEI, &Cpu::IMPL, 2, false};
_instructions[0xD8] = {"CLD", &Cpu::CLD, &Cpu::IMP, 2, false}; _instructions[0xD8] = {"CLD", &Cpu::CLD, &Cpu::IMPL, 2, false};
_instructions[0x9A] = {"TXS", &Cpu::TXS, &Cpu::IMP, 2, false}; _instructions[0x9A] = {"TXS", &Cpu::TXS, &Cpu::IMPL, 2, false};
_instructions[0x10] = {"BPL", &Cpu::BPL, &Cpu::REL, 2, false}; _instructions[0x10] = {"BPL", &Cpu::BPL, &Cpu::REL, 2, false};
_instructions[0x4C] = {"JMP", &Cpu::JMP, &Cpu::ABS, 3, false}; _instructions[0x4C] = {"JMP", &Cpu::JMP, &Cpu::ABSL, 3, false};
_instructions[0x6C] = {"JMP", &Cpu::JMP, &Cpu::IND, 5, false}; _instructions[0x6C] = {"JMP", &Cpu::JMP, &Cpu::IND, 5, false};
_instructions[0x20] = {"JSR", &Cpu::JSR, &Cpu::ABS, 6, false}; _instructions[0x20] = {"JSR", &Cpu::JSR, &Cpu::ABSL, 6, false};
_instructions[0x38] = {"SEC", &Cpu::SEC, &Cpu::IMP, 2, false}; _instructions[0x38] = {"SEC", &Cpu::SEC, &Cpu::IMPL, 2, false};
_instructions[0xB0] = {"BCS", &Cpu::BCS, &Cpu::REL, 2, false}; _instructions[0xB0] = {"BCS", &Cpu::BCS, &Cpu::REL, 2, false};
_instructions[0x90] = {"BCC", &Cpu::BCC, &Cpu::REL, 2, false}; _instructions[0x90] = {"BCC", &Cpu::BCC, &Cpu::REL, 2, false};
_instructions[0xF0] = {"BEQ", &Cpu::BEQ, &Cpu::REL, 2, false}; _instructions[0xF0] = {"BEQ", &Cpu::BEQ, &Cpu::REL, 2, false};
_instructions[0x85] = {"STA", &Cpu::STA, &Cpu::ZP0, 3, false}; _instructions[0x85] = {"STA", &Cpu::STA, &Cpu::ZP0, 3, false};
_instructions[0x24] = {"BIT", &Cpu::BIT, &Cpu::ZP0, 3, false}; _instructions[0x24] = {"BIT", &Cpu::BIT, &Cpu::ZP0, 3, false};
_instructions[0x2C] = {"BIT", &Cpu::BIT, &Cpu::ABS, 4, false}; _instructions[0x2C] = {"BIT", &Cpu::BIT, &Cpu::ABSL, 4, false};
_instructions[0x70] = {"BVS", &Cpu::BVS, &Cpu::REL, 2, false}; _instructions[0x70] = {"BVS", &Cpu::BVS, &Cpu::REL, 2, false};
_instructions[0x50] = {"BVC", &Cpu::BVC, &Cpu::REL, 2, false}; _instructions[0x50] = {"BVC", &Cpu::BVC, &Cpu::REL, 2, false};
_instructions[0x60] = {"RTS", &Cpu::RTS, &Cpu::IMP, 6, false}; _instructions[0x60] = {"RTS", &Cpu::RTS, &Cpu::IMPL, 6, false};
_instructions[0xF8] = {"SED", &Cpu::SED, &Cpu::IMP, 2, false}; _instructions[0xF8] = {"SED", &Cpu::SED, &Cpu::IMPL, 2, false};
_instructions[0x08] = {"PHP", &Cpu::PHP, &Cpu::IMP, 3, false}; _instructions[0x08] = {"PHP", &Cpu::PHP, &Cpu::IMPL, 3, false};
_instructions[0x68] = {"PLA", &Cpu::PLA, &Cpu::IMP, 4, false}; _instructions[0x68] = {"PLA", &Cpu::PLA, &Cpu::IMPL, 4, false};
_instructions[0xC1] = {"CMP", &Cpu::CMP, &Cpu::IZX, 6, false}; _instructions[0xC1] = {"CMP", &Cpu::CMP, &Cpu::IZX, 6, false};
_instructions[0xC5] = {"CMP", &Cpu::CMP, &Cpu::ZP0, 3, false}; _instructions[0xC5] = {"CMP", &Cpu::CMP, &Cpu::ZP0, 3, false};
_instructions[0xC9] = {"CMP", &Cpu::CMP, &Cpu::IMM, 2, false}; _instructions[0xC9] = {"CMP", &Cpu::CMP, &Cpu::IMM, 2, false};
_instructions[0xCD] = {"CMP", &Cpu::CMP, &Cpu::ABS, 4, false}; _instructions[0xCD] = {"CMP", &Cpu::CMP, &Cpu::ABSL, 4, false};
_instructions[0xD1] = {"CMP", &Cpu::CMP, &Cpu::IZY, 5, true}; _instructions[0xD1] = {"CMP", &Cpu::CMP, &Cpu::IZY, 5, true};
_instructions[0xD5] = {"CMP", &Cpu::CMP, &Cpu::ZPX, 4, false}; _instructions[0xD5] = {"CMP", &Cpu::CMP, &Cpu::ZPX, 4, false};
_instructions[0xD9] = {"CMP", &Cpu::CMP, &Cpu::ABY, 4, true}; _instructions[0xD9] = {"CMP", &Cpu::CMP, &Cpu::ABY, 4, true};
_instructions[0xDD] = {"CMP", &Cpu::CMP, &Cpu::ABX, 4, true}; _instructions[0xDD] = {"CMP", &Cpu::CMP, &Cpu::ABX, 4, true};
_instructions[0x30] = {"BMI", &Cpu::BMI, &Cpu::REL, 2, false}; _instructions[0x30] = {"BMI", &Cpu::BMI, &Cpu::REL, 2, false};
_instructions[0x48] = {"PHA", &Cpu::PHA, &Cpu::IMP, 3, false}; _instructions[0x48] = {"PHA", &Cpu::PHA, &Cpu::IMPL, 3, false};
_instructions[0x28] = {"PLP", &Cpu::PLP, &Cpu::IMP, 4, false}; _instructions[0x28] = {"PLP", &Cpu::PLP, &Cpu::IMPL, 4, false};
_instructions[0xB8] = {"CLV", &Cpu::CLV, &Cpu::IMP, 2, false}; _instructions[0xB8] = {"CLV", &Cpu::CLV, &Cpu::IMPL, 2, false};
_instructions[0xC0] = {"CPY", &Cpu::CPY, &Cpu::IMM, 2, false}; _instructions[0xC0] = {"CPY", &Cpu::CPY, &Cpu::IMM, 2, false};
_instructions[0xC4] = {"CPY", &Cpu::CPY, &Cpu::ZP0, 3, false}; _instructions[0xC4] = {"CPY", &Cpu::CPY, &Cpu::ZP0, 3, false};
_instructions[0xCC] = {"CPY", &Cpu::CPY, &Cpu::ABS, 4, false}; _instructions[0xCC] = {"CPY", &Cpu::CPY, &Cpu::ABSL, 4, false};
_instructions[0xE0] = {"CPX", &Cpu::CPX, &Cpu::IMM, 2, false}; _instructions[0xE0] = {"CPX", &Cpu::CPX, &Cpu::IMM, 2, false};
_instructions[0xE4] = {"CPX", &Cpu::CPX, &Cpu::ZP0, 3, false}; _instructions[0xE4] = {"CPX", &Cpu::CPX, &Cpu::ZP0, 3, false};
_instructions[0xEC] = {"CPX", &Cpu::CPX, &Cpu::ABS, 4, false}; _instructions[0xEC] = {"CPX", &Cpu::CPX, &Cpu::ABSL, 4, false};
_instructions[0xE1] = {"SBC", &Cpu::SBC, &Cpu::IZX, 6, false}; _instructions[0xE1] = {"SBC", &Cpu::SBC, &Cpu::IZX, 6, false};
_instructions[0xE5] = {"SBC", &Cpu::SBC, &Cpu::ZP0, 3, false}; _instructions[0xE5] = {"SBC", &Cpu::SBC, &Cpu::ZP0, 3, false};
_instructions[0xE9] = {"SBC", &Cpu::SBC, &Cpu::IMM, 2, false}; _instructions[0xE9] = {"SBC", &Cpu::SBC, &Cpu::IMM, 2, false};
_instructions[0xED] = {"SBC", &Cpu::SBC, &Cpu::ABS, 4, false}; _instructions[0xED] = {"SBC", &Cpu::SBC, &Cpu::ABSL, 4, false};
_instructions[0xF1] = {"SBC", &Cpu::SBC, &Cpu::IZY, 5, true}; _instructions[0xF1] = {"SBC", &Cpu::SBC, &Cpu::IZY, 5, true};
_instructions[0xF5] = {"SBC", &Cpu::SBC, &Cpu::ZPX, 4, false}; _instructions[0xF5] = {"SBC", &Cpu::SBC, &Cpu::ZPX, 4, false};
_instructions[0xF9] = {"SBC", &Cpu::SBC, &Cpu::ABY, 4, true}; _instructions[0xF9] = {"SBC", &Cpu::SBC, &Cpu::ABY, 4, true};
_instructions[0xFD] = {"SBC", &Cpu::SBC, &Cpu::ABX, 4, true}; _instructions[0xFD] = {"SBC", &Cpu::SBC, &Cpu::ABX, 4, true};
_instructions[0x84] = {"STY", &Cpu::STY, &Cpu::ZP0, 3, false}; _instructions[0x84] = {"STY", &Cpu::STY, &Cpu::ZP0, 3, false};
_instructions[0x8C] = {"STY", &Cpu::STY, &Cpu::ABS, 4, false}; _instructions[0x8C] = {"STY", &Cpu::STY, &Cpu::ABSL, 4, false};
_instructions[0x94] = {"STY", &Cpu::STY, &Cpu::ZPX, 4, false}; _instructions[0x94] = {"STY", &Cpu::STY, &Cpu::ZPX, 4, false};
_instructions[0xC8] = {"INY", &Cpu::INY, &Cpu::IMP, 2, false}; _instructions[0xC8] = {"INY", &Cpu::INY, &Cpu::IMPL, 2, false};
_instructions[0xE8] = {"INX", &Cpu::INX, &Cpu::IMP, 2, false}; _instructions[0xE8] = {"INX", &Cpu::INX, &Cpu::IMPL, 2, false};
_instructions[0xCA] = {"DEX", &Cpu::DEX, &Cpu::IMP, 2, false}; _instructions[0xCA] = {"DEX", &Cpu::DEX, &Cpu::IMPL, 2, false};
_instructions[0xA8] = {"TAY", &Cpu::TAY, &Cpu::IMP, 2, false}; _instructions[0xA8] = {"TAY", &Cpu::TAY, &Cpu::IMPL, 2, false};
_instructions[0xAA] = {"TAX", &Cpu::TAX, &Cpu::IMP, 2, false}; _instructions[0xAA] = {"TAX", &Cpu::TAX, &Cpu::IMPL, 2, false};
_instructions[0x98] = {"TYA", &Cpu::TYA, &Cpu::IMP, 2, false}; _instructions[0x98] = {"TYA", &Cpu::TYA, &Cpu::IMPL, 2, false};
_instructions[0x8A] = {"TXA", &Cpu::TXA, &Cpu::IMP, 2, false}; _instructions[0x8A] = {"TXA", &Cpu::TXA, &Cpu::IMPL, 2, false};
_instructions[0xBA] = {"TSX", &Cpu::TSX, &Cpu::IMP, 2, false}; _instructions[0xBA] = {"TSX", &Cpu::TSX, &Cpu::IMPL, 2, false};
_instructions[0x40] = {"RTI", &Cpu::RTI, &Cpu::IMP, 6, false}; _instructions[0x40] = {"RTI", &Cpu::RTI, &Cpu::IMPL, 6, false};
_instructions[0x46] = {"LSR", &Cpu::LSR, &Cpu::ZP0, 5, false}; _instructions[0x46] = {"LSR", &Cpu::LSR, &Cpu::ZP0, 5, false};
_instructions[0x4E] = {"LSR", &Cpu::LSR, &Cpu::ABS, 6, false}; _instructions[0x4E] = {"LSR", &Cpu::LSR, &Cpu::ABSL, 6, false};
_instructions[0x4A] = {"LSR", &Cpu::LSR_ACC, &Cpu::IMP, 2, false}; _instructions[0x4A] = {"LSR", &Cpu::LSR_ACC, &Cpu::IMPL, 2, false};
_instructions[0x56] = {"LSR", &Cpu::LSR, &Cpu::ZPX, 6, false}; _instructions[0x56] = {"LSR", &Cpu::LSR, &Cpu::ZPX, 6, false};
_instructions[0x5E] = {"LSR", &Cpu::LSR, &Cpu::ABX, 7, false}; _instructions[0x5E] = {"LSR", &Cpu::LSR, &Cpu::ABX, 7, false};
_instructions[0x66] = {"ROR", &Cpu::ROR, &Cpu::ZP0, 5, false}; _instructions[0x66] = {"ROR", &Cpu::ROR, &Cpu::ZP0, 5, false};
_instructions[0x6A] = {"ROR", &Cpu::ROR_ACC, &Cpu::IMP, 2, false}; _instructions[0x6A] = {"ROR", &Cpu::ROR_ACC, &Cpu::IMPL, 2, false};
_instructions[0x6E] = {"ROR", &Cpu::ROR, &Cpu::ABS, 6, false}; _instructions[0x6E] = {"ROR", &Cpu::ROR, &Cpu::ABSL, 6, false};
_instructions[0x76] = {"ROR", &Cpu::ROR, &Cpu::ZPX, 6, false}; _instructions[0x76] = {"ROR", &Cpu::ROR, &Cpu::ZPX, 6, false};
_instructions[0x7E] = {"ROR", &Cpu::ROR, &Cpu::ABX, 7, false}; _instructions[0x7E] = {"ROR", &Cpu::ROR, &Cpu::ABX, 7, false};
_instructions[0x26] = {"ROL", &Cpu::ROL, &Cpu::ZP0, 5, false}; _instructions[0x26] = {"ROL", &Cpu::ROL, &Cpu::ZP0, 5, false};
_instructions[0x2A] = {"ROL", &Cpu::ROL_ACC, &Cpu::IMP, 2, false}; _instructions[0x2A] = {"ROL", &Cpu::ROL_ACC, &Cpu::IMPL, 2, false};
_instructions[0x2E] = {"ROL", &Cpu::ROL, &Cpu::ABS, 6, false}; _instructions[0x2E] = {"ROL", &Cpu::ROL, &Cpu::ABSL, 6, false};
_instructions[0x36] = {"ROL", &Cpu::ROL, &Cpu::ZPX, 6, false}; _instructions[0x36] = {"ROL", &Cpu::ROL, &Cpu::ZPX, 6, false};
_instructions[0x3E] = {"ROL", &Cpu::ROL, &Cpu::ABX, 7, false}; _instructions[0x3E] = {"ROL", &Cpu::ROL, &Cpu::ABX, 7, false};
_instructions[0xE6] = {"INC", &Cpu::INC, &Cpu::ZP0, 5, false}; _instructions[0xE6] = {"INC", &Cpu::INC, &Cpu::ZP0, 5, false};
_instructions[0xEE] = {"INC", &Cpu::INC, &Cpu::ABS, 6, false}; _instructions[0xEE] = {"INC", &Cpu::INC, &Cpu::ABSL, 6, false};
_instructions[0xF6] = {"INC", &Cpu::INC, &Cpu::ZPX, 6, false}; _instructions[0xF6] = {"INC", &Cpu::INC, &Cpu::ZPX, 6, false};
_instructions[0xFE] = {"INC", &Cpu::INC, &Cpu::ABX, 7, false}; _instructions[0xFE] = {"INC", &Cpu::INC, &Cpu::ABX, 7, false};
_instructions[0xC6] = {"DEC", &Cpu::DEC, &Cpu::ZP0, 5, false}; _instructions[0xC6] = {"DEC", &Cpu::DEC, &Cpu::ZP0, 5, false};
_instructions[0xCE] = {"DEC", &Cpu::DEC, &Cpu::ABS, 6, false}; _instructions[0xCE] = {"DEC", &Cpu::DEC, &Cpu::ABSL, 6, false};
_instructions[0xD6] = {"DEC", &Cpu::DEC, &Cpu::ZPX, 6, false}; _instructions[0xD6] = {"DEC", &Cpu::DEC, &Cpu::ZPX, 6, false};
_instructions[0xDE] = {"DEC", &Cpu::DEC, &Cpu::ABX, 7, false}; _instructions[0xDE] = {"DEC", &Cpu::DEC, &Cpu::ABX, 7, false};
} }
@ -170,8 +174,8 @@ namespace nes {
SP = 0xFD; SP = 0xFD;
flags = 0; flags = 0;
uint16_t lo = _bus->read(0xFFFC); uint16_t lo = _system->read(0xFFFC);
uint16_t hi = _bus->read(0xFFFD); uint16_t hi = _system->read(0xFFFD);
PC = (hi << 8) | lo; PC = (hi << 8) | lo;
@ -182,7 +186,7 @@ namespace nes {
bool executed = false; bool executed = false;
if(_ticks == 0) { if(_ticks == 0) {
uint8_t opcode = _bus->read(PC++); uint8_t opcode = _system->read(PC++);
auto instruction = _instructions[opcode]; auto instruction = _instructions[opcode];
_currentOpcode = opcode; _currentOpcode = opcode;
@ -207,23 +211,24 @@ namespace nes {
} }
void Cpu::nmi() { void Cpu::nmi() {
_bus->write(STACK_BASE + SP--, PC >> 8); _system->write(STACK_BASE + SP--, PC >> 8);
_bus->write(STACK_BASE + SP--, PC & 0x00FF); _system->write(STACK_BASE + SP--, PC & 0x00FF);
setFlag(Break, false); setFlag(Break, false);
setFlag(Unused, true); setFlag(Unused, true);
setFlag(InterruptDisable, true); setFlag(InterruptDisable, true);
_bus->write(STACK_BASE + SP--, flags); _system->write(STACK_BASE + SP--, flags);
uint8_t lo = _bus->read(0xFFFA); uint8_t lo = _system->read(0xFFFA);
uint8_t hi = _bus->read(0xFFFB); uint8_t hi = _system->read(0xFFFB);
PC = (hi << 8) | lo; PC = (hi << 8) | lo;
_ticks = 8; _ticks = 8;
} }
#ifdef NES_LOGGING
std::string Cpu::state() const { std::string Cpu::state() const {
return std::format("{} ({:02X}), PC: {:X}, SP: {:X}, A: {:02X}, X: {:02X}, Y: {:02X}, [N:{}, V:{}, B{}, D{}, I{}, Z:{}, C:{}], H: {:08X}", return fmt::format("{} ({:02X}), PC: {:X}, SP: {:X}, A: {:02X}, X: {:02X}, Y: {:02X}, [N:{}, V:{}, B{}, D{}, I{}, Z:{}, C:{}], H: {:08X}",
_instructions[_currentOpcode].name, _instructions[_currentOpcode].name,
_currentOpcode, _currentOpcode,
PC, SP, A, X, Y, PC, SP, A, X, Y,
@ -234,8 +239,9 @@ namespace nes {
(int)getFlag(InterruptDisable), (int)getFlag(InterruptDisable),
(int)getFlag(Zero), (int)getFlag(Zero),
(int)getFlag(Carry), (int)getFlag(Carry),
_bus->zpHash()); _system->zpHash());
} }
#endif
void Cpu::setFlag(CpuFlags flag, bool value) { void Cpu::setFlag(CpuFlags flag, bool value) {
if(value) { if(value) {
@ -253,10 +259,6 @@ namespace nes {
PC = address; PC = address;
} }
Bus* Cpu::bus() const {
return _bus.get();
}
void Cpu::branch(Cpu::InstructionArgs args) { void Cpu::branch(Cpu::InstructionArgs args) {
_ticks++; _ticks++;
@ -277,19 +279,19 @@ namespace nes {
return {PC++, 0}; return {PC++, 0};
} }
Cpu::InstructionArgs Cpu::ABS() { Cpu::InstructionArgs Cpu::ABSL() {
uint8_t lo = _bus->read(PC++); uint8_t lo = _system->read(PC++);
uint8_t hi = _bus->read(PC++); uint8_t hi = _system->read(PC++);
uint16_t address = (hi << 8) | lo; uint16_t address = (hi << 8) | lo;
return {address, 0}; return {address, 0};
} }
Cpu::InstructionArgs Cpu::IMP() { Cpu::InstructionArgs Cpu::IMPL() {
return {0, 0}; return {0, 0};
} }
Cpu::InstructionArgs Cpu::REL() { Cpu::InstructionArgs Cpu::REL() {
uint16_t address = _bus->read(PC++); uint16_t address = _system->read(PC++);
if (address & 0x80) { if (address & 0x80) {
address |= 0xFF00; address |= 0xFF00;
@ -299,22 +301,22 @@ namespace nes {
} }
Cpu::InstructionArgs Cpu::ZP0() { Cpu::InstructionArgs Cpu::ZP0() {
uint16_t address = _bus->read(PC++); uint16_t address = _system->read(PC++);
return {address, 0}; return {address, 0};
} }
Cpu::InstructionArgs Cpu::IZX() { Cpu::InstructionArgs Cpu::IZX() {
uint8_t ptrAddress = _bus->read(PC++) + X; uint8_t ptrAddress = _system->read(PC++) + X;
uint8_t lo = _bus->read(ptrAddress); uint8_t lo = _system->read(ptrAddress);
uint8_t hi = _bus->read(++ptrAddress); uint8_t hi = _system->read(++ptrAddress);
uint16_t address = (hi << 8) | lo; uint16_t address = (hi << 8) | lo;
return {address, 0}; return {address, 0};
} }
Cpu::InstructionArgs Cpu::IZY() { Cpu::InstructionArgs Cpu::IZY() {
uint8_t ptrAddress = _bus->read(PC++); uint8_t ptrAddress = _system->read(PC++);
uint8_t lo = _bus->read(ptrAddress); uint8_t lo = _system->read(ptrAddress);
uint8_t hi = _bus->read(++ptrAddress); uint8_t hi = _system->read(++ptrAddress);
uint16_t address = (hi << 8) | lo; uint16_t address = (hi << 8) | lo;
address += Y; address += Y;
@ -327,17 +329,17 @@ namespace nes {
} }
Cpu::InstructionArgs Cpu::IND() { Cpu::InstructionArgs Cpu::IND() {
uint8_t ptrLo = _bus->read(PC++); uint8_t ptrLo = _system->read(PC++);
uint8_t ptrHi = _bus->read(PC++); uint8_t ptrHi = _system->read(PC++);
uint16_t ptrAddress = (ptrHi << 8) | ptrLo; uint16_t ptrAddress = (ptrHi << 8) | ptrLo;
uint8_t lo = _bus->read(ptrAddress); uint8_t lo = _system->read(ptrAddress);
uint8_t hi = 0; uint8_t hi = 0;
if(ptrLo == 0xFF) { if(ptrLo == 0xFF) {
hi = _bus->read(ptrAddress & 0xFF00); hi = _system->read(ptrAddress & 0xFF00);
} else { } else {
hi = _bus->read(++ptrAddress); hi = _system->read(++ptrAddress);
} }
uint16_t address = (hi << 8) | lo; uint16_t address = (hi << 8) | lo;
@ -345,8 +347,8 @@ namespace nes {
} }
Cpu::InstructionArgs Cpu::ABX() { Cpu::InstructionArgs Cpu::ABX() {
uint8_t lo = _bus->read(PC++); uint8_t lo = _system->read(PC++);
uint8_t hi = _bus->read(PC++); uint8_t hi = _system->read(PC++);
uint16_t address = (hi << 8) | lo; uint16_t address = (hi << 8) | lo;
address += X; address += X;
@ -359,8 +361,8 @@ namespace nes {
} }
Cpu::InstructionArgs Cpu::ABY() { Cpu::InstructionArgs Cpu::ABY() {
uint8_t lo = _bus->read(PC++); uint8_t lo = _system->read(PC++);
uint8_t hi = _bus->read(PC++); uint8_t hi = _system->read(PC++);
uint16_t address = (hi << 8) | lo; uint16_t address = (hi << 8) | lo;
address += Y; address += Y;
@ -373,41 +375,41 @@ namespace nes {
} }
Cpu::InstructionArgs Cpu::ZPX() { Cpu::InstructionArgs Cpu::ZPX() {
uint8_t address = _bus->read(PC++) + X; uint8_t address = _system->read(PC++) + X;
return {address, 0}; return {address, 0};
} }
Cpu::InstructionArgs Cpu::ZPY() { Cpu::InstructionArgs Cpu::ZPY() {
uint8_t address = _bus->read(PC++) + Y; uint8_t address = _system->read(PC++) + Y;
return {address, 0}; return {address, 0};
} }
// CPU instructions // CPU instructions
void Cpu::LDA(Cpu::InstructionArgs args) { void Cpu::LDA(Cpu::InstructionArgs args) {
A = _bus->read(args.address); A = _system->read(args.address);
setFlag(Negative, A & 0x80); setFlag(Negative, A & 0x80);
setFlag(Zero, A == 0); setFlag(Zero, A == 0);
} }
void Cpu::LDX(InstructionArgs args) { void Cpu::LDX(InstructionArgs args) {
X = _bus->read(args.address); X = _system->read(args.address);
setFlag(Negative, X & 0x80); setFlag(Negative, X & 0x80);
setFlag(Zero, X == 0); setFlag(Zero, X == 0);
} }
void Cpu::LDY(Cpu::InstructionArgs args) { void Cpu::LDY(Cpu::InstructionArgs args) {
Y = _bus->read(args.address); Y = _system->read(args.address);
setFlag(Negative, Y & 0x80); setFlag(Negative, Y & 0x80);
setFlag(Zero, Y == 0); setFlag(Zero, Y == 0);
} }
void Cpu::STA(Cpu::InstructionArgs args) { void Cpu::STA(Cpu::InstructionArgs args) {
_bus->write(args.address, A); _system->write(args.address, A);
} }
void Cpu::STX(InstructionArgs args) { void Cpu::STX(InstructionArgs args) {
_bus->write(args.address, X); _system->write(args.address, X);
} }
void Cpu::CLC(Cpu::InstructionArgs args) { void Cpu::CLC(Cpu::InstructionArgs args) {
@ -415,7 +417,7 @@ namespace nes {
} }
void Cpu::ADC(Cpu::InstructionArgs args) { void Cpu::ADC(Cpu::InstructionArgs args) {
uint16_t value = _bus->read(args.address); uint16_t value = _system->read(args.address);
uint16_t result = A + value + getFlag(Carry); uint16_t result = A + value + getFlag(Carry);
setFlag(Carry, result > 255); setFlag(Carry, result > 255);
@ -427,7 +429,7 @@ namespace nes {
} }
void Cpu::SBC(Cpu::InstructionArgs args) { void Cpu::SBC(Cpu::InstructionArgs args) {
uint16_t value = _bus->read(args.address) ^ 0xFF; uint16_t value = _system->read(args.address) ^ 0xFF;
uint16_t result = A + value + getFlag(Carry); uint16_t result = A + value + getFlag(Carry);
setFlag(Carry, result > 255); setFlag(Carry, result > 255);
@ -477,8 +479,8 @@ namespace nes {
void Cpu::JSR(Cpu::InstructionArgs args) { void Cpu::JSR(Cpu::InstructionArgs args) {
PC--; PC--;
_bus->write(STACK_BASE + SP--, PC >> 8); _system->write(STACK_BASE + SP--, PC >> 8);
_bus->write(STACK_BASE + SP--, PC & 0x00FF); _system->write(STACK_BASE + SP--, PC & 0x00FF);
PC = args.address; PC = args.address;
} }
@ -505,7 +507,7 @@ namespace nes {
} }
void Cpu::BIT(Cpu::InstructionArgs args) { void Cpu::BIT(Cpu::InstructionArgs args) {
uint8_t value = _bus->read(args.address); uint8_t value = _system->read(args.address);
setFlag(Zero, (A & value) == 0); setFlag(Zero, (A & value) == 0);
setFlag(Negative, value & (1 << 7)); setFlag(Negative, value & (1 << 7));
setFlag(Overflow, value & (1 << 6)); setFlag(Overflow, value & (1 << 6));
@ -524,8 +526,8 @@ namespace nes {
} }
void Cpu::RTS(Cpu::InstructionArgs args) { void Cpu::RTS(Cpu::InstructionArgs args) {
uint16_t lo = _bus->read(STACK_BASE + ++SP); uint16_t lo = _system->read(STACK_BASE + ++SP);
uint16_t hi = _bus->read(STACK_BASE + ++SP); uint16_t hi = _system->read(STACK_BASE + ++SP);
PC = (hi << 8) | lo; PC = (hi << 8) | lo;
PC++; PC++;
@ -536,25 +538,25 @@ namespace nes {
} }
void Cpu::PHP(Cpu::InstructionArgs args) { void Cpu::PHP(Cpu::InstructionArgs args) {
_bus->write(STACK_BASE + SP--, flags | Break | Unused); _system->write(STACK_BASE + SP--, flags | Break | Unused);
setFlag(Break, false); setFlag(Break, false);
setFlag(Unused, false); setFlag(Unused, false);
} }
void Cpu::PLA(Cpu::InstructionArgs args) { void Cpu::PLA(Cpu::InstructionArgs args) {
A = _bus->read(STACK_BASE + ++SP); A = _system->read(STACK_BASE + ++SP);
setFlag(Zero, A == 0); setFlag(Zero, A == 0);
setFlag(Negative, A & 0x80); setFlag(Negative, A & 0x80);
} }
void Cpu::AND(Cpu::InstructionArgs args) { void Cpu::AND(Cpu::InstructionArgs args) {
A &= _bus->read(args.address);; A &= _system->read(args.address);;
setFlag(Zero, A == 0); setFlag(Zero, A == 0);
setFlag(Negative, A & 0x80); setFlag(Negative, A & 0x80);
} }
void Cpu::CMP(Cpu::InstructionArgs args) { void Cpu::CMP(Cpu::InstructionArgs args) {
uint16_t value = _bus->read(args.address); uint16_t value = _system->read(args.address);
uint16_t diff = A - value; uint16_t diff = A - value;
setFlag(Carry, A >= value); setFlag(Carry, A >= value);
setFlag(Zero, (diff & 0x00FF) == 0); setFlag(Zero, (diff & 0x00FF) == 0);
@ -568,16 +570,16 @@ namespace nes {
} }
void Cpu::PHA(Cpu::InstructionArgs args) { void Cpu::PHA(Cpu::InstructionArgs args) {
_bus->write(STACK_BASE + SP--, A); _system->write(STACK_BASE + SP--, A);
} }
void Cpu::PLP(Cpu::InstructionArgs args) { void Cpu::PLP(Cpu::InstructionArgs args) {
flags = _bus->read(STACK_BASE + ++SP); flags = _system->read(STACK_BASE + ++SP);
setFlag(Unused, true); setFlag(Unused, true);
} }
void Cpu::ORA(Cpu::InstructionArgs args) { void Cpu::ORA(Cpu::InstructionArgs args) {
A |= _bus->read(args.address); A |= _system->read(args.address);
setFlag(Zero, A == 0); setFlag(Zero, A == 0);
setFlag(Negative, A & 0x80); setFlag(Negative, A & 0x80);
} }
@ -587,13 +589,13 @@ namespace nes {
} }
void Cpu::EOR(Cpu::InstructionArgs args) { void Cpu::EOR(Cpu::InstructionArgs args) {
A ^= _bus->read(args.address); A ^= _system->read(args.address);
setFlag(Zero, A == 0); setFlag(Zero, A == 0);
setFlag(Negative, A & 0x80); setFlag(Negative, A & 0x80);
} }
void Cpu::CPY(Cpu::InstructionArgs args) { void Cpu::CPY(Cpu::InstructionArgs args) {
uint16_t value = _bus->read(args.address); uint16_t value = _system->read(args.address);
uint16_t diff = Y - value; uint16_t diff = Y - value;
setFlag(Carry, Y >= value); setFlag(Carry, Y >= value);
setFlag(Zero, (diff & 0x00FF) == 0); setFlag(Zero, (diff & 0x00FF) == 0);
@ -601,7 +603,7 @@ namespace nes {
} }
void Cpu::CPX(Cpu::InstructionArgs args) { void Cpu::CPX(Cpu::InstructionArgs args) {
uint16_t value = _bus->read(args.address); uint16_t value = _system->read(args.address);
uint16_t diff = X - value; uint16_t diff = X - value;
setFlag(Carry, X >= value); setFlag(Carry, X >= value);
setFlag(Zero, (diff & 0x00FF) == 0); setFlag(Zero, (diff & 0x00FF) == 0);
@ -609,7 +611,7 @@ namespace nes {
} }
void Cpu::STY(Cpu::InstructionArgs args) { void Cpu::STY(Cpu::InstructionArgs args) {
_bus->write(args.address, Y); _system->write(args.address, Y);
} }
void Cpu::INY(Cpu::InstructionArgs args) { void Cpu::INY(Cpu::InstructionArgs args) {
@ -664,25 +666,25 @@ namespace nes {
PC++; PC++;
setFlag(InterruptDisable, true); setFlag(InterruptDisable, true);
_bus->write(STACK_BASE + SP--, PC >> 8); _system->write(STACK_BASE + SP--, PC >> 8);
_bus->write(STACK_BASE + SP--, PC & 0x00FF); _system->write(STACK_BASE + SP--, PC & 0x00FF);
setFlag(Break, true); setFlag(Break, true);
_bus->write(STACK_BASE + SP--, flags); _system->write(STACK_BASE + SP--, flags);
setFlag(Break, false); setFlag(Break, false);
uint16_t lo = _bus->read(0xFFFE); uint16_t lo = _system->read(0xFFFE);
uint16_t hi = _bus->read(0xFFFF); uint16_t hi = _system->read(0xFFFF);
PC = (hi << 8) | lo; PC = (hi << 8) | lo;
} }
void Cpu::RTI(Cpu::InstructionArgs args) { void Cpu::RTI(Cpu::InstructionArgs args) {
flags = _bus->read(STACK_BASE + ++SP); flags = _system->read(STACK_BASE + ++SP);
setFlag(Break, false); setFlag(Break, false);
setFlag(Unused, false); setFlag(Unused, false);
uint16_t lo = _bus->read(STACK_BASE + ++SP); uint16_t lo = _system->read(STACK_BASE + ++SP);
uint16_t hi = _bus->read(STACK_BASE + ++SP); uint16_t hi = _system->read(STACK_BASE + ++SP);
PC = (hi << 8) | lo; PC = (hi << 8) | lo;
} }
@ -694,12 +696,12 @@ namespace nes {
} }
void Cpu::LSR(Cpu::InstructionArgs args) { void Cpu::LSR(Cpu::InstructionArgs args) {
uint8_t value = _bus->read(args.address); uint8_t value = _system->read(args.address);
setFlag(Carry, value & 0x01); setFlag(Carry, value & 0x01);
value = value >> 1; value = value >> 1;
setFlag(Zero, value == 0); setFlag(Zero, value == 0);
setFlag(Negative, value & 0x80); setFlag(Negative, value & 0x80);
_bus->write(args.address, value); _system->write(args.address, value);
} }
void Cpu::ASL_ACC(Cpu::InstructionArgs args) { void Cpu::ASL_ACC(Cpu::InstructionArgs args) {
@ -710,12 +712,12 @@ namespace nes {
} }
void Cpu::ASL(Cpu::InstructionArgs args) { void Cpu::ASL(Cpu::InstructionArgs args) {
uint8_t value = _bus->read(args.address); uint8_t value = _system->read(args.address);
setFlag(Carry, value & 0x80); setFlag(Carry, value & 0x80);
value = value << 1; value = value << 1;
setFlag(Zero, value == 0); setFlag(Zero, value == 0);
setFlag(Negative, value & 0x80); setFlag(Negative, value & 0x80);
_bus->write(args.address, value); _system->write(args.address, value);
} }
void Cpu::ROR_ACC(Cpu::InstructionArgs args) { void Cpu::ROR_ACC(Cpu::InstructionArgs args) {
@ -727,13 +729,13 @@ namespace nes {
} }
void Cpu::ROR(Cpu::InstructionArgs args) { void Cpu::ROR(Cpu::InstructionArgs args) {
uint8_t value = _bus->read(args.address); uint8_t value = _system->read(args.address);
uint8_t carryOriginal = getFlag(Carry); uint8_t carryOriginal = getFlag(Carry);
setFlag(Carry, value & 0x01); setFlag(Carry, value & 0x01);
value = (value >> 1) | (carryOriginal << 7); value = (value >> 1) | (carryOriginal << 7);
setFlag(Zero, value == 0); setFlag(Zero, value == 0);
setFlag(Negative, value & 0x80); setFlag(Negative, value & 0x80);
_bus->write(args.address, value); _system->write(args.address, value);
} }
void Cpu::ROL_ACC(Cpu::InstructionArgs args) { void Cpu::ROL_ACC(Cpu::InstructionArgs args) {
@ -745,25 +747,25 @@ namespace nes {
} }
void Cpu::ROL(Cpu::InstructionArgs args) { void Cpu::ROL(Cpu::InstructionArgs args) {
uint8_t value = _bus->read(args.address); uint8_t value = _system->read(args.address);
uint8_t carryOriginal = getFlag(Carry); uint8_t carryOriginal = getFlag(Carry);
setFlag(Carry, value & 0x80); setFlag(Carry, value & 0x80);
value = (value << 1) | carryOriginal; value = (value << 1) | carryOriginal;
setFlag(Zero, value == 0); setFlag(Zero, value == 0);
setFlag(Negative, value & 0x80); setFlag(Negative, value & 0x80);
_bus->write(args.address, value); _system->write(args.address, value);
} }
void Cpu::INC(Cpu::InstructionArgs args) { void Cpu::INC(Cpu::InstructionArgs args) {
uint8_t value = _bus->read(args.address); uint8_t value = _system->read(args.address);
_bus->write(args.address, ++value); _system->write(args.address, ++value);
setFlag(Zero, value == 0); setFlag(Zero, value == 0);
setFlag(Negative, value & 0x80); setFlag(Negative, value & 0x80);
} }
void Cpu::DEC(Cpu::InstructionArgs args) { void Cpu::DEC(Cpu::InstructionArgs args) {
uint8_t value = _bus->read(args.address); uint8_t value = _system->read(args.address);
_bus->write(args.address, --value); _system->write(args.address, --value);
setFlag(Zero, value == 0); setFlag(Zero, value == 0);
setFlag(Negative, value & 0x80); setFlag(Negative, value & 0x80);
} }

View File

@ -5,12 +5,14 @@
#ifndef NES_CPU_H #ifndef NES_CPU_H
#define NES_CPU_H #define NES_CPU_H
#include "Bus.h"
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
#include <memory>
namespace nes { namespace nes {
class System;
enum CpuFlags: uint8_t { enum CpuFlags: uint8_t {
Carry = (1 << 0), Carry = (1 << 0),
Zero = (1 << 1), Zero = (1 << 1),
@ -40,19 +42,21 @@ namespace nes {
static constexpr uint16_t STACK_BASE = 0x0100; static constexpr uint16_t STACK_BASE = 0x0100;
public: public:
Cpu(); explicit Cpu(System* system);
void reset(); void reset();
bool tick(); bool tick();
void setFlag(CpuFlags flag, bool value); void setFlag(CpuFlags flag, bool value);
bool getFlag(CpuFlags flag) const; bool getFlag(CpuFlags flag) const;
void setStartAddress(uint16_t address); void setStartAddress(uint16_t address);
Bus* bus() const;
void nmi(); void nmi();
#ifdef NES_LOGGING
std::string state() const; std::string state() const;
#endif
private: private:
size_t _ticks; size_t _ticks;
std::unique_ptr<Bus> _bus; System* _system;
std::vector<Instruction> _instructions; std::vector<Instruction> _instructions;
private: private:
@ -74,8 +78,8 @@ namespace nes {
private: private:
InstructionArgs IMM(); InstructionArgs IMM();
InstructionArgs ABS(); InstructionArgs ABSL();
InstructionArgs IMP(); InstructionArgs IMPL();
InstructionArgs REL(); InstructionArgs REL();
InstructionArgs ZP0(); InstructionArgs ZP0();
InstructionArgs IZX(); InstructionArgs IZX();

44
src/Dma.cpp Normal file
View File

@ -0,0 +1,44 @@
//
// Created by selim on 9/23/23.
//
#include "Dma.h"
#include "Ppu.h"
#include "System.h"
namespace nes {
Dma::Dma(System *system, Ppu *ppu): _system{system}, _ppu{ppu} {
}
bool Dma::active() const {
return _active;
}
void Dma::tick(uint64_t cycles) {
if(_dummy) {
if(cycles % 2 == 1) {
_dummy = false;
}
} else {
if(cycles % 2 == 0) {
_data = _system->read((_page << 8) | _address);
} else {
_ppu->writeOam(_address, _data);
_address++;
if(_address == 0) {
_active = false;
_dummy = true;
}
}
}
}
void Dma::start(uint8_t page) {
_page = page;
_active = true;
}
}

38
src/Dma.h Normal file
View File

@ -0,0 +1,38 @@
//
// Created by selim on 9/23/23.
//
#ifndef NES_DMA_H
#define NES_DMA_H
#include <cstdint>
namespace nes {
class System;
class Ppu;
class Dma {
public:
Dma(System* system, Ppu* ppu);
[[nodiscard]] bool active() const;
void tick(uint64_t cycles);
void start(uint8_t page);
private:
System* _system;
Ppu* _ppu;
private:
uint8_t _page = 0;
uint8_t _address = 0;
uint8_t _data = 0;
private:
bool _active = false;
bool _dummy = true;
};
}
#endif //NES_DMA_H

View File

@ -6,7 +6,7 @@
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <format> #include <fmt/format.h>
namespace nes { namespace nes {
@ -19,7 +19,7 @@ namespace nes {
return; return;
} }
std::string cycleString = std::format("[{:06d}] ", cycle); std::string cycleString = fmt::format("[{:06d}] ", cycle);
std::memcpy(_data.get() + _offset, cycleString.data(), cycleString.size()); std::memcpy(_data.get() + _offset, cycleString.data(), cycleString.size());
_offset += cycleString.size(); _offset += cycleString.size();

View File

@ -8,6 +8,7 @@
#include <cstddef> #include <cstddef>
#include <memory> #include <memory>
#include <filesystem> #include <filesystem>
#include <string_view>
namespace nes { namespace nes {

View File

@ -1,80 +0,0 @@
//
// Created by Selim Mustafaev on 11.08.2023.
//
#include "Nes.h"
#include "Cartridge.h"
#include "Cpu.h"
#include <utility>
namespace nes {
Nes::Nes():
_cycles{}
#ifdef NES_LOGGING
,_logger(500*1024*1024)
#endif
{
_cpu = std::make_unique<Cpu>();
_ppu = std::make_shared<Ppu>();
}
void Nes::insertCartridge(const fs::path &path, std::optional<uint16_t> address) {
_cartridge = std::make_shared<Cartridge>(path);
_cpu->bus()->connect(_cartridge);
_cpu->bus()->connect(_ppu);
_ppu->connect(_cartridge);
reset(address);
}
void Nes::connect(std::shared_ptr<Controller> controller) {
_cpu->bus()->connect(std::move(controller));
}
void Nes::reset(std::optional<uint16_t> address) {
_cpu->reset();
_ppu->reset();
if(address) {
_cpu->setStartAddress(address.value());
}
}
void Nes::setNewFrameCallback(std::function<void(const Pixel *)> onNewFrame) {
_ppu->onNewFrame = std::move(onNewFrame);
}
void Nes::tick() {
bool needInterrupt = _ppu->tick();
#ifdef NES_LOGGING
_logger.addLine(_cycles, _ppu->state());
#endif
if(_cycles % 3 == 0) {
bool instructionExecuted = _cpu->tick();
#ifdef NES_LOGGING
if(instructionExecuted) {
_logger.addLine(_cycles, _cpu->state());
}
#endif
}
if(needInterrupt) {
_cpu->nmi();
#ifdef NES_LOGGING
_logger.addLine(_cycles, "NMI");
_logger.addLine(_cycles, _cpu->state());
#endif
}
#ifdef NES_LOGGING
if(_cycles == 3000000) {
_logger.dump("/home/selim/Documents/log.txt");
}
#endif
_cycles++;
}
}

144
src/Oam.cpp Normal file
View File

@ -0,0 +1,144 @@
//
// Created by Selim Mustafaev on 24.09.2023.
//
#include "Oam.h"
#include "Ppu.h"
#include <cstring>
namespace nes {
Oam::Oam(Ppu *ppu): _ppu{ppu} {
}
void Oam::setAddress(uint8_t address) {
_address = address;
}
uint8_t Oam::read() const {
return reinterpret_cast<const uint8_t*>(_data)[_address];
}
void Oam::write(uint8_t value) {
reinterpret_cast<uint8_t*>(_data)[_address] = value;
}
void Oam::write(uint8_t address, uint8_t value) {
reinterpret_cast<uint8_t*>(_data)[address] = value;
}
bool Oam::detectVisibleSprites(int16_t scanline, bool bigSprite) {
memset(_visibleSprites, 0xFF, MAX_SPRITES_PER_SCANLINE*sizeof(SpriteInfo));
_visibleSpriteCount = 0;
for (auto& shifter: _spriteShifters) {
shifter.reset();
}
size_t oamIndex = 0;
uint8_t spriteHeight = bigSprite ? 16 : 8;
bool spriteOverflow = false;
_spriteZeroVisible = false;
while (oamIndex < OAM_SIZE && _visibleSpriteCount < MAX_SPRITES_PER_SCANLINE + 1) {
uint8_t yStart = _data[oamIndex].y;
uint8_t yEnd = yStart + spriteHeight;
if(scanline >= yStart && scanline < yEnd) {
if(_visibleSpriteCount < MAX_SPRITES_PER_SCANLINE) {
_visibleSprites[_visibleSpriteCount] = _data[oamIndex];
_visibleSpriteCount++;
if(oamIndex == 0) {
_spriteZeroVisible = true;
}
} else {
spriteOverflow = true;
}
}
oamIndex++;
}
return spriteOverflow;
}
void Oam::loadShifters(int16_t scanline, bool bigSprite, bool patternSprite) {
for(uint8_t i = 0; i < _visibleSpriteCount; ++i) {
auto sprite = _visibleSprites[i];
bool flippedVertically = sprite.attr & 0x80;
bool flippedHorizontally = sprite.attr & 0x40;
bool isTopHalf = (scanline - sprite.y) < 8;
uint8_t tileOffset = isTopHalf ? 0 : 1;
uint8_t rowOffset = flippedVertically ? (7 - scanline + sprite.y) : (scanline - sprite.y);
uint16_t patternAddress = 0;
if(bigSprite) {
patternAddress = (sprite.id & 0x01) << 12
| ((sprite.id & 0xFE) + tileOffset) << 4
| (rowOffset & 0x07);
} else {
patternAddress = patternSprite << 12
| sprite.id << 4
| rowOffset;
}
uint8_t patternBitsLo = _ppu->internalRead(patternAddress);
uint8_t patternBitsHi = _ppu->internalRead(patternAddress + 8);
if(flippedHorizontally) {
patternBitsLo = flipByte(patternBitsLo);
patternBitsHi = flipByte(patternBitsHi);
}
_spriteShifters[i].loadHighByte(patternBitsLo, patternBitsHi);
}
}
uint8_t Oam::flipByte(uint8_t byte) const {
byte = (byte & 0xF0) >> 4 | (byte & 0x0F) << 4;
byte = (byte & 0xCC) >> 2 | (byte & 0x33) << 2;
byte = (byte & 0xAA) >> 1 | (byte & 0x55) << 1;
return byte;
}
void Oam::updateShifters() {
for (size_t i = 0; i < _visibleSpriteCount; ++i) {
if(_visibleSprites[i].x > 0) {
_visibleSprites[i].x--;
} else {
_spriteShifters[i].shift();
}
}
}
SpritePixelInfo Oam::getPixel() {
SpritePixelInfo pixel;
_firstVisibleSpriteBeingRendered = false;
for (size_t i = 0; i < _visibleSpriteCount; ++i) {
if(_visibleSprites[i].x != 0) {
continue;
}
pixel.pattern = _spriteShifters[i].getValue(0);
pixel.palette = (_visibleSprites[i].attr & 0x03) + SPRITE_PALETTE_OFFSET;
pixel.priority = (_visibleSprites[i].attr & 0x20) == 0;
if(pixel.pattern > 0) {
_firstVisibleSpriteBeingRendered = (i == 0);
break;
}
}
return pixel;
}
bool Oam::spriteZeroBeingRendered() const {
return _spriteZeroVisible && _firstVisibleSpriteBeingRendered;
}
}

66
src/Oam.h Normal file
View File

@ -0,0 +1,66 @@
//
// Created by Selim Mustafaev on 24.09.2023.
//
#ifndef NES_OAM_H
#define NES_OAM_H
#include "Shifter.h"
#include <cstdint>
#include <cstddef>
#include <tuple>
namespace nes {
class Ppu;
struct SpriteInfo {
uint8_t y = 0xFF;
uint8_t id = 0x00;
uint8_t attr = 0x00;
uint8_t x = 0xFF;
};
struct SpritePixelInfo {
uint8_t pattern = 0;
uint8_t palette = 0;
uint8_t priority = 0;
};
class Oam {
public:
static constexpr size_t OAM_SIZE = 64;
static constexpr size_t MAX_SPRITES_PER_SCANLINE = 8;
static constexpr uint8_t SPRITE_PALETTE_OFFSET = 4;
public:
explicit Oam(Ppu* ppu);
void setAddress(uint8_t address);
[[nodiscard]] uint8_t read() const;
void write(uint8_t value);
void write(uint8_t address, uint8_t value);
public:
bool detectVisibleSprites(int16_t scanline, bool bigSprite);
void loadShifters(int16_t scanline, bool bigSprite, bool patternSprite);
void updateShifters();
[[nodiscard]] SpritePixelInfo getPixel();
[[nodiscard]] bool spriteZeroBeingRendered() const;
private:
[[nodiscard]] uint8_t flipByte(uint8_t byte) const;
private:
Ppu* _ppu;
uint8_t _address = 0x00;
SpriteInfo _data[OAM_SIZE];
SpriteInfo _visibleSprites[MAX_SPRITES_PER_SCANLINE];
Shifter _spriteShifters[MAX_SPRITES_PER_SCANLINE];
uint8_t _visibleSpriteCount = 0;
bool _spriteZeroVisible = false;
bool _firstVisibleSpriteBeingRendered = false;
};
}
#endif //NES_OAM_H

View File

@ -3,81 +3,38 @@
// //
#include "Ppu.h" #include "Ppu.h"
#include <format>
#ifdef NES_LOGGING
#include <fmt/format.h>
#endif
namespace nes { namespace nes {
Ppu::Ppu(): _column{}, _scanline{}, _status{}, _control{}, _mask{} { Ppu::Ppu(): _column{}, _scanline{}, _status{}, _control{}, _mask{} {
_frameBuffer = std::make_unique<Pixel[]>(SCREEN_WIDTH*SCREEN_HEIGHT); _frameBuffer = std::make_unique<Pixel[]>(SCREEN_WIDTH*SCREEN_HEIGHT);
_nameTable = std::make_unique<uint8_t[]>(1024); _nameTable = std::make_unique<uint8_t[]>(1024);
_nameTable2 = std::make_unique<uint8_t[]>(1024);
_paletteTable = std::make_unique<uint8_t[]>(32); _paletteTable = std::make_unique<uint8_t[]>(32);
_palette = std::make_unique<Pixel[]>(64); _oam = std::make_unique<Oam>(this);
_oamData = std::make_unique<SpriteInfo[]>(64);
_palette[0x00] = Pixel(84, 84, 84); _palette = {
_palette[0x01] = Pixel(0, 30, 116); {84, 84, 84}, {0, 30, 116}, {8, 16, 144}, {48, 0, 136},
_palette[0x02] = Pixel(8, 16, 144); {68, 0, 100}, {92, 0, 48}, {84, 4, 0}, {60, 24, 0},
_palette[0x03] = Pixel(48, 0, 136); {32, 42, 0}, {8, 58, 0}, {0, 64, 0}, {0, 60, 0},
_palette[0x04] = Pixel(68, 0, 100); {0, 50, 60}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
_palette[0x05] = Pixel(92, 0, 48); {152, 150, 152}, {8, 76, 196}, {48, 50, 236}, {92, 30, 228},
_palette[0x06] = Pixel(84, 4, 0); {136, 20, 176}, {160, 20, 100}, {152, 34, 32}, {120, 60, 0},
_palette[0x07] = Pixel(60, 24, 0); {84, 90, 0}, {40, 114, 0}, {8, 124, 0}, {0, 118, 40},
_palette[0x08] = Pixel(32, 42, 0); {0, 102, 120}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
_palette[0x09] = Pixel(8, 58, 0); {236, 238, 236}, {76, 154, 236}, {120, 124, 236}, {176, 98, 236},
_palette[0x0A] = Pixel(0, 64, 0); {228, 84, 236}, {236, 88, 180}, {236, 106, 100}, {212, 136, 32},
_palette[0x0B] = Pixel(0, 60, 0); {160, 170, 0}, {116, 196, 0}, {76, 208, 32}, {56, 204, 108},
_palette[0x0C] = Pixel(0, 50, 60); {56, 180, 204}, {60, 60, 60}, {0, 0, 0}, {0, 0, 0},
_palette[0x0D] = Pixel(0, 0, 0); {236, 238, 236}, {168, 204, 236}, {188, 188, 236}, {212, 178, 236},
_palette[0x0E] = Pixel(0, 0, 0); {236, 174, 236}, {236, 174, 212}, {236, 180, 176}, {228, 196, 144},
_palette[0x0F] = Pixel(0, 0, 0); {204, 210, 120}, {180, 222, 120}, {168, 226, 144}, {152, 226, 180},
_palette[0x10] = Pixel(152, 150, 152); {160, 214, 228}, {160, 162, 160}, {0, 0, 0}, {0, 0, 0}
_palette[0x11] = Pixel(8, 76, 196); };
_palette[0x12] = Pixel(48, 50, 236);
_palette[0x13] = Pixel(92, 30, 228);
_palette[0x14] = Pixel(136, 20, 176);
_palette[0x15] = Pixel(160, 20, 100);
_palette[0x16] = Pixel(152, 34, 32);
_palette[0x17] = Pixel(120, 60, 0);
_palette[0x18] = Pixel(84, 90, 0);
_palette[0x19] = Pixel(40, 114, 0);
_palette[0x1A] = Pixel(8, 124, 0);
_palette[0x1B] = Pixel(0, 118, 40);
_palette[0x1C] = Pixel(0, 102, 120);
_palette[0x1D] = Pixel(0, 0, 0);
_palette[0x1E] = Pixel(0, 0, 0);
_palette[0x1F] = Pixel(0, 0, 0);
_palette[0x20] = Pixel(236, 238, 236);
_palette[0x21] = Pixel(76, 154, 236);
_palette[0x22] = Pixel(120, 124, 236);
_palette[0x23] = Pixel(176, 98, 236);
_palette[0x24] = Pixel(228, 84, 236);
_palette[0x25] = Pixel(236, 88, 180);
_palette[0x26] = Pixel(236, 106, 100);
_palette[0x27] = Pixel(212, 136, 32);
_palette[0x28] = Pixel(160, 170, 0);
_palette[0x29] = Pixel(116, 196, 0);
_palette[0x2A] = Pixel(76, 208, 32);
_palette[0x2B] = Pixel(56, 204, 108);
_palette[0x2C] = Pixel(56, 180, 204);
_palette[0x2D] = Pixel(60, 60, 60);
_palette[0x2E] = Pixel(0, 0, 0);
_palette[0x2F] = Pixel(0, 0, 0);
_palette[0x30] = Pixel(236, 238, 236);
_palette[0x31] = Pixel(168, 204, 236);
_palette[0x32] = Pixel(188, 188, 236);
_palette[0x33] = Pixel(212, 178, 236);
_palette[0x34] = Pixel(236, 174, 236);
_palette[0x35] = Pixel(236, 174, 212);
_palette[0x36] = Pixel(236, 180, 176);
_palette[0x37] = Pixel(228, 196, 144);
_palette[0x38] = Pixel(204, 210, 120);
_palette[0x39] = Pixel(180, 222, 120);
_palette[0x3A] = Pixel(168, 226, 144);
_palette[0x3B] = Pixel(152, 226, 180);
_palette[0x3C] = Pixel(160, 214, 228);
_palette[0x3D] = Pixel(160, 162, 160);
_palette[0x3E] = Pixel(0, 0, 0);
_palette[0x3F] = Pixel(0, 0, 0);
} }
bool Ppu::tick() { bool Ppu::tick() {
@ -94,12 +51,15 @@ namespace nes {
// All visible scanlines // All visible scanlines
if (_scanline >= -1 && _scanline < 240) { if (_scanline >= -1 && _scanline < 240) {
if(_scanline == 0 && _column == 0) { // ???
_column = 1; //if(_scanline == 0 && _column == 0) {
} // _column = 1;
//}
if (_scanline == -1 && _column == 1) { if (_scanline == -1 && _column == 1) {
_status.verticalBlank = 0; _status.verticalBlank = 0;
_status.spriteOverflow = 0;
_status.spriteZeroHit = 0;
} }
// Preloading some data ahead of time // Preloading some data ahead of time
@ -108,6 +68,9 @@ namespace nes {
_bgPatternShifter.shift(); _bgPatternShifter.shift();
_bgAttribShifter.shift(); _bgAttribShifter.shift();
} }
if(_mask.renderSprites && _column >= 2 && _column < 258) {
_oam->updateShifters();
}
prepareNextBgTile(_column); prepareNextBgTile(_column);
} }
@ -131,22 +94,62 @@ namespace nes {
} }
} }
uint8_t colorIndex = 0; if(_column == 257 && _scanline >= 0) {
uint8_t palette = 0; _status.spriteOverflow = _oam->detectVisibleSprites(_scanline, _control.spriteSize);
}
if(_column == 340) {
_oam->loadShifters(_scanline, _control.spriteSize, _control.patternSprite);
}
uint8_t bgPattern = 0;
uint8_t bgPalette = 0;
if(_mask.renderBackground) { if(_mask.renderBackground) {
colorIndex = _bgPatternShifter.getValue(_fineX); bgPattern = _bgPatternShifter.getValue(_fineX);
palette = _bgAttribShifter.getValue(_fineX); bgPalette = _bgAttribShifter.getValue(_fineX);
Pixel pixel = getColor(palette, colorIndex); }
_curPalette = palette; uint8_t fgPattern = 0;
_curColorIndex = colorIndex; uint8_t fgPalette = 0;
uint8_t priority = 0;
if(_column < SCREEN_WIDTH && _scanline < SCREEN_HEIGHT && _scanline >= 0) { if(_mask.renderSprites) {
setPixel(_scanline, _column, pixel); auto fgPixel = _oam->getPixel();
fgPattern = fgPixel.pattern;
fgPalette = fgPixel.palette;
priority = fgPixel.priority;
}
uint8_t pattern = 0;
uint8_t palette = 0;
if(bgPattern == 0 && fgPattern > 0) {
pattern = fgPattern;
palette = fgPalette;
}
else if(bgPattern > 0 && fgPattern == 0) {
pattern = bgPattern;
palette = bgPalette;
}
else if(bgPattern > 0 && fgPattern > 0) {
pattern = priority ? fgPattern : bgPattern;
palette = priority ? fgPalette : bgPalette;
if(_mask.renderBackground && _mask.renderSprites && _oam->spriteZeroBeingRendered()) {
bool renderLeft = _mask.renderBackgroundLeft && _mask.renderSpritesLeft;
int16_t firstRow = renderLeft ? 0 : 8;
if(_column > firstRow && _column < 258) {
_status.spriteZeroHit = true;
}
} }
} }
if(_column < SCREEN_WIDTH && _scanline < SCREEN_HEIGHT && _scanline >= 0) {
Pixel pixel = getColor(palette, pattern);
setPixel(_scanline, _column, pixel);
}
_column++; _column++;
if(_column >= 341) { if(_column >= 341) {
_column = 0; _column = 0;
@ -178,7 +181,7 @@ namespace nes {
_vRamAddress.value += _control.incrementMode ? 32 : 1; _vRamAddress.value += _control.incrementMode ? 32 : 1;
break; break;
case OamData: case OamData:
value = reinterpret_cast<uint8_t*>(_oamData.get())[_oamAddress]; value = _oam->read();
break; break;
} }
return value; return value;
@ -219,10 +222,10 @@ namespace nes {
_vRamAddress.value += _control.incrementMode ? 32 : 1; _vRamAddress.value += _control.incrementMode ? 32 : 1;
break; break;
case OamAddress: case OamAddress:
_oamAddress = value; _oam->setAddress(value);
break; break;
case OamData: case OamData:
reinterpret_cast<uint8_t*>(_oamData.get())[_oamAddress] = value; _oam->write(value);
break; break;
default: default:
break; break;
@ -234,8 +237,8 @@ namespace nes {
_frameBuffer[index] = pixel; _frameBuffer[index] = pixel;
} }
void Ppu::connect(std::shared_ptr<Cartridge> cartridge) { void Ppu::connect(Cartridge* cartridge) {
_cartridge = std::move(cartridge); _cartridge = cartridge;
} }
void Ppu::reset() { void Ppu::reset() {
@ -262,7 +265,25 @@ namespace nes {
} }
else if(address >= 0x2000 && address < 0x3F00) { else if(address >= 0x2000 && address < 0x3F00) {
address &= 0x0FFF; address &= 0x0FFF;
return _nameTable[address & 0x03FF]; if(_cartridge->mirroring() == Cartridge::Mirroring::Vertical) {
if (address >= 0x0000 && address <= 0x03FF)
return _nameTable[address & 0x03FF];
if (address >= 0x0400 && address <= 0x07FF)
return _nameTable2[address & 0x03FF];
if (address >= 0x0800 && address <= 0x0BFF)
return _nameTable[address & 0x03FF];
if (address >= 0x0C00 && address <= 0x0FFF)
return _nameTable2[address & 0x03FF];
} else {
if (address >= 0x0000 && address <= 0x03FF)
return _nameTable[address & 0x03FF];
if (address >= 0x0400 && address <= 0x07FF)
return _nameTable[address & 0x03FF];
if (address >= 0x0800 && address <= 0x0BFF)
return _nameTable2[address & 0x03FF];
if (address >= 0x0C00 && address <= 0x0FFF)
return _nameTable2[address & 0x03FF];
}
} }
else if(address >= 0x3F00 && address < 0x4000) { else if(address >= 0x3F00 && address < 0x4000) {
address &= 0x1F; address &= 0x1F;
@ -284,7 +305,25 @@ namespace nes {
} }
else if(address >= 0x2000 && address < 0x3F00) { else if(address >= 0x2000 && address < 0x3F00) {
address &= 0x0FFF; address &= 0x0FFF;
_nameTable[address & 0x03FF] = value; if(_cartridge->mirroring() == Cartridge::Mirroring::Vertical) {
if (address >= 0x0000 && address <= 0x03FF)
_nameTable[address & 0x03FF] = value;
if (address >= 0x0400 && address <= 0x07FF)
_nameTable2[address & 0x03FF] = value;
if (address >= 0x0800 && address <= 0x0BFF)
_nameTable[address & 0x03FF] = value;
if (address >= 0x0C00 && address <= 0x0FFF)
_nameTable2[address & 0x03FF] = value;
} else {
if (address >= 0x0000 && address <= 0x03FF)
_nameTable[address & 0x03FF] = value;
if (address >= 0x0400 && address <= 0x07FF)
_nameTable[address & 0x03FF] = value;
if (address >= 0x0800 && address <= 0x0BFF)
_nameTable2[address & 0x03FF] = value;
if (address >= 0x0C00 && address <= 0x0FFF)
_nameTable2[address & 0x03FF] = value;
}
} }
else if(address >= 0x3F00 && address < 0x4000) { else if(address >= 0x3F00 && address < 0x4000) {
address &= 0x1F; address &= 0x1F;
@ -390,11 +429,12 @@ namespace nes {
} }
} }
#ifdef NES_LOGGING
std::string Ppu::state() const { std::string Ppu::state() const {
auto palettes = (uint32_t*)_paletteTable.get(); auto palettes = (uint32_t*)_paletteTable.get();
return std::format("X:{:03d}, Y:{:03d}, V:{:d}, vR: [CX:{:02X}, CY:{:02X}, NX:{:d}, NY:{:d}, FY:{:d}], tR: [CX:{:02X}, CY:{:02X}, NX:{:d}, NY:{:d}, FY:{:d}], FX:{:d}, AL:{:d}, PI:{:02X}, CI:{:02X}, ASL:{:04X}, ASH:{:04X}", return fmt::format("X:{:03d}, Y:{:03d}, V:{:d}, vR: [CX:{:02X}, CY:{:02X}, NX:{:d}, NY:{:d}, FY:{:d}], tR: [CX:{:02X}, CY:{:02X}, NX:{:d}, NY:{:d}, FY:{:d}], FX:{:d}, AL:{:d}, ASL:{:04X}, ASH:{:04X}",
_column, _column,
_scanline, _scanline,
_status.verticalBlank, _status.verticalBlank,
@ -410,9 +450,12 @@ namespace nes {
_tRamAddress.fineY, _tRamAddress.fineY,
_fineX, _fineX,
_addressWriteInProgress, _addressWriteInProgress,
_curPalette,
_curColorIndex,
_bgAttribShifter._lo, _bgAttribShifter._lo,
_bgAttribShifter._hi); _bgAttribShifter._hi);
} }
#endif
void Ppu::writeOam(uint8_t address, uint8_t data) {
_oam->write(address, data);
}
} }

View File

@ -7,10 +7,12 @@
#include "Cartridge.h" #include "Cartridge.h"
#include "Shifter.h" #include "Shifter.h"
#include "Oam.h"
#include <cstdint> #include <cstdint>
#include <memory> #include <memory>
#include <functional> #include <functional>
#include <vector>
namespace nes { namespace nes {
@ -97,29 +99,26 @@ namespace nes {
uint8_t msb; uint8_t msb;
}; };
struct SpriteInfo {
uint8_t y;
uint8_t id;
uint8_t attr;
uint8_t x;
};
public: public:
Ppu(); Ppu();
bool tick(); bool tick();
void write(uint16_t address, uint8_t value); void write(uint16_t address, uint8_t value);
uint8_t read(uint16_t address); uint8_t read(uint16_t address);
void setPixel(uint16_t row, uint16_t column, Pixel pixel); void setPixel(uint16_t row, uint16_t column, Pixel pixel);
void connect(std::shared_ptr<Cartridge> cartridge); void connect(Cartridge* cartridge);
void reset(); void reset();
std::string state() const; void writeOam(uint8_t address, uint8_t data);
uint8_t internalRead(uint16_t address);
void internalWrite(uint16_t address, uint8_t value);
#ifdef NES_LOGGING
[[nodiscard]] std::string state() const;
#endif
public: public:
std::function<void(const Pixel*)> onNewFrame; std::function<void(const Pixel*)> onNewFrame;
private: private:
uint8_t internalRead(uint16_t address);
void internalWrite(uint16_t address, uint8_t value);
Pixel getColor(uint8_t palette, uint8_t pixel); Pixel getColor(uint8_t palette, uint8_t pixel);
void prepareNextBgTile(uint16_t column); void prepareNextBgTile(uint16_t column);
void incrementScrollX(); void incrementScrollX();
@ -146,19 +145,14 @@ namespace nes {
private: private:
std::unique_ptr<uint8_t[]> _nameTable; std::unique_ptr<uint8_t[]> _nameTable;
std::unique_ptr<Pixel[]> _palette; std::unique_ptr<uint8_t[]> _nameTable2;
std::vector<Pixel> _palette;
std::unique_ptr<uint8_t[]> _paletteTable; std::unique_ptr<uint8_t[]> _paletteTable;
std::unique_ptr<Oam> _oam;
std::unique_ptr<SpriteInfo[]> _oamData;
uint8_t _oamAddress;
private: private:
std::unique_ptr<Pixel[]> _frameBuffer; std::unique_ptr<Pixel[]> _frameBuffer;
std::shared_ptr<Cartridge> _cartridge; Cartridge* _cartridge;
private: // For debug
uint8_t _curPalette;
uint8_t _curColorIndex;
}; };
} }

View File

@ -14,15 +14,25 @@ namespace nes {
_hi = (_hi & 0xFF00) | hi; _hi = (_hi & 0xFF00) | hi;
} }
void Shifter::loadHighByte(uint8_t lo, uint8_t hi) {
_lo = lo << 8;
_hi = hi << 8;
}
void Shifter::shift() { void Shifter::shift() {
_lo <<= 1; _lo <<= 1;
_hi <<= 1; _hi <<= 1;
} }
uint16_t Shifter::getValue(uint8_t offset) { uint8_t Shifter::getValue(uint8_t offset) const {
uint16_t mask = 0x8000 >> offset; uint16_t mask = 0x8000 >> offset;
bool loBit = _lo & mask; bool loBit = _lo & mask;
bool hiBit = _hi & mask; bool hiBit = _hi & mask;
return (hiBit << 1) | loBit; return (hiBit << 1) | loBit;
} }
void Shifter::reset() {
_lo = 0;
_hi = 0;
}
} }

View File

@ -13,8 +13,10 @@ namespace nes {
public: public:
Shifter(); Shifter();
void load(uint8_t lo, uint8_t hi); void load(uint8_t lo, uint8_t hi);
void loadHighByte(uint8_t lo, uint8_t hi);
void shift(); void shift();
uint16_t getValue(uint8_t offset); uint8_t getValue(uint8_t offset) const;
void reset();
public: public:
uint16_t _lo; uint16_t _lo;

140
src/System.cpp Normal file
View File

@ -0,0 +1,140 @@
//
// Created by Selim Mustafaev on 11.08.2023.
//
#include "System.h"
#include "Cartridge.h"
#include "Cpu.h"
#include "Ppu.h"
#include "Controller.h"
#include <utility>
#include <iostream>
namespace nes {
System::System():
_cycles{}
#ifdef NES_LOGGING
,_logger(500*1024*1024)
#endif
{
_ram = std::make_unique<uint8_t[]>(2*1024);
_cpu = std::make_unique<Cpu>(this);
_ppu = std::make_unique<Ppu>();
_dma = std::make_unique<Dma>(this, _ppu.get());
_controller1 = std::make_shared<Controller>();
}
void System::insertCartridge(const fs::path &path, std::optional<uint16_t> address) {
_cartridge = std::make_unique<Cartridge>(path);
_ppu->connect(_cartridge.get());
reset(address);
}
void System::connect(std::shared_ptr<Controller> controller) {
_controller1 = std::move(controller);
}
void System::reset(std::optional<uint16_t> address) {
_cpu->reset();
_ppu->reset();
if(address) {
_cpu->setStartAddress(address.value());
}
}
void System::setNewFrameCallback(std::function<void(const Pixel *)> onNewFrame) {
_ppu->onNewFrame = std::move(onNewFrame);
}
void System::tick() {
bool needInterrupt = _ppu->tick();
#ifdef NES_LOGGING
_logger.addLine(_cycles, _ppu->state());
#endif
if(_cycles % 3 == 0) {
if(_dma->active()) {
_dma->tick(_cycles);
} else {
bool instructionExecuted = _cpu->tick();
#ifdef NES_LOGGING
if(instructionExecuted) {
_logger.addLine(_cycles, _cpu->state());
}
#endif
}
}
if(needInterrupt) {
_cpu->nmi();
#ifdef NES_LOGGING
_logger.addLine(_cycles, "NMI");
_logger.addLine(_cycles, _cpu->state());
#endif
}
#ifdef NES_LOGGING
if(_cycles == 3000000) {
_logger.dump("/home/selim/Documents/log.txt");
}
#endif
_cycles++;
}
uint8_t System::read(uint16_t address) {
if(address < 0x2000) {
return _ram[address & 0x07FF];
}
else if(address >= 0x2000 && address < 0x4000) {
return _ppu->read(address & 0x0007);
}
else if(address >= 0x8000) {
return _cartridge->readPrg(address);
}
else if(address == 0x4016) {
return _controller1->read();
}
else if(address == 0x4017) {
// Controller 2
return 0;
}
return 0;
}
void System::write(uint16_t address, uint8_t value) {
if(address < 0x2000) {
_ram[address & 0x07FF] = value;
}
else if(address >= 0x2000 && address < 0x4000) {
_ppu->write(address & 0x0007, value);
}
else if(address >= 0x8000) {
std::cout << "Cartridge write at address: " << address << std::endl;
}
else if(address == 0x4014) {
_dma->start(value);
}
else if(address == 0x4016) {
_controller1->poll();
}
else if(address == 0x4017) {
// Controller 2
}
}
// For debug
// Calc "hash" - just sum of the bytes of RAM
// Can be useful for detecting changes in memory
uint32_t System::zpHash() const {
uint32_t sum = 0;
for(size_t i = 0; i < 2048; ++i) {
sum += _ram[i];
}
return sum;
}
}

View File

@ -2,35 +2,46 @@
// Created by Selim Mustafaev on 11.08.2023. // Created by Selim Mustafaev on 11.08.2023.
// //
#ifndef NES_NES_H #ifndef NES_SYSTEM_H
#define NES_NES_H #define NES_SYSTEM_H
#include "Cpu.h"
#include "Cartridge.h"
#include "Ppu.h"
#include "Logger.h" #include "Logger.h"
#include "Cpu.h"
#include "Ppu.h"
#include "Cartridge.h"
#include "Dma.h"
#include <filesystem> #include <filesystem>
#include <optional> #include <optional>
#include <functional>
namespace nes { namespace nes {
class Controller;
namespace fs = std::filesystem; namespace fs = std::filesystem;
class Nes { class System {
public: public:
Nes(); System();
void insertCartridge(const fs::path& path, std::optional<uint16_t> address = std::nullopt); void insertCartridge(const fs::path& path, std::optional<uint16_t> address = std::nullopt);
void connect(std::shared_ptr<Controller> controller); void connect(std::shared_ptr<Controller> controller);
void reset(std::optional<uint16_t> address = std::nullopt); void reset(std::optional<uint16_t> address = std::nullopt);
void setNewFrameCallback(std::function<void(const Pixel*)> onNewFrame); void setNewFrameCallback(std::function<void(const Pixel*)> onNewFrame);
void tick(); void tick();
uint8_t read(uint16_t address);
void write(uint16_t address, uint8_t value);
[[nodiscard]] uint32_t zpHash() const;
private: private:
uint64_t _cycles; uint64_t _cycles;
std::unique_ptr<uint8_t[]> _ram;
std::unique_ptr<Cpu> _cpu; std::unique_ptr<Cpu> _cpu;
std::shared_ptr<Ppu> _ppu; std::unique_ptr<Ppu> _ppu;
std::shared_ptr<Cartridge> _cartridge; std::unique_ptr<Cartridge> _cartridge;
std::shared_ptr<Controller> _controller1;
std::unique_ptr<Dma> _dma;
#ifdef NES_LOGGING #ifdef NES_LOGGING
Logger _logger; Logger _logger;
@ -39,4 +50,4 @@ namespace nes {
} }
#endif //NES_NES_H #endif //NES_SYSTEM_H

15
src/include/NesKitCpp.h Normal file
View File

@ -0,0 +1,15 @@
//
// Header.h
//
//
// Created by Selim Mustafaev on 27.09.2023.
//
// This is umbrella header for SPM module
//
#ifndef NesKitCpp_h
#define NesKitCpp_h
#include "../System.h"
#endif /* Header_h */