pub struct AyString(_);
Expand description

The type of a parsed AY file’s strings.

Implementations§

Returns a reference to the string as an array of bytes.

Examples found in repository?
src/ay.rs (line 149)
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
    fn as_ref(&self) -> &[u8] {
        self.as_slice()
    }
}

impl Deref for AyBlob {
    type Target = [u8];
    fn deref(&self) -> &[u8] {
        self.as_slice()
    }
}

impl AyBlob {
    fn new(slice: &[u8]) -> Self {
        AyBlob(NonNull::from(slice))
    }

    /// Returns a reference to data.
    pub fn as_slice(&self) -> &[u8] {
        unsafe { self.0.as_ref() } // creating of this type is private so it will be only made pinned
    }
}

impl AyString {
    fn new(slice: &[u8]) -> Self {
        AyString(NonNull::from(slice))
    }

    /// Returns a reference to the string as an array of bytes.
    pub fn as_slice(&self) -> &[u8] {
        unsafe { self.0.as_ref() } // creating of this type is private so it will be only made pinned
    }

    /// Returns a string reference if the underlying bytes can form a proper UTF-8 string.
    pub fn to_str(&self) -> Result<&str, core::str::Utf8Error> {
        core::str::from_utf8(self.as_slice())
    }

    /// Returns a reference to a `str` if the underlying bytes can form a proper UTF-8 string.
    /// Otherwise, returns a [String], including invalid characters.
    pub fn to_str_lossy(&self) -> Cow<str> {
        String::from_utf8_lossy(self.as_slice())
    }

Returns a string reference if the underlying bytes can form a proper UTF-8 string.

Returns a reference to a str if the underlying bytes can form a proper UTF-8 string. Otherwise, returns a String, including invalid characters.

Examples found in repository?
src/ay.rs (line 125)
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
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "AyString: {:?}", self.to_str_lossy())
    }
}

impl fmt::Display for AyString {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_str_lossy())
    }
}

impl fmt::Debug for AyFile {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "AyFile {{ raw: ({:?}), meta: {:?}, songs: {:?} }}", self.raw.len(), self.meta, self.songs)
    }
}

impl AsRef<[u8]> for AyBlob {
    fn as_ref(&self) -> &[u8] {
        self.as_slice()
    }
}

impl AsRef<[u8]> for AyString {
    fn as_ref(&self) -> &[u8] {
        self.as_slice()
    }
}

impl Deref for AyBlob {
    type Target = [u8];
    fn deref(&self) -> &[u8] {
        self.as_slice()
    }
}

impl AyBlob {
    fn new(slice: &[u8]) -> Self {
        AyBlob(NonNull::from(slice))
    }

    /// Returns a reference to data.
    pub fn as_slice(&self) -> &[u8] {
        unsafe { self.0.as_ref() } // creating of this type is private so it will be only made pinned
    }
}

impl AyString {
    fn new(slice: &[u8]) -> Self {
        AyString(NonNull::from(slice))
    }

    /// Returns a reference to the string as an array of bytes.
    pub fn as_slice(&self) -> &[u8] {
        unsafe { self.0.as_ref() } // creating of this type is private so it will be only made pinned
    }

    /// Returns a string reference if the underlying bytes can form a proper UTF-8 string.
    pub fn to_str(&self) -> Result<&str, core::str::Utf8Error> {
        core::str::from_utf8(self.as_slice())
    }

    /// Returns a reference to a `str` if the underlying bytes can form a proper UTF-8 string.
    /// Otherwise, returns a [String], including invalid characters.
    pub fn to_str_lossy(&self) -> Cow<str> {
        String::from_utf8_lossy(self.as_slice())
    }
}

static PLAYER_ONE: &[u8] = &[
/* 0000:*/ 0xF3,           /* di                  */
/* 0001:*/ 0xCD,0x00,0x00, /* call 0000H -> init  */
/* 0004:*/ 0xED,0x5E,      /* im   2     :loop1   */
/* 0006:*/ 0xFB,           /* ei                  */
/* 0007:*/ 0x76,           /* halt                */
/* 0008:*/ 0x18,0xFA,      /* jr   0004H -> loop1 */
];
static PLAYER_TWO: &[u8] = &[
/* 0000:*/ 0xF3,           /* di                      */
/* 0001:*/ 0xCD,0x00,0x00, /* call 0000H -> init      */
/* 0004:*/ 0xED,0x56,      /* im   1     :loop1       */
/* 0006:*/ 0xFB,           /* ei                      */
/* 0007:*/ 0x76,           /* halt                    */
/* 0008:*/ 0xCD,0x00,0x00, /* call 0000H -> interrupt */
/* 000B:*/ 0x18,0xF7,      /* jr   0004H -> loop1     */
];
const PLAYER_INIT_OFFSET: usize = 2;
const PLAYER_TWO_INTERRUPT_OFFSET: usize = 9;

