Skip to content

Commit cc9e9ca

Browse files
committed
pointer instead of reference
1 parent 934c8f8 commit cc9e9ca

13 files changed

+80
-65
lines changed

backup_restore.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from nes_py import NESEnv
2+
import tqdm
3+
env = NESEnv('./nes_py/tests/games/super-mario-bros-1.nes')
4+
5+
done = True
6+
7+
try:
8+
for i in tqdm.tqdm(range(5000)):
9+
if done:
10+
state = env.reset()
11+
done = False
12+
else:
13+
state, reward, done, info = env.step(env.action_space.sample())
14+
if (i + 1) % 12:
15+
env._backup()
16+
if (i + 1) % 27:
17+
env._restore()
18+
except KeyboardInterrupt:
19+
pass

nes_py/nes/include/emulator.hpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Emulator {
5858
///
5959
/// @param rom_path the path to the ROM for the emulator to run
6060
///
61-
Emulator(std::string rom_path);
61+
explicit Emulator(std::string rom_path);
6262

6363
/// Return a 32-bit pointer to the screen buffer's first address.
6464
///
@@ -88,10 +88,20 @@ class Emulator {
8888
void step();
8989

9090
/// Create a backup state on the emulator.
91-
void backup();
91+
inline void backup() {
92+
backup_bus = bus;
93+
backup_picture_bus = picture_bus;
94+
backup_cpu = cpu;
95+
backup_ppu = ppu;
96+
}
9297

9398
/// Restore the backup state on the emulator.
94-
void restore();
99+
inline void restore() {
100+
bus = backup_bus;
101+
picture_bus = backup_picture_bus;
102+
cpu = backup_cpu;
103+
ppu = backup_ppu;
104+
}
95105
};
96106

97107
} // namespace NES

