1 /*****************************************************************************/
5 /* CPU address space for the 6502 simulator */
9 /* (C) 2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
44 #include "addrspace.h"
48 /*****************************************************************************/
50 /*****************************************************************************/
54 AddressSpace* NewAddressSpace (unsigned Size)
55 /* Allocate a new address space and return it */
60 AddressSpace* AS = xmalloc (sizeof (AddressSpace) +
61 (Size - 1) * sizeof (ChipInstance*));
63 /* Initialize the struct */
66 for (I = 0; I < Size; ++I) {
70 /* Return the new struct */
76 void ASWrite (AddressSpace* AS, unsigned Addr, unsigned char Val)
77 /* Write a byte to a given location */
79 const ChipInstance* CI;
81 /* Make sure, the addresses are in a valid range */
82 PRECONDITION (Addr < AS->Size);
84 /* Get the instance of the chip at this address */
87 /* Check if the memory is mapped */
89 Break ("Writing to unassigned memory at $%06X", Addr);
91 CI->C->Data->Write (CI->Data, Addr - CI->Addr, Val);
97 unsigned char ASRead (AddressSpace* AS, unsigned Addr)
98 /* Read a byte from a location */
100 const ChipInstance* CI;
102 /* Make sure, the addresses are in a valid range */
103 PRECONDITION (Addr < AS->Size);
105 /* Get the instance of the chip at this address */
108 /* Check if the memory is mapped */
110 Break ("Reading from unassigned memory at $%06X", Addr);
113 return CI->C->Data->Read (CI->Data, Addr - CI->Addr);
119 void ASWriteCtrl (AddressSpace* AS, unsigned Addr, unsigned char Val)
120 /* Write a byte to a given location */
122 const ChipInstance* CI;
124 /* Make sure, the addresses are in a valid range */
125 PRECONDITION (Addr < AS->Size);
127 /* Get the instance of the chip at this address */
130 /* Check if the memory is mapped */
132 Break ("Writing to unassigned memory at $%06X", Addr);
134 CI->C->Data->WriteCtrl (CI->Data, Addr - CI->Addr, Val);
140 unsigned char ASReadCtrl (AddressSpace* AS, unsigned Addr)
141 /* Read a byte from a location */
143 const ChipInstance* CI;
145 /* Make sure, the addresses are in a valid range */
146 PRECONDITION (Addr < AS->Size);
148 /* Get the instance of the chip at this address */
151 /* Check if the memory is mapped */
153 Break ("Reading from unassigned memory at $%06X", Addr);
156 return CI->C->Data->ReadCtrl (CI->Data, Addr - CI->Addr);
161 void ASAssignChip (AddressSpace* AS, ChipInstance* CI,
162 unsigned Addr, unsigned Range)
163 /* Assign a chip instance to memory locations */
165 /* Make sure, the addresses are in a valid range */
166 PRECONDITION (Addr + Range <= AS->Size);
168 /* Assign the chip instance */
170 CHECK (AS->Data[Addr] == 0);
171 AS->Data[Addr++] = CI;
174 /* Set the backpointer to us */
179 ChipInstance* ASGetChip (const AddressSpace* AS, unsigned Addr)
180 /* Get the chip that is located at the given address (may return NULL). */
182 /* Make sure, the addresses are in a valid range */
183 PRECONDITION (Addr < AS->Size);
185 /* Return the chip instance */
186 return AS->Data[Addr];