-
Notifications
You must be signed in to change notification settings - Fork 81
/
ssd_address.cpp
204 lines (178 loc) · 4.55 KB
/
ssd_address.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* Copyright 2009, 2010 Brendan Tauras */
/* ssd_address.cpp is part of FlashSim. */
/* FlashSim is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version. */
/* FlashSim is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. */
/* You should have received a copy of the GNU General Public License
* along with FlashSim. If not, see <http://www.gnu.org/licenses/>. */
/****************************************************************************/
/* Address class
* Brendan Tauras 2009-06-19
*
* Class to manage physical addresses for the SSD. It was designed to have
* public members like a struct for quick access but also have checking,
* printing, and assignment functionality. An instance is created for each
* physical address in the Event class.
*/
#include <stdio.h>
#include "ssd.h"
using namespace ssd;
Address::Address(void):
package(0),
die(0),
plane(0),
block(0),
page(0),
valid(NONE)
{
return;
}
Address::Address(const Address &address)
{
*this = address;
return;
}
Address::Address(const Address *address)
{
*this = *address;
return;
}
/* see "enum address_valid" in ssd.h for details on valid status */
Address::Address(uint package, uint die, uint plane, uint block, uint page, enum address_valid valid):
package(package),
die(die),
plane(plane),
block(block),
page(page),
valid(valid)
{
return;
}
Address::Address(uint address, enum address_valid valid):
valid(valid)
{
assert(address >= 0);
set_linear_address(address);
}
Address::~Address()
{
return;
}
/* default values for parameters are the global settings
* see "enum address_valid" in ssd.h for details on valid status
* note that method only checks for out-of-bounds types of errors */
enum address_valid Address::check_valid(uint ssd_size, uint package_size, uint die_size, uint plane_size, uint block_size)
{
enum address_valid tmp = NONE;
/* must check current valid status first
* so we cannot expand the valid status */
if(valid >= PACKAGE && package < ssd_size)
{
tmp = PACKAGE;
if(valid >= DIE && die < package_size)
{
tmp = DIE;
if(valid >= PLANE && plane < die_size)
{
tmp = PLANE;
if(valid >= BLOCK && block < plane_size)
{
tmp = BLOCK;
if(valid >= PAGE && page < block_size)
tmp = PAGE;
}
}
}
}
else
tmp = NONE;
valid = tmp;
return valid;
}
/* returns enum indicating to what level two addresses match
* limits comparison to the fields that are valid */
enum address_valid Address::compare(const Address &address) const
{
enum address_valid match = NONE;
if(package == address.package && valid >= PACKAGE && address.valid >= PACKAGE)
{
match = PACKAGE;
if(die == address.die && valid >= DIE && address.valid >= DIE)
{
match = DIE;
if(plane == address.plane && valid >= PLANE && address.valid >= PLANE)
{
match = PLANE;
if(block == address.block && valid >= BLOCK && address.valid >= BLOCK)
{
match = BLOCK;
if(page == address.page && valid >= PAGE && address.valid >= PAGE)
{
match = PAGE;
}
}
}
}
}
return match;
}
/* default stream is stdout */
void Address::print(FILE *stream)
{
fprintf(stream, "(%d, %d, %d, %d, %d, %d)", package, die, plane, block, page, (int) valid);
return;
}
void Address::set_linear_address(ulong address)
{
real_address = address;
page = address % BLOCK_SIZE;
address /= BLOCK_SIZE;
block = address % PLANE_SIZE;
address /= PLANE_SIZE;
plane = address % DIE_SIZE;
address /= DIE_SIZE;
die = address % PACKAGE_SIZE;
address /= PACKAGE_SIZE;
package = address % SSD_SIZE;
address /= SSD_SIZE;
}
void Address::set_linear_address(ulong address, enum address_valid valid)
{
set_linear_address(address);
this->valid = valid;
}
unsigned long Address::get_linear_address() const
{
return real_address;
}
void Address::operator+(int i)
{
set_linear_address(real_address + i);
}
void Address::operator+(uint i)
{
set_linear_address(real_address + i);
}
Address &Address::operator+=(const uint i)
{
set_linear_address(real_address + i);
return *this;
}
Address &Address::operator=(const Address &rhs)
{
if(this == &rhs)
return *this;
package = rhs.package;
die = rhs.die;
plane = rhs.plane;
block = rhs.block;
page = rhs.page;
valid = rhs.valid;
real_address = rhs.real_address;
return *this;
}