/
igs017.cpp
5957 lines (4886 loc) · 214 KB
/
igs017.cpp
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// license:BSD-3-Clause
// copyright-holders:Pierpaolo Prazzoli, Luca Elia
/************************************************************************************************************
-= IGS017 / IGS031 Based Hardware =-
driver by Pierpaolo Prazzoli, Luca Elia (l.elia@tin.it)
CPU: Z180 or 68000
Video: IGS017 or IGS031 (2 tilemaps, variable size sprites, protection)
Other: IGS025 (8255), IGS022 (protection, MCU), IGS029 (protection)
Sound: M6295(K668/AR17961) + [YM2413(U3567)]
-------------------------------------------------------------------------------------------------------------
Year + Game PCB CPU Sound Custom Other
-------------------------------------------------------------------------------------------------------------
96 Shuzi Leyuan (V127M) NO-0131-4 Z180 AR17961 U3567 IGS017 8255 Battery
97 Chaoji Damanguan II (V754C) NO-0147-6 68000 K668 IGS031 8255 Battery
97 Tian Jiang Shen Bing (V137C) NO-0157-2 Z180 AR17961 U3567 IGS017 IGS025 Battery
97 Manguan Daheng (V123T1) NO-0252 68000 M6295 IGS031 IGS025 IGS???* Battery
98 Genius 6 (V110F) NO-0131-4 Z180 K668 U3567 IGS017 IGS003c Battery
98 Long Hu Zhengba 2 (set 1) NO-0206 68000 K668 IGS031 IGS025 IGS022* Battery
98 Shuang Long Qiang Zhu 2 VS (VS203J) NO-0207 68000 K668 IGS031 IGS025 IGS022 Battery
98 Manguan Caishen (V103CS) NO-0192-1 68000 K668 IGS017 IGS025 IGS029 Battery
99 Tarzan (V107) NO-0228? Z180 U6295 IGS031 IGS025 IGS029 Battery
99 Tarzan (V109C) NO-0248-1 Z180 U6295 IGS031 IGS025 Battery
00 Chaoji Damanguan 2 - Jiaqiang Ban (V100C) NO-0271 68000 K668 IGS031 IGS025 Battery
00? Super Tarzan (V100I) NO-0230-1 Z180 K668 IGS031 IGS025 Battery
00? Happy Skill (V611IT) NO-0281 Z180 K668 IGS031 IGS025 Battery
00? Champion Poker 2 (V100A) unreadable Z180 M6295 IGS031 IGS025 Battery
00? Super Poker (V100xD03) / Formosa NO-0187 Z180 K668 U3567 IGS017 IGS025 Battery
-------------------------------------------------------------------------------------------------------------
not present in another set *
To Do:
- Protection emulation in some games, instead of patching the roms.
- NVRAM.
- mgcs: Finish IGS029 protection simulation.
Notes:
- Test mode is usually accessed by keeping test (F2) pressed during boot.
- iqblocka: keep start (1) pressed during boot for DSWs & input test. Keep test (F2) pressed for book-keeping / setup [pass: press deal (2)].
- iqblockf/genius6: press service1 (9) then press deal (2) eight times to switch to gambling. Then test (F2) enters book-keeping / setup.
- lhzb2, mgcs, slqz2, tjsb: press test (F2) + book (0) during inputs test for sound test.
- mgdh, sdmg2: press keys A + B during test mode for sound test (B1 + B2 + B3 when using a joystick in mgdh).
- spkrform: to switch from poker to Formosa press service1 (9). To switch back, press in sequence:
service3 (right of 0) then Bet (M) then press "Hold 1".."Hold 5" (Z, X, C, V, B)
************************************************************************************************************/
#include "emu.h"
#include "cpu/m68000/m68000.h"
#include "cpu/z180/z180.h"
#include "machine/i8255.h"
#include "igs022.h"
#include "machine/ticket.h"
#include "machine/timer.h"
#include "sound/okim6295.h"
#include "sound/ymopl.h"
#include "igs017_igs031.h"
#include "emupal.h"
#include "screen.h"
#include "speaker.h"
#include "multibyte.h"
#include "igspoker.lh"
#include "igsslot.lh"
#define LOG_PROT_STRING (1U << 1)
#define LOG_PROT_BITSWAP (1U << 2)
#define LOG_PROT_INCDEC (1U << 3)
#define LOG_PROT_INC (1U << 4)
#define LOG_PROT_SCRAMBLE (1U << 5)
#define LOG_PROT_REMAP (1U << 6)
#define LOG_PROT_IGS022 (1U << 7)
#define LOG_PROT_IGS029 (1U << 8)
//#define VERBOSE (LOG_GENERAL | LOG_PROT_STRING | LOG_PROT_BITSWAP | LOG_PROT_INCDEC | LOG_PROT_INC | LOG_PROT_SCRAMBLE | LOG_PROT_REMAP | LOG_PROT_IGS022 | LOG_PROT_IGS029)
#define VERBOSE (0)
#include "logmacro.h"
/***************************************************************************
---- IGS Multiplexer ----
***************************************************************************/
class igs_mux_device :
public device_t,
public device_memory_interface
{
public:
igs_mux_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock);
void address_w(u8 data);
void data_w(u8 data);
u8 data_r();
protected:
virtual void device_start() override;
virtual void device_reset() override;
virtual space_config_vector memory_space_config() const override;
private:
address_space_config m_space_config;
u8 m_address = 0;
};
device_memory_interface::space_config_vector igs_mux_device::memory_space_config() const
{
return space_config_vector {
std::make_pair(0, &m_space_config)
};
}
DEFINE_DEVICE_TYPE(IGS_MUX, igs_mux_device, "igs_mux", "IGS I/O Multiplexer")
igs_mux_device::igs_mux_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
: device_t(mconfig, IGS_MUX, tag, owner, clock)
, device_memory_interface(mconfig, *this)
, m_space_config("igs_mux", ENDIANNESS_LITTLE, 8, 8, 0)
{ }
void igs_mux_device::device_start()
{
save_item(NAME(m_address));
}
void igs_mux_device::device_reset()
{
}
void igs_mux_device::address_w(u8 data)
{
m_address = data;
}
void igs_mux_device::data_w(u8 data)
{
space().write_byte(m_address, data);
}
u8 igs_mux_device::data_r()
{
return space().read_byte(m_address);
}
/***************************************************************************
---- IGS String Protection ----
***************************************************************************/
class igs_string_device : public device_t
{
public:
igs_string_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock);
u8 result_r(); // 0x05: result_r
void do_bitswap_w(offs_t offset, u8 data); // 0x20-0x27: do_bitswap_w
u8 advance_string_offs_r(address_space &space); // 0x40: advance_string_offs_r
// Call after decryption, e.g. at the end of init_<gamename>
void dump(const char *filename, u32 string_addr, u32 xor_addr, bool is_16bits) const;
protected:
virtual void device_start() override;
virtual void device_reset() override;
private:
required_memory_region m_region_key;
u16 m_string_word = 0, m_word = 0;
u8 m_string_offs = 0;
};
DEFINE_DEVICE_TYPE(IGS_STRING, igs_string_device, "igs_string", "IGS String Protection")
igs_string_device::igs_string_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
: device_t(mconfig, IGS_STRING, tag, owner, clock)
, m_region_key(*this, DEVICE_SELF)
{ }
void igs_string_device::dump(const char *filename, u32 string_addr, u32 xor_addr, bool is_16bits) const
{
FILE *f = fopen(filename, "wb");
if (!f)
{
printf("Error opening igs_string dump file %s\n", filename);
fclose(f);
return;
}
printf("igs_string addr = %x, %x (%d bits)\n", string_addr, xor_addr, is_16bits ? 16 : 8);
const u8 * const string_base = (const u8 *)memregion(":maincpu")->base() + string_addr;
const u8 * const xor_base = (const u8 *)memregion(":maincpu")->base() + xor_addr;
for (u32 i = 0; i < 0xec; ++i)
{
const u32 addr = is_16bits ? BYTE_XOR_BE(i) : i;
const u8 data = string_base[addr] ^ xor_base[addr];
fwrite(&data, 1, 1, f);
}
fclose(f);
}
void igs_string_device::device_start()
{
save_item(NAME(m_string_offs));
save_item(NAME(m_string_word));
save_item(NAME(m_word));
}
void igs_string_device::device_reset()
{
m_string_word = m_word = 0;
m_string_offs = 0;
}
u8 igs_string_device::result_r()
{
const u8 res = bitswap<8>(m_word, 5, 2, 9, 7, 10, 13, 12, 15);
LOGMASKED(LOG_PROT_STRING, "%s: read bitswap - word %04x -> %02x\n", machine().describe_context(), m_word, res);
return res;
}
void igs_string_device::do_bitswap_w(offs_t offset, u8 data)
{
const u16 x = m_word;
m_word = 0;
m_word |= (BIT( x, 0) ^ BIT(m_string_word, 0)) << 1; // bit 1
m_word |= (BIT(~x, 1) ^ BIT(m_string_word, 1)) << 2; // bit 2
m_word |= (BIT(~x, 2) ^ BIT(m_string_word, 2)) << 3; // bit 3
m_word |= (BIT(~x, 13) ^ BIT( x, 3)) << 4; // bit 4
m_word |= (BIT(~x, 4) ^ BIT(m_string_word, 4)) << 5; // bit 5
m_word |= (BIT( x, 5) ^ BIT(m_string_word, 5)) << 6; // bit 6
m_word |= (BIT(~x, 6) ^ BIT(m_string_word, 6)) << 7; // bit 7
m_word |= (BIT(~x, 7) ^ BIT(m_string_word, 7)) << 8; // bit 8
m_word |= (BIT(~x, 8) ^ BIT(m_string_word, 8)) << 9; // bit 9
m_word |= (BIT( x, 9) ^ BIT(m_string_word, 9)) << 10; // bit 10
m_word |= (BIT(~x, 10) ^ BIT( x, 3)) << 11; // bit 11
m_word |= (BIT( x, 11) ^ BIT(m_string_word, 11)) << 12; // bit 12
m_word |= (BIT(~x, 12) ^ BIT(m_string_word, 12)) << 13; // bit 13
m_word |= (BIT( x, 13) ^ BIT(m_string_word, 13)) << 14; // bit 14
m_word |= (BIT( x, 14) ^ BIT(m_string_word, 14)) << 15; // bit 15
// bit 0
const u16 bit0 = BIT(~x, 15) ^ BIT(x, 7);
// bit 0 is further xor'd with bit x of the data written to this address
const u16 xor0 = BIT(data, offset & 7);
m_word |= bit0 ^ xor0;
LOGMASKED(LOG_PROT_STRING, "%s: exec bitswap on port %02x = %02x - bit0 %x, xor0 %x, word %04x -> %04x\n", machine().describe_context(),
offset, data, bit0, xor0, x, m_word);
}
u8 igs_string_device::advance_string_offs_r(address_space &space)
{
const u8 next_string_offs = ((m_string_offs + 1) < 0xec ? (m_string_offs + 1) : 0);
const u8 shift = (next_string_offs & 1) ? 8 : 0;
const u8 next_string_byte = m_region_key ? m_region_key->base()[next_string_offs] : 0;
const u16 next_string_word = (m_string_word & (0xff << (8 - shift))) | (next_string_byte << shift);
LOGMASKED(LOG_PROT_STRING, "%s: advance string offs %02x -> %02x, string data = %02x, string word %04x -> %04x\n", machine().describe_context(),
m_string_offs, next_string_offs, next_string_byte, m_string_word, next_string_word);
m_string_offs = next_string_offs;
m_string_word = next_string_word;
return space.unmap();
}
// Similar protection to that found in igs011.cpp:
/***************************************************************************
---- IGS Parametric Bitswaps Protection ----
The protection involves an internal 16-bit value (val), two mode registers
(mode_f = 0..f, mode_3 = 0..3) and 8 x 16-bit registers (word).
The two modes affect the bitswap, and are set by loading the (same) mode-specific value
to all the word registers, and then writing to the mode_f or mode_3 trigger register.
The bitswap of the internal value is then performed writing to one of 8 trigger registers,
according to the modes, trigger register and value written.
The result is read through a fixed bitswap of the internal value.
***************************************************************************/
class igs_bitswap_device : public device_t
{
public:
igs_bitswap_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock);
u8 result_r(); // 0x03: result_r
void word_w(u8 data); // 0x40-0x47: word_w
void mode_f_w(u8 data); // 0x48: mode_f_w
void mode_3_w(u8 data); // 0x50: mode_3_w
void do_bitswap_w(offs_t offset, u8 data); // 0x80-0x87: do_bitswap_w
void reset_w(u8 data); // 0xa0: reset_w
template <unsigned N> void set_m3_bits(u8 b0, u8 b1, u8 b2, u8 b3);
void set_mf_bits(u8 b0, u8 b1, u8 b2, u8 b3);
void set_val_xor(u16 val_xor);
protected:
virtual void device_start() override;
virtual void device_reset() override;
private:
u8 m_m3 = 0, m_mf = 0;
u16 m_val = 0, m_word = 0;
u8 m_m3_bits[4][4] = { {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} };
u8 m_mf_bits[4] = {0,0,0,0};
u16 m_val_xor = 0;
};
template <unsigned N> void igs_bitswap_device::set_m3_bits(u8 b0, u8 b1, u8 b2, u8 b3)
{
m_m3_bits[N][0] = b0;
m_m3_bits[N][1] = b1;
m_m3_bits[N][2] = b2;
m_m3_bits[N][3] = b3;
#if 0
printf("igs_bitswap: INIT m3_bits[%x] =", N);
for (int i = 0; i < 4; ++i)
{
u8 bit = m_m3_bits[N][i];
printf(" %c%d", BIT(bit, 7) ? '~' : ' ', BIT(bit, 7) ? (bit ^ 0xff) : bit);
}
printf("\n");
#endif
}
void igs_bitswap_device::set_mf_bits(u8 b0, u8 b1, u8 b2, u8 b3)
{
m_mf_bits[0] = b0;
m_mf_bits[1] = b1;
m_mf_bits[2] = b2;
m_mf_bits[3] = b3;
#if 0
printf("igs_bitswap: INIT mf_bits = %d %d %d %d\n", m_mf_bits[0], m_mf_bits[1], m_mf_bits[2], m_mf_bits[3]);
#endif
}
void igs_bitswap_device::set_val_xor(u16 val_xor)
{
m_val_xor = val_xor;
#if 0
printf("igs_bitswap: INIT val_xor = %04x\n", m_val_xor);
#endif
}
DEFINE_DEVICE_TYPE(IGS_BITSWAP, igs_bitswap_device, "igs_bitswap", "IGS Bitswap Protection")
igs_bitswap_device::igs_bitswap_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
: device_t(mconfig, IGS_BITSWAP, tag, owner, clock)
{ }
void igs_bitswap_device::device_start()
{
save_item(NAME(m_m3));
save_item(NAME(m_mf));
save_item(NAME(m_val));
save_item(NAME(m_word));
}
void igs_bitswap_device::device_reset()
{
m_m3 = m_mf = 0;
m_val = m_word = 0;
}
u8 igs_bitswap_device::result_r()
{
u8 res = bitswap<8>(m_val, 5, 2, 9, 7, 10, 13, 12, 15);
LOGMASKED(LOG_PROT_BITSWAP, "%s: read bitswap - val %04x -> %02x\n", machine().describe_context(), m_val, res);
return res;
}
void igs_bitswap_device::word_w(u8 data)
{
m_word = (m_word << 8) | data;
LOGMASKED(LOG_PROT_BITSWAP, "%s: word = %02x\n", machine().describe_context(), m_word);
}
void igs_bitswap_device::mode_f_w(u8 data)
{
m_mf = 0;
if ((m_word & 0x0f00) != 0x0a00) m_mf |= 0x08;
if ((m_word & 0xf000) != 0x9000) m_mf |= 0x04;
if ((m_word & 0x000f) != 0x0006) m_mf |= 0x02;
if ((m_word & 0x00f0) != 0x0090) m_mf |= 0x01;
LOGMASKED(LOG_PROT_BITSWAP, "%s: mode_f = %x (word = %04x)\n", machine().describe_context(), m_mf, m_word);
}
void igs_bitswap_device::mode_3_w(u8 data)
{
m_m3 = 0;
if ((m_word & 0x000f) != 0x0003) m_m3 |= 0x02;
if ((m_word & 0x00f0) != 0x0050) m_m3 |= 0x01;
LOGMASKED(LOG_PROT_BITSWAP, "%s: mode_3 = %x (word = %04x)\n", machine().describe_context(), m_m3, m_word);
}
void igs_bitswap_device::do_bitswap_w(offs_t offset, u8 data)
{
u16 x = m_val;
// bits 1-15 come from inverting some bits, xor-ing 4 bits (they change per game) with the mf bits, then shifting to the left
m_val ^= m_val_xor;
for (int i = 0; i < 4; ++i)
m_val ^= BIT(m_mf, i) << m_mf_bits[i];
m_val <<= 1;
// bit 0 is the xor of 4 bits from val (some inverted). The 4 bits are picked based on m3 (and they change per game)
u16 bit0 = 0;
for (int i = 0; i < 4; ++i)
{
u8 bit = m_m3_bits[m_m3][i];
bit0 ^= BIT(bit, 7) ? (BIT(x, bit ^ 0xff) ^ 1) : BIT(x, bit);
}
// bit 0 is further xor'd with bit x of the data written to this address (8x)
u16 xor0 = BIT(data, offset & 7);
m_val |= bit0 ^ xor0;
LOGMASKED(LOG_PROT_BITSWAP, "%s: exec bitswap on port %02x = %02x - mode_3 %02x, mode_f %02x, bit0 %x, xor0 %x, val %04x -> %04x\n", machine().describe_context(), offset, data, m_m3, m_mf, bit0, xor0, x, m_val);
return;
}
void igs_bitswap_device::reset_w(u8 data)
{
LOGMASKED(LOG_PROT_BITSWAP, "%s: reset bitswap - val %04x -> 0\n", machine().describe_context(), m_val);
m_val = 0;
}
/***************************************************************************
---- IGS Inc/Dec Protection ----
The chip holds an internal 4-bit value. It is manipulated by issuing commands,
where each command is assigned a specific address range, and is triggered
by writing to that range. Possible commands:
- [offset 0] RESET: value = 0
- [offset 1] DEC: decrement value
- [offset 3] INC: increment value
The protection value is read from an additional address range:
- [offset 5] READ: read bitswap(value). Only 4 bits are checked (0-32-1--, i.e. mask $B4)
***************************************************************************/
class igs_incdec_device : public device_t
{
public:
igs_incdec_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock);
u8 result_r();
void reset_w(u8 data);
void inc_w(u8 data);
void dec_w(u8 data);
protected:
virtual void device_start() override;
virtual void device_reset() override;
private:
u8 m_val = 0;
};
u8 igs_incdec_device::result_r()
{
u8 res = (BIT(m_val, 0) << 7) |
(BIT(m_val, 3) << 5) |
(BIT(m_val, 2) << 4) |
(BIT(m_val, 1) << 2) ;
LOGMASKED(LOG_PROT_INCDEC, "%s: value read, %02x -> %02x\n", machine().describe_context(), m_val, res);
return res;
}
void igs_incdec_device::reset_w(u8 data)
{
m_val = 0x00;
LOGMASKED(LOG_PROT_INCDEC, "%s: reset -> %02x\n", machine().describe_context(), m_val);
}
void igs_incdec_device::inc_w(u8 data)
{
m_val++;
LOGMASKED(LOG_PROT_INCDEC, "%s: inc -> %02x\n", machine().describe_context(), m_val);
}
void igs_incdec_device::dec_w(u8 data)
{
m_val--;
LOGMASKED(LOG_PROT_INCDEC, "%s: dec -> %02x\n", machine().describe_context(), m_val);
}
DEFINE_DEVICE_TYPE(IGS_INCDEC, igs_incdec_device, "igs_incdec", "IGS Inc/Dec Protection")
igs_incdec_device::igs_incdec_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
: device_t(mconfig, IGS_INCDEC, tag, owner, clock)
{ }
void igs_incdec_device::device_start()
{
save_item(NAME(m_val));
}
void igs_incdec_device::device_reset()
{
m_val = 0;
}
/***************************************************************************
---- IGS Inc Protection ----
***************************************************************************/
class igs_inc_device : public device_t
{
public:
igs_inc_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock);
u8 result_r();
void reset_w(u8 data);
void inc_w(u8 data);
protected:
virtual void device_start() override;
virtual void device_reset() override;
private:
u8 m_val = 0;
};
u8 igs_inc_device::result_r()
{
const u8 res = (BIT(~m_val, 0) | (BIT(m_val, 1) & BIT(m_val, 2))) << 5;
LOGMASKED(LOG_PROT_INC, "%s: value read, %02x -> %02x\n", machine().describe_context(), m_val, res);
return res;
}
void igs_inc_device::reset_w(u8 data)
{
m_val = 0x00;
LOGMASKED(LOG_PROT_INC, "%s: reset -> %02x\n", machine().describe_context(), m_val);
}
void igs_inc_device::inc_w(u8 data)
{
m_val++;
LOGMASKED(LOG_PROT_INC, "%s: inc -> %02x\n", machine().describe_context(), m_val);
}
DEFINE_DEVICE_TYPE(IGS_INC, igs_inc_device, "igs_inc", "IGS Inc Protection")
igs_inc_device::igs_inc_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
: device_t(mconfig, IGS_INC, tag, owner, clock)
{ }
void igs_inc_device::device_start()
{
save_item(NAME(m_val));
}
void igs_inc_device::device_reset()
{
m_val = 0;
}
namespace {
// igs017_state
class igs017_state : public driver_device
{
public:
igs017_state(const machine_config &mconfig, device_type type, const char *tag)
: driver_device(mconfig, type, tag)
, m_remap_addr(-1)
, m_maincpu(*this, "maincpu")
, m_screen(*this, "screen")
, m_igs017_igs031(*this, "igs017_igs031")
, m_oki(*this, "oki")
, m_igs_mux(*this, "igs_mux")
, m_ppi(*this, "ppi8255")
// Optional shared pointers
, m_decrypted_opcodes(*this, "decrypted_opcodes")
// Optional devices
, m_hopper(*this, "hopper")
, m_igs_bitswap(*this, "igs_bitswap")
, m_igs_string(*this, "igs_string")
, m_igs_incdec(*this, "igs_incdec")
, m_igs_inc(*this, "igs_inc")
, m_igs022(*this,"igs022")
// Optional I/O
, m_io_coins(*this, "COINS")
, m_io_joy(*this, "JOY")
, m_io_key(*this, "KEY%u", 0U)
, m_io_dsw(*this, "DSW%u", 1U)
, m_lamps(*this, "lamp%u", 1U)
{ }
// Construct
void base_machine_oki(machine_config &config, const XTAL &xtal_oki);
// Z180
void cpoker2(machine_config &config);
void genius6(machine_config &config);
void happyskl(machine_config &config);
void iqblocka(machine_config &config);
void iqblockf(machine_config &config);
void spkrform(machine_config &config);
void starzan(machine_config &config);
void tarzan(machine_config &config);
void tjsb(machine_config &config);
// 68000
void lhzb2(machine_config &config);
void lhzb2a(machine_config &config);
void mgcs(machine_config &config);
void mgdh(machine_config &config);
void mgdha(machine_config &config);
void sdmg2(machine_config &config);
void sdmg2p(machine_config &config);
void slqz2(machine_config &config);
// Init
void init_cpoker2();
void init_happyskl();
void init_iqblocka();
void init_lhzb2();
void init_lhzb2a();
void init_mgcs();
void init_mgdh();
void init_mgdha();
void init_sdmg2();
void init_sdmg2p();
void init_slqz2();
void init_spkrform();
void init_starzan();
void init_tarzan();
void init_tarzana();
void init_tarzanc();
void init_tjsb();
protected:
virtual void machine_start() override;
virtual void machine_reset() override;
virtual void video_start() override;
private:
int m_remap_addr;
required_device<cpu_device> m_maincpu;
required_device<screen_device> m_screen;
required_device<igs017_igs031_device> m_igs017_igs031;
required_device<okim6295_device> m_oki;
required_device<igs_mux_device> m_igs_mux;
required_device<i8255_device> m_ppi;
// Optional shared pointers
optional_shared_ptr<u8> m_decrypted_opcodes;
// Optional devices
optional_device<hopper_device> m_hopper;
optional_device<igs_bitswap_device> m_igs_bitswap;
optional_device<igs_string_device> m_igs_string;
optional_device<igs_incdec_device> m_igs_incdec;
optional_device<igs_inc_device> m_igs_inc;
optional_device<igs022_device> m_igs022;
// Optional I/O
optional_ioport m_io_coins;
optional_ioport m_io_joy;
optional_ioport_array<5> m_io_key;
optional_ioport_array<3> m_io_dsw;
output_finder<6> m_lamps;
u8 m_input_select;
u8 m_dsw_select;
u8 m_scramble_data;
u8 m_igs022_latch;
// IGS029 protection (communication)
u8 m_igs029_send_data, m_igs029_recv_data;
u8 m_igs029_send_buf[256], m_igs029_recv_buf[256];
int m_igs029_send_len, m_igs029_recv_len;
// IGS029 protection (mgcs)
u32 m_igs029_mgcs_long;
void dsw_select_w(u8 data);
u8 dsw_r();
template<u8 WarnMask>
void input_select_w(u8 data)
{
m_input_select = data;
if (data & WarnMask)
logerror("%s: warning, unknown bits written in input_select_w = %02x\n", machine().describe_context(), data);
}
template<u8 Bit, u8 WarnMask>
void oki_sound_bank_w(u8 data)
{
m_oki->set_rom_bank(BIT(data, Bit));
if (data & WarnMask)
logerror("%s: warning, unknown bits written in oki_sound_bank_w = %02x\n", machine().describe_context(), data);
}
template<u8 Bit, u8 WarnMask>
void hopper_motor_w(u8 data)
{
m_hopper->motor_w(BIT(data, Bit));
if (data & WarnMask)
logerror("%s: warning, unknown bits written in hopper_motor_w = %02x\n", machine().describe_context(), data);
}
template<u8 Bit, u8 WarnMask, int Counter>
void counter_w(u8 data)
{
machine().bookkeeping().coin_counter_w(Counter, BIT(data, Bit));
// popmessage("COUNTER %d: %02X", Counter, data);
if (data & WarnMask)
logerror("%s: warning, unknown bits written in counter_w = %02x\n", machine().describe_context(), data);
}
template<u8 Bit, u8 WarnMask>
void igs022_execute_w(u8 data)
{
if (!BIT(m_igs022_latch, Bit) && BIT(data, Bit)) // 0 -> 1 executes
{
m_igs022->handle_command();
const u8 new_scramble_data = (m_scramble_data + 1) & 0x7f;
LOGMASKED(LOG_PROT_IGS022, "%s: IGS022 scrambled data %02x -> %02x\n", machine().describe_context(), m_scramble_data, new_scramble_data);
m_scramble_data = new_scramble_data;
}
if (data & WarnMask)
logerror("%s: warning, unknown bits written in igs022_execute_w = %02x\n", machine().describe_context(), data);
m_igs022_latch = data;
}
// Palette
u16 lhzb2a_palette_bitswap(u16 bgr) const;
u16 mgcs_palette_bitswap(u16 bgr) const;
u16 slqz2_palette_bitswap(u16 bgr) const;
u16 tarzan_palette_bitswap(u16 bgr) const;
u16 tjsb_palette_bitswap(u16 bgr) const;
// Game Specific I/O
void incdec_remap_addr_w(offs_t offset, u8 data);
void cpoker2_lamps_sound_w(u8 data);
void happyskl_lamps_sound_w(u8 data);
u8 lhzb2_keys_r();
void lhzb2_keys_hopper_w(u8 data);
u8 lhzb2_scramble_data_r();
void lhzb2_igs022_execute_w(u8 data);
void lhzb2a_keys_hopper_w(offs_t offset, u16 data, u16 mem_mask = ~0);
u16 lhzb2a_input_r(offs_t offset);
void lhzb2a_remap_addr_w(address_space &space, u16 data);
u8 mgcs_keys_joy_r();
void mgcs_keys_hopper_igs029_w(u8 data);
u8 mgcs_scramble_data_r();
void mgcs_scramble_data_w(u8 data);
u8 mgcs_igs029_data_r();
void mgcs_igs029_data_w(u8 data);
u8 mgdh_keys_r();
void mgdh_keys_hopper_w(u8 data);
void mgdh_counter_w(u8 data);
u8 sdmg2_keys_joy_r();
void sdmg2_keys_hopper_w(u8 data);
u8 sdmg2p_keys_r();
void slqz2_sound_hopper_w(u8 data);
u8 slqz2_scramble_data_r();
void starzan_counter_w(u8 data);
void starzan_lamps_sound_w(u8 data);
u8 tarzan_keys_joy_r();
void tarzan_counter_w(u8 data);
void tarzan_dsw_sound_w(u8 data);
void tarzan_incdec_remap_addr_w(offs_t offset, u8 data);
// Machine
DECLARE_MACHINE_RESET(lhzb2a);
TIMER_DEVICE_CALLBACK_MEMBER(iqblocka_interrupt);
TIMER_DEVICE_CALLBACK_MEMBER(mgcs_interrupt);
TIMER_DEVICE_CALLBACK_MEMBER(mgdh_interrupt);
// Decrypt
void decrypt_program_rom(int mask, int a7, int a6, int a5, int a4, int a3, int a2, int a1, int a0);
void lhzb2_decrypt_sprites();
void lhzb2_decrypt_tiles();
void mgcs_decrypt_program_rom();
void mgcs_decrypt_tiles();
void mgcs_flip_sprites();
void mgcs_igs029_run();
void slqz2_decrypt_tiles();
void spkrform_decrypt_sprites();
void starzan_decrypt_program_rom();
void starzan_decrypt_sprites();
void tarzan_decrypt_program_rom();
void tarzan_decrypt_sprites(size_t max_size);
void tarzan_decrypt_tiles(int address_xor);
void tarzana_decrypt_program_rom();
void tjsb_decrypt_sprites();
// ROM Patches
// void lhzb2_patch_rom();
// void mgcs_patch_rom();
// void slqz2_patch_rom();
void mgdh_patch_rom();
void spkrform_patch_rom();
// Memory maps
void decrypted_opcodes_map(address_map &map);
void igs_bitswap_mux_map(address_map &map);
void igs_fixed_data_mux_map(address_map &map);
void igs_string_mux_map(address_map &map);
void cpoker2_io(address_map &map);
void cpoker2_map(address_map &map);
void cpoker2_mux_map(address_map &map);
void happyskl_io(address_map &map);
void happyskl_mux_map(address_map &map);
void iqblocka_io(address_map &map);
void iqblocka_map(address_map &map);
void iqblocka_mux_map(address_map &map);
void iqblockf_mux_map(address_map &map);
void lhzb2_map(address_map &map);
void lhzb2_mux_map(address_map &map);
void lhzb2a_map(address_map &map);
void lhzb2a_mux_map(address_map &map);
void mgcs_map(address_map &map);
void mgcs_mux_map(address_map &map);
void mgdh_mux_map(address_map &map);
void mgdh_map(address_map &map);
void mgdha_mux_map(address_map &map);
void sdmg2_map(address_map &map);
void sdmg2_mux_map(address_map &map);
void sdmg2p_map(address_map &map);
void sdmg2p_mux_map(address_map &map);
void slqz2_map(address_map &map);
void slqz2_mux_map(address_map &map);
void spkrform_io(address_map &map);
void spkrform_mux_map(address_map &map);
void starzan_io(address_map &map);
void starzan_mux_map(address_map &map);
void tarzan_io(address_map &map);
void tarzan_mux_map(address_map &map);
void tjsb_io(address_map &map);
void tjsb_map(address_map &map);
void tjsb_mux_map(address_map &map);
};
void igs017_state::machine_start()
{
m_lamps.resolve();
save_item(NAME(m_remap_addr));
save_item(NAME(m_input_select));
save_item(NAME(m_dsw_select));
save_item(NAME(m_scramble_data));
save_item(NAME(m_igs022_latch));
save_item(NAME(m_igs029_send_data));
save_item(NAME(m_igs029_recv_data));
save_item(NAME(m_igs029_send_buf));
save_item(NAME(m_igs029_recv_buf));
save_item(NAME(m_igs029_send_len));
save_item(NAME(m_igs029_recv_len));
save_item(NAME(m_igs029_mgcs_long));
}
void igs017_state::machine_reset()
{
m_input_select = m_dsw_select = 0xff;
m_scramble_data = 0;
m_igs029_send_len = m_igs029_recv_len = 0;
m_igs029_mgcs_long = 0;
}
void igs017_state::dsw_select_w(u8 data)
{
m_dsw_select = data;
}
u8 igs017_state::dsw_r()
{
u8 ret = 0xff;
if (!BIT(m_dsw_select, 0)) ret &= m_io_dsw[0]->read();
if (!BIT(m_dsw_select, 1)) ret &= m_io_dsw[1]->read();
if (!BIT(m_dsw_select, 2)) ret &= m_io_dsw[2]->read();
return ret;
}
/***************************************************************************
Video Hardware
***************************************************************************/
void igs017_state::video_start()
{
m_igs017_igs031->video_start();
}
// palette bitswap callbacks
u16 igs017_state::mgcs_palette_bitswap(u16 bgr) const
{
bgr = ((bgr & 0xff00) >> 8) | ((bgr & 0x00ff) << 8);
return bitswap<16>(bgr, 7, 8, 9, 2, 14, 3, 13, 15, 12, 11, 10, 0, 1, 4, 5, 6);
}
u16 igs017_state::lhzb2a_palette_bitswap(u16 bgr) const
{
return bitswap<16>(bgr, 15,9,13,12,11,5,4,8,7,6,0,14,3,2,1,10);
}
u16 igs017_state::tjsb_palette_bitswap(u16 bgr) const
{
return bitswap<16>(bgr, 15,12,3,6,10,5,4,2,9,13,8,7,11,1,0,14);
}
u16 igs017_state::slqz2_palette_bitswap(u16 bgr) const
{
return bitswap<16>(bgr, 15,14,9,4,11,10,12,3,7,6,5,8,13,2,1,0);
}
u16 igs017_state::tarzan_palette_bitswap(u16 bgr) const
{
return bitswap<16>(bgr, 15, 0,1,2,3,4, 10,11,12,13,14, 5,6,7,8,9);
}
/***************************************************************************
Decryption
***************************************************************************/
[[maybe_unused]] void save_decrypted_rom(const u8 * const rom, int rom_size)
{
FILE *f = fopen("igs017_decrypted.bin", "wb");
fwrite(rom, 1, rom_size, f);
fclose(f);
}
void igs017_state::decrypt_program_rom(int mask, int a7, int a6, int a5, int a4, int a3, int a2, int a1, int a0)
{
const int rom_size = memregion("maincpu")->bytes();
u8 * const rom = memregion("maincpu")->base();
std::unique_ptr<u8[]> tmp = std::make_unique<u8[]>(rom_size);
// decrypt the program ROM
// XOR layer
for (int i = 0; i < rom_size; i++)
{
if (i & 0x2000)