Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / sam / devices / common / regdef / include / Map.h
CommitLineData
920dae64
AT
1/*
2* ========== Copyright Header Begin ==========================================
3*
4* OpenSPARC T2 Processor File: Map.h
5* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
6* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
7*
8* The above named program is free software; you can redistribute it and/or
9* modify it under the terms of the GNU General Public
10* License version 2 as published by the Free Software Foundation.
11*
12* The above named program is distributed in the hope that it will be
13* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15* General Public License for more details.
16*
17* You should have received a copy of the GNU General Public
18* License along with this work; if not, write to the Free Software
19* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20*
21* ========== Copyright Header End ============================================
22*/
23#ifndef __Map_h
24#define __Map_h
25
26#include <stdlib.h>
27#include <assert.h>
28#include <string>
29#include "Datatype.h"
30using namespace std;
31
32/** @file Map.h
33 * Map.h defines classes for implementing an efficient address map.
34 *
35 * @sa Map_Module
36 */
37
38/** @defgroup Map_Module The Map Module
39 * The Map module defines classes for implementing an efficient address
40 * map. The map has a very simple interface, though the
41 * implementation is intended to be fast and space-efficient, even for
42 * large sparse maps. The implementation uses template classes so that
43 * the Map class is independent of the datatype of the objects contained
44 * in the address space.
45 *
46 * This class is very useful for mapping an address
47 * to a Register. In fact, it is specialized to the case where the
48 * address refers to an 8-byte-aligned, 8-byte-sized piece of memory.
49 * Ideally this restriction should be generalized.
50 *
51 * @{
52 */
53
54// MAP_TOTAL_BITS is 43 to allow a map to cover whole 43-bit JBUS address
55
56static const uint MAP_LEVEL0_BITS = 10; // most significant bits
57static const uint MAP_LEVEL1_BITS = 10;
58static const uint MAP_LEVEL2_BITS = 10;
59static const uint MAP_LEVEL3_BITS = 10;
60static const uint MAP_LEVEL4_BITS = 3; // least significant bits
61static const uint MAP_TOTAL_BITS = MAP_LEVEL0_BITS + MAP_LEVEL1_BITS +
62 MAP_LEVEL2_BITS + MAP_LEVEL3_BITS +
63 MAP_LEVEL4_BITS;
64
65static const uint MAP_WIDTH = 1 << MAP_LEVEL4_BITS;
66static const Addr MAP_MIN_ADDR = 0;
67static const Addr MAP_MAX_ADDR = (Addr(1) << MAP_TOTAL_BITS) - 1;
68
69/** The MapGeneric class defines the generic interface to a map
70 * class. This allows the user of a map to be independent of the
71 * implementation of the map. The destructor is virtual so that
72 * it can be overloaded. The insert, remove and lookup methods
73 * are virtual and typically overloaded by a sub-class in
74 * order to give a specific Map implementation.
75 */
76
77template<class T> class MapGeneric
78{
79public:
80 /** Constructor for MapGeneric.
81 * @param n the name of the map.
82 * @param s the start address of the map.
83 * @param e the end address of the map.
84 * @param w the size in bytes of elements in the map. This must
85 * be an integral power of 2.
86 *
87 * A map will be provided for the address range [s, e]. Addresses
88 * outside this range will fail.
89 */
90 MapGeneric(string n, Addr s, Addr e, uint w);
91
92 /** Destructor for MapGeneric. */
93 virtual ~MapGeneric() {}
94
95 /** Insert a mapping from the specified address to the specified item.
96 * @param addr the address to be inserted into the map.
97 * @param item a pointer to the item assoicated with this address.
98 * @return true if the mapping was inserted successfully, otherwise false.
99 */
100 virtual bool insert(Addr addr, T *item) { return false; }
101
102 /** Remove a mapping from the specified address, and return a pointer
103 * to the associated item (if any).
104 * @param addr the address to be removed from the map.
105 * @return a pointer to the associated object, or NULL if no object at
106 * the specified address.
107 */
108 virtual T *remove(Addr addr) { return NULL; }
109
110 /** Lookup a mapping at the specified address, and return a pointer
111 * to the associated item (if any).
112 * @param addr the address to be looked up in the map.
113 * @return a pointer to the associated object, or NULL if no object at
114 * the specified address.
115 */
116 virtual T *lookup(Addr addr) { return NULL; }
117
118protected:
119 string name;
120 Addr start;
121 Addr end;
122};
123
124template<class T> struct MapLevel0; // forward reference
125
126template<class T> class Map : public MapGeneric<T>
127{
128public:
129 /** Constructor for Map.
130 * @param n the name of the map.
131 * @param s the start address of the map.
132 * @param e the end address of the map.
133 * @param w the size in bytes of elements in the map. For this
134 * implementation w must be exactly 8 bytes (the default)
135 * which corresponds to the size of almost all memory-mapped
136 * registers.
137 *
138 * A map will be provided for the address range [s, e]. Addresses
139 * outside this range will fail.
140 */
141 Map(string n, Addr s=MAP_MIN_ADDR, Addr e=MAP_MAX_ADDR, uint w=MAP_WIDTH);
142
143 /** Destructor for Map. */
144 ~Map();
145
146 /** Insert a mapping from the specified address to the specified item.
147 * @param addr the address to be inserted into the map.
148 * @param item a pointer to the item assoicated with this address.
149 * @return true if the mapping was inserted successfully, otherwise false.
150 */
151 bool insert(Addr addr, T *item);
152
153 /** Remove a mapping from the specified address, and return a pointer
154 * to the associated item (if any).
155 * @param addr the address to be removed from the map.
156 * @return a pointer to the associated object, or NULL if no object at
157 * the specified address.
158 */
159 T *remove(Addr addr);
160
161 /** Lookup a mapping at the specified address, and return a pointer
162 * to the associated item (if any).
163 * @param addr the address to be looked up in the map.
164 * @return a pointer to the associated object, or NULL if no object at
165 * the specified address.
166 */
167 T *lookup(Addr addr);
168
169private:
170 MapLevel0<T> *map;
171 uint count_level0;
172 uint count_level1;
173 uint count_level2;
174 uint count_level3;
175 uint count_level4;
176 uint64 bytes;
177};
178
179#include "pMap.h"
180
181#endif