Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/quicknes/nes_emu/Nes_State.h
2 views
1
2
// NES state snapshot for saving and restoring emulator state
3
4
// Nes_Emu 0.7.0
5
6
#ifndef NES_STATE_H
7
#define NES_STATE_H
8
9
#include "Nes_File.h"
10
#include "Nes_Cpu.h"
11
class Nes_Emu;
12
class Nes_State;
13
14
typedef long frame_count_t;
15
16
// Writes state to a file
17
class Nes_State_Writer : public Nes_File_Writer {
18
public:
19
// Begin writing file
20
blargg_err_t begin( Auto_File_Writer );
21
22
// Write emulator's current state to file and end
23
blargg_err_t end( Nes_Emu const& );
24
25
// Write state to file and end
26
blargg_err_t end( Nes_State const& );
27
};
28
29
// Reads state from a file
30
class Nes_State_Reader : public Nes_File_Reader {
31
public:
32
33
// Begin reading state snapshot from file
34
blargg_err_t begin( Auto_File_Reader, Nes_State* = 0 );
35
36
// Go to next unrecognized block in file
37
blargg_err_t next_block();
38
39
// State as read from file. Only valid after all blocks have been read.
40
Nes_State const& state() const;
41
42
public:
43
Nes_State_Reader();
44
~Nes_State_Reader();
45
private:
46
Nes_State* owned;
47
Nes_State* state_;
48
};
49
50
class Nes_State_ {
51
public:
52
53
blargg_err_t write_blocks( Nes_File_Writer& ) const;
54
void set_nes_state( nes_state_t const& );
55
blargg_err_t read_blocks( Nes_File_Reader& );
56
57
enum { ram_size = 0x800 };
58
enum { sram_max = 0x2000 };
59
enum { spr_ram_size = 0x100 };
60
enum { nametable_max = 0x800 };
61
enum { chr_max = 0x2000 };
62
BOOST::uint8_t *ram, *sram, *spr_ram, *nametable, *chr;
63
nes_state_t nes;
64
Nes_Cpu::registers_t* cpu;
65
joypad_state_t* joypad;
66
apu_state_t* apu;
67
ppu_state_t* ppu;
68
mapper_state_t* mapper;
69
70
bool nes_valid, cpu_valid, joypad_valid, apu_valid, ppu_valid;
71
bool mapper_valid, ram_valid, spr_ram_valid;
72
short sram_size, nametable_size, chr_size;
73
74
// Invalidate all state
75
void clear();
76
77
// Change timestamp
78
void set_timestamp( frame_count_t );
79
80
// Timestamp snapshot was taken at
81
frame_count_t timestamp() const;
82
};
83
84
// Snapshot of emulator state
85
class Nes_State : private Nes_State_ {
86
public:
87
88
Nes_State();
89
90
#if 0 // What is this?
91
Nes_State_::set_timestamp;
92
Nes_State_::timestamp;
93
Nes_State_::clear;
94
#endif
95
96
// Write snapshot to file
97
blargg_err_t write( Auto_File_Writer ) const;
98
99
// Read snapshot from file
100
blargg_err_t read( Auto_File_Reader );
101
102
private:
103
Nes_Cpu::registers_t cpu;
104
joypad_state_t joypad;
105
apu_state_t apu;
106
ppu_state_t ppu;
107
mapper_state_t mapper;
108
BOOST::uint8_t ram [ram_size];
109
BOOST::uint8_t sram [sram_max];
110
BOOST::uint8_t spr_ram [spr_ram_size];
111
BOOST::uint8_t nametable [nametable_max];
112
BOOST::uint8_t chr [chr_max];
113
114
friend class Nes_Emu;
115
friend class Nes_State_Writer;
116
friend class Nes_State_Reader;
117
friend class Nes_Recorder;
118
public:
119
blargg_err_t read_sta_file( Auto_File_Reader );
120
};
121
122
frame_count_t const invalid_frame_count = LONG_MAX / 2 + 1; // a large positive value
123
124
int mem_differs( void const* in, int compare, unsigned long count );
125
126
inline Nes_State const& Nes_State_Reader::state() const
127
{
128
assert( depth() == 0 && block_type() == group_end );
129
return *state_;
130
}
131
132
inline blargg_err_t Nes_State_Writer::begin( Auto_File_Writer dw )
133
{
134
return Nes_File_Writer::begin( dw, state_file_tag );
135
}
136
137
inline void Nes_State_::set_timestamp( frame_count_t t ) { nes.frame_count = t; }
138
139
inline frame_count_t Nes_State_::timestamp() const { return nes.frame_count; }
140
141
#endif
142
143
144