]> git.sur5r.net Git - cc65/blob - src/sim65/addrspace.c
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / src / sim65 / addrspace.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                addrspace.c                                */
4 /*                                                                           */
5 /*                 CPU address space for the 6502 simulator                  */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2002-2012, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 /* common */
37 #include "check.h"
38 #include "xmalloc.h"
39
40 /* sim65 */
41 #include "chip.h"
42 #include "cpucore.h"
43 #include "error.h"
44 #include "addrspace.h"
45
46
47
48 /*****************************************************************************/
49 /*                                   Code                                    */
50 /*****************************************************************************/
51
52
53
54 AddressSpace* NewAddressSpace (unsigned Size)
55 /* Allocate a new address space and return it */
56 {
57     unsigned I;
58
59     /* Allocate memory */
60     AddressSpace* AS = xmalloc (sizeof (AddressSpace) +
61                                 (Size - 1) * sizeof (ChipInstance*));
62
63     /* Initialize the struct */
64     AS->CPU  = 0;
65     AS->Size = Size;
66     for (I = 0; I < Size; ++I) {
67         AS->Data[I] = 0;
68     }
69
70     /* Return the new struct */
71     return AS;
72 }
73
74
75
76 void ASWrite (AddressSpace* AS, unsigned Addr, unsigned char Val)
77 /* Write a byte to a given location */
78 {
79     const ChipInstance* CI;
80
81     /* Make sure, the addresses are in a valid range */
82     PRECONDITION (Addr < AS->Size);
83
84     /* Get the instance of the chip at this address */
85     CI = AS->Data[Addr];
86
87     /* Check if the memory is mapped */
88     if (CI == 0) {
89         Break ("Writing to unassigned memory at $%06X", Addr);
90     } else {
91         CI->C->Data->Write (CI->Data, Addr - CI->Addr, Val);
92     }
93 }
94
95
96
97 unsigned char ASRead (AddressSpace* AS, unsigned Addr)
98 /* Read a byte from a location */
99 {
100     const ChipInstance* CI;
101
102     /* Make sure, the addresses are in a valid range */
103     PRECONDITION (Addr < AS->Size);
104
105     /* Get the instance of the chip at this address */
106     CI = AS->Data[Addr];
107
108     /* Check if the memory is mapped */
109     if (CI == 0) {
110         Break ("Reading from unassigned memory at $%06X", Addr);
111         return 0xFF;
112     } else {
113         return CI->C->Data->Read (CI->Data, Addr - CI->Addr);
114     }
115 }
116
117
118
119 void ASWriteCtrl (AddressSpace* AS, unsigned Addr, unsigned char Val)
120 /* Write a byte to a given location */
121 {
122     const ChipInstance* CI;
123
124     /* Make sure, the addresses are in a valid range */
125     PRECONDITION (Addr < AS->Size);
126
127     /* Get the instance of the chip at this address */
128     CI = AS->Data[Addr];
129
130     /* Check if the memory is mapped */
131     if (CI == 0) {
132         Break ("Writing to unassigned memory at $%06X", Addr);
133     } else {
134         CI->C->Data->WriteCtrl (CI->Data, Addr - CI->Addr, Val);
135     }
136 }
137
138
139
140 unsigned char ASReadCtrl (AddressSpace* AS, unsigned Addr)
141 /* Read a byte from a location */
142 {
143     const ChipInstance* CI;
144
145     /* Make sure, the addresses are in a valid range */
146     PRECONDITION (Addr < AS->Size);
147
148     /* Get the instance of the chip at this address */
149     CI = AS->Data[Addr];
150
151     /* Check if the memory is mapped */
152     if (CI == 0) {
153         Break ("Reading from unassigned memory at $%06X", Addr);
154         return 0xFF;
155     } else {
156         return CI->C->Data->ReadCtrl (CI->Data, Addr - CI->Addr);
157     }
158 }
159
160
161 void ASAssignChip (AddressSpace* AS, ChipInstance* CI,
162                    unsigned Addr, unsigned Range)
163 /* Assign a chip instance to memory locations */
164 {
165     /* Make sure, the addresses are in a valid range */
166     PRECONDITION (Addr + Range <= AS->Size);
167
168     /* Assign the chip instance */
169     while (Range--) {
170         CHECK (AS->Data[Addr] == 0);
171         AS->Data[Addr++] = CI;
172     }
173
174     /* Set the backpointer to us */
175     CI->AS = AS;
176 }
177
178
179 ChipInstance* ASGetChip (const AddressSpace* AS, unsigned Addr)
180 /* Get the chip that is located at the given address (may return NULL). */
181 {
182     /* Make sure, the addresses are in a valid range */
183     PRECONDITION (Addr < AS->Size);
184
185     /* Return the chip instance */
186     return AS->Data[Addr];
187 }
188
189
190