forked from ChartreuseK/VGASNOW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradplay.c
742 lines (608 loc) · 15.6 KB
/
radplay.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
/* RADPLAY - Hayden Kroepfl 2017
*
* A simple Reality Adlib tracker file player for DOS.
*
* This version modified for use with the SNOW christmas demo
*
* Written for Turbo C 2.01 on a PC/XT Clone.
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
#define VERSION "0.2.1"
#define HEADLEN 18 /* RAD header length */
#define INSTLEN 11 /* Length of instrument definition */
#define TIMER50 0x5D38 /* PIT timer for 50Hz */
#define TIMER18 0xFFFF /* PIT timer for 18.2Hz */
#define CTRL8253 0x43
#define TIMERMODE 0x3C /* Mode 2, binary count, least/most sig */
#define CNTR0 0x40
#define TIMERVECT 0x1C
/* I/O Ports */
#define AL_ADDR 0x388
#define AL_DATA 0x389
/* Adlib delays */
#define DLYR 6
#define DLYD 25
#define CHANS 9
/* RAD Commands */
#define CMD_PORTUP 1
#define CMD_PORTDN 2
#define CMD_TONESLIDE 3
#define CMD_TONEVOLSLIDE 5
#define CMD_VOLSLIDE 10
#define CMD_SETVOL 12
#define CMD_JMPLINE 13
#define CMD_SETSPEED 15
/* Handy typedefs */
typedef unsigned char uchar;
typedef signed char schar;
typedef unsigned int uint;
typedef unsigned short ushort;
uchar far *screen = (uchar far *)0xB8000000L;
/* Globals */
volatile uchar speed; /* Current speed */
uchar slow; /* Slow-timer (If set use 18.2Hz interrupt, if not then 50Hz) */
uchar spdcnt; /* Countdown between notes */
volatile uchar running;
/* Instrument table, names are adlib base registers */
struct INST {
uchar r23;
uchar r20;
uchar r43;
uchar r40;
uchar r63;
uchar r60;
uchar r83;
uchar r80;
uchar rC0;
uchar rE3;
uchar rE0;
} insts[31];
uchar al_choff[] = {
0x00, 0x01, 0x02, 0x08, 0x09, 0x0A, 0x10, 0x11, 0x12
};
/* Pattern offset table, pointers to start of pattern in data */
ushort patoff[32];
/* Order list (of patterns to play. Val > 80h = jump */
uchar order[128];
uchar orderlen;
uchar curorder;
uchar curpat;
uchar curline;
/* Pattern/note data */
uchar *data;
uint datalen;
ushort dataoff;
ushort patpos; /* Offset into data for current pattern */
/* Previous OPL register values for effects, since we can't read back */
uchar prev_vol[CHANS]; /* Previous volume values OPL 43h */
uchar prev_freqlow[CHANS]; /* Previous freq values OPL A0h */
uchar prev_freqhigh[CHANS]; /* Previous freq values OPL B0h */
/* Effect/Command parameters */
uchar toneslide_speed[CHANS]; /* Tone slide speed */
ushort toneslide_freq[CHANS]; /* Tone slide desitination freq */
struct EFFECTS {
schar portslide;
uchar toneslide;
schar volslide;
} effects[CHANS];
char notepr[][3] = {
"C#","D-","D#","E-",
"F-","F#","G-","G#",
"A-","A#","B-","C-",
"--","--","--","--" };
/* Conversion of note to frequency.
* C = 0x156 (Low C below octave? We start at C#?)
*
* Taken from original Reality Tracker play routine.
*/
ushort notefreq[] = {
0x16b, 0x181, 0x198, 0x1b0, 0x1ca, 0x1e5,
0x202, 0x220, 0x241, 0x263, 0x287, 0x2ae
};
/* Range of one octave in frequency */
#define NOTE_C 0x156
#define OCTAVE (0x2ae - NOTE_C)
/* Convert octave and note to a linearized frequency for slides */
#define linearfreq(oct, note) (((oct)*OCTAVE)+notefreq[(note)]-NOTE_C)
#define linearfreq2(oct, freq) (((oct)*OCTAVE)+(freq)-NOTE_C)
void interrupt (*oldhandler)();
/* Declarations */
void interrupt play();
void doeffects(void);
int do_note(uchar chan, uchar oct, uchar note, uchar cmd, uchar param, uchar inst);
void set_note(uchar chan, uchar oct, uchar note);
void set_linear_freq(uchar chan, short lfreq);
short get_linear_freq(uchar chan);
void set_volume(uchar chan, uchar vol);
uchar get_volume(uchar chan);
void load_inst(uchar i, uchar chan);
int read_data(FILE *fp);
int read_patoff(FILE *fp);
int read_orders(FILE *fp);
int read_insts(FILE *fp);
void print_desc(FILE *fp);
void al_delay(int d);
void al_clr(void);
void al_write(uchar port, uchar val);
extern int snow(void);
int main(int argc, char **argv)
{
uchar buf[32];
FILE *rfile;
int i;
puts("RADPLAY "VERSION" - Hayden Kroepfl 2017");
al_clr();
if (argc < 2) {
puts("usage: RADPLAY filename.RAD");
return 1;
}
rfile = fopen(argv[1], "rb");
if (rfile == NULL) {
puts("Error opening file");
return 2;
}
/* Read in the header */
if (HEADLEN != fread(buf, 1, HEADLEN, rfile)) {
puts("Error reading header");
return 2;
}
if (buf[0] != 'R' || buf[1] != 'A' || buf[2] != 'D') {
puts("Not a RAD file!");
return 2;
}
/* We only support version 1.0 RAD files */
if (buf[0x10] != 0x10) {
printf("Invalid RAD version %02x\n", buf[0x10]);
return 2;
}
speed = buf[0x11] & 0x1F; /* Initial speed */
slow = (buf[0x11] & 0x40) != 0; /* Fast(50Hz) or Slow (18.2Hz) */
if (buf[0x11] & 0x80) {
/* Read description */
print_desc(rfile);
}
/* Load instruments */
if (read_insts(rfile) < 0) {
return -2;
}
/* Load orders */
if (read_orders(rfile) < 0) {
return -2;
}
if (read_patoff(rfile) < 0) {
return -2;
}
if (read_data(rfile) < 0) {
return -2;
}
printf("Data length: %d\n", datalen);
/* Fixup patoff to be 0 based */
for (i = 0; i < 32; i++)
if (patoff[i])
patoff[i] -= dataoff;
/* Set PIT Timer 0 to our speed if in fast mode */
if (!slow) {
outportb(CTRL8253, TIMERMODE);
outportb(CNTR0, TIMER50 & 0xFF);
outportb(CNTR0, TIMER50 >> 8);
puts("FAST MODE");
} else {
puts("SLOW MODE");
}
oldhandler = getvect(TIMERVECT);
/* Begin playback */
curorder = 0;
curpat = order[curorder];
patpos = patoff[curpat];
running = 1;
setvect(TIMERVECT, play);
snow();
outportb(CTRL8253, TIMERMODE);
outportb(CNTR0, TIMER18 & 0xFF);
outportb(CNTR0, TIMER18 >> 8);
setvect(TIMERVECT, oldhandler);
al_clr();
if (data)
free(data);
return 0;
}
/*
* Playback routine
*
* Call at 50 or 18.2Hz intervals (depending on fast/slow)
*/
void interrupt play()
{
uchar line, chan, note[2];
uchar oct, n, inst, cmd, param;
int nextline;
/* Check for done flag */
if (patpos == 0xFFFF) {
al_clr();
running = 0;
return;
}
/*
* Read a new line if the count is up
*/
if (spdcnt-- == 0) {
for (chan = 0; chan < CHANS; chan++) {
effects[chan].portslide = 0;
effects[chan].toneslide = 0;
effects[chan].volslide = 0;
}
line = data[patpos]; /* Read in line number */
/*
* If the next line read matches the current line number,
* ie. We've already handled any blank lines
*/
if (curline++ == (line&0x7F)) {
patpos++;
do {
/*
* Read all the channel changes in this line
*/
chan = data[patpos++];
note[0] = data[patpos++];
note[1] = data[patpos++];
/*
* Check for a command, if so read the parameter
*/
param = 0;
if (note[1] & 0xF)
param = data[patpos++];
cmd = note[1] & 0xF;
/*
* Extract note data from note packet
*/
oct = (note[0] >> 4) & 0x7;
n = note[0] & 0xF;
inst = (note[1]>>4) | ((note[0]&0x80)>>3);
if ((nextline = do_note(chan&0x7F, oct, n, cmd, param, inst)) > 0){
/*
* Jump to line nextline-1 in next pattern, ignore
* remaining channels on this line
*/
curpat = order[++curorder];
/*
* Check if the next pattern is to be a jump instad
*/
while (curpat & 0x80) {
curorder = curpat - 0x80;
curpat = order[curorder];
}
/* Get the offset for the selected pattern */
patpos = patoff[curpat];
/* Go through pattern till we find matching line */
while ((data[patpos] & 0x7F) < nextline) {
if (data[patpos] * 0x80) {
/* End of pattern searching for line
* Stop playback
*/
patpos = 0xFFFF;
}
/* Skip line */
patpos++;
while (!(data[patpos++] & 0x80)) {
patpos++; /* Skip note[0] */
if (data[patpos++] & 0xF) /* Skip note[1] */
patpos++; /* Skip parameter */
}
}
curline = nextline;
goto skip;
}
} while (!(chan & 0x80));
}
/*
* Check if we hit the end of a pattern
*/
if ((line & 0x80) || (curline >= 0x80)) {
curpat = order[++curorder];
/*
* Check if the next pattern is to be a jump instad
*/
while (curpat & 0x80) {
curorder = curpat - 0x80;
curpat = order[curorder];
}
/* Get the offset for the selected pattern */
patpos = patoff[curpat];
curline = 0;
}
/*
* Reset spdcnt to current speed
* FOR INITIAL TESTING SET TO 0 SINCE WE HAVE NO EFFECTS
*/
skip:
spdcnt = speed-1;
}
/* Update effects for the line */
doeffects();
}
void doeffects(void)
{
uchar chan;
short lfreq;
short vol;
for (chan = 0; chan < CHANS; chan++) {
if (effects[chan].portslide) {
lfreq = get_linear_freq(chan);
lfreq += (short)(effects[chan].portslide);
set_linear_freq(chan, lfreq);
}
if (effects[chan].toneslide) {
lfreq = get_linear_freq(chan);
if (lfreq < toneslide_freq[chan]) {
lfreq += toneslide_speed[chan];
if (lfreq >= toneslide_freq[chan]) {
effects[chan].toneslide = 0;
lfreq = toneslide_freq[chan];
}
} else if (lfreq > toneslide_freq[chan]) {
lfreq -= toneslide_speed[chan];
if (lfreq <= toneslide_freq[chan]) {
effects[chan].toneslide = 0;
lfreq = toneslide_freq[chan];
}
} else {
effects[chan].toneslide = 0;
}
set_linear_freq(chan, lfreq);
}
if (effects[chan].volslide) {
vol = get_volume(chan);
vol += effects[chan].volslide;
if (vol < 0)
vol = 0;
set_volume(chan, vol);
}
}
}
int do_note(uchar chan, uchar oct, uchar note, uchar cmd, uchar param, uchar inst)
{
/*
* If there is a note change
*/
if (note) {
/*
* Check if this is a toneslide + note
*/
if (cmd == CMD_TONESLIDE) {
/*
* oct+note is the destination frequency
*/
toneslide_freq[chan] = linearfreq(oct, note);
/* If param != 0 then change the speed */
if (param)
toneslide_speed[chan] = param;
effects[chan].toneslide = 1;
return 0;
} else {
/* Set note (or KEY-OFF) */
set_note(chan, oct, 15); /*KEY-OFF*/
/*
* Change instrument for channel
*/
if (inst)
load_inst(inst-1, chan);
set_note(chan, oct, note);
}
}
/*
* Handle any commands
*/
switch(cmd) {
case CMD_PORTUP: /* Portamento Up */
effects[chan].portslide = (schar)param;
break;
case CMD_PORTDN: /* Portamento Down */
effects[chan].portslide = -(schar)param;
break;
case CMD_TONESLIDE: /* Slide tone (no note specified) */
effects[chan].toneslide = 1;
if (param)
toneslide_speed[chan] = param;
break;
case CMD_TONEVOLSLIDE: /* Slide tone and volume */
effects[chan].toneslide = 1;
/* Fall through */
case CMD_VOLSLIDE: /* Volume slide (Down < 50, Up > 50) */
effects[chan].volslide = (param < 50) ? -param : param - 50;
break;
case CMD_SETVOL: /* Set volume for channel */
set_volume(chan, param);
break;
case CMD_JMPLINE: /* Jump to line in next pattern */
return 1+param;
case CMD_SETSPEED: /* Set playback speed */
speed = param;
break;
}
return 0;
}
void set_note(uchar chan, uchar oct, uchar note)
{
ushort freq;
if (!note)
return;
if (note < 13) {
freq = 0x2000 | (((ushort)oct << 10) + notefreq[note-1]);
prev_freqlow[chan] = (uchar)freq;
prev_freqhigh[chan] = (uchar)(freq >> 8);
al_write(0xA0 + chan, (uchar)freq);
al_write(0xB0 + chan, (uchar)(freq >> 8));
}
else {
/* KEY-OFF */
prev_freqhigh[chan] &= ~0x20;
al_write(0xB0 + chan, prev_freqhigh[chan]);
}
}
/* Set frequency of channel from a linear freq */
void set_linear_freq(uchar chan, short lfreq)
{
uchar oct;
ushort nfreq;
ushort freq;
oct = lfreq / OCTAVE;
nfreq = (lfreq % OCTAVE) + NOTE_C;
/* Mask out old frequency */
freq = (prev_freqhigh[chan] & ~0x1F) << 8;
freq |= nfreq;
freq |= (ushort)oct << 10;
prev_freqlow[chan] = (uchar)freq;
prev_freqhigh[chan] = (uchar)(freq >> 8);
al_write(0xA0 + chan, (uchar)freq);
al_write(0xB0 + chan, (uchar)(freq >> 8));
}
/* Get frequency of channel as a linear freq */
short get_linear_freq(uchar chan)
{
ushort freq, nfreq;
uchar oct;
freq = (ushort)prev_freqlow[chan] | ((ushort)prev_freqhigh[chan] << 8);
oct = (freq >> 10) & 0x7;
nfreq = freq & 0x3FF;
return linearfreq2(oct, nfreq);
}
/* Set volume for specified channel */
void set_volume(uchar chan, uchar vol)
{
uchar new43;
uchar choff = al_choff[chan];
if (vol >= 64)
vol = 63;
new43 = prev_vol[chan] & ~0x3f; /* Mask out old volume */
new43 |= vol ^ 0x3F; /* Invert volume */
prev_vol[chan] = new43;
al_write(0x43+choff, new43);
}
/* Get volume for specified channel */
uchar get_volume(uchar chan)
{
uchar vol;
vol = prev_vol[chan] & 0x3F;
vol ^= 0x3F;
return vol;
}
/* Load instrument i into OPL channel chan */
void load_inst(uchar i, uchar chan)
{
uchar choff;
choff = al_choff[chan];
al_write(0x23+choff, insts[i].r23);
al_write(0x20+choff, insts[i].r20);
al_write(0x43+choff, insts[i].r43);
prev_vol[chan] = insts[i].r43;
al_write(0x40+choff, insts[i].r40);
al_write(0x63+choff, insts[i].r63);
al_write(0x60+choff, insts[i].r60);
al_write(0x83+choff, insts[i].r83);
al_write(0x80+choff, insts[i].r80);
al_write(0xE3+choff, insts[i].rE3);
al_write(0xE0+choff, insts[i].rE0);
al_write(0xC0+chan, insts[i].rC0);
}
/* Read in data section from file */
int read_data(FILE *fp)
{
long int len, old;
/* Get length of data section */
old = ftell(fp);
fseek(fp, 0, SEEK_END);
len = ftell(fp) - old;
fseek(fp, old, SEEK_SET);
/* Filesize is max 65535, has to fit in one segment */
if (len > 65535L) {
puts("Error file too long");
return -2;
}
dataoff = old;
data = malloc((int)len);
if (data == NULL) {
puts("Failed to alloc space for data");
return -3;
}
datalen = fread(data, 1, (int)len, fp);
if (ferror(fp)) {
puts("Error reading pattern data");
return -1;
}
return 0;
}
/* Load in pattern offset table */
int read_patoff(FILE *fp)
{
if (32 != fread(patoff, 2, 32, fp)) {
puts("Failed to read in pattern offset table.");
return -1;
}
return 0;
}
/* Load in order list */
int read_orders(FILE *fp)
{
int ch = fgetc(fp);
orderlen = ch;
if (ch == EOF || orderlen != fread(order, 1, orderlen, fp)) {
puts("Error reading orders list");
return -1;
}
return 0;
}
/* Load in instrument table */
int read_insts(FILE *fp)
{
int ch = fgetc(fp);
while (ch && ch != EOF) {
if (INSTLEN != fread(&insts[ch-1], 1, INSTLEN, fp)) {
printf("Error reading instrument %d\n", ch-1);
return -1;
}
ch = fgetc(fp);
}
return 0;
}
/* Print a RAD file description */
void print_desc(FILE *fp)
{
int ch = fgetc(fp);
puts("Description:");
while (ch && ch != EOF) {
if (ch >= 0x20) {
putchar(ch);
} else if (ch == 0x01) {
puts("");
} else {
for (; ch > 0; ch--)
putchar(' ');
}
ch = fgetc(fp);
}
puts("");
}
/* Delay function between adlib accesses */
void al_delay(int d)
{
for (; d > 0; d--)
inportb(AL_ADDR);
}
/* Write value to OPL2 register */
void al_write(uchar port, uchar val)
{
outportb(AL_ADDR, port);
al_delay(DLYR);
outportb(AL_DATA, val);
al_delay(DLYD);
}
/* Reset adlib registers */
void al_clr(void)
{
int i;
for (i = 0; i < 256; i++)
al_write(i, 0);
}