From 8e722999b05769db93cec989307bb3ac0d20e562 Mon Sep 17 00:00:00 2001
From: Pavel Kirilin <win10@list.ru>
Date: Fri, 28 Feb 2020 03:11:56 +0400
Subject: [PATCH] Added free method to CPU and RAM.

Signed-off-by: Pavel Kirilin <win10@list.ru>
---
 src/cpu/logic.rs |  4 ++++
 src/main.rs      |  1 +
 src/ram/logic.rs | 15 +++++++++++++++
 3 files changed, 20 insertions(+)

diff --git a/src/cpu/logic.rs b/src/cpu/logic.rs
index 8800084..ee1fe7d 100644
--- a/src/cpu/logic.rs
+++ b/src/cpu/logic.rs
@@ -21,4 +21,8 @@ impl CPU {
         }
         (&mut self.memory).store_instructions(instructions)
     }
+
+    pub fn free(&mut self) -> MSLResult<()> {
+        (&mut self.memory).free()
+    }
 }
diff --git a/src/main.rs b/src/main.rs
index 7cd6f7e..75669bc 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -98,6 +98,7 @@ fn main() -> MSLResult<()> {
     } else {
         repl::start_repl_session()?;
     }
+    cpu.free()?;
     Ok(())
     //    println!("{:#?}", opt);
 }
diff --git a/src/ram/logic.rs b/src/ram/logic.rs
index 7b43fb5..81d199d 100644
--- a/src/ram/logic.rs
+++ b/src/ram/logic.rs
@@ -32,6 +32,7 @@ impl RAM {
     ) -> MSLResult<()> {
         if address < self.mem.len() {
             self.mem[address] = data;
+            debug!("Data stored in RAM 0x{}", address);
             Ok(())
         } else {
             Err(MSLError::MemoryError(format!(
@@ -43,9 +44,23 @@ impl RAM {
     }
 
     pub fn store_instructions(&mut self, instructions: Vec<Instruction>) -> MSLResult<()> {
+        let instruction_len = instructions.len();
         for (i, item) in instructions.into_iter().enumerate() {
             self.store_data(i, RamValue::INSTR(item))?;
         }
+        debug!("{} instructions was loaded into RAM.", instruction_len);
+        Ok(())
+    }
+
+    pub fn free(&mut self) -> MSLResult<()> {
+        let freed = self.mem.iter().filter_map(|x| {
+            if RamValue::EMPTY == *x {
+                None
+            } else {
+                Some(RamValue::EMPTY)
+            }
+        }).collect::<Vec<_>>().len();
+        debug!("{} cells of RAM was freed.", freed);
         Ok(())
     }
 }
-- 
GitLab