1 /*****************************************************************************/
5 /* RAM plugin for the sim65 6502 simulator */
9 /* (C) 2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 int InitChip (const struct SimData* Data);
51 /* Initialize the chip, return an error code */
53 static void* InitInstance (unsigned Addr, unsigned Range, void* CfgInfo);
54 /* Initialize a new chip instance */
56 static void WriteCtrl (void* Data, unsigned Offs, unsigned char Val);
57 /* Write control data */
59 static void Write (void* Data, unsigned Offs, unsigned char Val);
62 static unsigned char ReadCtrl (void* Data, unsigned Offs);
63 /* Read control data */
65 static unsigned char Read (void* Data, unsigned Offs);
70 /*****************************************************************************/
72 /*****************************************************************************/
76 /* Control data passed to the main program */
77 static const struct ChipData RAMData[1] = {
79 "RAM", /* Name of the chip */
80 CHIPDATA_VER_MAJOR, /* Version information */
83 /* -- Exported functions -- */
93 /* The SimData pointer we get when InitChip is called */
94 static const SimData* Sim;
96 /* Possible RAM attributes */
97 #define ATTR_INITIALIZED 0x01 /* RAM cell is intialized */
98 #define ATTR_WPROT 0x02 /* RAM cell is write protected */
100 /* Data for one RAM instance */
101 typedef struct InstanceData InstanceData;
102 struct InstanceData {
103 unsigned BaseAddr; /* Base address */
104 unsigned Range; /* Memory range */
105 unsigned char* MemAttr; /* Memory attributes */
106 unsigned char* Mem; /* The memory itself */
111 /*****************************************************************************/
112 /* Exported function */
113 /*****************************************************************************/
117 int GetChipData (const ChipData** Data, unsigned* Count)
119 /* Pass the control structure to the caller */
121 *Count = sizeof (Data) / sizeof (Data[0]);
123 /* Call was successful */
129 /*****************************************************************************/
131 /*****************************************************************************/
135 int InitChip (const struct SimData* Data)
136 /* Initialize the chip, return an error code */
138 /* Remember the pointer */
141 /* Always successful */
147 static void* InitInstance (unsigned Addr, unsigned Range, void* CfgInfo)
148 /* Initialize a new chip instance */
150 /* Allocate a new instance structure */
151 InstanceData* D = Sim->Malloc (sizeof (InstanceData));
153 /* Initialize the structure, allocate RAM and attribute memory */
156 D->MemAttr = Sim->Malloc (Range * sizeof (D->MemAttr[0]));
157 D->Mem = Sim->Malloc (Range * sizeof (D->Mem[0]));
159 /* Clear the RAM and attribute memory */
160 memset (D->MemAttr, 0, Range * sizeof (D->MemAttr[0]));
161 memset (D->Mem, 0, Range * sizeof (D->Mem[0]));
163 /* Done, return the instance data */
169 static void WriteCtrl (void* Data, unsigned Offs, unsigned char Val)
170 /* Write control data */
172 /* Cast the data pointer */
173 InstanceData* D = (InstanceData*) Data;
175 /* Do the write and remember the cell as initialized */
177 D->MemAttr[Offs] |= ATTR_INITIALIZED;
182 static void Write (void* Data, unsigned Offs, unsigned char Val)
183 /* Write user data */
185 /* Cast the data pointer */
186 InstanceData* D = (InstanceData*) Data;
188 /* Check for a write to a write protected cell */
189 if (D->MemAttr[Offs] & ATTR_WPROT) {
190 Sim->Warning ("Writing to write protected memory at $%04X", D->BaseAddr+Offs);
193 /* Do the write and remember the cell as initialized */
195 D->MemAttr[Offs] |= ATTR_INITIALIZED;
200 static unsigned char ReadCtrl (void* Data, unsigned Offs)
201 /* Read control data */
203 /* Cast the data pointer */
204 InstanceData* D = (InstanceData*) Data;
206 /* Read the cell and return the value */
212 static unsigned char Read (void* Data, unsigned Offs)
215 /* Cast the data pointer */
216 InstanceData* D = (InstanceData*) Data;
218 /* Check for a read from an uninitialized cell */
219 if ((D->MemAttr[Offs] & ATTR_INITIALIZED) == 0) {
220 /* We're reading a memory cell that was never written to */
221 Sim->Warning ("Reading from uninitialized memory at $%04X", D->BaseAddr+Offs);
224 /* Read the cell and return the value */