From 86497070b30ec024123727ca23ca0122e040fbb3 Mon Sep 17 00:00:00 2001 From: Isabelle L Date: Wed, 24 Jun 2020 20:32:39 -0500 Subject: [PATCH] starting to work on loading programs to the system --- Cargo.lock | 7 ++++++- Cargo.toml | 4 ++-- ceres-asm/Cargo.toml | 2 -- ceres-sys/Cargo.toml | 3 +-- ceres-sys/src/lib.rs | 10 ++++++++++ ceres-sys/src/memory.rs | 18 ++++++++++++++++++ 6 files changed, 37 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 141eafd..6f136b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -72,7 +72,7 @@ version = "0.1.0" dependencies = [ "ceres-asm 0.1.0", "ceres-sys 0.1.0", - "offbrand 0.1.0", + "offbrand 0.1.0 (git+https://github.com/izzabelle/offbrand)", "structopt 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "thiserror 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -88,6 +88,9 @@ dependencies = [ [[package]] name = "ceres-sys" version = "0.1.0" +dependencies = [ + "thiserror 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "cfg-if" @@ -427,6 +430,7 @@ dependencies = [ [[package]] name = "offbrand" version = "0.1.0" +source = "git+https://github.com/izzabelle/offbrand#e4ec4d2a8081595d6412acd60c2448d37a1614f4" dependencies = [ "image 0.23.5 (registry+https://github.com/rust-lang/crates.io-index)", "minifb 0.16.0 (git+https://github.com/emoon/rust_minifb)", @@ -1065,6 +1069,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum num-rational 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5c000134b5dbf44adc5cb772486d335293351644b801551abe8f75c84cfa4aef" "checksum num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096" "checksum num_cpus 1.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" +"checksum offbrand 0.1.0 (git+https://github.com/izzabelle/offbrand)" = "" "checksum once_cell 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0b631f7e854af39a1739f401cf34a8a013dfe09eac4fa4dba91e9768bd28168d" "checksum orbclient 0.3.27 (registry+https://github.com/rust-lang/crates.io-index)" = "f8b18f57ab94fbd058e30aa57f712ec423c0bb7403f8493a6c58eef0c36d9402" "checksum pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677" diff --git a/Cargo.toml b/Cargo.toml index c2723fc..14c838b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,8 +5,8 @@ authors = ["Izzy "] edition = "2018" [dependencies] -#offbrand = { git = "https://github.com/izzabelle/offbrand" } -offbrand = { path = "../offbrand" } +offbrand = { git = "https://github.com/izzabelle/offbrand" } +#offbrand = { path = "../offbrand" } structopt = "0.3.14" ceres-sys = { path = "ceres-sys" } ceres-asm = { path = "ceres-asm" } diff --git a/ceres-asm/Cargo.toml b/ceres-asm/Cargo.toml index d619ad7..7624412 100644 --- a/ceres-asm/Cargo.toml +++ b/ceres-asm/Cargo.toml @@ -4,8 +4,6 @@ version = "0.1.0" authors = ["Isabelle L. "] edition = "2018" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] logos = "0.11.4" thiserror = "1.0.20" diff --git a/ceres-sys/Cargo.toml b/ceres-sys/Cargo.toml index 1e0dde0..c15ddae 100644 --- a/ceres-sys/Cargo.toml +++ b/ceres-sys/Cargo.toml @@ -4,6 +4,5 @@ version = "0.1.0" authors = ["Isabelle L. "] edition = "2018" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] +thiserror = "1.0.20" diff --git a/ceres-sys/src/lib.rs b/ceres-sys/src/lib.rs index 0131b18..807ed30 100644 --- a/ceres-sys/src/lib.rs +++ b/ceres-sys/src/lib.rs @@ -13,6 +13,16 @@ pub const SCREEN_HEIGHT: usize = 144; /// ceres video memory buffer length pub const VIDEO_MEMORY_LEN: usize = SCREEN_HEIGHT * SCREEN_WIDTH; +/// result type +pub type Result = std::result::Result; + +/// error type +#[derive(Debug, thiserror::Error)] +pub enum CeresSystemError { + #[error(transparent)] + StdIo(#[from] std::io::Error), +} + /// the core system structure of ceres pub struct System { pub registers: Registers, diff --git a/ceres-sys/src/memory.rs b/ceres-sys/src/memory.rs index 61ec4ed..3f792bd 100644 --- a/ceres-sys/src/memory.rs +++ b/ceres-sys/src/memory.rs @@ -1,3 +1,6 @@ +use crate::Result; +use std::{io::Read, path::PathBuf}; + /// all the memory as one big fat fucking slice pub struct Memory { data: [u16; std::u16::MAX as usize], @@ -8,6 +11,21 @@ impl Memory { pub fn init() -> Memory { Memory { data: [0x00; std::u16::MAX as usize] } } + + /// loads a program to the memory + pub fn load_program(&mut self, path: PathBuf) -> Result<()> { + // load bytes + let mut file = std::fs::File::open(path)?; + let mut raw_data: Vec = Vec::new(); + file.read(&mut raw_data)?; + + // construct words + (0..(raw_data.len() / 2)).for_each(|i| { + let bytes: [u8; 2] = [raw_data[i * 2], raw_data[i * 2 + 1]]; + self.data[i] = u16::from_le_bytes(bytes); + }); + Ok(()) + } } impl std::ops::Index for Memory {