summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/machine/mc8123.cpp
blob: 55d850a77eade9feaa2dd7dd9b385f1dab5afd8b (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// license:BSD-3-Clause
// copyright-holders:Nicola Salmoria, David Widel
/***************************************************************************

NEC MC-8123 encryption emulation

Decryption tables provided by David Widel
Decryption algorithm by Nicola Salmoria

The NEC MC-8123 is a Z80 with built-in encryption.
It contains 0x2000 bytes of battery-backed RAM, when the battery dies the
game stops working. It's possible that the CPU would work as a normal Z80
in that case, this hasn't been verified.

When the CPU reads a byte from memory, it uses 12 bits of the address and
the information of whether the byte is an opcode or not to select a byte in
the internal RAM. The byte controls how the decryption works.

A special value in the internal RAM instructs the CPU to not apply the decryption
algorithm at all. This is necessary for the CPU to be able to use RAM areas
normally.

In theory, the size of the key would make the encryption quite secure.

In practice, the contents of the internal RAM were generated using a linear
congruential generator, so the whole key can be generated starting from a single
24-bit seed.

The LCG is the same that was used to generate the FD1094 keys.
Note that the algorithm skips the special value that would instruct the CPU to
not decrypt at that address.

void generate_key(uint8_t *key, int seed, int upper_bound)
{
    int i;

    for (i = 0; i < upper_bound; ++i)
    {
        uint8_t byteval;

        do
        {
            seed = seed * 0x29;
            seed += seed << 16;

            byteval = (~seed >> 16) & 0xff;
        } while (byteval == 0xff);

        key[i] = byteval;
    }

    for (; i < 0x2000; ++i)
        key[i] = 0xff;
}


The known games that use this CPU are:

CPU #    Game                      Notes              Seed   Upper Limit
-------- ------------------------- ------------------ ------ -----
317-5014 DakkoChan Jansoh          NEC MC-8123B       206850  1C00
317-0029 Block Gal                 NEC MC-8123B 651   091755  1800?
317-0030 Perfect Billiard                             9451EC  1C00
317-0042 Opa Opa                                      B31FD0  1C00
317-0043 Wonder Boy Monster Land                      640708  1C00
317-0054 Shinobi (sound CPU)       NEC MC-8123B 652   088992  1800
317-0057 Fantasy Zone 2                               ADFACE  1800
317-0064 Ufo Senshi Yohko Chan                        880603  1C00
317-0066 Altered Beast (sound CPU) NEC MC-8123B 704   ED8600  1800
317-5002 Gigas                     NEC MC-8123 638    234567  1C00
317-5012 Ganbare Chinsan Ooshoubu  NEC MC-8123A       804B54  1C00
317-5??? Ninja Kid II (sound CPU)  NEC MC-8123A 646   27998D  1800
317-???? Center Court (sound CPU)  NEC MC-8123B 703   640506  1800

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

#include "emu.h"
#include "mc8123.h"



static int decrypt_type0(int val,int param,int swap)
{
	if (swap == 0) val = BITSWAP8(val,7,5,3,1,2,0,6,4);
	if (swap == 1) val = BITSWAP8(val,5,3,7,2,1,0,4,6);
	if (swap == 2) val = BITSWAP8(val,0,3,4,6,7,1,5,2);
	if (swap == 3) val = BITSWAP8(val,0,7,3,2,6,4,1,5);

	if (BIT(param,3) && BIT(val,7))
		val ^= (1<<5)|(1<<3)|(1<<0);

	if (BIT(param,2) && BIT(val,6))
		val ^= (1<<7)|(1<<2)|(1<<1);

	if (BIT(val,6)) val ^= (1<<7);

	if (BIT(param,1) && BIT(val,7))
		val ^= (1<<6);

	if (BIT(val,2)) val ^= (1<<5)|(1<<0);

	val ^= (1<<4)|(1<<3)|(1<<1);

	if (BIT(param,2)) val ^= (1<<5)|(1<<2)|(1<<0);
	if (BIT(param,1)) val ^= (1<<7)|(1<<6);
	if (BIT(param,0)) val ^= (1<<5)|(1<<0);

	if (BIT(param,0)) val = BITSWAP8(val,7,6,5,1,4,3,2,0);

	return val;
}


static int decrypt_type1a(int val,int param,int swap)
{
	if (swap == 0) val = BITSWAP8(val,4,2,6,5,3,7,1,0);
	if (swap == 1) val = BITSWAP8(val,6,0,5,4,3,2,1,7);
	if (swap == 2) val = BITSWAP8(val,2,3,6,1,4,0,7,5);
	if (swap == 3) val = BITSWAP8(val,6,5,1,3,2,7,0,4);

	if (BIT(param,2)) val = BITSWAP8(val,7,6,1,5,3,2,4,0);

	if (BIT(val,1)) val ^= (1<<0);
	if (BIT(val,6)) val ^= (1<<3);
	if (BIT(val,7)) val ^= (1<<6)|(1<<3);
	if (BIT(val,2)) val ^= (1<<6)|(1<<3)|(1<<1);
	if (BIT(val,4)) val ^= (1<<7)|(1<<6)|(1<<2);

	if (BIT(val,7) ^ BIT(val,2))
		val ^= (1<<4);

	val ^= (1<<6)|(1<<3)|(1<<1)|(1<<0);

	if (BIT(param,3)) val ^= (1<<7)|(1<<2);
	if (BIT(param,1)) val ^= (1<<6)|(1<<3);

	if (BIT(param,0)) val = BITSWAP8(val,7,6,1,4,3,2,5,0);

	return val;
}

static int decrypt_type1b(int val,int param,int swap)
{
	if (swap == 0) val = BITSWAP8(val,1,0,3,2,5,6,4,7);
	if (swap == 1) val = BITSWAP8(val,2,0,5,1,7,4,6,3);
	if (swap == 2) val = BITSWAP8(val,6,4,7,2,0,5,1,3);
	if (swap == 3) val = BITSWAP8(val,7,1,3,6,0,2,5,4);

	if (BIT(val,2) && BIT(val,0))
		val ^= (1<<7)|(1<<4);

	if (BIT(val,7)) val ^= (1<<2);
	if (BIT(val,5)) val ^= (1<<7)|(1<<2);
	if (BIT(val,1)) val ^= (1<<5);
	if (BIT(val,6)) val ^= (1<<1);
	if (BIT(val,4)) val ^= (1<<6)|(1<<5);
	if (BIT(val,0)) val ^= (1<<6)|(1<<2)|(1<<1);
	if (BIT(val,3)) val ^= (1<<7)|(1<<6)|(1<<2)|(1<<1)|(1<<0);

	val ^= (1<<6)|(1<<4)|(1<<0);

	if (BIT(param,3)) val ^= (1<<4)|(1<<1);
	if (BIT(param,2)) val ^= (1<<7)|(1<<6)|(1<<3)|(1<<0);
	if (BIT(param,1)) val ^= (1<<4)|(1<<3);
	if (BIT(param,0)) val ^= (1<<6)|(1<<2)|(1<<1)|(1<<0);

	return val;
}

static int decrypt_type2a(int val,int param,int swap)
{
	if (swap == 0) val = BITSWAP8(val,0,1,4,3,5,6,2,7);
	if (swap == 1) val = BITSWAP8(val,6,3,0,5,7,4,1,2);
	if (swap == 2) val = BITSWAP8(val,1,6,4,5,0,3,7,2);
	if (swap == 3) val = BITSWAP8(val,4,6,7,5,2,3,1,0);

	if (BIT(val,3) || (BIT(param,1) && BIT(val,2)))
		val = BITSWAP8(val,6,0,7,4,3,2,1,5);

	if (BIT(val,5)) val ^= (1<<7);
	if (BIT(val,6)) val ^= (1<<5);
	if (BIT(val,0)) val ^= (1<<6);
	if (BIT(val,4)) val ^= (1<<3)|(1<<0);
	if (BIT(val,1)) val ^= (1<<2);

	val ^= (1<<7)|(1<<6)|(1<<5)|(1<<4)|(1<<1);

	if (BIT(param,2)) val ^= (1<<4)|(1<<3)|(1<<2)|(1<<1)|(1<<0);

	if (BIT(param,3))
	{
		if (BIT(param,0))
			val = BITSWAP8(val,7,6,5,3,4,1,2,0);
		else
			val = BITSWAP8(val,7,6,5,1,2,4,3,0);
	}
	else
	{
		if (BIT(param,0))
			val = BITSWAP8(val,7,6,5,2,1,3,4,0);
	}

	return val;
}

static int decrypt_type2b(int val,int param,int swap)
{
	// only 0x20 possible encryptions for this method - all others have 0x40
	// this happens because BIT(param,2) cancels the other three

	if (swap == 0) val = BITSWAP8(val,1,3,4,6,5,7,0,2);
	if (swap == 1) val = BITSWAP8(val,0,1,5,4,7,3,2,6);
	if (swap == 2) val = BITSWAP8(val,3,5,4,1,6,2,0,7);
	if (swap == 3) val = BITSWAP8(val,5,2,3,0,4,7,6,1);

	if (BIT(val,7) && BIT(val,3))
		val ^= (1<<6)|(1<<4)|(1<<0);

	if (BIT(val,7)) val ^= (1<<2);
	if (BIT(val,5)) val ^= (1<<7)|(1<<3);
	if (BIT(val,1)) val ^= (1<<5);
	if (BIT(val,4)) val ^= (1<<7)|(1<<5)|(1<<3)|(1<<1);

	if (BIT(val,7) && BIT(val,5))
		val ^= (1<<4)|(1<<0);

	if (BIT(val,5) && BIT(val,1))
		val ^= (1<<4)|(1<<0);

	if (BIT(val,6)) val ^= (1<<7)|(1<<5);
	if (BIT(val,3)) val ^= (1<<7)|(1<<6)|(1<<5)|(1<<1);
	if (BIT(val,2)) val ^= (1<<3)|(1<<1);

	val ^= (1<<7)|(1<<3)|(1<<2)|(1<<1);

	if (BIT(param,3)) val ^= (1<<6)|(1<<3)|(1<<1);
	if (BIT(param,2)) val ^= (1<<7)|(1<<6)|(1<<5)|(1<<3)|(1<<2)|(1<<1); // same as the other three combined
	if (BIT(param,1)) val ^= (1<<7);
	if (BIT(param,0)) val ^= (1<<5)|(1<<2);

	return val;
}

static int decrypt_type3a(int val,int param,int swap)
{
	if (swap == 0) val = BITSWAP8(val,5,3,1,7,0,2,6,4);
	if (swap == 1) val = BITSWAP8(val,3,1,2,5,4,7,0,6);
	if (swap == 2) val = BITSWAP8(val,5,6,1,2,7,0,4,3);
	if (swap == 3) val = BITSWAP8(val,5,6,7,0,4,2,1,3);

	if (BIT(val,2)) val ^= (1<<7)|(1<<5)|(1<<4);
	if (BIT(val,3)) val ^= (1<<0);

	if (BIT(param,0)) val = BITSWAP8(val,7,2,5,4,3,1,0,6);

	if (BIT(val,1)) val ^= (1<<6)|(1<<0);
	if (BIT(val,3)) val ^= (1<<4)|(1<<2)|(1<<1);

	if (BIT(param,3)) val ^= (1<<4)|(1<<3);

	if (BIT(val,3)) val = BITSWAP8(val,5,6,7,4,3,2,1,0);

	if (BIT(val,5)) val ^= (1<<2)|(1<<1);

	val ^= (1<<6)|(1<<5)|(1<<4)|(1<<3);

	if (BIT(param,2)) val ^= (1<<7);
	if (BIT(param,1)) val ^= (1<<4);
	if (BIT(param,0)) val ^= (1<<0);

	return val;
}

static int decrypt_type3b(int val,int param,int swap)
{
	if (swap == 0) val = BITSWAP8(val,3,7,5,4,0,6,2,1);
	if (swap == 1) val = BITSWAP8(val,7,5,4,6,1,2,0,3);
	if (swap == 2) val = BITSWAP8(val,7,4,3,0,5,1,6,2);
	if (swap == 3) val = BITSWAP8(val,2,6,4,1,3,7,0,5);

	if (BIT(val,2)) val ^= (1<<7);

	if (BIT(val,7)) val = BITSWAP8(val,7,6,3,4,5,2,1,0);

	if (BIT(param,3)) val ^= (1<<7);

	if (BIT(val,4)) val ^= (1<<6);
	if (BIT(val,1)) val ^= (1<<6)|(1<<4)|(1<<2);

	if (BIT(val,7) && BIT(val,6))
		val ^= (1<<1);

	if (BIT(val,7)) val ^= (1<<1);

	if (BIT(param,3)) val ^= (1<<7);
	if (BIT(param,2)) val ^= (1<<0);

	if (BIT(param,3)) val = BITSWAP8(val,4,6,3,2,5,0,1,7);

	if (BIT(val,4)) val ^= (1<<1);
	if (BIT(val,5)) val ^= (1<<4);
	if (BIT(val,7)) val ^= (1<<2);

	val ^= (1<<5)|(1<<3)|(1<<2);

	if (BIT(param,1)) val ^= (1<<7);
	if (BIT(param,0)) val ^= (1<<3);

	return val;
}

static int decrypt(int val, int key, int opcode)
{
	int type = 0;
	int swap = 0;
	int param = 0;

	key ^= 0xff;

	// no encryption
	if (key == 0x00)
		return val;

	type ^= BIT(key,0) << 0;
	type ^= BIT(key,2) << 0;
	type ^= BIT(key,0) << 1;
	type ^= BIT(key,1) << 1;
	type ^= BIT(key,2) << 1;
	type ^= BIT(key,4) << 1;
	type ^= BIT(key,4) << 2;
	type ^= BIT(key,5) << 2;

	swap ^= BIT(key,0) << 0;
	swap ^= BIT(key,1) << 0;
	swap ^= BIT(key,2) << 1;
	swap ^= BIT(key,3) << 1;

	param ^= BIT(key,0) << 0;
	param ^= BIT(key,0) << 1;
	param ^= BIT(key,2) << 1;
	param ^= BIT(key,3) << 1;
	param ^= BIT(key,0) << 2;
	param ^= BIT(key,1) << 2;
	param ^= BIT(key,6) << 2;
	param ^= BIT(key,1) << 3;
	param ^= BIT(key,6) << 3;
	param ^= BIT(key,7) << 3;

	if (!opcode)
	{
		param ^= 1 << 0;
		type ^= 1 << 0;
	}

	switch (type)
	{
		default:
		case 0: return decrypt_type0(val,param,swap);
		case 1: return decrypt_type0(val,param,swap);
		case 2: return decrypt_type1a(val,param,swap);
		case 3: return decrypt_type1b(val,param,swap);
		case 4: return decrypt_type2a(val,param,swap);
		case 5: return decrypt_type2b(val,param,swap);
		case 6: return decrypt_type3a(val,param,swap);
		case 7: return decrypt_type3b(val,param,swap);
	}
}


static uint8_t mc8123_decrypt(offs_t addr,uint8_t val,const uint8_t *key,int opcode)
{
	int tbl_num;

	/* pick the translation table from bits fd57 of the address */
	tbl_num = (addr & 7) + ((addr & 0x10)>>1) + ((addr & 0x40)>>2) + ((addr & 0x100)>>3) + ((addr & 0xc00)>>4) + ((addr & 0xf000)>>4) ;

	return decrypt(val,key[tbl_num + (opcode ? 0 : 0x1000)],opcode);
}

void mc8123_decode(uint8_t *rom, uint8_t *opcodes, const uint8_t *key, int length)
{
	for (int A = 0x0000;A < length;A++)
	{
		int adr = A >= 0xc000 ? (A & 0x3fff) | 0x8000 : A;
		uint8_t src = rom[A];

		/* decode the opcodes */
		opcodes[A] = mc8123_decrypt(adr,src,key,1);

		/* decode the data */
		rom[A] = mc8123_decrypt(adr,src,key,0);
	}
}