pub fn is_cpu_safe_for_snapshot<C: Cpu>(cpu: &C) -> bool
Expand description

Returns true if a cpu is safe for a snapshot using lossy formats.

Examples found in repository?
src/z80/saver.rs (line 430)
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_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
Hide additional examples
src/sna.rs (line 345)
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)
}