annotate playercode/munitrk.c @ 17:80fa6dd10e14

Fix some C++ 'ism's
author darius
date Thu, 23 Apr 1998 22:54:49 +0000
parents d14fd386d182
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
1 /*
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
2
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
3 Name:
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
4 MUNITRK.C
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
5
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
6 Description:
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
7 All routines dealing with the manipulation of UNITRK(tm) streams
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
8
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
9 Portability:
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
10 All systems - all compilers
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
11
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
12 */
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
13
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
14 #include <string.h>
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
15 #include "mikmod.h"
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
16
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
17 #define BUFPAGE 128 // smallest unibuffer size
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
18 #define TRESHOLD 16
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
19
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
20 UWORD unioperands[256] =
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
21 { 0, // not used
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
22 1, // UNI_NOTE
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
23 1, // UNI_INSTRUMENT
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
24 1, // UNI_PTEFFECT0
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
25 1, // UNI_PTEFFECT1
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
26 1, // UNI_PTEFFECT2
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
27 1, // UNI_PTEFFECT3
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
28 1, // UNI_PTEFFECT4
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
29 1, // UNI_PTEFFECT5
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
30 1, // UNI_PTEFFECT6
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
31 1, // UNI_PTEFFECT7
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
32 1, // UNI_PTEFFECT8
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
33 1, // UNI_PTEFFECT9
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
34 1, // UNI_PTEFFECTA
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
35 1, // UNI_PTEFFECTB
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
36 1, // UNI_PTEFFECTC
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
37 1, // UNI_PTEFFECTD
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
38 1, // UNI_PTEFFECTE
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
39 1, // UNI_PTEFFECTF
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
40 1, // UNI_S3MEFFECTA
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
41 1, // UNI_S3MEFFECTD
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
42 1, // UNI_S3MEFFECTE
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
43 1, // UNI_S3MEFFECTF
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
44 1, // UNI_S3MEFFECTI
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
45 1, // UNI_S3MEFFECTQ
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
46 1, // UNI_S3MEFFECTR
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
47 1, // UNI_S3MEFFECTT
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
48 1, // UNI_S3MEFFECTU
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
49 0, // UNI_KEYOFF
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
50 1, // UNI_KEYFADE
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
51 2, // UNI_VOLEFFECTS
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
52 1, // UNI_XMEFFECT4
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
53 1, // UNI_XMEFFECTA
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
54 1, // UNI_XMEFFECTE1
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
55 1, // UNI_XMEFFECTE2
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
56 1, // UNI_XMEFFECTEA
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
57 1, // UNI_XMEFFECTEB
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
58 1, // UNI_XMEFFECTG
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
59 1, // UNI_XMEFFECTH
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
60 1, // UNI_XMEFFECTL
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
61 1, // UNI_XMEFFECTP
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
62 1, // UNI_XMEFFECTX1
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
63 1, // UNI_XMEFFECTX2
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
64 1, // UNI_ITEFFECTG
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
65 1, // UNI_ITEFFECTH
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
66 1, // UNI_ITEFFECTI
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
67 1, // UNI_ITEFFECTM
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
68 1, // UNI_ITEFFECTN
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
69 1, // UNI_ITEFFECTP
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
70 1, // UNI_ITEFFECTU
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
71 1, // UNI_ITEFFECTW
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
72 1, // UNI_ITEFFECTY
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
73 1 // UNI_ITEFFECTS0
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
74 };
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
75
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
76
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
77 // unibuffer is increased by BUFPAGE
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
78 // bytes when unipc reaches unimax-TRESHOLD
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
79
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
80
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
81 /*
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
82 Ok.. I'll try to explain the new internal module format.. so here it goes:
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
83
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
84
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
85 The UNITRK(tm) Format:
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
86 ======================
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
87
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
88 A UNITRK stream is an array of bytes representing a single track
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
89 of a pattern. It's made up of 'repeat/length' bytes, opcodes and
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
90 operands (sort of a assembly language):
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
91
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
92 rrrlllll
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
93 [REP/LEN][OPCODE][OPERAND][OPCODE][OPERAND] [REP/LEN][OPCODE][OPERAND]..
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
94 ^ ^ ^
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
95 |-------ROWS 0 - 0+REP of a track---------| |-------ROWS xx - xx+REP of a track...
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
96
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
97
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
98 The rep/len byte contains the number of bytes in the current row,
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
99 _including_ the length byte itself (So the LENGTH byte of row 0 in the
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
100 previous example would have a value of 5). This makes it easy to search
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
101 through a stream for a particular row. A track is concluded by a 0-value
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
102 length byte.
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
103
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
104 The upper 3 bits of the rep/len byte contain the number of times -1 this
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
105 row is repeated for this track. (so a value of 7 means this row is repeated
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
106 8 times)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
107
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
108 Opcodes can range from 1 to 255 but currently only opcodes 1 to 45 are
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
109 being used. Each opcode can have a different number of operands. You can
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
110 find the number of operands to a particular opcode by using the opcode
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
111 as an index into the 'unioperands' table.
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
112
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
113 */
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
114
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
115
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
116 /***************************************************************************
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
117 >>>>>>>>>>> Next are the routines for reading a UNITRK stream: <<<<<<<<<<<<<
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
118 ***************************************************************************/
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
119
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
120
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
121 static UBYTE *rowstart; // startadress of a row
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
122 static UBYTE *rowend; // endaddress of a row (exclusive)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
123 static UBYTE *rowpc; // current unimod(tm) programcounter
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
124
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
125
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
126 void UniSetRow(UBYTE *t)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
127 {
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
128 rowstart = t;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
129 rowpc = rowstart;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
130 rowend = rowstart+(*(rowpc++)&0x1f);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
131 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
132
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
133
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
134 UBYTE UniGetByte(void)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
135 {
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
136 return (rowpc<rowend) ? *(rowpc++) : 0;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
137 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
138
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
139
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
140 void UniSkipOpcode(UBYTE op)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
141 {
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
142 UWORD t = unioperands[op];
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
143 while(t--) UniGetByte();
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
144 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
145
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
146
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
147 UBYTE *UniFindRow(UBYTE *t, UWORD row)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
148 // Finds the address of row number 'row' in the UniMod(tm) stream 't'
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
149 // returns NULL if the row can't be found.
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
150 {
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
151 UBYTE c,l;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
152
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
153 while(1)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
154 { c = *t; // get rep/len byte
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
155 if(!c) return NULL; // zero ? -> end of track..
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
156 l = (c>>5)+1; // extract repeat value
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
157 if(l>row) break; // reached wanted row? -> return pointer
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
158 row -= l; // haven't reached row yet.. update row
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
159 t += c&0x1f; // point t to the next row
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
160 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
161
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
162 return t;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
163 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
164
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
165
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
166
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
167 /***************************************************************************
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
168 >>>>>>>>>>> Next are the routines for CREATING UNITRK streams: <<<<<<<<<<<<<
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
169 ***************************************************************************/
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
170
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
171
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
172 static UBYTE *unibuf; // pointer to the temporary unitrk buffer
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
173 static UWORD unimax; // maximum number of bytes to be written to this buffer
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
174
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
175 static UWORD unipc; // index in the buffer where next opcode will be written
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
176 static UWORD unitt; // holds index of the rep/len byte of a row
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
177 static UWORD lastp; // holds index to the previous row (needed for compressing)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
178
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
179
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
180 void UniReset(void)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
181 // Resets index-pointers to create a new track.
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
182 {
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
183 unitt = 0; // reset index to rep/len byte
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
184 unipc = 1; // first opcode will be written to index 1
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
185 lastp = 0; // no previous row yet
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
186 unibuf[0] = 0; // clear rep/len byte
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
187 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
188
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
189
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
190 void UniWrite(UBYTE data)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
191 // Appends one byte of data to the current row of a track.
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
192 {
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
193 // write byte to current position and update
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
194
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
195 unibuf[unipc++] = data;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
196
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
197 // Check if we've reached the end of the buffer
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
198
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
199 if(unipc > (unimax-TRESHOLD))
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
200 { UBYTE *newbuf;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
201
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
202 // We've reached the end of the buffer, so expand
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
203 // the buffer by BUFPAGE bytes
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
204
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
205 newbuf = (UBYTE *)realloc(unibuf, unimax+BUFPAGE);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
206
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
207 // Check if realloc succeeded
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
208
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
209 if(newbuf!=NULL)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
210 { unibuf = newbuf;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
211 unimax+=BUFPAGE;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
212 } else
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
213 { // realloc failed, so decrease unipc so we won't write beyond
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
214 // the end of the buffer.. I don't report the out-of-memory
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
215 // here; the UniDup() will fail anyway so that's where the
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
216 // loader sees that something went wrong
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
217
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
218 unipc--;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
219 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
220 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
221 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
222
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
223
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
224 BOOL MyCmp(UBYTE *a, UBYTE *b, UWORD l)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
225 {
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
226 UWORD t;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
227
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
228 for(t=0; t<l; t++)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
229 if(*(a++) != *(b++)) return 0;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
230 return 1;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
231 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
232
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
233
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
234 void UniNewline(void)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
235 // Closes the current row of a unitrk stream (updates the rep/len byte)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
236 // and sets pointers to start a new row.
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
237 {
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
238 UWORD n,l,len;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
239
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
240 n = (unibuf[lastp]>>5)+1; // repeat of previous row
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
241 l = (unibuf[lastp]&0x1f); // length of previous row
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
242
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
243 len = unipc-unitt; // length of current row
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
244
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
245 // Now, check if the previous and the current row are identical..
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
246 // when they are, just increase the repeat field of the previous row
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
247
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
248 if(n<8 && len==l && MyCmp(&unibuf[lastp+1],&unibuf[unitt+1],len-1))
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
249 { unibuf[lastp]+=0x20;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
250 unipc = unitt+1;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
251 } else
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
252 { // current and previous row aren't equal.. so just update the pointers
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
253
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
254 unibuf[unitt] = len;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
255 lastp = unitt;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
256 unitt = unipc;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
257 unipc++;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
258 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
259 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
260
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
261
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
262 UBYTE *UniDup(void)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
263 // Terminates the current unitrk stream and returns a pointer
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
264 // to a copy of the stream.
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
265 {
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
266 UBYTE *d;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
267
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
268 unibuf[unitt] = 0;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
269
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
270 if((d=(UBYTE *)_mm_malloc(unipc))==NULL) return NULL;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
271 memcpy(d,unibuf,unipc);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
272
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
273 return d;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
274 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
275
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
276
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
277 UWORD TrkLen(UBYTE *t)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
278 // Determines the length (in rows) of a unitrk stream 't'
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
279 {
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
280 UWORD len = 0;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
281 UBYTE c;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
282
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
283 while(c = *t & 0x1f)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
284 { len += c;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
285 t += c;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
286 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
287 len++;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
288
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
289 return len;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
290 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
291
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
292
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
293 BOOL UniInit(void)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
294 {
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
295 unimax = BUFPAGE;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
296
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
297 if(!(unibuf=(UBYTE *)_mm_malloc(unimax))) return 0;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
298 return 1;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
299 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
300
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
301
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
302 void UniCleanup(void)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
303 {
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
304 if(unibuf!=NULL) free(unibuf);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
305 unibuf = NULL;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
306 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
307