summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/machine/theglobp.cpp
blob: 6b60bb1c9456619b4a2a43020963da82c2b709fa (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
// license:BSD-3-Clause
// copyright-holders:Nicola Salmoria
/***************************************************************************

  machine.c

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

  The Glob protection description:

  The Glob is designed to run on modified Pacman hardware.  It contains
  two graphics ROMs at 5E and 5F, but contains code ROMs on a daughterboard
  similar in concept to Ms. Pacman.  However, these code ROMs are decrypted
  through additional circuitry.  The daughterboard was encased in epoxy.

  Here's a description of the protection as best as I can give it.

  1)  The decrypted D0 bit fed to the CPU is simply an inversion of the
      D5 bit from the code ROMs.
  2)  The decrypted D1 bit fed to the CPU is simply an inversion of the
      D2 bit from the code ROMs.
  3)  The other 6 data bits are decrypted by a 10H8 PAL.  The PAL also
      takes as input a 4-bit counter.  The counter is incremented and
      decremented as follows:
      - the Z-80 command IN($xx) where xx is an odd number decrements the
        counter; an even number increments the counter.
        Ex:  IN($64) would increment the counter, IN($6B) would decrement
        the counter.
  4)  The PAL output also contains the two ROM enable lines used to enable
      the two encrypted code ROMs.  As long as the system is working
      correctly, these ROMs will always be enabled.

  As it so happens, only four counter values are ever used, which is
  fortunate because the PAL only contains signals to enable the ROMs for
  those four counter values.  The valid counter values are $8, $9, $A, and
  $B.  The counter's intial value is $A, which is set by jumpers on the
  daughterboard.  Following is a description of the resulting decryptions
  for these four counter states.

  COUNTER       ENCRYPTED   DECRYPTED
                  VALUE       VALUE

                DDDDDDDD    DDDDDDDD
                76543210    76543210

  Counter = 8:  abcdefgh    EAhBDgFC
  Counter = 9:  abcdefgh    FAgeDBFC
  Counter = A:  abcdefgh    EHDBagFC
  Counter = B:  abcdefgh    GHDEaBFC

  In the above diagram, capital letters represent inverted bits.  Notice
  that bits D2 and D5 are the same independent of counter state, this is
  because these bits are not decrypted by the PAL.


  In the code below, all four of these decryption patterns are used to
  decrypt the entire code ROMs before execution.  This is done for speed,
  since we can then just bankswitch between the decrypted code sets on
  each IN($xx) command, as opposed to dynamically decrypting every byte.

  - Mike Balfour (mab22@po.cwru.edu)

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

#include "emu.h"
#include "includes/pacman.h"


void pacman_state::theglobp_decrypt_rom_8()
{
	int oldbyte,inverted_oldbyte,newbyte;
	int mem;
	UINT8 *RAM;

	RAM = memregion("maincpu")->base();


	for (mem=0;mem<0x4000;mem++)
	{
		oldbyte = RAM[mem];
		inverted_oldbyte = ~oldbyte;

		/*  Note: D2 is inverted and connected to D1, D5 is inverted and
		    connected to D0.  The other six data bits are converted by a
		    PAL10H8 driven by the counter. */

		/* Direct inversion */
		newbyte  = (inverted_oldbyte & 0x04) >> 1;
		newbyte |= (inverted_oldbyte & 0x20) >> 5;
		/* PAL */
		newbyte |= (oldbyte & 0x01) << 5;
		newbyte |= (oldbyte & 0x02) << 1;
		newbyte |= (inverted_oldbyte & 0x08) << 4;
		newbyte |= (inverted_oldbyte & 0x10) >> 1;
		newbyte |= (inverted_oldbyte & 0x40) >> 2;
		newbyte |= (inverted_oldbyte & 0x80) >> 1;

		RAM[mem + 0x10000] = newbyte;
	}

	return;
}


void pacman_state::theglobp_decrypt_rom_9()
{
	int oldbyte,inverted_oldbyte,newbyte;
	int mem;
	UINT8 *RAM;

	RAM = memregion("maincpu")->base();

	for (mem=0;mem<0x4000;mem++)
	{
		oldbyte = RAM[mem];
		inverted_oldbyte = ~oldbyte;

		/*  Note: D2 is inverted and connected to D1, D5 is inverted and
		    connected to D0.  The other six data bits are converted by a
		    PAL10H8 driven by the counter. */

		/* Direct inversion */
		newbyte  = (inverted_oldbyte & 0x04) >> 1;
		newbyte |= (inverted_oldbyte & 0x20) >> 5;
		/* PAL */
		newbyte |= (oldbyte & 0x01) << 5;
		newbyte |= (inverted_oldbyte & 0x02) << 6;
		newbyte |= (oldbyte & 0x08) << 1;
		newbyte |= (inverted_oldbyte & 0x10) >> 1;
		newbyte |= (inverted_oldbyte & 0x40) >> 4;
		newbyte |= (inverted_oldbyte & 0x80) >> 1;

		RAM[mem + 0x14000] = newbyte;
	}

	return;
}

