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
|
unit Codegen;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Math, Instruments, Song, Utils,
HugeDatatypes, Constants, Dialogs, strutils, FileUtil, LazFileUtils, process;
type
{ EAssemblyException }
EAssemblyException = class(Exception)
public
ProgramName: String;
constructor Create(Prog, Msg: String);
end;
ECodegenRenameException = class(Exception);
TExportMode = (emNormal, emPreview, emGBS);
procedure AssembleSong(Song: TSong; Filename: string; Mode: TExportMode = emNormal);
procedure RenderSongToGBDKC(Song: TSong; DescriptorName: String; Filename: string; Bank: Integer = -1);
procedure RenderSongToRGBDSAsm(Song: TSong; DescriptorName: String; Filename: string);
implementation
uses AvgLvlTree;
type
TUsedStuff = record
HighestDutyInst, HighestWaveInst, HighestNoiseInst: Integer;
HighestWaveform: Integer;
UsedPatterns: TAvgLvlTree;
end;
function CompareIntPointers(Data1, Data2: Pointer): integer;
begin
Result := Integer(Data1^) - Integer(Data2^);
end;
function FindUsedStuff(const Song: TSong): TUsedStuff;
var
I, J: Integer;
Pat: PPattern;
Cell: TCell;
Instr: TInstrument;
InstValue: Integer;
Highest: ^Integer;
Waveform: Integer;
begin
Result.UsedPatterns := TAvgLvlTree.Create(@CompareIntPointers);
Result.HighestDutyInst := -1;
Result.HighestWaveInst := -1;
Result.HighestNoiseInst := -1;
Result.HighestWaveform := -1;
for Instr in Song.Instruments.Wave do begin
if not Instr.SubpatternEnabled then Continue;
for Cell in Instr.Subpattern do
if Cell.EffectCode = $9 then begin
Waveform := Cell.EffectParams.Value;
if InRange(Waveform, 0, 15) and (Waveform > Result.HighestWaveform) then
Result.HighestWaveform := Waveform;
end;
end;
for I := Low(Song.OrderMatrix) to High(Song.OrderMatrix) do begin
case I of
0, 1: Highest := @Result.HighestDutyInst;
2: Highest := @Result.HighestWaveInst;
3: Highest := @Result.HighestNoiseInst;
end;
for J := Low(Song.OrderMatrix[I]) to High(Song.OrderMatrix[I])-1 do begin
if Result.UsedPatterns.Find(@Song.OrderMatrix[I, J]) <> nil then Continue;
Result.UsedPatterns.Add(@Song.OrderMatrix[I, J]);
Pat := Song.Patterns.KeyData[Song.OrderMatrix[I, J]];
for Cell in Pat^ do begin
if (Cell.EffectCode = $9) and (I = 2) then begin // waveforms on wave channel
Waveform := Cell.EffectParams.Value;
if InRange(Waveform, 0, 15) and (Waveform > Result.HighestWaveform) then
Result.HighestWaveform := Waveform;
end;
if Cell.Instrument = 0 then Continue;
if InRange(Cell.Instrument, 0, 15) then
InstValue := Cell.Instrument
else
InstValue := 0;
if InstValue > Highest^ then
Highest^ := InstValue;
if (I = 2) then begin // Wave channel
Waveform := Song.Instruments.Wave[Cell.Instrument].Waveform;
if Waveform > Result.HighestWaveform then
Result.HighestWaveform := Waveform;
end;
end;
end;
end;
end;
procedure FreeUsedStuff(const UsedStuff: TUsedStuff);
begin
UsedStuff.UsedPatterns.Free;
end;
function PatternIsUsed(Pattern: Integer; const UsedStuff: TUsedStuff): Boolean;
begin
Result := UsedStuff.UsedPatterns.Find(@Pattern) <> nil;
end;
function InstrumentIsUsed(Instrument: Integer; Type_: TInstrumentType; const UsedStuff: TUsedStuff): Boolean;
begin
case Type_ of
itSquare: Result := Instrument <= UsedStuff.HighestDutyInst;
itWave: Result := Instrument <= UsedStuff.HighestWaveInst;
itNoise: Result := Instrument <= UsedStuff.HighestNoiseInst;
end;
end;
procedure RenderSongToGBDKC(Song: TSong; DescriptorName: String; Filename: string; Bank: Integer = -1);
function RenderGBDKCell(Cell: TCell): string;
var
SL: TStringList;
begin
SL := TStringList.Create;
SL.Delimiter := ',';
if (Cell.Note = NO_NOTE) or (NoteToCMap.IndexOf(Cell.Note) = -1) then
SL.Add('___')
else
SL.Add(NoteToCMap.KeyData[Cell.Note]);
if InRange(Cell.Instrument, 0, 15) then
SL.Add(IntToStr(Cell.Instrument))
else
SL.Add('0');
SL.Add('0x' + EffectCodeToStr(Cell.EffectCode, Cell.EffectParams));
Result := SL.DelimitedText;
SL.Free;
end;
function RenderGBDKSubpatternCell(Cell: TCell; Last: Boolean): string;
var
SL: TStringList;
begin
SL := TStringList.Create;
SL.Delimiter := ',';
if Cell.Note = NO_NOTE then
SL.Add('___')
else
SL.Add(IntToStr(Cell.Note));
if Last and (Cell.Volume = 0) then
SL.Add(IntToStr(1)) // Automatically insert jump back to row 1 if last cell
else
SL.Add(IntToStr(EnsureRange(Cell.Volume, 0, 32)));
SL.Add('0x' + EffectCodeToStr(Cell.EffectCode, Cell.EffectParams));
Result := SL.DelimitedText;
SL.Free;
end;
function RenderGBDKPattern(Name: string; Pat: TPattern): string;
var
Cell: TCell;
begin
Result := 'static const unsigned char ' + Name + '[] = {' + LineEnding;
for Cell in Pat do
Result += ' DN(' + RenderGBDKCell(Cell) + '),' + LineEnding;
Result += '};';
end;
function RenderGBDKSubpattern(Name: string; Pat: TPattern): string;
var
I: Integer;
begin
Result := 'static const unsigned char ' + Name + '[] = {' + LineEnding;
for I := 0 to 31 do
Result += ' DN(' + RenderGBDKSubpatternCell(Pat[I], I = 31) + '),' + LineEnding;
Result += '};';
end;
function RenderGBDKOrder(Number: integer; Order: array of integer): string;
var
SL: TStringList;
I: integer;
begin
SL := TStringList.Create;
SL.StrictDelimiter := True;
SL.Delimiter := ',';
for I := Low(Order) to High(Order)-1 do // HACK: account for off-by-one error
SL.Add('P' + IntToStr(Order[I]));
Result := 'static const unsigned char* const order' + IntToStr(Number) + '[] = {';
Result += SL.DelimitedText;
Result += '};';
SL.Free;
end;
function RenderGBDKInstrument(Instrument: TInstrument; Num: Integer): string;
var
SL: TStringList;
AsmInstrument: TAsmInstrument;
J: integer;
HighMask: byte;
TypePrefix: String;
begin
AsmInstrument := InstrumentToBytes(Instrument);
SL := TStringList.Create;
SL.StrictDelimiter := True;
SL.Delimiter := ',';
if Instrument.Type_ = itNoise then
begin
SL.Add(IntToStr(AsmInstrument[1])); // envelope
HighMask := AsmInstrument[0];
if Instrument.LengthEnabled then
HighMask := HighMask or %01000000;
if Instrument.CounterStep = swSeven then
HighMask := HighMask or %10000000;
SL.Add(IntToStr(HighMask));
end
else
for J := Low(AsmInstrument) to High(AsmInstrument) do
SL.Add(IntToStr(AsmInstrument[J]));
WriteStr(TypePrefix, Instrument.Type_);
if Instrument.SubpatternEnabled then
SL.Insert(SL.Count-1, Format('%sSP%d', [TypePrefix, Num]))
else
SL.Insert(SL.Count-1, '0');
if Instrument.Type_ = itNoise then begin
SL.Add('0');
SL.Add('0');
end;
Result := '{'+SL.DelimitedText+'}';
end;
function RenderGBDKInstrumentBank(Name: string; Bank: TInstrumentBank; Limit: Integer): string;
var
I: integer;
InstrType: String;
begin
case Bank[1].Type_ of
itSquare: InstrType := 'hUGEDutyInstr_t';
itWave: InstrType := 'hUGEWaveInstr_t';
itNoise: InstrType := 'hUGENoiseInstr_t';
end;
if Limit = -1 then
Exit('static const ' + InstrType + '* ' + Name + ' = NULL;'+LineEnding);
Result := 'static const ' + InstrType + ' ' + Name + '[] = {'+LineEnding;
for I := Low(Bank) to Limit do begin
Result += ' '+RenderGBDKInstrument(Bank[I], I) + ','+LineEnding;
end;
Result += '};';
end;
function RenderGBDKWaves(Waves: TWaveBank; Limit: Integer): string;
var
I, J: integer;
begin
if Limit = -1 then
Exit('static const unsigned char* waves = NULL;'+LineEnding);
Result := 'static const unsigned char waves[] = {'+LineEnding;
for I := Low(Waves) to Limit do
begin
Result += ' ';
J := Low(Waves[I]);
while J < High(Waves[I]) do
begin
Result += IntToStr((Waves[I, J] shl 4) or Waves[I, J + 1])+',';
Inc(J, 2);
end;
Result += LineEnding;
end;
Result += '};';
end;
var
OrderMatrix: TOrderMatrix;
OutSL: TStringList;
I: integer;
F: Text;
TypePrefix: String;
UsedStuff: TUsedStuff;
begin
Song := OptimizeSong(Song);
UsedStuff := FindUsedStuff(Song);
OutSL := TStringList.Create;
if Bank <> -1 then begin
OutSL.Add('#pragma bank '+IntToStr(Bank));
OutSL.Add('');
end;
OutSL.Add('#include "hUGEDriver.h"');
OutSL.Add('#include <stddef.h>');
OutSL.Add('');
for I := 0 to Song.Patterns.Count - 1 do
if PatternIsUsed(Song.Patterns.Keys[I], UsedStuff) then
OutSL.Add(RenderGBDKPattern('P' + IntToStr(Song.Patterns.Keys[I]),
Song.Patterns.Data[I]^));
OutSL.Add('');
for I := Low(Song.Instruments.All) to High(Song.Instruments.All) do
if InstrumentIsUsed(ModInst(I), Song.Instruments.All[I].Type_, UsedStuff) then
with Song.Instruments.All[I] do begin
if SubpatternEnabled then begin
WriteStr(TypePrefix, Type_);
OutSL.Add(RenderGBDKSubpattern(TypePrefix+'SP' + IntToStr(ModInst(I)), Subpattern));
end;
end;
OutSL.Add(RenderGBDKOrder(1, Song.OrderMatrix[0]));
OutSL.Add(RenderGBDKOrder(2, Song.OrderMatrix[1]));
OutSL.Add(RenderGBDKOrder(3, Song.OrderMatrix[2]));
OutSL.Add(RenderGBDKOrder(4, Song.OrderMatrix[3]));
OutSL.Add('');
OutSL.Add(RenderGBDKInstrumentBank('duty_instruments', Song.Instruments.Duty, UsedStuff.HighestDutyInst));
OutSL.Add(RenderGBDKInstrumentBank('wave_instruments', Song.Instruments.Wave, UsedStuff.HighestWaveInst));
OutSL.Add(RenderGBDKInstrumentBank('noise_instruments', Song.Instruments.Noise, UsedStuff.HighestNoiseInst));
OutSL.Add('');
OutSL.Add(RenderGBDKWaves(Song.Waves, UsedStuff.HighestWaveform));
OutSL.Add('');
if Bank <> -1 then
OutSL.Add(Format('const void __at(%d) __bank_%s;', [Bank, DescriptorName]));
OutSL.Add(Format(
'const hUGESong_t %s = {%d, %d, %d, %d, %d, order1, order2, order3,'+
'order4, duty_instruments, wave_instruments, noise_instruments, NULL, waves};',
[DescriptorName,
Song.TicksPerRow[0], Song.TicksPerRow[1], Song.TicksPerRow[2], Song.TicksPerRow[3],
OrderCount(Song)*2
]));
AssignFile(F, Filename);
Rewrite(F);
Write(F, OutSL.Text);
CloseFile(F);
OutSL.Free;
FreeUsedStuff(UsedStuff);
end;
function RenderOrderTable(OrderMatrix: TOrderMatrix): string;
function ArrayHelper(Ints: array of integer): string;
var
I: integer;
SL: TStringList;
begin
SL := TStringList.Create;
SL.StrictDelimiter := True;
SL.Delimiter := ',';
for I := Low(Ints) to High(Ints)-1 do // HACK: account for the off-by-one error
SL.Add('P' + IntToStr(Ints[I]));
Result := SL.DelimitedText;
SL.Free;
end;
var
Res: TStringList;
begin
Res := TStringList.Create;
Res.Add('order1: dw ' + ArrayHelper(OrderMatrix[0]));
Res.Add('order2: dw ' + ArrayHelper(OrderMatrix[1]));
Res.Add('order3: dw ' + ArrayHelper(OrderMatrix[2]));
Res.Add('order4: dw ' + ArrayHelper(OrderMatrix[3]));
Result := Res.Text;
Res.Free;
end;
function RenderInstruments(Instruments: TInstrumentBank; Limit: Integer): string;
var
ResultSL: TStringList;
AsmInstrument: TAsmInstrument;
I, J: integer;
TypePrefix: string;
HighMask: byte;
begin
ResultSL := TStringList.Create;
for I := Low(Instruments) to Limit do
begin
WriteStr(TypePrefix, Instruments[I].Type_);
ResultSL.Add(Format('%s%s:', [TypePrefix, 'inst' + IntToStr(I)]));
AsmInstrument := InstrumentToBytes(Instruments[I]);
if Instruments[I].Type_ = itNoise then
begin
ResultSL.Add('db '+IntToStr(AsmInstrument[1])); // envelope
HighMask := AsmInstrument[0];
if Instruments[I].LengthEnabled then
HighMask := HighMask or %01000000;
if Instruments[I].CounterStep = swSeven then
HighMask := HighMask or %10000000;
ResultSL.Add('db '+IntToStr(HighMask));
end
else begin
for J := Low(AsmInstrument) to High(AsmInstrument) do
ResultSL.Add('db '+IntToStr(AsmInstrument[J]));
end;
if Instruments[I].SubpatternEnabled then
ResultSL.Insert(ResultSL.Count-1, Format('dw %sSP%d', [TypePrefix, I]))
else
ResultSL.Insert(ResultSL.Count-1, 'dw 0');
if Instruments[I].Type_ = itNoise then
ResultSL.Add('ds 2');
ResultSL.Add('');
end;
Result := ResultSL.Text;
ResultSL.Free;
end;
function RenderCell(Cell: TCell): string;
var
SL: TStringList;
begin
SL := TStringList.Create;
SL.Delimiter := ',';
SL.StrictDelimiter := True;
if (Cell.Note = NO_NOTE) or (NoteToDriverMap.IndexOf(Cell.Note) = -1) then
SL.Add('___')
else
SL.Add(NoteToDriverMap.KeyData[Cell.Note]);
if InRange(Cell.Instrument, 0, 15) then
SL.Add(IntToStr(Cell.Instrument))
else
SL.Add('0');
SL.Add('$' + EffectCodeToStr(Cell.EffectCode, Cell.EffectParams));
// RGBDS thinks you're defining a new macro if you don't have a space first.
Result := ' dn ' + SL.DelimitedText;
SL.Free;
end;
function RenderSubpatternCell(Cell: TCell; Last: Boolean): string;
var
SL: TStringList;
begin
SL := TStringList.Create;
SL.Delimiter := ',';
SL.StrictDelimiter := True;
if (Cell.Note = NO_NOTE) then
SL.Add('___')
else
SL.Add(IntToStr(Cell.Note));
if Last and (Cell.Volume = 0) then
SL.Add(IntToStr(1)) // Automatically insert jump back to row 1 if last cell
else
SL.Add(IntToStr(EnsureRange(Cell.Volume, 0, 32)));
SL.Add('$' + EffectCodeToStr(Cell.EffectCode, Cell.EffectParams));
// RGBDS thinks you're defining a new macro if you don't have a space first.
Result := ' dn ' + SL.DelimitedText;
SL.Free;
end;
function RenderPattern(Name: string; Pattern: TPattern): string;
var
SL: TStringList;
I: integer;
begin
SL := TStringList.Create;
SL.Add(Name + ':');
for I := Low(TPattern) to High(TPattern) do
SL.Add(RenderCell(Pattern[I]));
Result := SL.Text;
SL.Free;
end;
function RenderSubpattern(Name: string; Pattern: TPattern): string;
var
SL: TStringList;
I: integer;
begin
SL := TStringList.Create;
SL.Add(Name + ':');
for I := 0 to 31 do
SL.Add(RenderSubpatternCell(Pattern[I], I = 31)); // TODO: hardcoded value
Result := SL.Text;
SL.Free;
end;
function RenderWaveforms(Waves: TWaveBank; Limit: Integer): string;
var
SL, ResultSL: TStringList;
I, J: integer;
begin
ResultSL := TStringList.Create;
for I := Low(Waves) to Limit do
begin
SL := TStringList.Create;
SL.StrictDelimiter := True;
SL.Delimiter := ',';
J := Low(Waves[I]);
while J < High(Waves[I]) do
begin
SL.Add(IntToStr((Waves[I, J] shl 4) or Waves[I, J + 1]));
Inc(J, 2);
end;
ResultSL.Add(Format('wave%d: db %s', [I, SL.DelimitedText]));
SL.Free;
end;
Result := ResultSL.Text;
ResultSL.Free;
end;
procedure RenderSongToRGBDSAsm(Song: TSong; DescriptorName: String; Filename: string);
var
OutSL: TStringList;
F: Text;
I: Integer;
TypePrefix: String;
UsedStuff: TUsedStuff;
begin
Song := OptimizeSong(Song);
UsedStuff := FindUsedStuff(Song);
OutSL := TStringList.Create;
OutSL.Add('include "hUGE.inc"');
OutSL.Add('');
OutSL.Add('SECTION "'+DescriptorName+' Song Data", ROMX');
OutSL.Add('');
// Render song descriptor
OutSL.Add(DescriptorName+'::');
OutSL.Add('db '+IntToStr(Song.TicksPerRow[0])+', '
+IntToStr(Song.TicksPerRow[1])+', '
+IntToStr(Song.TicksPerRow[2])+', '
+IntToStr(Song.TicksPerRow[3]));
OutSL.Add('db '+IntToStr(OrderCount(Song)*2));
OutSL.Add('dw order1, order2, order3, order4');
OutSL.Add('dw duty_instruments, wave_instruments, noise_instruments');
OutSL.Add('dw routines');
OutSL.Add('dw waves');
OutSL.Add('');
// Render order matrix
OutSL.Add(RenderOrderTable(Song.OrderMatrix));
// Render instruments
OutSL.Add('duty_instruments:');
OutSL.Add(RenderInstruments(Song.Instruments.Duty, UsedStuff.HighestDutyInst));
OutSL.Add('');
OutSL.Add('wave_instruments:');
OutSL.Add(RenderInstruments(Song.Instruments.Wave, UsedStuff.HighestWaveInst));
OutSL.Add('');
OutSL.Add('noise_instruments:');
OutSL.Add(RenderInstruments(Song.Instruments.Noise, UsedStuff.HighestNoiseInst));
OutSL.Add('');
// Render routines
OutSL.Add('routines:');
for I := Low(TRoutineBank) to High(TRoutineBank) do begin
OutSL.Add('__hUGE_Routine_'+IntToStr(I)+':');
OutSL.Add(Song.Routines[I]);
OutSL.Add('__end_hUGE_Routine_'+IntToStr(I)+':');
OutSL.Add('ret');
OutSL.Add('');
end;
// Render waves
OutSL.Add('waves:');
OutSL.Add(RenderWaveforms(Song.Waves, UsedStuff.HighestWaveform));
// Render patterns
for I := 0 to Song.Patterns.Count - 1 do
if PatternIsUsed(Song.Patterns.Keys[I], UsedStuff) then
OutSL.Add(RenderPattern('P' + IntToStr(Song.Patterns.Keys[I]), Song.Patterns.Data[I]^));
// Render subpatterns
for I := Low(Song.Instruments.All) to High(Song.Instruments.All) do
if InstrumentIsUsed(ModInst(I), Song.Instruments.All[I].Type_, UsedStuff) then
with Song.Instruments.All[I] do begin
if SubpatternEnabled then begin
WriteStr(TypePrefix, Type_);
OutSL.Add(RenderSubpattern(TypePrefix+'SP' + IntToStr(ModInst(I)), Subpattern));
end;
end;
AssignFile(F, Filename);
Rewrite(F);
Write(F, OutSL.Text);
CloseFile(F);
OutSL.Free;
FreeUsedStuff(UsedStuff);
end;
procedure RectifyGBSFile(GBSFile: string);
{ This procedure removes the useless $400 bytes located after the GBS header.
Since RGBDS has no feature to simply offset a section of code, we have to
manually introduce padding and then remove it with this silly routine. }
var
Stream: TFileStream;
Buffer: array of byte;
begin
Stream := TFileStream.Create(GBSFile, fmOpenRead);
SetLength(Buffer, Stream.Size - $400);
Stream.Read(Buffer[0], $70);
Stream.Seek($400, soCurrent); // Skip the empty byte padding
Stream.Read(Buffer[$70], Stream.Size - Stream.Position);
Stream.Free;
Stream := TFileStream.Create(GBSFile, fmOpenWrite);
Stream.WriteBuffer(Buffer[0], Length(Buffer));
Stream.Free;
end;
procedure AssembleSong(Song: TSong; Filename: string; Mode: TExportMode);
var
OutFile: Text;
I: integer;
TypePrefix: String;
Proc: TProcess;
FilePath: string;
RenameSucceeded: Boolean;
UsedStuff: TUsedStuff;
procedure Die;
var
OutSL: TStringList;
begin
OutSL := TStringList.Create;
try
OutSL.LoadFromStream(Proc.Output);
raise EAssemblyException.Create(Proc.Executable, OutSL.Text);
finally
OutSL.Free;
end;
end;
procedure WriteHTT(F: string; S: string);
begin
AssignFile(OutFile, F);
Rewrite(OutFile);
Write(OutFile, S);
CloseFile(OutFile);
end;
function Assemble(OutFile: string; InFile: string; Defines: array of string): integer;
var
Define: string;
begin
Proc.Executable := 'rgbasm';
Proc.Parameters.Clear;
Proc.Parameters.Add('--include');
Proc.Parameters.Add(ConcatPaths([CacheDir, 'render']));
Proc.Parameters.Add('--include');
Proc.Parameters.Add(ConcatPaths([RuntimeDir, 'hUGEDriver']));
Proc.Parameters.Add('-o' + OutFile);
for Define in Defines do
// HACK: This just removes all instances of double quotes to avoid a bug
// where arguments don't get passed in right with double quotes.
Proc.Parameters.Add('-D' + ReplaceStr(Define, '"', ''));
Proc.Parameters.Add(InFile);
Proc.Execute;
Result := Proc.ExitStatus;
end;
function Link(OutFile: string; InFiles: array of string; Map: string = '';
Sym: string = ''): integer;
var
InFile: string;
begin
Proc.Executable := 'rgblink';
Proc.Parameters.Clear;
Proc.Parameters.Add('-o' + OutFile);
if Map <> '' then
Proc.Parameters.Add('-m' + Map);
if Sym <> '' then
Proc.Parameters.Add('-n' + Sym);
for InFile in InFiles do
Proc.Parameters.Add(InFile);
Proc.Execute;
Result := Proc.ExitStatus;
end;
function Fix(GBFile: string): integer;
begin
Proc.Executable := 'rgbfix';
Proc.Parameters.Clear;
Proc.Parameters.Add('-p0');
Proc.Parameters.Add('-v');
Proc.Parameters.Add(GBFile);
Proc.Execute;
Result := Proc.ExitStatus;
end;
begin
Song := OptimizeSong(Song);
UsedStuff := FindUsedStuff(Song);
if not DirectoryExists(ConcatPaths([CacheDir, 'render'])) then
CreateDir(ConcatPaths([CacheDir, 'render']));
FilePath := Filename;
Filename := ConcatPaths([CacheDir, 'render', ExtractFileNameWithoutExt(ExtractFileNameOnly(Filename))]);
WriteHTT(ConcatPaths([CacheDir, 'render', 'wave.htt']), RenderWaveforms(Song.Waves, UsedStuff.HighestWaveform));
WriteHTT(ConcatPaths([CacheDir, 'render', 'order.htt']), RenderOrderTable(Song.OrderMatrix));
WriteHTT(ConcatPaths([CacheDir, 'render', 'duty_instrument.htt']), RenderInstruments(Song.Instruments.Duty, UsedStuff.HighestDutyInst));
WriteHTT(ConcatPaths([CacheDir, 'render', 'wave_instrument.htt']), RenderInstruments(Song.Instruments.Wave, UsedStuff.HighestWaveInst));
WriteHTT(ConcatPaths([CacheDir, 'render', 'noise_instrument.htt']), RenderInstruments(Song.Instruments.Noise, UsedStuff.HighestNoiseInst));
for I := Low(TRoutineBank) to High(TRoutineBank) do
WriteHTT(ConcatPaths([CacheDir, 'render', 'routine'+IntToStr(I)+'.htt']), Song.Routines[I]);
AssignFile(OutFile, ConcatPaths([CacheDir, 'render', 'pattern.htt']));
Rewrite(OutFile);
for I := 0 to Song.Patterns.Count - 1 do
if PatternIsUsed(Song.Patterns.Keys[I], UsedStuff) then
Write(OutFile, RenderPattern('P' + IntToStr(Song.Patterns.Keys[I]),
Song.Patterns.Data[I]^));
CloseFile(OutFile);
AssignFile(OutFile, ConcatPaths([CacheDir, 'render', 'subpattern.htt']));
Rewrite(OutFile);
for I := Low(Song.Instruments.All) to High(Song.Instruments.All) do
if InstrumentIsUsed(ModInst(I), Song.Instruments.All[I].Type_, UsedStuff) then
with Song.Instruments.All[I] do begin
if SubpatternEnabled then begin
WriteStr(TypePrefix, Type_);
Write(OutFile, RenderSubpattern(TypePrefix+'SP' + IntToStr(ModInst(I)), Subpattern));
end;
end;
CloseFile(OutFile);
// Build the file
Proc := TProcess.Create(nil);
Proc.Options := Proc.Options + [poWaitOnExit, poUsePipes, poStdErrToOutput, poNoConsole];
try
// Assemble
if Mode = emPreview then
begin
if Assemble(Filename + '_driver.obj',
ConcatPaths([RuntimeDir, 'hUGEDriver', 'hUGEDriver.asm']),
['PREVIEW_MODE']) <> 0 then Die;
end
else if Assemble(Filename + '_driver.obj', ConcatPaths([RuntimeDir, 'hUGEDriver', 'hUGEDriver.asm']), []) <> 0 then Die;
if Assemble(Filename + '_song.obj',
ConcatPaths([RuntimeDir, 'hUGEDriver', 'song.asm']),
['SONG_DESCRIPTOR=song',
'ORDER_COUNT='+IntToStr(OrderCount(Song)*2),
'TICKS0='+IntToStr(Song.TicksPerRow[0]),
'TICKS1='+IntToStr(Song.TicksPerRow[1]),
'TICKS2='+IntToStr(Song.TicksPerRow[2]),
'TICKS3='+IntToStr(Song.TicksPerRow[3])]) <> 0 then Die;
if Mode = emGBS then
begin
if Assemble(Filename + '_gbs.obj',
ConcatPaths([RuntimeDir, 'hUGEDriver', 'gbs.asm']),
['SONG_DESCRIPTOR=song',
'GBS_TITLE="'+PadRight(LeftStr(Song.Name, 32), 32)+'"',
'GBS_AUTHOR="'+PadRight(LeftStr(Song.Artist, 32), 32)+'"',
'GBS_COPYRIGHT="'+PadRight(IntToStr(CurrentYear), 32)+'"',
'TIMER_MODULO='+IntToStr(IfThen(Song.TimerEnabled, Song.TimerDivider, 0)),
'TIMER_CONTROL='+IntToStr(IfThen(Song.TimerEnabled, 4, 0))]) <> 0 then Die;
end
else if Assemble(Filename + '_player.obj',
ConcatPaths([RuntimeDir, 'hUGEDriver', 'player.asm']),
['SONG_DESCRIPTOR=song',
IfThen(Song.TimerEnabled, 'USE_TIMER=1', ''),
'TIMER_MODULO='+IntToStr(Song.TimerDivider)]) <> 0 then Die;
// Link
if Mode = emGBS then
begin
if Link(Filename + '.gbs',
[Filename + '_driver.obj',
Filename + '_song.obj',
Filename + '_gbs.obj']) <> 0 then Die;
end
else
begin
if Link(Filename + '.gb',
[Filename + '_driver.obj',
Filename + '_song.obj',
Filename + '_player.obj'],
Filename + '.map',
Filename + '.sym') <> 0 then Die;
end;
// Fix
if Mode = emGBS then
RectifyGBSFile(Filename + '.gbs')
else
begin
if (Fix(Filename + '.gb') <> 0) then
Die;
end;
// Move to destination
if Mode <> emPreview then
begin
if FileExists(FilePath) then
DeleteFile(FilePath);
case Mode of
emGBS: RenameSucceeded := RenameFile(Filename + '.gbs', FilePath);
else RenameSucceeded := RenameFile(Filename + '.gb', FilePath);
end;
if not RenameSucceeded then
raise ECodegenRenameException.Create(FilePath);
{$ifdef DEVELOPMENT}
RenameFile(Filename + '.sym', FilePath + '.sym');
RenameFile(Filename + '.map', FilePath + '.map');
{$endif}
{$ifdef PRODUCTION}
DeleteFile(Filename + '.sym');
DeleteFile(Filename + '.map');
{$endif}
DeleteFile(Filename + '_driver.obj');
DeleteFile(Filename + '_song.obj');
DeleteFile(Filename + '_player.obj');
DeleteFile(Filename + '_gbs.obj');
end;
finally
Proc.Free;
FreeUsedStuff(UsedStuff);
end;
end;
{ EAssemblyException }
constructor EAssemblyException.Create(Prog, Msg: String);
begin
inherited Create(Msg);
Self.ProgramName := Prog;
end;
end.
|