Class: CacheSim::Cache
- Inherits:
-
Object
- Object
- CacheSim::Cache
- Defined in:
- cache_simulation.rb
Overview
This is the class that creates the actual cache and performs most of the actions.
Initialization:
cache = CacheSim::Cache.new(size)
MainMemory is initialized here and, while the contents can be changed, main_memory itself cannot. Ex. I cannot create a brand new instance of main_memory once the cache is initialized
Instance Attribute Summary collapse
-
#main_memory ⇒ MainMemory
readonly
Retrieves current contents of main memory.
-
#size ⇒ Integer
Returns size of cache.
-
#slots ⇒ Array
Returns an array of slots.
Instance Method Summary collapse
-
#display_cache ⇒ void
Displays the cache in it's current state.
-
#display_read(address) ⇒ void
Takes an address, prints out the actions you are taking, determines if it's a hit or a miss, then reads the data from the cache.
-
#display_write(address, data) ⇒ void
Takes an address and data, prints out the actions you are taking, determines if it's a hit or a miss, then writes that data to the cache.
-
#initialize(size) ⇒ Cache
constructor
Initializes new Cache object.
-
#perform_actions(op) ⇒ void
Takes an array of operations and performs them on the cache.
Constructor Details
#initialize(size) ⇒ Cache
With this example, there are 16 slots and each slot has space for 16 pieces of data
Initializes new Cache object
81 82 83 84 85 |
# File 'cache_simulation.rb', line 81 def initialize(size) @size = size @slots = initialize_slots @main_memory = CacheSim::MainMemory.new(8*(@size**2)-1) end |
Instance Attribute Details
#main_memory ⇒ MainMemory (readonly)
Retrieves current contents of main memory
74 75 76 |
# File 'cache_simulation.rb', line 74 def main_memory @main_memory end |
#size ⇒ Integer
Returns size of cache
68 69 70 |
# File 'cache_simulation.rb', line 68 def size @size end |
#slots ⇒ Array
Returns an array of slots
71 72 73 |
# File 'cache_simulation.rb', line 71 def slots @slots end |
Instance Method Details
#display_cache ⇒ void
This method returns an undefined value.
Displays the cache in it's current state
177 178 179 180 181 182 |
# File 'cache_simulation.rb', line 177 def display_cache puts "Slot | Valid | Tag | Data " @slots.each do |slot| puts " #{slot.num} | #{slot.is_valid} | #{slot.tag} | #{slot.saved_blocks.join(" ")}" end end |
#display_read(address) ⇒ void
This method returns an undefined value.
Takes an address, prints out the actions you are taking, determines if it's a hit or a miss, then reads the data from the cache.
168 169 170 171 172 173 |
# File 'cache_simulation.rb', line 168 def display_read(address) puts "What address would you like read?" puts address value, hit_miss = read(address) puts "At that byte, there is the value #{value} (Cache #{hit_miss})" end |
#display_write(address, data) ⇒ void
This method returns an undefined value.
Takes an address and data, prints out the actions you are taking, determines if it's a hit or a miss, then writes that data to the cache.
155 156 157 158 159 160 161 162 |
# File 'cache_simulation.rb', line 155 def display_write(address, data) puts "What address would you like to write to?" puts address puts "What data would you like to write at that address?" puts data hit_miss = write(address, data) puts "Value #{data} has been written to address #{address} (Cache #{hit_miss})" end |
#perform_actions(op) ⇒ void
This function could be redone to take user input instead
This function prints out the cache as it goes along
This method returns an undefined value.
Takes an array of operations and performs them on the cache
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'cache_simulation.rb', line 128 def perform_actions(op) i = 0 while i < op.length get_action(op[i]) if op[i] == "D" display_cache i+=1 elsif op[i] == "R" display_read(op[i+1]) i+=2 elsif op[i] == "W" display_write(op[i+1], op[i+2]) i+=3 else # added in as a double check in case I got off track # if user input, would be a check to the user that they didn't # enter a D, R, or W raise ArgumentError.new("Expecting a D, R, or W at this point. Got #{op[i]}.") end end end |