From 4115d83eb537ffbd81e2fd8d12790a8fad769199 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Sun, 22 Dec 2024 23:33:33 +0100 Subject: nasm: Allow file paths for the configuration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This opens up the door for developers to pass their own configuration files. Signed-off-by: Miquel Sabaté Solà --- README.md | 21 +++++++++++++++------ crates/nasm/src/main.rs | 5 ++++- lib/xixanta/src/mapping.rs | 26 +++++++++++++++++--------- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 7d29b1a..bbc281e 100644 --- a/README.md +++ b/README.md @@ -30,12 +30,21 @@ By default it will assume the configuration for an `NROM` mapper, but this can be changed with the `-c/--configuration` flag, which accepts the following values: -- `empty`: just `HEADER` and `CODE`. Useful for one-liners (e.g. "how is this - instruction encoded in binary?"). -- `nrom`: the default configuration. It has the following segments: `HEADER`, - `CODE`, `VECTORS`, and `CHARS`. -- `nrom65`: same as `nrom` but it also has `STARTUP` for compatibility with the - default linker configuration from [cc65](https://github.com/cc65/cc65). +- [empty](./lib/xixanta/src/mappings/empty.toml): just `HEADER` and `CODE`. + Useful for one-liners (e.g. "how is this instruction encoded in binary?"). +- [nrom](./lib/xixanta/src/mappings/nrom.toml): the default configuration. + It has the following segments: `HEADER`, `CODE`, `VECTORS`, and `CHARS`. +- [nrom65](./lib/xixanta/src/mappings/nrom65.toml): same as `nrom` but it + also has `STARTUP` for compatibility with the default linker configuration + from [cc65](https://github.com/cc65/cc65). +- [unrom or uxrom](./lib/xixanta/src/mappings/unrom.toml): a configuration for + UxROM chips, with seven swappable banks bound at 0x8000 and 16KB of size + (`PRG0`..`PRG6`), and a fixed bank on 0xC000 and 16KB of size as well + (`FIXED`). + +Alternatively, you can also pass a path to a configuration of your own. Check +out the [ones already bundled](./lib/xixanta/src/mappings) on this application +for reference. ## `readrom` diff --git a/crates/nasm/src/main.rs b/crates/nasm/src/main.rs index c93dda0..ff8bcae 100644 --- a/crates/nasm/src/main.rs +++ b/crates/nasm/src/main.rs @@ -14,7 +14,10 @@ struct Args { /// when this argument is not given. file: Option, - /// Linker configuration to be used. Defaults to 'nrom'. + /// Linker configuration to be used. This configuration can be an identifier + /// for the configurations already baked in into this application, or it can + /// be a file path to a configuration of your choosing. See the + /// documentation for more information on this format. Defaults to 'nrom'. #[arg(short = 'c', long)] config: Option, diff --git a/lib/xixanta/src/mapping.rs b/lib/xixanta/src/mapping.rs index a196c8e..12b64c8 100644 --- a/lib/xixanta/src/mapping.rs +++ b/lib/xixanta/src/mapping.rs @@ -97,18 +97,26 @@ pub struct Mapping { } /// Returns a vector corresponding to the configuration of mappings that is -/// expected for the given `name`. Returns an error if the configuration cannot -/// be parsed or there's something wrong about it. +/// expected for the given `name`. This `name` can either be an already known +/// identifier (e.g. "nrom"), or a file path. Returns an error if the +/// configuration cannot be parsed or there's something wrong about it. pub fn get_mapping_configuration(name: &str) -> Result, String> { - let text = match name.to_lowercase().as_str() { - "empty" => EMPTY_CONFIG, - "nrom" => NROM_CONFIG, - "nrom65" => NROM65_CONFIG, - "uxrom" | "unrom" => UXROM_CONFIG, - _ => return Err("mapper configuration is not known".to_string()), + let configuration = if std::fs::exists(name).unwrap_or(false) { + match std::fs::read_to_string(name) { + Ok(contents) => load_configuration_for(contents.as_str())?, + Err(_) => return Err(format!("could not read '{}'", name)), + } + } else { + let text = match name.to_lowercase().as_str() { + "empty" => EMPTY_CONFIG, + "nrom" => NROM_CONFIG, + "nrom65" => NROM65_CONFIG, + "uxrom" | "unrom" => UXROM_CONFIG, + _ => return Err("mapper configuration is not known".to_string()), + }; + load_configuration_for(text)? }; - let configuration = load_configuration_for(text)?; validate_configuration(&configuration)?; Ok(configuration) -- cgit v1.2.3