/
arduino_tritone.ino
executable file
·354 lines (218 loc) · 6.13 KB
/
arduino_tritone.ino
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
//Tritone beeper engine port from ZX Spectrum to Arduino
//by Shiru (shiru@mail.ru) 17.02.17
//include music data generated by the bin2.py script
//#include "music_data.h"
//#include "music_data_test.h"
#include "music_data_bourrasque.h"
//#include "music_data_incantation.h"
//#include "music_data_misterbeep.h"
//#include "music_data_souvenirs_tritone.h"
//#include "music_data_snakecharmer.h"
//include sample data
#include "drum_sample_data.h"
//general settings
#define COMPILE_ADR 40000 //address for compiled Beepola module, default is 40000
//#define SPEAKER_PORT PORTD //speaker is on the port D
//#define SPEAKER_DDR DDRD
//#define SPEAKER_BIT (1<<7) //PD7 (Uno pin 7)
// perso
#define SPEAKER_PORT PORTB //speaker is on the port b
#define SPEAKER_DDR DDRB
#define SPEAKER_BIT (1<<1) //Pb1 (Uno pin 9)
#define LED 8
//there is no sample rate, width of the output slots in AVR clocks used instead
//calculated for 22988 Hz sample rate, which is closest to the original Tritone 22875 Hz
#define PULSE_SLOT_1 320
#define PULSE_SLOT_2 224
#define PULSE_SLOT_3 152
#define DRUM_SLOT ((PULSE_SLOT_1+PULSE_SLOT_2+PULSE_SLOT_3)/2) //drum samples played at 45977 Hz
#define SAMPLE_RATE (16000000/(PULSE_SLOT_1+PULSE_SLOT_2+PULSE_SLOT_3)) //just for reference
//array of counters and reload values, for each channel
unsigned int acc [3];
unsigned int add [3];
unsigned char out [3];
unsigned char duty[3];
//drum sample player variables
unsigned char drum_output;
unsigned char drum_pulse_length;
unsigned char drum_sync;
const unsigned char *drum_data;
//sync counters to syncronize the music parser with the synth code
//more tricky than other engines because of the original's quirk
//it is volatile because it the main thread reads it back
volatile unsigned char parser_sync_enable;
volatile unsigned char parser_sync_l;
volatile unsigned char parser_sync_h;
//song parser variables
unsigned int song_tempo;
unsigned char *order_list;
unsigned char *pattern_ptr;
//pointer to the actual interrupt handler that gets changed all the time
void (*interrupt_handler)(void);
//initialize all
void setup()
{
unsigned char chn;
cli();
//initialize channel variables
for(chn=0;chn<3;++chn)
{
acc [chn]=0;
add [chn]=0;
out [chn]=0;
duty[chn]=0;
}
//initialize song parser variables
parser_sync_enable=0;
parser_sync_l=0;
parser_sync_h=0;
order_list=music_data;
song_new_pattern();
//set a port pin as the output
SPEAKER_DDR|=SPEAKER_BIT;
//set timer2 to generate interrupts at the sample rate
TCCR2A=0;
TCCR2B=0;
TCNT2 =0;
TCCR2A|=(1<<WGM21);
TCCR2B|=(0<<CS02)|(1<<CS01)|(0<<CS00); //prescaler=8
TIMSK2|=(1<<OCIE2A);
//force the timer setting and next interrupt handler to start the handler sequence
interrupt_handler_tone_1();
sei();
}
//update parser sync counters
//they were made that way in the original to save CPU time
void run_parser_sync(void)
{
if(parser_sync_enable)
{
--parser_sync_l;
if(!parser_sync_l)
{
--parser_sync_h;
if(!parser_sync_h) parser_sync_enable=0;
}
}
}
//first tone slot, for loudest channel
void interrupt_handler_tone_1(void)
{
OCR2A=PULSE_SLOT_1/8-1;
SPEAKER_PORT=out[2];
acc[0]+=add[0];
acc[1]+=add[1];
acc[2]+=add[2];
if((acc[0]>>8)>=duty[0]) out[0]=SPEAKER_BIT; else out[0]=0;
if((acc[1]>>8)>=duty[1]) out[1]=SPEAKER_BIT; else out[1]=0;
if((acc[2]>>8)>=duty[2]) out[2]=SPEAKER_BIT; else out[2]=0;
interrupt_handler=interrupt_handler_tone_2;
}
//second tone slot
void interrupt_handler_tone_2(void)
{
OCR2A=PULSE_SLOT_2/8-1;
SPEAKER_PORT=out[1];
interrupt_handler=interrupt_handler_tone_3;
}
//rhird tone slot, for quietest channel
void interrupt_handler_tone_3(void)
{
OCR2A=PULSE_SLOT_3/8-1;
SPEAKER_PORT=out[0];
run_parser_sync();
interrupt_handler=interrupt_handler_tone_1;
}
//drum sample player interrupt
void interrupt_handler_drum(void)
{
OCR2A=DRUM_SLOT/8-1;
SPEAKER_PORT=drum_output;
if(drum_pulse_length)
{
--drum_pulse_length;
digitalWrite(LED,HIGH);
}
else
{
drum_output^=SPEAKER_BIT;
drum_pulse_length=pgm_read_byte_near(drum_data);
++drum_data;
if(!drum_pulse_length) interrupt_handler=interrupt_handler_tone_1;
}
if(drum_sync&1) run_parser_sync(); //drum sample rate is twice higher than tone sample rate, keep sync
++drum_sync;
}
ISR(TIMER2_COMPA_vect)
{
interrupt_handler(); //call currently assigned interrupt handler using function pointer
}
void song_new_pattern(void)
{
unsigned int off;
while(1)
{
off=pgm_read_byte_near(order_list)+(pgm_read_byte_near(order_list+1)<<8);
order_list+=2;
if(off) break;
off=pgm_read_byte_near(order_list)+(pgm_read_byte_near(order_list+1)<<8);
order_list=music_data+(off-COMPILE_ADR);
}
pattern_ptr=music_data+(off-COMPILE_ADR);
song_tempo=pgm_read_byte_near(pattern_ptr)+(pgm_read_byte_near(pattern_ptr+1)<<8);
pattern_ptr+=2;
}
//main loop
void loop()
{
unsigned char n,chn;
unsigned int frq;
while(1)
{
//update row
while(1)
{
n=pgm_read_byte_near(pattern_ptr);
if(n==0xff) //end of pattern
{
song_new_pattern();
continue;
}
if(n>=2&&n<128) //drum sound
{
drum_data=(const unsigned char*)pgm_read_word(&(drum_sample_list[n-2]));
drum_output=0;
drum_pulse_length=0;
drum_sync=0;
interrupt_handler=interrupt_handler_drum;
++pattern_ptr;
}
break;
}
for(chn=0;chn<3;++chn)
{
n=pgm_read_byte_near(pattern_ptr);
++pattern_ptr;
if(!n) //key off
{
add [chn]=0;
duty[chn]=0;
}
if(n>=128)
{
frq=(n<<8)+pgm_read_byte_near(pattern_ptr);
add [chn]=frq&0xfff;
duty[chn]=(frq>>8)&0xf0;
++pattern_ptr;
}
}
//set up parser sync counters
parser_sync_l=song_tempo&255;
parser_sync_h=song_tempo>>8;
parser_sync_enable=1;
//wait for the next row
//delay 1 is important, by some reason song tempo starts to jump a lot without it
// while(parser_sync_enable) delay(1);
while(parser_sync_enable);
}
}