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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
|
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "romdb.h"
#include "util.h"
#include "hash.h"
#include "genesis.h"
#include "menu.h"
#include "xband.h"
#include "realtec.h"
#define DOM_TITLE_START 0x120
#define DOM_TITLE_END 0x150
#define TITLE_START DOM_TITLE_END
#define TITLE_END (TITLE_START+48)
#define ROM_END 0x1A4
#define RAM_ID 0x1B0
#define RAM_FLAGS 0x1B2
#define RAM_START 0x1B4
#define RAM_END 0x1B8
#define REGION_START 0x1F0
char const *save_type_name(uint8_t save_type)
{
if (save_type == SAVE_I2C) {
return "EEPROM";
} else if(save_type == SAVE_NOR) {
return "NOR Flash";
}
return "SRAM";
}
enum {
I2C_IDLE,
I2C_START,
I2C_DEVICE_ACK,
I2C_ADDRESS_HI,
I2C_ADDRESS_HI_ACK,
I2C_ADDRESS,
I2C_ADDRESS_ACK,
I2C_READ,
I2C_READ_ACK,
I2C_WRITE,
I2C_WRITE_ACK
};
char * i2c_states[] = {
"idle",
"start",
"device ack",
"address hi",
"address hi ack",
"address",
"address ack",
"read",
"read_ack",
"write",
"write_ack"
};
void eeprom_init(eeprom_state *state, uint8_t *buffer, uint32_t size)
{
state->slave_sda = 1;
state->host_sda = state->scl = 0;
state->buffer = buffer;
state->size = size;
state->state = I2C_IDLE;
}
void set_host_sda(eeprom_state *state, uint8_t val)
{
if (state->scl) {
if (val & ~state->host_sda) {
//low to high, stop condition
state->state = I2C_IDLE;
state->slave_sda = 1;
} else if (~val & state->host_sda) {
//high to low, start condition
state->state = I2C_START;
state->slave_sda = 1;
state->counter = 8;
}
}
state->host_sda = val;
}
void set_scl(eeprom_state *state, uint8_t val)
{
if (val & ~state->scl) {
//low to high transition
switch (state->state)
{
case I2C_START:
case I2C_ADDRESS_HI:
case I2C_ADDRESS:
case I2C_WRITE:
state->latch = state->host_sda | state->latch << 1;
state->counter--;
if (!state->counter) {
switch (state->state & 0x7F)
{
case I2C_START:
state->state = I2C_DEVICE_ACK;
break;
case I2C_ADDRESS_HI:
state->address = state->latch << 8;
state->state = I2C_ADDRESS_HI_ACK;
break;
case I2C_ADDRESS:
state->address |= state->latch;
state->state = I2C_ADDRESS_ACK;
break;
case I2C_WRITE:
state->buffer[state->address] = state->latch;
state->state = I2C_WRITE_ACK;
break;
}
}
break;
case I2C_DEVICE_ACK:
if (state->latch & 1) {
state->state = I2C_READ;
state->counter = 8;
if (state->size < 256) {
state->address = state->latch >> 1;
}
state->latch = state->buffer[state->address];
} else {
if (state->size < 256) {
state->address = state->latch >> 1;
state->state = I2C_WRITE;
} else if (state->size < 4096) {
state->address = (state->latch & 0xE) << 7;
state->state = I2C_ADDRESS;
} else {
state->state = I2C_ADDRESS_HI;
}
state->counter = 8;
}
break;
case I2C_ADDRESS_HI_ACK:
state->state = I2C_ADDRESS;
state->counter = 8;
break;
case I2C_ADDRESS_ACK:
state->state = I2C_WRITE;
state->address &= state->size-1;
state->counter = 8;
break;
case I2C_READ:
state->counter--;
if (!state->counter) {
state->state = I2C_READ_ACK;
}
break;
case I2C_READ_ACK:
state->state = I2C_READ;
state->counter = 8;
state->address++;
//TODO: page mask
state->address &= state->size-1;
state->latch = state->buffer[state->address];
break;
case I2C_WRITE_ACK:
state->state = I2C_WRITE;
state->counter = 8;
state->address++;
//TODO: page mask
state->address &= state->size-1;
break;
}
} else if (~val & state->scl) {
//high to low transition
switch (state->state & 0x7F)
{
case I2C_DEVICE_ACK:
case I2C_ADDRESS_HI_ACK:
case I2C_ADDRESS_ACK:
case I2C_READ_ACK:
case I2C_WRITE_ACK:
state->slave_sda = 0;
break;
case I2C_READ:
state->slave_sda = state->latch >> 7;
state->latch = state->latch << 1;
break;
default:
state->slave_sda = 1;
break;
}
}
state->scl = val;
}
uint8_t get_sda(eeprom_state *state)
{
return state->host_sda & state->slave_sda;
}
enum {
NOR_NORMAL,
NOR_PRODUCTID,
NOR_BOOTBLOCK
};
enum {
NOR_CMD_IDLE,
NOR_CMD_AA,
NOR_CMD_55
};
//Technically this value shoudl be slightly different between NTSC and PAL
//as it's defined as 200 micro-seconds, not in clock cycles
#define NOR_WRITE_PAUSE 10690
void nor_flash_init(nor_state *state, uint8_t *buffer, uint32_t size, uint32_t page_size, uint16_t product_id, uint8_t bus_flags)
{
state->buffer = buffer;
state->page_buffer = malloc(page_size);
memset(state->page_buffer, 0xFF, page_size);
state->size = size;
state->page_size = page_size;
state->product_id = product_id;
state->last_write_cycle = 0xFFFFFFFF;
state->mode = NOR_NORMAL;
state->cmd_state = NOR_CMD_IDLE;
state->alt_cmd = 0;
state->bus_flags = bus_flags;
}
void nor_run(nor_state *state, uint32_t cycle)
{
if (state->last_write_cycle == 0xFFFFFFFF) {
return;
}
if (cycle - state->last_write_cycle >= NOR_WRITE_PAUSE) {
state->last_write_cycle = 0xFFFFFFFF;
for (uint32_t i = 0; i < state->page_size; i++) {
state->buffer[state->current_page + i] = state->page_buffer[i];
}
memset(state->page_buffer, 0xFF, state->page_size);
}
}
uint8_t nor_flash_read_b(uint32_t address, void *vcontext)
{
m68k_context *m68k = vcontext;
genesis_context *gen = m68k->system;
nor_state *state = &gen->nor;
if (
((address & 1) && state->bus_flags == RAM_FLAG_EVEN) ||
(!(address & 1) && state->bus_flags == RAM_FLAG_ODD)
) {
return 0xFF;
}
if (state->bus_flags != RAM_FLAG_BOTH) {
address = address >> 1;
}
nor_run(state, m68k->current_cycle);
switch (state->mode)
{
case NOR_NORMAL:
return state->buffer[address & (state->size-1)];
break;
case NOR_PRODUCTID:
switch (address & (state->size - 1))
{
case 0:
return state->product_id >> 8;
case 1:
return state->product_id;
case 2:
//TODO: Implement boot block protection
return 0xFE;
default:
return 0xFE;
}
break;
case NOR_BOOTBLOCK:
break;
}
return 0xFF;
}
uint16_t nor_flash_read_w(uint32_t address, void *context)
{
uint16_t value = nor_flash_read_b(address, context) << 8;
value |= nor_flash_read_b(address+1, context);
return value;
}
void nor_write_byte(nor_state *state, uint32_t address, uint8_t value, uint32_t cycle)
{
switch(state->mode)
{
case NOR_NORMAL:
if (state->last_write_cycle != 0xFFFFFFFF) {
state->current_page = address & (state->size - 1) & ~(state->page_size - 1);
}
state->page_buffer[address & (state->page_size - 1)] = value;
break;
case NOR_PRODUCTID:
break;
case NOR_BOOTBLOCK:
//TODO: Implement boot block protection
state->mode = NOR_NORMAL;
break;
}
}
void *nor_flash_write_b(uint32_t address, void *vcontext, uint8_t value)
{
m68k_context *m68k = vcontext;
genesis_context *gen = m68k->system;
nor_state *state = &gen->nor;
if (
((address & 1) && state->bus_flags == RAM_FLAG_EVEN) ||
(!(address & 1) && state->bus_flags == RAM_FLAG_ODD)
) {
return vcontext;
}
if (state->bus_flags != RAM_FLAG_BOTH) {
address = address >> 1;
}
nor_run(state, m68k->current_cycle);
switch (state->cmd_state)
{
case NOR_CMD_IDLE:
if (value == 0xAA && (address & (state->size - 1)) == 0x5555) {
state->cmd_state = NOR_CMD_AA;
} else {
nor_write_byte(state, address, value, m68k->current_cycle);
state->cmd_state = NOR_CMD_IDLE;
}
break;
case NOR_CMD_AA:
if (value == 0x55 && (address & (state->size - 1)) == 0x2AAA) {
state->cmd_state = NOR_CMD_55;
} else {
nor_write_byte(state, 0x5555, 0xAA, m68k->current_cycle);
nor_write_byte(state, address, value, m68k->current_cycle);
state->cmd_state = NOR_CMD_IDLE;
}
break;
case NOR_CMD_55:
if ((address & (state->size - 1)) == 0x5555) {
if (state->alt_cmd) {
switch(value)
{
case 0x10:
puts("UNIMPLEMENTED: NOR flash erase");
break;
case 0x20:
puts("UNIMPLEMENTED: NOR flash disable protection");
break;
case 0x40:
state->mode = NOR_BOOTBLOCK;
break;
case 0x60:
state->mode = NOR_PRODUCTID;
break;
}
} else {
switch(value)
{
case 0x80:
state->alt_cmd = 1;
break;
case 0x90:
state->mode = NOR_PRODUCTID;
break;
case 0xA0:
puts("UNIMPLEMENTED: NOR flash enable protection");
break;
case 0xF0:
state->mode = NOR_NORMAL;
break;
default:
printf("Unrecognized unshifted NOR flash command %X\n", value);
}
}
} else {
nor_write_byte(state, 0x5555, 0xAA, m68k->current_cycle);
nor_write_byte(state, 0x2AAA, 0x55, m68k->current_cycle);
nor_write_byte(state, address, value, m68k->current_cycle);
}
state->cmd_state = NOR_CMD_IDLE;
break;
}
return vcontext;
}
void *nor_flash_write_w(uint32_t address, void *vcontext, uint16_t value)
{
nor_flash_write_b(address, vcontext, value >> 8);
return nor_flash_write_b(address + 1, vcontext, value);
}
uint16_t read_sram_w(uint32_t address, m68k_context * context)
{
genesis_context * gen = context->system;
address &= gen->save_ram_mask;
switch(gen->save_type)
{
case RAM_FLAG_BOTH:
return gen->save_storage[address] << 8 | gen->save_storage[address+1];
case RAM_FLAG_EVEN:
return gen->save_storage[address >> 1] << 8 | 0xFF;
case RAM_FLAG_ODD:
return gen->save_storage[address >> 1] | 0xFF00;
}
return 0xFFFF;//We should never get here
}
uint8_t read_sram_b(uint32_t address, m68k_context * context)
{
genesis_context * gen = context->system;
address &= gen->save_ram_mask;
switch(gen->save_type)
{
case RAM_FLAG_BOTH:
return gen->save_storage[address];
case RAM_FLAG_EVEN:
if (address & 1) {
return 0xFF;
} else {
return gen->save_storage[address >> 1];
}
case RAM_FLAG_ODD:
if (address & 1) {
return gen->save_storage[address >> 1];
} else {
return 0xFF;
}
}
return 0xFF;//We should never get here
}
m68k_context * write_sram_area_w(uint32_t address, m68k_context * context, uint16_t value)
{
genesis_context * gen = context->system;
if ((gen->bank_regs[0] & 0x3) == 1) {
address &= gen->save_ram_mask;
switch(gen->save_type)
{
case RAM_FLAG_BOTH:
gen->save_storage[address] = value >> 8;
gen->save_storage[address+1] = value;
break;
case RAM_FLAG_EVEN:
gen->save_storage[address >> 1] = value >> 8;
break;
case RAM_FLAG_ODD:
gen->save_storage[address >> 1] = value;
break;
}
}
return context;
}
m68k_context * write_sram_area_b(uint32_t address, m68k_context * context, uint8_t value)
{
genesis_context * gen = context->system;
if ((gen->bank_regs[0] & 0x3) == 1) {
address &= gen->save_ram_mask;
switch(gen->save_type)
{
case RAM_FLAG_BOTH:
gen->save_storage[address] = value;
break;
case RAM_FLAG_EVEN:
if (!(address & 1)) {
gen->save_storage[address >> 1] = value;
}
break;
case RAM_FLAG_ODD:
if (address & 1) {
gen->save_storage[address >> 1] = value;
}
break;
}
}
return context;
}
m68k_context * write_bank_reg_w(uint32_t address, m68k_context * context, uint16_t value)
{
genesis_context * gen = context->system;
address &= 0xE;
address >>= 1;
gen->bank_regs[address] = value;
if (!address) {
if (value & 1) {
//Used for games that only use the mapper for SRAM
context->mem_pointers[gen->mapper_start_index] = NULL;
//For games that need more than 4MB
for (int i = 4; i < 8; i++)
{
context->mem_pointers[gen->mapper_start_index + i] = NULL;
}
} else {
//Used for games that only use the mapper for SRAM
context->mem_pointers[gen->mapper_start_index] = gen->cart + 0x200000/2;
//For games that need more than 4MB
for (int i = 4; i < 8; i++)
{
context->mem_pointers[gen->mapper_start_index + i] = gen->cart + 0x40000*gen->bank_regs[i];
}
}
} else {
void *new_ptr = gen->cart + 0x40000*value;
if (context->mem_pointers[gen->mapper_start_index + address] != new_ptr) {
m68k_invalidate_code_range(gen->m68k, address * 0x80000, (address + 1) * 0x80000);
context->mem_pointers[gen->mapper_start_index + address] = new_ptr;
}
}
return context;
}
m68k_context * write_bank_reg_b(uint32_t address, m68k_context * context, uint8_t value)
{
if (address & 1) {
write_bank_reg_w(address, context, value);
}
return context;
}
eeprom_map *find_eeprom_map(uint32_t address, genesis_context *gen)
{
for (int i = 0; i < gen->num_eeprom; i++)
{
if (address >= gen->eeprom_map[i].start && address <= gen->eeprom_map[i].end) {
return gen->eeprom_map + i;
}
}
return NULL;
}
void * write_eeprom_i2c_w(uint32_t address, void * context, uint16_t value)
{
genesis_context *gen = ((m68k_context *)context)->system;
eeprom_map *map = find_eeprom_map(address, gen);
if (!map) {
fatal_error("Could not find EEPROM map for address %X\n", address);
}
if (map->scl_mask) {
set_scl(&gen->eeprom, (value & map->scl_mask) != 0);
}
if (map->sda_write_mask) {
set_host_sda(&gen->eeprom, (value & map->sda_write_mask) != 0);
}
return context;
}
void * write_eeprom_i2c_b(uint32_t address, void * context, uint8_t value)
{
genesis_context *gen = ((m68k_context *)context)->system;
eeprom_map *map = find_eeprom_map(address, gen);
if (!map) {
fatal_error("Could not find EEPROM map for address %X\n", address);
}
uint16_t expanded, mask;
if (address & 1) {
expanded = value;
mask = 0xFF;
} else {
expanded = value << 8;
mask = 0xFF00;
}
if (map->scl_mask & mask) {
set_scl(&gen->eeprom, (expanded & map->scl_mask) != 0);
}
if (map->sda_write_mask & mask) {
set_host_sda(&gen->eeprom, (expanded & map->sda_write_mask) != 0);
}
return context;
}
uint16_t read_eeprom_i2c_w(uint32_t address, void * context)
{
genesis_context *gen = ((m68k_context *)context)->system;
eeprom_map *map = find_eeprom_map(address, gen);
if (!map) {
fatal_error("Could not find EEPROM map for address %X\n", address);
}
uint16_t ret = 0;
if (map->sda_read_bit < 16) {
ret = get_sda(&gen->eeprom) << map->sda_read_bit;
}
return ret;
}
uint8_t read_eeprom_i2c_b(uint32_t address, void * context)
{
genesis_context *gen = ((m68k_context *)context)->system;
eeprom_map *map = find_eeprom_map(address, gen);
if (!map) {
fatal_error("Could not find EEPROM map for address %X\n", address);
}
uint8_t bit = address & 1 ? map->sda_read_bit : map->sda_read_bit - 8;
uint8_t ret = 0;
if (bit < 8) {
ret = get_sda(&gen->eeprom) << bit;
}
return ret;
}
tern_node *load_rom_db()
{
tern_node *db = parse_bundled_config("rom.db");
if (!db) {
fatal_error("Failed to load ROM DB\n");
}
return db;
}
void free_rom_info(rom_info *info)
{
free(info->name);
if (info->save_type != SAVE_NONE) {
free(info->save_buffer);
if (info->save_type == SAVE_I2C) {
free(info->eeprom_map);
}
}
free(info->map);
free(info->port1_override);
free(info->port2_override);
free(info->ext_override);
free(info->mouse_mode);
}
char *get_header_name(uint8_t *rom)
{
//TODO: Should probably prefer the title field that corresponds to the user's region preference
uint8_t *last = rom + TITLE_END - 1;
uint8_t *src = rom + TITLE_START;
for (;;)
{
while (last > src && (*last <= 0x20 || *last >= 0x80))
{
last--;
}
if (last == src) {
if (src == rom + TITLE_START) {
src = rom + DOM_TITLE_START;
last = rom + DOM_TITLE_END - 1;
} else {
return strdup("UNKNOWN");
}
} else {
last++;
char *ret = malloc(last - src + 1);
uint8_t *dst;
uint8_t last_was_space = 1;
for (dst = ret; src < last; src++)
{
if (*src >= 0x20 && *src < 0x80) {
if (*src == ' ') {
if (last_was_space) {
continue;
}
last_was_space = 1;
} else {
last_was_space = 0;
}
*(dst++) = *src;
}
}
*dst = 0;
return ret;
}
}
}
char *region_chars = "JUEW";
uint8_t region_bits[] = {REGION_J, REGION_U, REGION_E, REGION_J|REGION_U|REGION_E};
uint8_t translate_region_char(uint8_t c)
{
for (int i = 0; i < sizeof(region_bits); i++)
{
if (c == region_chars[i]) {
return region_bits[i];
}
}
uint8_t bin_region = 0;
if (c >= '0' && c <= '9') {
bin_region = c - '0';
} else if (c >= 'A' && c <= 'F') {
bin_region = c - 'A' + 0xA;
} else if (c >= 'a' && c <= 'f') {
bin_region = c - 'a' + 0xA;
}
uint8_t ret = 0;
if (bin_region & 8) {
ret |= REGION_E;
}
if (bin_region & 4) {
ret |= REGION_U;
}
if (bin_region & 1) {
ret |= REGION_J;
}
return ret;
}
uint8_t get_header_regions(uint8_t *rom)
{
uint8_t regions = 0;
for (int i = 0; i < 3; i++)
{
regions |= translate_region_char(rom[REGION_START + i]);
}
return regions;
}
uint32_t get_u32be(uint8_t *data)
{
return *data << 24 | data[1] << 16 | data[2] << 8 | data[3];
}
uint32_t calc_mask(uint32_t src_size, uint32_t start, uint32_t end)
{
uint32_t map_size = end-start+1;
if (src_size < map_size) {
return nearest_pow2(src_size)-1;
} else if (!start) {
return 0xFFFFFF;
} else {
return nearest_pow2(map_size)-1;
}
}
uint8_t has_ram_header(uint8_t *rom, uint32_t rom_size)
{
return rom_size >= (RAM_END + 4) && rom[RAM_ID] == 'R' && rom[RAM_ID + 1] == 'A';
}
uint32_t read_ram_header(rom_info *info, uint8_t *rom)
{
uint32_t ram_start = get_u32be(rom + RAM_START);
uint32_t ram_end = get_u32be(rom + RAM_END);
uint32_t ram_flags = info->save_type = rom[RAM_FLAGS] & RAM_FLAG_MASK;
ram_start &= 0xFFFFFE;
ram_end |= 1;
info->save_mask = ram_end - ram_start;
uint32_t save_size = info->save_mask + 1;
if (ram_flags != RAM_FLAG_BOTH) {
save_size /= 2;
}
info->save_size = save_size;
info->save_buffer = malloc(save_size);
return ram_start;
}
void add_memmap_header(rom_info *info, uint8_t *rom, uint32_t size, memmap_chunk const *base_map, int base_chunks)
{
uint32_t rom_end = get_u32be(rom + ROM_END) + 1;
if (size > rom_end) {
rom_end = size;
} else if (rom_end > nearest_pow2(size)) {
rom_end = nearest_pow2(size);
}
if (size >= 0x80000 && !memcmp("SEGA SSF", rom + 0x100, 8)) {
info->mapper_start_index = 0;
info->map_chunks = base_chunks + 9;
info->map = malloc(sizeof(memmap_chunk) * info->map_chunks);
memset(info->map, 0, sizeof(memmap_chunk)*9);
memcpy(info->map+9, base_map, sizeof(memmap_chunk) * base_chunks);
info->map[0].start = 0;
info->map[0].end = 0x80000;
info->map[0].mask = 0xFFFFFF;
info->map[0].flags = MMAP_READ;
info->map[0].buffer = rom;
if (has_ram_header(rom, size)){
read_ram_header(info, rom);
}
for (int i = 1; i < 8; i++)
{
info->map[i].start = i * 0x80000;
info->map[i].end = (i + 1) * 0x80000;
info->map[i].mask = 0x7FFFF;
info->map[i].buffer = (i + 1) * 0x80000 <= size ? rom + i * 0x80000 : rom;
info->map[i].ptr_index = i;
info->map[i].flags = MMAP_READ | MMAP_PTR_IDX | MMAP_CODE | MMAP_FUNC_NULL;
info->map[i].read_16 = (read_16_fun)read_sram_w;//these will only be called when mem_pointers[i] == NULL
info->map[i].read_8 = (read_8_fun)read_sram_b;
info->map[i].write_16 = (write_16_fun)write_sram_area_w;//these will be called all writes to the area
info->map[i].write_8 = (write_8_fun)write_sram_area_b;
}
info->map[8].start = 0xA13000;
info->map[8].end = 0xA13100;
info->map[8].mask = 0xFF;
info->map[8].write_16 = (write_16_fun)write_bank_reg_w;
info->map[8].write_8 = (write_8_fun)write_bank_reg_b;
} else if (has_ram_header(rom, size)) {
uint32_t ram_start = read_ram_header(info, rom);
info->map_chunks = base_chunks + (ram_start >= rom_end ? 2 : 3);
info->map = malloc(sizeof(memmap_chunk) * info->map_chunks);
memset(info->map, 0, sizeof(memmap_chunk)*2);
memcpy(info->map+2, base_map, sizeof(memmap_chunk) * base_chunks);
if (ram_start >= rom_end) {
info->map[0].end = rom_end < 0x400000 ? nearest_pow2(rom_end) - 1 : 0xFFFFFF;
if (info->map[0].end > ram_start) {
info->map[0].end = ram_start;
}
//TODO: ROM mirroring
info->map[0].mask = 0xFFFFFF;
info->map[0].flags = MMAP_READ;
info->map[0].buffer = rom;
info->map[1].start = ram_start;
info->map[1].mask = info->save_mask;
info->map[1].end = ram_start + info->save_mask + 1;
info->map[1].flags = MMAP_READ | MMAP_WRITE;
if (info->save_type == RAM_FLAG_ODD) {
info->map[1].flags |= MMAP_ONLY_ODD;
} else if (info->save_type == RAM_FLAG_EVEN) {
info->map[1].flags |= MMAP_ONLY_EVEN;
}
info->map[1].buffer = info->save_buffer;
} else {
//Assume the standard Sega mapper
info->map[0].end = 0x200000;
info->map[0].mask = 0xFFFFFF;
info->map[0].flags = MMAP_READ;
info->map[0].buffer = rom;
info->map[1].start = 0x200000;
info->map[1].end = 0x400000;
info->map[1].mask = 0x1FFFFF;
info->map[1].flags = MMAP_READ | MMAP_PTR_IDX | MMAP_FUNC_NULL;
info->map[1].ptr_index = info->mapper_start_index = 0;
info->map[1].read_16 = (read_16_fun)read_sram_w;//these will only be called when mem_pointers[2] == NULL
info->map[1].read_8 = (read_8_fun)read_sram_b;
info->map[1].write_16 = (write_16_fun)write_sram_area_w;//these will be called all writes to the area
info->map[1].write_8 = (write_8_fun)write_sram_area_b;
info->map[1].buffer = rom + 0x200000;
memmap_chunk *last = info->map + info->map_chunks - 1;
memset(last, 0, sizeof(memmap_chunk));
last->start = 0xA13000;
last->end = 0xA13100;
last->mask = 0xFF;
last->write_16 = (write_16_fun)write_bank_reg_w;
last->write_8 = (write_8_fun)write_bank_reg_b;
}
} else {
info->map_chunks = base_chunks + 1;
info->map = malloc(sizeof(memmap_chunk) * info->map_chunks);
memset(info->map, 0, sizeof(memmap_chunk));
memcpy(info->map+1, base_map, sizeof(memmap_chunk) * base_chunks);
info->map[0].end = rom_end > 0x400000 ? rom_end : 0x400000;
info->map[0].mask = rom_end < 0x400000 ? nearest_pow2(rom_end) - 1 : 0xFFFFFF;
info->map[0].flags = MMAP_READ;
info->map[0].buffer = rom;
info->save_type = SAVE_NONE;
}
}
rom_info configure_rom_heuristics(uint8_t *rom, uint32_t rom_size, memmap_chunk const *base_map, uint32_t base_chunks)
{
rom_info info;
info.name = get_header_name(rom);
info.regions = get_header_regions(rom);
add_memmap_header(&info, rom, rom_size, base_map, base_chunks);
info.port1_override = info.port2_override = info.ext_override = info.mouse_mode = NULL;
return info;
}
typedef struct {
rom_info *info;
uint8_t *rom;
uint8_t *lock_on;
tern_node *root;
tern_node *rom_db;
uint32_t rom_size;
uint32_t lock_on_size;
int index;
int num_els;
uint16_t ptr_index;
} map_iter_state;
void eeprom_read_fun(char *key, tern_val val, uint8_t valtype, void *data)
{
int bit = atoi(key);
if (bit < 0 || bit > 15) {
fprintf(stderr, "bit %s is out of range", key);
return;
}
if (valtype != TVAL_PTR) {
fprintf(stderr, "bit %s has a non-scalar value", key);
return;
}
char *pin = val.ptrval;
if (strcmp(pin, "sda")) {
fprintf(stderr, "bit %s is connected to unrecognized read pin %s", key, pin);
return;
}
eeprom_map *map = data;
map->sda_read_bit = bit;
}
void eeprom_write_fun(char *key, tern_val val, uint8_t valtype, void *data)
{
int bit = atoi(key);
if (bit < 0 || bit > 15) {
fprintf(stderr, "bit %s is out of range", key);
return;
}
if (valtype != TVAL_PTR) {
fprintf(stderr, "bit %s has a non-scalar value", key);
return;
}
char *pin = val.ptrval;
eeprom_map *map = data;
if (!strcmp(pin, "sda")) {
map->sda_write_mask = 1 << bit;
return;
}
if (!strcmp(pin, "scl")) {
map->scl_mask = 1 << bit;
return;
}
fprintf(stderr, "bit %s is connected to unrecognized write pin %s", key, pin);
}
void process_sram_def(char *key, map_iter_state *state)
{
if (!state->info->save_size) {
char * size = tern_find_path(state->root, "SRAM\0size\0", TVAL_PTR).ptrval;
if (!size) {
fatal_error("ROM DB map entry %d with address %s has device type SRAM, but the SRAM size is not defined\n", state->index, key);
}
state->info->save_size = atoi(size);
if (!state->info->save_size) {
fatal_error("SRAM size %s is invalid\n", size);
}
state->info->save_mask = nearest_pow2(state->info->save_size)-1;
state->info->save_buffer = malloc(state->info->save_size);
memset(state->info->save_buffer, 0, state->info->save_size);
char *bus = tern_find_path(state->root, "SRAM\0bus\0", TVAL_PTR).ptrval;
if (!strcmp(bus, "odd")) {
state->info->save_type = RAM_FLAG_ODD;
} else if(!strcmp(bus, "even")) {
state->info->save_type = RAM_FLAG_EVEN;
} else {
state->info->save_type = RAM_FLAG_BOTH;
}
}
}
void process_eeprom_def(char * key, map_iter_state *state)
{
if (!state->info->save_size) {
char * size = tern_find_path(state->root, "EEPROM\0size\0", TVAL_PTR).ptrval;
if (!size) {
fatal_error("ROM DB map entry %d with address %s has device type EEPROM, but the EEPROM size is not defined\n", state->index, key);
}
state->info->save_size = atoi(size);
if (!state->info->save_size) {
fatal_error("EEPROM size %s is invalid\n", size);
}
char *etype = tern_find_path(state->root, "EEPROM\0type\0", TVAL_PTR).ptrval;
if (!etype) {
etype = "i2c";
}
if (!strcmp(etype, "i2c")) {
state->info->save_type = SAVE_I2C;
} else {
fatal_error("EEPROM type %s is invalid\n", etype);
}
state->info->save_buffer = malloc(state->info->save_size);
memset(state->info->save_buffer, 0xFF, state->info->save_size);
state->info->eeprom_map = malloc(sizeof(eeprom_map) * state->num_els);
memset(state->info->eeprom_map, 0, sizeof(eeprom_map) * state->num_els);
}
}
void process_nor_def(char *key, map_iter_state *state)
{
if (!state->info->save_size) {
char *size = tern_find_path(state->root, "NOR\0size\0", TVAL_PTR).ptrval;
if (!size) {
fatal_error("ROM DB map entry %d with address %s has device type NOR, but the NOR size is not defined\n", state->index, key);
}
state->info->save_size = atoi(size);
if (!state->info->save_size) {
fatal_error("NOR size %s is invalid\n", size);
}
char *page_size = tern_find_path(state->root, "NOR\0page_size\0", TVAL_PTR).ptrval;
if (!page_size) {
fatal_error("ROM DB map entry %d with address %s has device type NOR, but the NOR page size is not defined\n", state->index, key);
}
state->info->save_page_size = atoi(size);
if (!state->info->save_page_size) {
fatal_error("NOR page size %s is invalid\n", size);
}
char *product_id = tern_find_path(state->root, "NOR\0product_id\0", TVAL_PTR).ptrval;
if (!product_id) {
fatal_error("ROM DB map entry %d with address %s has device type NOR, but the NOR product ID is not defined\n", state->index, key);
}
state->info->save_product_id = strtol(product_id, NULL, 16);
char *bus = tern_find_path(state->root, "NOR\0bus\0", TVAL_PTR).ptrval;
if (!strcmp(bus, "odd")) {
state->info->save_bus = RAM_FLAG_ODD;
} else if(!strcmp(bus, "even")) {
state->info->save_bus = RAM_FLAG_EVEN;
} else {
state->info->save_bus = RAM_FLAG_BOTH;
}
state->info->save_type = SAVE_NOR;
state->info->save_buffer = malloc(state->info->save_size);
memset(state->info->save_buffer, 0xFF, state->info->save_size);
}
}
void add_eeprom_map(tern_node *node, uint32_t start, uint32_t end, map_iter_state *state)
{
eeprom_map *eep_map = state->info->eeprom_map + state->info->num_eeprom;
eep_map->start = start;
eep_map->end = end;
eep_map->sda_read_bit = 0xFF;
tern_node * bits_read = tern_find_node(node, "bits_read");
if (bits_read) {
tern_foreach(bits_read, eeprom_read_fun, eep_map);
}
tern_node * bits_write = tern_find_node(node, "bits_write");
if (bits_write) {
tern_foreach(bits_write, eeprom_write_fun, eep_map);
}
printf("EEPROM address %X: sda read: %X, sda write: %X, scl: %X\n", start, eep_map->sda_read_bit, eep_map->sda_write_mask, eep_map->scl_mask);
state->info->num_eeprom++;
}
void map_iter_fun(char *key, tern_val val, uint8_t valtype, void *data)
{
map_iter_state *state = data;
if (valtype != TVAL_NODE) {
fatal_error("ROM DB map entry %d with address %s is not a node\n", state->index, key);
}
tern_node *node = val.ptrval;
uint32_t start = strtol(key, NULL, 16);
uint32_t end = strtol(tern_find_ptr_default(node, "last", "0"), NULL, 16);
if (!end || end < start) {
fatal_error("'last' value is missing or invalid for ROM DB map entry %d with address %s\n", state->index, key);
}
char * dtype = tern_find_ptr_default(node, "device", "ROM");
uint32_t offset = strtol(tern_find_ptr_default(node, "offset", "0"), NULL, 16);
memmap_chunk *map = state->info->map + state->index;
map->start = start;
map->end = end + 1;
if (!strcmp(dtype, "ROM")) {
map->buffer = state->rom + offset;
map->flags = MMAP_READ;
map->mask = calc_mask(state->rom_size - offset, start, end);
} else if (!strcmp(dtype, "LOCK-ON")) {
rom_info lock_info;
if (state->lock_on) {
lock_info = configure_rom(state->rom_db, state->lock_on, state->lock_on_size, NULL, 0, NULL, 0);
} else if (state->rom_size > start) {
//This is a bit of a hack to deal with pre-combined S3&K/S2&K ROMs and S&K ROM hacks
lock_info = configure_rom(state->rom_db, state->rom + start, state->rom_size - start, NULL, 0, NULL, 0);
} else {
//skip this entry if there is no lock on cartridge attached
return;
}
uint32_t matching_chunks = 0;
for (int i = 0; i < lock_info.map_chunks; i++)
{
if (lock_info.map[i].start < 0xC00000 && lock_info.map[i].end > 0x200000) {
matching_chunks++;
}
}
if (matching_chunks == 0) {
//Nothing mapped in the relevant range for the lock-on cart, ignore this mapping
return;
} else if (matching_chunks > 1) {
state->info->map_chunks += matching_chunks - 1;
state->info->map = realloc(state->info->map, sizeof(memmap_chunk) * state->info->map_chunks);
memset(state->info->map + state->info->map_chunks - (matching_chunks - 1), 0, sizeof(memmap_chunk) * (matching_chunks - 1));
map = state->info->map + state->index;
}
for (int i = 0; i < matching_chunks; i++)
{
if (lock_info.map[i].start >= 0xC00000 || lock_info.map[i].end <= 0x200000) {
continue;
}
*map = lock_info.map[i];
if (map->start < 0x200000) {
if (map->buffer) {
map->buffer += (0x200000 - map->start) & ((map->flags & MMAP_AUX_BUFF) ? map->aux_mask : map->mask);
}
map->start = 0x200000;
}
map++;
state->index++;
}
if (state->info->save_type == SAVE_NONE && lock_info.save_type != SAVE_NONE) {
//main cart has no save device, but lock-on cart does
if (state->lock_on) {
state->info->is_save_lock_on = 1;
}
state->info->save_buffer = lock_info.save_buffer;
state->info->save_size = lock_info.save_size;
state->info->save_mask = lock_info.save_mask;
state->info->save_page_size = lock_info.save_page_size;
state->info->save_product_id = lock_info.save_product_id;
state->info->save_type = lock_info.save_type;
state->info->save_bus = lock_info.save_bus;
lock_info.save_buffer = NULL;
lock_info.save_type = SAVE_NONE;
}
free_rom_info(&lock_info);
return;
} else if (!strcmp(dtype, "EEPROM")) {
process_eeprom_def(key, state);
add_eeprom_map(node, start, end, state);
map->write_16 = write_eeprom_i2c_w;
map->write_8 = write_eeprom_i2c_b;
map->read_16 = read_eeprom_i2c_w;
map->read_8 = read_eeprom_i2c_b;
map->mask = 0xFFFFFF;
} else if (!strcmp(dtype, "SRAM")) {
process_sram_def(key, state);
map->buffer = state->info->save_buffer + offset;
map->flags = MMAP_READ | MMAP_WRITE;
if (state->info->save_type == RAM_FLAG_ODD) {
map->flags |= MMAP_ONLY_ODD;
} else if(state->info->save_type == RAM_FLAG_EVEN) {
map->flags |= MMAP_ONLY_EVEN;
}
map->mask = calc_mask(state->info->save_size, start, end);
} else if (!strcmp(dtype, "RAM")) {
uint32_t size = strtol(tern_find_ptr_default(node, "size", "0"), NULL, 16);
if (!size || size > map->end - map->start) {
size = map->end - map->start;
}
map->buffer = malloc(size);
map->mask = calc_mask(size, start, end);
map->flags = MMAP_READ | MMAP_WRITE;
char *bus = tern_find_ptr_default(node, "bus", "both");
if (!strcmp(bus, "odd")) {
map->flags |= MMAP_ONLY_ODD;
} else if (!strcmp(bus, "even")) {
map->flags |= MMAP_ONLY_EVEN;
} else {
map->flags |= MMAP_CODE;
}
} else if (!strcmp(dtype, "NOR")) {
process_nor_def(key, state);
map->write_16 = nor_flash_write_w;
map->write_8 = nor_flash_write_b;
map->read_16 = nor_flash_read_w;
map->read_8 = nor_flash_read_b;
map->mask = 0xFFFFFF;
} else if (!strcmp(dtype, "Sega mapper")) {
state->info->mapper_start_index = state->ptr_index++;
state->info->map_chunks+=7;
state->info->map = realloc(state->info->map, sizeof(memmap_chunk) * state->info->map_chunks);
memset(state->info->map + state->info->map_chunks - 7, 0, sizeof(memmap_chunk) * 7);
map = state->info->map + state->index;
char *save_device = tern_find_path(node, "save\0device\0", TVAL_PTR).ptrval;
if (save_device && !strcmp(save_device, "EEPROM")) {
process_eeprom_def(key, state);
add_eeprom_map(node, start & map->mask, end & map->mask, state);
} else if (save_device && !strcmp(save_device, "SRAM")) {
process_sram_def(key, state);
} else if(has_ram_header(state->rom, state->rom_size)) {
//no save definition in ROM DB entry, but there is an SRAM header
//this support is mostly to handle homebrew that uses the SSF2 product ID
//in an attempt to signal desire for the full Sega/SSF2 mapper, but also uses SRAM unlike SSF2
read_ram_header(state->info, state->rom);
}
for (int i = 0; i < 7; i++, state->index++, map++)
{
map->start = start + i * 0x80000;
map->end = start + (i + 1) * 0x80000;
map->mask = 0x7FFFF;
map->buffer = state->rom + offset + i * 0x80000;
map->ptr_index = state->ptr_index++;
if (i < 3) {
map->flags = MMAP_READ | MMAP_PTR_IDX | MMAP_CODE;
} else {
map->flags = MMAP_READ | MMAP_PTR_IDX | MMAP_CODE | MMAP_FUNC_NULL;
if (save_device && !strcmp(save_device, "EEPROM")) {
map->write_16 = write_eeprom_i2c_w;
map->write_8 = write_eeprom_i2c_b;
map->read_16 = read_eeprom_i2c_w;
map->read_8 = read_eeprom_i2c_b;
} else {
map->read_16 = (read_16_fun)read_sram_w;//these will only be called when mem_pointers[2] == NULL
map->read_8 = (read_8_fun)read_sram_b;
map->write_16 = (write_16_fun)write_sram_area_w;//these will be called all writes to the area
map->write_8 = (write_8_fun)write_sram_area_b;
}
}
}
map->start = 0xA13000;
map->end = 0xA13100;
map->mask = 0xFF;
map->write_16 = (write_16_fun)write_bank_reg_w;
map->write_8 = (write_8_fun)write_bank_reg_b;
} else if (!strcmp(dtype, "MENU")) {
//fake hardware for supporting menu
map->buffer = NULL;
map->mask = 0xFF;
map->write_16 = menu_write_w;
map->read_16 = menu_read_w;
} else if (!strcmp(dtype, "fixed")) {
uint16_t *value = malloc(2);
map->buffer = value;
map->mask = 0;
map->flags = MMAP_READ;
*value = strtol(tern_find_ptr_default(node, "value", "0"), NULL, 16);
} else {
fatal_error("Invalid device type %s for ROM DB map entry %d with address %s\n", dtype, state->index, key);
}
state->index++;
}
rom_info configure_rom(tern_node *rom_db, void *vrom, uint32_t rom_size, void *lock_on, uint32_t lock_on_size, memmap_chunk const *base_map, uint32_t base_chunks)
{
uint8_t product_id[GAME_ID_LEN+1];
uint8_t *rom = vrom;
product_id[GAME_ID_LEN] = 0;
for (int i = 0; i < GAME_ID_LEN; i++)
{
if (rom[GAME_ID_OFF + i] <= ' ') {
product_id[i] = 0;
break;
}
product_id[i] = rom[GAME_ID_OFF + i];
}
printf("Product ID: %s\n", product_id);
uint8_t raw_hash[20];
sha1(vrom, rom_size, raw_hash);
uint8_t hex_hash[41];
bin_to_hex(hex_hash, raw_hash, 20);
printf("SHA1: %s\n", hex_hash);
tern_node * entry = tern_find_node(rom_db, hex_hash);
if (!entry) {
entry = tern_find_node(rom_db, product_id);
}
if (!entry) {
puts("Not found in ROM DB, examining header\n");
if (xband_detect(rom, rom_size)) {
return xband_configure_rom(rom_db, rom, rom_size, lock_on, lock_on_size, base_map, base_chunks);
}
if (realtec_detect(rom, rom_size)) {
return realtec_configure_rom(rom, rom_size, base_map, base_chunks);
}
return configure_rom_heuristics(rom, rom_size, base_map, base_chunks);
}
rom_info info;
info.name = tern_find_ptr(entry, "name");
if (info.name) {
printf("Found name: %s\n", info.name);
info.name = strdup(info.name);
} else {
info.name = get_header_name(rom);
}
char *dbreg = tern_find_ptr(entry, "regions");
info.regions = 0;
if (dbreg) {
while (*dbreg != 0)
{
info.regions |= translate_region_char(*(dbreg++));
}
}
if (!info.regions) {
info.regions = get_header_regions(rom);
}
tern_node *map = tern_find_node(entry, "map");
if (map) {
info.save_type = SAVE_NONE;
info.map_chunks = tern_count(map);
if (info.map_chunks) {
info.map_chunks += base_chunks;
info.save_buffer = NULL;
info.save_size = 0;
info.map = malloc(sizeof(memmap_chunk) * info.map_chunks);
info.eeprom_map = NULL;
info.num_eeprom = 0;
memset(info.map, 0, sizeof(memmap_chunk) * info.map_chunks);
map_iter_state state = {
.info = &info,
.rom = rom,
.lock_on = lock_on,
.root = entry,
.rom_db = rom_db,
.rom_size = rom_size,
.lock_on_size = lock_on_size,
.index = 0,
.num_els = info.map_chunks - base_chunks,
.ptr_index = 0
};
tern_foreach(map, map_iter_fun, &state);
memcpy(info.map + state.index, base_map, sizeof(memmap_chunk) * base_chunks);
} else {
add_memmap_header(&info, rom, rom_size, base_map, base_chunks);
}
} else {
add_memmap_header(&info, rom, rom_size, base_map, base_chunks);
}
tern_node *device_overrides = tern_find_node(entry, "device_overrides");
if (device_overrides) {
info.port1_override = tern_find_ptr(device_overrides, "1");
info.port2_override = tern_find_ptr(device_overrides, "2");
info.ext_override = tern_find_ptr(device_overrides, "ext");
} else {
info.port1_override = info.port2_override = info.ext_override = NULL;
}
info.mouse_mode = tern_find_ptr(entry, "mouse_mode");
return info;
}
|