Compare commits

...

6 Commits

Author SHA1 Message Date
Rhinemann ed4a9f08cd Playing with tables. 2023-06-28 00:10:53 +03:00
Rhinemann f522eab9bf Minor code cleanup. 2023-06-27 23:46:26 +03:00
Rhinemann ca91c2ea56 Added documentation. 2023-06-27 23:45:55 +03:00
Rhinemann 7ab69ecbe8 Very minor cleanup. 2023-06-27 22:34:57 +03:00
Rhinemann 1e505683cb Shortened summation code. 2023-06-27 22:28:38 +03:00
Rhinemann 289b87a556 Rewrote bit shifts. 2023-06-27 22:28:01 +03:00
2 changed files with 56 additions and 30 deletions

View File

@ -13,4 +13,6 @@ panic = 'abort' # Abort on panic
strip = true # Strip symbols from binary*
[dependencies]
text_io = "0.1.12"
text_io = "0.1.12"
prettytable-rs = "0.10.0"
#tabled = "0.12.2"

View File

@ -1,5 +1,5 @@
use prettytable::{Cell, Row, Table};
use std::collections::VecDeque;
use std::fmt::Formatter;
use std::{cmp, fmt};
fn capitalise(s: &str) -> String {
@ -18,6 +18,7 @@ pub struct BasicRegister {
}
impl BasicRegister {
/// Adjusts a register to a given size in bits.
pub fn adjusted_by_size(&mut self, resulting_size: usize) -> Self {
let current_memory_size: usize = self.memory.len();
let difference: i32 = current_memory_size as i32 - resulting_size as i32;
@ -36,14 +37,12 @@ impl BasicRegister {
}
}
pub fn negate(&mut self) {
self.memory = self.memory.iter().map(|val| !val).collect();
}
/// Returns a register which was logically negated.
pub fn negated(&self) -> Self {
BasicRegister::new(self.memory.iter().map(|val| !val).collect())
}
/// Shifts the register to the left by a specified number of steps, shifting in the provided values.
pub fn left_shift(
&mut self,
shift_in_value_default: Option<bool>,
@ -53,12 +52,15 @@ impl BasicRegister {
let bits_shifted: usize = bits_shifted_default.unwrap_or(1) as usize;
let mut shifted_bits: VecDeque<bool> = [].into();
(0..bits_shifted).for_each(|_| shifted_bits.push_back(self.memory.pop_front().unwrap()));
(0..bits_shifted).for_each(|_| self.memory.push_back(shift_in_value));
for _i in 0..bits_shifted {
shifted_bits.push_back(self.memory.pop_front().unwrap());
self.memory.push_back(shift_in_value)
}
shifted_bits
}
/// Shifts the register to the left by a specified number of steps, shifting in the provided values.
pub fn right_shift(
&mut self,
shift_in_value_default: Option<bool>,
@ -68,19 +70,35 @@ impl BasicRegister {
let bits_shifted: usize = bits_shifted_default.unwrap_or(1) as usize;
let mut shifted_bits: VecDeque<bool> = [].into();
(0..bits_shifted).for_each(|_| shifted_bits.push_back(self.memory.pop_back().unwrap()));
(0..bits_shifted).for_each(|_| self.memory.push_front(shift_in_value));
for _i in 0..bits_shifted {
shifted_bits.push_back(self.memory.pop_back().unwrap());
self.memory.push_front(shift_in_value)
}
shifted_bits
}
/// Constructs a new BasicRegister from a given VecDeque<bool> as memory.
pub fn new(memory: VecDeque<bool>) -> Self {
Self { memory }
}
/// Returns the number of bits stored in a register.
pub fn len(&self) -> usize {
self.memory.len()
}
/*pub fn table(&self) -> Table {
let mut table = Table::new();
let mut memory_row: Vec<Cell> = vec![Cell::new("Memory")];
self.memory
.iter()
.for_each(|value| memory_row.push(Cell::new((*value as u8).to_string().as_str())));
table.add_row(Row::new(memory_row));
table
}*/
}
impl Default for BasicRegister {
@ -92,7 +110,7 @@ impl Default for BasicRegister {
}
impl fmt::Display for BasicRegister {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Memory: [")?;
for (count, v) in self.memory.iter().enumerate() {
@ -106,8 +124,8 @@ impl fmt::Display for BasicRegister {
}
}
impl fmt::Binary for BasicRegister {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
/*impl fmt::Binary for BasicRegister {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Memory: ")?;
for v in self.memory.iter() {
@ -116,12 +134,18 @@ impl fmt::Binary for BasicRegister {
write!(f, "")
}
}
}*/
/// Converts a u8 number to a boolean.
///
/// # Returns
/// - false - if the number is zero or below.
/// - true - if the number is above zero.
fn u8_to_bool(number: u8) -> bool {
number > 0
}
/// Reads a Vec<char> containing only 1-s and 0-s from user.
fn read_vec(variable_name: &str) -> Vec<char> {
loop {
print!("Enter {variable_name}: ");
@ -136,10 +160,11 @@ fn read_vec(variable_name: &str) -> Vec<char> {
}
}
pub fn char_to_bool_vector(char_vector: Vec<char>) -> VecDeque<bool> {
/// Converts a Vec<char> containing only 1-s and 0-s to VecDeque<bool>.
pub fn char_to_bool_vecdeque(char_vector: Vec<char>) -> VecDeque<bool> {
let mut bool_vector: VecDeque<bool> = VecDeque::new();
for value in char_vector.iter() {
for value in char_vector.into_iter() {
match value {
'0' => bool_vector.push_back(false),
'1' => bool_vector.push_back(true),
@ -150,17 +175,17 @@ pub fn char_to_bool_vector(char_vector: Vec<char>) -> VecDeque<bool> {
bool_vector
}
/// Handles getting the memory for the register from the user.
pub fn get_memory(variable_name: &str) -> VecDeque<bool> {
char_to_bool_vector(read_vec(variable_name))
char_to_bool_vecdeque(read_vec(variable_name))
}
/// Aligns two registers by the length of the bigger one.
pub fn align_registers(
mut first_register: BasicRegister,
mut second_register: BasicRegister,
) -> (BasicRegister, BasicRegister) {
let first_size: usize = first_register.memory.len();
let second_size: usize = second_register.memory.len();
let required_size: usize = cmp::max(first_size, second_size);
let required_size: usize = cmp::max(first_register.len(), second_register.len());
(
first_register.adjusted_by_size(required_size),
@ -168,6 +193,8 @@ pub fn align_registers(
)
}
/// Sums two terms containing binary numbers and keeps the carry-out.
/// Returns a tuple containing the register with result and a carry-out.
fn binary_sum_with_carry(
first_term: &BasicRegister,
second_term: &BasicRegister,
@ -176,34 +203,31 @@ fn binary_sum_with_carry(
let mut carry: bool = false;
let mut current_bit_sum: u8;
for i in (1..first_term.len()).rev() {
for i in (0..first_term.len()).rev() {
current_bit_sum = first_term.memory[i] as u8 + second_term.memory[i] as u8 + carry as u8;
carry = u8_to_bool(current_bit_sum & 2);
sum.memory[i] = u8_to_bool(current_bit_sum & 1);
}
let final_bit_sum: u8 = first_term.memory[0] as u8 + second_term.memory[0] as u8 + carry as u8;
let final_carry: bool = u8_to_bool(final_bit_sum & 2);
sum.memory[0] = u8_to_bool(final_bit_sum & 1);
(sum, final_carry)
(sum, carry)
}
/// Sums two terms containing binary numbers.
/// Returns the BasicRegister with the result.
pub fn binary_sum(first_term: &BasicRegister, second_term: &BasicRegister) -> BasicRegister {
binary_sum_with_carry(first_term, second_term).0
}
/// Subtracts the value of the subtrahend from the minuend in binary using ones' complement.
pub fn binary_subtraction(minuend: &BasicRegister, subtrahend: &BasicRegister) -> BasicRegister {
let (difference, final_carry) = binary_sum_with_carry(minuend, &subtrahend.negated());
match final_carry {
true => {
let mut complement = vec![false; difference.len() - 1];
complement.append(&mut vec![true]);
let mut complement: Vec<bool> = vec![false; difference.len() - 1];
complement.extend([true]);
binary_sum(&difference, &BasicRegister::new(complement.into()))
}
false => difference.negated(),
}
// BasicRegister::default()
}