Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge feat/confloader to dev/0.0.1 #9

Merged
merged 46 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
2a8b141
Add: file configuration for JSON YAMLTOML and in-line
Esgr0bar May 12, 2024
d03e0ad
Delete configuration.rs
Esgr0bar May 12, 2024
2ebf899
Delete test.json
Esgr0bar May 12, 2024
36763c7
re-upload file at the right place
Esgr0bar May 12, 2024
d26347e
upload json to test configuration parser
Esgr0bar May 12, 2024
c25d6ce
Update configuration.rs : comment example main to allow merge without…
Esgr0bar May 16, 2024
bab8ccd
Create test.toml
Esgr0bar May 24, 2024
43fb2e0
Rename test.TOML to test.toml
Esgr0bar May 24, 2024
b5171f3
Create test.yaml
Esgr0bar May 24, 2024
3df6238
Create test.xml
Esgr0bar May 24, 2024
419c001
Update configuration.rs to handle only toml and use anyhow::Error
Esgr0bar May 28, 2024
1b8dc70
Update configuration.rs : update documentation
Esgr0bar May 28, 2024
fbeb6fb
Update Cargo.toml
Esgr0bar May 28, 2024
4c2bdb4
Update Cargo.toml
Esgr0bar May 28, 2024
7522baf
fix: after review comment
Esgr0bar Jun 10, 2024
73c30fb
add: common traits
Esgr0bar Jun 10, 2024
1c30090
Delete configuration.rs
Esgr0bar Jun 10, 2024
d217116
Delete lib.rs
Esgr0bar Jun 10, 2024
afe9999
Delete utils.rs
Esgr0bar Jun 10, 2024
6ab1f48
Delete config_struct.rs
Esgr0bar Jun 10, 2024
949eeea
Delete architecture.rs
Esgr0bar Jun 10, 2024
afe9559
Add files via upload
Esgr0bar Jun 10, 2024
8aa0b59
feat: added some generic traits/structs, started configuration refactor
standard3 Jun 18, 2024
142b7df
del: useless struct
standard3 Jun 18, 2024
0e61d34
fix: added &self to generic traits
standard3 Jun 18, 2024
a055036
feat: riscv base structs
standard3 Jun 18, 2024
bd3b9fd
feat: added machine struct
standard3 Jun 18, 2024
d53d989
chore: common traits on riscv mmumode
standard3 Jun 18, 2024
38433e9
fmt: removed useless docstrings
standard3 Jun 18, 2024
d29f5b0
del: useless tests files
standard3 Jun 18, 2024
7cd604d
chore: easily identifiable addresses
standard3 Jun 18, 2024
ad93938
feat: MemoryRegion impl, added MemorySpace, Machine generic struct
standard3 Jun 18, 2024
437fd3b
feat: created TryFromPath and FromPath traits
standard3 Jun 18, 2024
b377b99
wip: moved configuration to root
standard3 Jun 18, 2024
297e772
fix: implement from_path to every machine
standard3 Jun 19, 2024
037f927
fix: clippy è_é
standard3 Jun 19, 2024
0ae4e35
fix: removed false assertion
standard3 Jun 19, 2024
aa5299d
feat: renamed test to riscv sample
standard3 Jun 19, 2024
a2962e8
del: useless file
standard3 Jun 19, 2024
372ff63
feat: configuration integration test
standard3 Jun 19, 2024
8e7ab58
fix: moved machine generic in the good order
standard3 Jun 19, 2024
3191018
feat: riscv cpu & mmu
standard3 Jun 19, 2024
190acde
feat: changed public machine API, removed useless traits, added integ…
standard3 Jun 19, 2024
f67ed6a
fix: bad memory regions
standard3 Jun 19, 2024
4bbd76b
fix: typo
standard3 Jun 19, 2024
4113a76
fmt: changed fake data by todo! macros
standard3 Jun 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ license = "GPL-3.0"
authors = ["Théo Abel <theo.abel53@gmail.com>"] # todo: add other contributors
Esgr0bar marked this conversation as resolved.
Show resolved Hide resolved

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9.34"
toml = "0.8.12"
serde_json = "1.0"
serde-xml-rs = "0.6"
138 changes: 138 additions & 0 deletions src/configuration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
use serde::{Serialize, Deserialize};
use std::fs;
use std::error::Error;
use std::env;

/// Represents the main configuration for a machine.
///
/// Fields:
/// - `mmu_mode`: Optional string that specifies the MMU mode, which will be manually converted to a specific type.
/// - `memspaces`: A vector of `MemorySpace` that defines different memory regions.
/// - `dumpfile`: Optional string specifying the path to a dump file.
/// - `outfile`: Optional string specifying the path to an output file.
///
/// Description:
/// - This struct is used to parse and hold the configuration data of a machine from various file formats.
/// - It includes memory spaces, MMU mode, and optional paths for dump and output files.
///
/// Purpose:
/// - To provide a structured representation of machine configuration that can be easily loaded, manipulated, and accessed.
///
/// Technical Explanation:
/// - The `mmu_mode` is stored as a string to allow flexibility in how it is represented and used in different contexts.
/// - `memspaces` is a vector of `MemorySpace`, which details the type and address range of memory spaces.
#[derive(Debug, Serialize, Deserialize)]
Esgr0bar marked this conversation as resolved.
Show resolved Hide resolved
struct MachineConfig {
Esgr0bar marked this conversation as resolved.
Show resolved Hide resolved
mmu_mode: Option<String>,
memspaces: Vec<MemorySpace>,
dumpfile: Option<String>,
outfile: Option<String>,
}

