]> git.sur5r.net Git - cc65/blob - src/sim65/chips/ram.c
Working
[cc65] / src / sim65 / chips / ram.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                   ram.c                                   */
4 /*                                                                           */
5 /*                  RAM plugin for the sim65 6502 simulator                  */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2002-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 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 #include <stdlib.h>
37 #include <string.h>
38
39 /* sim65 */
40 #include "chipif.h"
41
42
43
44 /*****************************************************************************/
45 /*                                   Forwards                                */
46 /*****************************************************************************/
47
48
49
50 static int InitChip (const struct SimData* Data);
51 /* Initialize the chip, return an error code */
52
53 static void* CreateInstance (unsigned Addr, unsigned Range, void* CfgInfo);
54 /* Create a new chip instance */
55
56 static void DestroyInstance (void* Data);
57 /* Destroy a chip instance */
58
59 static void WriteCtrl (void* Data, unsigned Offs, unsigned char Val);
60 /* Write control data */
61
62 static void Write (void* Data, unsigned Offs, unsigned char Val);
63 /* Write user data */
64
65 static unsigned char ReadCtrl (void* Data, unsigned Offs);
66 /* Read control data */
67
68 static unsigned char Read (void* Data, unsigned Offs);
69 /* Read user data */
70
71
72
73 /*****************************************************************************/
74 /*                                     Data                                  */
75 /*****************************************************************************/
76
77
78
79 /* Control data passed to the main program */
80 static const struct ChipData CData[1] = {
81     {
82         "RAM",                  /* Name of the chip */
83         CHIPDATA_TYPE_CHIP,     /* Type of the chip */
84         CHIPDATA_VER_MAJOR,     /* Version information */
85         CHIPDATA_VER_MINOR,
86
87         /* -- Exported functions -- */
88         InitChip,
89         CreateInstance,
90         DestroyInstance,
91         WriteCtrl,
92         Write,
93         ReadCtrl,
94         Read
95     }
96 };
97
98 /* The SimData pointer we get when InitChip is called */
99 static const SimData* Sim;
100
101 /* Possible RAM attributes */
102 #define ATTR_INITIALIZED        0x01    /* RAM cell is intialized */
103 #define ATTR_WPROT              0x02    /* RAM cell is write protected */
104
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 */
112 };
113
114
115
116 /*****************************************************************************/
117 /*                               Exported function                           */
118 /*****************************************************************************/
119
120
121
122 int GetChipData (const ChipData** Data, unsigned* Count)
123 {
124     /* Pass the control structure to the caller */
125     *Data = CData;
126     *Count = sizeof (CData) / sizeof (CData[0]);
127
128     /* Call was successful */
129     return 0;
130 }
131
132                                        
133
134 /*****************************************************************************/
135 /*                                     Code                                  */
136 /*****************************************************************************/
137
138
139
140 static int InitChip (const struct SimData* Data)
141 /* Initialize the chip, return an error code */
142 {
143     /* Remember the pointer */
144     Sim = Data;
145
146     /* Always successful */
147     return 0;
148 }
149
150
151
152 static void* CreateInstance (unsigned Addr, unsigned Range, void* CfgInfo)
153 /* Create a new chip instance */
154 {
155     long Val;
156
157     /* Allocate a new instance structure */
158     InstanceData* D = Sim->Malloc (sizeof (InstanceData));
159
160     /* Initialize the structure, allocate RAM and attribute memory */
161     D->BaseAddr = Addr;
162     D->Range    = Range;
163     D->MemAttr  = Sim->Malloc (Range * sizeof (D->MemAttr[0]));
164     D->Mem      = Sim->Malloc (Range * sizeof (D->Mem[0]));
165
166     /* Check if a value is given that should be used to clear the RAM.
167      * Otherwise use random values.
168      */
169     if (Sim->GetCfgNum (CfgInfo, "fill", &Val) == 0) {
170         /* No "fill" attribute */
171         unsigned I;
172         for (I = 0; I < Range; ++I) {
173             D->Mem[I] = (unsigned char) rand ();
174         }
175     } else {
176         /* Got a "fill" attribute */
177         memset (D->Mem, (int) Val, Range);
178     }
179
180     /* Clear the attribute memory */
181     memset (D->MemAttr, 0, Range * sizeof (D->MemAttr[0]));
182
183     /* Done, return the instance data */
184     return D;
185 }
186
187
188
189 static void DestroyInstance (void* Data)
190 /* Destroy a chip instance */
191 {
192     /* Cast the data pointer */
193     InstanceData* D = (InstanceData*) Data;
194
195     /* Free memory and attributes */
196     Sim->Free (D->Mem);
197     Sim->Free (D->MemAttr);
198
199     /* Free the instance data itself */
200     free (D);
201 }
202
203
204
205 static void WriteCtrl (void* Data, unsigned Offs, unsigned char Val)
206 /* Write control data */
207 {
208     /* Cast the data pointer */
209     InstanceData* D = (InstanceData*) Data;
210
211     /* Do the write and remember the cell as initialized */
212     D->Mem[Offs] = Val;
213     D->MemAttr[Offs] |= ATTR_INITIALIZED;
214 }
215
216
217
218 static void Write (void* Data, unsigned Offs, unsigned char Val)
219 /* Write user data */
220 {
221     /* Cast the data pointer */
222     InstanceData* D = (InstanceData*) Data;
223
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);
227     }
228
229     /* Do the write and remember the cell as initialized */
230     D->Mem[Offs] = Val;
231     D->MemAttr[Offs] |= ATTR_INITIALIZED;
232 }
233
234
235
236 static unsigned char ReadCtrl (void* Data, unsigned Offs)
237 /* Read control data */
238 {
239     /* Cast the data pointer */
240     InstanceData* D = (InstanceData*) Data;
241
242     /* Read the cell and return the value */
243     return D->Mem[Offs];
244 }
245
246
247
248 static unsigned char Read (void* Data, unsigned Offs)
249 /* Read user data */
250 {
251     /* Cast the data pointer */
252     InstanceData* D = (InstanceData*) Data;
253
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);
258     }
259
260     /* Read the cell and return the value */
261     return D->Mem[Offs];
262 }
263
264
265
266