1 /* Mapping ranges, the building blocks of the physical memory system.
2    Copyright 2001, 2003 Brian R. Gaeke.
3 
4 This file is part of VMIPS.
5 
6 VMIPS is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2 of the License, or (at your
9 option) any later version.
10 
11 VMIPS is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15 
16 You should have received a copy of the GNU General Public License along
17 with VMIPS; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19 
20 #include "range.h"
21 #include "accesstypes.h"
22 #include "error.h"
23 #include <cassert>
24 
25 /* Returns true if ADDR is mapped by this Range object; false otherwise. */
26 bool
incorporates(uint32 addr)27 Range::incorporates(uint32 addr)
28 {
29 	uint32 base = getBase ();
30 	return (addr >= base) && (addr < (base + getExtent()));
31 }
32 
33 bool
overlaps(Range * r)34 Range::overlaps(Range *r)
35 {
36 	assert(r);
37 
38 	uint32 end = getBase() + getExtent();
39 	uint32 r_end = r->getBase() + r->getExtent();
40 
41 	/* Key: --- = this, +++ = r, *** = overlap */
42 	/* [---[****]+++++++] */
43 	if (getBase() <= r->getBase() && end > r->getBase())
44 		return true;
45 	/* [+++++[***]------] */
46 	else if (r->getBase() <= getBase() && r_end > getBase())
47 		return true;
48 	/* [+++++[****]+++++] */
49 	else if (getBase() >= r->getBase() && end <= r_end)
50 		return true;
51 	/* [---[********]---] */
52 	else if (r->getBase() >= getBase() && r_end <= end)
53 		return true;
54 
55 	/* If we got here, we've determined the ranges don't overlap. */
56 	return false;
57 }
58 
59 uint32
fetch_word(uint32 offset,int mode,DeviceExc * client)60 Range::fetch_word(uint32 offset, int mode, DeviceExc *client)
61 {
62 	return ((uint32 *)address)[offset / 4];
63 }
64 
65 uint16
fetch_halfword(uint32 offset,DeviceExc * client)66 Range::fetch_halfword(uint32 offset, DeviceExc *client)
67 {
68 	return ((uint16 *)address)[offset / 2];
69 }
70 
71 uint8
fetch_byte(uint32 offset,DeviceExc * client)72 Range::fetch_byte(uint32 offset, DeviceExc *client)
73 {
74 	return ((uint8 *)address)[offset];
75 }
76 
77 void
store_word(uint32 offset,uint32 data,DeviceExc * client)78 Range::store_word(uint32 offset, uint32 data, DeviceExc *client)
79 {
80 	uint32 *werd;
81 	/* calculate address */
82 	werd = ((uint32 *) address) + (offset / 4);
83 	/* store word */
84 	*werd = data;
85 }
86 
87 void
store_halfword(uint32 offset,uint16 data,DeviceExc * client)88 Range::store_halfword(uint32 offset, uint16 data, DeviceExc *client)
89 {
90 	uint16 *halfwerd;
91 	/* calculate address */
92 	halfwerd = ((uint16 *) address) + (offset / 2);
93 	/* store halfword */
94 	*halfwerd = data;
95 }
96 
97 void
store_byte(uint32 offset,uint8 data,DeviceExc * client)98 Range::store_byte(uint32 offset, uint8 data, DeviceExc *client)
99 {
100 	uint8 *byte;
101 	byte = ((uint8 *) address) + offset;
102 	/* store halfword */
103 	*byte = data;
104 }
105 
106