1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
use crate::io::{Audio, LoadSave, Serial};
use crate::logs::{elog, log, LogLevel};
use crate::state::GBState;
use std::fs::File;
use std::io::{Read, Write};
#[derive(Debug)]
pub struct FSLoadSave {
rom_file: String,
save_file: String,
state_file: Option<String>,
}
impl FSLoadSave {
pub fn new(rom_file: impl Into<String>, save_file: impl Into<String>) -> Self {
Self {
rom_file: rom_file.into(),
save_file: save_file.into(),
state_file: None,
}
}
pub fn state_file(mut self, state_file: impl Into<String>) -> Self {
self.state_file = Some(state_file.into());
self
}
}
impl LoadSave for FSLoadSave {
type Error = std::io::Error;
fn load_rom(&self, rom: &mut [u8]) -> Result<(), std::io::Error> {
let mut f = File::open(&self.rom_file)?;
f.read(rom)?;
return Ok(());
}
fn load_bootrom(&self, boot_rom: &mut [u8]) -> Result<(), std::io::Error> {
log(LogLevel::Debug, format!("MBC: {:02x}", boot_rom[0x147]));
log(LogLevel::Debug, format!("CGB: {:02x}", boot_rom[0x143]));
if boot_rom[0x143] == 0x80 || boot_rom[0x143] == 0xc0 {
unimplemented!("CGB Boot rom is not implemented");
// let bytes = include_bytes!("../assets/cgb_boot.bin");
// self.boot_rom[..0x900].copy_from_slice(bytes);
// self.cgb_mode = true;
// self.display.cgb_mode = true;
} else {
let bytes = include_bytes!("../../assets/dmg_boot.bin");
boot_rom[..0x100].copy_from_slice(bytes);
}
Ok(())
}
fn load_external_ram(&self, external_ram: &mut [u8]) -> Result<(), std::io::Error> {
let mut f = File::open(&self.save_file)?;
f.read(external_ram)?;
log(
LogLevel::Infos,
format!("Save file loaded from \"{}\"!", self.save_file),
);
Ok(())
}
fn save_external_ram(&self, external_ram: &[u8]) -> Result<(), std::io::Error> {
let mut f = File::create(&self.save_file)?;
f.write_all(&external_ram)?;
log(
LogLevel::Infos,
format!("Save written to \"{}\"!", self.save_file),
);
Ok(())
}
fn dump_state<S: Serial, A: Audio>(&self, state: &GBState<S, A>) -> Result<(), std::io::Error> {
{
let mut vram_dump_file = File::create(format!("{}.vram.dump", self.rom_file))?;
for addr in 0x8000..0xa000 {
vram_dump_file.write_all(format!("{:02X} ", state.mem.r(addr)).as_bytes())?;
}
}
{
let mut wram_dump_file = File::create(format!("{}.wram.dump", self.rom_file))?;
for addr in 0xc000..0xe000 {
wram_dump_file.write_all(format!("{:02X} ", state.mem.r(addr)).as_bytes())?;
}
}
{
let mut io_dump_file = File::create(format!("{}.io.dump", self.rom_file))?;
for addr in 0xff00..0xff80 {
io_dump_file.write_all(format!("{:02X} ", state.mem.r(addr)).as_bytes())?;
}
}
{
let mut hram_dump_file = File::create(format!("{}.hram.dump", self.rom_file))?;
for addr in 0xff80..=0xffff {
hram_dump_file.write_all(format!("{:02X} ", state.mem.r(addr)).as_bytes())?;
}
}
{
let mut cpu_dump_file = File::create(format!("{}.cpu.dump", self.rom_file))?;
for i in 0..8 {
cpu_dump_file.write_all(format!("{:02X} ", state.cpu.r[i]).as_bytes())?;
}
cpu_dump_file.write_all(format!("{:04X} ", state.cpu.pc).as_bytes())?;
cpu_dump_file.write_all(format!("{:04X} ", state.cpu.sp).as_bytes())?;
}
Ok(())
}
fn save_state<S: Serial, A: Audio>(&self, state: &GBState<S, A>) -> Result<(), std::io::Error> {
if let Some(state_file) = &self.state_file {
let mut state_file = File::create(state_file)?;
for addr in 0x8000..0xa000 {
state_file.write_all(&[state.mem.r(addr)])?;
}
state_file.write_all(state.mem.wram_00.as_ref())?;
state_file.write_all(state.mem.wram_01.as_ref())?;
for addr in 0xff00..0xff80 {
state_file.write_all(&[state.mem.r(addr)])?;
}
state_file.write_all(state.mem.hram.as_ref())?;
state_file.write_all(&[state.mem.interrupts_register])?;
state_file.write_all(&state.cpu.r)?;
state_file.write_all(&state.cpu.pc.to_le_bytes())?;
state_file.write_all(&state.cpu.sp.to_le_bytes())?;
state_file.write_all(&[state.mem.boot_rom_on.into(), state.mem.ime.into()])?;
} else {
elog(
LogLevel::Error,
format!("Tried to save state without state_file specified"),
);
}
Ok(())
}
fn load_state<S: Serial, A: Audio>(
&self,
state: &mut GBState<S, A>,
) -> Result<(), std::io::Error> {
if let Some(state_file) = &self.state_file {
let mut state_file = File::open(state_file)?;
let mut vram = Box::new([0; 0x2000]);
state_file.read_exact(vram.as_mut())?;
for i in 0x0000..0x2000 {
state.mem.w(0x8000 + i, vram[i as usize]);
}
state_file.read_exact(state.mem.wram_00.as_mut())?;
state_file.read_exact(state.mem.wram_01.as_mut())?;
let mut io = [0; 0x80];
state_file.read_exact(io.as_mut())?;
for i in 0x00..0x80 {
state.mem.w(0xff00 + i, io[i as usize]);
}
state_file.read_exact(state.mem.hram.as_mut())?;
let mut reg8 = [0; 1];
state_file.read_exact(reg8.as_mut())?;
state.mem.interrupts_register = reg8[0];
state_file.read_exact(&mut state.cpu.r)?;
let mut reg16 = [0; 2];
state_file.read_exact(&mut reg16)?;
state.cpu.pc = u16::from_le_bytes(reg16);
state_file.read_exact(&mut reg16)?;
state.cpu.sp = u16::from_le_bytes(reg16);
state_file.read_exact(reg8.as_mut())?;
state.mem.boot_rom_on = reg8[0] != 0;
state_file.read_exact(reg8.as_mut())?;
state.mem.ime = reg8[0] != 0;
}
Ok(())
}
}
|