Debian Codegraph @

src/engine/platform/c140.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "c140.h"
21 #include "../engine.h"
22 #include "../../ta-log.h"
23 #include <math.h>
24
25 #define CHIP_FREQBASE (is219?74448896:12582912)
26
27 #define rWrite(a,v) {if(!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if(dumpWrites) addWrite(a,v); }}
28
29 const char* regCheatSheetC140[]={
30 "CHx_RVol", "00+x*10",
31 "CHx_LVol", "01+x*10",
32 "CHx_FreqH", "02+x*10",
33 "CHx_FreqL", "03+x*10",
34 "CHx_Bank", "04+x*10",
35 "CHx_Ctrl", "05+x*10",
36 "CHx_StartH", "06+x*10",
37 "CHx_StartL", "07+x*10",
38 "CHx_EndH", "08+x*10",
39 "CHx_EndL", "09+x*10",
40 "CHx_LoopH", "0A+x*10",
41 "CHx_LoopL", "0B+x*10",
42 "Timer", "1FA",
43 "IRQ", "1FE",
44 NULL
45 };
46
47 const char* regCheatSheetC219[]={
48 "CHx_RVol", "00+x*10",
49 "CHx_LVol", "01+x*10",
50 "CHx_FreqH", "02+x*10",
51 "CHx_FreqL", "03+x*10",
52 "CHx_Ctrl", "05+x*10",
53 "CHx_StartH", "06+x*10",
54 "CHx_StartL", "07+x*10",
55 "CHx_EndH", "08+x*10",
56 "CHx_EndL", "09+x*10",
57 "CHx_LoopH", "0A+x*10",
58 "CHx_LoopL", "0B+x*10",
59 "BankA", "1F7",
60 "BankB", "1F1",
61 "BankC", "1F3",
62 "BankD", "1F5",
63 NULL
64 };
65
66 const char** DivPlatformC140::getRegisterSheet() {
67 return is219?regCheatSheetC219:regCheatSheetC140;
68 }
69
70 void DivPlatformC140::acquire_219(short** buf, size_t len) {
71 for (int i=0; i<totalChans; i++) {
72 oscBuf[i]->begin(len);
73 }
74 for (size_t h=0; h<len; h++) {
75 while (!writes.empty()) {
76 QueuedWrite w=writes.front();
77 c219_write(&c219,w.addr,w.val);
78 regPool[w.addr&0x1ff]=w.val;
79 writes.pop();
80 }
81
82 c219_tick(&c219,1);
83 // scale as 16bit
84 c219.lout>>=10;
85 c219.rout>>=10;
86
87 if (c219.lout<-32768) c219.lout=-32768;
88 if (c219.lout>32767) c219.lout=32767;
89
90 if (c219.rout<-32768) c219.rout=-32768;
91 if (c219.rout>32767) c219.rout=32767;
92
93 buf[0][h]=c219.lout;
94 buf[1][h]=c219.rout;
95
96 for (int i=0; i<totalChans; i++) {
97 if (c219.voice[i].inv_lout) {
98 oscBuf[i]->putSample(h,(c219.voice[i].lout-c219.voice[i].rout)>>10);
99 } else {
100 oscBuf[i]->putSample(h,(c219.voice[i].lout+c219.voice[i].rout)>>10);
101 }
102 }
103 }
104 for (int i=0; i<totalChans; i++) {
105 oscBuf[i]->end(len);
106 }
107 }
108
109 void DivPlatformC140::acquire_140(short** buf, size_t len) {
110 for (int i=0; i<totalChans; i++) {
111 oscBuf[i]->begin(len);
112 }
113 for (size_t h=0; h<len; h++) {
114 while (!writes.empty()) {
115 QueuedWrite w=writes.front();
116 c140_write(&c140,w.addr,w.val);
117 regPool[w.addr&0x1ff]=w.val;
118 writes.pop();
119 }
120
121 c140_tick(&c140, 1);
122 // scale as 16bit
123 c140.lout >>= 10;
124 c140.rout >>= 10;
125
126 if (c140.lout<-32768) c140.lout=-32768;
127 if (c140.lout>32767) c140.lout=32767;
128
129 if (c140.rout<-32768) c140.rout=-32768;
130 if (c140.rout>32767) c140.rout=32767;
131
132 buf[0][h]=c140.lout;
133 buf[1][h]=c140.rout;
134
135 for (int i=0; i<totalChans; i++) {
136 oscBuf[i]->putSample(h,(c140.voice[i].lout+c140.voice[i].rout)>>10);
137 }
138 }
139 for (int i=0; i<totalChans; i++) {
140 oscBuf[i]->end(len);
141 }
142 }
143
144 void DivPlatformC140::acquire(short** buf, size_t len) {
145 if (is219) {
146 acquire_219(buf,len);
147 } else {
148 acquire_140(buf,len);
149 }
150 }
151
152 void DivPlatformC140::tick(bool sysTick) {
153 for (int i=0; i<totalChans; i++) {
154 chan[i].std.next();
155 if (chan[i].std.vol.had) {
156 chan[i].outVol=(chan[i].vol*MIN(chan[i].macroVolMul,chan[i].std.vol.val))/chan[i].macroVolMul;
157 chan[i].volChangedL=true;
158 chan[i].volChangedR=true;
159 }
160 if (NEW_ARP_STRAT) {
161 chan[i].handleArp();
162 } else if (chan[i].std.arp.had) {
163 if (!chan[i].inPorta) {
164 chan[i].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val));
165 }
166 chan[i].freqChanged=true;
167 }
168 if (is219) {
169 if (chan[i].std.duty.had) {
170 unsigned char singleByte=(
171 (chan[i].noise?1:0)|
172 (chan[i].invert?2:0)|
173 (chan[i].surround?4:0)
174 );
175 if (singleByte!=(chan[i].std.duty.val&7)) {
176 chan[i].noise=chan[i].std.duty.val&1;
177 chan[i].invert=chan[i].std.duty.val&2;
178 chan[i].surround=chan[i].std.duty.val&4;
179 chan[i].freqChanged=true;
180 chan[i].writeCtrl=true;
181 }
182 }
183 }
184 if (chan[i].std.pitch.had) {
185 if (chan[i].std.pitch.mode) {
186 chan[i].pitch2+=chan[i].std.pitch.val;
187 CLAMP_VAR(chan[i].pitch2,-32768,32767);
188 } else {
189 chan[i].pitch2=chan[i].std.pitch.val;
190 }
191 chan[i].freqChanged=true;
192 }
193 if (chan[i].std.panL.had) {
194 chan[i].chPanL=(255*(chan[i].std.panL.val&255))/chan[i].macroPanMul;
195 chan[i].volChangedL=true;
196 }
197
198 if (chan[i].std.panR.had) {
199 chan[i].chPanR=(255*(chan[i].std.panR.val&255))/chan[i].macroPanMul;
200 chan[i].volChangedR=true;
201 }
202
203 if (chan[i].std.phaseReset.had) {
204 if ((chan[i].std.phaseReset.val==1) && chan[i].active) {
205 chan[i].audPos=0;
206 chan[i].setPos=true;
207 }
208 }
209 if (chan[i].volChangedL) {
210 chan[i].chVolL=(chan[i].outVol*chan[i].chPanL)/255;
211 rWrite(1+(i<<4),chan[i].chVolL);
212 chan[i].volChangedL=false;
213 }
214 if (chan[i].volChangedR) {
215 chan[i].chVolR=(chan[i].outVol*chan[i].chPanR)/255;
216 rWrite(0+(i<<4),chan[i].chVolR);
217 chan[i].volChangedR=false;
218 }
219 if (chan[i].setPos) {
220 // force keyon
221 chan[i].keyOn=true;
222 chan[i].setPos=false;
223 } else {
224 chan[i].audPos=0;
225 }
226 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
227 DivSample* s=parent->getSample(chan[i].sample);
228 unsigned char ctrl=0;
229 double off=(s->centerRate>=1)?((double)s->centerRate/parent->getCenterRate()):1.0;
230 chan[i].freq=(int)(off*parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,false,2,chan[i].pitch2,chipClock,CHIP_FREQBASE));
231 if (chan[i].freq<0) chan[i].freq=0;
232 if (chan[i].freq>65535) chan[i].freq=65535;
233 if (is219) {
234 ctrl|=(chan[i].active?0x80:0)|((s->isLoopable() || chan[i].noise)?0x10:0)|((s->depth==DIV_SAMPLE_DEPTH_C219)?1:0)|(chan[i].invert?0x40:0)|(chan[i].surround?8:0)|(chan[i].noise?4:0);
235 } else {
236 ctrl|=(chan[i].active?0x80:0)|((s->isLoopable())?0x10:0)|((s->depth==DIV_SAMPLE_DEPTH_MULAW)?0x08:0);
237 }
238 if (chan[i].keyOn) {
239 unsigned int bank=0;
240 unsigned int start=0;
241 unsigned int loop=0;
242 unsigned int end=0;
243 if (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) {
244 if (is219) {
245 bank=(sampleOff[chan[i].sample]>>16)&3;
246 start=sampleOff[chan[i].sample]&0xffff;
247 end=MIN(start+(s->length8>>1)-1,65535);
248 } else {
249 bank=(sampleOff[chan[i].sample]>>16)&0xff;
250 start=sampleOff[chan[i].sample]&0xffff;
251 end=MIN(start+s->length8-1,65535);
252 }
253 } else if (chan[i].noise && is219) {
254 bank=groupBank[i>>2];
255 start=0;
256 end=1;
257 }
258 if (chan[i].audPos>0) {
259 start=MIN(start+(MIN(chan[i].audPos,s->length8)>>1),65535);
260 }
261 if (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen && s->isLoopable()) {
262 if (is219) {
263 loop=MIN(start+(s->loopStart>>1),65535);
264 end=MIN(start+(s->loopEnd>>1),65535);
265 } else {
266 loop=MIN(start+s->loopStart+1,65535);
267 end=MIN(start+s->loopEnd+1,65535);
268 }
269 } else if (chan[i].noise && is219) {
270 loop=0;
271 }
272 rWrite(0x05+(i<<4),0); // force keyoff first
273 if (is219) {
274 if (groupBank[i>>2]!=bank) {
275 groupBank[i>>2]=bank;
276 rWrite(0x1f1+(((3+(i>>2))&3)<<1),groupBank[i>>2]);
277 // shut everyone else up
278 for (int j=0; j<4; j++) {
279 int ch=(i&(~3))|j;
280 if (chan[ch].active && !chan[ch].keyOn && (i&3)!=j) {
281 chan[ch].sample=-1;
282 chan[ch].active=false;
283 chan[ch].keyOff=true;
284 chan[ch].macroInit(NULL);
285 rWrite(0x05+(ch<<4),ctrl);
286 }
287 }
288 }
289 } else {
290 switch (bankType) {
291 case 0:
292 bank=((bank&8)<<2)|(bank&7);
293 break;
294 case 1:
295 bank=((bank&0x18)<<1)|(bank&7);
296 break;
297 }
298 rWrite(0x04+(i<<4),bank);
299 }
300 rWrite(0x06+(i<<4),(start>>8)&0xff);
301 rWrite(0x07+(i<<4),start&0xff);
302 rWrite(0x08+(i<<4),(end>>8)&0xff);
303 rWrite(0x09+(i<<4),end&0xff);
304 rWrite(0x0a+(i<<4),(loop>>8)&0xff);
305 rWrite(0x0b+(i<<4),loop&0xff);
306 if (!chan[i].std.vol.had) {
307 chan[i].outVol=chan[i].vol;
308 chan[i].volChangedL=true;
309 chan[i].volChangedR=true;
310 }
311 chan[i].writeCtrl=true;
312 chan[i].keyOn=false;
313 }
314 if (chan[i].keyOff) {
315 chan[i].writeCtrl=true;
316 chan[i].keyOff=false;
317 }
318 if (chan[i].freqChanged) {
319 rWrite(0x02+(i<<4),chan[i].freq>>8);
320 rWrite(0x03+(i<<4),chan[i].freq&0xff);
321 chan[i].freqChanged=false;
322 }
323 if (chan[i].writeCtrl) {
324 rWrite(0x05+(i<<4),ctrl);
325 chan[i].writeCtrl=false;
326 }
327 }
328 }
329
330 for (int i=0; i<4; i++) {
331 bankLabel[i][0]='0'+groupBank[i];
332 }
333 }
334
335 int DivPlatformC140::dispatch(DivCommand c) {
336 switch (c.cmd) {
337 case DIV_CMD_NOTE_ON: {
338 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA);
339 chan[c.chan].macroVolMul=ins->type==DIV_INS_AMIGA?64:255;
340 chan[c.chan].macroPanMul=ins->type==DIV_INS_AMIGA?127:255;
341 if (c.value!=DIV_NOTE_NULL) {
342 chan[c.chan].sample=ins->amiga.getSample(c.value);
343 chan[c.chan].sampleNote=c.value;
344 c.value=ins->amiga.getFreq(c.value);
345 chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote;
346 }
347 if (c.value!=DIV_NOTE_NULL) {
348 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value);
349 }
350 if (chan[c.chan].sample<0 || chan[c.chan].sample>=parent->song.sampleLen) {
351 chan[c.chan].sample=-1;
352 }
353 if (c.value!=DIV_NOTE_NULL) {
354 chan[c.chan].freqChanged=true;
355 chan[c.chan].note=c.value;
356 }
357 chan[c.chan].active=true;
358 chan[c.chan].keyOn=true;
359 chan[c.chan].macroInit(ins);
360 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
361 chan[c.chan].outVol=chan[c.chan].vol;
362 chan[c.chan].volChangedL=true;
363 chan[c.chan].volChangedR=true;
364 }
365 break;
366 }
367 case DIV_CMD_NOTE_OFF:
368 chan[c.chan].sample=-1;
369 chan[c.chan].active=false;
370 chan[c.chan].keyOff=true;
371 chan[c.chan].macroInit(NULL);
372 break;
373 case DIV_CMD_NOTE_OFF_ENV:
374 case DIV_CMD_ENV_RELEASE:
375 chan[c.chan].std.release();
376 break;
377 case DIV_CMD_INSTRUMENT:
378 if (chan[c.chan].ins!=c.value || c.value2==1) {
379 chan[c.chan].ins=c.value;
380 }
381 break;
382 case DIV_CMD_VOLUME:
383 chan[c.chan].vol=c.value;
384 if (!chan[c.chan].std.vol.has) {
385 chan[c.chan].outVol=c.value;
386 }
387 chan[c.chan].volChangedL=true;
388 chan[c.chan].volChangedR=true;
389 break;
390 case DIV_CMD_GET_VOLUME:
391 if (chan[c.chan].std.vol.has) {
392 return chan[c.chan].vol;
393 }
394 return chan[c.chan].outVol;
395 break;
396 case DIV_CMD_STD_NOISE_MODE:
397 if (!is219) break;
398 chan[c.chan].noise=c.value;
399 chan[c.chan].writeCtrl=true;
400 break;
401 case DIV_CMD_SNES_INVERT:
402 if (!is219) break;
403 chan[c.chan].invert=c.value&15;
404 chan[c.chan].surround=c.value>>4;
405 chan[c.chan].writeCtrl=true;
406 break;
407 case DIV_CMD_PANNING:
408 chan[c.chan].chPanL=c.value;
409 chan[c.chan].chPanR=c.value2;
410 chan[c.chan].volChangedL=true;
411 chan[c.chan].volChangedR=true;
412 break;
413 case DIV_CMD_PITCH:
414 chan[c.chan].pitch=c.value;
415 chan[c.chan].freqChanged=true;
416 break;
417 case DIV_CMD_NOTE_PORTA: {
418 int destFreq=NOTE_FREQUENCY(c.value2+chan[c.chan].sampleNoteDelta);
419 bool return2=false;
420 if (destFreq>chan[c.chan].baseFreq) {
421 chan[c.chan].baseFreq+=c.value;
422 if (chan[c.chan].baseFreq>=destFreq) {
423 chan[c.chan].baseFreq=destFreq;
424 return2=true;
425 }
426 } else {
427 chan[c.chan].baseFreq-=c.value;
428 if (chan[c.chan].baseFreq<=destFreq) {
429 chan[c.chan].baseFreq=destFreq;
430 return2=true;
431 }
432 }
433 chan[c.chan].freqChanged=true;
434 if (return2) {
435 chan[c.chan].inPorta=false;
436 return 2;
437 }
438 break;
439 }
440 case DIV_CMD_LEGATO: {
441 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val-12):(0)));
442 chan[c.chan].freqChanged=true;
443 chan[c.chan].note=c.value;
444 break;
445 }
446 case DIV_CMD_PRE_PORTA:
447 if (chan[c.chan].active && c.value2) {
448 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA));
449 }
450 if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=NOTE_FREQUENCY(chan[c.chan].note);
451 chan[c.chan].inPorta=c.value;
452 break;
453 case DIV_CMD_SAMPLE_POS:
454 chan[c.chan].audPos=c.value;
455 chan[c.chan].setPos=true;
456 break;
457 case DIV_CMD_GET_VOLMAX:
458 return 255;
459 break;
460 case DIV_CMD_MACRO_OFF:
461 chan[c.chan].std.mask(c.value,true);
462 break;
463 case DIV_CMD_MACRO_ON:
464 chan[c.chan].std.mask(c.value,false);
465 break;
466 case DIV_CMD_MACRO_RESTART:
467 chan[c.chan].std.restart(c.value);
468 break;
469 default:
470 break;
471 }
472 return 1;
473 }
474
475 void DivPlatformC140::muteChannel(int ch, bool mute) {
476 isMuted[ch]=mute;
477 if (is219) {
478 c219.voice[ch].muted=mute;
479 } else {
480 c140.voice[ch].muted=mute;
481 }
482 }
483
484 void DivPlatformC140::forceIns() {
485 for (int i=0; i<totalChans; i++) {
486 chan[i].insChanged=true;
487 chan[i].freqChanged=true;
488 chan[i].volChangedL=true;
489 chan[i].volChangedR=true;
490 chan[i].sample=-1;
491 }
492 if (is219) {
493 // restore banks
494 for (int i=0; i<4; i++) {
495 rWrite(0x1f1+(((3+i)&3)<<1),groupBank[i]);
496 }
497 }
498 }
499
500 void* DivPlatformC140::getChanState(int ch) {
501 return &chan[ch];
502 }
503
504 DivMacroInt* DivPlatformC140::getChanMacroInt(int ch) {
505 return &chan[ch].std;
506 }
507
508 unsigned short DivPlatformC140::getPan(int ch) {
509 return (chan[ch].chPanL<<8)|(chan[ch].chPanR);
510 }
511
512 DivDispatchOscBuffer* DivPlatformC140::getOscBuffer(int ch) {
513 return oscBuf[ch];
514 }
515
516 void DivPlatformC140::reset() {
517 while (!writes.empty()) writes.pop();
518 memset(regPool,0,512);
519 if (is219) {
520 c219_reset(&c219);
521 } else {
522 c140_reset(&c140);
523 }
524 for (int i=0; i<totalChans; i++) {
525 chan[i]=DivPlatformC140::Channel();
526 chan[i].std.setEngine(parent);
527 rWrite(0x05+(i<<4),0);
528 }
529 for (int i=0; i<4; i++) {
530 groupBank[i]=0;
531 }
532 }
533
534 int DivPlatformC140::getOutputCount() {
535 return 2;
536 }
537
538 void DivPlatformC140::notifyInsChange(int ins) {
539 for (int i=0; i<totalChans; i++) {
540 if (chan[i].ins==ins) {
541 chan[i].insChanged=true;
542 }
543 }
544 }
545
546 void DivPlatformC140::notifyWaveChange(int wave) {
547
548 }
549
550 void DivPlatformC140::notifyInsDeletion(void* ins) {
551 for (int i=0; i<totalChans; i++) {
552 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
553 }
554 }
555
556 void DivPlatformC140::poke(unsigned int addr, unsigned short val) {
557 rWrite(addr,val);
558 }
559
560 void DivPlatformC140::poke(std::vector<DivRegWrite>& wlist) {
561 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
562 }
563
564 unsigned char* DivPlatformC140::getRegisterPool() {
565 return regPool;
566 }
567
568 int DivPlatformC140::getRegisterPoolSize() {
569 return 512;
570 }
571
572 float DivPlatformC140::getPostAmp() {
573 return 3.0f;
574 }
575
576 void DivPlatformC140::getPaired(int ch, std::vector<DivChannelPair>& ret) {
577 if (!is219) return;
578 if ((ch&3)==0) {
579 ret.push_back(DivChannelPair(bankLabel[ch>>2],ch+1,ch+2,ch+3,-1,-1,-1,-1,-1));
580 }
581 }
582
583 const void* DivPlatformC140::getSampleMem(int index) {
584 return index == 0 ? sampleMem : NULL;
585 }
586
587 size_t DivPlatformC140::getSampleMemCapacity(int index) {
588 if (index!=0) return 0;
589 if (is219) return 524288;
590 switch (bankType) {
591 case 0:
592 return 2097152;
593 case 1:
594 return 4194304;
595 }
596 return 16777216;
597 }
598
599 size_t DivPlatformC140::getSampleMemUsage(int index) {
600 return index == 0 ? sampleMemLen : 0;
601 }
602
603 bool DivPlatformC140::isSampleLoaded(int index, int sample) {
604 if (index!=0) return false;
605 if (sample<0 || sample>32767) return false;
606 return sampleLoaded[sample];
607 }
608
609 const DivMemoryComposition* DivPlatformC140::getMemCompo(int index) {
610 if (index!=0) return NULL;
611 return &memCompo;
612 }
613
614 void DivPlatformC140::renderSamples(int sysID) {
615 memset(sampleMem,0,is219?524288:16777216);
616 memset(sampleOff,0,32768*sizeof(unsigned int));
617 memset(sampleLoaded,0,32768*sizeof(bool));
618
619 memCompo=DivMemoryComposition();
620 memCompo.name="Sample ROM";
621
622 size_t memPos=0;
623 for (int i=0; i<parent->song.sampleLen; i++) {
624 DivSample* s=parent->song.sample[i];
625 if (!s->renderOn[0][sysID]) {
626 sampleOff[i]=0;
627 continue;
628 }
629
630 if (is219) { // C219 (8-bit)
631 unsigned int length=s->length8+4;
632 // fit sample size to single bank size
633 if (length>131072) {
634 length=131072;
635 }
636 if (length&1) length++;
637 if ((memPos&0xfe0000)!=((memPos+length)&0xfe0000)) {
638 memPos=((memPos+0x1ffff)&0xfe0000);
639 }
640 logV("%d",length);
641 if (memPos>=(getSampleMemCapacity())) {
642 logW("out of C219 memory for sample %d!",i);
643 break;
644 }
645 if (memPos+length>=(getSampleMemCapacity())) {
646 length=getSampleMemCapacity()-memPos;
647 logW("out of C219 memory for sample %d!",i);
648 }
649 if (s->depth==DIV_SAMPLE_DEPTH_C219) {
650 unsigned char next=0;
651 unsigned int sPos=0;
652 for (unsigned int i=0; i<length; i++) {
653 if (sPos<s->lengthC219) {
654 next=s->dataC219[sPos++];
655 if (s->isLoopable()) {
656 if ((int)sPos>=s->loopEnd) {
657 sPos=s->loopStart;
658 }
659 }
660 }
661 sampleMem[(memPos+i)^1]=next;
662 }
663 } else {
664 signed char next=0;
665 unsigned int sPos=0;
666 for (unsigned int i=0; i<length; i++) {
667 if (sPos<s->length8) {
668 next=s->data8[sPos++];
669 if (s->isLoopable()) {
670 if ((int)sPos>=s->loopEnd) {
671 sPos=s->loopStart;
672 }
673 }
674 }
675 sampleMem[(memPos+i)^1]=next;
676 }
677 }
678 sampleOff[i]=memPos>>1;
679 sampleLoaded[i]=true;
680 memCompo.entries.push_back(DivMemoryEntry((DivMemoryEntryType)(DIV_MEMORY_BANK0+((memPos>>17)&3)),"Sample",i,memPos,memPos+length));
681 memPos+=length;
682 } else { // C140 (16-bit)
683 unsigned int length=s->length16+4;
684 // fit sample size to single bank size
685 if (length>(131072)) {
686 length=131072;
687 }
688 if ((memPos&0xfe0000)!=((memPos+length)&0xfe0000)) {
689 memPos=((memPos+0x1ffff)&0xfe0000);
690 }
691 if (memPos>=(getSampleMemCapacity())) {
692 logW("out of C140 memory for sample %d!",i);
693 break;
694 }
695 // why is C140 not G.711-compliant? this weird bit mangling had me puzzled for 3 hours...
696 if (memPos+length>=(getSampleMemCapacity())) {
697 length=getSampleMemCapacity()-memPos;
698 logW("out of C140 memory for sample %d!",i);
699 }
700 if (s->depth==DIV_SAMPLE_DEPTH_MULAW) {
701 for (unsigned int i=0; i<length; i+=2) {
702 if ((i>>1)>=s->lengthMuLaw) break;
703 unsigned char x=s->dataMuLaw[i>>1]^0xff;
704 if (x&0x80) x^=15;
705 unsigned char c140Mu=(x&0x80)|((x&15)<<3)|((x&0x70)>>4);
706 sampleMem[i+memPos]=0;
707 sampleMem[1+i+memPos]=c140Mu;
708 }
709 } else {
710 short next=0;
711 unsigned int sPos=0;
712 for (unsigned int i=0; i<length; i+=2) {
713 if (sPos<s->samples) {
714 next=s->data16[sPos++];
715 if (s->isLoopable()) {
716 if ((int)sPos>=s->loopEnd) {
717 sPos=s->loopStart;
718 }
719 }
720 }
721 sampleMem[memPos+i]=((unsigned short)next);
722 sampleMem[memPos+i+1]=((unsigned short)next)>>8;
723 }
724 }
725 sampleOff[i]=memPos>>1;
726 sampleLoaded[i]=true;
727 memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"Sample",i,memPos,memPos+length));
728 memPos+=length;
729 }
730 }
731 sampleMemLen=memPos+256;
732
733 memCompo.used=sampleMemLen;
734 memCompo.capacity=getSampleMemCapacity(0);
735 }
736
737 void DivPlatformC140::set219(bool is_219) {
738 is219=is_219;
739 totalChans=is219?16:24;
740 }
741
742 int DivPlatformC140::getClockRangeMin() {
743 if (is219) return 1000000;
744 return MIN_CUSTOM_CLOCK;
745 }
746
747 int DivPlatformC140::getClockRangeMax() {
748 if (is219) return 100000000;
749 return MAX_CUSTOM_CLOCK;
750 }
751
752 void DivPlatformC140::setFlags(const DivConfig& flags) {
753 if (is219) {
754 chipClock=50113000; // 50.113MHz clock input in Namco NA-1/NA-2 PCB
755 CHECK_CUSTOM_CLOCK;
756 rate=chipClock/1136; // assumed as ~44100hz
757 } else {
758 chipClock=32000*256; // 8.192MHz and 12.288MHz input, verified from Assault Schematics
759 CHECK_CUSTOM_CLOCK;
760 rate=chipClock/192;
761 }
762 bankType=flags.getInt("bankType",0);
763 if (!is219) {
764 c140_bank_type(&c140,bankType);
765 }
766 for (int i=0; i<totalChans; i++) {
767 oscBuf[i]->setRate(rate);
768 }
769 }
770
771 int DivPlatformC140::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
772 parent=p;
773 dumpWrites=false;
774 skipRegisterWrites=false;
775 bankType=0;
776
777 memset(bankLabel,0,16);
778
779 for (int i=0; i<totalChans; i++) {
780 isMuted[i]=false;
781 oscBuf[i]=new DivDispatchOscBuffer;
782 }
783 sampleMem=new unsigned char[is219?524288:16777216];
784 sampleMemLen=0;
785 if (is219) {
786 c219_init(&c219);
787 c219.sample_mem=(signed char*)sampleMem;
788 } else {
789 c140_init(&c140);
790 c140.sample_mem=(short*)sampleMem;
791 }
792 setFlags(flags);
793 reset();
794
795 return totalChans;
796 }
797
798 void DivPlatformC140::quit() {
799 delete[] sampleMem;
800 for (int i=0; i<totalChans; i++) {
801 delete oscBuf[i];
802 }
803 }
804
805 // initialization of important arrays
806 DivPlatformC140::DivPlatformC140() {
807 sampleOff=new unsigned int[32768];
808 sampleLoaded=new bool[32768];
809 }
810
811 DivPlatformC140::~DivPlatformC140() {
812 delete[] sampleOff;
813 delete[] sampleLoaded;
814 }