This project simulates a small part of the MOS6502 CPU functionality. Below is an explanation of a specific sequence:
-
Initialization:
- The number of CPU cycles is set.
CPU
,Memory
, andStack
objects are initialized.- The CPU is then reset to its initial state.
-
Fetch and Execute Cycle:
- The CPU fetches the first instruction, which is a
Jump to Subroutine (JSR)
. - Two bytes (representing the target address) are fetched from memory and pushed onto the stack.
- The Program Counter (PC) is decremented by one to simulate the instruction cycle correctly.
- The CPU fetches the first instruction, which is a
-
Custom Instruction Simulation:
- The CPU fetches a value from memory to simulate an in-house function:
INS_LD_SV_TEMP = 0x54
. - This function reads one byte from a specified memory location and stores it as a temporary variable in Zero Page at
tempIndex = 0xC8
.
- The CPU fetches a value from memory to simulate an in-house function:
-
Generic Address Handling:
- Due to differences in memory address sizes, the simulation functions are designed to handle both one-byte and two-byte memory addresses dynamically.
This simulation provides an example of how the MOS6502 operates, with a focus on the JSR
instruction and temporary variable handling using Zero Page memory.
This project implements a MOS6502 CPU Emulator, complete with memory and logging utilities, and is set up for unit testing using Google Test (gtest) in a C++20 environment within Microsoft Visual Studio.
The MOS6502 CPU Emulator simulates the behavior of the MOS6502 microprocessor, along with components like:
- CPU operations: Implements instructions like
INS_JSR
(Jump to Subroutine) andINS_LD_ACC_IMMID
(Load Accumulator Immediate). - Memory management: Memory simulation for addressable space.
- Testing: Uses Google Test for unit tests to validate CPU behavior and memory interactions.
- Logging: Provides insights into memory state and execution flow.
This documentation covers setting up the project, linking Google Test, and running the emulator and its tests in a C++20 environment using MS Visual Studio.
- Microsoft Visual Studio:
- Version: 2022 or newer.
- C++ development environment installed.
- Google Test (gtest):
- Built with C++20 and configured for Dynamic Debug Runtime (/MDd).
-
Open your Visual Studio project and go to Project Properties.
-
Add the following directories for
gtest
:- Include Directories:
C:\***\googletest\googletest\include
- Library Directories:
C:\***\googletest\build\lib\Debug
- Include Directories:
-
Go to Configuration Properties > Linker > Input > Additional Dependencies and add: gtest.lib gtest_main.lib
-
Ensure the runtime library is consistent:
- C++ Runtime Library: Set to Multi-threaded Debug DLL (/MDd).
- Match this for both your project and the
gtest.lib
.
- Open Project Properties.
- Go to Configuration Properties > C/C++ > Language.
- Set C++ Language Standard to: ISO C++20 (/std:c++20)
- Ensure you’re working in the Debug configuration (top of Visual Studio).
- Clean and rebuild the project.
The main.cpp
file initializes Google Test, runs the MOS6502 emulator simulation, and logs memory operations.
Memory The Memory class provides addressable storage for the CPU.
Stack Stack class designed to handle operations related to the CPU’s stack pointer (SP).
Implements core MOS6502 instructions like:
INS_JSR (Jump to Subroutine) INS_LD_ACC_IMMID (Load Accumulator Immediate)
The LOGGER.h utility logs: Memory addresses Execution flow
Google Test Integration Google Test is used to validate:
CPU instruction execution. Memory read/write operations. Logging correctness. To run the tests:
Set up the environment as described above. Build and run the project. Test results will appear in the Output window of Visual Studio.
graphql
Copy code
MOS6502/
├── CPU.h CPU Emulator Header
├── Memory.h Memory Simulation Header
├── Stack.h Stack Simulation Header
├── LOGGER.h Logging Utilities
├── main.cpp Main Emulator and Test Runner
├── tests/ Unit Tests for CPU and Memory
└── CMakeLists.txt Optional CMake support (if needed)
This project is licensed under the GNU License.