summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/machine/taitosj.c
blob: 814e6a725460ad038ff51e11c3ed8154cdeaea3d (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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/***************************************************************************

  machine.c

  Functions to emulate general aspects of the machine (RAM, ROM, interrupts,
  I/O ports)

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

#include "driver.h"
#include "cpu/m6805/m6805.h"


#define VERBOSE	1
#define LOG(x) do { if (VERBOSE) logerror x; } while (0)


static UINT8 fromz80,toz80;
static UINT8 zaccept,zready,busreq;
static UINT8 portA_in,portA_out;

static UINT8 spacecr_prot_value;
static UINT8 protection_value;
static UINT32 address;

void taitosj_register_main_savestate(void);
WRITE8_HANDLER( taitosj_bankswitch_w );

MACHINE_START( taitosj )
{
	memory_configure_bank(1, 0, 1, memory_region(REGION_CPU1) + 0x6000, 0);
	memory_configure_bank(1, 1, 1, memory_region(REGION_CPU1) + 0x10000, 0);

	taitosj_register_main_savestate();

	state_save_register_global(fromz80);
	state_save_register_global(toz80);
	state_save_register_global(zaccept);
	state_save_register_global(zready);
	state_save_register_global(busreq);

	state_save_register_global(portA_in);
	state_save_register_global(portA_out);
	state_save_register_global(address);
	state_save_register_global(spacecr_prot_value);
	state_save_register_global(protection_value);
}

MACHINE_RESET( taitosj )
{
	/* set the default ROM bank (many games only have one bank and */
	/* never write to the bank selector register) */
    taitosj_bankswitch_w(0, 0);


	zaccept = 1;
	zready = 0;
	busreq = 0;
 	if (machine->drv->cpu[2].type != CPU_DUMMY)
	cpunum_set_input_line(2,0,CLEAR_LINE);

	spacecr_prot_value = 0;
}


WRITE8_HANDLER( taitosj_bankswitch_w )
{
	coin_lockout_global_w(~data & 1);

	if(data & 0x80) memory_set_bank(1, 1);
	else memory_set_bank(1, 0);
}



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

                           PROTECTION HANDLING

 Some of the games running on this hardware are protected with a 68705 mcu.
 It can either be on a daughter board containing Z80+68705+one ROM, which
 replaces the Z80 on an unprotected main board; or it can be built-in on the
 main board. The two are fucntionally equivalent.

 The 68705 can read commands from the Z80, send back result codes, and has
 direct access to the Z80 memory space. It can also trigger IRQs on the Z80.

***************************************************************************/
READ8_HANDLER( taitosj_fake_data_r )
{
	LOG(("%04x: protection read\n",activecpu_get_pc()));
	return 0;
}

WRITE8_HANDLER( taitosj_fake_data_w )
{
	LOG(("%04x: protection write %02x\n",activecpu_get_pc(),data));
}

READ8_HANDLER( taitosj_fake_status_r )
{
	LOG(("%04x: protection status read\n",activecpu_get_pc()));
	return 0xff;
}


/* timer callback : */
READ8_HANDLER( taitosj_mcu_data_r )
{
	LOG(("%04x: protection read %02x\n",activecpu_get_pc(),toz80));
	zaccept = 1;
	return toz80;
}

/* timer callback : */
static TIMER_CALLBACK( taitosj_mcu_real_data_w )
{
	zready = 1;
	cpunum_set_input_line(2,0,ASSERT_LINE);
	fromz80 = param;
}

WRITE8_HANDLER( taitosj_mcu_data_w )
{
	LOG(("%04x: protection write %02x\n",activecpu_get_pc(),data));
	timer_call_after_resynch(NULL, data,taitosj_mcu_real_data_w);
	/* temporarily boost the interleave to sync things up */
	cpu_boost_interleave(attotime_zero, ATTOTIME_IN_USEC(10));
}

READ8_HANDLER( taitosj_mcu_status_r )
{
	/* temporarily boost the interleave to sync things up */
	cpu_boost_interleave(attotime_zero, ATTOTIME_IN_USEC(10));

	/* bit 0 = the 68705 has read data from the Z80 */
	/* bit 1 = the 68705 has written data for the Z80 */
	return ~((zready << 0) | (zaccept << 1));
}

READ8_HANDLER( taitosj_68705_portA_r )
{
	LOG(("%04x: 68705 port A read %02x\n",activecpu_get_pc(),portA_in));
	return portA_in;
}

WRITE8_HANDLER( taitosj_68705_portA_w )
{
	LOG(("%04x: 68705 port A write %02x\n",activecpu_get_pc(),data));
	portA_out = data;
}



