forked from mamedev/mame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfddebug.cpp
More file actions
2439 lines (2025 loc) · 84 KB
/
Copy pathfddebug.cpp
File metadata and controls
2439 lines (2025 loc) · 84 KB
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:Aaron Giles
#include <functional>
#include "emu.h"
#include "machine/fddebug.h"
void fd1094_init_debugging(running_machine &machine, const char *cpureg, const char *keyreg, const char *statreg, void (*changed)(running_machine &))
{
}
#if 0
/***************************************************************************
fddebug.c
FD1094 decryption helper routines.
****************************************************************************
When searching for new keys, here are some common sequences in the
System 16B games that are useful.
IRQ4 handler entry points:
common sequence 1:
MOVE SR,(A7) 40D7
MOVE.B #$23,(A7) 1EBC 0023
MOVEM.L D0-D7/A0-A6,-(A7) 48E7 FFFE
common sequence 2:
MOVEM.L D0-D7/A0-A6,-(A7) 48E7 FFFE
common sequence 3:
BRA.W <previous sequence> 6000 xxxx
IRQ4 handler exit points:
common sequence (often appears twice nearby):
MOVE (A7)+,D0-D7/A0-A6 4CDF 7FFF
RTE 4E73
Entry points:
common sequence 1:
LEA <stack>.L,A7 4FF9 xxxx xxxx
MOVE #$2700,SR 46FC 2700
CMPI.L #$00xxffff,D0 0C80 00xx FFFF
MOVEQ #0,D0
MOVE.L D0,D1 2200
MOVE.L D0,D2 2400
MOVE.L D0,D3 2600
MOVE.L D0,D4 2800
MOVE.L D0,D5 2A00
MOVE.L D0,D6 2C00
MOVE.L D0,D7 2E00
common sequence 2:
LEA <stack>.W,A7 4FF8 xxxx
MOVE #$2700,SR 46FC 2700
CMPI.L #$00xxffff,D0 0C80 00xx FFFF
MOVEQ #0,D0
MOVE.L D0,D1 2200
MOVE.L D0,D2 2400
MOVE.L D0,D3 2600
MOVE.L D0,D4 2800
MOVE.L D0,D5 2A00
MOVE.L D0,D6 2C00
MOVE.L D0,D7 2E00
common sequence 3:
LEA <stack>.W,A7 4FF8 xxxx
MOVE #$2700,SR 46FC 2700
MOVEQ #0,D0
MOVE.L D0,D1 2200
MOVE.L D0,D2 2400
MOVE.L D0,D3 2600
MOVE.L D0,D4 2800
MOVE.L D0,D5 2A00
MOVE.L D0,D6 2C00
MOVE.L D0,D7 2E00
common sequence 4:
BRA.W <previous sequence> 6000 xxxx
****************************************************************************
These constraints worked for finding exctleag's seed:
fdcset 0410,4ff9
fdcset 0412,0000
fdcset 0414,0000
fdcset 0416,46fc
fdcset 0418,2700
fdcset 041a,0c80
fdcset 041c,0000,ff00
fdcset 041e,ffff
//fdcset 0f9e,40d7,ffff,irq
fdcset 0fa0,1ebc,ffff,irq
fdcset 0fa2,0023,ffff,irq
//fdcset 0fa4,48e7,ffff,irq
fdcset 0fa6,fffe,ffff,irq
fdcset 0fa8,13f8,ffff,irq
fdcset 0fac,00c4,ffff,irq
fdcset 0fae,0001,ffff,irq
//fdcset 1060,4cdf,ffff,irq
fdcset 1062,7fff,ffff,irq
//fdcset 1064,4e73,ffff,irq
//fdcset 1070,4cdf,ffff,irq
fdcset 1072,7fff,ffff,irq
//fdcset 1074,4e73,ffff,irq
****************************************************************************
Add something like this to debug_view_memory::write
// hack for FD1094 editing
#ifdef FD1094_HACK
if (source.m_base == machine().root_device().memregion("user2"))
{
extern void fd1094_regenerate_key(running_machine &machine);
fd1094_regenerate_key(machine());
}
#endif
***************************************************************************/
#include "emu.h"
#include "machine/fd1094.h"
#include "cpu/m68000/m68000.h"
#include "debug/debugcmd.h"
#include "debug/debugcon.h"
#include "debug/debugcpu.h"
#include "debug/debugvw.h"
#include "machine/fddebug.h"
/***************************************************************************
CONSTANTS
***************************************************************************/
#define KEY_SIZE 8192
#define MAX_CONSTRAINTS 100
#define MAX_SEARCH_DEPTH 10000
/* status byte breakdown */
#define STATE_MASK 0xff00
#define HIBITS_MASK 0x00c0
#define SEARCH_MASK 0x0020
#define STATUS_MASK 0x001f
/* possible status values */
#define STATUS_UNVISITED 0x00
#define STATUS_LOCKED 0x01
#define STATUS_NOCHANGE 0x02
#define STATUS_GUESS 0x03
/* sizes for the opcode table */
#define SIZE_BYTE 1 /* single byte */
#define SIZE_WORD 2 /* single word */
#define SIZE_LONG 3 /* single long */
#define SIZE_BIT 4 /* single byte, limited to bit sizes (0-7) */
#define SIZE_MASK 7
/* operand sizes */
#define OF_SIZEMASK (SIZE_MASK << 0)
#define OF_BYTE (SIZE_BYTE << 0) /* byte size operation */
#define OF_WORD (SIZE_WORD << 0) /* word size operation */
#define OF_LONG (SIZE_LONG << 0) /* long size operation */
/* immediate sizes */
#define OF_ISIZEMASK (SIZE_MASK << 3)
#define OF_IMMB (SIZE_BYTE << 3) /* immediate byte follows */
#define OF_IMMW (SIZE_WORD << 3) /* immediate word follows */
#define OF_IMML (SIZE_LONG << 3) /* immediate long follows */
#define OF_IMMBIT (SIZE_BIT << 3) /* immediate byte follows */
/* other opcode flags */
#define OF_EASRC 0x00000040 /* standard EA is source */
#define OF_EADST 0x00000080 /* standard EA is destination */
#define OF_EADREG 0x00000100 /* EA with data register is allowed */
#define OF_EAAREG 0x00000200 /* EA with address register is allowed */
#define OF_EAA 0x00000400 /* EA with (An) is allowed */
#define OF_EAPLUS 0x00000800 /* EA with (An)+ is allowed */
#define OF_EAMINUS 0x00001000 /* EA with -(An) is allowed */
#define OF_EADISP 0x00002000 /* EA with (D,An) displacement is allowed */
#define OF_EAABS 0x00004000 /* EA with absolute (both word and long) is allowed */
#define OF_EAIMM 0x00008000 /* EA with immediate is allowed */
#define OF_EAPCR 0x00010000 /* EA with PC-relative addressing is allowed */
#define OF_RARE 0x00080000 /* opcode is not commonly used */
#define OF_BRANCH 0x00100000 /* opcode represents a branch */
#define OF_JMP 0x00200000 /* opcode represents a jmp/jsr */
#define OF_MOVE 0x00400000 /* opcode has MOVE semantics */
#define OF_LENMASK 0xf0000000 /* opcode length mask */
#define OF_INVALID 0xffffffff /* invalid opcode */
/***************************************************************************
TYPE DEFINITIONS
***************************************************************************/
/* a single possible instruction decoding */
struct fd1094_possibility
{
offs_t basepc; /* starting PC of the possibility */
int length; /* number of words */
uint8_t instrbuffer[10]; /* instruction data for disassembler */
uint8_t keybuffer[10]; /* array of key values to produce the instruction data */
uint8_t iffy; /* is this an iffy possibility? */
char dasm[256]; /* disassembly */
};
/* an entry in the opcode table */
struct optable_entry
{
uint32_t flags; /* per-opcode flags */
const char * string; /* identifying string */
};
/***************************************************************************
GLOBAL VARIABLES
***************************************************************************/
/* array of PCs not to stop at */
static uint8_t * ignorepc;
static uint8_t ignore_all;
/* array of information about each opcode */
static std::unique_ptr<optable_entry[]> optable;
/* buffer for undoing operations */
static uint8_t * undobuff;
/* array of possible instruction decodings */
static fd1094_possibility posslist[4*4*4*4*4];
static int posscount;
/* array of possible seeds */
static uint32_t * possible_seed;
/* array of constraints */
static fd1094_constraint constraints[MAX_CONSTRAINTS];
static int constcount;
/* stack of search addresses */
static uint32_t searchstack[MAX_SEARCH_DEPTH];
static int searchsp;
/* current key generation parameters */
static uint32_t fd1094_global;
static uint32_t fd1094_seed;
static uint8_t keydirty;
/* pointers to our data */
static uint16_t * coderegion;
static uint32_t coderegion_words;
static uint8_t * keyregion;
static uint16_t * keystatus;
static uint32_t keystatus_words;
/* key changed callback */
static void (*key_changed)(running_machine &);
/***************************************************************************
FUNCTION PROTOTYPES
***************************************************************************/
static void set_default_key_params(running_machine &machine);
static void load_overlay_file(running_machine &machine);
static void save_overlay_file(running_machine &machine);
static int instruction_hook(device_t &device, offs_t curpc);
static void execute_fdsave(running_machine &machine, int ref, int params, const char **param);
static void execute_fdoutput(running_machine &machine, int ref, int params, const char **param);
static void execute_fdseed(running_machine &machine, int ref, int params, const char **param);
static void execute_fdlockguess(running_machine &machine, int ref, int params, const char **param);
static void execute_fdeliminate(running_machine &machine, int ref, int params, const char **param);
static void execute_fdunlock(running_machine &machine, int ref, int params, const char **param);
static void execute_fdignore(running_machine &machine, int ref, int params, const char **param);
static void execute_fdundo(running_machine &machine, int ref, int params, const char **param);
static void execute_fdstatus(running_machine &machine, int ref, int params, const char **param);
static void execute_fdstate(running_machine &machine, int ref, int params, const char **param);
static void execute_fdpc(running_machine &machine, int ref, int params, const char **param);
static void execute_fdsearch(running_machine &machine, int ref, int params, const char **param);
static void execute_fddasm(running_machine &machine, int ref, int params, const char **param);
static void execute_fdcset(running_machine &machine, int ref, int params, const char **param);
static void execute_fdclist(running_machine &machine, int ref, int params, const char **param);
static void execute_fdcsearch(running_machine &machine, int ref, int params, const char **param);
static fd1094_possibility *try_all_possibilities(address_space &space, int basepc, int offset, int length, uint8_t *instrbuffer, uint8_t *keybuffer, fd1094_possibility *possdata);
static void tag_possibility(running_machine &machine, fd1094_possibility *possdata, uint8_t status);
static void perform_constrained_search(running_machine &machine);
static uint32_t find_global_key_matches(uint32_t startwith, uint16_t *output);
static int find_constraint_sequence(uint32_t global, int quick);
static int does_key_work_for_constraints(const uint16_t *base, uint8_t *key);
static uint32_t reconstruct_base_seed(int keybaseaddr, uint32_t startseed);
static void build_optable(running_machine &machine);
static int validate_ea(address_space &space, uint32_t pc, uint8_t modereg, const uint8_t *parambase, uint32_t flags);
static int validate_opcode(address_space &space, uint32_t pc, const uint8_t *opdata, int maxwords);
/***************************************************************************
INLINE FUNCTIONS
***************************************************************************/
/*-----------------------------------------------
addr_to_keyaddr - given an address,
return the address in the key that will be
used to decrypt it
-----------------------------------------------*/
static inline int addr_to_keyaddr(offs_t address)
{
/* for address xx0000-xx0006 (but only if >= 000008), use key xx2000-xx2006 */
if ((address & 0x0ffc) == 0 && address >= 4)
return (address & 0x1fff) | 0x1000;
else
return address & 0x1fff;
}
/*-----------------------------------------------
mask_for_keyaddr - given a key address,
return a mask indicating which bits should
always be 1
-----------------------------------------------*/
static inline uint8_t mask_for_keyaddr(offs_t address)
{
/* the first half of the key always has bit 0x80 set; the second half 0x40 */
/* however, the values at 0000-0003 and 1000-1003 don't follow this rule */
if ((address & 0x0ffc) == 0)
return 0x00;
else if ((address & 0x1000) == 0)
return 0x80;
else
return 0x40;
}
/*-----------------------------------------------
advance_seed - advance the PRNG seed by
the specified number of steps
-----------------------------------------------*/
static inline uint32_t advance_seed(uint32_t seed, int count)
{
/* iterate over the seed for 'count' reps */
while (count--)
{
seed = seed * 0x29;
seed += seed << 16;
}
return seed;
}
/*-----------------------------------------------
key_value_from_seed - extract the key value
from a seed and apply the given mask
-----------------------------------------------*/
static inline uint8_t key_value_from_seed(uint32_t seed, uint8_t mask)
{
/* put bits 16-21 of the seed in the low 6 bits and OR with the mask */
return ((~seed >> 16) & 0x3f) | mask;
}
/*-----------------------------------------------
generate_key_bytes - generate a sequence of
consecutive key bytes, starting with the
given seed
-----------------------------------------------*/
static inline void generate_key_bytes(uint8_t *dest, uint32_t keyoffs, uint32_t count, uint32_t seed)
{
int bytenum;
/* generate 'count' bytes of a key */
for (bytenum = 0; bytenum < count; bytenum++)
{
uint32_t keyaddr = (keyoffs + bytenum) & 0x1fff;
uint8_t mask = mask_for_keyaddr(keyaddr);
/* advance the seed first, then store the derived value */
seed = advance_seed(seed, 1);
dest[keyaddr] = key_value_from_seed(seed, mask);
}
}
/*-----------------------------------------------
get_opcode_length - return the length of
an opcode based on the opcode
-----------------------------------------------*/
static inline uint8_t get_opcode_length(uint16_t opcode)
{
/* return the length from the table */
return optable[opcode].flags >> 28;
}
/*-----------------------------------------------
set_constraint - set the values of a
constraint
-----------------------------------------------*/
static inline void set_constraint(fd1094_constraint *constraint, uint32_t pc, uint16_t state, uint16_t value, uint16_t mask)
{
constraint->pc = pc;
constraint->state = state;
constraint->value = value & mask;
constraint->mask = mask;
}
/*-----------------------------------------------
print_possibilities - print possibilities
for a given address
-----------------------------------------------*/
static inline void print_possibilities(running_machine &machine)
{
machine.debugger().console().printf("Possibilities @ %06X:\n", posslist[0].basepc);
for (int i = 0; i < posscount; i++)
machine.debugger().console().printf(" %c%2x: %s\n", posslist[i].iffy ? ' ' : '*', i, posslist[i].dasm);
}
/*-----------------------------------------------
pc_is_valid - is a given PC value valid?
0=no, 1=yes, 2=unlikely
-----------------------------------------------*/
static inline int pc_is_valid(address_space &space, uint32_t pc, uint32_t flags)
{
/* if we're odd or out of range, fail */
if ((pc & 1) == 1)
return 0;
if (pc & 0xff000000)
return 0;
if (space.direct().read_ptr(pc) == nullptr)
return 0;
return 1;
}
/*-----------------------------------------------
addr_is_valid - is a given address value
valid? 0=no, 1=yes, 2=unlikely
-----------------------------------------------*/
static inline int addr_is_valid(address_space &space, uint32_t addr, uint32_t flags)
{
/* if this a JMP, the address is a PC */
if (flags & OF_JMP)
return pc_is_valid(space, addr, flags);
/* if we're odd or out of range, fail */
if ((flags & OF_SIZEMASK) != OF_BYTE && (addr & 1) == 1)
return 0;
if ((addr & 0xff000000) != 0 && (addr & 0xff000000) != 0xff000000)
return 0;
/* if we're invalid, fail */
if (strcmp(const_cast<address_space &>(space)->get_handler_string(read_or_write::READ, addr), "segaic16_memory_mapper_lsb_r") == 0)
return 2;
return 1;
}
/***************************************************************************
CORE IMPLEMENTATION
***************************************************************************/
/*-----------------------------------------------
fd1094_init_debugging - set up debugging
-----------------------------------------------*/
void fd1094_init_debugging(running_machine &machine, const char *cpureg, const char *keyreg, const char *statreg, void (*changed)(running_machine &))
{
/* set the key changed callback */
key_changed = changed;
/* set up the regions */
coderegion = (uint16_t *)machine.root_device().memregion(cpureg)->base();
coderegion_words = machine.root_device().memregion(cpureg)->bytes() / 2;
keyregion = (uint8_t *)machine.root_device().memregion(keyreg)->base();
keystatus = (uint16_t *)machine.root_device().memregion(statreg)->base();
keystatus_words = machine.root_device().memregion(statreg)->bytes() / 2;
assert(coderegion_words == keystatus_words);
/* allocate memory for the ignore table */
ignorepc = make_unique_clear<uint8_t>(1 << 23);
/* allocate memory for the undo buffer */
undobuff = std::make_unique<uint8_t[]>(keystatus_words * 2);
memcpy(undobuff, keystatus, keystatus_words * 2);
/* allocate memory for the possible seeds array */
possible_seed = std::make_unique<uint32_t[]>(65536);
/* build the opcode table */
build_optable(machine);
/* set up default constraints */
constcount = 0;
set_constraint(&constraints[constcount++], 0x000000, FD1094_STATE_RESET, 0x0000, 0xffff);
set_constraint(&constraints[constcount++], 0x000002, FD1094_STATE_RESET, 0x0000, 0xffff);
set_constraint(&constraints[constcount++], 0x000004, FD1094_STATE_RESET, 0x0000, 0xffff);
set_constraint(&constraints[constcount++], 0x000006, FD1094_STATE_RESET, 0x0000, 0xc001);
/* determine the key parameters */
set_default_key_params(machine);
/* read the key overlay file */
load_overlay_file(machine);
/* add some commands */
using namespace std::placeholder;
machine.debugger().console().register_command("fdsave", CMDFLAG_NONE, 0, 0, 0, std::bind(&execute_fdsave, std::ref(machine), _1, _2, _3));
machine.debugger().console().register_command("fdoutput", CMDFLAG_NONE, 0, 1, 1, std::bind(&execute_fdoutput, std::ref(machine), _1, _2, _3));
machine.debugger().console().register_command("fdseed", CMDFLAG_NONE, 0, 2, 2, std::bind(&execute_fdseed, std::ref(machine), _1, _2, _3));
machine.debugger().console().register_command("fdguess", CMDFLAG_NONE, STATUS_GUESS, 1, 1, std::bind(&execute_fdlockguess, std::ref(machine), _1, _2, _3));
machine.debugger().console().register_command("fdlock", CMDFLAG_NONE, STATUS_LOCKED, 1, 1, std::bind(&execute_fdlockguess, std::ref(machine), _1, _2, _3));
machine.debugger().console().register_command("fdeliminate", CMDFLAG_NONE, 0, 1, 10, std::bind(&execute_fdeliminate, std::ref(machine), _1, _2, _3));
machine.debugger().console().register_command("fdunlock", CMDFLAG_NONE, 0, 1, 1, std::bind(&execute_fdunlock, std::ref(machine), _1, _2, _3));
machine.debugger().console().register_command("fdignore", CMDFLAG_NONE, 0, 0, 1, std::bind(&execute_fdignore, std::ref(machine), _1, _2, _3));
machine.debugger().console().register_command("fdundo", CMDFLAG_NONE, 0, 0, 0, std::bind(&execute_fdundo, std::ref(machine), _1, _2, _3));
machine.debugger().console().register_command("fdstatus", CMDFLAG_NONE, 0, 0, 0, std::bind(&execute_fdstatus, std::ref(machine), _1, _2, _3));
machine.debugger().console().register_command("fdstate", CMDFLAG_NONE, 0, 0, 1, std::bind(&execute_fdstate, std::ref(machine), _1, _2, _3));
machine.debugger().console().register_command("fdpc", CMDFLAG_NONE, 0, 0, 1, std::bind(&execute_fdpc, std::ref(machine), _1, _2, _3));
machine.debugger().console().register_command("fdsearch", CMDFLAG_NONE, 0, 0, 0, std::bind(&execute_fdsearch, std::ref(machine), _1, _2, _3));
machine.debugger().console().register_command("fddasm", CMDFLAG_NONE, 0, 1, 1, std::bind(&execute_fddasm, std::ref(machine), _1, _2, _3));
machine.debugger().console().register_command("fdcset", CMDFLAG_NONE, 0, 2, 4, std::bind(&execute_fdcset, std::ref(machine), _1, _2, _3));
machine.debugger().console().register_command("fdclist", CMDFLAG_NONE, 0, 0, 0, std::bind(&execute_fdclist, std::ref(machine), _1, _2, _3));
machine.debugger().console().register_command("fdcsearch", CMDFLAG_NONE, 0, 0, 0, std::bind(&execute_fdcsearch, std::ref(machine), _1, _2, _3));
/* set up the instruction hook */
machine.device("maincpu")->debug()->set_instruction_hook(instruction_hook);
/* regenerate the key */
if (keydirty)
fd1094_regenerate_key(machine);
}
/*-----------------------------------------------
set_default_key_params - based on the game
name, set some defaults
-----------------------------------------------*/
static void set_default_key_params(running_machine &machine)
{
static const struct
{
const char * gamename;
uint32_t global;
uint32_t seed;
} default_keys[] =
{
{ "altbeastj1", 0xFCAFF9F9, 0x177AC6 },
{ "bullet", 0x12A8F9EC, 0x1B1FC3 },
};
int keynum;
/* look for a matching game and set the key appropriately */
for (keynum = 0; keynum < std::size(default_keys); keynum++)
if (strcmp(machine.system().name, default_keys[keynum].gamename) == 0)
{
fd1094_global = default_keys[keynum].global;
fd1094_seed = default_keys[keynum].seed;
keydirty = true;
break;
}
}
/*-----------------------------------------------
load_overlay_file - load the key overlay
file
-----------------------------------------------*/
static void load_overlay_file(running_machine &machine)
{
int pcaddr;
/* determine the filename and open the file */
emu_file file(OPEN_FLAG_READ);
osd_file::error filerr = file.open(machine.system().name, ".kov");
if (filerr == osd_file::error::NONE)
{
file.read(keystatus, keystatus_words * 2);
/* convert from big-endian */
for (pcaddr = 0; pcaddr < keystatus_words; pcaddr++)
keystatus[pcaddr] = big_endianize_int16(keystatus[pcaddr]) & ~SEARCH_MASK;
}
/* mark the key dirty */
keydirty = true;
}
/*-----------------------------------------------
save_overlay_file - save the key overlay
file
-----------------------------------------------*/
static void save_overlay_file(running_machine &machine)
{
int pcaddr;
/* determin the filename and open the file */
emu_file file(OPEN_FLAG_WRITE | OPEN_FLAG_CREATE);
osd_file::error filerr = file.open(machine.system().name, ".kov");
if (filerr == osd_file::error::NONE)
{
/* convert to big-endian */
for (pcaddr = 0; pcaddr < keystatus_words; pcaddr++)
keystatus[pcaddr] = big_endianize_int16(keystatus[pcaddr]);
/* write the data */
file.write(keystatus, keystatus_words * 2);
/* convert from big-endian */
for (pcaddr = 0; pcaddr < keystatus_words; pcaddr++)
keystatus[pcaddr] = big_endianize_int16(keystatus[pcaddr]);
}
}
/*-----------------------------------------------
fd1094_regenerate_key - regenerate the key
based on the raw parameters and the overlay
data
-----------------------------------------------*/
void fd1094_regenerate_key(running_machine &machine)
{
int reps = keystatus_words / KEY_SIZE;
int keyaddr, repnum;
/* store the global key in the first 4 bytes */
keyregion[0] = fd1094_global >> 24;
keyregion[1] = fd1094_global >> 16;
keyregion[2] = fd1094_global >> 8;
keyregion[3] = fd1094_global >> 0;
/* then generate the remaining 8188 bytes */
generate_key_bytes(keyregion, 4, 8192 - 4, fd1094_seed);
/* apply the overlay */
for (keyaddr = 4; keyaddr < KEY_SIZE; keyaddr++)
{
keyregion[keyaddr] |= keystatus[keyaddr] & HIBITS_MASK;
/* if we're locked, propogate that info to all our reps */
if ((keystatus[keyaddr] & STATUS_MASK) == STATUS_LOCKED)
for (repnum = 1; repnum < reps; repnum++)
{
keystatus[repnum * KEY_SIZE + keyaddr] = (keystatus[repnum * KEY_SIZE + keyaddr] & ~STATUS_MASK) | STATUS_LOCKED;
if ((keyaddr & 0x1ffc) == 0x1000)
keystatus[repnum * KEY_SIZE + keyaddr - 0x1000] = (keystatus[repnum * KEY_SIZE + keyaddr - 0x1000] & ~STATUS_MASK) | STATUS_LOCKED;
}
}
/* update the key with the current fd1094 manager */
if (key_changed != nullptr)
(*key_changed)(machine);
/* force all memory and disassembly views to update */
machine.debug_view().update_all(DVT_MEMORY);
machine.debug_view().update_all(DVT_DISASSEMBLY);
/* reset keydirty */
keydirty = false;
}
/*-----------------------------------------------
instruction_hook - per-instruction hook
-----------------------------------------------*/
static int instruction_hook(device_t &device, offs_t curpc)
{
int curfdstate = fd1094_set_state(keyregion, -1);
uint8_t instrbuffer[10], keybuffer[5];
int i, keystat;
/* quick exit if we're ignoring */
if (ignore_all || ignorepc[curpc/2])
return 0;
/* quick exit if we're already locked */
keystat = keystatus[curpc/2] & STATUS_MASK;
keystatus[curpc/2] = (keystatus[curpc/2] & ~STATE_MASK) | (curfdstate << 8);
if (keystat == STATUS_LOCKED || keystat == STATUS_NOCHANGE)
{
uint16_t opcode = fd1094_decode(curpc/2, coderegion[curpc/2], keyregion, 0);
int length = get_opcode_length(opcode);
for (i = 1; i < length; i++)
{
keystat = keystatus[curpc/2 + i] & STATUS_MASK;
if (keystat != STATUS_LOCKED && keystat != STATUS_NOCHANGE)
break;
}
if (i == length)
{
for (i = 1; i < length; i++)
keystatus[curpc/2 + i] = (keystatus[curpc/2 + i] & ~STATE_MASK) | (curfdstate << 8);
return 0;
}
}
/* try all possible decodings at the current pc */
posscount = try_all_possibilities(device.memory().space(AS_PROGRAM), curpc, 0, 0, instrbuffer, keybuffer, posslist) - posslist;
if (keydirty)
fd1094_regenerate_key(device.machine());
/* if we only ended up with one possibility, mark that one as good */
if (posscount == 1)
{
tag_possibility(device.machine(), &posslist[0], STATUS_LOCKED);
fd1094_regenerate_key(device.machine());
return 0;
}
/* print possibilities and break */
print_possibilities(device.machine());
return 1;
}
/*-----------------------------------------------
execute_fdsave - handle the 'fdsave' command
-----------------------------------------------*/
static void execute_fdsave(running_machine &machine, int ref, int params, const char **param)
{
save_overlay_file(machine);
machine.debugger().console().printf("File saved\n");
}
/*-----------------------------------------------
execute_fdoutput - output the current key
to a file
-----------------------------------------------*/
static void execute_fdoutput(running_machine &machine, int ref, int params, const char **param)
{
/* make sure we're up-to-date */
if (keydirty)
fd1094_regenerate_key(machine);
/* determin the filename and open the file */
emu_file file(OPEN_FLAG_WRITE | OPEN_FLAG_CREATE);
osd_file::error filerr = file.open(param[0]);
if (filerr == osd_file::error::NONE)
file.write(keyregion, KEY_SIZE);
machine.debugger().console().printf("File '%s' saved\n", param[0]);
}
/*-----------------------------------------------
execute_fdseed - handle the 'fdseed' command
-----------------------------------------------*/
static void execute_fdseed(running_machine &machine, int ref, int params, const char **param)
{
uint64_t num1, num2;
/* extract the parameters */
if (!machine.debugger().commands().validate_number_parameter(param[0], &num1))
return;
if (!machine.debugger().commands().validate_number_parameter(param[1], &num2))
return;
/* set the global and seed, and then regenerate the key */
fd1094_global = num1;
fd1094_seed = num2;
/* clear out our buffer */
memset(keystatus, 0, keystatus_words * sizeof(keystatus[0]));
/* regenerate the key and reset the 68000 */
fd1094_regenerate_key(machine);
}
/*-----------------------------------------------
execute_fdlockguess - handle the 'fdlock'
and 'fdguess' commands
-----------------------------------------------*/
static void execute_fdlockguess(running_machine &machine, int ref, int params, const char **param)
{
uint64_t num1;
/* extract the parameter */
if (!machine.debugger().commands().validate_number_parameter(param[0], &num1))
return;
/* make sure it is within range of our recent possibilities */
if (num1 >= posscount)
{
machine.debugger().console().printf("Possibility of out range (%x max)\n", posscount);
return;
}
/* create an undo buffer */
memcpy(undobuff, keystatus, keystatus_words * 2);
/* tag this possibility as indicated by the ref parameter, and then regenerate the key */
tag_possibility(machine, &posslist[num1], ref);
fd1094_regenerate_key(machine);
}
/*-----------------------------------------------
execute_fdeliminate - handle the
'fdeliminate' command
-----------------------------------------------*/
static void execute_fdeliminate(running_machine &machine, int ref, int params, const char **param)
{
int pnum, posssrc, possdst;
int plist[10];
/* extract parameters */
for (pnum = 0; pnum < params; pnum++)
{
uint64_t num1;
/* extract the parameters */
if (!machine.debugger().commands().validate_number_parameter(param[pnum], &num1))
return;
/* make sure it is within range of our recent possibilities */
if (num1 >= posscount)
{
machine.debugger().console().printf("Possibility %x of out range (%x max)\n", (int)num1, posscount);
return;
}
/* set the entry */
plist[pnum] = num1;
}
/* loop over parameters */
for (posssrc = possdst = 0; posssrc < posscount; posssrc++)
{
/* is the current pnum in our list to delete? */
for (pnum = 0; pnum < params; pnum++)
if (plist[pnum] == posssrc)
break;
/* if not, copy to the dest */
if (pnum == params)
posslist[possdst++] = posslist[posssrc];
}
/* set the final count */
posscount = possdst;
/* reprint the possibilities */
print_possibilities(machine);
}
/*-----------------------------------------------
execute_fdunlock - handle the 'fdunlock'
command
-----------------------------------------------*/
static void execute_fdunlock(running_machine &machine, int ref, int params, const char **param)
{
device_t *cpu = machine.debugger().console().get_visible_cpu();
/* support 0 or 1 parameters */
uint64_t offset;
if (params != 1 || !machine.debugger().commands().validate_number_parameter(param[0], &offset))
offset = cpu->state().pc();
int keyaddr = addr_to_keyaddr(offset / 2);
/* toggle the ignore PC status */
machine.debugger().console().printf("Unlocking PC %06X\n", (int)offset);
/* iterate over all reps and unlock them */
const int reps = keystatus_words / KEY_SIZE;
for (int repnum = 0; repnum < reps; repnum++)
{
uint16_t *dest = &keystatus[repnum * KEY_SIZE + keyaddr];
if ((*dest & STATUS_MASK) == STATUS_LOCKED)
*dest &= ~STATUS_MASK & ~HIBITS_MASK;
/* unlock the duplicate key bytes as well */
if ((keyaddr & 0x1ffc) == 0x1000)
{
dest = &keystatus[repnum * KEY_SIZE + keyaddr - 0x1000];
if ((*dest & STATUS_MASK) == STATUS_LOCKED)
*dest &= ~STATUS_MASK & ~HIBITS_MASK;
}
}
}
/*-----------------------------------------------
execute_fdignore - handle the 'fdignore'
command
-----------------------------------------------*/
static void execute_fdignore(running_machine &machine, int ref, int params, const char **param)
{
device_t *cpu = machine.debugger().console().get_visible_cpu();
/* support 0 or 1 parameters */
if (params == 1 && strcmp(param[0], "all") == 0)
{
ignore_all = true;
machine.debugger().console().printf("Ignoring all unknown opcodes\n");
return;
}
uint64_t offset;
if (params != 1 || !machine.debugger().commands().validate_number_parameter(param[0], &offset))
offset = cpu->state().pc();
offset /= 2;
/* toggle the ignore PC status */
ignorepc[offset] = !ignorepc[offset];
if (ignorepc[offset])
machine.debugger().console().printf("Ignoring address %06X\n", (int)offset * 2);
else
machine.debugger().console().printf("No longer ignoring address %06X\n", (int)offset * 2);
/* if no parameter given, implicitly run as well */
if (params == 0)
machine.debugger().console().get_visible_cpu()->debug()->go();
}
/*-----------------------------------------------
execute_fdundo - handle the 'fdundo'
command
-----------------------------------------------*/
static void execute_fdundo(running_machine &machine, int ref, int params, const char **param)
{
/* copy the undobuffer back and regenerate the key */
memcpy(keystatus, undobuff, keystatus_words * 2);
fd1094_regenerate_key(machine);
machine.debugger().console().printf("Undid last change\n");
}
/*-----------------------------------------------
execute_fdstatus - handle the 'fdstatus'
command
-----------------------------------------------*/
static void execute_fdstatus(running_machine &machine, int ref, int params, const char **param)
{
int numreps = keystatus_words / KEY_SIZE;
int locked = 4, nomatter = 0, guesses = 0;
int keyaddr;
/* count how many locked keys we have */
for (keyaddr = 4; keyaddr < KEY_SIZE; keyaddr++)
{
int count[STATUS_MASK + 1] = { 0 };
int repnum;
for (repnum = 0; repnum < numreps; repnum++)
count[keystatus[repnum * KEY_SIZE + keyaddr] & STATUS_MASK]++;
if (count[STATUS_LOCKED] > 0)
locked++;
else if (count[STATUS_GUESS] > 0)
guesses++;