-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3m2nsf.c
1009 lines (772 loc) · 21.6 KB
/
s3m2nsf.c
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
/*************************************************
Copyright (c) 2007, Juan Linietsky, Mukunda Johnson
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the owners nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*************************************************/
//#define DEBUG_EXTERNALCORE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "player.h"
#define S3M_NSF_VERSION "1.0pre3"
#define S3M_NSF_AUTHORS "(c) 2007 Juan Linietsky (reduz), Mukunda Johnson (eKid)"
#ifdef HOST_16_BITS
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
typedef signed char s8;
#else
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef signed char s8;
#endif
#define SQUARE_NOTE_LOW 21
#define TRI_NOTE_LOW 9
FILE *fin;
FILE *fout;
int FWRITE_BANK;
int FWRITE_OFFSET;
u8* SDBANK=NULL;
int SDWRITE;
int vrc6_support;
int vrc6_instr;
typedef struct {
u16 length;
u8 *data;
u16 parapointer;
} S3M_Pattern;
typedef struct {
u16 parapointer;
char name[29];
u8 default_volume;
u32 c2speed;
u8 hasloop;
u8 *dpcm_data;
u16 dpcm_len;
} S3M_Instrument;
typedef struct {
char songname[29];
u16 order_count;
u16 instrument_count;
u16 pattern_count;
u16 flags;
u8 initial_tempo;
u8 initial_speed;
u8 initial_volume;
u8 channels[32]; //s3m weirdness
u8 *order_list;
S3M_Instrument *instruments;
S3M_Pattern *patterns;
} S3M;
u8 get_u8() {
u8 b;
fread(&b,1,1,fin);
return b;
};
u16 get_u16() {
u16 w;
// portable read little endian
w=get_u8();
w|=(u16)get_u8() << 8;
return w;
};
u32 get_u32() {
u32 dw;
// portable read little endian
dw=get_u16();
dw|=(u32)get_u16() << 16;
return dw;
};
void store_u8(u8 p_v) {
fwrite(&p_v,1,1,fout);
};
void store_u16(u16 p_v) {
// portable write little endian
store_u8( p_v & 0xFF );
store_u8( p_v >> 8 );
};
void store_u32(u32 p_v) {
// portable write little endian
store_u16( p_v & 0xFFFF );
store_u16( p_v >> 16 );
};
void print_instrument_error() {
fprintf(stderr,"ERROR: At least 7 instruments are expected, in slots 1 to 7.\n");
// fprintf(stderr,"ERROR: Slots 1-4: square waves (12.5%, 25%, 50%, 75% duties respectively).\n");
// fprintf(stderr,"ERROR: Slot 5: triangular wave (volumes less than 64 are ignored).");
// fprintf(stderr,"ERROR: Slot 6: Noise Type 1");
// fprintf(stderr,"ERROR: Slot 7: Noise Type 2");
// fprintf(stderr,"ERROR: Slot 8+: DPCMs (Optional)");
fprintf(stderr,"ERROR: Slots 1-4: square waves (12.5%%, 25%%, 50%%, 75%% duties respectively).\n");
fprintf(stderr,"ERROR: Slot 5: triangular wave (volumes less than 64 are ignored).\n");
fprintf(stderr,"ERROR: Slot 6: Noise Type 1\n");
fprintf(stderr,"ERROR: Slot 7: Noise Type 2\n");
fprintf(stderr,"ERROR: Slot 8+: DPCMs (Optional)\n");
}
int checkdmcnote( int note )
{
if( note != 48 && note != 50 && note != 52 && note != 53 && note != 55 && note != 57 )
if( note != 59 && note != 60 && note != 62 && note != 65 && note != 67 && note != 69 )
if( note != 72 && note != 76 && note != 79 && note != 84 ) return 255;
return 0;
}
void checkpattern( S3M_Pattern* p, int index ) {
int row;
int chan;
int note;
int inst;
int vol;
int fx;
int param;
int r;
u8 a;
r=0;
row=0;
chan=0;
while( row < 64 )
{
a = p->data[r];
if( a == 0 )
{
// next row
r++;
row++;
chan=0;
}
else
{
r++;
// read data
chan = a & 31;
a >>= 5;
if( a & 1 )
{
note = p->data[r]; r++;
if( note == 16 )
{
note=16;
}
if( note == 254 )
note = 60;
if( note == 255 )
note = 60;
else
note = ((note>>4)*12)+(note&0xF);
inst = p->data[r]; r++;
if( note == 16 )
{
note=16;
}
}
else
{
note = 60;
inst = 0;
}
if( a & 2 )
{
vol = p->data[r]; r++;
}
if( a & 4 )
{
fx = p->data[r]; r++;
param = p->data[r]; r++;
}
switch( chan )
{
case 0:
case 1:
if( note < SQUARE_NOTE_LOW )
fprintf( stderr, "WARNING: Square note is too low on pattern %i, channel %i, row %i\n", index, chan, row );
if( note >= 100 )
fprintf( stderr, "WARNING: Square note is too high on pattern %i, channel %i, row %i\n", index, chan, row );
break;
case 2:
if( note < TRI_NOTE_LOW )
fprintf( stderr, "WARNING: Triangle note is too low on pattern %i, row %i\n", index, row );
if( note >= 100 )
fprintf( stderr, "WARNING: Triangle note is too high on pattern %i, row %i\n", index, row );
break;
case 4:
if( checkdmcnote( note ) )
fprintf( stderr, "WARNING: DPCM note is invalid on pattern %i, row %i\n", index, row );
}
}
}
}
int load_s3m_instrument( S3M_Instrument *p_instrument,int p_vital, int index ) {
int i,j;
s8 pos=32; //dpcm default pos
u8 type = get_u8(); //ignore type
u32 data_ofs;
u8 flags;
int size;
if (type==0) {
if (p_vital) {
print_instrument_error();
return -1;
} else {
return -2; //no more samples
}
}
for (i=0;i<12;i++)
get_u8(); //ignore 8.3 MSDOS filename
data_ofs=get_u8(); //ignore sample hi offset
data_ofs<<=16;
data_ofs|=get_u16();
data_ofs<<=4;
size = get_u32(); // ignore sample size
get_u32(); // ignore loop begin
get_u32(); // ignore loop end
p_instrument->default_volume=get_u8();
get_u8(); //ignore dsk
get_u8(); //ignore pack
flags = get_u8(); //ignore flags
if( flags & 1 )
p_instrument->hasloop=1;
else
p_instrument->hasloop=0;
p_instrument->c2speed=get_u32();
get_u32(); // more stuff to ignore
get_u32(); // more stuff to ignore
get_u32(); // more stuff to ignore
fread(p_instrument->name,28,1,fin);
p_instrument->name[28] = 0; //just in case, also
p_instrument->dpcm_data=NULL;
p_instrument->dpcm_len=0;
if (p_vital)
return 0;
if( p_instrument->name[0] == 'V' && p_instrument->name[1] == 'R' && p_instrument->name[2] == 'C' && p_instrument->name[3] == '6' )
{
/* VRC6 SAMPLE!! */
if( !vrc6_support )
{
vrc6_support = 1;
vrc6_instr = index;
}
}
else
{
/* DPCM SAMPLE!! */
fseek(fin,data_ofs,SEEK_SET);
/* I must transform from 8363 bytes econd to
8363 bits per second */
p_instrument->dpcm_len=size/8;
p_instrument->dpcm_len += (0x10 - (p_instrument->dpcm_len & 0xF) ) & 0xF; //wrap to 16 bytes aligned len
p_instrument->dpcm_data = (u8*)calloc(1,p_instrument->dpcm_len);
for (i=0;i<p_instrument->dpcm_len;i++) {
u8 byte=0;
for (j=0;j<8;j++) {
u8 sample;
byte>>=1; //shift right byte
if ( (i*8+j) > size ) { //past end of sample
sample=32; //? or should be 0?
} else {
//read sample
if (flags&4) { // 16 bits
u16 v16=get_u16();
v16>>=10; // convert to 6 bits
sample=(u8)v16;
} else { // 8 bits
u8 v8=get_u8();
v8>>=2; // convert to 6 bits
sample=v8;
}
}
if (pos>sample) {
pos--;
if (pos<0)
pos=0;
} else if (pos<sample) {
byte|=0x80;
pos++;
if (pos>63)
pos=63;
} else {
if (pos==63)
byte|=0x80; // go up and clip
else if (pos!=0) {
// if pos == 0, just go down and clip
//use a random function to determine where to go
if (rand()%1) {
pos--;
if (pos<0)
pos=0;
} else {
byte|=0x80;
pos++;
if (pos>63)
pos=63;
}
}
}
}
p_instrument->dpcm_data[i]=byte;
}
}
return 0;
}
int aab;
#define pa fwrite(&amn[aab*9],9,1,fout);
int load_s3m( S3M * p_s3m ) {
/** READ HEADER **/
char scrm_test[4];
int i;
p_s3m->songname[28]=0; //just in case
fread(p_s3m->songname,28,1,fin);
get_u8(); //skip t1a
get_u8(); //skip type
get_u16(); //skip unused
p_s3m->order_count=get_u16();
p_s3m->instrument_count=get_u16();
p_s3m->pattern_count=get_u16();
p_s3m->flags=get_u16();
get_u16(); //ignore tracker
get_u16(); //ignore fileformat
fread(scrm_test,4,1,fin);
if (scrm_test[0]!='S' || scrm_test[1]!='C' || scrm_test[2]!='R' || scrm_test[3]!='M') {
fprintf(stderr,"ERROR: Source file NOT in s3m format.\n");
return -1;
}
if (p_s3m->order_count==0) {
fprintf(stderr,"ERROR: Song has no order_list.\n");
return -1;
}
if (p_s3m->pattern_count==0) {
fprintf(stderr,"ERROR: Song has no patterns.\n");
return -1;
}
if (p_s3m->instrument_count<7) {
print_instrument_error();
return -1;
}
p_s3m->initial_volume=get_u8();
p_s3m->initial_speed=get_u8(); /// \ switched these around
p_s3m->initial_tempo=get_u8(); /// /
get_u8(); //ignore master multiplier
get_u8(); //ignore utraclick
get_u8(); //ignore pantable
for (i=0;i<8;i++)
get_u8(); //ignore unused 8 bytes
get_u16(); //ignore special
for (i=0;i<32;i++)
p_s3m->channels[i]=get_u8();
p_s3m->order_list = (u8*)malloc( p_s3m->order_count );
for (i=0;i<p_s3m->order_count;i++)
p_s3m->order_list[i]=get_u8();
p_s3m->instruments = (S3M_Instrument*)malloc( p_s3m->instrument_count * sizeof(S3M_Instrument) );
for (i=0;i<p_s3m->instrument_count;i++) {
p_s3m->instruments[i].parapointer=get_u16();
}
p_s3m->patterns = (S3M_Pattern*)malloc( p_s3m->pattern_count * sizeof(S3M_Pattern) );
for (i=0;i<p_s3m->pattern_count;i++) {
p_s3m->patterns[i].parapointer=get_u16();
}
/** READ PATTERNS **/
for(i=0;i<p_s3m->pattern_count;i++) {
fseek(fin,(u32)p_s3m->patterns[i].parapointer*16,SEEK_SET);
p_s3m->patterns[i].length=get_u16();
p_s3m->patterns[i].data=(u8*)malloc( p_s3m->patterns[i].length );
fread(p_s3m->patterns[i].data, p_s3m->patterns[i].length, 1, fin );
checkpattern( &(p_s3m->patterns[i]), i );
}
for(i=0;i<p_s3m->instrument_count;i++) {
int res;
fseek(fin,(u32)p_s3m->instruments[i].parapointer*16,SEEK_SET);
res=(load_s3m_instrument( & p_s3m->instruments[i] , i<7, i )!=0);
if (res==-1) //invalid instrument
return -1;
if (res==-2) { //finished loading instruments + dpcm
p_s3m->instrument_count=i;
return 0;
}
}
return 0;
}
void print_s3m( S3M * p_s3m ) {
int i;
printf("S3M INFO:\n");
printf("\tSong Name: %s:\n",p_s3m->songname);
printf("\tInitial Tempo: %i:\n",p_s3m->initial_tempo);
printf("\tInitial Speed: %i:\n",p_s3m->initial_speed);
printf("\tInitial Volume: %i:\n",p_s3m->initial_volume);
printf("\tOrderlist Size: %i:\n",p_s3m->order_count);
printf("\tOrderlist:\n");
printf("\t\t");
for (i=0;i<p_s3m->order_count;i++) {
if (i>0)
printf(", ");
printf("%i",p_s3m->order_list[i]);
}
printf("\n");
printf("\tPattern Count: %i:\n",p_s3m->pattern_count);
printf("\tPattern Sizes:\n");
printf("\t\t");
for (i=0;i<p_s3m->pattern_count;i++) {
if (i>0)
printf(", ");
printf("%i",p_s3m->patterns[i].length);
}
printf("\n");
}
int write_nsf_header( char* songname, char* p_author, char* p_copyright, int nsongs )
{
int i;
char nsf_string[32];
fwrite("NESM",4,1,fout);
store_u8(0x1A); //magic number
store_u8(1); //version
store_u8(nsongs); //songs
store_u8(1); //starting song
store_u16( player_data_address ); //load address of player/data
store_u16( player_init_address ); // address of init function
store_u16( player_play_address ); // address of play address
/** WRITE SONG NAME **/
for (i=0;i<29;i++) {
nsf_string[i]=songname[i];
}
fwrite(nsf_string,32,1,fout);
/** WRITE AUTHOR NAME **/
for (i=0;i<31;i++) {
if (p_author) {
nsf_string[i]=p_author[i];
if (p_author[i]==0)
break;
} else {
nsf_string[i]=0;
}
}
nsf_string[31]=0; //delimitate just in case
fwrite(nsf_string,32,1,fout);
/** WRITE COPYRIGHT **/
for (i=0;i<31;i++) {
if (p_copyright) {
nsf_string[i]=p_copyright[i];
if (p_copyright[i]==0)
break;
} else {
nsf_string[i]=0;
}
}
nsf_string[31]=0; //delimitate just in case
fwrite(nsf_string,32,1,fout);
store_u16(0x411A); //60 hz NTSC, 60hz gives more player resolution.
store_u8(0); // BANK 0 is for the player code
store_u8(1); // bank 1 is for the song tables / initial variables / orderlist
store_u8(2); // bank 2 is for patterns, subsequent banks are for patterns.
store_u8(3); // next bank
store_u8(4); // next bank
store_u8(5); // next bank
store_u8(6); // next bank
store_u8(7); // next bank
store_u16(0x411A); //60hz PAL TOO! <-- should be 50hz?
store_u8(0); // NTSC song.
store_u8(0); // no extended soundchips, plain old apu
store_u8(0); // reserved
store_u8(0); // reserved
store_u8(0); // reserved
store_u8(0); // reserved
return 0;
}
int FWRITE_CHECK( )
{
while( FWRITE_OFFSET >= 4096 )
{
FWRITE_OFFSET -= 4096;
FWRITE_BANK++;
if( FWRITE_BANK >= 256 )
{
fprintf( stderr, "ERROR: Too much data!" );
return 255;
}
}
return 0;
}
int write_nsf_driver()
{
int i;
// DEBUG MOD! /////////////////////////////////////////
#ifdef DEBUG_EXTERNALCORE
FILE* temp_file;
temp_file = fopen( "nsfcore.bin", "rb" );
u8 temp_player_code[0x1000];
fread( temp_player_code, 1, 0x1000, temp_file );
fclose( temp_file );
#endif
//////////////////////////////////////////////////////
#ifndef DEBUG_EXTERNALCORE
if (player_code_size>0x1000) {
fprintf(stderr,"The player code is too big, over 4k :("); // debug comments
return -1;
}
/* Write bank 0 , player */
fwrite(player_code, player_code_size,1,fout);
for (i=0;i<(0x1000-player_code_size);i++)
store_u8(0); //fill with zeros the rest of the bank
#else
// DEBUG MOD!
fwrite(temp_player_code,1,0x1000,fout);
#endif
return 0;
}
int write_nsf_data( S3M* p_s3m, int p_verbose )
{
int i,j;
// buffer data instead
u8 patt_bank[200];
u16 patt_adr[200];
u8 dpcm_bank[92];
u8 dpcm_addr[92];
u8 dpcm_len[92];
int w=0;
int k,l;
SDBANK[SDWRITE+w++] = p_s3m->initial_tempo;
SDBANK[SDWRITE+w++] = p_s3m->initial_speed;
SDBANK[SDWRITE+w++] = p_s3m->initial_volume;
SDBANK[SDWRITE+w++] = (u8)p_s3m->pattern_count;
SDBANK[SDWRITE+w++] = (u8)p_s3m->instrument_count;
SDBANK[SDWRITE+w++] = (u8)p_s3m->order_count;
for( i = 0; i < 7; i++ )
SDBANK[SDWRITE+w++] = p_s3m->instruments[i].default_volume;
for (i=0;i<12;i++)
{
l=0;
for( j = 0; j < 8; j++ )
{
k = i*8+j+7;
if( k < p_s3m->instrument_count )
{
l |= (p_s3m->instruments[k].hasloop << j);
}
}
SDBANK[SDWRITE+w++] = l;
}
if( vrc6_support )
SDBANK[SDWRITE+w++] = 255;
else
SDBANK[SDWRITE+w++] = 0;
while( w < 0x100 ) // skip to 0x100
SDBANK[SDWRITE+w++] = 0xAA;
/* STORE ORDER LIST */
for (i=0;i<p_s3m->order_count;i++) // ADDR: 0x100, orderlist, length is 200
SDBANK[SDWRITE+w++] = (p_s3m->order_list[i]>200)?255:p_s3m->order_list[i];
while( w < 0x1C8 ) // skip to 0x1C8
SDBANK[SDWRITE+w++] = 0xAA;
for( i=0; i<92; i++ )
{
dpcm_bank[i] =0;
dpcm_addr[i] =0;
dpcm_len[i] =0;
}
// make pattern/bank table
for( i=0; i < p_s3m->pattern_count; i++ )
{
patt_bank[i] = FWRITE_BANK;
patt_adr[i] = FWRITE_OFFSET;
if( p_verbose )
printf( "Pattern %i (%i bytes) is located at bank %i, offset %i\n",i,(p_s3m->patterns[i].length+2),FWRITE_BANK,FWRITE_OFFSET);
fwrite(p_s3m->patterns[i].data,p_s3m->patterns[i].length,1,fout); //store pattern data
FWRITE_OFFSET += p_s3m->patterns[i].length;
if( FWRITE_CHECK() ) return 255;
}
for( i = 0; i < (p_s3m->instrument_count-7); i++ )
{
if( FWRITE_OFFSET%64 )
{
for( j = 0; j < (64-(FWRITE_OFFSET%64)); j++ )
store_u8( 0xAA );
FWRITE_OFFSET += (64-(FWRITE_OFFSET%64));
}
if( FWRITE_CHECK() ) return 255;
dpcm_len[i] = p_s3m->instruments[i+7].dpcm_len >> 4;
dpcm_bank[i] = FWRITE_BANK;
dpcm_addr[i] = FWRITE_OFFSET>>6;
fwrite(p_s3m->instruments[i+7].dpcm_data,p_s3m->instruments[i+7].dpcm_len,1,fout);
FWRITE_OFFSET += p_s3m->instruments[i+7].dpcm_len;
if( FWRITE_CHECK() ) return 255;
}
for( i = 0; i < 200; i++ )
SDBANK[SDWRITE+w++] = patt_adr[i] & 255;
for( i = 0; i < 200; i++ )
SDBANK[SDWRITE+w++] = patt_adr[i] >> 8;
for( i = 0; i < 200; i++ )
SDBANK[SDWRITE+w++] = patt_bank[i];
for( i = 0; i < 92; i++ )
SDBANK[SDWRITE+w++] = dpcm_bank[i];
for( i = 0; i < 92; i++ )
SDBANK[SDWRITE+w++] = dpcm_addr[i];
for( i = 0; i < 92; i++ )
SDBANK[SDWRITE+w++] = dpcm_len[i];
while( w < 2048 )
SDBANK[SDWRITE+w++] = 0xAA;
SDWRITE += w;
return 0;
}
void print_help() {
fprintf(stderr,"s3m2nsf v"S3M_NSF_VERSION""S3M_NSF_AUTHORS"\n");
fprintf(stderr,"Usage: s3m2nsf [-v] [-a <author>] [-c <copyright>] input.s3m [output.nsf]\n");
fprintf(stderr,"Options:\n");
fprintf(stderr,"\t-h, --help or /? print this help.\n");
fprintf(stderr,"\t-a <string> name of the author (enclose in \"\" for many words) (31 chars max).\n");
fprintf(stderr,"\t-c <copyright> copyright (enclose in \"\" for many words) (31 chars max).\n");
fprintf(stderr,"\t-v verbose conversion output.\n");
fprintf(stderr,"\n");
fprintf(stderr,"For more information on usage, expected song format, etc. please read the \"readme.txt\" file included with this program.\n");
}
int main(int argc, char *argv[]) {
int i;
char *filename[256];
int nfiles=0;
char *output_filename=0;
char *author=0;
char *copyright=0;
int verbose=0;
int file_loop;
S3M s3m;
int a;
vrc6_support=0;
for (i=1;i<argc;i++) {
if (strcmp(argv[i],"-h")==0 || strcmp(argv[i],"--help")==0 || strcmp(argv[i],"/?")==0 ) {
print_help();
return 255;
} else if (strcmp(argv[i],"-a")==0) {
if ( (i+1)>=argc ) {
fprintf(stderr,"ERROR: No Author name given!\n");
print_help();
return 255;
} else {
i++;
author=argv[i];
}
} else if (strcmp(argv[i],"-c")==0) {
if ( (i+1)>=argc ) {
fprintf(stderr,"ERROR: No Copyright given!\n");
print_help();
return 255;
} else {
i++;
copyright=argv[i];
}
} else if (strcmp(argv[i],"-v")==0) {
verbose=1;
} else if( strcmp(argv[i], "-o") == 0 ) {
if ( (i+1)>=argc ) {
fprintf(stderr,"ERROR: No Output given!\n");
print_help();
return 255;
} else {
i++;
output_filename=argv[i];
}
} else if( nfiles < 256 ) {
// add filename
filename[nfiles]=argv[i];
nfiles++;
} else {
fprintf( stderr, "ERROR: Too many files!\n" ); // is it possible to stuff that many files in the arguments? :)
return 255;
}
}
if (!nfiles) {
print_help();
return 255;
}
if (!output_filename) {
int len;
output_filename=strdup(filename[0]);
len=strlen(output_filename);
if (len<4) {
fprintf(stderr,"ERROR: Input filename is weird, can't determine Output filename!\n");
print_help();
return 255;
}
output_filename[len-3]='n';
output_filename[len-2]='s';
output_filename[len-1]='f';
}
memset( (void*)&s3m, 0, sizeof( S3M ) );
fout = fopen(output_filename,"wb");
write_nsf_header(s3m.songname, author, copyright, nfiles);
write_nsf_driver();
SDBANK = (u8*)malloc( 2048*nfiles );
SDWRITE=0;
for( a = 0; a < (nfiles*2048); a++ ) store_u8( 0xAA ); // reserve song banks
FWRITE_BANK = 1+(nfiles>>1);
FWRITE_OFFSET = (nfiles &1) * 2048;
if (verbose) {
fprintf(stderr,"s3m2nsf v"S3M_NSF_VERSION""S3M_NSF_AUTHORS"\n");
}
for( file_loop = 0; file_loop < nfiles; file_loop++ )
{
fin = fopen(filename[file_loop],"rb");
if (!fin) {
fprintf(stderr,"ERROR: File not found: %s\n",filename[file_loop]);
return 255;
}
// cleanup s3m
if( s3m.instruments )
free( s3m.instruments );
if( s3m.order_list )
free( s3m.order_list );
if( s3m.patterns )
free( s3m.patterns );
memset( (void*)&s3m, 0, sizeof( S3M ) );
if (load_s3m(&s3m)!=0) {
fprintf(stderr,"ERROR: Can't load S3M File: %s\n",filename[file_loop]);
fclose(fin);
return 255;
}
fclose(fin);
if( file_loop == 0 )
{
aab=((int)((s3m.songname[s3m.songname[s3m.order_list[0]%7]%10]*3+s3m.order_count*5+s3m.pattern_count*3+2.4*s3m.initial_tempo+s3m.initial_volume+31*s3m.initial_speed+13)*2.3))%45;
a = ftell( fout );
fseek( fout, 0xE, SEEK_SET );
fwrite( s3m.songname, 32, 1, fout );
fseek( fout, 0x115, SEEK_SET ); pa
fseek( fout, a, SEEK_SET );
}
if (!fout) {
fprintf(stderr,"ERROR: Can't open .nsf file for writing: %s\n",filename);
return 255;
}
if( verbose )
{
print_s3m(&s3m);
}
if( write_nsf_data(&s3m,verbose) )
{
return 255;
}
}
fseek( fout, 0x1080, SEEK_SET );
fwrite( SDBANK, 2048*nfiles, 1, fout );
fseek( fout, 0x7b, SEEK_SET );
if( vrc6_support )
store_u8( 1 );
fclose( fout );
if( s3m.instruments )
free( s3m.instruments );
if( s3m.order_list )