File: ittime.c

package info (click to toggle)
ocp 1%3A0.1.21-5
  • links: PTS
  • area: main
  • in suites: buster
  • size: 5,528 kB
  • sloc: ansic: 91,461; cpp: 9,729; sh: 3,119; makefile: 2,493
file content (245 lines) | stat: -rw-r--r-- 5,404 bytes parent folder | download | duplicates (3)
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
/* OpenCP Module Player
 * copyright (c) '94-'10 Niklas Beisert <nbeisert@physik.tu-muenchen.de>
 *
 * ITPlay timing/sync handlers for IMS
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * revision history: (please note changes here)
 *  -nb980510   Niklas Beisert <nbeisert@physik.tu-muenchen.de>
 *    -first release
 */

#include "config.h"
#include <signal.h>
#include <string.h>
#include <stdio.h> /* FILE * in itplay.h */
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include "types.h"
#include "itplay.h"

int __attribute__ ((visibility ("internal"))) it_precalctime(struct it_module *this, int startpos, int (*calctimer)[2], int calcn, int ite)
{
	uint8_t *patptr=0;

	int patdelaytick=0;
	int patdelayrow=0;
	int sync=-1;
	int looped=0;
	int gotorow=(startpos>>8)&0xFF;
	int gotoord=startpos&0xFF;
	int curord=-1;
	int currow=-1;

	int curspeed=this->inispeed;
	int curtick=this->inispeed-1;

	/* int loopord=0;   unused */
	int tempo=this->initempo;
	int timerval=0;
	int timerfrac=0;

	uint8_t tempos[64];
	uint8_t cmds[64];
	uint8_t specials[64];
	uint8_t patloopcount[64];
	uint8_t patloopstart[64];

	int it;

	memset(tempos, 0, this->nchan);
	memset(specials, 0, this->nchan);
	memset(cmds, 0, this->nchan);


	for (it=0; it<ite; it++)
	{
		int i;
		int p;

		curtick++;
		if ((curtick==(curspeed+patdelaytick))&&patdelayrow)
		{
			curtick=0;
			patdelayrow--;
		}
		if (curtick==(curspeed+patdelaytick))
		{
			patdelaytick=0;
			curtick=0;
			currow++;
			if ((gotoord==-1)&&(currow==this->patlens[this->orders[curord]]))
			{
				gotoord=curord+1;
				gotorow=0;
			}
			if (gotoord!=-1)
			{
				if (gotoord!=curord)
				{
					memset(patloopcount, 0, this->nchan);
					memset(patloopstart, 0, this->nchan);
				}

				if (gotoord>=this->endord)
					gotoord=0;
				while (this->orders[gotoord]==0xFFFF)
					gotoord++;
				if (gotoord==this->endord)
					gotoord=0;
				if (gotorow>=this->patlens[this->orders[gotoord]])
				{
					gotoord++;
					gotorow=0;
					while (this->orders[gotoord]==0xFFFF)
						gotoord++;
					if (gotoord==this->endord)
						gotoord=0;
				}
				if (gotoord<curord)
					looped=1;
				curord=gotoord;
				patptr=this->patterns[this->orders[curord]];
				for (currow=0; currow<gotorow; currow++)
				{
					while (*patptr)
						patptr+=6;
					patptr++;
				}
				gotoord=-1;
			}

			for (i=0; i<this->nchan; i++)
				cmds[i]=0;
			if (!patptr)
			{
				fprintf(stderr, "playit: ittime.c: patptr not set\n");
				kill(getpid(), SIGTERM);
				abort();
			}
			while (*patptr)
			{
				int ch=*patptr++-1;

				int data=patptr[4];

				cmds[ch]=patptr[3];
				switch (cmds[ch])
				{
					case cmdSpeed:
						if (data)
							curspeed=data;
						break;
					case cmdJump:
						gotorow=0;
						gotoord=data;
						break;
					case cmdBreak:
						if (gotoord==-1)
							gotoord=curord+1;
						gotorow=data;
						break;
					case cmdSpecial:
						if (data)
							specials[ch]=data;
						switch (specials[ch]>>4)
						{
							case cmdSPatDelayTick:
								patdelaytick=specials[ch]&0xF;
								break;
							case cmdSPatLoop:
								if (!(specials[ch]&0xF))
									patloopstart[ch]=currow;
								else {
									patloopcount[ch]++;
									if (patloopcount[ch]<=(specials[ch]&0xF))
									{
										gotorow=patloopstart[ch];
										gotoord=curord;
									} else {
										patloopcount[ch]=0;
										patloopstart[ch]=currow+1;
									}
								}
								break;
							case cmdSPatDelayRow:
								patdelayrow=specials[ch]&0xF;
								break;
						}
						break;
					case cmdTempo:
						if (data)
							tempos[ch]=data;
						if (tempos[ch]>=0x20)
							tempo=tempos[ch];
						break;
#if 0
					TODO this upcode is invalid
					case cmdSync:
						sync=data;
						break;
#endif
				}
				patptr+=5;
			}
			patptr++;
		} else
			for (i=0; i<this->nchan; i++)
				if ((cmds[i]==cmdTempo)&&(tempos[i]<0x20))
				{
					tempo+=(tempos[i]<0x10)?-tempos[i]:(tempos[i]&0xF);
					tempo=(tempo<0x20)?0x20:(tempo>0xFF)?0xFF:tempo;
				}



		p=(curord<<16)|(currow<<8)|curtick;
		for (i=0; i<calcn; i++)
			if ((p==calctimer[i][0])&&(calctimer[i][1]<0))
				if (!++calctimer[i][1])
					calctimer[i][1]=timerval;

		if (sync!=-1)
			for (i=0; i<calcn; i++)
				if ((calctimer[i][0]==(-256-sync))&&(calctimer[i][1]<0))
					if (!++calctimer[i][1])
						calctimer[i][1]=timerval;

		sync=-1;

		if (looped)
			for (i=0; i<calcn; i++)
				if ((calctimer[i][0]==-1)&&(calctimer[i][1]<0))
					if (!++calctimer[i][1])
						calctimer[i][1]=timerval;

		looped=0;

		timerfrac+=4096*163840/tempo;
		timerval+=timerfrac>>12;
		timerfrac&=4095;

		for (i=0; i<calcn; i++)
			if (calctimer[i][1]<0)
				break;

		if (i==calcn)
			break;
	}

	return 1;
}