OSDN Git Service

Index: component/ChangeLog
[pf3gnuchains/pf3gnuchains3x.git] / sid / component / cache / cache.h
1 // cache.h -- A universal memory cache. -*- C++ -*-
2
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.
6
7 #ifndef CACHE_H
8 #define CACHE_H
9
10 #include "cacheutil.h"
11
12 using std::string;
13 using std::vector;
14
15 using sid::bus;
16 using sid::component;
17 using sid::host_int_4;
18
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;
27
28 class cache_component;
29 class cache_bus: public bus
30 {
31 public:
32   cache_bus (cache_component& c)
33     :cache(c) {}
34
35 private:
36   cache_component& cache;
37
38   template <typename DataType>
39   bus::status write_any (host_int_4 addr, DataType data)
40   {
41     return cache.write_any (addr, data);
42   }
43   
44   template <typename DataType>
45   bus::status read_any (host_int_4 addr, DataType& data)
46   {
47     return cache.read_any (addr, data);
48   }
49
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); }
53   
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)
62 };
63
64 // FIFO cache replacement algorithm
65
66 class cache_replacement_fifo: public cache_replacement_algorithm
67 {
68 public:
69   void replace (cache_set& cset, cache_line& old_line, cache_line new_line);
70
71 private:
72   vector <int> fifo;
73 };
74
75 // Least recently used (LRU) replacement algorithm
76
77 class cache_replacement_lru: public cache_replacement_algorithm
78 {
79 public:
80   void replace (cache_set& cset, cache_line& old_line, cache_line new_line);
81   void update (cache_set& cset, cache_line& selected);
82
83 private:
84   vector <int> lru;
85 };
86
87 // Random replacement algorithm
88
89 class cache_replacement_random: public cache_replacement_algorithm
90 {
91 public:
92   void replace (cache_set& cset, cache_line& old_line, cache_line new_line);
93 };
94
95 // Null replacement algorithm; used by direct mapped caches
96
97 class cache_replacement_null: public cache_replacement_algorithm
98 {
99 public:
100   void replace (cache_set& cset, cache_line& old_line, cache_line new_line);
101 };
102
103 \f
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
110 {
111 public:
112   cache_component (unsigned asoctvty, unsigned cache_sz,
113                    unsigned line_sz, cache_replacement_algorithm& replacer);
114
115   template <typename DataType> bus::status 
116   write_any (host_int_4 addr, DataType data);
117   
118   template <typename DataType> bus::status
119   read_any (host_int_4 addr, DataType& data);
120
121 private:
122   cache acache;
123
124   cache_bus upstream;
125   bus* downstream;
126
127   callback_pin<cache_component> report_pin;
128   void emit_report (host_int_4 ignore);
129
130   callback_pin<cache_component> flush_pin;
131   void flush_line (host_int_4 addr);
132
133   callback_pin<cache_component> invalidate_all_pin;
134   void invalidate_all_lines (host_int_4 ignore);
135
136   callback_pin<cache_component> invalidate_pin;
137   void invalidate_line (host_int_4 addr);
138
139   callback_pin<cache_component> prefetch_pin;
140   void prefetch_line (host_int_4 addr);
141
142   callback_pin<cache_component> lock_pin;
143   void lock_line (host_int_4 addr);
144
145   callback_pin<cache_component> unlock_pin;
146   void unlock_line (host_int_4 addr);
147
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);
158
159   unsigned line_offset (const cache_line& line, const host_int_4& addr); 
160
161   bus::status read_line (cache_line& line);
162   bus::status write_line (cache_line& line);
163
164   bool write_allocate_p;
165   bool write_through_p;
166   bool collect_p;
167   string report_heading;
168
169   struct
170   {
171     unsigned long reads;
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;
179   } stats;
180
181   unsigned line_size;
182   unsigned cache_size;
183   unsigned assoc;
184 };
185
186 #endif // CACHE_H