/
AYMaker10
539 lines (522 loc) · 14.8 KB
/
AYMaker10
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
{
AY Make Russian Version.
(c)2001 Bulba S.V.
You can use this source as you want without any reference to author.
Originally compiled in Delphi 5.
Sergey Bulba, vorobey@mail.khstu.ru, http://bulba.at.kz/
}
program AYMakeR;
{$APPTYPE CONSOLE}
uses SysUtils;
type
TAYFileHeader = packed record
FileID,TypeID:longword;
FileVersion,PlayerVersion:byte;
PSpecialPlayer,PAuthor,PMisc:smallint;
NumOfSongs,FirstSong:byte;
PSongsStructure:smallint;
end;
TSongStructure = packed record
PSongName,PSongData:smallint;
end;
TSongData = packed record
ChanA,ChanB,ChanC,Noise:byte;
SongLength:word;
FadeLength:word;
HiReg,LoReg:byte;
PPoints,PAddresses:smallint;
end;
TPoints = packed record
Stek,Init,Inter:word;
end;
TAddresses = packed record
Addr,Leng:word;
Offs:smallint;
end;
var
AYFileHeader:TAYFileHeader;
SongStructure:packed array of TSongStructure;
SongData:packed array of TSongData;
Points:packed array of TPoints;
BlockFiles:array of string;
Strings:array of record
Value:string;
Used:boolean;
end;
Ripper:string;
SongModel:array of record
Name:integer;
BlockFile:array of record
Number:integer;
Coords:TAddresses;
end;
end;
Ini:text;
WorkString:string;
LineCounter,SongCounter,StringCounter,BlockFileCounter:integer;
AuthorSt,MiscSt:integer;
const
NTegs = 16;
Tegs:array [0..NTegs - 1] of string =
('author','misc','ripper','name','firstsong','stack','hireg','loreg',
'codeaddress','codename','interrupt','init','fileversion',
'playerversion','songlength','fadelength');
function NewString(ns:string):integer;
var
Index:integer;
begin
for Index := 0 to StringCounter - 1 do
if Strings[Index].Value = ns then
begin
Result := Index;
exit
end;
Result := StringCounter;
inc(StringCounter);
SetLength(Strings,StringCounter);
Strings[Result].Value := ns;
Strings[Result].Used := False
end;
function NewBlockFile(ns:string):integer;
var
Index:integer;
begin
for Index := 0 to BlockFileCounter - 1 do
if BlockFiles[Index] = ns then
begin
Result := Index;
exit
end;
Result := BlockFileCounter;
inc(BlockFileCounter);
SetLength(BlockFiles,BlockFileCounter);
BlockFiles[Result] := ns
end;
procedure ExitIniError(EN:integer);
const
Errors:array[0..7] of string =
('Bad line structure','Unknown key word','Song key words without "NewSong"',
'Can not evalute numeric value','"CodeAddress" without "CodeName"',
'"CodeAddress" can not be zero','Value out of range',
'Can not evalute time value');
begin
Close(Ini);
Writeln('Error in line number ' + IntToStr(LineCounter) + ': '+ Errors[EN]);
Halt
end;
function FindTegNumber(t:string):integer;
var
i:integer;
begin
Result := -1;
i := 0;
while i < NTegs do
begin
if Tegs[i] = t then
begin
Result := i;
exit
end;
inc(i)
end;
end;
function MyStrToInt(s:string;sz:integer):longword;
var
ErCd,i:integer;
begin
Val(S,Result,ErCd);
if ErCd <> 0 then
if S[1] = '#' then
begin
if S = '#' then ExitIniError(3);
s := UpperCase(s);
i := 2;
Result := 0;
while i <= Length(s) do
if s[i] in ['0'..'9'] then
begin
Result := Result * 16 + byte(s[i]) - $30;
inc(i)
end
else if s[i] in ['A'..'F'] then
begin
Result := Result * 16 + byte(s[i]) - byte('A') + 10;
inc(i)
end
else
ExitIniError(3)
end
else
ExitIniError(3);
if ((sz = 1) and (Result > 255)) or
((sz = 2) and (Result > 65535)) then
ExitIniError(6)
end;
function MyTimeToInt(s:string):longword;
var
DotsPos:integer;
Mins,ErrPos:integer;
Secs:real;
begin
DotsPos := Pos(':',s);
if DotsPos = 0 then
begin
Result := MyStrToInt(s,2);
exit
end;
if (DotsPos = 1) or (DotsPos = Length(s)) then ExitIniError(7);
Val(Copy(s,1,DotsPos - 1),Mins,ErrPos);
if ErrPos <> 0 then ExitIniError(7);
Val(Copy(s,DotsPos + 1,Length(s)),Secs,ErrPos);
if ErrPos <> 0 then ExitIniError(7);
Result := round((Mins * 60 + Secs) * 50);
if Result > 65535 then ExitIniError(6)
end;
procedure RecognizeString(ws:string);
var
Teg,Parameter:string;
EqPos,Index,TegNumber:integer;
begin
ws := Trim(ws);
if ws = '' then exit;
EqPos := Pos('=',ws);
if EqPos = 0 then
if LowerCase(ws) = 'newsong' then
begin
Index := SongCounter;
inc(SongCounter);
SetLength(SongModel,SongCounter);
SetLength(SongStructure,SongCounter);
SetLength(SongData,SongCounter);
SetLength(Points,SongCounter);
SongModel[Index].Name := 0;
SetLength(SongModel[Index].BlockFile,0);
SongData[Index].ChanA := 0;
SongData[Index].ChanB := 1;
SongData[Index].ChanC := 2;
SongData[Index].Noise := 3;
SongData[Index].SongLength := 0;
SongData[Index].FadeLength := 0;
SongData[Index].HiReg := 0;
SongData[Index].LoReg := 0;
Points[Index].Stek := 0;
Points[Index].Init := 0;
Points[Index].Inter := 0;
exit
end;
if (EqPos < 2) or (EqPos = Length(ws)) then ExitIniError(0);
Teg := LowerCase(Trim(Copy(ws,1,EqPos - 1)));
Parameter := Trim(Copy(ws,EqPos + 1,Length(ws)));
TegNumber := FindTegNumber(Teg);
case TegNumber of
-1:ExitIniError(1);
0 :AuthorSt := NewString(Parameter);
1 :MiscSt := NewString(Parameter);
2 :Ripper := Parameter;
3 :if SongCounter = 0 then
ExitIniError(2)
else
SongModel[SongCounter - 1].Name := NewString(Parameter);
4 :AYFileHeader.FirstSong := MyStrToInt(Parameter,1);
5 :if SongCounter = 0 then
ExitIniError(2)
else
Points[SongCounter - 1].Stek := MyStrToInt(Parameter,2);
6 :if SongCounter = 0 then
ExitIniError(2)
else
SongData[SongCounter - 1].HiReg := MyStrToInt(Parameter,1);
7 :if SongCounter = 0 then
ExitIniError(2)
else
SongData[SongCounter - 1].LoReg := MyStrToInt(Parameter,1);
8 :if SongCounter = 0 then
ExitIniError(2)
else
begin
Index := Length(SongModel[SongCounter - 1].BlockFile);
if Index = 0 then
ExitIniError(4)
else
begin
EqPos := MyStrToInt(Parameter,2);
if EqPos = 0 then ExitIniError(5);
SongModel[SongCounter - 1].BlockFile[Index - 1].Coords.Addr := EqPos
end;
end;
9 :if SongCounter = 0 then
ExitIniError(2)
else
begin
Index := Length(SongModel[SongCounter - 1].BlockFile);
SetLength(SongModel[SongCounter - 1].BlockFile,Index + 1);
SongModel[SongCounter - 1].BlockFile[Index].Number :=
NewBlockFile(Parameter);
SongModel[SongCounter - 1].BlockFile[Index].Coords.Addr := 0
end;
10:if SongCounter = 0 then
ExitIniError(2)
else
Points[SongCounter - 1].Inter := MyStrToInt(Parameter,2);
11:if SongCounter = 0 then
ExitIniError(2)
else
Points[SongCounter - 1].Init := MyStrToInt(Parameter,2);
12:AYFileHeader.FileVersion := MyStrToInt(Parameter,1);
13:AYFileHeader.PlayerVersion := MyStrToInt(Parameter,1);
14:if SongCounter = 0 then
ExitIniError(2)
else
begin
SongData[SongCounter - 1].SongLength := MyTimeToInt(Parameter);
if SongData[SongCounter - 1].SongLength = 0 then
ExitIniError(6)
end;
15:if SongCounter = 0 then
ExitIniError(2)
else
SongData[SongCounter - 1].FadeLength := MyTimeToInt(Parameter);
end;
end;
procedure CheckAll;
var
i,j:integer;
begin
AYFileHeader.NumOfSongs := SongCounter - 1;
if AYFileHeader.FirstSong >= SongCounter then
begin
Writeln('"FirstSong" parameter must be in range from 0 '
+ 'to number of song minus 1');
halt
end;
if BlockFileCounter = 0 then
begin
Writeln('No any files was defined');
halt
end;
for i := 0 to SongCounter - 1 do
begin
if Length(SongModel[i].BlockFile) = 0 then
begin
Writeln('No files was defined for song number ' + IntToStr(i) + '.');
halt
end;
if SongData[i].SongLength = 0 then
begin
Writeln('"SongLength" was not defined for song number ' +
IntToStr(i) + '.');
halt
end;
for j := 0 to Length(SongModel[i].BlockFile) - 1 do
if SongModel[i].BlockFile[j].Coords.Addr = 0 then
begin
Writeln('"CodeAddress" not defined for song number ' +
IntToStr(i) + '.');
halt
end
end;
end;
function IntelWord(a:word):word;
asm
xchg ah,al
end;
procedure ExitError(EN:integer);
const
Errors:array[0..3] of string =
('Relative offset out of range',
'One of block files not found or can not be open',
'One of block file size greater than 65535 bytes',
'"CodeAddress" plus its code length greater then 65536');
begin
Writeln('Error: '+ Errors[EN]);
Halt
end;
procedure NormalizeAll;
var
CurrentOffset,RO,i,j:integer;
StOfs,Lens:array of integer;
BlockFile:file;
begin
AYFileHeader.PSongsStructure := $200;
CurrentOffset := SizeOf(TAYFileHeader) + SizeOf(TSongStructure) * SongCounter;
for i := 0 to SongCounter - 1 do
begin
RO := CurrentOffset - (SizeOf(TAYFileHeader) +
SizeOf(TSongStructure) * i + 2);
if RO > 32767 then ExitError(0);
SongStructure[i].PSongData := IntelWord(RO);
inc(CurrentOffset,SizeOf(TSongData))
end;
for i := 0 to SongCounter - 1 do
begin
RO := CurrentOffset - (SizeOf(TAYFileHeader) + SizeOf(TSongStructure) *
SongCounter + SizeOf(TSongData) * i + 10);
if RO > 32767 then ExitError(0);
SongData[i].PPoints := IntelWord(RO);
inc(CurrentOffset,SizeOf(TPoints))
end;
for i := 0 to SongCounter - 1 do
begin
RO := CurrentOffset - (SizeOf(TAYFileHeader) + SizeOf(TSongStructure) *
SongCounter + SizeOf(TSongData) * i + 12);
if RO > 32767 then ExitError(0);
SongData[i].PAddresses := IntelWord(RO);
inc(CurrentOffset,SizeOf(TAddresses) * Length(SongModel[i].BlockFile) + 2)
end;
Strings[AuthorSt].Used := True;
Strings[MiscSt].Used := True;
for i := 0 to SongCounter - 1 do
Strings[SongModel[i].Name].Used := True;
SetLength(StOfs,StringCounter);
for i := 0 to StringCounter - 1 do
if Strings[i].Used then
begin
StOfs[i] := CurrentOffset;
inc(CurrentOffset,Length(Strings[i].Value) + 1)
end;
RO := StOfs[AuthorSt] - 12;
if RO > 32767 then ExitError(0);
AYFileHeader.PAuthor := IntelWord(RO);
RO := StOfs[MiscSt] - 14;
if RO > 32767 then ExitError(0);
AYFileHeader.PMisc := IntelWord(RO);
for i := 0 to SongCounter - 1 do
begin
RO := StOfs[SongModel[i].Name] - (SizeOf(TAYFileHeader) +
SizeOf(TSongStructure) * i);
if RO > 32767 then ExitError(0);
SongStructure[i].PSongName := IntelWord(RO);
end;
inc(CurrentOffset,Length(Ripper));
SetLength(StOfs,BlockFileCounter);
SetLength(Lens,BlockFileCounter);
FileMode := 0;
for i := 0 to BlockFileCounter - 1 do
begin
StOfs[i] := CurrentOffset;
AssignFile(BlockFile,BlockFiles[i]);
{$i-}
Reset(BlockFile,1);
{$i+}
if IOResult <> 0 then ExitError(1);
RO := FileSize(BlockFile);
CloseFile(BlockFile);
if RO > 65535 then ExitError(2);
Lens[i] := RO;
inc(CurrentOffset,RO);
end;
CurrentOffset := SizeOf(TAYFileHeader) +
(SizeOf(TSongStructure) + SizeOf(TSongData) +
SizeOf(TPoints)) * SongCounter + 4;
for i := 0 to SongCounter - 1 do
begin
for j := 0 to Length(SongModel[i].BlockFile) - 1 do
begin
RO := StOfs[SongModel[i].BlockFile[j].Number] - CurrentOffset;
if RO > 32767 then ExitError(0);
SongModel[i].BlockFile[j].Coords.Offs := IntelWord(RO);
if SongModel[i].BlockFile[j].Coords.Addr +
Lens[SongModel[i].BlockFile[j].Number] > 65536 then ExitError(3);
SongModel[i].BlockFile[j].Coords.Addr :=
IntelWord(SongModel[i].BlockFile[j].Coords.Addr);
SongModel[i].BlockFile[j].Coords.Leng :=
IntelWord(Lens[SongModel[i].BlockFile[j].Number]);
inc(CurrentOffset,SizeOf(TAddresses))
end;
inc(CurrentOffset,2)
end;
for i := 0 to SongCounter - 1 do
begin
Points[i].Stek := IntelWord(Points[i].Stek);
Points[i].Init := IntelWord(Points[i].Init);
Points[i].Inter := IntelWord(Points[i].Inter);
SongData[i].SongLength := IntelWord(SongData[i].SongLength);
SongData[i].FadeLength := IntelWord(SongData[i].FadeLength)
end;
end;
procedure SaveAYFile;
const
Zero:integer = 0;
var
AYFile,BlockFile:file;
FN:string;
i,j:integer;
buff:array of byte;
begin
FN := ParamStr(1);
i := Length(FN);
while (i > 0) and (FN[i] <> '.') do dec(i);
if i > 0 then FN := Copy(FN,1,i - 1);
FN := FN + '.ay';
AssignFile(AYFile,FN);
Rewrite(AYFile,1);
BlockWrite(AYFile,AYFileHeader,SizeOf(TAYFileHeader));
for i := 0 to SongCounter - 1 do
BlockWrite(AYFile,SongStructure[i],SizeOf(TSongStructure));
for i := 0 to SongCounter - 1 do
BlockWrite(AYFile,SongData[i],SizeOf(TSongData));
for i := 0 to SongCounter - 1 do
BlockWrite(AYFile,Points[i],SizeOf(TPoints));
for i := 0 to SongCounter - 1 do
begin
for j := 0 to Length(SongModel[i].BlockFile) - 1 do
BlockWrite(AYFile,SongModel[i].BlockFile[j].Coords,SizeOf(TAddresses));
BlockWrite(AYFile,Zero,2)
end;
for i := 0 to StringCounter - 1 do
if Strings[i].Used then
begin
if Length(Strings[i].Value) > 0 then
BlockWrite(AYFile,Strings[i].Value[1],Length(Strings[i].Value));
BlockWrite(AYFile,Zero,1);
end;
if Length(Ripper) > 0 then
BlockWrite(AYFile,Ripper[1],Length(Ripper));
for i := 0 to BlockFileCounter - 1 do
begin
AssignFile(BlockFile,BlockFiles[i]);
Reset(BlockFile,1);
j := FileSize(BlockFile);
SetLength(buff,j);
BlockRead(BlockFile,buff[0],j);
CloseFile(BlockFile);
BlockWrite(AYFile,buff[0],j);
end;
CloseFile(AYFile);
end;
begin
if ParamCount <> 1 then
begin
Writeln(' Usage:');
Writeln(' AYMakeR [inifilename]')
end
else
begin
AssignFile(Ini,ParamStr(1));
Reset(Ini);
LineCounter := 0;
SongCounter := 0;
StringCounter := 0;
NewString('');
BlockFileCounter := 0;
AYFileHeader.FileID := $5941585A;
AYFileHeader.TypeID := $4C554D45;
AYFileHeader.FileVersion := 0;
AYFileHeader.PlayerVersion := 0;
AYFileHeader.PSpecialPlayer := 0;
AYFileHeader.FirstSong := 0;
AuthorSt := 0;
MiscSt := 0;
Ripper := '';
while not eof(Ini) do
begin
Readln(Ini,WorkString);
inc(LineCounter);
RecognizeString(WorkString);
end;
Close(Ini);
CheckAll;
NormalizeAll;
SaveAYFile
end
end.