1 // cache.h -- A universal memory cache. -*- C++ -*-
3 // Copyright (C) 2001 Red Hat.
4 // This file is part of SID and is licensed under the GPL.
5 // See the file COPYING.SID for conditions for redistribution.
10 #include "cacheutil.h"
17 using sid::host_int_4;
19 using sidutil::fixed_attribute_map_component;
20 using sidutil::fixed_bus_map_component;
21 using sidutil::fixed_pin_map_component;
22 using sidutil::fixed_accessor_map_component;
23 using sidutil::no_relation_component;
24 using sidutil::callback_pin;
25 using sidutil::make_attribute;
26 using sidutil::parse_attribute;
28 class cache_component;
29 class cache_bus: public bus
32 cache_bus (cache_component& c)
36 cache_component& cache;
38 template <typename DataType>
39 bus::status write_any (host_int_4 addr, DataType data)
41 return cache.write_any (addr, data);
44 template <typename DataType>
45 bus::status read_any (host_int_4 addr, DataType& data)
47 return cache.read_any (addr, data);
50 #define DEFN_METHOD(DataType) \
51 bus::status write(host_int_4 addr, DataType data) throw () { return this->write_any(addr, data); } \
52 bus::status read(host_int_4 addr, DataType& data) throw () { return this->read_any(addr, data); }
54 DEFN_METHOD (sid::big_int_1)
55 DEFN_METHOD (sid::big_int_2)
56 DEFN_METHOD (sid::big_int_4)
57 DEFN_METHOD (sid::big_int_8)
58 DEFN_METHOD (sid::little_int_1)
59 DEFN_METHOD (sid::little_int_2)
60 DEFN_METHOD (sid::little_int_4)
61 DEFN_METHOD (sid::little_int_8)
64 // FIFO cache replacement algorithm
66 class cache_replacement_fifo: public cache_replacement_algorithm
69 void replace (cache_set& cset, cache_line& old_line, cache_line new_line);
75 // Least recently used (LRU) replacement algorithm
77 class cache_replacement_lru: public cache_replacement_algorithm
80 void replace (cache_set& cset, cache_line& old_line, cache_line new_line);
81 void update (cache_set& cset, cache_line& selected);
87 // Random replacement algorithm
89 class cache_replacement_random: public cache_replacement_algorithm
92 void replace (cache_set& cset, cache_line& old_line, cache_line new_line);
95 // Null replacement algorithm; used by direct mapped caches
97 class cache_replacement_null: public cache_replacement_algorithm
100 void replace (cache_set& cset, cache_line& old_line, cache_line new_line);
104 class cache_component: public virtual component,
105 protected fixed_attribute_map_component,
106 protected fixed_bus_map_component,
107 protected fixed_pin_map_component,
108 protected fixed_accessor_map_component,
109 protected no_relation_component
112 cache_component (unsigned asoctvty, unsigned cache_sz,
113 unsigned line_sz, cache_replacement_algorithm& replacer);
115 template <typename DataType> bus::status
116 write_any (host_int_4 addr, DataType data);
118 template <typename DataType> bus::status
119 read_any (host_int_4 addr, DataType& data);
127 callback_pin<cache_component> report_pin;
128 void emit_report (host_int_4 ignore);
130 callback_pin<cache_component> flush_pin;
131 void flush_line (host_int_4 addr);
133 callback_pin<cache_component> invalidate_all_pin;
134 void invalidate_all_lines (host_int_4 ignore);
136 callback_pin<cache_component> invalidate_pin;
137 void invalidate_line (host_int_4 addr);
139 callback_pin<cache_component> prefetch_pin;
140 void prefetch_line (host_int_4 addr);
142 callback_pin<cache_component> lock_pin;
143 void lock_line (host_int_4 addr);
145 callback_pin<cache_component> unlock_pin;
146 void unlock_line (host_int_4 addr);
148 string read_hit_rate ();
149 string write_hit_rate ();
150 string get_nothing () { return ""; }
151 status set_nothing (const string& ignore) { return sid::component::ok; }
152 string associativity ();
153 status dump (const string& ignore);
154 string get_hash_mask ();
155 status set_hash_mask (const string& ignore);
156 string get_hash_shift ();
157 status set_hash_shift (const string& ignore);
159 unsigned line_offset (const cache_line& line, const host_int_4& addr);
161 bus::status read_line (cache_line& line);
162 bus::status write_line (cache_line& line);
164 bool write_allocate_p;
165 bool write_through_p;
167 string report_heading;
172 unsigned long writes;
173 unsigned long read_hits;
174 unsigned long write_hits;
175 unsigned long misaligned_reads;
176 unsigned long misaligned_writes;
177 unsigned long flushes;
178 unsigned long replacements;