/*
 *  Port B connections:
 *
 *  all bits are logical 1 when read (+5V pullup)
 *
 *  0   W  !68INTRQ
 *  1   W  !68LRD (enables latch which holds command from the Z80)
 *  2   W  !68LWR (loads the latch which holds data for the Z80, and sets a
 *                 status bit so the Z80 knows there's data waiting)
 *  3   W  to Z80 !BUSRQ (aka !WAIT) pin
 *  4   W  !68WRITE (triggers write to main Z80 memory area and increases low
 *                   8 bits of the latched address)
 *  5   W  !68READ (triggers read from main Z80 memory area and increases low
 *                   8 bits of the latched address)
 *  6   W  !LAL (loads the latch which holds the low 8 bits of the address of
 *               the main Z80 memory location to access)
 *  7   W  !UAL (loads the latch which holds the high 8 bits of the address of
 *               the main Z80 memory location to access)
 */

READ8_HANDLER( taitosj_68705_portB_r )
{
	return 0xff;
}

/* timer callback : 68705 is going to read data from the Z80 */
static TIMER_CALLBACK( taitosj_mcu_data_real_r )
{
	zready = 0;
}

/* timer callback : 68705 is writing data for the Z80 */
static TIMER_CALLBACK( taitosj_mcu_status_real_w )
{
	toz80 = param;
	zaccept = 0;
}

WRITE8_HANDLER( taitosj_68705_portB_w )
{
	LOG(("%04x: 68705 port B write %02x\n",activecpu_get_pc(),data));

	if (~data & 0x01)
	{
		LOG(("%04x: 68705  68INTRQ **NOT SUPPORTED**!\n",activecpu_get_pc()));
	}
	if (~data & 0x02)
	{
		/* 68705 is going to read data from the Z80 */
		timer_call_after_resynch(NULL, 0,taitosj_mcu_data_real_r);
		cpunum_set_input_line(2,0,CLEAR_LINE);
		portA_in = fromz80;
		LOG(("%04x: 68705 <- Z80 %02x\n",activecpu_get_pc(),portA_in));
	}
	if (~data & 0x08)
		busreq = 1;
	else
		busreq = 0;
	if (~data & 0x04)
	{
		LOG(("%04x: 68705 -> Z80 %02x\n",activecpu_get_pc(),portA_out));

		/* 68705 is writing data for the Z80 */
		timer_call_after_resynch(NULL, portA_out,taitosj_mcu_status_real_w);
	}
	if (~data & 0x10)
	{
		LOG(("%04x: 68705 write %02x to address %04x\n",activecpu_get_pc(),portA_out,address));

		memory_set_context(0);
		program_write_byte(address, portA_out);
		memory_set_context(2);

		/* increase low 8 bits of latched address for burst writes */
		address = (address & 0xff00) | ((address + 1) & 0xff);
	}
	if (~data & 0x20)
	{
		memory_set_context(0);
		portA_in = program_read_byte(address);
		memory_set_context(2);
		LOG(("%04x: 68705 read %02x from address %04x\n",activecpu_get_pc(),portA_in,address));
	}
	if (~data & 0x40)
	{
		LOG(("%04x: 68705 address low %02x\n",activecpu_get_pc(),portA_out));
		address = (address & 0xff00) | portA_out;
	}
	if (~data & 0x80)
	{
		LOG(("%04x: 68705 address high %02x\n",activecpu_get_pc(),portA_out));
		address = (address & 0x00ff) | (portA_out << 8);
	}
}

/*
 *  Port C connections:
 *
 *  0   R  ZREADY (1 when the Z80 has written a command in the latch)
 *  1   R  ZACCEPT (1 when the Z80 has read data from the latch)
 *  2   R  from Z80 !BUSAK pin
 *  3   R  68INTAK (goes 0 when the interrupt request done with 68INTRQ
 *                  passes through)
 */

READ8_HANDLER( taitosj_68705_portC_r )
{
	int res;

	res = (zready << 0) | (zaccept << 1) | ((busreq^1) << 2);
	LOG(("%04x: 68705 port C read %02x\n",activecpu_get_pc(),res));
	return res;
}


/* Space Cruiser protection (otherwise the game resets on the asteroids level) */

READ8_HANDLER( spacecr_prot_r )
{
	int pc = activecpu_get_pc();

	if( pc != 0x368A && pc != 0x36A6 )
		logerror("Read protection from an unknown location: %04X\n",pc);

	spacecr_prot_value ^= 0xff;

	return spacecr_prot_value;
}


/* Alpine Ski protection crack routines */

WRITE8_HANDLER( alpine_protection_w )
{
	switch (data)
	{
	case 0x05:
		protection_value = 0x18;
		break;
	case 0x07:
	case 0x0c:
	case 0x0f:
		protection_value = 0x00;		/* not used as far as I can tell */
		break;
	case 0x16:
		protection_value = 0x08;
		break;
	case 0x1d:
		protection_value = 0x18;
		break;
	default:
		protection_value = data;		/* not used as far as I can tell */
		break;
	}
}

WRITE8_HANDLER( alpinea_bankswitch_w )
{
    taitosj_bankswitch_w(offset, data);
	protection_value = data >> 2;
}

READ8_HANDLER( alpine_port_2_r )
{
	return input_port_2_r(offset) | protection_value;
}