commit 05c903f535e52bfb5a4c7979eb87ba5e75d54dc2
parent e7aa518e36013753a72d3343b1b4c1c24840f6f6
Author: Ryan Jeffrey <ryan@ryanmj.xyz>
Date: Tue, 7 Jun 2022 14:26:09 -0700
Config stub
Diffstat:
A | src/config.rs | | | 129 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | src/main.rs | | | 12 | ++++++++++-- |
2 files changed, 139 insertions(+), 2 deletions(-)
diff --git a/src/config.rs b/src/config.rs
@@ -0,0 +1,129 @@
+// RISC II emulator configuration.
+// (C) Ryan Jeffrey <ryan@ryanmj.xyz>, 2022
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or (at
+// your option) any later version.
+
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// Struct definitions.
+
+use std::env;
+use std::fmt;
+
+/// Configuration of the emulator.
+pub struct Config {
+ /// Amount of memory the system will have.
+ mem: u32,
+ /// Number of CPUs the system will have.
+ ncpu: u32,
+ config_path: String,
+}
+
+// Struct impls.
+
+impl Config {
+ pub fn new() -> Config {
+ Config {
+ mem: 0,
+ ncpu: 0,
+ config_path: format!(""),
+ }
+ }
+
+ pub fn init() -> Result<Config, String> {
+ let mut config = Self::new();
+ let args: Vec<String> = env::args().collect();
+
+ let mut skips = 1i32;
+ for (i, arg) in args.iter().enumerate() {
+ if skips > 0 {
+ skips -= 1;
+ continue;
+ }
+
+ match arg.as_str() {
+ "--mem" => {
+ config.mem = args_get_next_uint(&args, i, &format!("mem"))?;
+ skips += 1;
+ }
+ "--ncpu" => {
+ config.ncpu = args_get_next_uint(&args, i, &format!("ncpu"))?;
+ skips += 1;
+ }
+ "--config" => {
+ config.config_path = args_get_next_arg(&args, i, &format!("config"))?.clone();
+ skips += 1;
+ }
+ _ => {
+ println!(
+ "Usage: riscii [OPTIONS]
+--config Path to configuration file (default=~/.config/riscii/config.toml)
+--mem Size of memory (in megabytes) (default=512)
+--ncpu Number of cores to emulate (default=1)
+"
+ );
+ return Err(format!("Invalid command line argument: {}", arg));
+ }
+ }
+ }
+
+ Ok(config)
+ }
+}
+
+// Local functions.
+
+fn args_check_size(args: &Vec<String>, i: usize, what: &String) -> Result<(), String> {
+ if i >= args.len() {
+ Err(format!(
+ "Invalid command line argument: {} takes an argument.",
+ what
+ ))
+ } else {
+ Ok(())
+ }
+}
+
+fn args_get_next_arg<'a>(
+ args: &'a Vec<String>,
+ i: usize,
+ what: &String,
+) -> Result<&'a String, String> {
+ args_check_size(&args, i, &what)?;
+ Ok(&args[i + 1])
+}
+
+fn args_get_next_uint(args: &Vec<String>, i: usize, what: &String) -> Result<u32, String> {
+ args_check_size(&args, i, &what)?;
+ Ok(match args[i + 1].parse::<u32>() {
+ core::result::Result::Ok(u) => u,
+ core::result::Result::Err(e) => {
+ return Err(format!(
+ "Invalid command line argument for {}: {}, err: {}.",
+ what,
+ args[i + 1],
+ e
+ ))
+ }
+ })
+}
+
+impl fmt::Display for Config {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(
+ f,
+ "Number of cpus: {}
+Memory (MB): {}
+Configuration file: {}",
+ self.ncpu, self.mem, self.config_path
+ )
+ }
+}
diff --git a/src/main.rs b/src/main.rs
@@ -17,6 +17,8 @@ mod main_test;
extern crate core;
+mod config;
+
use core::convert::TryInto;
use std::fs;
@@ -372,7 +374,7 @@ fn get_program(path: &String) -> Result<Vec<u8>, String> {
println!("Opening binary file {}.", path);
Ok(match fs::read(path) {
- Ok(mut raw_p) => raw_p.to_vec(),
+ Ok(raw_p) => raw_p.to_vec(),
Err(raw_e) => return Err(raw_e.to_string()),
})
}
@@ -388,7 +390,13 @@ fn decode_file(file: &Vec<u8>, pos: usize) -> Result<(), DecodeError> {
}
fn main() -> Result<(), String> {
- let program = get_program(&String::from("test.bin"))?;
+ let config = config::Config::init()?;
+
+ println!(
+ "Running emulator with the following configuration: \n{}\n",
+ config
+ );
+ //let program = get_program(&String::from("test.bin"))?;
Ok(())
}