| 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 |
#ifndef CPU_INLINE_H_ |
| 20 |
#define CPU_INLINE_H_ |
| 21 |
|
| 22 |
#include <stdlib.h> |
| 23 |
#include "apu.h" |
| 24 |
#include "fds.h" |
| 25 |
|
| 26 |
#define mod_cycles_op(op, vl) cpu.cycles op vl |
| 27 |
#define r2006_during_rendering()\ |
| 28 |
if (!ppu.vblank && r2001.visible && (ppu.frame_y > ppu_sclines.vint) &&\ |
| 29 |
(ppu.screen_y < SCR_LINES)) {\ |
| 30 |
_r2006_during_rendering()\ |
| 31 |
} else {\ |
| 32 |
r2006.value += r2000.r2006_inc;\ |
| 33 |
} |
| 34 |
#define _r2006_during_rendering()\ |
| 35 |
r2006_inc()\ |
| 36 |
if ((r2006.value & 0x1F) == 0x1F) {\ |
| 37 |
r2006.value ^= 0x41F;\ |
| 38 |
} else {\ |
| 39 |
r2006.value++;\ |
| 40 |
} |
| 41 |
|
| 42 |
INLINE static void apu_wr_reg(struct NESAPU* a, int ts, WORD address, BYTE value) { |
| 43 |
apu_tick(a,ts); |
| 44 |
|
| 45 |
if (!(address & 0x0010)) { |
| 46 |
/* -------------------- square 1 --------------------*/ |
| 47 |
if (address <= 0x4003) { |
| 48 |
if (address == 0x4000) { |
| 49 |
square_reg0(a->S1); |
| 50 |
return; |
| 51 |
} |
| 52 |
if (address == 0x4001) { |
| 53 |
square_reg1(a->S1); |
| 54 |
sweep_silence(a->S1) |
| 55 |
return; |
| 56 |
} |
| 57 |
if (address == 0x4002) { |
| 58 |
square_reg2(a->S1); |
| 59 |
sweep_silence(a->S1) |
| 60 |
return; |
| 61 |
} |
| 62 |
if (address == 0x4003) { |
| 63 |
square_reg3(a->S1,a->apu.length_clocked); |
| 64 |
sweep_silence(a->S1) |
| 65 |
return; |
| 66 |
} |
| 67 |
return; |
| 68 |
} |
| 69 |
/* -------------------- square 2 --------------------*/ |
| 70 |
if (address <= 0x4007) { |
| 71 |
if (address == 0x4004) { |
| 72 |
square_reg0(a->S2); |
| 73 |
return; |
| 74 |
} |
| 75 |
if (address == 0x4005) { |
| 76 |
square_reg1(a->S2); |
| 77 |
sweep_silence(a->S2) |
| 78 |
return; |
| 79 |
} |
| 80 |
if (address == 0x4006) { |
| 81 |
square_reg2(a->S2); |
| 82 |
sweep_silence(a->S2) |
| 83 |
return; |
| 84 |
} |
| 85 |
if (address == 0x4007) { |
| 86 |
square_reg3(a->S2,a->apu.length_clocked); |
| 87 |
sweep_silence(a->S2) |
| 88 |
return; |
| 89 |
} |
| 90 |
return; |
| 91 |
} |
| 92 |
/* -------------------- triangle --------------------*/ |
| 93 |
if (address <= 0x400B) { |
| 94 |
if (address == 0x4008) { |
| 95 |
/* length counter */ |
| 96 |
/* |
| 97 |
* il triangle ha una posizione diversa per il |
| 98 |
* flag LCHalt. |
| 99 |
*/ |
| 100 |
a->TR.length.halt = value & 0x80; |
| 101 |
/* linear counter */ |
| 102 |
a->TR.linear.reload = value & 0x7F; |
| 103 |
return; |
| 104 |
} |
| 105 |
if (address == 0x400A) { |
| 106 |
/* timer (low 8 bits) */ |
| 107 |
a->TR.timer = (a->TR.timer & 0x0700) | value; |
| 108 |
if (!a->TR.firstTime) { |
| 109 |
a->TR.firstTime=1; |
| 110 |
a->TR.frequency=1; |
| 111 |
} |
| 112 |
return; |
| 113 |
} |
| 114 |
if (address == 0x400B) { |
| 115 |
/* length counter */ |
| 116 |
/* |
| 117 |
* se non disabilitato, una scrittura in |
| 118 |
* questo registro, carica immediatamente il |
| 119 |
* length counter del canale, tranne nel caso |
| 120 |
* in cui la scrittura avvenga nello stesso |
| 121 |
* momento del clock di un length counter e |
| 122 |
* con il length diverso da zero. |
| 123 |
*/ |
| 124 |
if (a->TR.length.enabled && !(a->apu.length_clocked && a->TR.length.value)) { |
| 125 |
a->TR.length.value = length_table[value >> 3]; |
| 126 |
} |
| 127 |
/* timer (high 3 bits) */ |
| 128 |
a->TR.timer = (a->TR.timer & 0x00FF) | ((value & 0x07) << 8); |
| 129 |
/* |
| 130 |
* scrivendo in questo registro si setta |
| 131 |
* automaticamente l'halt flag del triangle. |
| 132 |
*/ |
| 133 |
a->TR.linear.halt = TRUE; |
| 134 |
if (!a->TR.firstTime) { |
| 135 |
a->TR.firstTime=1; |
| 136 |
a->TR.frequency=1; |
| 137 |
} |
| 138 |
return; |
| 139 |
} |
| 140 |
return; |
| 141 |
} |
| 142 |
/* --------------------- noise ----------------------*/ |
| 143 |
if (address <= 0x400F) { |
| 144 |
if (address == 0x400C) { |
| 145 |
a->NS.length.halt = value & 0x20; |
| 146 |
/* envelope */ |
| 147 |
a->NS.envelope.constant_volume = value & 0x10; |
| 148 |
a->NS.envelope.divider = value & 0x0F; |
| 149 |
return; |
| 150 |
} |
| 151 |
if (address == 0x400E) { |
| 152 |
a->NS.mode = value & 0x80; |
| 153 |
a->NS.timer = value & 0x0F; |
| 154 |
if (!a->NS.firstTime) { |
| 155 |
a->NS.firstTime=1; |
| 156 |
a->NS.frequency=1; |
| 157 |
} |
| 158 |
return; |
| 159 |
} |
| 160 |
if (address == 0x400F) { |
| 161 |
/* |
| 162 |
* se non disabilitato, una scrittura in |
| 163 |
* questo registro, carica immediatamente il |
| 164 |
* length counter del canale, tranne nel caso |
| 165 |
* in cui la scrittura avvenga nello stesso |
| 166 |
* momento del clock di un length counter e |
| 167 |
* con il length diverso da zero. |
| 168 |
*/ |
| 169 |
if (a->NS.length.enabled && !(a->apu.length_clocked && a->NS.length.value)) { |
| 170 |
a->NS.length.value = length_table[value >> 3]; |
| 171 |
} |
| 172 |
/* envelope */ |
| 173 |
a->NS.envelope.enabled = TRUE; |
| 174 |
return; |
| 175 |
} |
| 176 |
return; |
| 177 |
} |
| 178 |
return; |
| 179 |
} else { |
| 180 |
/* ---------------------- DMC -----------------------*/ |
| 181 |
if (address <= 0x4013) { |
| 182 |
if (address == 0x4010) { |
| 183 |
a->DMC.irq_enabled = value & 0x80; |
| 184 |
/* se l'irq viene disabilitato allora... */ |
| 185 |
if (!a->DMC.irq_enabled) { |
| 186 |
/* ...azzero l'interrupt flag del DMC */ |
| 187 |
a->r4015.value &= 0x7F; |
| 188 |
} |
| 189 |
a->DMC.loop = value & 0x40; |
| 190 |
a->DMC.rate_index = value & 0x0F; |
| 191 |
return; |
| 192 |
} |
| 193 |
if (address == 0x4011) { |
| 194 |
BYTE save = a->DMC.counter; |
| 195 |
|
| 196 |
value &= 0x7F; |
| 197 |
|
| 198 |
/* |
| 199 |
* questa lo faccio perche' in alcuni giochi come Batman, |
| 200 |
* Ninja Gaiden 3, Castlevania II ed altri, producono |
| 201 |
* un popping del suono fastidioso; |
| 202 |
* from Fceu doc: |
| 203 |
* Why do some games make a popping sound (Batman, Ninja Gaiden 3, |
| 204 |
* Castlevania II etc.)? These games do a very crude drum imitation |
| 205 |
* by causing a large jump in the output level for a short period of |
| 206 |
* time via the register at $4011. The analog filters on a real |
| 207 |
* Famicom make it sound decent(better). I have not completely |
| 208 |
* emulated these filters. |
| 209 |
* (Xodnizel) |
| 210 |
*/ |
| 211 |
if (a->r4011.frames > 1) { |
| 212 |
a->r4011.output = (value - save) >> 3; |
| 213 |
a->DMC.counter = a->DMC.output = save + a->r4011.output; |
| 214 |
} else { |
| 215 |
a->DMC.counter = a->DMC.output = value; |
| 216 |
} |
| 217 |
a->oscBuf[4]->putSample(a->timestamp,a->muted[4]?0:(a->DMC.output<<8)); |
| 218 |
|
| 219 |
a->r4011.cycles = a->r4011.frames = 0; |
| 220 |
a->r4011.value = value; |
| 221 |
return; |
| 222 |
} |
| 223 |
if (address == 0x4012) { |
| 224 |
a->DMC.address_start = (value << 6) | 0xC000; |
| 225 |
return; |
| 226 |
} |
| 227 |
if (address == 0x4013) { |
| 228 |
/* sample length */ |
| 229 |
a->DMC.length = (value << 4) | 0x01; |
| 230 |
return; |
| 231 |
} |
| 232 |
return; |
| 233 |
} |
| 234 |
/* --------------------------------------------------*/ |
| 235 |
if (address == 0x4015) { |
| 236 |
/* |
| 237 |
* 76543210 |
| 238 |
* || ||||| |
| 239 |
* || ||||+- Pulse channel 1's length counter enabled flag |
| 240 |
* || |||+-- Pulse channel 2's length counter enabled flag |
| 241 |
* || ||+--- Triangle channel's length counter enabled flag |
| 242 |
* || |+---- Noise channel's length counter enabled flag |
| 243 |
* || +----- If clear, the DMC's bytes remaining is set to 0, |
| 244 |
* || otherwise the DMC sample is restarted only if the |
| 245 |
* || DMC's bytes remaining is 0 |
| 246 |
* |+------- Frame interrupt flag |
| 247 |
* +-------- DMC interrupt flag |
| 248 |
*/ |
| 249 |
/* |
| 250 |
* dopo la write il bit 7 (dmc flag) deve |
| 251 |
* essere azzerato mentre lascio inalterati |
| 252 |
* i bit 5 e 6. |
| 253 |
*/ |
| 254 |
a->r4015.value = (a->r4015.value & 0x60) | (value & 0x1F); |
| 255 |
/* |
| 256 |
* quando il flag di abilitazione del length |
| 257 |
* counter di ogni canale e' a 0, il counter |
| 258 |
* dello stesso canale e' immediatamente azzerato. |
| 259 |
*/ |
| 260 |
if (!(a->S1.length.enabled = a->r4015.value & 0x01)) { |
| 261 |
a->S1.length.value = 0; |
| 262 |
} |
| 263 |
if (!(a->S2.length.enabled = a->r4015.value & 0x02)) { |
| 264 |
a->S2.length.value = 0; |
| 265 |
} |
| 266 |
if (!(a->TR.length.enabled = a->r4015.value & 0x04)) { |
| 267 |
a->TR.length.value = 0; |
| 268 |
} |
| 269 |
if (!(a->NS.length.enabled = a->r4015.value & 0x08)) { |
| 270 |
a->NS.length.value = 0; |
| 271 |
} |
| 272 |
/* |
| 273 |
* se il bit 4 e' 0 allora devo azzerare i bytes |
| 274 |
* rimanenti del DMC, alrimenti devo riavviare |
| 275 |
* la lettura dei sample DMC solo nel caso che |
| 276 |
* in cui i bytes rimanenti siano a 0. |
| 277 |
*/ |
| 278 |
if (!(a->r4015.value & 0x10)) { |
| 279 |
a->DMC.remain = 0; |
| 280 |
a->DMC.empty = TRUE; |
| 281 |
} else if (!a->DMC.remain) { |
| 282 |
a->DMC.remain = a->DMC.length; |
| 283 |
a->DMC.address = a->DMC.address_start; |
| 284 |
} |
| 285 |
return; |
| 286 |
} |
| 287 |
|
| 288 |
#if defined (VECCHIA_GESTIONE_JITTER) |
| 289 |
if (address == 0x4017) { |
| 290 |
/* APU frame counter */ |
| 291 |
r4017.jitter.value = value; |
| 292 |
/* |
| 293 |
* nell'2A03 se la scrittura del $4017 avviene |
| 294 |
* in un ciclo pari, allora l'effettiva modifica |
| 295 |
* avverra' nel ciclo successivo. |
| 296 |
*/ |
| 297 |
if (cpu.odd_cycle) { |
| 298 |
r4017.jitter.delay = TRUE; |
| 299 |
} else { |
| 300 |
r4017.jitter.delay = FALSE; |
| 301 |
r4017_jitter(); |
| 302 |
} |
| 303 |
return; |
| 304 |
} |
| 305 |
#else |
| 306 |
if (address == 0x4017) { |
| 307 |
/* APU frame counter */ |
| 308 |
a->r4017.jitter.value = value; |
| 309 |
/* |
| 310 |
* nell'2A03 se la scrittura del $4017 avviene |
| 311 |
* in un ciclo pari, allora l'effettiva modifica |
| 312 |
* avverra' nel ciclo successivo. |
| 313 |
*/ |
| 314 |
if (a->apu.odd_cycle) { |
| 315 |
a->r4017.jitter.delay = TRUE; |
| 316 |
} else { |
| 317 |
a->r4017.jitter.delay = FALSE; |
| 318 |
r4017_jitter(1) |
| 319 |
r4017_reset_frame() |
| 320 |
} |
| 321 |
return; |
| 322 |
} |
| 323 |
#endif |
| 324 |
} |
| 325 |
|
| 326 |
#if defined (DEBUG) |
| 327 |
//fprintf(stderr, "Alert: Attempt to write APU port %04X\n", address); |
| 328 |
#endif |
| 329 |
|
| 330 |
return; |
| 331 |
} |
| 332 |
|
| 333 |
INLINE static BYTE fds_wr_mem(struct _fds* fds, WORD address, BYTE value) { |
| 334 |
if (address == 0x4023) { |
| 335 |
fds->enabled_snd_reg=value&0x02; |
| 336 |
} |
| 337 |
if ((address >= 0x4040) && (address <= 0x408A)) { |
| 338 |
if (fds->enabled_snd_reg) { |
| 339 |
if ((address >= 0x4040) && (address <= 0x407F)) { |
| 340 |
fds->snd.wave.data[address & 0x003F] = value & 0x3F; |
| 341 |
return (TRUE); |
| 342 |
} |
| 343 |
if (address == 0x4080) { |
| 344 |
fds->snd.volume.speed = value & 0x3F; |
| 345 |
fds->snd.volume.increase = value & 0x40; |
| 346 |
fds->snd.volume.mode = value & 0x80; |
| 347 |
return (TRUE); |
| 348 |
} |
| 349 |
if (address == 0x4082) { |
| 350 |
fds->snd.main.frequency = (fds->snd.main.frequency & 0xFF00) | value; |
| 351 |
return (TRUE); |
| 352 |
} |
| 353 |
if (address == 0x4083) { |
| 354 |
fds->snd.main.frequency = ((value & 0x0F) << 8) | (fds->snd.main.frequency & 0x00FF); |
| 355 |
fds->snd.envelope.disabled = value & 0x40; |
| 356 |
fds->snd.main.silence = value & 0x80; |
| 357 |
return (TRUE); |
| 358 |
} |
| 359 |
if (address == 0x4084) { |
| 360 |
fds->snd.sweep.speed = value & 0x3F; |
| 361 |
fds->snd.sweep.increase = value & 0x40; |
| 362 |
fds->snd.sweep.mode = value & 0x80; |
| 363 |
return (TRUE); |
| 364 |
} |
| 365 |
if (address == 0x4085) { |
| 366 |
fds->snd.sweep.bias = ((SBYTE) (value << 1)) / 2; |
| 367 |
fds->snd.modulation.index = 0; |
| 368 |
return (TRUE); |
| 369 |
} |
| 370 |
if (address == 0x4086) { |
| 371 |
fds->snd.modulation.frequency = (fds->snd.modulation.frequency & 0xFF00) | value; |
| 372 |
return (TRUE); |
| 373 |
} |
| 374 |
if (address == 0x4087) { |
| 375 |
fds->snd.modulation.frequency = ((value & 0x0F) << 8) |
| 376 |
| (fds->snd.modulation.frequency & 0x00FF); |
| 377 |
fds->snd.modulation.disabled = value & 0x80; |
| 378 |
return (TRUE); |
| 379 |
} |
| 380 |
if (address == 0x4088) { |
| 381 |
BYTE i; |
| 382 |
|
| 383 |
// 0,2,4,6,-8,-6,-4,-2 |
| 384 |
for (i = 0; i < 32; i++) { |
| 385 |
BYTE a = i << 1; |
| 386 |
|
| 387 |
if (i < 31) { |
| 388 |
fds->snd.modulation.data[a] = fds->snd.modulation.data[a + 2]; |
| 389 |
} else { |
| 390 |
BYTE tmp = ((value & 0x03) | (0x3F * (value & 0x04))); |
| 391 |
fds->snd.modulation.data[a] = (SBYTE) tmp; |
| 392 |
} |
| 393 |
fds->snd.modulation.data[a + 1] = fds->snd.modulation.data[a]; |
| 394 |
} |
| 395 |
return (TRUE); |
| 396 |
} |
| 397 |
if (address == 0x4089) { |
| 398 |
fds->snd.wave.writable = value & 0x80; |
| 399 |
fds->snd.wave.volume = value & 0x03; |
| 400 |
return (TRUE); |
| 401 |
} |
| 402 |
if (address == 0x408A) { |
| 403 |
fds->snd.envelope.speed = value; |
| 404 |
return (TRUE); |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
return (FALSE); |
| 410 |
} |
| 411 |
#endif /* CPU_INLINE_H_ */ |