Skip to main content

ds_rom/rom/
autoload.rs

1use std::borrow::Cow;
2
3use super::raw::{AutoloadInfo, AutoloadKind};
4
5/// An autoload block.
6pub struct Autoload<'a> {
7    data: Cow<'a, [u8]>,
8    info: AutoloadInfo,
9}
10
11impl<'a> Autoload<'a> {
12    /// Creates a new autoload block from raw data.
13    pub fn new<T: Into<Cow<'a, [u8]>>>(data: T, info: AutoloadInfo) -> Self {
14        Self { data: data.into(), info }
15    }
16
17    /// Returns a reference to the code of this [`Autoload`].
18    pub fn code(&self) -> &[u8] {
19        &self.data[..self.info.code_size() as usize]
20    }
21
22    /// Returns a reference to the full data of this [`Autoload`].
23    pub fn full_data(&self) -> &[u8] {
24        &self.data
25    }
26
27    /// Consumes this [`Autoload`] and returns the data.
28    pub fn into_data(self) -> Box<[u8]> {
29        self.data.into_owned().into_boxed_slice()
30    }
31
32    /// Returns the base address of this [`Autoload`].
33    pub fn base_address(&self) -> u32 {
34        self.info.base_address()
35    }
36
37    /// Returns the end address of this [`Autoload`].
38    pub fn end_address(&self) -> u32 {
39        self.info.base_address() + self.info.code_size() + self.info.bss_size()
40    }
41
42    /// Returns the kind of this [`Autoload`].
43    pub fn kind(&self) -> AutoloadKind {
44        self.info.kind()
45    }
46
47    /// Returns the size of the uninitialized data of this [`Autoload`].
48    pub fn bss_size(&self) -> u32 {
49        self.info.bss_size()
50    }
51
52    /// Returns a reference to the info of this [`Autoload`].
53    pub fn info(&self) -> &AutoloadInfo {
54        &self.info
55    }
56}