Previous 15994 Revisions Next

r5771 Thursday 17th September, 2009 at 16:57:05 UTC by Wilbert Pol
Set eol-style
[src/mess/devices]messram.c messram.h

src/mess/devices/messram.c
r5770r5771
1/*************************************************************************
2 
3    MESS RAM device
4 
5    Provides a configurable amount of RAM to drivers
6 
7**************************************************************************/
8 
9#include <stdio.h>
10#include <ctype.h>
11 
12#include "driver.h"
13#include "messram.h"
14 
15 
16/***************************************************************************
17    CONSTANTS
18***************************************************************************/
19 
20#define RAM_STRING_BUFLEN   16
21#define MAX_RAM_OPTIONS      16
22 
23 
24/***************************************************************************
25    TYPE DEFINITIONS
26***************************************************************************/
27 
28typedef struct _messram_state messram_state;
29struct _messram_state
30{
31   UINT32 size; /* total amount of ram configured */
32   UINT8 *ram;  /* pointer to the start of ram */
33};
34 
35 
36/*****************************************************************************
37    INLINE FUNCTIONS
38*****************************************************************************/
39 
40INLINE messram_state *get_safe_token(const device_config *device)
41{
42   assert(device != NULL);
43   assert(device->token != NULL);
44   assert(device->type == MESSRAM);
45 
46   return (messram_state *)device->token;
47}
48 
49 
50/*****************************************************************************
51    HELPER FUNCTIONS
52*****************************************************************************/
53 
54/*-------------------------------------------------
55    parse_string - convert a ram string to an
56   integer value
57-------------------------------------------------*/
58 
59static UINT32 parse_string(const char *s)
60{
61   UINT32 ram;
62   char suffix = '\0';
63 
64   s += sscanf(s, "%u%c", &ram, &suffix);
65 
66   switch(tolower(suffix))
67   {
68   case 'k':
69      /* kilobytes */
70      ram *= 1024;
71      break;
72 
73   case 'm':
74      /* megabytes */
75      ram *= 1024*1024;
76      break;
77 
78   case '\0':
79      /* no suffix */
80      break;
81 
82   default:
83      /* parse failure */
84      ram = 0;
85      break;
86   }
87 
88   return ram;
89}
90 
91 
92/*****************************************************************************
93    DEVICE INTERFACE
94*****************************************************************************/
95 
96static DEVICE_START( messram )
97{
98   messram_state *messram = get_safe_token(device);
99   ram_config *config = device->inline_config;
100   const char *ramsize_string = options_get_string(mame_options(), OPTION_RAMSIZE);
101 
102   /* if we have a command line option, use it */
103   if ((ramsize_string != NULL) && (ramsize_string[0] != '\0'))
104      messram->size = parse_string(ramsize_string);
105   else
106      messram->size = parse_string(config->default_size);
107 
108   /* allocate space for the ram */
109   messram->ram = auto_alloc_array(device->machine, UINT8, messram->size);
110 
111   /* register for state saving */
112   state_save_register_device_item(device, 0, messram->size);
113   state_save_register_device_item_pointer(device, 0, messram->ram, messram->size);
114}
115 
116 
117static DEVICE_RESET( messram )
118{
119   messram_state *messram = get_safe_token(device);
120   ram_config *config = device->inline_config;
121 
122   /* reset ram to the default value */
123   memset(messram->ram, config->default_value, messram->size);
124}
125 
126 
127static DEVICE_VALIDITY_CHECK( messram )
128{
129   ram_config *config = device->inline_config;
130   const char *ramsize_string;
131   int is_valid = FALSE;
132   UINT32 specified_ram = 0;
133   int error = FALSE;
134 
135   /* verify default ram value */
136   if (parse_string(config->default_size) == 0)
137   {
138      mame_printf_error("%s: '%s' has an invalid default RAM option: %s\n", driver->source_file, driver->name, config->default_size);
139      error = TRUE;
140   }
141 
142   /* command line options are only parsed for the device named "messram" */
143   if (strcmp(device->tag, "messram") == 0)
144   {
145      /* verify command line ram option */
146      ramsize_string = options_get_string(mame_options(), OPTION_RAMSIZE);
147 
148      if ((ramsize_string != NULL) && (ramsize_string[0] != '\0'))
149      {
150         specified_ram = ram_parse_string(ramsize_string);
151 
152         if (specified_ram == 0)
153         {
154            mame_printf_error("%s: '%s' cannot recognize the RAM option %s\n", driver->source_file, driver->name, ramsize_string);
155            error = TRUE;
156         }
157      }
158      else
159      {
160         /* not specifying the ramsize on the command line is valid as well */
161         is_valid = TRUE;
162      }
163 
164      /* compare command line option to default value */
165      if (parse_string(config->default_size) == specified_ram)
166         is_valid = TRUE;
167   }
168   else
169      is_valid = TRUE;
170 
171   /* verify extra ram options */
172   if (config->extra_options != NULL)
173   {
174      const char *s;
175 
176      astring *buffer = astring_alloc();
177      astring_cpyc(buffer, config->extra_options);
178      astring_replacechr(buffer, ',', 0);
179 
180      s = astring_c(buffer);
181 
182      /* try to parse each option */
183      while(*s != '\0')
184      {
185         UINT32 option_ram_size = parse_string(s);
186 
187         if (option_ram_size == 0)
188         {
189            mame_printf_error("%s: '%s' has an invalid RAM option: %s\n", driver->source_file, driver->name, s);
190            error = TRUE;
191         }
192 
193         if (option_ram_size == specified_ram)
194            is_valid = TRUE;
195 
196         s += strlen(s) + 1;
197      }
198 
199      astring_free(buffer);
200   }
201 
202   if (!is_valid)
203   {
204      mame_printf_error("%s: '%s' cannot recognize the RAM option %s", driver->source_file, driver->name, ramsize_string);
205      mame_printf_error(" (valid options are %s", config->default_size);
206 
207      if (config->extra_options != NULL)
208         mame_printf_error(",%s).\n", config->extra_options);
209      else
210         mame_printf_error(").\n");
211 
212      error = TRUE;
213   }
214 
215   return error;
216}
217 
218 
219DEVICE_GET_INFO( messram )
220{
221   switch (state)
222   {
223      /* --- the following bits of info are returned as 64-bit signed integers --- */
224      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(messram_state);         break;
225      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = sizeof(ram_config);            break;
226      case DEVINFO_INT_CLASS:                     info->i = DEVICE_CLASS_OTHER;            break;
227 
228      /* --- the following bits of info are returned as pointers to data or functions --- */
229      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(messram);   break;
230      case DEVINFO_FCT_STOP:                     /* Nothing */                        break;
231      case DEVINFO_FCT_RESET:                     info->reset = DEVICE_RESET_NAME(messram);   break;
232      case DEVINFO_FCT_VALIDITY_CHECK:            info->validity_check = DEVICE_VALIDITY_CHECK_NAME(messram); break;
233 
234      /* --- the following bits of info are returned as NULL-terminated strings --- */
235      case DEVINFO_STR_NAME:                     strcpy(info->s, "MESS RAM");            break;
236      case DEVINFO_STR_FAMILY:                  strcpy(info->s, "RAM");                  break;
237      case DEVINFO_STR_VERSION:                  strcpy(info->s, "1.00");               break;
238      case DEVINFO_STR_SOURCE_FILE:               strcpy(info->s, __FILE__);               break;
239      case DEVINFO_STR_CREDITS:                  strcpy(info->s, "Copyright MESS Team");      break;
240   }
241}
242 
243 
244/***************************************************************************
245    IMPLEMENTATION
246***************************************************************************/
247 
248UINT32 messram_get_size(const device_config *device)
249{
250   messram_state *messram = get_safe_token(device);
251   return messram->size;
252}
253 
254 
255UINT8 *messram_get_ptr(const device_config *device)
256{
257   messram_state *messram = get_safe_token(device);
258   return messram->ram;
259}
260 
261 
262void messram_dump(const device_config *device, const char *filename)
263{
264   messram_state *messram = get_safe_token(device);
265   file_error filerr;
266   mame_file *file;
267 
268   /* use a default filename */
269   if (!filename)
270      filename = "ram.bin";
271 
272   /* open the file */
273   filerr = mame_fopen(NULL, filename, OPEN_FLAG_WRITE, &file);
274   if (filerr == FILERR_NONE)
275   {
276      /* write the data */
277      mame_fwrite(file, messram->ram, messram->size);
278 
279      /* close file */
280      mame_fclose(file);
281   }
282}
1/*************************************************************************
2
3    MESS RAM device
4
5    Provides a configurable amount of RAM to drivers
6
7**************************************************************************/
8
9#include <stdio.h>
10#include <ctype.h>
11
12#include "driver.h"
13#include "messram.h"
14
15
16/***************************************************************************
17    CONSTANTS
18***************************************************************************/
19
20#define RAM_STRING_BUFLEN   16
21#define MAX_RAM_OPTIONS      16
22
23
24/***************************************************************************
25    TYPE DEFINITIONS
26***************************************************************************/
27
28typedef struct _messram_state messram_state;
29struct _messram_state
30{
31   UINT32 size; /* total amount of ram configured */
32   UINT8 *ram;  /* pointer to the start of ram */
33};
34
35
36/*****************************************************************************
37    INLINE FUNCTIONS
38*****************************************************************************/
39
40INLINE messram_state *get_safe_token(const device_config *device)
41{
42   assert(device != NULL);
43   assert(device->token != NULL);
44   assert(device->type == MESSRAM);
45
46   return (messram_state *)device->token;
47}
48
49
50/*****************************************************************************
51    HELPER FUNCTIONS
52*****************************************************************************/
53
54/*-------------------------------------------------
55    parse_string - convert a ram string to an
56   integer value
57-------------------------------------------------*/
58
59static UINT32 parse_string(const char *s)
60{
61   UINT32 ram;
62   char suffix = '\0';
63
64   s += sscanf(s, "%u%c", &ram, &suffix);
65
66   switch(tolower(suffix))
67   {
68   case 'k':
69      /* kilobytes */
70      ram *= 1024;
71      break;
72
73   case 'm':
74      /* megabytes */
75      ram *= 1024*1024;
76      break;
77
78   case '\0':
79      /* no suffix */
80      break;
81
82   default:
83      /* parse failure */
84      ram = 0;
85      break;
86   }
87
88   return ram;
89}
90
91
92/*****************************************************************************
93    DEVICE INTERFACE
94*****************************************************************************/
95
96static DEVICE_START( messram )
97{
98   messram_state *messram = get_safe_token(device);
99   ram_config *config = device->inline_config;
100   const char *ramsize_string = options_get_string(mame_options(), OPTION_RAMSIZE);
101
102   /* if we have a command line option, use it */
103   if ((ramsize_string != NULL) && (ramsize_string[0] != '\0'))
104      messram->size = parse_string(ramsize_string);
105   else
106      messram->size = parse_string(config->default_size);
107
108   /* allocate space for the ram */
109   messram->ram = auto_alloc_array(device->machine, UINT8, messram->size);
110
111   /* register for state saving */
112   state_save_register_device_item(device, 0, messram->size);
113   state_save_register_device_item_pointer(device, 0, messram->ram, messram->size);
114}
115
116
117static DEVICE_RESET( messram )
118{
119   messram_state *messram = get_safe_token(device);
120   ram_config *config = device->inline_config;
121
122   /* reset ram to the default value */
123   memset(messram->ram, config->default_value, messram->size);
124}
125
126
127static DEVICE_VALIDITY_CHECK( messram )
128{
129   ram_config *config = device->inline_config;
130   const char *ramsize_string;
131   int is_valid = FALSE;
132   UINT32 specified_ram = 0;
133   int error = FALSE;
134
135   /* verify default ram value */
136   if (parse_string(config->default_size) == 0)
137   {
138      mame_printf_error("%s: '%s' has an invalid default RAM option: %s\n", driver->source_file, driver->name, config->default_size);
139      error = TRUE;
140   }
141
142   /* command line options are only parsed for the device named "messram" */
143   if (strcmp(device->tag, "messram") == 0)
144   {
145      /* verify command line ram option */
146      ramsize_string = options_get_string(mame_options(), OPTION_RAMSIZE);
147
148      if ((ramsize_string != NULL) && (ramsize_string[0] != '\0'))
149      {
150         specified_ram = ram_parse_string(ramsize_string);
151
152         if (specified_ram == 0)
153         {
154            mame_printf_error("%s: '%s' cannot recognize the RAM option %s\n", driver->source_file, driver->name, ramsize_string);
155            error = TRUE;
156         }
157      }
158      else
159      {
160         /* not specifying the ramsize on the command line is valid as well */
161         is_valid = TRUE;
162      }
163
164      /* compare command line option to default value */
165      if (parse_string(config->default_size) == specified_ram)
166         is_valid = TRUE;
167   }
168   else
169      is_valid = TRUE;
170
171   /* verify extra ram options */
172   if (config->extra_options != NULL)
173   {
174      const char *s;
175
176      astring *buffer = astring_alloc();
177      astring_cpyc(buffer, config->extra_options);
178      astring_replacechr(buffer, ',', 0);
179
180      s = astring_c(buffer);
181
182      /* try to parse each option */
183      while(*s != '\0')
184      {
185         UINT32 option_ram_size = parse_string(s);
186
187         if (option_ram_size == 0)
188         {
189            mame_printf_error("%s: '%s' has an invalid RAM option: %s\n", driver->source_file, driver->name, s);
190            error = TRUE;
191         }
192
193         if (option_ram_size == specified_ram)
194            is_valid = TRUE;
195
196         s += strlen(s) + 1;
197      }
198
199      astring_free(buffer);
200   }
201
202   if (!is_valid)
203   {
204      mame_printf_error("%s: '%s' cannot recognize the RAM option %s", driver->source_file, driver->name, ramsize_string);
205      mame_printf_error(" (valid options are %s", config->default_size);
206
207      if (config->extra_options != NULL)
208         mame_printf_error(",%s).\n", config->extra_options);
209      else
210         mame_printf_error(").\n");
211
212      error = TRUE;
213   }
214
215   return error;
216}
217
218
219DEVICE_GET_INFO( messram )
220{
221   switch (state)
222   {
223      /* --- the following bits of info are returned as 64-bit signed integers --- */
224      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(messram_state);         break;
225      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = sizeof(ram_config);            break;
226      case DEVINFO_INT_CLASS:                     info->i = DEVICE_CLASS_OTHER;            break;
227
228      /* --- the following bits of info are returned as pointers to data or functions --- */
229      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(messram);   break;
230      case DEVINFO_FCT_STOP:                     /* Nothing */                        break;
231      case DEVINFO_FCT_RESET:                     info->reset = DEVICE_RESET_NAME(messram);   break;
232      case DEVINFO_FCT_VALIDITY_CHECK:            info->validity_check = DEVICE_VALIDITY_CHECK_NAME(messram); break;
233
234      /* --- the following bits of info are returned as NULL-terminated strings --- */
235      case DEVINFO_STR_NAME:                     strcpy(info->s, "MESS RAM");            break;
236      case DEVINFO_STR_FAMILY:                  strcpy(info->s, "RAM");                  break;
237      case DEVINFO_STR_VERSION:                  strcpy(info->s, "1.00");               break;
238      case DEVINFO_STR_SOURCE_FILE:               strcpy(info->s, __FILE__);               break;
239      case DEVINFO_STR_CREDITS:                  strcpy(info->s, "Copyright MESS Team");      break;
240   }
241}
242
243
244/***************************************************************************
245    IMPLEMENTATION
246***************************************************************************/
247
248UINT32 messram_get_size(const device_config *device)
249{
250   messram_state *messram = get_safe_token(device);
251   return messram->size;
252}
253
254
255UINT8 *messram_get_ptr(const device_config *device)
256{
257   messram_state *messram = get_safe_token(device);
258   return messram->ram;
259}
260
261
262void messram_dump(const device_config *device, const char *filename)
263{
264   messram_state *messram = get_safe_token(device);
265   file_error filerr;
266   mame_file *file;
267
268   /* use a default filename */
269   if (!filename)
270      filename = "ram.bin";
271
272   /* open the file */
273   filerr = mame_fopen(NULL, filename, OPEN_FLAG_WRITE, &file);
274   if (filerr == FILERR_NONE)
275   {
276      /* write the data */
277      mame_fwrite(file, messram->ram, messram->size);
278
279      /* close file */
280      mame_fclose(file);
281   }
282}
src/mess/devices/messram.h
r5770r5771
1/*************************************************************************
2 
3    MESS RAM device
4 
5    Provides a configurable amount of RAM to drivers
6 
7**************************************************************************/
8 
9#ifndef __MESSRAM_H__
10#define __MESSRAM_H__
11 
12 
13/***************************************************************************
14    CONSTANTS
15***************************************************************************/
16 
17#define RAM_DEFAULT_VALUE   0xcd
18 
19enum
20{
21   DEVINFO_STR_MESSRAM_SIZE = DEVINFO_STR_DEVICE_SPECIFIC
22};
23 
24 
25/***************************************************************************
26    TYPE DEFINITIONS
27***************************************************************************/
28 
29typedef struct _ram_config ram_config;
30struct _ram_config
31{
32   const char *default_size;
33   const char *extra_options;
34   UINT8 default_value;
35};
36 
37 
38/***************************************************************************
39    DEVICE CONFIGURATION MACROS
40***************************************************************************/
41 
42#define MESSRAM DEVICE_GET_INFO_NAME(messram)
43 
44#define MDRV_RAM_ADD(_tag) \
45   MDRV_DEVICE_ADD(_tag, MESSRAM, 0) \
46   MDRV_DEVICE_CONFIG_DATA32(ram_config, default_value, RAM_DEFAULT_VALUE)
47 
48#define MDRV_RAM_REMOVE(_tag) \
49   MDRV_DEVICE_REMOVE(_tag, MESSRAM)
50 
51#define MDRV_RAM_MODIFY(_tag) \
52   MDRV_DEVICE_MODIFY(_tag, MESSRAM)
53 
54#define MDRV_RAM_DEFAULT_SIZE(_default_size) \
55   MDRV_DEVICE_CONFIG_DATAPTR(ram_config, default_size, _default_size)
56 
57#define MDRV_RAM_EXTRA_OPTIONS(_extra_options) \
58   MDRV_DEVICE_CONFIG_DATAPTR(ram_config, extra_options, _extra_options)
59 
60#define MDRV_RAM_DEFAULT_VALUE(_default_value) \
61   MDRV_DEVICE_CONFIG_DATA32(ram_config, default_value, _default_value)
62 
63 
64/***************************************************************************
65    FUNCTION PROTOTYPES
66***************************************************************************/
67 
68DEVICE_GET_INFO( messram );
69UINT32 messram_get_size(const device_config *device);
70UINT8 *messram_get_ptr(const device_config *device);
71void messram_dump(const device_config *device, const char *filename);
72 
73 
74#endif /* __MESSRAM_H__ */
1/*************************************************************************
2
3    MESS RAM device
4
5    Provides a configurable amount of RAM to drivers
6
7**************************************************************************/
8
9#ifndef __MESSRAM_H__
10#define __MESSRAM_H__
11
12
13/***************************************************************************
14    CONSTANTS
15***************************************************************************/
16
17#define RAM_DEFAULT_VALUE   0xcd
18
19enum
20{
21   DEVINFO_STR_MESSRAM_SIZE = DEVINFO_STR_DEVICE_SPECIFIC
22};
23
24
25/***************************************************************************
26    TYPE DEFINITIONS
27***************************************************************************/
28
29typedef struct _ram_config ram_config;
30struct _ram_config
31{
32   const char *default_size;
33   const char *extra_options;
34   UINT8 default_value;
35};
36
37
38/***************************************************************************
39    DEVICE CONFIGURATION MACROS
40***************************************************************************/
41
42#define MESSRAM DEVICE_GET_INFO_NAME(messram)
43
44#define MDRV_RAM_ADD(_tag) \
45   MDRV_DEVICE_ADD(_tag, MESSRAM, 0) \
46   MDRV_DEVICE_CONFIG_DATA32(ram_config, default_value, RAM_DEFAULT_VALUE)
47
48#define MDRV_RAM_REMOVE(_tag) \
49   MDRV_DEVICE_REMOVE(_tag, MESSRAM)
50
51#define MDRV_RAM_MODIFY(_tag) \
52   MDRV_DEVICE_MODIFY(_tag, MESSRAM)
53
54#define MDRV_RAM_DEFAULT_SIZE(_default_size) \
55   MDRV_DEVICE_CONFIG_DATAPTR(ram_config, default_size, _default_size)
56
57#define MDRV_RAM_EXTRA_OPTIONS(_extra_options) \
58   MDRV_DEVICE_CONFIG_DATAPTR(ram_config, extra_options, _extra_options)
59
60#define MDRV_RAM_DEFAULT_VALUE(_default_value) \
61   MDRV_DEVICE_CONFIG_DATA32(ram_config, default_value, _default_value)
62
63
64/***************************************************************************
65    FUNCTION PROTOTYPES
66***************************************************************************/
67
68DEVICE_GET_INFO( messram );
69UINT32 messram_get_size(const device_config *device);
70UINT8 *messram_get_ptr(const device_config *device);
71void messram_dump(const device_config *device, const char *filename);
72
73
74#endif /* __MESSRAM_H__ */

Previous 15994 Revisions Next


© 1998-2018 The MESS Team