impl AyFile {
    /// Initializes `memory` and the `cpu` registers, creates a player routine, and loads song data into `memory`.
    /// Provide `song_index` of the desired song from this file to be played.
    ///
    /// # Panics
    /// * If `song_index` is larger or equal to the number of contained songs.
    ///   You may get the number of songs by invoking `.songs.len()` method.
    /// * If a special player is required. See [AyMeta].
    /// * If the capacity of provided memory is less than 64kb.
    pub fn initialize_player<C, M>(&self, cpu: &mut C, memory: &mut M, song_index: usize)
    where C: Cpu, M: ZxMemory
    {
        if self.meta.special_player {
            panic!("can't initialize file with a special player");
        }
        let song = &self.songs[song_index];
        debug!("loading song: ({}) {}", song_index, song.name.to_str_lossy());
        let rawmem = memory.mem_mut();
        for p in rawmem[0x0000..0x0100].iter_mut() { *p = 0xC9 };
        for p in rawmem[0x0100..0x4000].iter_mut() { *p = 0xFF };
        for p in rawmem[0x4000..].iter_mut() { *p = 0x00 };
        rawmem[0x0038] = 0xFB;
        debug!("INIT: ${:x}", song.init);
        let init = if song.init == 0 {
            debug!("INIT using: ${:x}", song.blocks[0].address);
            song.blocks[0].address
        }
        else {
            song.init
        }.to_le_bytes();
        debug!("INTERRUPT: ${:x}", song.interrupt);
        let player = if song.interrupt == 0 {
            debug!("PLAYER ONE");
            PLAYER_ONE
        }
        else {
            debug!("PLAYER TWO");
            PLAYER_TWO
        };
        rawmem[0x0000..player.len()].copy_from_slice(player);
        if song.interrupt != 0 {
            let intr = song.interrupt.to_le_bytes();
            rawmem[PLAYER_TWO_INTERRUPT_OFFSET..PLAYER_TWO_INTERRUPT_OFFSET+intr.len()].copy_from_slice(&intr);
        }
        rawmem[PLAYER_INIT_OFFSET..PLAYER_INIT_OFFSET+init.len()].copy_from_slice(&init);
        for block in song.blocks.iter() {
            debug!("Block: ${:x} <- {:?}", block.address, block.data);
            let address = block.address as usize;
            let mut data = block.data.as_slice();
            if address + data.len() > (u16::max_value() as usize) + 1 {
                debug!("Block too large: ${:x}", address + data.len());
                data = &data[..u16::max_value() as usize - address];
            }
            rawmem[address..address+data.len()].copy_from_slice(data);
        }
        debug!("STACK: ${:x}", song.stack);
        // debug_memory(0x0000, &rawmem[0x0000..player.len()]);
        cpu.reset();
        cpu.set_i(self.meta.player_version);
        cpu.set_reg(Reg8::H, None, song.hi_reg);
        cpu.set_reg(Reg8::L, None, song.lo_reg);
        cpu.set_reg(Reg8::D, None, song.hi_reg);
        cpu.set_reg(Reg8::E, None, song.lo_reg);
        cpu.set_reg(Reg8::B, None, song.hi_reg);
        cpu.set_reg(Reg8::C, None, song.lo_reg);
        cpu.exx();
        cpu.set_acc(song.hi_reg);
        cpu.set_flags(CpuFlags::from_bits_truncate(song.lo_reg));
        cpu.ex_af_af();
        cpu.set_reg(Reg8::H, None, song.hi_reg);
        cpu.set_reg(Reg8::L, None, song.lo_reg);
        cpu.set_reg(Reg8::D, None, song.hi_reg);
        cpu.set_reg(Reg8::E, None, song.lo_reg);
        cpu.set_reg(Reg8::B, None, song.hi_reg);
        cpu.set_reg(Reg8::C, None, song.lo_reg);
        cpu.set_reg(Reg8::H, Some(Prefix::Yfd), song.hi_reg);
        cpu.set_reg(Reg8::L, Some(Prefix::Yfd), song.lo_reg);
        cpu.set_reg(Reg8::H, Some(Prefix::Xdd), song.hi_reg);
        cpu.set_reg(Reg8::L, Some(Prefix::Xdd), song.lo_reg);
        cpu.set_acc(song.hi_reg);
        cpu.set_flags(CpuFlags::from_bits_truncate(song.lo_reg));
        cpu.disable_interrupts();
        cpu.set_sp(song.stack);
        cpu.set_im(InterruptMode::Mode0);
        cpu.set_pc(0x0000);
    }

Trait Implementations§

Converts this type into a shared reference of the (usually inferred) input type.
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Views self as an immutable bit-slice region with the O ordering.
Attempts to view self as an immutable bit-slice region with the O ordering. 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
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.