void pacman_state::theglobp_decrypt_rom_A()
{
	int oldbyte,inverted_oldbyte,newbyte;
	int mem;
	UINT8 *RAM;

	RAM = memregion("maincpu")->base();

	for (mem=0;mem<0x4000;mem++)
	{
		oldbyte = RAM[mem];
		inverted_oldbyte = ~oldbyte;

		/*  Note: D2 is inverted and connected to D1, D5 is inverted and
		    connected to D0.  The other six data bits are converted by a
		    PAL10H8 driven by the counter. */

		/* Direct inversion */
		newbyte  = (inverted_oldbyte & 0x04) >> 1;
		newbyte |= (inverted_oldbyte & 0x20) >> 5;
		/* PAL */
		newbyte |= (inverted_oldbyte & 0x01) << 6;
		newbyte |= (oldbyte & 0x02) << 1;
		newbyte |= (inverted_oldbyte & 0x08) << 4;
		newbyte |= (inverted_oldbyte & 0x10) << 1;
		newbyte |= (inverted_oldbyte & 0x40) >> 2;
		newbyte |= (oldbyte & 0x80) >> 4;

		RAM[mem + 0x18000] = newbyte;
	}

	return;
}

void pacman_state::theglobp_decrypt_rom_B()
{
	int oldbyte,inverted_oldbyte,newbyte;
	int mem;
	UINT8 *RAM;

	RAM = memregion("maincpu")->base();

	for (mem=0;mem<0x4000;mem++)
	{
		oldbyte = RAM[mem];
		inverted_oldbyte = ~oldbyte;

		/*  Note: D2 is inverted and connected to D1, D5 is inverted and
		    connected to D0.  The other six data bits are converted by a
		    PAL10H8 driven by the counter. */

		/* Direct inversion */
		newbyte  = (inverted_oldbyte & 0x04) >> 1;
		newbyte |= (inverted_oldbyte & 0x20) >> 5;
		/* PAL */
		newbyte |= (inverted_oldbyte & 0x01) << 6;
		newbyte |= (inverted_oldbyte & 0x02) << 6;
		newbyte |= (oldbyte & 0x08) << 1;
		newbyte |= (inverted_oldbyte & 0x10) << 1;
		newbyte |= (inverted_oldbyte & 0x40) >> 4;
		newbyte |= (oldbyte & 0x80) >> 4;

		RAM[mem + 0x1C000] = newbyte;
	}

	return;
}


READ8_MEMBER(pacman_state::theglobp_decrypt_rom )
{
	if (offset & 0x01)
	{
		m_counter = (m_counter - 1) & 0x0F;
	}
	else
	{
		m_counter = (m_counter + 1) & 0x0F;
	}

	switch(m_counter)
	{
		case 0x08:  membank ("bank1")->set_entry (0);        break;
		case 0x09:  membank ("bank1")->set_entry (1);        break;
		case 0x0A:  membank ("bank1")->set_entry (2);        break;
		case 0x0B:  membank ("bank1")->set_entry (3);        break;
		default:
			logerror("Invalid counter = %02X\n",m_counter);
			break;
	}

	return 0;
}


MACHINE_START_MEMBER(pacman_state,theglobp)
{
	UINT8 *RAM = memregion("maincpu")->base();

	/* While the PAL supports up to 16 decryption methods, only four
	    are actually used in the PAL.  Therefore, we'll take a little
	    memory overhead and decrypt the ROMs using each method in advance. */
	theglobp_decrypt_rom_8();
	theglobp_decrypt_rom_9();
	theglobp_decrypt_rom_A();
	theglobp_decrypt_rom_B();

	membank("bank1")->configure_entries(0, 4, &RAM[0x10000], 0x4000);

	save_item(NAME(m_counter));
}


MACHINE_RESET_MEMBER(pacman_state,theglobp)
{
	/* The initial state of the counter is 0x0A */
	m_counter = 0x0A;
	membank("bank1")->set_entry(2);
}