Trait spectrusty_formats::snapshot::SnapshotCreator
source · pub trait SnapshotCreator {
Show 17 methods
fn model(&self) -> ComputerModel;
fn extensions(&self) -> Extensions;
fn cpu(&self) -> CpuModel;
fn current_clock(&self) -> FTs;
fn border_color(&self) -> BorderColor;
fn issue(&self) -> ReadEarMode;
fn memory_ref(&self, range: MemoryRange) -> Result<&[u8], ZxMemoryError>;
fn joystick(&self) -> Option<JoystickModel> { ... }
fn ay_state(
&self,
_choice: Ay3_891xDevice
) -> Option<(AyRegister, &[u8; 16])> { ... }
fn ula128_flags(&self) -> Ula128MemFlags { ... }
fn ula3_flags(&self) -> Ula3CtrlFlags { ... }
fn timex_flags(&self) -> ScldCtrlFlags { ... }
fn timex_memory_banks(&self) -> u8 { ... }
fn is_interface1_rom_paged_in(&self) -> bool { ... }
fn is_plus_d_rom_paged_in(&self) -> bool { ... }
fn is_disciple_rom_paged_in(&self) -> bool { ... }
fn is_tr_dos_rom_paged_in(&self) -> bool { ... }
}
Expand description
The methods can be called more than one time.
Required Methods§
fn model(&self) -> ComputerModel
fn extensions(&self) -> Extensions
fn cpu(&self) -> CpuModel
fn current_clock(&self) -> FTs
fn border_color(&self) -> BorderColor
fn issue(&self) -> ReadEarMode
fn memory_ref(&self, range: MemoryRange) -> Result<&[u8], ZxMemoryError>
Provided Methods§
sourcefn joystick(&self) -> Option<JoystickModel>
fn joystick(&self) -> Option<JoystickModel>
Examples found in repository?
src/z80/saver.rs (line 377)
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
pub fn save_z80v1<C: SnapshotCreator, W: Write>(
snapshot: &C,
mut wr: W
) -> Result<SnapshotResult>
{
use ComputerModel::*;
let mut result = SnapshotResult::OK;
let model = snapshot.model();
match model {
Spectrum48 => {},
Spectrum16|SpectrumNTSC|TimexTC2048 => {
result.insert(SnapshotResult::MODEL_NSUP);
}
_ => return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a version 1 snapshot of this computer model"))
};
let extensions = snapshot.extensions();
if let Err(bad_ext) = model.validate_extensions(extensions) {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
format!("Z80: the model {} can't be saved with {}", model, bad_ext)))
}
if extensions.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in()
|| extensions.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in()
|| extensions.intersects(Extensions::DISCIPLE) && snapshot.is_disciple_rom_paged_in()
|| extensions.intersects(Extensions::TR_DOS) && snapshot.is_tr_dos_rom_paged_in()
{
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a version 1 snapshot with the external ROM paged in"))
}
if extensions != Extensions::NONE {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
let cpu = get_nmos_cpu(snapshot.cpu(), &mut result);
let border = snapshot.border_color();
let issue = snapshot.issue();
let joystick = snapshot.joystick();
if snapshot.ay_state(Ay3_891xDevice::Ay128k).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Melodik).is_some()
|| snapshot.ay_state(Ay3_891xDevice::FullerBox).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Timex).is_some()
{
result.insert(SnapshotResult::SOUND_CHIP_NSUP);
}
let mut header = Header::default();
init_z80_header(
&mut header,
Z80Version::V1,
&cpu,
border,
issue,
joystick,
&mut result
);
header.write_struct(wr.by_ref())?;
let is_16k = model == ComputerModel::Spectrum16;
let ramend = if is_16k { 0x4000 } else { 0xC000 };
let mem_slice = snapshot.memory_ref(MemoryRange::Ram(0..ramend))?;
compress_write_all(mem_slice, wr.by_ref())?;
if is_16k {
compress_repeat_write_all(!0, 0x8000, wr.by_ref())?;
}
wr.write_all(MEMORY_V1_TERM)?;
wr.flush()?;
Ok(result)
}
/// Saves a **Z80** file version 2 into `wr` from the provided reference to a `snapshot` struct
/// implementing [SnapshotCreator].
///
/// # Errors
/// This function may return an error from attempts to write the file or if for some reason
/// a snapshot could not be created.
pub fn save_z80v2<C: SnapshotCreator, W: Write>(
snapshot: &C,
wr: W
) -> Result<SnapshotResult>
{
let mut result = SnapshotResult::OK;
let model = snapshot.model();
let ext = snapshot.extensions();
let border = snapshot.border_color();
let issue = snapshot.issue();
let joystick = snapshot.joystick();
let cpu = get_nmos_cpu(snapshot.cpu(), &mut result);
if !is_cpu_safe_for_snapshot(&cpu) {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "Z80: can't safely snapshot the CPU state"))
}
if let Err(bad_ext) = model.validate_extensions(ext) {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
format!("Z80: the model {} can't be saved with {}", model, bad_ext)))
}
let mut header = Header::default();
init_z80_header(
&mut header,
Z80Version::V2,
&cpu,
border,
issue,
joystick,
&mut result
);
let mut head_ex = HeaderEx::default();
init_z80_header_ex(
&mut head_ex,
cpu,
model,
ext,
snapshot,
select_hw_model_v2,
&mut result
)?;
save_all_v2v3(Z80Version::V2, snapshot, model, &header, &head_ex, wr)?;
Ok(result)
}
/// Saves a **Z80** file version 3 into `wr` from the provided reference to a `snapshot` struct
/// implementing [SnapshotCreator].
///
/// # Errors
/// This function may return an error from attempts to write the file or if for some reason
/// a snapshot could not be created.
pub fn save_z80v3<C: SnapshotCreator, W: Write>(
snapshot: &C,
wr: W
) -> Result<SnapshotResult>
{
use ComputerModel::*;
let mut result = SnapshotResult::OK;
let model = snapshot.model();
let ext = snapshot.extensions();
let border = snapshot.border_color();
let issue = snapshot.issue();
let joystick = snapshot.joystick();
let cpu = get_nmos_cpu(snapshot.cpu(), &mut result);
if !is_cpu_safe_for_snapshot(&cpu) {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "Z80: can't safely snapshot the CPU state"))
}
if let Err(bad_ext) = model.validate_extensions(ext) {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
format!("Z80: the model {} can't be saved with {}", model, bad_ext)))
}
let mut header = Header::default();
init_z80_header(
&mut header,
Z80Version::V3,
&cpu,
border,
issue,
joystick,
&mut result
);
let mut head_ex = HeaderEx::default();
init_z80_header_ex(
&mut head_ex,
cpu,
model,
ext,
snapshot,
select_hw_model_v3,
&mut result
)?;
let (ts_lo, ts_hi) = cycles_to_z80(snapshot.current_clock(), model);
head_ex.ts_lo = ts_lo.to_le_bytes();
head_ex.ts_hi = ts_hi;
if (ext.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in())
|| (ext.intersects(Extensions::DISCIPLE) && snapshot.is_disciple_rom_paged_in())
{
head_ex.mgt_rom = !0;
}
if let Some(JoystickModel::Sinclair2) = joystick {
head_ex.joy_bindings = [ 3, 1, 3, 2, 3, 4, 3, 8, 3,10];
head_ex.joy_ascii = [31, 0,32, 0,33, 0,34, 0,35, 0];
}
match model {
SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e => {
head_ex.port2 = snapshot.ula3_flags().bits();
}
_ => {
head_ex.fn1 = !0;
if !(ext.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in()) {
head_ex.fn2 = !0;
}
}
}
if ext.intersects(Extensions::PLUS_D) {
head_ex.mgt_type = 16;
}
// head_ex.flags4 = 0;
// head_ex.disciple1 = 0;
// head_ex.disciple2 = 0;
save_all_v2v3(Z80Version::V3, snapshot, model, &header, &head_ex, wr)?;
Ok(result)
}
More examples
src/sna.rs (line 352)
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
pub fn save_sna<C: SnapshotCreator, W: Write>(
snapshot: &C,
mut wr: W
) -> Result<SnapshotResult>
{
use ComputerModel::*;
let mut result = SnapshotResult::KEYB_ISSUE_NSUP;
let model = snapshot.model();
let is_128 = match model {
Spectrum48 => false,
Spectrum128 => true,
SpectrumPlus2|SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e|SpectrumSE => {
result.insert(SnapshotResult::MODEL_NSUP);
true
}
Spectrum16|SpectrumNTSC|TimexTC2048|TimexTC2068|TimexTS2068 => {
result.insert(SnapshotResult::MODEL_NSUP);
false
}
};
let extensions = snapshot.extensions();
if extensions.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in()
|| extensions.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in()
{
return Err(Error::new(ErrorKind::InvalidInput,
"SNA: can't create a snapshot with the external ROM paged in"))
}
if extensions != Extensions::NONE && extensions != Extensions::TR_DOS {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
let cpu = match snapshot.cpu() {
CpuModel::NMOS(cpu) => cpu,
CpuModel::CMOS(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
},
CpuModel::BM1(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
}
};
if !is_cpu_safe_for_snapshot(&cpu) {
return Err(Error::new(ErrorKind::InvalidInput, "SNA: can't safely snapshot the CPU state"))
}
let mut sna = make_header(&cpu);
sna.border = snapshot.border_color().into();
if snapshot.joystick().is_some() {
result.insert(SnapshotResult::JOYSTICK_NSUP);
}
if is_128 || snapshot.ay_state(Ay3_891xDevice::Melodik).is_some()
|| snapshot.ay_state(Ay3_891xDevice::FullerBox).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Timex).is_some() {
result.insert(SnapshotResult::SOUND_CHIP_NSUP);
}
if !is_128 {
return save_sna48(snapshot, cpu, model == ComputerModel::Spectrum16, sna, wr, result)
}
let memflags = snapshot.ula128_flags();
let mut sna_ext = SnaHeader128 {
pc: cpu.get_pc().to_le_bytes(),
port_data: memflags.bits(),
..Default::default()
};
if extensions.intersects(Extensions::TR_DOS) {
sna_ext.trdos_rom = snapshot.is_tr_dos_rom_paged_in().into();
}
sna.write_struct(wr.by_ref())?;
let last_page: usize = memflags.last_ram_page_bank();
let index48 = [5,2,last_page];
for page in index48.iter() {
wr.write_all(
snapshot.memory_ref(MemoryRange::Ram(page * PAGE_SIZE..(page + 1) * PAGE_SIZE))?
)?;
}
sna_ext.write_struct(wr.by_ref())?;
for page in (0..8).filter(|n| !index48.contains(n) && *n != last_page) {
wr.write_all(
snapshot.memory_ref(MemoryRange::Ram(page * PAGE_SIZE..(page + 1) * PAGE_SIZE))?
)?;
}
wr.flush()?;
Ok(result)
}
sourcefn ay_state(&self, _choice: Ay3_891xDevice) -> Option<(AyRegister, &[u8; 16])>
fn ay_state(&self, _choice: Ay3_891xDevice) -> Option<(AyRegister, &[u8; 16])>
Examples found in repository?
src/z80/saver.rs (line 213)
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
fn init_z80_header_ex<S: SnapshotCreator, C: Cpu>(
head_ex: &mut HeaderEx,
cpu: C,
model: ComputerModel,
ext: Extensions,
snapshot: &S,
select_hw_model: HwModelSelector<S>,
result: &mut SnapshotResult
) -> Result<()>
{
use ComputerModel::*;
head_ex.pc = cpu.get_pc().to_le_bytes();
let (hw_mode, alt_hw) = select_hw_model(model, ext, snapshot, result)?;
let mut flags3 = Flags3::empty();
flags3.set(Flags3::ALT_HW_MODE, alt_hw);
head_ex.hw_mode = hw_mode;
let res = if let Some(res) = snapshot.ay_state(Ay3_891xDevice::Ay128k) {
if snapshot.ay_state(Ay3_891xDevice::FullerBox).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Melodik).is_some()
{
result.insert(SnapshotResult::SOUND_CHIP_NSUP);
}
Some(res)
}
else if let Some(res) = snapshot.ay_state(Ay3_891xDevice::FullerBox) {
flags3.insert(Flags3::AY_SOUND_EMU|Flags3::AY_FULLER_BOX);
Some(res)
}
else if let Some(res) = snapshot.ay_state(Ay3_891xDevice::Melodik) {
flags3.insert(Flags3::AY_SOUND_EMU);
Some(res)
}
else {
snapshot.ay_state(Ay3_891xDevice::Timex)
};
if let Some((ay_sel_reg, ay_regs)) = res {
head_ex.ay_sel_reg = ay_sel_reg.into();
head_ex.ay_regs = *ay_regs;
}
head_ex.port1 = match model {
Spectrum128|SpectrumPlus2|SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e|
SpectrumSE => snapshot.ula128_flags().bits(),
TimexTC2048|TimexTC2068|TimexTS2068 => snapshot.timex_memory_banks(),
_ => 0
};
head_ex.ifrom = match model {
TimexTC2048|TimexTS2068|TimexTC2068 => {
if ext.intersects(Extensions::IF1|Extensions::PLUS_D|Extensions::DISCIPLE) {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
snapshot.timex_flags().bits()
}
_ if ext.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in() => {
!0
}
_ => 0
};
head_ex.flags3 = flags3.bits();
Ok(())
}
fn save_ram_pages<W: Write, S: SnapshotCreator, I: Iterator<Item=(u8, usize)>>(
mut wr: W,
snapshot: &S,
pages: I
) -> Result<()>
{
let mut buf = Vec::with_capacity(0x1000);
for (ptype, page) in pages {
buf.clear();
let mem_slice = snapshot.memory_ref(MemoryRange::Ram(page * PAGE_SIZE..(page + 1) * PAGE_SIZE))?;
compress_write_all(mem_slice, &mut buf)?;
let (mem_head, slice) = match buf.len().try_into() {
Ok(core::u16::MAX)|Err(..) => {
(MemoryHeader::new(core::u16::MAX, ptype), mem_slice)
}
Ok(length) => (MemoryHeader::new(length, ptype), &buf[..]),
};
mem_head.write_struct(wr.by_ref())?;
wr.write_all(slice)?;
}
wr.flush()
}
fn save_all_v2v3<W: Write, S: SnapshotCreator>(
version: Z80Version,
snapshot: &S,
model: ComputerModel,
header: &Header,
head_ex: &HeaderEx,
mut wr: W
) -> Result<()>
{
use ComputerModel::*;
header.write_struct(wr.by_ref())?;
let ex_len: u16 = match version {
Z80Version::V2 => 23,
Z80Version::V3 if head_ex.port2 != 0 => 55,
Z80Version::V3 => 54,
_ => unreachable!()
};
wr.write_all(&ex_len.to_le_bytes()[..])?;
head_ex.write_struct_with_limit(wr.by_ref(), ex_len as usize)?;
match model {
Spectrum16 => {
save_ram_pages(wr, snapshot, iter::once((8, 0)))
}
Spectrum48|SpectrumNTSC|TimexTC2048|TimexTS2068|TimexTC2068 => {
save_ram_pages(wr, snapshot,
[(8, 0), (4, 1), (5, 2)].iter().copied())
}
Spectrum128|SpectrumPlus2|SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e => {
save_ram_pages(wr, snapshot,
(0..8).map(|page| (page as u8 + 3, page))
)
}
_ => unreachable!()
}
}
fn get_nmos_cpu(cpu: CpuModel, result: &mut SnapshotResult) -> Z80NMOS {
match cpu {
CpuModel::NMOS(cpu) => cpu,
CpuModel::CMOS(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
},
CpuModel::BM1(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
}
}
}
/// Saves a **Z80** file version 1 into `wr` from the provided reference to a `snapshot` struct
/// implementing [SnapshotCreator].
///
/// # Errors
/// This function may return an error from attempts to write the file or if for some reason
/// a snapshot could not be created.
pub fn save_z80v1<C: SnapshotCreator, W: Write>(
snapshot: &C,
mut wr: W
) -> Result<SnapshotResult>
{
use ComputerModel::*;
let mut result = SnapshotResult::OK;
let model = snapshot.model();
match model {
Spectrum48 => {},
Spectrum16|SpectrumNTSC|TimexTC2048 => {
result.insert(SnapshotResult::MODEL_NSUP);
}
_ => return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a version 1 snapshot of this computer model"))
};
let extensions = snapshot.extensions();
if let Err(bad_ext) = model.validate_extensions(extensions) {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
format!("Z80: the model {} can't be saved with {}", model, bad_ext)))
}
if extensions.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in()
|| extensions.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in()
|| extensions.intersects(Extensions::DISCIPLE) && snapshot.is_disciple_rom_paged_in()
|| extensions.intersects(Extensions::TR_DOS) && snapshot.is_tr_dos_rom_paged_in()
{
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a version 1 snapshot with the external ROM paged in"))
}
if extensions != Extensions::NONE {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
let cpu = get_nmos_cpu(snapshot.cpu(), &mut result);
let border = snapshot.border_color();
let issue = snapshot.issue();
let joystick = snapshot.joystick();
if snapshot.ay_state(Ay3_891xDevice::Ay128k).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Melodik).is_some()
|| snapshot.ay_state(Ay3_891xDevice::FullerBox).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Timex).is_some()
{
result.insert(SnapshotResult::SOUND_CHIP_NSUP);
}
let mut header = Header::default();
init_z80_header(
&mut header,
Z80Version::V1,
&cpu,
border,
issue,
joystick,
&mut result
);
header.write_struct(wr.by_ref())?;
let is_16k = model == ComputerModel::Spectrum16;
let ramend = if is_16k { 0x4000 } else { 0xC000 };
let mem_slice = snapshot.memory_ref(MemoryRange::Ram(0..ramend))?;
compress_write_all(mem_slice, wr.by_ref())?;
if is_16k {
compress_repeat_write_all(!0, 0x8000, wr.by_ref())?;
}
wr.write_all(MEMORY_V1_TERM)?;
wr.flush()?;
Ok(result)
}
More examples
src/sna.rs (line 356)
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
pub fn save_sna<C: SnapshotCreator, W: Write>(
snapshot: &C,
mut wr: W
) -> Result<SnapshotResult>
{
use ComputerModel::*;
let mut result = SnapshotResult::KEYB_ISSUE_NSUP;
let model = snapshot.model();
let is_128 = match model {
Spectrum48 => false,
Spectrum128 => true,
SpectrumPlus2|SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e|SpectrumSE => {
result.insert(SnapshotResult::MODEL_NSUP);
true
}
Spectrum16|SpectrumNTSC|TimexTC2048|TimexTC2068|TimexTS2068 => {
result.insert(SnapshotResult::MODEL_NSUP);
false
}
};
let extensions = snapshot.extensions();
if extensions.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in()
|| extensions.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in()
{
return Err(Error::new(ErrorKind::InvalidInput,
"SNA: can't create a snapshot with the external ROM paged in"))
}
if extensions != Extensions::NONE && extensions != Extensions::TR_DOS {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
let cpu = match snapshot.cpu() {
CpuModel::NMOS(cpu) => cpu,
CpuModel::CMOS(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
},
CpuModel::BM1(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
}
};
if !is_cpu_safe_for_snapshot(&cpu) {
return Err(Error::new(ErrorKind::InvalidInput, "SNA: can't safely snapshot the CPU state"))
}
let mut sna = make_header(&cpu);
sna.border = snapshot.border_color().into();
if snapshot.joystick().is_some() {
result.insert(SnapshotResult::JOYSTICK_NSUP);
}
if is_128 || snapshot.ay_state(Ay3_891xDevice::Melodik).is_some()
|| snapshot.ay_state(Ay3_891xDevice::FullerBox).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Timex).is_some() {
result.insert(SnapshotResult::SOUND_CHIP_NSUP);
}
if !is_128 {
return save_sna48(snapshot, cpu, model == ComputerModel::Spectrum16, sna, wr, result)
}
let memflags = snapshot.ula128_flags();
let mut sna_ext = SnaHeader128 {
pc: cpu.get_pc().to_le_bytes(),
port_data: memflags.bits(),
..Default::default()
};
if extensions.intersects(Extensions::TR_DOS) {
sna_ext.trdos_rom = snapshot.is_tr_dos_rom_paged_in().into();
}
sna.write_struct(wr.by_ref())?;
let last_page: usize = memflags.last_ram_page_bank();
let index48 = [5,2,last_page];
for page in index48.iter() {
wr.write_all(
snapshot.memory_ref(MemoryRange::Ram(page * PAGE_SIZE..(page + 1) * PAGE_SIZE))?
)?;
}
sna_ext.write_struct(wr.by_ref())?;
for page in (0..8).filter(|n| !index48.contains(n) && *n != last_page) {
wr.write_all(
snapshot.memory_ref(MemoryRange::Ram(page * PAGE_SIZE..(page + 1) * PAGE_SIZE))?
)?;
}
wr.flush()?;
Ok(result)
}
sourcefn ula128_flags(&self) -> Ula128MemFlags
fn ula128_flags(&self) -> Ula128MemFlags
Examples found in repository?
src/z80/saver.rs (line 239)
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
fn init_z80_header_ex<S: SnapshotCreator, C: Cpu>(
head_ex: &mut HeaderEx,
cpu: C,
model: ComputerModel,
ext: Extensions,
snapshot: &S,
select_hw_model: HwModelSelector<S>,
result: &mut SnapshotResult
) -> Result<()>
{
use ComputerModel::*;
head_ex.pc = cpu.get_pc().to_le_bytes();
let (hw_mode, alt_hw) = select_hw_model(model, ext, snapshot, result)?;
let mut flags3 = Flags3::empty();
flags3.set(Flags3::ALT_HW_MODE, alt_hw);
head_ex.hw_mode = hw_mode;
let res = if let Some(res) = snapshot.ay_state(Ay3_891xDevice::Ay128k) {
if snapshot.ay_state(Ay3_891xDevice::FullerBox).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Melodik).is_some()
{
result.insert(SnapshotResult::SOUND_CHIP_NSUP);
}
Some(res)
}
else if let Some(res) = snapshot.ay_state(Ay3_891xDevice::FullerBox) {
flags3.insert(Flags3::AY_SOUND_EMU|Flags3::AY_FULLER_BOX);
Some(res)
}
else if let Some(res) = snapshot.ay_state(Ay3_891xDevice::Melodik) {
flags3.insert(Flags3::AY_SOUND_EMU);
Some(res)
}
else {
snapshot.ay_state(Ay3_891xDevice::Timex)
};
if let Some((ay_sel_reg, ay_regs)) = res {
head_ex.ay_sel_reg = ay_sel_reg.into();
head_ex.ay_regs = *ay_regs;
}
head_ex.port1 = match model {
Spectrum128|SpectrumPlus2|SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e|
SpectrumSE => snapshot.ula128_flags().bits(),
TimexTC2048|TimexTC2068|TimexTS2068 => snapshot.timex_memory_banks(),
_ => 0
};
head_ex.ifrom = match model {
TimexTC2048|TimexTS2068|TimexTC2068 => {
if ext.intersects(Extensions::IF1|Extensions::PLUS_D|Extensions::DISCIPLE) {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
snapshot.timex_flags().bits()
}
_ if ext.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in() => {
!0
}
_ => 0
};
head_ex.flags3 = flags3.bits();
Ok(())
}
More examples
src/sna.rs (line 366)
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
pub fn save_sna<C: SnapshotCreator, W: Write>(
snapshot: &C,
mut wr: W
) -> Result<SnapshotResult>
{
use ComputerModel::*;
let mut result = SnapshotResult::KEYB_ISSUE_NSUP;
let model = snapshot.model();
let is_128 = match model {
Spectrum48 => false,
Spectrum128 => true,
SpectrumPlus2|SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e|SpectrumSE => {
result.insert(SnapshotResult::MODEL_NSUP);
true
}
Spectrum16|SpectrumNTSC|TimexTC2048|TimexTC2068|TimexTS2068 => {
result.insert(SnapshotResult::MODEL_NSUP);
false
}
};
let extensions = snapshot.extensions();
if extensions.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in()
|| extensions.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in()
{
return Err(Error::new(ErrorKind::InvalidInput,
"SNA: can't create a snapshot with the external ROM paged in"))
}
if extensions != Extensions::NONE && extensions != Extensions::TR_DOS {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
let cpu = match snapshot.cpu() {
CpuModel::NMOS(cpu) => cpu,
CpuModel::CMOS(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
},
CpuModel::BM1(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
}
};
if !is_cpu_safe_for_snapshot(&cpu) {
return Err(Error::new(ErrorKind::InvalidInput, "SNA: can't safely snapshot the CPU state"))
}
let mut sna = make_header(&cpu);
sna.border = snapshot.border_color().into();
if snapshot.joystick().is_some() {
result.insert(SnapshotResult::JOYSTICK_NSUP);
}
if is_128 || snapshot.ay_state(Ay3_891xDevice::Melodik).is_some()
|| snapshot.ay_state(Ay3_891xDevice::FullerBox).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Timex).is_some() {
result.insert(SnapshotResult::SOUND_CHIP_NSUP);
}
if !is_128 {
return save_sna48(snapshot, cpu, model == ComputerModel::Spectrum16, sna, wr, result)
}
let memflags = snapshot.ula128_flags();
let mut sna_ext = SnaHeader128 {
pc: cpu.get_pc().to_le_bytes(),
port_data: memflags.bits(),
..Default::default()
};
if extensions.intersects(Extensions::TR_DOS) {
sna_ext.trdos_rom = snapshot.is_tr_dos_rom_paged_in().into();
}
sna.write_struct(wr.by_ref())?;
let last_page: usize = memflags.last_ram_page_bank();
let index48 = [5,2,last_page];
for page in index48.iter() {
wr.write_all(
snapshot.memory_ref(MemoryRange::Ram(page * PAGE_SIZE..(page + 1) * PAGE_SIZE))?
)?;
}
sna_ext.write_struct(wr.by_ref())?;
for page in (0..8).filter(|n| !index48.contains(n) && *n != last_page) {
wr.write_all(
snapshot.memory_ref(MemoryRange::Ram(page * PAGE_SIZE..(page + 1) * PAGE_SIZE))?
)?;
}
wr.flush()?;
Ok(result)
}
sourcefn ula3_flags(&self) -> Ula3CtrlFlags
fn ula3_flags(&self) -> Ula3CtrlFlags
Examples found in repository?
src/z80/saver.rs (line 529)
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
pub fn save_z80v3<C: SnapshotCreator, W: Write>(
snapshot: &C,
wr: W
) -> Result<SnapshotResult>
{
use ComputerModel::*;
let mut result = SnapshotResult::OK;
let model = snapshot.model();
let ext = snapshot.extensions();
let border = snapshot.border_color();
let issue = snapshot.issue();
let joystick = snapshot.joystick();
let cpu = get_nmos_cpu(snapshot.cpu(), &mut result);
if !is_cpu_safe_for_snapshot(&cpu) {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "Z80: can't safely snapshot the CPU state"))
}
if let Err(bad_ext) = model.validate_extensions(ext) {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
format!("Z80: the model {} can't be saved with {}", model, bad_ext)))
}
let mut header = Header::default();
init_z80_header(
&mut header,
Z80Version::V3,
&cpu,
border,
issue,
joystick,
&mut result
);
let mut head_ex = HeaderEx::default();
init_z80_header_ex(
&mut head_ex,
cpu,
model,
ext,
snapshot,
select_hw_model_v3,
&mut result
)?;
let (ts_lo, ts_hi) = cycles_to_z80(snapshot.current_clock(), model);
head_ex.ts_lo = ts_lo.to_le_bytes();
head_ex.ts_hi = ts_hi;
if (ext.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in())
|| (ext.intersects(Extensions::DISCIPLE) && snapshot.is_disciple_rom_paged_in())
{
head_ex.mgt_rom = !0;
}
if let Some(JoystickModel::Sinclair2) = joystick {
head_ex.joy_bindings = [ 3, 1, 3, 2, 3, 4, 3, 8, 3,10];
head_ex.joy_ascii = [31, 0,32, 0,33, 0,34, 0,35, 0];
}
match model {
SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e => {
head_ex.port2 = snapshot.ula3_flags().bits();
}
_ => {
head_ex.fn1 = !0;
if !(ext.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in()) {
head_ex.fn2 = !0;
}
}
}
if ext.intersects(Extensions::PLUS_D) {
head_ex.mgt_type = 16;
}
// head_ex.flags4 = 0;
// head_ex.disciple1 = 0;
// head_ex.disciple2 = 0;
save_all_v2v3(Z80Version::V3, snapshot, model, &header, &head_ex, wr)?;
Ok(result)
}
sourcefn timex_flags(&self) -> ScldCtrlFlags
fn timex_flags(&self) -> ScldCtrlFlags
Examples found in repository?
src/z80/saver.rs (line 248)
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
fn init_z80_header_ex<S: SnapshotCreator, C: Cpu>(
head_ex: &mut HeaderEx,
cpu: C,
model: ComputerModel,
ext: Extensions,
snapshot: &S,
select_hw_model: HwModelSelector<S>,
result: &mut SnapshotResult
) -> Result<()>
{
use ComputerModel::*;
head_ex.pc = cpu.get_pc().to_le_bytes();
let (hw_mode, alt_hw) = select_hw_model(model, ext, snapshot, result)?;
let mut flags3 = Flags3::empty();
flags3.set(Flags3::ALT_HW_MODE, alt_hw);
head_ex.hw_mode = hw_mode;
let res = if let Some(res) = snapshot.ay_state(Ay3_891xDevice::Ay128k) {
if snapshot.ay_state(Ay3_891xDevice::FullerBox).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Melodik).is_some()
{
result.insert(SnapshotResult::SOUND_CHIP_NSUP);
}
Some(res)
}
else if let Some(res) = snapshot.ay_state(Ay3_891xDevice::FullerBox) {
flags3.insert(Flags3::AY_SOUND_EMU|Flags3::AY_FULLER_BOX);
Some(res)
}
else if let Some(res) = snapshot.ay_state(Ay3_891xDevice::Melodik) {
flags3.insert(Flags3::AY_SOUND_EMU);
Some(res)
}
else {
snapshot.ay_state(Ay3_891xDevice::Timex)
};
if let Some((ay_sel_reg, ay_regs)) = res {
head_ex.ay_sel_reg = ay_sel_reg.into();
head_ex.ay_regs = *ay_regs;
}
head_ex.port1 = match model {
Spectrum128|SpectrumPlus2|SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e|
SpectrumSE => snapshot.ula128_flags().bits(),
TimexTC2048|TimexTC2068|TimexTS2068 => snapshot.timex_memory_banks(),
_ => 0
};
head_ex.ifrom = match model {
TimexTC2048|TimexTS2068|TimexTC2068 => {
if ext.intersects(Extensions::IF1|Extensions::PLUS_D|Extensions::DISCIPLE) {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
snapshot.timex_flags().bits()
}
_ if ext.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in() => {
!0
}
_ => 0
};
head_ex.flags3 = flags3.bits();
Ok(())
}
sourcefn timex_memory_banks(&self) -> u8
fn timex_memory_banks(&self) -> u8
Examples found in repository?
src/z80/saver.rs (line 240)
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
fn init_z80_header_ex<S: SnapshotCreator, C: Cpu>(
head_ex: &mut HeaderEx,
cpu: C,
model: ComputerModel,
ext: Extensions,
snapshot: &S,
select_hw_model: HwModelSelector<S>,
result: &mut SnapshotResult
) -> Result<()>
{
use ComputerModel::*;
head_ex.pc = cpu.get_pc().to_le_bytes();
let (hw_mode, alt_hw) = select_hw_model(model, ext, snapshot, result)?;
let mut flags3 = Flags3::empty();
flags3.set(Flags3::ALT_HW_MODE, alt_hw);
head_ex.hw_mode = hw_mode;
let res = if let Some(res) = snapshot.ay_state(Ay3_891xDevice::Ay128k) {
if snapshot.ay_state(Ay3_891xDevice::FullerBox).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Melodik).is_some()
{
result.insert(SnapshotResult::SOUND_CHIP_NSUP);
}
Some(res)
}
else if let Some(res) = snapshot.ay_state(Ay3_891xDevice::FullerBox) {
flags3.insert(Flags3::AY_SOUND_EMU|Flags3::AY_FULLER_BOX);
Some(res)
}
else if let Some(res) = snapshot.ay_state(Ay3_891xDevice::Melodik) {
flags3.insert(Flags3::AY_SOUND_EMU);
Some(res)
}
else {
snapshot.ay_state(Ay3_891xDevice::Timex)
};
if let Some((ay_sel_reg, ay_regs)) = res {
head_ex.ay_sel_reg = ay_sel_reg.into();
head_ex.ay_regs = *ay_regs;
}
head_ex.port1 = match model {
Spectrum128|SpectrumPlus2|SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e|
SpectrumSE => snapshot.ula128_flags().bits(),
TimexTC2048|TimexTC2068|TimexTS2068 => snapshot.timex_memory_banks(),
_ => 0
};
head_ex.ifrom = match model {
TimexTC2048|TimexTS2068|TimexTC2068 => {
if ext.intersects(Extensions::IF1|Extensions::PLUS_D|Extensions::DISCIPLE) {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
snapshot.timex_flags().bits()
}
_ if ext.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in() => {
!0
}
_ => 0
};
head_ex.flags3 = flags3.bits();
Ok(())
}
sourcefn is_interface1_rom_paged_in(&self) -> bool
fn is_interface1_rom_paged_in(&self) -> bool
Examples found in repository?
src/z80/saver.rs (line 84)
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
fn select_hw_model_v2<S: SnapshotCreator>(
model: ComputerModel,
ext: Extensions,
snapshot: &S,
result: &mut SnapshotResult
) -> Result<(u8, bool)>
{
use ComputerModel::*;
if ext.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in()
|| ext.intersects(Extensions::DISCIPLE) && snapshot.is_disciple_rom_paged_in()
|| ext.intersects(Extensions::TR_DOS) && snapshot.is_tr_dos_rom_paged_in()
|| ext.contains(Extensions::IF1|Extensions::SAM_RAM) && snapshot.is_interface1_rom_paged_in()
{
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a version 2 snapshot with the external ROM paged in"))
}
if (ext&!(Extensions::IF1|Extensions::SAM_RAM)) != Extensions::NONE
|| ext.contains(Extensions::IF1|Extensions::SAM_RAM)
{
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
Ok(match model {
Spectrum16 if ext.intersects(Extensions::SAM_RAM) => (2, true),
Spectrum16 if ext.intersects(Extensions::IF1) => (1, true),
Spectrum16 => (0, true),
Spectrum48 if ext.intersects(Extensions::SAM_RAM) => (2, false),
Spectrum48 if ext.intersects(Extensions::IF1) => (1, false),
Spectrum48 => (0, false),
SpectrumNTSC => {
result.insert(SnapshotResult::MODEL_NSUP);
(if ext.intersects(Extensions::SAM_RAM) { 2 }
else { ext.intersects(Extensions::IF1) as u8 }, false)
}
Spectrum128 if ext.intersects(Extensions::IF1) => (4, false),
Spectrum128 => (3, false),
SpectrumPlus2 if ext.intersects(Extensions::IF1) => (4, true),
SpectrumPlus2 => (3, true),
SpectrumPlus2A => (7, true),
SpectrumPlus3 => (7, false),
SpectrumPlus3e => {
result.insert(SnapshotResult::MODEL_NSUP);
(7, false)
}
SpectrumSE => {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a snapshot of SpectrumSE"))
}
TimexTC2048|TimexTC2068|TimexTS2068 if ext.intersects(Extensions::IF1)
&& snapshot.is_interface1_rom_paged_in() => {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a snapshot of Timex + IF1 with IF1 ROM paged in"))
}
TimexTC2048 => (14, false),
TimexTC2068 => (15, false),
TimexTS2068 => (128, false),
})
}
fn select_hw_model_v3<S: SnapshotCreator>(
model: ComputerModel,
ext: Extensions,
snapshot: &S,
result: &mut SnapshotResult
) -> Result<(u8, bool)>
{
use ComputerModel::*;
if ext.intersects(Extensions::TR_DOS) && snapshot.is_tr_dos_rom_paged_in()
|| ext.contains(Extensions::IF1|Extensions::SAM_RAM) && snapshot.is_interface1_rom_paged_in()
{
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a version 3 snapshot with the external ROM paged in"))
}
if ext.contains(Extensions::IF1|Extensions::SAM_RAM) {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
Ok(match model {
Spectrum16 if ext.intersects(Extensions::SAM_RAM) => (2, true),
Spectrum16 if ext.intersects(Extensions::IF1) => (1, true),
Spectrum16 if ext.intersects(Extensions::PLUS_D|Extensions::DISCIPLE) => (3, true),
Spectrum16 => (0, true),
Spectrum48 if ext.intersects(Extensions::SAM_RAM) => (2, false),
Spectrum48 if ext.intersects(Extensions::IF1) => (1, false),
Spectrum48 if ext.intersects(Extensions::PLUS_D|Extensions::DISCIPLE) => (3, false),
Spectrum48 => (0, false),
SpectrumNTSC => {
result.insert(SnapshotResult::MODEL_NSUP);
(if ext.intersects(Extensions::SAM_RAM) { 2 }
else if ext.intersects(Extensions::IF1) { 1 }
else if ext.intersects(Extensions::PLUS_D|Extensions::DISCIPLE) { 3 }
else { 0 }, false)
}
Spectrum128 if ext.intersects(Extensions::IF1) => (5, false),
Spectrum128 if ext.intersects(Extensions::PLUS_D|Extensions::DISCIPLE) => (6, false),
Spectrum128 => (4, false),
SpectrumPlus2 if ext.intersects(Extensions::IF1) => (5, true),
SpectrumPlus2 if ext.intersects(Extensions::PLUS_D|Extensions::DISCIPLE) => (6, true),
SpectrumPlus2 => (4, true),
SpectrumPlus2A => (7, true),
SpectrumPlus3 => (7, false),
SpectrumPlus3e => {
result.insert(SnapshotResult::MODEL_NSUP);
(7, false)
}
SpectrumSE => {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a snapshot of SpectrumSE"))
}
TimexTC2048|TimexTC2068|TimexTS2068 if (ext.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in())
|| (ext.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in())
|| (ext.intersects(Extensions::DISCIPLE) && snapshot.is_disciple_rom_paged_in())
=> {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a snapshot of Timex with extension ROM paged in"))
}
TimexTC2048 => (14, false),
TimexTC2068 => (15, false),
TimexTS2068 => (128, false),
})
}
type HwModelSelector<S> = fn(
ComputerModel, Extensions, &S, &mut SnapshotResult
) -> Result<(u8, bool)>;
fn init_z80_header_ex<S: SnapshotCreator, C: Cpu>(
head_ex: &mut HeaderEx,
cpu: C,
model: ComputerModel,
ext: Extensions,
snapshot: &S,
select_hw_model: HwModelSelector<S>,
result: &mut SnapshotResult
) -> Result<()>
{
use ComputerModel::*;
head_ex.pc = cpu.get_pc().to_le_bytes();
let (hw_mode, alt_hw) = select_hw_model(model, ext, snapshot, result)?;
let mut flags3 = Flags3::empty();
flags3.set(Flags3::ALT_HW_MODE, alt_hw);
head_ex.hw_mode = hw_mode;
let res = if let Some(res) = snapshot.ay_state(Ay3_891xDevice::Ay128k) {
if snapshot.ay_state(Ay3_891xDevice::FullerBox).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Melodik).is_some()
{
result.insert(SnapshotResult::SOUND_CHIP_NSUP);
}
Some(res)
}
else if let Some(res) = snapshot.ay_state(Ay3_891xDevice::FullerBox) {
flags3.insert(Flags3::AY_SOUND_EMU|Flags3::AY_FULLER_BOX);
Some(res)
}
else if let Some(res) = snapshot.ay_state(Ay3_891xDevice::Melodik) {
flags3.insert(Flags3::AY_SOUND_EMU);
Some(res)
}
else {
snapshot.ay_state(Ay3_891xDevice::Timex)
};
if let Some((ay_sel_reg, ay_regs)) = res {
head_ex.ay_sel_reg = ay_sel_reg.into();
head_ex.ay_regs = *ay_regs;
}
head_ex.port1 = match model {
Spectrum128|SpectrumPlus2|SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e|
SpectrumSE => snapshot.ula128_flags().bits(),
TimexTC2048|TimexTC2068|TimexTS2068 => snapshot.timex_memory_banks(),
_ => 0
};
head_ex.ifrom = match model {
TimexTC2048|TimexTS2068|TimexTC2068 => {
if ext.intersects(Extensions::IF1|Extensions::PLUS_D|Extensions::DISCIPLE) {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
snapshot.timex_flags().bits()
}
_ if ext.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in() => {
!0
}
_ => 0
};
head_ex.flags3 = flags3.bits();
Ok(())
}
fn save_ram_pages<W: Write, S: SnapshotCreator, I: Iterator<Item=(u8, usize)>>(
mut wr: W,
snapshot: &S,
pages: I
) -> Result<()>
{
let mut buf = Vec::with_capacity(0x1000);
for (ptype, page) in pages {
buf.clear();
let mem_slice = snapshot.memory_ref(MemoryRange::Ram(page * PAGE_SIZE..(page + 1) * PAGE_SIZE))?;
compress_write_all(mem_slice, &mut buf)?;
let (mem_head, slice) = match buf.len().try_into() {
Ok(core::u16::MAX)|Err(..) => {
(MemoryHeader::new(core::u16::MAX, ptype), mem_slice)
}
Ok(length) => (MemoryHeader::new(length, ptype), &buf[..]),
};
mem_head.write_struct(wr.by_ref())?;
wr.write_all(slice)?;
}
wr.flush()
}
fn save_all_v2v3<W: Write, S: SnapshotCreator>(
version: Z80Version,
snapshot: &S,
model: ComputerModel,
header: &Header,
head_ex: &HeaderEx,
mut wr: W
) -> Result<()>
{
use ComputerModel::*;
header.write_struct(wr.by_ref())?;
let ex_len: u16 = match version {
Z80Version::V2 => 23,
Z80Version::V3 if head_ex.port2 != 0 => 55,
Z80Version::V3 => 54,
_ => unreachable!()
};
wr.write_all(&ex_len.to_le_bytes()[..])?;
head_ex.write_struct_with_limit(wr.by_ref(), ex_len as usize)?;
match model {
Spectrum16 => {
save_ram_pages(wr, snapshot, iter::once((8, 0)))
}
Spectrum48|SpectrumNTSC|TimexTC2048|TimexTS2068|TimexTC2068 => {
save_ram_pages(wr, snapshot,
[(8, 0), (4, 1), (5, 2)].iter().copied())
}
Spectrum128|SpectrumPlus2|SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e => {
save_ram_pages(wr, snapshot,
(0..8).map(|page| (page as u8 + 3, page))
)
}
_ => unreachable!()
}
}
fn get_nmos_cpu(cpu: CpuModel, result: &mut SnapshotResult) -> Z80NMOS {
match cpu {
CpuModel::NMOS(cpu) => cpu,
CpuModel::CMOS(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
},
CpuModel::BM1(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
}
}
}
/// Saves a **Z80** file version 1 into `wr` from the provided reference to a `snapshot` struct
/// implementing [SnapshotCreator].
///
/// # Errors
/// This function may return an error from attempts to write the file or if for some reason
/// a snapshot could not be created.
pub fn save_z80v1<C: SnapshotCreator, W: Write>(
snapshot: &C,
mut wr: W
) -> Result<SnapshotResult>
{
use ComputerModel::*;
let mut result = SnapshotResult::OK;
let model = snapshot.model();
match model {
Spectrum48 => {},
Spectrum16|SpectrumNTSC|TimexTC2048 => {
result.insert(SnapshotResult::MODEL_NSUP);
}
_ => return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a version 1 snapshot of this computer model"))
};
let extensions = snapshot.extensions();
if let Err(bad_ext) = model.validate_extensions(extensions) {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
format!("Z80: the model {} can't be saved with {}", model, bad_ext)))
}
if extensions.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in()
|| extensions.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in()
|| extensions.intersects(Extensions::DISCIPLE) && snapshot.is_disciple_rom_paged_in()
|| extensions.intersects(Extensions::TR_DOS) && snapshot.is_tr_dos_rom_paged_in()
{
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a version 1 snapshot with the external ROM paged in"))
}
if extensions != Extensions::NONE {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
let cpu = get_nmos_cpu(snapshot.cpu(), &mut result);
let border = snapshot.border_color();
let issue = snapshot.issue();
let joystick = snapshot.joystick();
if snapshot.ay_state(Ay3_891xDevice::Ay128k).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Melodik).is_some()
|| snapshot.ay_state(Ay3_891xDevice::FullerBox).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Timex).is_some()
{
result.insert(SnapshotResult::SOUND_CHIP_NSUP);
}
let mut header = Header::default();
init_z80_header(
&mut header,
Z80Version::V1,
&cpu,
border,
issue,
joystick,
&mut result
);
header.write_struct(wr.by_ref())?;
let is_16k = model == ComputerModel::Spectrum16;
let ramend = if is_16k { 0x4000 } else { 0xC000 };
let mem_slice = snapshot.memory_ref(MemoryRange::Ram(0..ramend))?;
compress_write_all(mem_slice, wr.by_ref())?;
if is_16k {
compress_repeat_write_all(!0, 0x8000, wr.by_ref())?;
}
wr.write_all(MEMORY_V1_TERM)?;
wr.flush()?;
Ok(result)
}
More examples
src/sna.rs (line 323)
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
pub fn save_sna<C: SnapshotCreator, W: Write>(
snapshot: &C,
mut wr: W
) -> Result<SnapshotResult>
{
use ComputerModel::*;
let mut result = SnapshotResult::KEYB_ISSUE_NSUP;
let model = snapshot.model();
let is_128 = match model {
Spectrum48 => false,
Spectrum128 => true,
SpectrumPlus2|SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e|SpectrumSE => {
result.insert(SnapshotResult::MODEL_NSUP);
true
}
Spectrum16|SpectrumNTSC|TimexTC2048|TimexTC2068|TimexTS2068 => {
result.insert(SnapshotResult::MODEL_NSUP);
false
}
};
let extensions = snapshot.extensions();
if extensions.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in()
|| extensions.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in()
{
return Err(Error::new(ErrorKind::InvalidInput,
"SNA: can't create a snapshot with the external ROM paged in"))
}
if extensions != Extensions::NONE && extensions != Extensions::TR_DOS {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
let cpu = match snapshot.cpu() {
CpuModel::NMOS(cpu) => cpu,
CpuModel::CMOS(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
},
CpuModel::BM1(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
}
};
if !is_cpu_safe_for_snapshot(&cpu) {
return Err(Error::new(ErrorKind::InvalidInput, "SNA: can't safely snapshot the CPU state"))
}
let mut sna = make_header(&cpu);
sna.border = snapshot.border_color().into();
if snapshot.joystick().is_some() {
result.insert(SnapshotResult::JOYSTICK_NSUP);
}
if is_128 || snapshot.ay_state(Ay3_891xDevice::Melodik).is_some()
|| snapshot.ay_state(Ay3_891xDevice::FullerBox).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Timex).is_some() {
result.insert(SnapshotResult::SOUND_CHIP_NSUP);
}
if !is_128 {
return save_sna48(snapshot, cpu, model == ComputerModel::Spectrum16, sna, wr, result)
}
let memflags = snapshot.ula128_flags();
let mut sna_ext = SnaHeader128 {
pc: cpu.get_pc().to_le_bytes(),
port_data: memflags.bits(),
..Default::default()
};
if extensions.intersects(Extensions::TR_DOS) {
sna_ext.trdos_rom = snapshot.is_tr_dos_rom_paged_in().into();
}
sna.write_struct(wr.by_ref())?;
let last_page: usize = memflags.last_ram_page_bank();
let index48 = [5,2,last_page];
for page in index48.iter() {
wr.write_all(
snapshot.memory_ref(MemoryRange::Ram(page * PAGE_SIZE..(page + 1) * PAGE_SIZE))?
)?;
}
sna_ext.write_struct(wr.by_ref())?;
for page in (0..8).filter(|n| !index48.contains(n) && *n != last_page) {
wr.write_all(
snapshot.memory_ref(MemoryRange::Ram(page * PAGE_SIZE..(page + 1) * PAGE_SIZE))?
)?;
}
wr.flush()?;
Ok(result)
}
sourcefn is_plus_d_rom_paged_in(&self) -> bool
fn is_plus_d_rom_paged_in(&self) -> bool
Examples found in repository?
src/z80/saver.rs (line 81)
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
fn select_hw_model_v2<S: SnapshotCreator>(
model: ComputerModel,
ext: Extensions,
snapshot: &S,
result: &mut SnapshotResult
) -> Result<(u8, bool)>
{
use ComputerModel::*;
if ext.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in()
|| ext.intersects(Extensions::DISCIPLE) && snapshot.is_disciple_rom_paged_in()
|| ext.intersects(Extensions::TR_DOS) && snapshot.is_tr_dos_rom_paged_in()
|| ext.contains(Extensions::IF1|Extensions::SAM_RAM) && snapshot.is_interface1_rom_paged_in()
{
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a version 2 snapshot with the external ROM paged in"))
}
if (ext&!(Extensions::IF1|Extensions::SAM_RAM)) != Extensions::NONE
|| ext.contains(Extensions::IF1|Extensions::SAM_RAM)
{
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
Ok(match model {
Spectrum16 if ext.intersects(Extensions::SAM_RAM) => (2, true),
Spectrum16 if ext.intersects(Extensions::IF1) => (1, true),
Spectrum16 => (0, true),
Spectrum48 if ext.intersects(Extensions::SAM_RAM) => (2, false),
Spectrum48 if ext.intersects(Extensions::IF1) => (1, false),
Spectrum48 => (0, false),
SpectrumNTSC => {
result.insert(SnapshotResult::MODEL_NSUP);
(if ext.intersects(Extensions::SAM_RAM) { 2 }
else { ext.intersects(Extensions::IF1) as u8 }, false)
}
Spectrum128 if ext.intersects(Extensions::IF1) => (4, false),
Spectrum128 => (3, false),
SpectrumPlus2 if ext.intersects(Extensions::IF1) => (4, true),
SpectrumPlus2 => (3, true),
SpectrumPlus2A => (7, true),
SpectrumPlus3 => (7, false),
SpectrumPlus3e => {
result.insert(SnapshotResult::MODEL_NSUP);
(7, false)
}
SpectrumSE => {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a snapshot of SpectrumSE"))
}
TimexTC2048|TimexTC2068|TimexTS2068 if ext.intersects(Extensions::IF1)
&& snapshot.is_interface1_rom_paged_in() => {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a snapshot of Timex + IF1 with IF1 ROM paged in"))
}
TimexTC2048 => (14, false),
TimexTC2068 => (15, false),
TimexTS2068 => (128, false),
})
}
fn select_hw_model_v3<S: SnapshotCreator>(
model: ComputerModel,
ext: Extensions,
snapshot: &S,
result: &mut SnapshotResult
) -> Result<(u8, bool)>
{
use ComputerModel::*;
if ext.intersects(Extensions::TR_DOS) && snapshot.is_tr_dos_rom_paged_in()
|| ext.contains(Extensions::IF1|Extensions::SAM_RAM) && snapshot.is_interface1_rom_paged_in()
{
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a version 3 snapshot with the external ROM paged in"))
}
if ext.contains(Extensions::IF1|Extensions::SAM_RAM) {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
Ok(match model {
Spectrum16 if ext.intersects(Extensions::SAM_RAM) => (2, true),
Spectrum16 if ext.intersects(Extensions::IF1) => (1, true),
Spectrum16 if ext.intersects(Extensions::PLUS_D|Extensions::DISCIPLE) => (3, true),
Spectrum16 => (0, true),
Spectrum48 if ext.intersects(Extensions::SAM_RAM) => (2, false),
Spectrum48 if ext.intersects(Extensions::IF1) => (1, false),
Spectrum48 if ext.intersects(Extensions::PLUS_D|Extensions::DISCIPLE) => (3, false),
Spectrum48 => (0, false),
SpectrumNTSC => {
result.insert(SnapshotResult::MODEL_NSUP);
(if ext.intersects(Extensions::SAM_RAM) { 2 }
else if ext.intersects(Extensions::IF1) { 1 }
else if ext.intersects(Extensions::PLUS_D|Extensions::DISCIPLE) { 3 }
else { 0 }, false)
}
Spectrum128 if ext.intersects(Extensions::IF1) => (5, false),
Spectrum128 if ext.intersects(Extensions::PLUS_D|Extensions::DISCIPLE) => (6, false),
Spectrum128 => (4, false),
SpectrumPlus2 if ext.intersects(Extensions::IF1) => (5, true),
SpectrumPlus2 if ext.intersects(Extensions::PLUS_D|Extensions::DISCIPLE) => (6, true),
SpectrumPlus2 => (4, true),
SpectrumPlus2A => (7, true),
SpectrumPlus3 => (7, false),
SpectrumPlus3e => {
result.insert(SnapshotResult::MODEL_NSUP);
(7, false)
}
SpectrumSE => {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a snapshot of SpectrumSE"))
}
TimexTC2048|TimexTC2068|TimexTS2068 if (ext.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in())
|| (ext.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in())
|| (ext.intersects(Extensions::DISCIPLE) && snapshot.is_disciple_rom_paged_in())
=> {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a snapshot of Timex with extension ROM paged in"))
}
TimexTC2048 => (14, false),
TimexTC2068 => (15, false),
TimexTS2068 => (128, false),
})
}
type HwModelSelector<S> = fn(
ComputerModel, Extensions, &S, &mut SnapshotResult
) -> Result<(u8, bool)>;
fn init_z80_header_ex<S: SnapshotCreator, C: Cpu>(
head_ex: &mut HeaderEx,
cpu: C,
model: ComputerModel,
ext: Extensions,
snapshot: &S,
select_hw_model: HwModelSelector<S>,
result: &mut SnapshotResult
) -> Result<()>
{
use ComputerModel::*;
head_ex.pc = cpu.get_pc().to_le_bytes();
let (hw_mode, alt_hw) = select_hw_model(model, ext, snapshot, result)?;
let mut flags3 = Flags3::empty();
flags3.set(Flags3::ALT_HW_MODE, alt_hw);
head_ex.hw_mode = hw_mode;
let res = if let Some(res) = snapshot.ay_state(Ay3_891xDevice::Ay128k) {
if snapshot.ay_state(Ay3_891xDevice::FullerBox).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Melodik).is_some()
{
result.insert(SnapshotResult::SOUND_CHIP_NSUP);
}
Some(res)
}
else if let Some(res) = snapshot.ay_state(Ay3_891xDevice::FullerBox) {
flags3.insert(Flags3::AY_SOUND_EMU|Flags3::AY_FULLER_BOX);
Some(res)
}
else if let Some(res) = snapshot.ay_state(Ay3_891xDevice::Melodik) {
flags3.insert(Flags3::AY_SOUND_EMU);
Some(res)
}
else {
snapshot.ay_state(Ay3_891xDevice::Timex)
};
if let Some((ay_sel_reg, ay_regs)) = res {
head_ex.ay_sel_reg = ay_sel_reg.into();
head_ex.ay_regs = *ay_regs;
}
head_ex.port1 = match model {
Spectrum128|SpectrumPlus2|SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e|
SpectrumSE => snapshot.ula128_flags().bits(),
TimexTC2048|TimexTC2068|TimexTS2068 => snapshot.timex_memory_banks(),
_ => 0
};
head_ex.ifrom = match model {
TimexTC2048|TimexTS2068|TimexTC2068 => {
if ext.intersects(Extensions::IF1|Extensions::PLUS_D|Extensions::DISCIPLE) {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
snapshot.timex_flags().bits()
}
_ if ext.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in() => {
!0
}
_ => 0
};
head_ex.flags3 = flags3.bits();
Ok(())
}
fn save_ram_pages<W: Write, S: SnapshotCreator, I: Iterator<Item=(u8, usize)>>(
mut wr: W,
snapshot: &S,
pages: I
) -> Result<()>
{
let mut buf = Vec::with_capacity(0x1000);
for (ptype, page) in pages {
buf.clear();
let mem_slice = snapshot.memory_ref(MemoryRange::Ram(page * PAGE_SIZE..(page + 1) * PAGE_SIZE))?;
compress_write_all(mem_slice, &mut buf)?;
let (mem_head, slice) = match buf.len().try_into() {
Ok(core::u16::MAX)|Err(..) => {
(MemoryHeader::new(core::u16::MAX, ptype), mem_slice)
}
Ok(length) => (MemoryHeader::new(length, ptype), &buf[..]),
};
mem_head.write_struct(wr.by_ref())?;
wr.write_all(slice)?;
}
wr.flush()
}
fn save_all_v2v3<W: Write, S: SnapshotCreator>(
version: Z80Version,
snapshot: &S,
model: ComputerModel,
header: &Header,
head_ex: &HeaderEx,
mut wr: W
) -> Result<()>
{
use ComputerModel::*;
header.write_struct(wr.by_ref())?;
let ex_len: u16 = match version {
Z80Version::V2 => 23,
Z80Version::V3 if head_ex.port2 != 0 => 55,
Z80Version::V3 => 54,
_ => unreachable!()
};
wr.write_all(&ex_len.to_le_bytes()[..])?;
head_ex.write_struct_with_limit(wr.by_ref(), ex_len as usize)?;
match model {
Spectrum16 => {
save_ram_pages(wr, snapshot, iter::once((8, 0)))
}
Spectrum48|SpectrumNTSC|TimexTC2048|TimexTS2068|TimexTC2068 => {
save_ram_pages(wr, snapshot,
[(8, 0), (4, 1), (5, 2)].iter().copied())
}
Spectrum128|SpectrumPlus2|SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e => {
save_ram_pages(wr, snapshot,
(0..8).map(|page| (page as u8 + 3, page))
)
}
_ => unreachable!()
}
}
fn get_nmos_cpu(cpu: CpuModel, result: &mut SnapshotResult) -> Z80NMOS {
match cpu {
CpuModel::NMOS(cpu) => cpu,
CpuModel::CMOS(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
},
CpuModel::BM1(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
}
}
}
/// Saves a **Z80** file version 1 into `wr` from the provided reference to a `snapshot` struct
/// implementing [SnapshotCreator].
///
/// # Errors
/// This function may return an error from attempts to write the file or if for some reason
/// a snapshot could not be created.
pub fn save_z80v1<C: SnapshotCreator, W: Write>(
snapshot: &C,
mut wr: W
) -> Result<SnapshotResult>
{
use ComputerModel::*;
let mut result = SnapshotResult::OK;
let model = snapshot.model();
match model {
Spectrum48 => {},
Spectrum16|SpectrumNTSC|TimexTC2048 => {
result.insert(SnapshotResult::MODEL_NSUP);
}
_ => return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a version 1 snapshot of this computer model"))
};
let extensions = snapshot.extensions();
if let Err(bad_ext) = model.validate_extensions(extensions) {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
format!("Z80: the model {} can't be saved with {}", model, bad_ext)))
}
if extensions.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in()
|| extensions.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in()
|| extensions.intersects(Extensions::DISCIPLE) && snapshot.is_disciple_rom_paged_in()
|| extensions.intersects(Extensions::TR_DOS) && snapshot.is_tr_dos_rom_paged_in()
{
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a version 1 snapshot with the external ROM paged in"))
}
if extensions != Extensions::NONE {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
let cpu = get_nmos_cpu(snapshot.cpu(), &mut result);
let border = snapshot.border_color();
let issue = snapshot.issue();
let joystick = snapshot.joystick();
if snapshot.ay_state(Ay3_891xDevice::Ay128k).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Melodik).is_some()
|| snapshot.ay_state(Ay3_891xDevice::FullerBox).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Timex).is_some()
{
result.insert(SnapshotResult::SOUND_CHIP_NSUP);
}
let mut header = Header::default();
init_z80_header(
&mut header,
Z80Version::V1,
&cpu,
border,
issue,
joystick,
&mut result
);
header.write_struct(wr.by_ref())?;
let is_16k = model == ComputerModel::Spectrum16;
let ramend = if is_16k { 0x4000 } else { 0xC000 };
let mem_slice = snapshot.memory_ref(MemoryRange::Ram(0..ramend))?;
compress_write_all(mem_slice, wr.by_ref())?;
if is_16k {
compress_repeat_write_all(!0, 0x8000, wr.by_ref())?;
}
wr.write_all(MEMORY_V1_TERM)?;
wr.flush()?;
Ok(result)
}
/// Saves a **Z80** file version 2 into `wr` from the provided reference to a `snapshot` struct
/// implementing [SnapshotCreator].
///
/// # Errors
/// This function may return an error from attempts to write the file or if for some reason
/// a snapshot could not be created.
pub fn save_z80v2<C: SnapshotCreator, W: Write>(
snapshot: &C,
wr: W
) -> Result<SnapshotResult>
{
let mut result = SnapshotResult::OK;
let model = snapshot.model();
let ext = snapshot.extensions();
let border = snapshot.border_color();
let issue = snapshot.issue();
let joystick = snapshot.joystick();
let cpu = get_nmos_cpu(snapshot.cpu(), &mut result);
if !is_cpu_safe_for_snapshot(&cpu) {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "Z80: can't safely snapshot the CPU state"))
}
if let Err(bad_ext) = model.validate_extensions(ext) {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
format!("Z80: the model {} can't be saved with {}", model, bad_ext)))
}
let mut header = Header::default();
init_z80_header(
&mut header,
Z80Version::V2,
&cpu,
border,
issue,
joystick,
&mut result
);
let mut head_ex = HeaderEx::default();
init_z80_header_ex(
&mut head_ex,
cpu,
model,
ext,
snapshot,
select_hw_model_v2,
&mut result
)?;
save_all_v2v3(Z80Version::V2, snapshot, model, &header, &head_ex, wr)?;
Ok(result)
}
/// Saves a **Z80** file version 3 into `wr` from the provided reference to a `snapshot` struct
/// implementing [SnapshotCreator].
///
/// # Errors
/// This function may return an error from attempts to write the file or if for some reason
/// a snapshot could not be created.
pub fn save_z80v3<C: SnapshotCreator, W: Write>(
snapshot: &C,
wr: W
) -> Result<SnapshotResult>
{
use ComputerModel::*;
let mut result = SnapshotResult::OK;
let model = snapshot.model();
let ext = snapshot.extensions();
let border = snapshot.border_color();
let issue = snapshot.issue();
let joystick = snapshot.joystick();
let cpu = get_nmos_cpu(snapshot.cpu(), &mut result);
if !is_cpu_safe_for_snapshot(&cpu) {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "Z80: can't safely snapshot the CPU state"))
}
if let Err(bad_ext) = model.validate_extensions(ext) {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
format!("Z80: the model {} can't be saved with {}", model, bad_ext)))
}
let mut header = Header::default();
init_z80_header(
&mut header,
Z80Version::V3,
&cpu,
border,
issue,
joystick,
&mut result
);
let mut head_ex = HeaderEx::default();
init_z80_header_ex(
&mut head_ex,
cpu,
model,
ext,
snapshot,
select_hw_model_v3,
&mut result
)?;
let (ts_lo, ts_hi) = cycles_to_z80(snapshot.current_clock(), model);
head_ex.ts_lo = ts_lo.to_le_bytes();
head_ex.ts_hi = ts_hi;
if (ext.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in())
|| (ext.intersects(Extensions::DISCIPLE) && snapshot.is_disciple_rom_paged_in())
{
head_ex.mgt_rom = !0;
}
if let Some(JoystickModel::Sinclair2) = joystick {
head_ex.joy_bindings = [ 3, 1, 3, 2, 3, 4, 3, 8, 3,10];
head_ex.joy_ascii = [31, 0,32, 0,33, 0,34, 0,35, 0];
}
match model {
SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e => {
head_ex.port2 = snapshot.ula3_flags().bits();
}
_ => {
head_ex.fn1 = !0;
if !(ext.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in()) {
head_ex.fn2 = !0;
}
}
}
if ext.intersects(Extensions::PLUS_D) {
head_ex.mgt_type = 16;
}
// head_ex.flags4 = 0;
// head_ex.disciple1 = 0;
// head_ex.disciple2 = 0;
save_all_v2v3(Z80Version::V3, snapshot, model, &header, &head_ex, wr)?;
Ok(result)
}
More examples
src/sna.rs (line 324)
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
pub fn save_sna<C: SnapshotCreator, W: Write>(
snapshot: &C,
mut wr: W
) -> Result<SnapshotResult>
{
use ComputerModel::*;
let mut result = SnapshotResult::KEYB_ISSUE_NSUP;
let model = snapshot.model();
let is_128 = match model {
Spectrum48 => false,
Spectrum128 => true,
SpectrumPlus2|SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e|SpectrumSE => {
result.insert(SnapshotResult::MODEL_NSUP);
true
}
Spectrum16|SpectrumNTSC|TimexTC2048|TimexTC2068|TimexTS2068 => {
result.insert(SnapshotResult::MODEL_NSUP);
false
}
};
let extensions = snapshot.extensions();
if extensions.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in()
|| extensions.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in()
{
return Err(Error::new(ErrorKind::InvalidInput,
"SNA: can't create a snapshot with the external ROM paged in"))
}
if extensions != Extensions::NONE && extensions != Extensions::TR_DOS {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
let cpu = match snapshot.cpu() {
CpuModel::NMOS(cpu) => cpu,
CpuModel::CMOS(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
},
CpuModel::BM1(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
}
};
if !is_cpu_safe_for_snapshot(&cpu) {
return Err(Error::new(ErrorKind::InvalidInput, "SNA: can't safely snapshot the CPU state"))
}
let mut sna = make_header(&cpu);
sna.border = snapshot.border_color().into();
if snapshot.joystick().is_some() {
result.insert(SnapshotResult::JOYSTICK_NSUP);
}
if is_128 || snapshot.ay_state(Ay3_891xDevice::Melodik).is_some()
|| snapshot.ay_state(Ay3_891xDevice::FullerBox).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Timex).is_some() {
result.insert(SnapshotResult::SOUND_CHIP_NSUP);
}
if !is_128 {
return save_sna48(snapshot, cpu, model == ComputerModel::Spectrum16, sna, wr, result)
}
let memflags = snapshot.ula128_flags();
let mut sna_ext = SnaHeader128 {
pc: cpu.get_pc().to_le_bytes(),
port_data: memflags.bits(),
..Default::default()
};
if extensions.intersects(Extensions::TR_DOS) {
sna_ext.trdos_rom = snapshot.is_tr_dos_rom_paged_in().into();
}
sna.write_struct(wr.by_ref())?;
let last_page: usize = memflags.last_ram_page_bank();
let index48 = [5,2,last_page];
for page in index48.iter() {
wr.write_all(
snapshot.memory_ref(MemoryRange::Ram(page * PAGE_SIZE..(page + 1) * PAGE_SIZE))?
)?;
}
sna_ext.write_struct(wr.by_ref())?;
for page in (0..8).filter(|n| !index48.contains(n) && *n != last_page) {
wr.write_all(
snapshot.memory_ref(MemoryRange::Ram(page * PAGE_SIZE..(page + 1) * PAGE_SIZE))?
)?;
}
wr.flush()?;
Ok(result)
}
sourcefn is_disciple_rom_paged_in(&self) -> bool
fn is_disciple_rom_paged_in(&self) -> bool
Examples found in repository?
src/z80/saver.rs (line 82)
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
fn select_hw_model_v2<S: SnapshotCreator>(
model: ComputerModel,
ext: Extensions,
snapshot: &S,
result: &mut SnapshotResult
) -> Result<(u8, bool)>
{
use ComputerModel::*;
if ext.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in()
|| ext.intersects(Extensions::DISCIPLE) && snapshot.is_disciple_rom_paged_in()
|| ext.intersects(Extensions::TR_DOS) && snapshot.is_tr_dos_rom_paged_in()
|| ext.contains(Extensions::IF1|Extensions::SAM_RAM) && snapshot.is_interface1_rom_paged_in()
{
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a version 2 snapshot with the external ROM paged in"))
}
if (ext&!(Extensions::IF1|Extensions::SAM_RAM)) != Extensions::NONE
|| ext.contains(Extensions::IF1|Extensions::SAM_RAM)
{
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
Ok(match model {
Spectrum16 if ext.intersects(Extensions::SAM_RAM) => (2, true),
Spectrum16 if ext.intersects(Extensions::IF1) => (1, true),
Spectrum16 => (0, true),
Spectrum48 if ext.intersects(Extensions::SAM_RAM) => (2, false),
Spectrum48 if ext.intersects(Extensions::IF1) => (1, false),
Spectrum48 => (0, false),
SpectrumNTSC => {
result.insert(SnapshotResult::MODEL_NSUP);
(if ext.intersects(Extensions::SAM_RAM) { 2 }
else { ext.intersects(Extensions::IF1) as u8 }, false)
}
Spectrum128 if ext.intersects(Extensions::IF1) => (4, false),
Spectrum128 => (3, false),
SpectrumPlus2 if ext.intersects(Extensions::IF1) => (4, true),
SpectrumPlus2 => (3, true),
SpectrumPlus2A => (7, true),
SpectrumPlus3 => (7, false),
SpectrumPlus3e => {
result.insert(SnapshotResult::MODEL_NSUP);
(7, false)
}
SpectrumSE => {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a snapshot of SpectrumSE"))
}
TimexTC2048|TimexTC2068|TimexTS2068 if ext.intersects(Extensions::IF1)
&& snapshot.is_interface1_rom_paged_in() => {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a snapshot of Timex + IF1 with IF1 ROM paged in"))
}
TimexTC2048 => (14, false),
TimexTC2068 => (15, false),
TimexTS2068 => (128, false),
})
}
fn select_hw_model_v3<S: SnapshotCreator>(
model: ComputerModel,
ext: Extensions,
snapshot: &S,
result: &mut SnapshotResult
) -> Result<(u8, bool)>
{
use ComputerModel::*;
if ext.intersects(Extensions::TR_DOS) && snapshot.is_tr_dos_rom_paged_in()
|| ext.contains(Extensions::IF1|Extensions::SAM_RAM) && snapshot.is_interface1_rom_paged_in()
{
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a version 3 snapshot with the external ROM paged in"))
}
if ext.contains(Extensions::IF1|Extensions::SAM_RAM) {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
Ok(match model {
Spectrum16 if ext.intersects(Extensions::SAM_RAM) => (2, true),
Spectrum16 if ext.intersects(Extensions::IF1) => (1, true),
Spectrum16 if ext.intersects(Extensions::PLUS_D|Extensions::DISCIPLE) => (3, true),
Spectrum16 => (0, true),
Spectrum48 if ext.intersects(Extensions::SAM_RAM) => (2, false),
Spectrum48 if ext.intersects(Extensions::IF1) => (1, false),
Spectrum48 if ext.intersects(Extensions::PLUS_D|Extensions::DISCIPLE) => (3, false),
Spectrum48 => (0, false),
SpectrumNTSC => {
result.insert(SnapshotResult::MODEL_NSUP);
(if ext.intersects(Extensions::SAM_RAM) { 2 }
else if ext.intersects(Extensions::IF1) { 1 }
else if ext.intersects(Extensions::PLUS_D|Extensions::DISCIPLE) { 3 }
else { 0 }, false)
}
Spectrum128 if ext.intersects(Extensions::IF1) => (5, false),
Spectrum128 if ext.intersects(Extensions::PLUS_D|Extensions::DISCIPLE) => (6, false),
Spectrum128 => (4, false),
SpectrumPlus2 if ext.intersects(Extensions::IF1) => (5, true),
SpectrumPlus2 if ext.intersects(Extensions::PLUS_D|Extensions::DISCIPLE) => (6, true),
SpectrumPlus2 => (4, true),
SpectrumPlus2A => (7, true),
SpectrumPlus3 => (7, false),
SpectrumPlus3e => {
result.insert(SnapshotResult::MODEL_NSUP);
(7, false)
}
SpectrumSE => {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a snapshot of SpectrumSE"))
}
TimexTC2048|TimexTC2068|TimexTS2068 if (ext.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in())
|| (ext.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in())
|| (ext.intersects(Extensions::DISCIPLE) && snapshot.is_disciple_rom_paged_in())
=> {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a snapshot of Timex with extension ROM paged in"))
}
TimexTC2048 => (14, false),
TimexTC2068 => (15, false),
TimexTS2068 => (128, false),
})
}
type HwModelSelector<S> = fn(
ComputerModel, Extensions, &S, &mut SnapshotResult
) -> Result<(u8, bool)>;
fn init_z80_header_ex<S: SnapshotCreator, C: Cpu>(
head_ex: &mut HeaderEx,
cpu: C,
model: ComputerModel,
ext: Extensions,
snapshot: &S,
select_hw_model: HwModelSelector<S>,
result: &mut SnapshotResult
) -> Result<()>
{
use ComputerModel::*;
head_ex.pc = cpu.get_pc().to_le_bytes();
let (hw_mode, alt_hw) = select_hw_model(model, ext, snapshot, result)?;
let mut flags3 = Flags3::empty();
flags3.set(Flags3::ALT_HW_MODE, alt_hw);
head_ex.hw_mode = hw_mode;
let res = if let Some(res) = snapshot.ay_state(Ay3_891xDevice::Ay128k) {
if snapshot.ay_state(Ay3_891xDevice::FullerBox).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Melodik).is_some()
{
result.insert(SnapshotResult::SOUND_CHIP_NSUP);
}
Some(res)
}
else if let Some(res) = snapshot.ay_state(Ay3_891xDevice::FullerBox) {
flags3.insert(Flags3::AY_SOUND_EMU|Flags3::AY_FULLER_BOX);
Some(res)
}
else if let Some(res) = snapshot.ay_state(Ay3_891xDevice::Melodik) {
flags3.insert(Flags3::AY_SOUND_EMU);
Some(res)
}
else {
snapshot.ay_state(Ay3_891xDevice::Timex)
};
if let Some((ay_sel_reg, ay_regs)) = res {
head_ex.ay_sel_reg = ay_sel_reg.into();
head_ex.ay_regs = *ay_regs;
}
head_ex.port1 = match model {
Spectrum128|SpectrumPlus2|SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e|
SpectrumSE => snapshot.ula128_flags().bits(),
TimexTC2048|TimexTC2068|TimexTS2068 => snapshot.timex_memory_banks(),
_ => 0
};
head_ex.ifrom = match model {
TimexTC2048|TimexTS2068|TimexTC2068 => {
if ext.intersects(Extensions::IF1|Extensions::PLUS_D|Extensions::DISCIPLE) {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
snapshot.timex_flags().bits()
}
_ if ext.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in() => {
!0
}
_ => 0
};
head_ex.flags3 = flags3.bits();
Ok(())
}
fn save_ram_pages<W: Write, S: SnapshotCreator, I: Iterator<Item=(u8, usize)>>(
mut wr: W,
snapshot: &S,
pages: I
) -> Result<()>
{
let mut buf = Vec::with_capacity(0x1000);
for (ptype, page) in pages {
buf.clear();
let mem_slice = snapshot.memory_ref(MemoryRange::Ram(page * PAGE_SIZE..(page + 1) * PAGE_SIZE))?;
compress_write_all(mem_slice, &mut buf)?;
let (mem_head, slice) = match buf.len().try_into() {
Ok(core::u16::MAX)|Err(..) => {
(MemoryHeader::new(core::u16::MAX, ptype), mem_slice)
}
Ok(length) => (MemoryHeader::new(length, ptype), &buf[..]),
};
mem_head.write_struct(wr.by_ref())?;
wr.write_all(slice)?;
}
wr.flush()
}
fn save_all_v2v3<W: Write, S: SnapshotCreator>(
version: Z80Version,
snapshot: &S,
model: ComputerModel,
header: &Header,
head_ex: &HeaderEx,
mut wr: W
) -> Result<()>
{
use ComputerModel::*;
header.write_struct(wr.by_ref())?;
let ex_len: u16 = match version {
Z80Version::V2 => 23,
Z80Version::V3 if head_ex.port2 != 0 => 55,
Z80Version::V3 => 54,
_ => unreachable!()
};
wr.write_all(&ex_len.to_le_bytes()[..])?;
head_ex.write_struct_with_limit(wr.by_ref(), ex_len as usize)?;
match model {
Spectrum16 => {
save_ram_pages(wr, snapshot, iter::once((8, 0)))
}
Spectrum48|SpectrumNTSC|TimexTC2048|TimexTS2068|TimexTC2068 => {
save_ram_pages(wr, snapshot,
[(8, 0), (4, 1), (5, 2)].iter().copied())
}
Spectrum128|SpectrumPlus2|SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e => {
save_ram_pages(wr, snapshot,
(0..8).map(|page| (page as u8 + 3, page))
)
}
_ => unreachable!()
}
}
fn get_nmos_cpu(cpu: CpuModel, result: &mut SnapshotResult) -> Z80NMOS {
match cpu {
CpuModel::NMOS(cpu) => cpu,
CpuModel::CMOS(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
},
CpuModel::BM1(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
}
}
}
/// Saves a **Z80** file version 1 into `wr` from the provided reference to a `snapshot` struct
/// implementing [SnapshotCreator].
///
/// # Errors
/// This function may return an error from attempts to write the file or if for some reason
/// a snapshot could not be created.
pub fn save_z80v1<C: SnapshotCreator, W: Write>(
snapshot: &C,
mut wr: W
) -> Result<SnapshotResult>
{
use ComputerModel::*;
let mut result = SnapshotResult::OK;
let model = snapshot.model();
match model {
Spectrum48 => {},
Spectrum16|SpectrumNTSC|TimexTC2048 => {
result.insert(SnapshotResult::MODEL_NSUP);
}
_ => return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a version 1 snapshot of this computer model"))
};
let extensions = snapshot.extensions();
if let Err(bad_ext) = model.validate_extensions(extensions) {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
format!("Z80: the model {} can't be saved with {}", model, bad_ext)))
}
if extensions.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in()
|| extensions.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in()
|| extensions.intersects(Extensions::DISCIPLE) && snapshot.is_disciple_rom_paged_in()
|| extensions.intersects(Extensions::TR_DOS) && snapshot.is_tr_dos_rom_paged_in()
{
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a version 1 snapshot with the external ROM paged in"))
}
if extensions != Extensions::NONE {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
let cpu = get_nmos_cpu(snapshot.cpu(), &mut result);
let border = snapshot.border_color();
let issue = snapshot.issue();
let joystick = snapshot.joystick();
if snapshot.ay_state(Ay3_891xDevice::Ay128k).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Melodik).is_some()
|| snapshot.ay_state(Ay3_891xDevice::FullerBox).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Timex).is_some()
{
result.insert(SnapshotResult::SOUND_CHIP_NSUP);
}
let mut header = Header::default();
init_z80_header(
&mut header,
Z80Version::V1,
&cpu,
border,
issue,
joystick,
&mut result
);
header.write_struct(wr.by_ref())?;
let is_16k = model == ComputerModel::Spectrum16;
let ramend = if is_16k { 0x4000 } else { 0xC000 };
let mem_slice = snapshot.memory_ref(MemoryRange::Ram(0..ramend))?;
compress_write_all(mem_slice, wr.by_ref())?;
if is_16k {
compress_repeat_write_all(!0, 0x8000, wr.by_ref())?;
}
wr.write_all(MEMORY_V1_TERM)?;
wr.flush()?;
Ok(result)
}
/// Saves a **Z80** file version 2 into `wr` from the provided reference to a `snapshot` struct
/// implementing [SnapshotCreator].
///
/// # Errors
/// This function may return an error from attempts to write the file or if for some reason
/// a snapshot could not be created.
pub fn save_z80v2<C: SnapshotCreator, W: Write>(
snapshot: &C,
wr: W
) -> Result<SnapshotResult>
{
let mut result = SnapshotResult::OK;
let model = snapshot.model();
let ext = snapshot.extensions();
let border = snapshot.border_color();
let issue = snapshot.issue();
let joystick = snapshot.joystick();
let cpu = get_nmos_cpu(snapshot.cpu(), &mut result);
if !is_cpu_safe_for_snapshot(&cpu) {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "Z80: can't safely snapshot the CPU state"))
}
if let Err(bad_ext) = model.validate_extensions(ext) {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
format!("Z80: the model {} can't be saved with {}", model, bad_ext)))
}
let mut header = Header::default();
init_z80_header(
&mut header,
Z80Version::V2,
&cpu,
border,
issue,
joystick,
&mut result
);
let mut head_ex = HeaderEx::default();
init_z80_header_ex(
&mut head_ex,
cpu,
model,
ext,
snapshot,
select_hw_model_v2,
&mut result
)?;
save_all_v2v3(Z80Version::V2, snapshot, model, &header, &head_ex, wr)?;
Ok(result)
}
/// Saves a **Z80** file version 3 into `wr` from the provided reference to a `snapshot` struct
/// implementing [SnapshotCreator].
///
/// # Errors
/// This function may return an error from attempts to write the file or if for some reason
/// a snapshot could not be created.
pub fn save_z80v3<C: SnapshotCreator, W: Write>(
snapshot: &C,
wr: W
) -> Result<SnapshotResult>
{
use ComputerModel::*;
let mut result = SnapshotResult::OK;
let model = snapshot.model();
let ext = snapshot.extensions();
let border = snapshot.border_color();
let issue = snapshot.issue();
let joystick = snapshot.joystick();
let cpu = get_nmos_cpu(snapshot.cpu(), &mut result);
if !is_cpu_safe_for_snapshot(&cpu) {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "Z80: can't safely snapshot the CPU state"))
}
if let Err(bad_ext) = model.validate_extensions(ext) {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
format!("Z80: the model {} can't be saved with {}", model, bad_ext)))
}
let mut header = Header::default();
init_z80_header(
&mut header,
Z80Version::V3,
&cpu,
border,
issue,
joystick,
&mut result
);
let mut head_ex = HeaderEx::default();
init_z80_header_ex(
&mut head_ex,
cpu,
model,
ext,
snapshot,
select_hw_model_v3,
&mut result
)?;
let (ts_lo, ts_hi) = cycles_to_z80(snapshot.current_clock(), model);
head_ex.ts_lo = ts_lo.to_le_bytes();
head_ex.ts_hi = ts_hi;
if (ext.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in())
|| (ext.intersects(Extensions::DISCIPLE) && snapshot.is_disciple_rom_paged_in())
{
head_ex.mgt_rom = !0;
}
if let Some(JoystickModel::Sinclair2) = joystick {
head_ex.joy_bindings = [ 3, 1, 3, 2, 3, 4, 3, 8, 3,10];
head_ex.joy_ascii = [31, 0,32, 0,33, 0,34, 0,35, 0];
}
match model {
SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e => {
head_ex.port2 = snapshot.ula3_flags().bits();
}
_ => {
head_ex.fn1 = !0;
if !(ext.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in()) {
head_ex.fn2 = !0;
}
}
}
if ext.intersects(Extensions::PLUS_D) {
head_ex.mgt_type = 16;
}
// head_ex.flags4 = 0;
// head_ex.disciple1 = 0;
// head_ex.disciple2 = 0;
save_all_v2v3(Z80Version::V3, snapshot, model, &header, &head_ex, wr)?;
Ok(result)
}
sourcefn is_tr_dos_rom_paged_in(&self) -> bool
fn is_tr_dos_rom_paged_in(&self) -> bool
Examples found in repository?
src/z80/saver.rs (line 83)
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
fn select_hw_model_v2<S: SnapshotCreator>(
model: ComputerModel,
ext: Extensions,
snapshot: &S,
result: &mut SnapshotResult
) -> Result<(u8, bool)>
{
use ComputerModel::*;
if ext.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in()
|| ext.intersects(Extensions::DISCIPLE) && snapshot.is_disciple_rom_paged_in()
|| ext.intersects(Extensions::TR_DOS) && snapshot.is_tr_dos_rom_paged_in()
|| ext.contains(Extensions::IF1|Extensions::SAM_RAM) && snapshot.is_interface1_rom_paged_in()
{
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a version 2 snapshot with the external ROM paged in"))
}
if (ext&!(Extensions::IF1|Extensions::SAM_RAM)) != Extensions::NONE
|| ext.contains(Extensions::IF1|Extensions::SAM_RAM)
{
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
Ok(match model {
Spectrum16 if ext.intersects(Extensions::SAM_RAM) => (2, true),
Spectrum16 if ext.intersects(Extensions::IF1) => (1, true),
Spectrum16 => (0, true),
Spectrum48 if ext.intersects(Extensions::SAM_RAM) => (2, false),
Spectrum48 if ext.intersects(Extensions::IF1) => (1, false),
Spectrum48 => (0, false),
SpectrumNTSC => {
result.insert(SnapshotResult::MODEL_NSUP);
(if ext.intersects(Extensions::SAM_RAM) { 2 }
else { ext.intersects(Extensions::IF1) as u8 }, false)
}
Spectrum128 if ext.intersects(Extensions::IF1) => (4, false),
Spectrum128 => (3, false),
SpectrumPlus2 if ext.intersects(Extensions::IF1) => (4, true),
SpectrumPlus2 => (3, true),
SpectrumPlus2A => (7, true),
SpectrumPlus3 => (7, false),
SpectrumPlus3e => {
result.insert(SnapshotResult::MODEL_NSUP);
(7, false)
}
SpectrumSE => {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a snapshot of SpectrumSE"))
}
TimexTC2048|TimexTC2068|TimexTS2068 if ext.intersects(Extensions::IF1)
&& snapshot.is_interface1_rom_paged_in() => {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a snapshot of Timex + IF1 with IF1 ROM paged in"))
}
TimexTC2048 => (14, false),
TimexTC2068 => (15, false),
TimexTS2068 => (128, false),
})
}
fn select_hw_model_v3<S: SnapshotCreator>(
model: ComputerModel,
ext: Extensions,
snapshot: &S,
result: &mut SnapshotResult
) -> Result<(u8, bool)>
{
use ComputerModel::*;
if ext.intersects(Extensions::TR_DOS) && snapshot.is_tr_dos_rom_paged_in()
|| ext.contains(Extensions::IF1|Extensions::SAM_RAM) && snapshot.is_interface1_rom_paged_in()
{
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a version 3 snapshot with the external ROM paged in"))
}
if ext.contains(Extensions::IF1|Extensions::SAM_RAM) {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
Ok(match model {
Spectrum16 if ext.intersects(Extensions::SAM_RAM) => (2, true),
Spectrum16 if ext.intersects(Extensions::IF1) => (1, true),
Spectrum16 if ext.intersects(Extensions::PLUS_D|Extensions::DISCIPLE) => (3, true),
Spectrum16 => (0, true),
Spectrum48 if ext.intersects(Extensions::SAM_RAM) => (2, false),
Spectrum48 if ext.intersects(Extensions::IF1) => (1, false),
Spectrum48 if ext.intersects(Extensions::PLUS_D|Extensions::DISCIPLE) => (3, false),
Spectrum48 => (0, false),
SpectrumNTSC => {
result.insert(SnapshotResult::MODEL_NSUP);
(if ext.intersects(Extensions::SAM_RAM) { 2 }
else if ext.intersects(Extensions::IF1) { 1 }
else if ext.intersects(Extensions::PLUS_D|Extensions::DISCIPLE) { 3 }
else { 0 }, false)
}
Spectrum128 if ext.intersects(Extensions::IF1) => (5, false),
Spectrum128 if ext.intersects(Extensions::PLUS_D|Extensions::DISCIPLE) => (6, false),
Spectrum128 => (4, false),
SpectrumPlus2 if ext.intersects(Extensions::IF1) => (5, true),
SpectrumPlus2 if ext.intersects(Extensions::PLUS_D|Extensions::DISCIPLE) => (6, true),
SpectrumPlus2 => (4, true),
SpectrumPlus2A => (7, true),
SpectrumPlus3 => (7, false),
SpectrumPlus3e => {
result.insert(SnapshotResult::MODEL_NSUP);
(7, false)
}
SpectrumSE => {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a snapshot of SpectrumSE"))
}
TimexTC2048|TimexTC2068|TimexTS2068 if (ext.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in())
|| (ext.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in())
|| (ext.intersects(Extensions::DISCIPLE) && snapshot.is_disciple_rom_paged_in())
=> {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a snapshot of Timex with extension ROM paged in"))
}
TimexTC2048 => (14, false),
TimexTC2068 => (15, false),
TimexTS2068 => (128, false),
})
}
type HwModelSelector<S> = fn(
ComputerModel, Extensions, &S, &mut SnapshotResult
) -> Result<(u8, bool)>;
fn init_z80_header_ex<S: SnapshotCreator, C: Cpu>(
head_ex: &mut HeaderEx,
cpu: C,
model: ComputerModel,
ext: Extensions,
snapshot: &S,
select_hw_model: HwModelSelector<S>,
result: &mut SnapshotResult
) -> Result<()>
{
use ComputerModel::*;
head_ex.pc = cpu.get_pc().to_le_bytes();
let (hw_mode, alt_hw) = select_hw_model(model, ext, snapshot, result)?;
let mut flags3 = Flags3::empty();
flags3.set(Flags3::ALT_HW_MODE, alt_hw);
head_ex.hw_mode = hw_mode;
let res = if let Some(res) = snapshot.ay_state(Ay3_891xDevice::Ay128k) {
if snapshot.ay_state(Ay3_891xDevice::FullerBox).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Melodik).is_some()
{
result.insert(SnapshotResult::SOUND_CHIP_NSUP);
}
Some(res)
}
else if let Some(res) = snapshot.ay_state(Ay3_891xDevice::FullerBox) {
flags3.insert(Flags3::AY_SOUND_EMU|Flags3::AY_FULLER_BOX);
Some(res)
}
else if let Some(res) = snapshot.ay_state(Ay3_891xDevice::Melodik) {
flags3.insert(Flags3::AY_SOUND_EMU);
Some(res)
}
else {
snapshot.ay_state(Ay3_891xDevice::Timex)
};
if let Some((ay_sel_reg, ay_regs)) = res {
head_ex.ay_sel_reg = ay_sel_reg.into();
head_ex.ay_regs = *ay_regs;
}
head_ex.port1 = match model {
Spectrum128|SpectrumPlus2|SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e|
SpectrumSE => snapshot.ula128_flags().bits(),
TimexTC2048|TimexTC2068|TimexTS2068 => snapshot.timex_memory_banks(),
_ => 0
};
head_ex.ifrom = match model {
TimexTC2048|TimexTS2068|TimexTC2068 => {
if ext.intersects(Extensions::IF1|Extensions::PLUS_D|Extensions::DISCIPLE) {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
snapshot.timex_flags().bits()
}
_ if ext.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in() => {
!0
}
_ => 0
};
head_ex.flags3 = flags3.bits();
Ok(())
}
fn save_ram_pages<W: Write, S: SnapshotCreator, I: Iterator<Item=(u8, usize)>>(
mut wr: W,
snapshot: &S,
pages: I
) -> Result<()>
{
let mut buf = Vec::with_capacity(0x1000);
for (ptype, page) in pages {
buf.clear();
let mem_slice = snapshot.memory_ref(MemoryRange::Ram(page * PAGE_SIZE..(page + 1) * PAGE_SIZE))?;
compress_write_all(mem_slice, &mut buf)?;
let (mem_head, slice) = match buf.len().try_into() {
Ok(core::u16::MAX)|Err(..) => {
(MemoryHeader::new(core::u16::MAX, ptype), mem_slice)
}
Ok(length) => (MemoryHeader::new(length, ptype), &buf[..]),
};
mem_head.write_struct(wr.by_ref())?;
wr.write_all(slice)?;
}
wr.flush()
}
fn save_all_v2v3<W: Write, S: SnapshotCreator>(
version: Z80Version,
snapshot: &S,
model: ComputerModel,
header: &Header,
head_ex: &HeaderEx,
mut wr: W
) -> Result<()>
{
use ComputerModel::*;
header.write_struct(wr.by_ref())?;
let ex_len: u16 = match version {
Z80Version::V2 => 23,
Z80Version::V3 if head_ex.port2 != 0 => 55,
Z80Version::V3 => 54,
_ => unreachable!()
};
wr.write_all(&ex_len.to_le_bytes()[..])?;
head_ex.write_struct_with_limit(wr.by_ref(), ex_len as usize)?;
match model {
Spectrum16 => {
save_ram_pages(wr, snapshot, iter::once((8, 0)))
}
Spectrum48|SpectrumNTSC|TimexTC2048|TimexTS2068|TimexTC2068 => {
save_ram_pages(wr, snapshot,
[(8, 0), (4, 1), (5, 2)].iter().copied())
}
Spectrum128|SpectrumPlus2|SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e => {
save_ram_pages(wr, snapshot,
(0..8).map(|page| (page as u8 + 3, page))
)
}
_ => unreachable!()
}
}
fn get_nmos_cpu(cpu: CpuModel, result: &mut SnapshotResult) -> Z80NMOS {
match cpu {
CpuModel::NMOS(cpu) => cpu,
CpuModel::CMOS(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
},
CpuModel::BM1(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
}
}
}
/// Saves a **Z80** file version 1 into `wr` from the provided reference to a `snapshot` struct
/// implementing [SnapshotCreator].
///
/// # Errors
/// This function may return an error from attempts to write the file or if for some reason
/// a snapshot could not be created.
pub fn save_z80v1<C: SnapshotCreator, W: Write>(
snapshot: &C,
mut wr: W
) -> Result<SnapshotResult>
{
use ComputerModel::*;
let mut result = SnapshotResult::OK;
let model = snapshot.model();
match model {
Spectrum48 => {},
Spectrum16|SpectrumNTSC|TimexTC2048 => {
result.insert(SnapshotResult::MODEL_NSUP);
}
_ => return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a version 1 snapshot of this computer model"))
};
let extensions = snapshot.extensions();
if let Err(bad_ext) = model.validate_extensions(extensions) {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
format!("Z80: the model {} can't be saved with {}", model, bad_ext)))
}
if extensions.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in()
|| extensions.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in()
|| extensions.intersects(Extensions::DISCIPLE) && snapshot.is_disciple_rom_paged_in()
|| extensions.intersects(Extensions::TR_DOS) && snapshot.is_tr_dos_rom_paged_in()
{
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"Z80: can't create a version 1 snapshot with the external ROM paged in"))
}
if extensions != Extensions::NONE {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
let cpu = get_nmos_cpu(snapshot.cpu(), &mut result);
let border = snapshot.border_color();
let issue = snapshot.issue();
let joystick = snapshot.joystick();
if snapshot.ay_state(Ay3_891xDevice::Ay128k).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Melodik).is_some()
|| snapshot.ay_state(Ay3_891xDevice::FullerBox).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Timex).is_some()
{
result.insert(SnapshotResult::SOUND_CHIP_NSUP);
}
let mut header = Header::default();
init_z80_header(
&mut header,
Z80Version::V1,
&cpu,
border,
issue,
joystick,
&mut result
);
header.write_struct(wr.by_ref())?;
let is_16k = model == ComputerModel::Spectrum16;
let ramend = if is_16k { 0x4000 } else { 0xC000 };
let mem_slice = snapshot.memory_ref(MemoryRange::Ram(0..ramend))?;
compress_write_all(mem_slice, wr.by_ref())?;
if is_16k {
compress_repeat_write_all(!0, 0x8000, wr.by_ref())?;
}
wr.write_all(MEMORY_V1_TERM)?;
wr.flush()?;
Ok(result)
}
More examples
src/sna.rs (line 374)
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
pub fn save_sna<C: SnapshotCreator, W: Write>(
snapshot: &C,
mut wr: W
) -> Result<SnapshotResult>
{
use ComputerModel::*;
let mut result = SnapshotResult::KEYB_ISSUE_NSUP;
let model = snapshot.model();
let is_128 = match model {
Spectrum48 => false,
Spectrum128 => true,
SpectrumPlus2|SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e|SpectrumSE => {
result.insert(SnapshotResult::MODEL_NSUP);
true
}
Spectrum16|SpectrumNTSC|TimexTC2048|TimexTC2068|TimexTS2068 => {
result.insert(SnapshotResult::MODEL_NSUP);
false
}
};
let extensions = snapshot.extensions();
if extensions.intersects(Extensions::IF1) && snapshot.is_interface1_rom_paged_in()
|| extensions.intersects(Extensions::PLUS_D) && snapshot.is_plus_d_rom_paged_in()
{
return Err(Error::new(ErrorKind::InvalidInput,
"SNA: can't create a snapshot with the external ROM paged in"))
}
if extensions != Extensions::NONE && extensions != Extensions::TR_DOS {
result.insert(SnapshotResult::EXTENSTION_NSUP);
}
let cpu = match snapshot.cpu() {
CpuModel::NMOS(cpu) => cpu,
CpuModel::CMOS(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
},
CpuModel::BM1(cpu) => {
result.insert(SnapshotResult::CPU_MODEL_NSUP);
cpu.into_flavour()
}
};
if !is_cpu_safe_for_snapshot(&cpu) {
return Err(Error::new(ErrorKind::InvalidInput, "SNA: can't safely snapshot the CPU state"))
}
let mut sna = make_header(&cpu);
sna.border = snapshot.border_color().into();
if snapshot.joystick().is_some() {
result.insert(SnapshotResult::JOYSTICK_NSUP);
}
if is_128 || snapshot.ay_state(Ay3_891xDevice::Melodik).is_some()
|| snapshot.ay_state(Ay3_891xDevice::FullerBox).is_some()
|| snapshot.ay_state(Ay3_891xDevice::Timex).is_some() {
result.insert(SnapshotResult::SOUND_CHIP_NSUP);
}
if !is_128 {
return save_sna48(snapshot, cpu, model == ComputerModel::Spectrum16, sna, wr, result)
}
let memflags = snapshot.ula128_flags();
let mut sna_ext = SnaHeader128 {
pc: cpu.get_pc().to_le_bytes(),
port_data: memflags.bits(),
..Default::default()
};
if extensions.intersects(Extensions::TR_DOS) {
sna_ext.trdos_rom = snapshot.is_tr_dos_rom_paged_in().into();
}
sna.write_struct(wr.by_ref())?;
let last_page: usize = memflags.last_ram_page_bank();
let index48 = [5,2,last_page];
for page in index48.iter() {
wr.write_all(
snapshot.memory_ref(MemoryRange::Ram(page * PAGE_SIZE..(page + 1) * PAGE_SIZE))?
)?;
}
sna_ext.write_struct(wr.by_ref())?;
for page in (0..8).filter(|n| !index48.contains(n) && *n != last_page) {
wr.write_all(
snapshot.memory_ref(MemoryRange::Ram(page * PAGE_SIZE..(page + 1) * PAGE_SIZE))?
)?;
}
wr.flush()?;
Ok(result)
}