#[non_exhaustive]
pub enum ComputerModel {
    Spectrum16,
    Spectrum48,
    SpectrumNTSC,
    Spectrum128,
    SpectrumPlus2,
    SpectrumPlus2A,
    SpectrumPlus3,
    SpectrumPlus3e,
    SpectrumSE,
    TimexTC2048,
    TimexTC2068,
    TimexTS2068,
}

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Spectrum16

§

Spectrum48

§

SpectrumNTSC

§

Spectrum128

§

SpectrumPlus2

§

SpectrumPlus2A

§

SpectrumPlus3

§

SpectrumPlus3e

§

SpectrumSE

§

TimexTC2048

§

TimexTC2068

§

TimexTS2068

Implementations§

Returns the number of T-states per single frame.

Examples found in repository?
src/z80/common.rs (line 240)
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
pub fn z80_to_cycles(ts_lo: u16, ts_hi: u8, model: ComputerModel) -> FTs {
    let total_ts = model.frame_tstates();
    let qts = total_ts / 4;
    let qcountdown = ts_lo as FTs;
    (((ts_hi + 1) % 4 + 1) as FTs * qts - (qcountdown + 1))
    .rem_euclid(total_ts)
}

pub fn cycles_to_z80(ts: FTs, model: ComputerModel) -> (u16, u8) {
    let total_ts = model.frame_tstates();
    let qts = total_ts / 4;
    let ts_lo = (qts - (ts.rem_euclid(qts)) - 1) as u16;
    let ts_hi = (ts.rem_euclid(total_ts) / qts - 1).rem_euclid(4) as u8;
    (ts_lo, ts_hi)
}

Returns issue or ReadEarMode::Clear when it does not apply to the model.

