1 /*****************************************************************************/
5 /* RAM plugin for the sim65 6502 simulator */
9 /* (C) 2002-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 /*****************************************************************************/
46 /*****************************************************************************/
50 static int InitChip (const struct SimData* Data);
51 /* Initialize the chip, return an error code */
53 static void* CreateInstance (unsigned Addr, unsigned Range, void* CfgInfo);
54 /* Create a new chip instance */
56 static void DestroyInstance (void* Data);
57 /* Destroy a chip instance */
59 static void WriteCtrl (void* Data, unsigned Offs, unsigned char Val);
60 /* Write control data */
62 static void Write (void* Data, unsigned Offs, unsigned char Val);
65 static unsigned char ReadCtrl (void* Data, unsigned Offs);
66 /* Read control data */
68 static unsigned char Read (void* Data, unsigned Offs);
73 /*****************************************************************************/
75 /*****************************************************************************/
79 /* Control data passed to the main program */
80 static const struct ChipData CData[1] = {
82 "RAM", /* Name of the chip */
83 CHIPDATA_TYPE_CHIP, /* Type of the chip */
84 CHIPDATA_VER_MAJOR, /* Version information */
87 /* -- Exported functions -- */
98 /* The SimData pointer we get when InitChip is called */
99 static const SimData* Sim;
101 /* Possible RAM attributes */
102 #define ATTR_INITIALIZED 0x01 /* RAM cell is intialized */
103 #define ATTR_WPROT 0x02 /* RAM cell is write protected */
105 /* Data for one RAM instance */
106 typedef struct InstanceData InstanceData;
107 struct InstanceData {
108 unsigned BaseAddr; /* Base address */
109 unsigned Range; /* Memory range */
110 unsigned char* MemAttr; /* Memory attributes */
111 unsigned char* Mem; /* The memory itself */
116 /*****************************************************************************/
117 /* Exported function */
118 /*****************************************************************************/
122 int GetChipData (const ChipData** Data, unsigned* Count)
124 /* Pass the control structure to the caller */
126 *Count = sizeof (CData) / sizeof (CData[0]);
128 /* Call was successful */
134 /*****************************************************************************/
136 /*****************************************************************************/
140 static int InitChip (const struct SimData* Data)
141 /* Initialize the chip, return an error code */
143 /* Remember the pointer */
146 /* Always successful */
152 static void* CreateInstance (unsigned Addr, unsigned Range, void* CfgInfo)
153 /* Create a new chip instance */
157 /* Allocate a new instance structure */
158 InstanceData* D = Sim->Malloc (sizeof (InstanceData));
160 /* Initialize the structure, allocate RAM and attribute memory */
163 D->MemAttr = Sim->Malloc (Range * sizeof (D->MemAttr[0]));
164 D->Mem = Sim->Malloc (Range * sizeof (D->Mem[0]));
166 /* Check if a value is given that should be used to clear the RAM.
167 * Otherwise use random values.
169 if (Sim->GetCfgNum (CfgInfo, "fill", &Val) == 0) {
170 /* No "fill" attribute */
172 for (I = 0; I < Range; ++I) {
173 D->Mem[I] = (unsigned char) rand ();
176 /* Got a "fill" attribute */
177 memset (D->Mem, (int) Val, Range);
180 /* Clear the attribute memory */
181 memset (D->MemAttr, 0, Range * sizeof (D->MemAttr[0]));
183 /* Done, return the instance data */
189 static void DestroyInstance (void* Data)
190 /* Destroy a chip instance */
192 /* Cast the data pointer */
193 InstanceData* D = (InstanceData*) Data;
195 /* Free memory and attributes */
197 Sim->Free (D->MemAttr);
199 /* Free the instance data itself */
205 static void WriteCtrl (void* Data, unsigned Offs, unsigned char Val)
206 /* Write control data */
208 /* Cast the data pointer */
209 InstanceData* D = (InstanceData*) Data;
211 /* Do the write and remember the cell as initialized */
213 D->MemAttr[Offs] |= ATTR_INITIALIZED;
218 static void Write (void* Data, unsigned Offs, unsigned char Val)
219 /* Write user data */
221 /* Cast the data pointer */
222 InstanceData* D = (InstanceData*) Data;
224 /* Check for a write to a write protected cell */
225 if (D->MemAttr[Offs] & ATTR_WPROT) {
226 Sim->Break ("Writing to write protected memory at $%04X", D->BaseAddr+Offs);
229 /* Do the write and remember the cell as initialized */
231 D->MemAttr[Offs] |= ATTR_INITIALIZED;
236 static unsigned char ReadCtrl (void* Data, unsigned Offs)
237 /* Read control data */
239 /* Cast the data pointer */
240 InstanceData* D = (InstanceData*) Data;
242 /* Read the cell and return the value */
248 static unsigned char Read (void* Data, unsigned Offs)
251 /* Cast the data pointer */
252 InstanceData* D = (InstanceData*) Data;
254 /* Check for a read from an uninitialized cell */
255 if ((D->MemAttr[Offs] & ATTR_INITIALIZED) == 0) {
256 /* We're reading a memory cell that was never written to */
257 Sim->Break ("Reading from uninitialized memory at $%04X", D->BaseAddr+Offs);
260 /* Read the cell and return the value */