-
Notifications
You must be signed in to change notification settings - Fork 3
/
list-rooms.cpp
3304 lines (3183 loc) · 147 KB
/
list-rooms.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
/*
SoEScriptDumper - makes SoE scripts human-readable
Copyright (C) 2020 black-sliver, neagix
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
Yes, this is a strange mix of C++ and C.
It started out as a quick mostly-C-hack and is now a complete mess.
Compile with g++/cpp.
Opposed to darkmoon's output this lists all triggers for maps instead of
annotating the ROM byte by byte.
Also it tries to parse sub-instructions as part of the instructions in some
instances to add functionality or generate more readable output.
Use AUTO_DISCOVER_SCRIPTS define to get all of the scripts parsed,
or add points of interest by hand to ..scripts lists.
**/
//TODO: add readable addr/flag names to sub-instr parser
//TODO: generate useful names for auto-discovered scripts
//TODO: inline RCALLS with args (INSTR af..b4)? (re)trace af and b4
//TODO: inline (unique) 24bit CALLS?
//TODO: trace sub-instr 00 to be sure what it would do
//TODO: remove duplicate (partial) scripts from absscripts
//TODO: don't print absscripts if already inlined
//TODO: differentiate between parallel "CALLS" and actual CALLS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <list>
#include <algorithm> // std::min_element, std::max_element, std::find
#include <map>
#include <stack>
#include <assert.h>
#include <string>
#if defined(WIN32) || defined(_WIN32)
#include <process.h>
#endif
#if !defined TEXT && !defined HTML4 && !defined HTML5
#ifdef __EMSCRIPTEN__
#define HTML5 // default to HTML5 output for emscripten
#else
#define TEXT // default to (ansi colored) TEXT output on desktop
#endif
#endif
#if defined TEXT && (defined HTML4 || defined HTML5)
#error "Can only define (ansi) TEXT OR HTML output"
#endif
// define/undefine to list certain script types or other things
#define SHOW_ENTER_SCRIPTS
#define SHOW_STEP_ON_SCRIPTS
#define SHOW_B_TRIGGER_SCRIPTS
#define SHOW_ABS_ADDR
// NOTE: sniff, gourds and doggo is untested in latest version
//#define PRINT_ALL_SNIFF_SPOTS // requires SHOW_B_TRIGGER_SCRIPTS
//#define DUMP_ALL_SNIFF_SPOTS // generate sniff.h for evermizer
//#define DUMP_ALL_SNIFF_SPOTS_JSON // generate sniff.json for cyb3r
//#define DUMP_ALL_SNIFF_FLAGS // generate sniffflags.inc for list-rooms
//#define PRINT_ALL_GOURDS // requires SHOW_B_TRIGGER_SCRIPTS
//#define DUMP_ALL_GOURDS // this is not implemented because act3 gourds suck
//#define DUMP_ALL_GOURD_FLAGS // as above
//#define DUMP_DOGGO_CHANGE // generate doggo.h for evermizer
//#define PRINT_ALL_TEXTS // try all indices for Texts
//#define TEXT_SHOW_DICT_USE
//#define PRINT_ALL_SCRIPTS // try all indices for NPC scripts
//#define NO_STATS // skip stats at the end
#define EXTENSIVE_ROOM_STATS // moar stats at the end
//#define FIND_EASTER_EGG // enable some code I used to find the easter egg
//#define PRINT_HEX // add hex dump to output
#define INLINE_RCALLS // prints out RCALLED code as part of script
#define AUTO_DISCOVER_SCRIPTS
//#define SHOW_UNUSED_SCRIPT_IDS
//#define HYPERLINKS
// test compiler feature
#ifndef __has_include
#pragma message "you may have to create an empty sniffflags.inc if missing"
#endif
// output formatting
#if defined TEXT
#define START ""
#define HEADING "\033[0;93m"
#define HEADING_TEXT "\033[1;93m"
#define HEADING_END HEADING
#define GREEN "\033[92m"
#define RED "\033[91m" // this means we don't know the length. breaks out of parsing
#define UNTRACED "\033[95m" // this means we know the length, but not what it does
#define NORMAL "\033[0m"
#define END ""
#define HEX_START "\t"
#define HEX_END ""
#define LT "<"
#define GT ">"
#define LE "<="
#define GE ">="
#elif defined HTML4
#define START "<font>"// color=\"#000\">"
#define HEADING "</font><font color=\"#770\">"
#ifdef NO_BOLD
#define HEADING_TEXT
#define HEADING_END
#else
#define HEADING_TEXT "</font><font color=\"#770\"><b>"
#define HEADING_END "</b>" HEADING
#endif
#define GREEN "</font><font color=\"#0f0\">"
#define RED "</font><font color=\"#f00\">"
#define UNTRACED "</font><font color=\"#707\">"
#define NORMAL "</font><font>"// color=\"#000\">"
#define END "</font>"
#define LT "<"
#define GT ">"
#define LE "<="
#define GE ">="
#define HEX_START "<i>"
#define HEX_END "</i>"
#else // HTML5
#define START "<span class=\"n\">"
#define HEADING "</span><span class=\"h\">"
#define HEADING_TEXT "</span><span class=\"t\">"
#define HEADING_END HEADING
#define GREEN "</span><span class=\"g\">"
#define RED "</span><span class=\"r\">"
#define UNTRACED "</span><span class=\"u\">"
#define NORMAL "</span><span class=\"n\">"
#define END "</span>"
#define LT "<"
#define GT ">"
#define LE "<="
#define GE ">="
#define HEX_START "<span class=\"hex\">"
#define HEX_END "</span>"
#endif
// some utility defines
#ifndef ABS
#define ABS(a) ( ((a)<0) ? (-1*(a)) : (a) )
#endif
#ifdef SHOW_ABS_ADDR
// absolute addresses
#define _ADDR (scriptstart+instroff)
#define _ADDRFMT "0x%06x"
#define _DST (unsigned)(scriptstart+dst)
#define _DSTFMT "0x%06x"
#else
// relative addresses
#define _ADDR instroff
#define _ADDRFMT "+x%02x"
#define _DST (unsigned)(dst)
#define _DSTFMT "+x%02x"
#endif
#if defined(HYPERLINKS) && (defined(HTML4) || defined(HTML5))
// anchors are always in absolute address format
#define ADDRFMT "<span id=\"addr_0x%06x\">" _ADDRFMT "</span>"
#define ADDR ((unsigned)(scriptstart+instroff)),(_ADDR)
#define DSTFMT "(to <a href=\"#addr_0x%06x\">" _DSTFMT "</a>)"
#define DST ((unsigned)(scriptstart+dst)),(_DST)
#else
#define ADDRFMT _ADDRFMT
#define DSTFMT "(to " _DSTFMT ")"
#define DST _DST
#define ADDR _ADDR
#endif
static bool batch = false;
#ifndef die
static void die(const char* msg)
{
fprintf(stderr, "%s", msg);
#if (defined(WIN32) || defined(_WIN32)) && !defined(main)
if (!batch) system("pause");
#endif
exit(1);
}
#endif
#ifdef AUTO_DISCOVER_SCRIPTS
static std::list<char*> strings; // buffer for generated strings
// NOTE: unique_ptr syntax is too ugly for me to handle
#endif
#include "data.h"
uint32_t map_list_addr = MAP_LIST_ADDR_US;
uint32_t scripts_start_addr = SCRIPTS_START_ADDR_US;
#ifdef SHOW_UNUSED_SCRIPT_IDS
static std::list<uint16_t> used_globalscripts;
static std::list<uint16_t> used_npcscripts;
#endif
static const char* get_map_name(uint8_t map_id, bool include_act = true)
{
const char* s = nullptr;
for (auto pair: maps) {
if (pair.first == map_id) {
s = pair.second;
if (include_act)
break;
const char* tmp = strstr(s, "- ");
if (tmp)
s = tmp + 2;
break;
}
}
return s;
}
enum class IntType : uint8_t { none=0, word, byte, subinstr1B, subinstr2B, subinstr3B };
struct DoggoData;
struct DoggoData {
IntType inttype=IntType::none;
uint16_t val=0;
uint8_t mapid=0;
DoggoData() {}
DoggoData(IntType i, uint16_t v, uint8_t m) : inttype(i) , val(v) , mapid(m) {}
};
struct LootData;
struct LootData {
enum class DataSet : uint8_t { none=0, item=1, amount=2, extra=4, check=8, set=16, callSniff=32, callGourd=64 };
DataSet dataset=DataSet::none;
uint8_t __padding=0;
uint16_t amount=0;
uint16_t item=0;
uint16_t extra=0;
uint32_t amount_pos=0;
uint32_t item_pos=0;
uint32_t extra_pos=0;
IntType amount_type=IntType::none;
IntType item_type=IntType::none;
IntType extra_type=IntType::none;
std::pair<uint16_t, uint8_t> check_flag={0,0};
std::pair<uint16_t, uint8_t> set_flag={0,0};
uint32_t item_instr_pos=0;
uint8_t item_instr_len=0;
uint8_t mapid=0;
uint16_t scriptid=0;
uint8_t x1=0;
uint8_t y1=0;
uint8_t x2=0;
uint8_t y2=0;
uint16_t mapref=0;
LootData(){}
LootData(uint8_t mapid, uint16_t scriptid, uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2) {
this->scriptid=scriptid; this->mapid=mapid; this->x1=x1; this->y1=y1; this->x2=x2; this->y2=y2;
}
std::string to_string() const;
std::string to_flag() const;
std::string to_h() const;
inline bool same_source(const LootData& other) const {
return ( amount_pos==other.amount_pos &&
item_pos==other.item_pos &&
extra_pos==other.extra_pos );
}
};
inline LootData::DataSet operator| (LootData::DataSet lhs, LootData::DataSet rhs) {
using T = std::underlying_type <LootData::DataSet>::type;
// using T = std::underlying_type_t <LootData::DataSet>; // c++14
return static_cast<LootData::DataSet>(static_cast<T>(lhs) | static_cast<T>(rhs));
};
inline LootData::DataSet& operator|= (LootData::DataSet& lhs, LootData::DataSet rhs) { lhs = lhs | rhs; return lhs; };
inline LootData::DataSet operator& (const LootData::DataSet lhs, LootData::DataSet rhs) {
using T = std::underlying_type <LootData::DataSet>::type;
// using T = std::underlying_type_t <LootData::DataSet>; // c++14
return static_cast<LootData::DataSet>(static_cast<T>(lhs) & static_cast<T>(rhs));
};
inline LootData::DataSet& operator&= (LootData::DataSet& lhs, LootData::DataSet rhs) { lhs = lhs & rhs; return lhs; };
inline bool operator! (LootData::DataSet e) { return e==LootData::DataSet::none; }
const char* to_string(IntType t) {
using T=IntType;
switch (t) {
case T::byte: return "Byte";
case T::word: return "Word";
case T::subinstr1B: return "Sub1B";
case T::subinstr2B: return "Sub2B";
case T::subinstr3B: return "Sub3B";
case T::none:
default: return "-";
}
}
std::string LootData::to_string() const {
char buf[256];
if (!!(dataset & LootData::DataSet::amount))
snprintf(buf, sizeof(buf), "map 0x%02x:%04x@%02x,%02x:%02x,%02x: %3ux 0x%04x (%-5s) @$%06x, next+%u, check $%04x&0x%02x, set $%04x&0x%02x",
mapid, scriptid, x1, y1, x2, y2, amount, item, ::to_string(item_type), item_pos, extra,
check_flag.first, (1<<check_flag.second),
set_flag.first, (1<<set_flag.second));
else
snprintf(buf, sizeof(buf), "map 0x%02x:%04x@%02x,%02x:%02x,%02x: 0x%04x (%-5s) @$%06x, next+%u, check $%04x&0x%02x, set $%04x&0x%02x",
mapid, scriptid, x1, y1, x2, y2, item, ::to_string(item_type), item_pos, extra,
check_flag.first, (1<<check_flag.second),
set_flag.first, (1<<set_flag.second));
return std::string(buf);
}
std::string LootData::to_flag() const {
char buf[256];
char sitem[32]; snprintf(sitem, sizeof(sitem), "0x%04x ", item);
char umap[16]; snprintf(umap, sizeof(umap), "MAP 0x%02x", mapid);
const char* smap = get_map_name(mapid, false);
if (!smap)
smap = umap;
const auto& prizeit = prizes.find(item);
if (prizeit != prizes.end()) {
strncpy(sitem, prizeit->second, sizeof(sitem));
sitem[sizeof(sitem)-1] = 0;
char* p = strchr(sitem, '(');
if (p) *p = 0;
}
snprintf(buf, sizeof(buf), "{{0x%04x, bm2bp(0x%02x)}, \"Sniffed %sin %s (#%u)\"},",
check_flag.first, (1<<check_flag.second), sitem, smap, (unsigned)mapref);
return std::string(buf);
}
std::string LootData::to_h() const {
// NOTE: since all sniff values are 16bit, we only dump addr+value
char buf[64];
snprintf(buf, sizeof(buf), "{0x%06x, 0x%04x},", item_pos&~0xc00000, item);
return std::string(buf);
}
static inline bool addr_valid(uint32_t addr, size_t len)
{
return ((addr&~(0xc00000)) < len);
}
static uint8_t read_buf8(const uint8_t* buf, uint32_t addr, size_t len)
{
addr &= ~(0xc00000);
if (addr >= len) {
fprintf(stderr, "** Illegal address 0x%x read **\n", (unsigned)addr);
assert(false);
return 0;
}
return buf[addr];
}
static uint16_t read_buf16(const uint8_t* buf, uint32_t addr, size_t len)
{
addr &= ~(0xc00000);
if (addr+1 >= len) {
fprintf(stderr, "** Illegal address 0x%x read **\n", (unsigned)addr);
assert(false);
return 0;
}
uint16_t res = 0;
res |= buf[addr+1]; res<<=8;
res |= buf[addr];
return res;
}
static uint32_t read_buf24(const uint8_t* buf, uint32_t addr, size_t len)
{
addr &= ~(0xc00000);
if (addr+2 >= len) {
fprintf(stderr, "** Illegal address 0x%x read **\n", (unsigned)addr);
assert(false);
return 0;
}
uint32_t res = 0;
res |= buf[addr+2]; res<<=8;
res |= buf[addr+1]; res<<=8;
res |= buf[addr];
return res;
}
#define addr_valid(addr) addr_valid(addr, len)
#define read8(addr) read_buf8 (buf, addr, len)
#define read16(addr) read_buf16(buf, addr, len)
#define read24(addr) read_buf24(buf, addr, len)
static inline uint32_t rom2scriptaddr(uint32_t romaddr)
{
romaddr &= ~(0x8000);
romaddr -= (scripts_start_addr & ~(0x8000));
return (romaddr&0x007fff) + ((romaddr&0x1ff0000)>>1);
}
static inline uint32_t script2romaddr(uint32_t scriptaddr)
{
return scripts_start_addr + (scriptaddr&0x007fff) + ((scriptaddr&0xff8000)<<1);
}
static bool sub1bIsVal(uint8_t subinstr)
{
uint8_t cmd = subinstr&0x70;
return (cmd==0x30 || cmd==0x40 || cmd==0x60);
}
static bool sub1bIsFinalVal(uint8_t subinstr)
{
return ((subinstr&0x80) && sub1bIsVal(subinstr));
}
static uint16_t sub1b2val(uint8_t subinstr)
{
uint8_t cmd = subinstr&0x70;
if (cmd == 0x30) return subinstr&0x0f;
if (cmd == 0x40) return 0xfff0|(subinstr&0x0f);
if (cmd == 0x60) return 0x10 + (subinstr&0x0f);
fprintf(stderr, "WARN: Sub-instr %02x is not a value!\n", subinstr);
return 0;
}
static int16_t sub1b2sval(uint8_t subinstr)
{
return (int16_t)sub1b2val(subinstr);
}
static std::string sub2currency(const std::string& subresult)
{
std::string res;
char* next = nullptr;
long curval = strtol(subresult.c_str(), &next, 0);
bool isint = (next && !*next);
if (isint && curval == 0x00) res = "Talons";
else if (isint && curval == 0x03) res = "Jewels";
else if (isint && curval == 0x06) res = "Gold Coins";
else if (isint && curval == 0x09) res = "Credits";
else res = "Currency "+subresult;
return res;
}
static bool subinstrIsEntityOnly(uint8_t subinstr)
{
switch (subinstr) { // NOTE: this excludes calculations
case 0xd0: // boy
case 0xd1: // dog
case 0xd2: // controlled char
case 0xd3: // non-controlled char
case 0xad: // last entity ($0341)
case 0xae: // entity attached to script?
return true;
default:
return false;
}
}
static bool subinstrIsEntity(uint8_t subinstr)
{
return subinstrIsEntityOnly(subinstr|0x80);
}
static const char* subinstr2name(uint8_t subinstr)
{
switch (subinstr&0x7f) {
case 0x50:
return "boy";
case 0x51:
return "dog";
case 0x52:
return "controlled char";
case 0x53:
return "non-controlled char";
case 0x2d:
return "last entity ($0341)";
case 0x2e:
return "entity attached to script?";
default:
return "???";
}
}
static const char* absscript2name(uint32_t addr)
{
for (const auto& pair: absscripts) {
if (pair.first == addr)
return pair.second;
}
return "Unknown";
}
static const char* npcscript2name(uint16_t id)
{
for (const auto& pair: npcscripts) {
if (pair.first == id)
return pair.second;
}
return "Unknown";
}
static const char* globalscript2name(uint8_t id, const char* def="Unknown")
{
for (const auto& pair: globalscripts) {
if (pair.first == id) {
return pair.second;
}
}
return def;
}
static std::string ramaddr2str(uint16_t addr)
{
auto it = ram.find(addr);
if (it != ram.end()) return it->second;
char buf[6];
snprintf(buf, 6, "$%04x", addr);
return std::string(buf);
}
static std::string rambit2str(uint16_t addr, uint8_t bp)
{
// bp2bm: 0:0x01, 1:0x02, 2:0x04, 3:0x08, 4:0x10, 5:0x20, 6:0x40, 7:0x80
auto it = flags.find(std::make_pair(addr,bp));
if (it != flags.end())
return std::string("(") + it->second + ") ";
return "";
}
static std::string hex(uint32_t val)
{
char buf[64]; snprintf(buf, sizeof(buf), "%02x", (unsigned)val);
return std::string(buf);
}
static std::string hex6(uint32_t val)
{
char buf[64]; snprintf(buf, sizeof(buf), "%06x", (unsigned)val);
return std::string(buf);
}
static std::string buf_get_text(const uint8_t* buf, uint32_t addr, uint32_t len, const char* spaces1="", const char* spaces2="")
{
std::string data = "";
std::string info = "";
std::string linebreak = std::string("\"\n") + spaces1 + spaces2 + "\"";
addr = read24(addr);
bool mode = (addr & 0x800000);
addr = 0xc00000 + (addr & 0x7fff) + ((addr & 0x7f8000)<<1);
if (addr>0xcfffff) return "";
#if 1
//out += mode?"1,":"0,";
info += hex6(addr);
info += GT " ";
#endif
if (mode) {
uint8_t next_plain = 0;
do {
uint8_t d = read8(addr);
if (next_plain) {
next_plain--;
if (!d && !next_plain) break;
data += (char)d;
} else if (d==0xc0) {
addr++;
#ifdef TEXT_SHOW_DICT_USE
data += '(';
data += hex(read8(addr));
data += ')';
#endif
uint32_t wordpp = 0x91f46c + ((uint16_t)read8(addr)<<1);
uint32_t wordp = 0x91f7d5 + read16(wordpp);
for (char c; (c=read8(wordp)); wordp++) {
data += c;
}
} else if ((d&0xc0)==0xc0) {
#ifdef TEXT_SHOW_DICT_USE
data += '(';
data += hex(d);
data += ')';
#endif
d = (d<<1)&0x7e;
uint32_t wordpp = 0x91f3ec + d;
uint32_t wordp = 0x91f66c + read16(wordpp);
for (char c; (c=read8(wordp)); wordp++) {
data += c;
}
} else if ((d&0xc0)==0x40) {
next_plain = d&0x3f;
} else if ((d&0xc0)==0x80) {
d<<=1;
char c;
c = (char)read8(0x91f32e + d);
data += c;
c = (char)read8(0x91f32f + d);
if (!c) break;
data += c;
} else if ((d&0xc0)==0) {
char c = (char)read8(0x91f3ae + d);
if (!c) break;
data += c;
}
addr++;
} while (true);
} else {
for (char c; (c=read8(addr)); addr++) {
data += c;
}
}
// make raw data look nice
std::string printable = "\"";
char c1=0, c2=0;
size_t n=0, m=0;
bool inPause=false;
for (auto c: data) {
if (m>0 && ((uint8_t)c == 0x96 || (uint8_t)c == 0x97)) {
printable += "\"\n\"";
n=0; m=0;
}
if ((uint8_t)c==0x80 && (uint8_t)c2==0x80 && inPause) {
// c1 = duration?
if (c1>=0x20 && c1<0x7f)
printable.erase(printable.length()-7, 7);
else
printable.erase(printable.length()-12, 12);
#if 1
char buf[3]; snprintf(buf, sizeof(buf), "%02x", (uint8_t)c1);
printable+="[PAUSE:" + std::string(buf) + "]";
m += 6+3;
#else
printable+="[PAUSE]";
m += 6;
#endif
}
else if (c>=0x20 && c<0x7f) printable+=c;
else if (c=='\n') { printable+="[LF]"; m+=3; }
else { printable += "[0x" + hex((uint8_t)c) + "]"; m+=5; }
if ((uint8_t)c==0x80) inPause=!inPause;
n++; m++;
if (((c==' ' || (!inPause && (uint8_t)c==0x80)) && (n>=80 || m>=80)) || c=='\n') {
printable += linebreak;
n=0; m=0;
}
c2 = c1;
c1 = c;
}
printable += '"';
return info+printable;
}
#define get_text(addr) buf_get_text(buf, addr, len)
#define get_text_i(addr, spaces1, spaces2) buf_get_text(buf, addr, len, spaces1, spaces2)
static std::string u16addr2str(uint16_t n)
{
char buf[6];
snprintf(buf, sizeof(buf), "$%04x", n);
return buf;
}
static std::string u8val2str(uint8_t n)
{
char buf[5];
snprintf(buf, sizeof(buf), "0x%02x", n);
return buf;
}
static std::string u16val2str(uint16_t n)
{
char buf[7];
snprintf(buf, sizeof(buf), "0x%04x", n);
return buf;
}
static std::string u24val2str(uint32_t n)
{
char buf[11];
snprintf(buf, sizeof(buf), "0x%06x", n);
return buf;
}
static std::string buf_parse_sub(const uint8_t* buf, uint32_t& addr, size_t len, bool* pok=nullptr, int* pexprlen=nullptr)
{
bool ok = true;
bool done = false;
std::string res;
uint8_t instr = 0;
int exprlen = 0;
static std::stack< std::pair<int,std::string> > stack; // yes, static...
do {
instr = read8(addr++);
done = instr&0x80;
if (subinstrIsEntity(instr)) {
exprlen++;
if (!res.empty()) res += " ";
res += subinstr2name(instr);
} else if (sub1bIsVal(instr)) {
exprlen++;
if (!res.empty()) res += " ";
res += std::to_string((int)sub1b2sval(instr)); // signed decimal for now
} else {
instr &= 0x7f;
switch(instr) {
case 0x00: // noop?
break;
case 0x01: // signed const byte
case 0x02: // unsigned const byte
{
bool issigned = (instr==0x01);
exprlen++;
if (!res.empty()) res += " ";
res += u8val2str(read8(addr++));
if (issigned) res += " signed";
break;
}
case 0x03: // signed const word
case 0x04: // unsigned const word
{
bool issigned = (instr==0x03);
exprlen++;
if (!res.empty()) res += " ";
res += u16val2str(read16(addr)); addr+=2;
if (issigned) res += " signed";
break;
}
case 0x05: // test bit
case 0x0a: // test temp bit
{
uint16_t baseaddr = (instr>=0x0a) ? 0x2834 : 0x2258;
uint16_t a = read16(addr)>>3;
uint8_t b = read16(addr)&0x07;
addr+=2;
res += u16addr2str(baseaddr+a) + "&" + u8val2str(1<<b); // TODO: named bits
exprlen=2;
break;
}
case 0x06: // read byte, signed
case 0x07: // read byte, unsigned
case 0x08: // read word, signed
case 0x09: // read word, unsigned
case 0x0b: // read temp byte, signed
case 0x0c: // read temp byte, unsigned
case 0x0d: // read temp word, signed
case 0x0e: // read temp word, unsigned
{
bool isbyte = (instr==0x06 || instr==0x07 || instr==0x0b || instr==0x0c);
//bool issigned = (instr==0x06 || instr==0x08 || instr==0x0b || instr==0x0d);
uint16_t baseaddr = (instr>=0x0a) ? 0x2834 : 0x2258;
exprlen++;
if (!res.empty()) res += " ";
res = u16addr2str(baseaddr+read16(addr)); // TODO: named vars
addr+=2;
if (isbyte) {
res = "("+res+")&0xff";
exprlen=2;
}
break;
}
case 0x0f: // script arg bit, like 05 and 08 but addr is only 8bit
{
uint8_t a = read8(addr)>>3;
uint8_t b = read8(addr++)&0x07;
if (!res.empty()) res += " ";
res += "arg" + std::to_string(a) + "&" + u8val2str(1<<b);
exprlen=2;
break;
}
case 0x10: // signed byte script arg
case 0x11: // unsigned byte script arg
case 0x12: // signed word script arg
case 0x13: // unsigned word script arg
{
bool isbyte = (instr==0x10 || instr==0x11);
bool issigned = (instr==0x10 || instr==0x12);
exprlen++;
if (!res.empty()) res += " ";
res += (issigned ? "signed arg" : "arg") + std::to_string(read8(addr++));
if (isbyte) {
res += "&0xff";
exprlen=2;
}
break;
}
case 0x14: // boolean invert
case 0x15: // bitwise invert
case 0x16: // flip sign
{
const char* op = (instr==0x14) ? "!" : (instr==0x15) ? "~" : "-";
std::string b = (exprlen>1) ? ("("+res+")") : res;
res = op+b;
exprlen = 2;
break;
}
case 0x17: // pull from stack, res = pulled * res
case 0x18: // pulled / res
case 0x1a: // pulled + res
case 0x1b: // pulled - res
case 0x1c: // pulled << res
case 0x1d: // pulled >> res
case 0x1e: // pulled < res (signed)
case 0x1f: // pulled > res (signed)
case 0x20: // pulled <= res (signed)
case 0x21: // pulled >= res (signed)
case 0x22: // pulled == res
case 0x23: // pulled != res
case 0x24: // pulled & res
case 0x25: // pulled | res
case 0x26: // pulled ^ res
case 0x27: // pulled || res
case 0x28: // pulled && res
{
const char* op = (instr==0x17) ? " * " : (instr==0x18) ? " / " :
(instr==0x1a) ? " + " : (instr==0x1b) ? " - " :
(instr==0x1c) ? LT LT : (instr==0x1d) ? GT GT :
(instr==0x1e) ? " " LT " " : (instr==0x1f) ? " " GT " " :
(instr==0x20) ? " " LE " " : (instr==0x21) ? " " GE " " :
(instr==0x22) ? " == " : (instr==0x23) ? " != " :
(instr==0x24) ? " & " : (instr==0x25) ? " | " :
(instr==0x26) ? " ^ " : (instr==0x27) ? " || " : " && ";
if (stack.empty()) {
fprintf(stderr, "WARN: pulled from empty stack at $%06x!!\n", addr);
ok = false;
break; // disable for debugging
}
auto& pulled = stack.top();
std::string a = (pulled.first>1) ? ("("+pulled.second+")") : pulled.second;
std::string b = (exprlen>1) ? ("("+res+")") : res;
res = a + op + b;
exprlen = 2;
stack.pop();
break;
}
case 0x29: // push to stack
stack.push(std::make_pair(exprlen,std::move(res)));
exprlen=0;
break;
case 0x2a: // random word
exprlen++;
if (!res.empty()) res += " ";
res += "RAND";
break;
case 0x2b: // (random word * $2) >> 16 = randrange[0,$2[
res = "RANDRANGE(0," LT + res + ")";
exprlen=1;
break;
case 0x2c: // dialog response
exprlen=1;
if (res.empty())
res = "Dialog response";
else {
res = "Dialog response (preselect "+res+")";
exprlen++;
}
break;
case 0x54: // $2 = script data[0x09]
if (!res.empty()) res += " ";
exprlen++;
res += "script[0x9]";
break;
case 0x55: // deref res
case 0x56: // deref res &0xff
if (exprlen>1)
res = "*(" + res + ")";
else
res = "*" + res;
exprlen = 1;
if (instr==0x56) {
res = "("+res+")&0xff";
exprlen=2;
}
break;
case 0x57:
if (!res.empty()) res += " ";
exprlen++;
res += "(player==dog)";
break;
case 0x58: // game timer bits 0-15 ($7e0b19..7e0b1a)
if (!res.empty()) res += " ";
exprlen+=2;
res += "GameTimer&0xffff";
break;
case 0x59: // bits 16-32 ($7e0b1b..7e0b1c)
if (!res.empty()) res += " ";
exprlen+=2;
res += "GameTimer>>16";
break;
case 0x5a: // Run shop: buy, get result
if (!res.empty()) res += " ";
exprlen++;
res += "Shop buy result";
break;
case 0x5b: // sell
if (!res.empty()) res += " ";
exprlen++;
res += "Shop sell result";
break;
case 0x5c: // Next damage will kill entity
if (exprlen>1)
res = "(" + res + ") will die";
else
res = res + " will die";
exprlen = 2;
break;
case 0x51:
case 0x19:
case 0x2f:
case 0x5d:
case 0x5e:
case 0x5f:
fprintf(stderr, "WARN: Invalid sub-instr 0x%02x at $%06x!\n", instr, addr);
if (!res.empty()) res += " ";
res += "[invalid " + u8val2str(instr) + "]";
ok = false;
break;
default:
fprintf(stderr, "WARN: Unhandled sub-instr 0x%02x at $%06x!\n", instr, addr);
if (!res.empty()) res += " ";
res += "[unknown " + u8val2str(instr) + "]";
ok = false;
}
}
} while (ok && !done);
if (pok) *pok = ok;
if (pexprlen) *pexprlen = exprlen;
if (ok && !stack.empty()) fprintf(stderr, "WARN: sub-instr stack not empty!\n"); // sadly this is actually used by the devs :S
else if (!ok) stack = std::stack< std::pair<int,std::string> >();
return res;
}
#define parse_sub(addrRef, ...) buf_parse_sub(buf, addrRef, len, __VA_ARGS__)
static void printwrite(const char* spaces, uint32_t scriptstart, unsigned instroff, uint8_t instr, uint16_t ramaddr, uint16_t val, const char* hex="") // this is loRAM only
{
const char* addrname = nullptr;
const char* valname = nullptr;
auto ramit = ram.find(ramaddr);
if (ramit != ram.end()) addrname = ramit->second;
auto ramvalit = ramvalues.find(ramaddr);
if (ramvalit != ramvalues.end()) {
auto valit = ramvalit->second.find(val);
if (valit != ramvalit->second.end()) valname = valit->second;
}
// TODO: print amount in decimal?
if (addrname && valname) {
printf("%s[" ADDRFMT "] (%02x) WRITE %s = %s%s\n",
spaces, ADDR, instr, addrname, valname, hex);
} else if (addrname) {
printf("%s[" ADDRFMT "] (%02x) WRITE %s = 0x%04x%s\n",
spaces, ADDR, instr, addrname, (unsigned)val, hex);
} else {
printf("%s[" ADDRFMT "] (%02x) WRITE $%04x = 0x%04x%s\n",
spaces, ADDR, instr, (unsigned)ramaddr, (unsigned)val, hex);
}
}
static void printwrite(const char* spaces, uint32_t scriptstart, unsigned instroff, uint8_t instr, uint16_t ramaddr, const char* val, const char* hex="") // this is loRAM only
{
const char* addrname = nullptr;
auto ramit = ram.find(ramaddr);
if (ramit != ram.end()) addrname = ramit->second;
// TODO: print amount in decimal?
if (addrname)
printf("%s[" ADDRFMT "] (%02x) WRITE %s = %s%s\n",
spaces, ADDR, instr, addrname, val, hex);
else
printf("%s[" ADDRFMT "] (%02x) WRITE $%04x = %s%s\n",
spaces, ADDR, instr, ramaddr, val, hex);
}
#ifdef PRINT_HEX
static const char* buf_hexdump(const uint8_t* buf, uint32_t len, char* out, size_t outlen, uint32_t start, uint32_t end, bool ellipsis=false)
{
out[0]=0;
if (end-start==0 && !ellipsis)
snprintf(out, outlen, "%s%02x%s", HEX_START, read8(start), HEX_END);
else if (end-start==0) // && ellipsis
snprintf(out, outlen, "%s%02x ...%s", HEX_START, read8(start), HEX_END);
else if (end>start) {
size_t endlen = strlen(HEX_END);
uint32_t addr = start;
int wr = snprintf(out, outlen, "%s%02x", HEX_START, read8(addr));
addr++;
while (outlen - wr > endlen+10/*6*/ && addr<=end) { // not perfect, but out should be big enough anyway
wr += snprintf(out+wr, outlen-wr, " %02x", read8(addr));
addr++;
}
if ((addr < end) || ellipsis)
snprintf(out+wr, outlen-wr, " ...%s", HEX_END);
else
snprintf(out+wr, outlen-wr, "%s", HEX_END);