Class: CacheSim::Cache

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Cache

Note:

With this example, there are 16 slots and each slot has space for 16 pieces of data

Initializes new Cache object

Examples:

Create a new cache

cache = CacheSim::Cache.new(16)

Parameters:

  • size (Integer)

    reflects the number and size of slots.



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_memoryMainMemory (readonly)

Retrieves current contents of main memory

Returns:



74
75
76
# File 'cache_simulation.rb', line 74

def main_memory
  @main_memory
end

#sizeInteger

Returns size of cache

Returns:

  • (Integer)

    number of slots in cache (and length of each slot array)



68
69
70
# File 'cache_simulation.rb', line 68

def size
  @size
end

#slotsArray

Returns an array of slots

Returns:

  • (Array)

    array of slots



71
72
73
# File 'cache_simulation.rb', line 71

def slots
  @slots
end

Instance Method Details

#display_cachevoid

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.

Parameters:

  • address (String)

    address that we want to read from



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.

Parameters:

  • address (String)

    address that the data should be written to

  • data (String)

    data to be written to 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

TODO:

This function could be redone to take user input instead

Note:

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

Examples:

Given an example array of operations

cache.perform_actions(["R", "4C3", "D", "W", "14C", "99"])
=> to screen:
(R)ead, (W)rite, or (D)isplay cache?
R
What address would you like read?
4C3
At that byte, there is the value c3 (Cache miss)
(R)ead, (W)rite, or (D)isplay cache?
D
Slot | Valid | Tag | Data
  0  |   1   |  0  | 0 1 2 3 4 5 6 7 8 9 a b c d e f
  1  |   0   |  0  | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  2  |   0   |  0  | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  3  |   0   |  0  | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  4  |   1   |  1  | 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
  5  |   1   |  1  | 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
  6  |   0   |  0  | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  7  |   0   |  0  | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  8  |   0   |  0  | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  9  |   0   |  0  | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  a  |   1   |  3  | a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af
  b  |   0   |  0  | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  c  |   1   |  4  | c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf
  d  |   0   |  0  | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  e  |   0   |  0  | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  f  |   0   |  0  | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
(R)ead, (W)rite, or (D)isplay cache?
W
What address would you like to write to?
14C
What data would you like to write at that address?
99

Parameters:

  • op (Array)

    an array of strings representing operations on the cache. Actions are R, W, and D, and each of the actions must be followed by the correct value or address.

Raises:

  • (ArgumentError)

    expected a D, W, or R and got something else



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