Skip to content

Commit

Permalink
feat(utils): create dedicated utils module;
Browse files Browse the repository at this point in the history
  • Loading branch information
5-pebbles committed Sep 6, 2024
1 parent 0508ef6 commit abc1509
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/emulation/memory.rs
Original file line number Diff line number Diff line change
@@ -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],
Expand Down
2 changes: 2 additions & 0 deletions src/emulation/program_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod emulation;
mod character_encoding;
mod errors;
mod instruction;
mod utils;

use errors::Error;

Expand Down
9 changes: 9 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit abc1509

Please sign in to comment.