nes_py/nes/include/mapper.hpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ enum NameTableMirroring {
2727
class Mapper {
2828
protected:
2929
/// The cartridge this mapper associates with
30-
Cartridge& cartridge;
30+
Cartridge* cartridge;
3131

3232
public:
3333
/// an enumeration of mapper IDs
@@ -42,15 +42,15 @@ class Mapper {
4242
///
4343
/// @param game a reference to a cartridge for the mapper to access
4444
///
45-
Mapper(Cartridge& game) : cartridge(game) { }
45+
Mapper(Cartridge* game) : cartridge(game) { }
4646

4747
/// Create a mapper based on given type, a game cartridge.
4848
///
4949
/// @param game a reference to a cartridge for the mapper to access
5050
/// @param callback the callback to signify a change in mirroring mode
5151
/// @return a pointer to a mapper class based on the given game
5252
///
53-
static Mapper* create(Cartridge& game, std::function<void(void)> callback);
53+
static Mapper* create(Cartridge* game, std::function<void(void)> callback);
5454

5555
/// Read a byte from the PRG RAM.
5656
///
@@ -89,11 +89,11 @@ class Mapper {
8989

9090
/// Return the name table mirroring mode of this mapper.
9191
inline virtual NameTableMirroring getNameTableMirroring() {
92-
return static_cast<NameTableMirroring>(cartridge.getNameTableMirroring());
92+
return static_cast<NameTableMirroring>(cartridge->getNameTableMirroring());
9393
}
9494

9595
/// Return true if this mapper has extended RAM, false otherwise.
96-
inline bool hasExtendedRAM() { return cartridge.hasExtendedRAM(); }
96+
inline bool hasExtendedRAM() { return cartridge->hasExtendedRAM(); }
9797
};
9898

9999
} // namespace NES

nes_py/nes/include/mappers/mapper_CNROM.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class MapperCNROM : public Mapper {
2525
///
2626
/// @param cart a reference to a cartridge for the mapper to access
2727
///
28-
MapperCNROM(Cartridge& cart);
28+
explicit MapperCNROM(Cartridge* cart);
2929

3030
/// Read a byte from the PRG RAM.
3131
///
@@ -49,7 +49,7 @@ class MapperCNROM : public Mapper {
4949
/// @return the byte located at the given address in CHR RAM
5050
///
5151
inline NES_Byte readCHR(NES_Address address) {
52-
return cartridge.getVROM()[address | (select_chr << 13)];
52+
return cartridge->getVROM()[address | (select_chr << 13)];
5353
}
5454

5555
/// Write a byte to an address in the CHR RAM.

nes_py/nes/include/mappers/mapper_NROM.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class MapperNROM : public Mapper {
2828
///
2929
/// @param cart a reference to a cartridge for the mapper to access
3030
///
31-
MapperNROM(Cartridge& cart);
31+
MapperNROM(Cartridge* cart);
3232

3333
/// Read a byte from the PRG RAM.
3434
///

nes_py/nes/include/mappers/mapper_SxROM.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class MapperSxROM : public Mapper {
5656
/// @param cart a reference to a cartridge for the mapper to access
5757
/// @param mirroring_cb the callback to change mirroring modes on the PPU
5858
///
59-
MapperSxROM(Cartridge& cart, std::function<void(void)> mirroring_cb);
59+
MapperSxROM(Cartridge* cart, std::function<void(void)> mirroring_cb);
6060

6161
/// Read a byte from the PRG RAM.
6262
///

nes_py/nes/include/mappers/mapper_UxROM.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class MapperUxROM : public Mapper {
3030
///
3131
/// @param cart a reference to a cartridge for the mapper to access
3232
///
33-
MapperUxROM(Cartridge& cart);
33+
MapperUxROM(Cartridge* cart);
3434

3535
/// Read a byte from the PRG RAM.
3636
///

nes_py/nes/src/emulator.cpp

+1-15
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Emulator::Emulator(std::string rom_path) {
3232
// load the ROM from disk, expect that the Python code has validated it
3333
cartridge.loadFromFile(rom_path);
3434
// create the mapper based on the mapper ID in the iNES header of the ROM
35-
Mapper* mapper(Mapper::create(cartridge, [&](){ picture_bus.update_mirroring(); }));
35+
Mapper* mapper(Mapper::create(&cartridge, [&](){ picture_bus.update_mirroring(); }));
3636
// give the IO buses a pointer to the mapper
3737
bus.set_mapper(mapper);
3838
picture_bus.set_mapper(mapper);
@@ -49,18 +49,4 @@ void Emulator::step() {
4949
}
5050
}
5151

52-
void Emulator::backup() {
53-
backup_bus = bus;
54-
backup_picture_bus = picture_bus;
55-
backup_cpu = cpu;
56-
backup_ppu = ppu;
57-
}
58-
59-
void Emulator::restore() {
60-
bus = backup_bus;
61-
picture_bus = backup_picture_bus;
62-
cpu = backup_cpu;
63-
ppu = backup_ppu;
64-
}
65-
6652
} // namespace NES

nes_py/nes/src/mapper.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
namespace NES {
1515

16-
Mapper* Mapper::create(Cartridge& game, std::function<void(void)> callback) {
17-
switch (static_cast<Mapper::Type>(game.getMapper())) {
16+
Mapper* Mapper::create(Cartridge* game, std::function<void(void)> callback) {
17+
switch (static_cast<Mapper::Type>(game->getMapper())) {
1818
case NROM:
1919
return new MapperNROM(game);
2020
case SxROM:

nes_py/nes/src/mappers/mapper_CNROM.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@
1010

1111
namespace NES {
1212

13-
MapperCNROM::MapperCNROM(Cartridge& cart) : Mapper(cart), select_chr(0) {
14-
is_one_bank = cart.getROM().size() == 0x4000;
13+
MapperCNROM::MapperCNROM(Cartridge* cart) : Mapper(cart), select_chr(0) {
14+
is_one_bank = cart->getROM().size() == 0x4000;
1515
}
1616

1717
NES_Byte MapperCNROM::readPRG(NES_Address address) {
1818
if (!is_one_bank)
19-
return cartridge.getROM()[address - 0x8000];
19+
return cartridge->getROM()[address - 0x8000];
2020
// mirrored
2121
else
22-
return cartridge.getROM()[(address - 0x8000) & 0x3fff];
22+
return cartridge->getROM()[(address - 0x8000) & 0x3fff];
2323
}
2424

2525
const NES_Byte* MapperCNROM::getPagePtr(NES_Address address) {
2626
if (!is_one_bank)
27-
return &cartridge.getROM()[address - 0x8000];
27+
return &cartridge->getROM()[address - 0x8000];
2828
// mirrored
2929
else
30-
return &cartridge.getROM()[(address - 0x8000) & 0x3fff];
30+
return &cartridge->getROM()[(address - 0x8000) & 0x3fff];
3131
}
3232

3333
void MapperCNROM::writeCHR(NES_Address address, NES_Byte value) {

nes_py/nes/src/mappers/mapper_NROM.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
namespace NES {
1212

13-
MapperNROM::MapperNROM(Cartridge &cart) : Mapper(cart) {
14-
if (cart.getROM().size() == 0x4000) // 1 bank
13+
MapperNROM::MapperNROM(Cartridge* cart) : Mapper(cart) {
14+
if (cart->getROM().size() == 0x4000) // 1 bank
1515
is_one_bank = true;
1616
else // 2 banks
1717
is_one_bank = false;
1818

19-
if (cart.getVROM().size() == 0) {
19+
if (cart->getVROM().size() == 0) {
2020
has_character_ram = true;
2121
character_ram.resize(0x2000);
2222
LOG(Info) << "Uses character RAM" << std::endl;
@@ -27,9 +27,9 @@ MapperNROM::MapperNROM(Cartridge &cart) : Mapper(cart) {
2727

2828
NES_Byte MapperNROM::readPRG(NES_Address address) {
2929
if (!is_one_bank)
30-
return cartridge.getROM()[address - 0x8000];
30+
return cartridge->getROM()[address - 0x8000];
3131
else // mirrored
32-
return cartridge.getROM()[(address - 0x8000) & 0x3fff];
32+
return cartridge->getROM()[(address - 0x8000) & 0x3fff];
3333
}
3434

3535
void MapperNROM::writePRG(NES_Address address, NES_Byte value) {
@@ -45,7 +45,7 @@ NES_Byte MapperNROM::readCHR(NES_Address address) {
4545
if (has_character_ram)
4646
return character_ram[address];
4747
else
48-
return cartridge.getVROM()[address];
48+
return cartridge->getVROM()[address];
4949
}
5050

5151
void MapperNROM::writeCHR(NES_Address address, NES_Byte value) {
@@ -61,9 +61,9 @@ void MapperNROM::writeCHR(NES_Address address, NES_Byte value) {
6161

6262
const NES_Byte* MapperNROM::getPagePtr(NES_Address address) {
6363
if (!is_one_bank)
64-
return &cartridge.getROM()[address - 0x8000];
64+
return &cartridge->getROM()[address - 0x8000];
6565
else // mirrored
66-
return &cartridge.getROM()[(address - 0x8000) & 0x3fff];
66+
return &cartridge->getROM()[(address - 0x8000) & 0x3fff];
6767
}
6868

6969
} // namespace NES

nes_py/nes/src/mappers/mapper_SxROM.cpp

+15-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace NES {
1212

13-
MapperSxROM::MapperSxROM(Cartridge &cart, std::function<void(void)> mirroring_cb) :
13+
MapperSxROM::MapperSxROM(Cartridge* cart, std::function<void(void)> mirroring_cb) :
1414
Mapper(cart),
1515
mirroring_callback(mirroring_cb),
1616
mirroing(HORIZONTAL),
@@ -25,20 +25,20 @@ MapperSxROM::MapperSxROM(Cartridge &cart, std::function<void(void)> mirroring_cb
2525
second_bank_prg(nullptr),
2626
first_bank_chr(nullptr),
2727
second_bank_chr(nullptr) {
28-
if (cart.getVROM().size() == 0) {
28+
if (cart->getVROM().size() == 0) {
2929
has_character_ram = true;
3030
character_ram.resize(0x2000);
3131
LOG(Info) << "Uses character RAM" << std::endl;
3232
} else {
3333
LOG(Info) << "Using CHR-ROM" << std::endl;
3434
has_character_ram = false;
35-
first_bank_chr = &cart.getVROM()[0];
36-
second_bank_chr = &cart.getVROM()[0x1000 * register_chr1];
35+
first_bank_chr = &cart->getVROM()[0];
36+
second_bank_chr = &cart->getVROM()[0x1000 * register_chr1];
3737
}
3838
// first bank
39-
first_bank_prg = &cart.getROM()[0];
39+
first_bank_prg = &cart->getROM()[0];
4040
// last bank
41-
second_bank_prg = &cart.getROM()[cart.getROM().size() - 0x4000];
41+
second_bank_prg = &cart->getROM()[cart->getROM().size() - 0x4000];
4242
}
4343

4444
NES_Byte MapperSxROM::readPRG(NES_Address address) {
@@ -70,22 +70,22 @@ void MapperSxROM::writePRG(NES_Address address, NES_Byte value) {
7070
// Recalculate CHR pointers
7171
if (mode_chr == 0) { // one 8KB bank
7272
// ignore last bit
73-
first_bank_chr = &cartridge.getVROM()[0x1000 * (register_chr0 | 1)];
73+
first_bank_chr = &cartridge->getVROM()[0x1000 * (register_chr0 | 1)];
7474
second_bank_chr = first_bank_chr + 0x1000;
7575
} else { // two 4KB banks
76-
first_bank_chr = &cartridge.getVROM()[0x1000 * register_chr0];
77-
second_bank_chr = &cartridge.getVROM()[0x1000 * register_chr1];
76+
first_bank_chr = &cartridge->getVROM()[0x1000 * register_chr0];
77+
second_bank_chr = &cartridge->getVROM()[0x1000 * register_chr1];
7878
}
7979
} else if (address <= 0xbfff) { // CHR Reg 0
8080
register_chr0 = temp_register;
8181
// OR 1 if 8KB mode
82-
first_bank_chr = &cartridge.getVROM()[0x1000 * (temp_register | (1 - mode_chr))];
82+
first_bank_chr = &cartridge->getVROM()[0x1000 * (temp_register | (1 - mode_chr))];
8383
if (mode_chr == 0)
8484
second_bank_chr = first_bank_chr + 0x1000;
8585
} else if (address <= 0xdfff) {
8686
register_chr1 = temp_register;
8787
if(mode_chr == 1)
88-
second_bank_chr = &cartridge.getVROM()[0x1000 * temp_register];
88+
second_bank_chr = &cartridge->getVROM()[0x1000 * temp_register];
8989
} else {
9090
// TODO: PRG-RAM
9191
if ((temp_register & 0x10) == 0x10) {
@@ -110,15 +110,15 @@ void MapperSxROM::writePRG(NES_Address address, NES_Byte value) {
110110
void MapperSxROM::calculatePRGPointers() {
111111
if (mode_prg <= 1) { // 32KB changeable
112112
// equivalent to multiplying 0x8000 * (register_prg >> 1)
113-
first_bank_prg = &cartridge.getROM()[0x4000 * (register_prg & ~1)];
113+
first_bank_prg = &cartridge->getROM()[0x4000 * (register_prg & ~1)];
114114
// add 16KB
115115
second_bank_prg = first_bank_prg + 0x4000;
116116
} else if (mode_prg == 2) { // fix first switch second
117-
first_bank_prg = &cartridge.getROM()[0];
117+
first_bank_prg = &cartridge->getROM()[0];
118118
second_bank_prg = first_bank_prg + 0x4000 * register_prg;
119119
} else { // switch first fix second
120-
first_bank_prg = &cartridge.getROM()[0x4000 * register_prg];
121-
second_bank_prg = &cartridge.getROM()[cartridge.getROM().size() - 0x4000];
120+
first_bank_prg = &cartridge->getROM()[0x4000 * register_prg];
121+
second_bank_prg = &cartridge->getROM()[cartridge->getROM().size() - 0x4000];
122122
}
123123
}
124124

nes_py/nes/src/mappers/mapper_UxROM.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
namespace NES {
1212

13-
MapperUxROM::MapperUxROM(Cartridge &cart) :
13+
MapperUxROM::MapperUxROM(Cartridge* cart) :
1414
Mapper(cart),
1515
select_prg(0) {
16-
if (cart.getVROM().size() == 0) {
16+
if (cart->getVROM().size() == 0) {
1717
has_character_ram = true;
1818
character_ram.resize(0x2000);
1919
LOG(Info) << "Uses character RAM" << std::endl;
@@ -22,19 +22,19 @@ MapperUxROM::MapperUxROM(Cartridge &cart) :
2222
}
2323

2424
// last - 16KB
25-
last_bank_pointer = &cart.getROM()[cart.getROM().size() - 0x4000];
25+
last_bank_pointer = &cart->getROM()[cart->getROM().size() - 0x4000];
2626
}
2727

2828
NES_Byte MapperUxROM::readPRG(NES_Address address) {
2929
if (address < 0xc000)
30-
return cartridge.getROM()[((address - 0x8000) & 0x3fff) | (select_prg << 14)];
30+
return cartridge->getROM()[((address - 0x8000) & 0x3fff) | (select_prg << 14)];
3131
else
3232
return *(last_bank_pointer + (address & 0x3fff));
3333
}
3434

3535
const NES_Byte* MapperUxROM::getPagePtr(NES_Address address) {
3636
if (address < 0xc000)
37-
return &cartridge.getROM()[((address - 0x8000) & 0x3fff) | (select_prg << 14)];
37+
return &cartridge->getROM()[((address - 0x8000) & 0x3fff) | (select_prg << 14)];
3838
else
3939
return last_bank_pointer + (address & 0x3fff);
4040
}
@@ -43,7 +43,7 @@ NES_Byte MapperUxROM::readCHR(NES_Address address) {
4343
if (has_character_ram)
4444
return character_ram[address];
4545
else
46-
return cartridge.getVROM()[address];
46+
return cartridge->getVROM()[address];
4747
}
4848

4949
void MapperUxROM::writeCHR(NES_Address address, NES_Byte value) {

0 commit comments

Comments
 (0)