pub struct MicroCartridge { /* private fields */ }
Expand description

This struct represents an emulated Microdrive tape cartridge.

It consist of up to MAX_SECTORS Sectors. Instances of this struct can be “inserted” into one of 8 ZxMicrodrives’s emulator drives.

Implementations§

Returns the current drive’s head position counted in sectors as floating point value.

The fractional part indicates how far the head position is within a sector.

Returns the number of the emulated physical sectors on the tape.

Returns the number of formatted sectors.

Returns an iterator of formatted sectors with their original indices.

Returns true if the cartridge is write protected.

Changes the write protected flag of the cartridge.

Returns true if the given sector is formatted.

Panics

Panics if sector equals to or is above the max_sectors limit.

Examples found in repository?
src/storage/microdrives.rs (line 407)
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
    fn erase_start(&mut self, delta_ts: u32) {
        self.forward(delta_ts);
        let TapeCursor { sector, secpos, .. } = self.tape_cursor;
        self.written = None;
        if self.is_sector_formatted(sector) {
            match secpos {
                SecPosition::Gap2 |
                SecPosition::Gap1 => {} // synchronized sector write may follow
                _ => { // otherwise clear sector
                    self.set_valid_sector(sector, false);
                }
            }
        }
    }

    // called when writing began or just erasing ended / motor stopped etc
    fn erase_forward(&mut self, delta_ts: u32) {
        let prev_cursor = self.tape_cursor;
        self.forward(delta_ts);
        let TapeCursor { sector, secpos, .. } = self.tape_cursor;
        if prev_cursor.sector != sector { // clear all previous sectors
            if delta_ts >= SECTOR_TS * (self.sectors.len() as u32 - 1) {
                self.clear_all_sectors();
            }
            else {
                let prev_sector = if let SecPosition::Gap2 = prev_cursor.secpos {
                    TapeCursor::add_sectors(prev_cursor.sector, 1, self.sectors.len() as u32)
                }
                else {
                    prev_cursor.sector
                };
                if sector < prev_sector {
                    self.sector_map.view_bits_mut::<LocalBits>()[prev_sector.into()..].fill(false);
                    self.sector_map.view_bits_mut::<LocalBits>()[..sector.into()].fill(false);
                }
                else {
                    self.sector_map.view_bits_mut::<LocalBits>()[prev_sector.into()..=sector.into()].fill(false);
                }
            }
        }
        else {
            match (prev_cursor.secpos, secpos) {
                // synchronized write has ended
                (SecPosition::Gap2, SecPosition::Gap2)|
                // synchronized write should follow
                (SecPosition::Gap1, SecPosition::Gap1)|
                (SecPosition::Gap1, SecPosition::Preamble2(..))|
                (SecPosition::Preamble2(..), SecPosition::Preamble2(..)) => {}
                _ => { // otherwise clear sector
                    self.set_valid_sector(sector, false);
                }
            }
        }
    }

    fn write_end(&mut self, delta_ts: u32) { // switched r/w w -> r, erase off, or motor off
        self.forward(delta_ts);
        // println!("wr: {:?}, delta: {} sec: {} cur: {} {:?}", self.written, delta_ts, self.tape_cursor.sector, self.tape_cursor.cursor, self.tape_cursor.secpos);
        if let Some(written) = self.written {
            if delta_ts < 2*BYTE_TS {
                const HEAD_SIZE_MIN: u16 = PREAMBLE_SIZE + HEAD_SIZE as u16;
                const HEAD_SIZE_MAX: u16 = HEAD_SIZE_MIN + 55;
                // const DATA_SIZE_MIN: u16 = PREAMBLE_SIZE + DATA_SIZE as u16;
                // const DATA_SIZE_MAX: u16 = DATA_SIZE_MIN + 110;
                #[allow(clippy::single_match)]
                match (written.get(), self.tape_cursor.secpos) {
                    (HEAD_SIZE_MIN..=HEAD_SIZE_MAX, SecPosition::Gap1) => {
                        // this may yield a "valid" sector with invalid data, but harmless
                        self.set_valid_sector(self.tape_cursor.sector, true);
                    }
                    // (DATA_SIZE_MIN..=DATA_SIZE_MAX, SecPosition::Gap2) => {
                    //     println!("ok valid data");
                    // }
                    _ => {}
                }
            }
        }
        self.written = None;
    }

    fn write_data_forward(&mut self, data: u8, delta_ts: u32) -> u16 {
        if let Some(written) = self.written {
            self.written = NonZeroU16::new(written.get().saturating_add(1));
            self.forward(delta_ts);
            let TapeCursor { sector, cursor, secpos } = self.tape_cursor;
            if delta_ts < BYTE_TS*3/2 {
                // println!("wr: {}, data: {:x}, sec: {} cur: {} {:?}", written.get(), data, sector, cursor, secpos);
                match (written.get(), data, secpos) {
                    (wr, 0x00, SecPosition::Preamble1(offs @ 1..=9))|
                    (wr, 0xff, SecPosition::Preamble1(offs @ 10..=11)) if wr == offs => {
                        return (BYTE_TS - (cursor - HEAD_PREAMBLE) % BYTE_TS) as u16;
                    }
                    (wr, _, SecPosition::Preamble1(PREAMBLE_SIZE)) if wr == PREAMBLE_SIZE => {
                        self.sectors[sector as usize].head[0] = data;
                        return (HEAD_START + BYTE_TS - cursor) as u16;
                    }
                    (wr, _, SecPosition::Header(offset)) if wr == PREAMBLE_SIZE + offset => {
                        self.sectors[sector as usize].head[offset as usize] = data;
                        return (BYTE_TS - (cursor - HEAD_START) % BYTE_TS) as u16;
                    }
                    (wr, _, SecPosition::Gap1) if wr >= PREAMBLE_SIZE + HEAD_SIZE as u16 => {
                        return (BYTE_TS - (cursor - GAP1_START) % BYTE_TS) as u16;
                    }
                    (wr, 0x00, SecPosition::Preamble2(offs @ 1..=9))|
                    (wr, 0xff, SecPosition::Preamble2(offs @ 10..=11)) if wr == offs => {
                        return (BYTE_TS - (cursor - DATA_PREAMBLE) % BYTE_TS) as u16;
                    }
                    (wr, _, SecPosition::Preamble2(PREAMBLE_SIZE)) if wr == PREAMBLE_SIZE => {
                        self.sectors[sector as usize].data[0] = data;
                        return (DATA_START + BYTE_TS - cursor) as u16;
                    }
                    (wr, _, SecPosition::Data(offset)) if wr == PREAMBLE_SIZE + offset => {
                        self.sectors[sector as usize].data[offset as usize] = data;
                        return (BYTE_TS - (cursor - DATA_START) % BYTE_TS) as u16;
                    }
                    (wr, _, SecPosition::Gap2) if wr >= PREAMBLE_SIZE + DATA_SIZE as u16 => {
                        return (BYTE_TS - (cursor - GAP2_START) % BYTE_TS) as u16;
                    }
                    _=> {}
                }
            }
            self.set_valid_sector(sector, false); // just erase all sector
            (BYTE_TS - cursor % BYTE_TS) as u16
        }
        else {
            // println!("start: {}", delta_ts);
            self.erase_forward(delta_ts);
            self.written = NonZeroU16::new(1);
            let TapeCursor { mut sector, secpos, .. } = self.tape_cursor;
            // println!("sector: {} cur: {} wr: {:?}, data: {:x}, {:?}", sector, self.tape_cursor.cursor, self.written, data, secpos);
            if data == 0 {
                if self.is_sector_formatted(sector) {
                    // println!("overwrite data block");
                    if let SecPosition::Preamble2(0..=2)|SecPosition::Gap1 = secpos { // synchronized write data sector
                        self.tape_cursor.cursor = DATA_PREAMBLE;
                        self.tape_cursor.secpos = SecPosition::Preamble2(0);
                        return BYTE_TS as u16;
                    }
                }
                if let SecPosition::Gap2 = secpos { // write starts on next sector
                    sector = TapeCursor::add_sectors(sector, 1, self.sectors.len() as u32);
                }
                // println!("begin sector");
                // write start somewhere in the middle of the sector, we just make it the new beginning of a sector 
                self.tape_cursor.sector = sector;
                self.tape_cursor.cursor = 0;
                self.tape_cursor.secpos = SecPosition::Preamble1(0);
            }
            self.set_valid_sector(sector, false); // just erase this sector
            BYTE_TS as u16
        }
    }

    fn read_data_forward(&mut self, delta_ts: u32) -> (u8, u16) { // data, delay
        self.forward(delta_ts);
        let TapeCursor { sector, cursor, secpos, .. } = self.tape_cursor;
        if self.is_sector_formatted(sector) {
            // println!("sec: {} cur: {} {:?} {:?}", sector, cursor, secpos, res);
            return match secpos {
                SecPosition::Preamble1(10..=11) => {
                    let data = self.sectors[sector as usize].head[0];
                    let delay = HEAD_START + BYTE_TS - cursor;
                    (data, delay as u16)
                }
                SecPosition::Header(offset) => {
                    let data = self.sectors[sector as usize].head[offset as usize];
                    let delay = BYTE_TS - (cursor - HEAD_START) % BYTE_TS;
                    (data, delay as u16)
                }
                SecPosition::Preamble1(..) => {
                    (0, (BYTE_TS - (cursor - HEAD_PREAMBLE) % BYTE_TS) as u16)
                }
                SecPosition::Gap1 => {
                    (!0, (BYTE_TS - (cursor - GAP1_START) % BYTE_TS) as u16)
                }
                SecPosition::Preamble2(10..=11) => {
                    let data = self.sectors[sector as usize].data[0];
                    let delay = DATA_START + BYTE_TS - cursor;
                    (data, delay as u16)
                }
                SecPosition::Data(offset) => {
                    let data = self.sectors[sector as usize].data[offset as usize];
                    let delay = BYTE_TS - (cursor - DATA_START) % BYTE_TS;
                    (data, delay as u16)
                }
                SecPosition::Preamble2(..) => {
                    (0, (BYTE_TS - (cursor - HEAD_PREAMBLE) % BYTE_TS) as u16)
                }
                SecPosition::Gap2 => {
                    let data = self.sectors[sector as usize].data[DATA_SIZE - 1];
                    (data, (BYTE_TS - (cursor - GAP2_START) % BYTE_TS) as u16)
                }
            }
        }
        (!0, (BYTE_TS - cursor % BYTE_TS) as u16)
    }

    #[inline(always)]
    fn forward(&mut self, delta_ts: u32) {
        self.tape_cursor.forward(delta_ts, self.sectors.len() as u32);
    }

    #[inline]
    fn gap_syn_protect(&self) -> CartridgeState {
        let mut gap = false;
        let mut syn = false;
        let TapeCursor { sector, secpos, .. } = self.tape_cursor;
        if self.is_sector_formatted(sector) {
            match secpos {
                SecPosition::Preamble1(0..=9)|
                SecPosition::Preamble2(0..=9) => {
                    gap = true;
                }
                SecPosition::Preamble1(10..=11)|
                SecPosition::Preamble2(10..=11) => {
                    gap = true;
                    syn = true;
                }
                _ => {}
            };
        }
        CartridgeState {
            gap, syn, write_protect: self.protec
        }
    }

Creates a new instance of MicroCartridge with provided sectors.

Note

Content of the sectors is not verified and is assumed to be properly formatted.

Panics

The number of sectors provided must not be greater than MAX_USABLE_SECTORS. max_sectors must not be 0 and must be grater or equal to the number of provided sectors and must not be greater than MAX_SECTORS.

Creates a new instance of MicroCartridge with custom max_sectors number.

Panics

max_sectors must not be 0 and must not be greater than MAX_SECTORS.

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
Returns the “default value” for a type. Read more
Deserialize this value from the given Serde deserializer. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more

Iterates through formatted sectors.

The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more

Iterates through formatted sectors.

The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
Serialize this value into the given Serde serializer. 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
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.