/// Defines a memory space with a type and address range.
#[derive(Serialize, Deserialize, Debug)]
Esgr0bar marked this conversation as resolved.
Show resolved Hide resolved
struct MemorySpace {
Esgr0bar marked this conversation as resolved.
Show resolved Hide resolved
space_type: SpaceType,
start_address: u64,
end_address: u64,
}

/// Enumerates types of memory spaces.
#[derive(Serialize, Deserialize, Debug)]
enum SpaceType {
RAM,
ROM,
}

/// Enumerates RISC-V MMU modes.
#[derive(Debug, Serialize, Deserialize)]
enum RiscVMMUMode {
SV39,
SV48,
}

/// Loads configuration from a specified file.
///
/// Args:
/// - `file_path`: The path to the configuration file.
///
/// Returns:
/// - A `Result` containing `MachineConfig` or an error.
///
/// Description:
/// - This function reads a configuration file and deserializes it into a `MachineConfig` instance.
///
/// Purpose:
/// - To abstract the details of loading and parsing configuration files into a single reusable function.
///
/// Technical Explanation:
/// - The function determines the file format based on the extension and uses the appropriate Serde deserializer.
/// - It handles TOML format.
///
/// Example:
/// ```rust
/// let config = load_config("config.toml").unwrap();
/// println!("{:?}", config);
/// ```
fn load_config(file_path: &str) -> Result<MachineConfig, anyhow::Error> {
Esgr0bar marked this conversation as resolved.
Show resolved Hide resolved
let config_str = fs::read_to_string(file_path)
.with_context(|| format!("Failed to read configuration file: {}", file_path))?;
let config: MachineConfig = match file_path.rsplit('.').next().context("No file extension found")? {
shard77 marked this conversation as resolved.
Show resolved Hide resolved
"toml" => toml::from_str(&config_str)
Esgr0bar marked this conversation as resolved.
Show resolved Hide resolved
.with_context(|| format!("Failed to parse TOML configuration file: {}", file_path))?,
_ => return Err(anyhow::Error::msg(format!("Unsupported file format: {}", file_path))),
};
Ok(config)
}
/// Main function demonstrating configuration loading and usage.
///
/// Example:
/// - This example loads a configuration from a JSON file and demonstrates how to manually convert the `mmu_mode`.
/// fn main() -> Result<(), anyhow::Error> {
// Collect command line arguments
/// let args: Vec<String> = env::args().collect();
// Check if a file path is provided
/// if args.len() < 2 {
/// eprintln!("Usage: {} <path_to_config_file>", args[0]);
/// std::process::exit(1);
/// }
// The file path is the second argument
/// let config_path = &args[1];
// Output the path for debugging purposes
/// println!("Loading configuration from: {}", config_path);
// Attempt to load the configuration
/// let file_conf = load_config(config_path)?;
// Output the loaded configuration for verification
/// println!("Loaded Configuration: {:#?}", file_conf);
///
/// let mmu_mode: Option<RiscVMMUMode> = match file_conf.mmu_mode {
/// Some(ref mode) => match mode.as_str() {
/// "SV39" => Some(RiscVMMUMode::SV39),
/// "SV48" => Some(RiscVMMUMode::SV48),
/// _ => None,
/// },
/// None => None,
/// };
/// let inline_conf = MachineConfig {
/// mmu_mode: Some("SV39".to_string()),
/// memspaces: vec![
/// MemorySpace {
/// space_type: SpaceType::RAM,
/// start_address: 0x0000000080000000,
/// end_address: 0x000000017fffffff,
/// },
/// MemorySpace {
/// space_type: SpaceType::ROM,
/// start_address: 0x0000000000000000,
/// end_address: 0x0000000000011fff,
/// },
/// ],
/// dumpfile: Some("dump.raw".to_string()),
/// outfile: Some("output.txt".to_string()),
/// };
// Output the loaded configuration and the manually converted MMU mode.
/// println!("File-based Configuration: {:#?}", file_conf);
/// println!("MMU Mode: {:?}", mmu_mode);
/// println!("Inline Configuration: {:#?}", inline_conf);
/// Ok(())
/// }
17 changes: 17 additions & 0 deletions tests/test.json
Esgr0bar marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"mmu_mode": "SV39",
"memspaces": [
{
"space_type": "RAM",
"start_address": 34359738368,
"end_address": 103079215103
},
{
"space_type": "ROM",
"start_address": 0,
"end_address": 73727
}
],
"dumpfile": "example_dump.raw",
"outfile": "example_output.txt"
}
14 changes: 14 additions & 0 deletions tests/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
mmu_mode = "SV39"

[[memspaces]]
space_type = "RAM"
start_address = 34359738368
end_address = 103079215103

[[memspaces]]
space_type = "ROM"
start_address = 0
end_address = 73727

dumpfile = "example_dump.raw"
outfile = "example_output.txt"
17 changes: 17 additions & 0 deletions tests/test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<configuration>
<mmu_mode>SV39</mmu_mode>
<memspaces>
<memspace>
<space_type>RAM</space_type>
<start_address>34359738368</start_address>
<end_address>103079215103</end_address>
</memspace>
<memspace>
<space_type>ROM</space_type>
<start_address>0</start_address>
<end_address>73727</end_address>
</memspace>
</memspaces>
<dumpfile>example_dump.raw</dumpfile>
<outfile>example_output.txt</outfile>
</configuration>
13 changes: 13 additions & 0 deletions tests/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mmu_mode: "SV39"

memspaces:
- space_type: "RAM"
start_address: 34359738368
end_address: 103079215103

- space_type: "ROM"
start_address: 0
end_address: 73727

dumpfile: "example_dump.raw"
outfile: "example_output.txt"
Loading