| 1 |
/* |
| 2 |
* Copyright (C) 2010-2019 Fabio Cavallo (aka FHorse) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <string.h> |
| 20 |
#include "apu.h" |
| 21 |
#include "mmc5.h" |
| 22 |
|
| 23 |
enum { MODE0, MODE1, MODE2, MODE3 }; |
| 24 |
enum { CHR_S, CHR_B }; |
| 25 |
enum { SPLIT_LEFT, SPLIT_RIGHT = 0x40 }; |
| 26 |
|
| 27 |
BYTE prg_ram_mode; |
| 28 |
|
| 29 |
void map_init_MMC5(struct _mmc5* mmc5) { |
| 30 |
memset(mmc5,0,sizeof(struct _mmc5)); |
| 31 |
|
| 32 |
mmc5->S3.frequency = 1; |
| 33 |
mmc5->S4.frequency = 1; |
| 34 |
mmc5->S3.length.enabled = 0; |
| 35 |
mmc5->S3.length.value = 0; |
| 36 |
mmc5->S4.length.enabled = 0; |
| 37 |
mmc5->S4.length.value = 0; |
| 38 |
|
| 39 |
mmc5->timestamp = 0; |
| 40 |
mmc5->lastSample = 0; |
| 41 |
mmc5->bb = NULL; |
| 42 |
mmc5->oscBuf[0] = NULL; |
| 43 |
mmc5->oscBuf[1] = NULL; |
| 44 |
mmc5->oscBuf[2] = NULL; |
| 45 |
|
| 46 |
mmc5->S3.timer = 2048; |
| 47 |
mmc5->S4.timer = 2048; |
| 48 |
} |
| 49 |
void extcl_cpu_wr_mem_MMC5(struct _mmc5* mmc5, int ts, WORD address, BYTE value) { |
| 50 |
if (address < 0x5000) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
extcl_apu_tick_MMC5(mmc5,ts); |
| 55 |
|
| 56 |
switch (address) { |
| 57 |
case 0x5000: |
| 58 |
square_reg0(mmc5->S3); |
| 59 |
return; |
| 60 |
case 0x5001: |
| 61 |
/* lo sweep non e' utilizzato */ |
| 62 |
return; |
| 63 |
case 0x5002: |
| 64 |
square_reg2(mmc5->S3); |
| 65 |
return; |
| 66 |
case 0x5003: |
| 67 |
square_reg3(mmc5->S3,0); |
| 68 |
return; |
| 69 |
case 0x5004: |
| 70 |
square_reg0(mmc5->S4); |
| 71 |
return; |
| 72 |
case 0x5005: |
| 73 |
/* lo sweep non e' utilizzato */ |
| 74 |
return; |
| 75 |
case 0x5006: |
| 76 |
square_reg2(mmc5->S4); |
| 77 |
return; |
| 78 |
case 0x5007: |
| 79 |
square_reg3(mmc5->S4,0); |
| 80 |
return; |
| 81 |
case 0x5010: |
| 82 |
mmc5->pcm.enabled = ~value & 0x01; |
| 83 |
mmc5->pcm.output = 0; |
| 84 |
if (mmc5->pcm.enabled) { |
| 85 |
mmc5->pcm.output = mmc5->pcm.amp; |
| 86 |
mmc5->oscBuf[2]->putSample(mmc5->timestamp,mmc5->muted[2]?0:(mmc5->pcm.output<<7)); |
| 87 |
} |
| 88 |
//mmc5->clocked = TRUE; |
| 89 |
return; |
| 90 |
case 0x5011: |
| 91 |
mmc5->pcm.amp = value; |
| 92 |
mmc5->pcm.output = 0; |
| 93 |
if (mmc5->pcm.enabled) { |
| 94 |
mmc5->pcm.output = mmc5->pcm.amp; |
| 95 |
mmc5->oscBuf[2]->putSample(mmc5->timestamp,mmc5->muted[2]?0:(mmc5->pcm.output<<7)); |
| 96 |
} |
| 97 |
//mmc5->clocked = TRUE; |
| 98 |
return; |
| 99 |
case 0x5015: |
| 100 |
if (!(mmc5->S3.length.enabled = value & 0x01)) { |
| 101 |
mmc5->S3.length.value = 0; |
| 102 |
} |
| 103 |
if (!(mmc5->S4.length.enabled = value & 0x02)) { |
| 104 |
mmc5->S4.length.value = 0; |
| 105 |
} |
| 106 |
return; |
| 107 |
} |
| 108 |
} |
| 109 |
void extcl_length_clock_MMC5(struct _mmc5* mmc5) { |
| 110 |
length_run(mmc5->S3) |
| 111 |
length_run(mmc5->S4) |
| 112 |
} |
| 113 |
void extcl_envelope_clock_MMC5(struct _mmc5* mmc5) { |
| 114 |
envelope_run(mmc5->S3) |
| 115 |
envelope_run(mmc5->S4) |
| 116 |
} |
| 117 |
void extcl_apu_tick_MMC5(struct _mmc5* mmc5, int len) { |
| 118 |
if (len<=mmc5->timestamp) return; |
| 119 |
|
| 120 |
int rem=len-mmc5->timestamp; |
| 121 |
if (rem>1) { |
| 122 |
// output now just in case |
| 123 |
int sample=mmc5->muted[0]?0:(mmc5->S3.output*10); |
| 124 |
if (!mmc5->muted[1]) { |
| 125 |
sample+=mmc5->S4.output*10; |
| 126 |
} |
| 127 |
if (!mmc5->muted[2]) { |
| 128 |
sample+=mmc5->pcm.output*2; |
| 129 |
} |
| 130 |
if (sample!=mmc5->lastSample) { |
| 131 |
blip_add_delta(mmc5->bb,mmc5->timestamp,sample-mmc5->lastSample); |
| 132 |
mmc5->lastSample=sample; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
while (rem>0) { |
| 137 |
// predict advance |
| 138 |
int advance=rem; |
| 139 |
if (advance>mmc5->S3.frequency) { |
| 140 |
advance=mmc5->S3.frequency; |
| 141 |
} |
| 142 |
if (advance>mmc5->S4.frequency) { |
| 143 |
advance=mmc5->S4.frequency; |
| 144 |
} |
| 145 |
if (advance<1) advance=1; |
| 146 |
|
| 147 |
int postTS=mmc5->timestamp+advance-1; |
| 148 |
|
| 149 |
// SQUARE 3 TICK |
| 150 |
if (!(mmc5->S3.frequency-=advance)) { |
| 151 |
square_output(mmc5->S3, 0) |
| 152 |
mmc5->S3.frequency = (mmc5->S3.timer + 1) << 1; |
| 153 |
mmc5->S3.sequencer = (mmc5->S3.sequencer + 1) & 0x07; |
| 154 |
mmc5->oscBuf[0]->putSample(postTS,mmc5->muted[0]?0:(mmc5->S3.output<<11)); |
| 155 |
} |
| 156 |
|
| 157 |
// SQUARE 4 TICK |
| 158 |
if (!(mmc5->S4.frequency-=advance)) { |
| 159 |
square_output(mmc5->S4, 0) |
| 160 |
mmc5->S4.frequency = (mmc5->S4.timer + 1) << 1; |
| 161 |
mmc5->S4.sequencer = (mmc5->S4.sequencer + 1) & 0x07; |
| 162 |
mmc5->oscBuf[1]->putSample(postTS,mmc5->muted[1]?0:(mmc5->S4.output<<11)); |
| 163 |
} |
| 164 |
|
| 165 |
// output sample |
| 166 |
mmc5->timestamp=postTS; |
| 167 |
int sample=mmc5->muted[0]?0:(mmc5->S3.output*10); |
| 168 |
if (!mmc5->muted[1]) { |
| 169 |
sample+=mmc5->S4.output*10; |
| 170 |
} |
| 171 |
if (!mmc5->muted[2]) { |
| 172 |
sample+=mmc5->pcm.output*2; |
| 173 |
} |
| 174 |
if (sample!=mmc5->lastSample) { |
| 175 |
blip_add_delta(mmc5->bb,mmc5->timestamp,sample-mmc5->lastSample); |
| 176 |
mmc5->lastSample=sample; |
| 177 |
} |
| 178 |
|
| 179 |
rem-=advance; |
| 180 |
mmc5->timestamp++; |
| 181 |
} |
| 182 |
mmc5->timestamp=len; |
| 183 |
} |