Skip to content

Commit

Permalink
Merge pull request #16 from Kautenja/memory_access
Browse files Browse the repository at this point in the history
Memory access
  • Loading branch information
Kautenja authored Jul 19, 2018
2 parents af4f758 + 0e6c490 commit 9e2ba99
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 5 deletions.
35 changes: 31 additions & 4 deletions nes_py/laines/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,44 @@
/// The CPU (MOS6502) for the NES
namespace CPU {

/* CPU state */
/// The RAM module for the CPU
u8 ram[0x800];

/**
Return the value of the given memory address.
This is meant as a public getter to the memory of the machine for RAM hacks.
@param address the 16-bit address to read from memory
@returns the byte located at the given address
*/
u8 read_mem(u16 address) { return ram[address % 0x800]; }

/**
Return the value of the given memory address.
This is meant as a public getter to the memory of the machine for RAM hacks.
@param address the 16-bit address to read from memory
@param value the 8-bit value to write to the given memory address
*/
void write_mem(u16 address, u8 value) { ram[address % 0x800] = value; }

/// accumulator, index x, index y, and the stack pointer
u8 A, X, Y, S;
/// the program counter for the machine instructions
u16 PC;
/// the flags register
Flags P;
// non-mask-able interrupt and interrupt request flag
/// non-mask-able interrupt and interrupt request flag
bool nmi, irq;

// Remaining clocks to end frame
// Original value is 29781. New value over-clocks the CPU (500000 is fast)
/**
The total number of CPU cycle per emulated frame.
Original value is 29781. New value over-clocks the CPU (500000 is fast)
*/
const int TOTAL_CYCLES = 29781;
/// Remaining clocks to end frame
int remainingCycles;

/* Cycle emulation */
Expand Down
2 changes: 2 additions & 0 deletions nes_py/laines/include/cpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ namespace CPU {

};

u8 read_mem(u16 address);
void write_mem(u16 address, u8 value);
void set_nmi(bool v = true);
void set_irq(bool v = true);
void power();
Expand Down
8 changes: 8 additions & 0 deletions nes_py/laines/nes_env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ extern "C" {
/// The height of the NES screen.
unsigned NESEnv_height() { return GUI::get_height(); }

/// The getter for RAM access
u8 NESEnv_read_mem(u16 address) { return CPU::read_mem(address); }

/// The setter for RAM access
void NESEnv_write_mem(u16 address, u8 value) {
CPU::write_mem(address, value);
}

/// Copy the screen of the emulator to an output buffer (NumPy array)
void NESEnv_screen(unsigned char *output_buffer) {
GUI::copy_screen(output_buffer);
Expand Down
33 changes: 33 additions & 0 deletions nes_py/nes_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
# setup the argument and return types for NESEnv_height
_LIB.NESEnv_height.argtypes = None
_LIB.NESEnv_height.restype = ctypes.c_uint
# setup the argument and return types for NESEnv_read_mem
_LIB.NESEnv_read_mem.argtypes = [ctypes.c_ushort]
_LIB.NESEnv_read_mem.restype = ctypes.c_ubyte
# setup the argument and return types for NESEnv_write_mem
_LIB.NESEnv_write_mem.argtypes = [ctypes.c_ushort, ctypes.c_ubyte]
_LIB.NESEnv_write_mem.restype = None
# setup the argument and return types for NESEnv_screen
_LIB.NESEnv_screen.argtypes = [ctypes.c_void_p]
_LIB.NESEnv_screen.restype = None
Expand Down Expand Up @@ -134,6 +140,33 @@ def _copy_screen(self):
# remove the 0th axis (padding from storing colors in 32 bit)
self.screen = self.screen[:, :, 1:]

def _read_mem(self, address):
"""
Read a byte from the given memory address.
Args:
address (int): the 16-bit address to read from
Returns:
(int) the 8-bit value at the given memory address
"""
return _LIB.NESEnv_read_mem(address)

def _write_mem(self, address, value):
"""
Write a byte to the given memory address.
Args:
address (int): the 16-bit address to write to
value (int): the 8-bit value to write to memory
Returns:
None
"""
_LIB.NESEnv_write_mem(address, value)

@property
def _reward(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def README():

setup(
name='nes_py',
version='0.2.8',
version='0.3.0',
description='An NES Emulator and OpenAI Gym interface',
long_description=README(),
long_description_content_type='text/markdown',
Expand Down

0 comments on commit 9e2ba99

Please sign in to comment.