From abc1509b120bae2ddc2d6473453148fffc344181 Mon Sep 17 00:00:00 2001 From: 5-pebbles <5-pebble@protonmail.com> Date: Fri, 6 Sep 2024 16:32:09 -0400 Subject: [PATCH] feat(utils): create dedicated utils module; --- src/emulation/memory.rs | 6 ++---- src/emulation/program_counter.rs | 2 ++ src/main.rs | 1 + src/utils.rs | 9 +++++++++ 4 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 src/utils.rs diff --git a/src/emulation/memory.rs b/src/emulation/memory.rs index 62fa1ef..84cf78d 100644 --- a/src/emulation/memory.rs +++ b/src/emulation/memory.rs @@ -1,13 +1,11 @@ use arbitrary_int::u6; +use crate::utils::tuple_as_usize; + use super::program_counter::ProgramCounter; const RAM_SIZE: usize = 3902; -fn tuple_as_usize(tuple: (u6, u6)) -> usize { - ((u16::from(tuple.0) << 6) | u16::from(tuple.1)) as usize -} - pub struct Memory { pub program_counter: ProgramCounter, pub ram: [u6; RAM_SIZE], diff --git a/src/emulation/program_counter.rs b/src/emulation/program_counter.rs index 4979260..da9b926 100644 --- a/src/emulation/program_counter.rs +++ b/src/emulation/program_counter.rs @@ -2,6 +2,8 @@ use std::{rc::Rc, sync::Mutex}; use arbitrary_int::{u6, Number}; +use crate::utils::tuple_as_usize; + // TODO add some docs #[derive(Debug, Clone, Default)] diff --git a/src/main.rs b/src/main.rs index 9048989..816969c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ mod emulation; mod character_encoding; mod errors; mod instruction; +mod utils; use errors::Error; diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..8dcd479 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,9 @@ +use arbitrary_int::{u12, u6}; + +pub fn tuple_as_usize(tuple: (u6, u6)) -> usize { + ((u16::from(tuple.0) << 6) | u16::from(tuple.1)) as usize +} + +pub fn tuple_as_u12(tuple: (u6, u6)) -> u12 { + u12::new((u16::from(tuple.0) << 6) | u16::from(tuple.1)) +}