1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
use crate::consts;
use crate::instruction::Instruction;
use crate::typedef::*;
use flate2::{read::GzDecoder, write::GzEncoder, Compression};
use semver::Version;
use serde::{Deserialize, Serialize};
use std::{
    fs,
    io::{Read, Write},
    path::Path,
};

/// The container for a program
///
/// You can load a `Program` from a file or create one using the [ProgramBuilder][program_builder]
///
/// [program_builder]: struct.ProgramBuilder.html
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Program {
    /// The target version of the `melon` API the program was compiled against
    target_version: Version,
    /// The ID of the System the program is compiled against
    system_id: String,
    /// The instuctions of the program
    instructions: Vec<Instruction>,
    /// (Optional) The *minimum* number of allocated memory pages (1 page = 1024 Byte)
    mem_pages: Option<u8>,
    /// The entry address of the program
    entry_point: Address,
}

impl Program {
    /// Loads a MsgPack encoded and gzipped melon image from the given file
    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Program> {
        let gz_buf = fs::read(path)?;

        let res = Self::from_slice(&gz_buf)?;

        Ok(res)
    }

    /// Decodes a program from MsgPack encoded and gzipped image data
    pub fn from_slice(vec: &[u8]) -> Result<Program> {
        let mut decoder = GzDecoder::new(vec);
        let mut msgpack_buf = Vec::new();
        decoder.read_to_end(&mut msgpack_buf)?;

        rmp_serde::from_slice(msgpack_buf.as_slice()).map_err(|e| format_err!("Error: {}", e))
    }

    /// Encodes the program as MsgPack encoded and gzipped image data
    pub fn to_vec(&self) -> Result<Vec<u8>> {
        let msgpack_buf = rmp_serde::to_vec(&self)?;

        let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
        encoder.write_all(msgpack_buf.as_slice())?;
        let gz_buf = encoder.finish()?;

        Ok(gz_buf)
    }

    /// Saves the program as a MsgPack encoded and gzipped image to the given file
    pub fn save_as<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let gz_buf = self.to_vec()?;

        fs::write(path, gz_buf)?;

        Ok(())
    }

    /// The target version of the `melon` API the program was compiled against
    pub fn target_version(&self) -> &Version {
        &self.target_version
    }
    /// The ID of the System the program is compiled against
    pub fn system_id(&self) -> &String {
        &self.system_id
    }
    /// The instuctions of the program
    pub fn instructions(&self) -> &Vec<Instruction> {
        &self.instructions
    }
    /// (Optional) The *minimum* number of allocated memory pages (1 page = 1024 Byte)
    pub fn mem_pages(&self) -> Option<u8> {
        self.mem_pages
    }
    /// The entry address of the program
    pub fn entry_point(&self) -> Address {
        self.entry_point
    }
}

/// A builder for generating `Program`s
pub struct ProgramBuilder {
    /// The ID of the System the program is compiled against
    system_id: String,
    /// The instuctions of the program
    instructions: Vec<Instruction>,
    /// (Optional) The *minimum* number of allocated memory pages (1 page = 1024 Byte)
    mem_pages: Option<u8>,
    /// The entry address of the program
    entry_point: Address,
}

impl ProgramBuilder {
    /// Creates a new `ProgramBuilder` with the given system_id
    pub fn new(system_id: &str) -> ProgramBuilder {
        ProgramBuilder {
            system_id: system_id.into(),
            entry_point: 0,
            instructions: Vec::new(),
            mem_pages: None,
        }
    }

    /// Adds instructions to the builder
    pub fn instructions(mut self, instructions: Vec<Instruction>) -> ProgramBuilder {
        self.instructions = instructions;
        self
    }

    /// Adds an entry point to the builder
    pub fn entry_point(mut self, entry_point: Address) -> ProgramBuilder {
        self.entry_point = entry_point;
        self
    }

    /// Adds a requirement for memory pages to the builder
    pub fn mem_pages(mut self, mem_pages: u8) -> ProgramBuilder {
        self.mem_pages = Some(mem_pages);
        self
    }

    /// Generates a `Program` with the given setup and also overwrites the version number
    #[cfg(test)]
    pub(crate) fn gen_with_version(&self, version: &str) -> Program {
        Program {
            target_version: Version::parse(version).expect("unable to parse version"),
            system_id: self.system_id.clone(),
            instructions: self.instructions.clone(),
            mem_pages: self.mem_pages,
            entry_point: self.entry_point,
        }
    }

    /// Generates a `Program` with the given setup
    pub fn gen(&self) -> Program {
        Program {
            target_version: (*consts::VERSION).clone(),
            system_id: self.system_id.clone(),
            instructions: self.instructions.clone(),
            mem_pages: self.mem_pages,
            entry_point: self.entry_point,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rand::{distributions::Standard, thread_rng, Rng};
    use std::path::PathBuf;
    use tempfile::TempDir;

    fn gen_dir() -> TempDir {
        tempfile::tempdir().expect("unable to create temporary directory")
    }

    #[test]
    fn save_and_load() {
        let rng = thread_rng();
        let tmp_dir = gen_dir();

        let file_name = tmp_dir
            .path()
            .join(PathBuf::from("test").with_extension(ROM_FILE_EXTENSION));

        let program = ProgramBuilder::new("bogus_system")
            .mem_pages(1)
            .instructions(rng.sample_iter(&Standard).take(100).collect())
            .gen_with_version("0.0.0");

        program.save_as(&file_name).unwrap();

        let loaded_program = Program::from_file(&file_name).unwrap();

        assert_eq!(program.target_version, loaded_program.target_version);
        assert_eq!(program.system_id, loaded_program.system_id);
        assert_eq!(program.mem_pages, loaded_program.mem_pages);
    }
}