summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/machine/snesbsx.c
blob: d52801583604d502df3fdaf8d55b0caa16cc9d73 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/***************************************************************************

  snesbsx.c

  File to handle emulation of the SNES "BS-X Satellaview".

  Based on byuu's research.

  TODO: basically everything. The hardware can dynamically remap where
  the memory handlers read/write (this will probably require some more
  rethinking of the way we use snes_ram), see bsx_update_memory_map

***************************************************************************/


enum
{
	SNES_BSX_CARTROM = 0,
	SNES_BSX_PRAM,
	SNES_BSX_FLASH
};


struct _snes_bsx_state
{
	// base regs
	// we don't emulate the base unit yet

	// cart regs
    UINT8 cart_regs[16];

	// flash regs
    UINT32 command;
    UINT8 write_old;
    UINT8 write_new;

    int flash_enable;
    int read_enable;
    int write_enable;

	UINT8 *pram;
	int ram_source;
};

static struct _snes_bsx_state  bsx_state;


static void bsx_update_memory_map(void)
{
	bsx_state.ram_source = BIT(bsx_state.cart_regs[0x01], 7) ? SNES_BSX_PRAM : SNES_BSX_FLASH;
//  UINT8 *RAM = (bsx_state.cart_regs[0x01] & 0x80) == 0x00 ? memory_region(space->machine, "flash") : bsx_state.pram;

	logerror("BSX: updated memory map, current RAM: %d", bsx_state.ram_source);
	if (!BIT(bsx_state.cart_regs[0x02], 7))
	{
		//LoROM mapping
		// 0x00-0x7d:0x8000-0xfff -> RAM (either flash or pram)
		// 0x80-0xff:0x8000-0xfff -> RAM
	}
	else
	{
		//HiROM mapping
		// 0x00-0x3f:0x8000-0xfff -> RAM - 'shadowed'
		// 0x40-0x7d:0x0000-0xfff -> RAM
		// 0x80-0xbf:0x8000-0xfff -> RAM - 'shadowed'
		// 0xc0-0xff:0x0000-0xfff -> RAM
	}

	if (BIT(bsx_state.cart_regs[0x03], 7))
	{
		// 0x60-0x6f:0x0000-0xffff -> PRAM
	}

	if (!BIT(bsx_state.cart_regs[0x05], 7))
	{
		// 0x40-0x4f:0x0000-0xffff -> PRAM
	}

	if (!BIT(bsx_state.cart_regs[0x06], 7))
	{
		// 0x50-0x5f:0x0000-0xffff -> PRAM
	}

	if (BIT(bsx_state.cart_regs[0x07], 7))
	{
		// 0x00-0x1f:0x8000-0xffff -> CART
	}

	if (BIT(bsx_state.cart_regs[0x08], 7))
	{
		// 0x80-0x9f:0x8000-0xffff -> CART
	}

	// 0x20-0x3f:0x6000-0x7fff -> PRAM
	// 0x70-0x77:0x0000-0xffff -> PRAM
}

static READ8_HANDLER( bsx_read )
{
	if ((offset & 0xf0ffff) == 0x005000)
	{
		//$[00-0f]:5000 MMIO
		UINT8 n = (offset >> 16) & 0x0f;
		return bsx_state.cart_regs[n];
	}

	if ((offset & 0xf8f000) == 0x105000)
	{
		//$[10-17]:[5000-5fff] SRAM
		return bsx_state.pram[((offset >> 16) & 7) * 0x1000 + (offset & 0xfff)];
	}

	return 0x00;
}


static WRITE8_HANDLER( bsx_write )
{
	if ((offset & 0xf0ffff) == 0x005000)
	{
		//$[00-0f]:5000 MMIO
		UINT8 n = (offset >> 16) & 0x0f;
		bsx_state.cart_regs[n] = data;
		if ((n == 0x0e) && (data & 0x80))
			bsx_update_memory_map();
	}

	if ((offset & 0xf8f000) == 0x105000)
	{
		//$[10-17]:[5000-5fff] SRAM
		bsx_state.pram[((offset >> 16) & 7) * 0x1000 + (offset & 0xfff)] = data;
	}
}

static void bsx_init( running_machine *machine )
{

	memset(bsx_state.cart_regs, 0, ARRAY_LENGTH(bsx_state.cart_regs));

	bsx_state.cart_regs[0x07] = 0x80;
	bsx_state.cart_regs[0x08] = 0x80;

	bsx_state.pram = auto_alloc_array(machine, UINT8, 0x80000);

	bsx_update_memory_map();
	// saves
}

#ifdef UNUSED_FUNCTION
static READ8_HANDLER( bsx_flash_read )
{
	UINT8 *FLASH = memory_region(space->machine, "flash");

	if (offset == 0x0002)
	{
		if (bsx_state.flash_enable)
			return 0x80;
	}

	if (offset == 0x5555)
	{
		if (bsx_state.flash_enable)
			return 0x80;
	}

	if (bsx_state.read_enable && offset >= 0xff00 && offset <= 0xff13)
	{
		//read flash cartridge vendor information
		switch(offset - 0xff00)
		{
			case 0x00: return 0x4d;
			case 0x01: return 0x00;
			case 0x02: return 0x50;
			case 0x03: return 0x00;
			case 0x04: return 0x00;
			case 0x05: return 0x00;
			case 0x06: return 0x2a;  //0x2a = 8mbit, 0x2b = 16mbit (not known to exist, though BIOS recognizes ID)
			case 0x07: return 0x00;
			default:   return 0x00;
		}
	}

	return FLASH[offset];
}

static WRITE8_HANDLER( bsx_flash_write )
{
	if ((offset & 0xff0000) == 0)
	{
		bsx_state.write_old = bsx_state.write_new;
		bsx_state.write_new = data;

		if (bsx_state.write_enable && bsx_state.write_old == bsx_state.write_new)
		{
//          currently we have the flashcart loaded in a rom_region -> we cannot write yet
		}
	}
	else
	{
		if (bsx_state.write_enable)
		{
			//          currently we have the flashcart loaded in a rom_region -> we cannot write yet
		}
	}

	if (offset == 0x0000)
	{
		bsx_state.command <<= 8;
		bsx_state.command |= data;

		if ((bsx_state.command & 0xffff) == 0x38d0)
		{
			bsx_state.flash_enable = 1;
			bsx_state.read_enable  = 1;
		}
	}

	if (offset == 0x2aaa)
	{
		bsx_state.command <<= 8;
		bsx_state.command |= data;
	}

	if (offset == 0x5555)
	{
		bsx_state.command <<= 8;
		bsx_state.command |= data;

		if ((bsx_state.command & 0xffffff) == 0xaa5570)
		{
			bsx_state.write_enable = 0;
		}

		if ((bsx_state.command & 0xffffff) == 0xaa55a0)
		{
			bsx_state.write_old = 0x00;
			bsx_state.write_new = 0x00;
			bsx_state.flash_enable = 1;
			bsx_state.write_enable = 1;
		}

		if ((bsx_state.command & 0xffffff) == 0xaa55f0)
		{
			bsx_state.flash_enable = 0;
			bsx_state.read_enable  = 0;
			bsx_state.write_enable = 0;
		}
	}
}

#endif