summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/x2212.c
blob: 06d29a40c7f0e76c62aef15aefbe2495c1c5fc9a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Xicor X2212
 *
 * 256 x 4 bit Nonvolatile Static RAM
 *
 */

#include "emu.h"
#include "machine/x2212.h"

#define SIZE_DATA ( 0x100 )

typedef struct
{
	UINT8 *sram;
	UINT8 *e2prom;
	UINT8 *default_data;
	int store;
	int array_recall;
} x2212_state;

/*-------------------------------------------------
    get_safe_token - makes sure that the passed
    in device is, in fact, an X2212
-------------------------------------------------*/

INLINE x2212_state *get_safe_token(const device_config *device)
{
	assert(device != NULL);
	assert(device->token != NULL);
	assert(device->type == X2212);

	return (x2212_state *)device->token;
}

void x2212_write( const device_config *device, int offset, int data )
{
	x2212_state *c = get_safe_token(device);

	c->sram[ offset ] = data;
}


int x2212_read( const device_config *device, int offset )
{
	x2212_state *c = get_safe_token(device);

	return c->sram[ offset ];
}

void x2212_store( const device_config *device, int store )
{
	x2212_state *c = get_safe_token(device);

	if( !store && c->store )
	{
		memcpy( c->e2prom, c->sram, SIZE_DATA );
	}

	c->store = store;
}

void x2212_array_recall( const device_config *device, int array_recall )
{
	x2212_state *c = get_safe_token(device);

	if( !array_recall && c->array_recall )
	{
		memcpy( c->sram, c->e2prom, SIZE_DATA );
	}

	c->array_recall = array_recall;
}

/*-------------------------------------------------
    device start callback
-------------------------------------------------*/

static DEVICE_START(x2212)
{
	x2212_state *c = get_safe_token(device);
	const x2212_config *config;

	/* validate some basic stuff */
	assert(device != NULL);
//  assert(device->static_config != NULL);
	assert(device->inline_config == NULL);
	assert(device->machine != NULL);
	assert(device->machine->config != NULL);

	c->sram = auto_alloc_array( device->machine, UINT8, SIZE_DATA );
	c->e2prom = auto_alloc_array( device->machine, UINT8, SIZE_DATA );
	c->store = 1;
	c->array_recall = 1;

	config = (const x2212_config *)device->static_config;
	if( config != NULL && config->data != NULL )
	{
		c->default_data = memory_region( device->machine, config->data );
	}

	state_save_register_device_item_pointer( device, 0, c->sram, SIZE_DATA );
	state_save_register_device_item_pointer( device, 0, c->e2prom, SIZE_DATA );
	state_save_register_device_item( device, 0, c->store );
	state_save_register_device_item( device, 0, c->array_recall );
}

/*-------------------------------------------------
    device reset callback
-------------------------------------------------*/

static DEVICE_RESET(x2212)
{
}

static DEVICE_NVRAM(x2212)
{
	x2212_state *c = get_safe_token(device);

	if( read_or_write )
	{
		mame_fwrite( file, c->sram, SIZE_DATA );
	}
	else
	{
		if( file )
		{
			mame_fread( file, c->e2prom, SIZE_DATA );
		}
		else
		{
			if( c->default_data != NULL )
			{
				memcpy( c->e2prom, c->default_data, SIZE_DATA );
			}
			else
			{
				memset( c->e2prom, 0xff, SIZE_DATA );
			}
		}

		memcpy( c->sram, c->e2prom, SIZE_DATA );
	}
}

/*-------------------------------------------------
    device get info callback
-------------------------------------------------*/

DEVICE_GET_INFO(x2212)
{
	switch (state)
	{
		/* --- the following bits of info are returned as 64-bit signed integers --- */
		case DEVINFO_INT_TOKEN_BYTES:			info->i = sizeof(x2212_state); break;
		case DEVINFO_INT_INLINE_CONFIG_BYTES:	info->i = 0; break; // sizeof(x2212_config)
		case DEVINFO_INT_CLASS:					info->i = DEVICE_CLASS_PERIPHERAL; break;

		/* --- the following bits of info are returned as pointers to data or functions --- */
		case DEVINFO_FCT_START:					info->start = DEVICE_START_NAME(x2212); break;
		case DEVINFO_FCT_STOP:					/* nothing */ break;
		case DEVINFO_FCT_RESET:					info->reset = DEVICE_RESET_NAME(x2212); break;
		case DEVINFO_FCT_NVRAM:					info->nvram = DEVICE_NVRAM_NAME(x2212); break;

		/* --- the following bits of info are returned as NULL-terminated strings --- */
		case DEVINFO_STR_NAME:					strcpy(info->s, "X2212"); break;
		case DEVINFO_STR_FAMILY:				strcpy(info->s, "EEPROM"); break;
		case DEVINFO_STR_VERSION:				strcpy(info->s, "1.0"); break;
		case DEVINFO_STR_SOURCE_FILE:			strcpy(info->s, __FILE__); break;
		case DEVINFO_STR_CREDITS:				strcpy(info->s, "Copyright Nicola Salmoria and the MAME Team"); break;
	}
}