Examples found in repository?
src/z80/loader.rs (lines 182-189)
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
pub fn load_z80<R: Read, S: SnapshotLoader>(
        mut rd: R,
        loader: &mut S
    ) -> Result<()>
{
    use ComputerModel::*;

    let header = Header::read_new_struct(rd.by_ref())?;
    let mut version = Z80Version::V1;

    let mut model = ComputerModel::Spectrum48;
    let mut extensions = Extensions::default();
    let mut cpu = create_cpu(&header)?;

    let header_ex = if cpu.get_pc() == 0 {
        let (ver, head_ex) = load_header_ex(rd.by_ref())?;
        version = ver;
        cpu.set_pc(u16::from_le_bytes(head_ex.pc));
        let (mdl, ext) = select_hw_model(version, &head_ex).ok_or_else(||
            io::Error::new(io::ErrorKind::InvalidData, "unsupported model")
        )?;
        model = mdl;
        extensions = ext;
        Some(head_ex)
    }
    else {
        None
    };

    let flags1 = Flags1::from(header.flags1);
    let border = flags1.border_color();
    let flags2 = Flags2::from(header.flags2);
    let joystick = flags2.joystick_model();
    let issue = model.applicable_issue(
        if flags2.is_issue2_emulation() {
            ReadEarMode::Issue2
        }
        else {
            ReadEarMode::Issue3
        }
    );

    loader.select_model(model, extensions, border, issue)
          .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
    loader.select_joystick(joystick);
    loader.assign_cpu(CpuModel::NMOS(cpu));

    // clippy false positive: https://github.com/rust-lang/rust-clippy/issues/9274
    #[allow(clippy::read_zero_byte_vec)] {
    let mut buf = Vec::new();
    if version == Z80Version::V1 {
        if flags1.is_mem_compressed() {
            rd.read_to_end(&mut buf)?;
            let buf = match buf.get(buf.len() - 4..) {
                Some(MEMORY_V1_TERM) => &buf[..buf.len() - 4],
                _ => &buf[..]
            };
            let decompress = MemDecompress::new(buf);
            loader.read_into_memory(MemoryRange::Ram(0..3*PAGE_SIZE), decompress)?;
        }
        else {
            loader.read_into_memory(MemoryRange::Ram(0..3*PAGE_SIZE), rd)?;
        }
    }
    else {
        while let Some((len, page, is_compressed)) = load_mem_header(rd.by_ref())? {
            let range = mem_page_to_range(page, model, extensions).ok_or_else(||
                io::Error::new(io::ErrorKind::InvalidData, "unsupported memory page")
            )?;
            if is_compressed {
                buf.resize(len, 0);
                rd.read_exact(&mut buf)?;
                let decompress = MemDecompress::new(&buf);
                loader.read_into_memory(range, decompress)?;
            }
            else {
                loader.read_into_memory(range, rd.by_ref().take(len as u64))?;
            }
        }
    }}

    if let Some(head_ex) = header_ex {
        if let Some(choice) = select_ay_model(model, Flags3::from(head_ex.flags3)) {
            loader.setup_ay(choice, head_ex.ay_sel_reg.into(), &head_ex.ay_regs);
        }

        if version == Z80Version::V3 {
            let ts = z80_to_cycles(u16::from_le_bytes(head_ex.ts_lo), head_ex.ts_hi, model);
            loader.set_clock(ts);
            let data = head_ex.port2;
            if data != 0 {
                match model {
                    SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e|
                    SpectrumSE => {
                        loader.write_port(0x1ffd, data);
                    }
                    _ => {}
                }
            }
        }

        match model {
            TimexTC2048|TimexTS2068|TimexTC2068 => {
                loader.write_port(0xf4, head_ex.port1);
            }
            Spectrum128|SpectrumPlus2|
            SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e|
            SpectrumSE => {
                loader.write_port(0x7ffd, head_ex.port1);
            }
            _ => {}
        }
        match model {
            TimexTC2048|TimexTS2068|TimexTC2068 => {
                loader.write_port(0xff, head_ex.ifrom);
            }
            Spectrum16|Spectrum48|
            Spectrum128|SpectrumPlus2|
            SpectrumPlus2A|SpectrumPlus3|SpectrumPlus3e|
            SpectrumSE
            if head_ex.ifrom == 0xff && extensions.intersects(Extensions::IF1) => {
                loader.interface1_rom_paged_in();
            }
            _ => {}
        }
    }
    Ok(())
}
Examples found in repository?
src/z80/saver.rs (line 357)
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)
}

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts self into T using Into<T>. Read more
Causes self to use its Binary implementation when Debug-formatted.
Causes self to use its Display implementation when Debug-formatted. Read more
Causes self to use its LowerExp implementation when Debug-formatted. Read more
Causes self to use its LowerHex implementation when Debug-formatted. Read more
Causes self to use its Octal implementation when Debug-formatted.
Causes self to use its Pointer implementation when Debug-formatted. Read more
Causes self to use its UpperExp implementation when Debug-formatted. Read more
Causes self to use its UpperHex implementation when Debug-formatted. Read more
Formats each item in a sequence. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Convert to S a sample type from self.
Pipes by value. This is generally the method you want to use. Read more
Borrows self and passes that borrow into the pipe function. Read more
Mutably borrows self and passes that borrow into the pipe function. Read more
Borrows self, then passes self.borrow() into the pipe function. Read more
Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
Borrows self, then passes self.as_ref() into the pipe function.
Mutably borrows self, then passes self.as_mut() into the pipe function. Read more
Borrows self, then passes self.deref() into the pipe function.
Mutably borrows self, then passes self.deref_mut() into the pipe function. Read more
Immutable access to a value. Read more
Mutable access to a value. Read more
Immutable access to the Borrow<B> of a value. Read more
Mutable access to the BorrowMut<B> of a value. Read more
Immutable access to the AsRef<R> view of a value. Read more
Mutable access to the AsMut<R> view of a value. Read more
Immutable access to the Deref::Target of a value. Read more
Mutable access to the Deref::Target of a value. Read more
Calls .tap() only in debug builds, and is erased in release builds.
Calls .tap_mut() only in debug builds, and is erased in release builds. Read more
Calls .tap_borrow() only in debug builds, and is erased in release builds. Read more
Calls .tap_borrow_mut() only in debug builds, and is erased in release builds. Read more
Calls .tap_ref() only in debug builds, and is erased in release builds. Read more
Calls .tap_ref_mut() only in debug builds, and is erased in release builds. Read more
Calls .tap_deref() only in debug builds, and is erased in release builds. Read more
Calls .tap_deref_mut() only in debug builds, and is erased in release builds. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
Attempts to convert self into T using TryInto<T>. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.