1
/* FCE Ultra - NES/Famicom Emulator
3
* Copyright notice for this file:
4
* Copyright (C) 2013 CaH4e3
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25
static uint8 *WRAM = NULL;
26
static uint32 WRAMSIZE;
29
static uint8 pcm_enable = 0;
30
static int16 pcm_latch = 0x3F6, pcm_clock = 0x3F6;
31
static writefunc pcmwrite;
33
static SFORMAT StateRegs[] =
39
static int16 step_size[49] = {
40
16, 17, 19, 21, 23, 25, 28, 31, 34, 37,
41
41, 45, 50, 55, 60, 66, 73, 80, 88, 97,
42
107, 118, 130, 143, 157, 173, 190, 209, 230, 253,
43
279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
44
724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552
46
static int32 step_adj[16] = { -1, -1, -1, -1, 2, 5, 7, 9, -1, -1, -1, -1, 2, 5, 7, 9 };
49
static int32 jedi_table[16 * 49];
50
static int32 acc = 0; //ADPCM accumulator, initial condition must be 0
51
static int32 decstep = 0; //ADPCM decoding step, initial condition must be 0
53
static void jedi_table_init()
57
for (step = 0; step < 49; step++)
59
for (nib = 0; nib < 16; nib++)
61
int value = (2 * (nib & 0x07) + 1) * step_size[step] / 8;
62
jedi_table[step * 16 + nib] = ((nib & 0x08) != 0) ? -value : value;
67
static uint8 decode(uint8 code)
69
acc += jedi_table[decstep + code];
70
if ((acc & ~0x7ff) != 0) // acc is > 2047
73
decstep += step_adj[code & 7] * 16;
74
if (decstep < 0) decstep = 0;
75
if (decstep > 48 * 16) decstep = 48 * 16;
76
return (acc >> 8) & 0xff;
79
static void Sync(void) {
80
uint32 sbank = reg[1] & 0x7;
81
uint32 bbank = reg[2];
83
setprg8r(0x10, 0x6000, reg[3] & 3);
84
if(reg[0] & 2) { // UNROM mode
85
setprg16(0x8000, (bbank << 3) | sbank);
87
setprg16(0xC000, (bbank << 3) | 6 | (reg[1] & 1));
89
setprg16(0xC000, (bbank << 3) | 7);
91
uint32 bank = (bbank << 3) | sbank;
93
setprg16(0x8000, bank);
94
setprg16(0xC000, bank);
96
setprg32(0x8000, bank >> 1);
98
setmirror((reg[0] & 1) ^ 1);
101
static DECLFW(UNLDSOUNDV1Write) {
103
// FCEU_printf("cmd %04x:%02x\n", A, V);
107
static DECLFW(UNLDSOUNDV1WriteSnd) {
111
// pcmwrite(0x4011, (V & 0xF) << 3);
112
pcmwrite(0x4011, decode(V & 0xf));
116
FCEU_printf("misc %04x:%02x\n", A, V);
119
static DECLFR(UNLDSOUNDV1ReadSnd) {
121
return (X.DB & 0xBF) | ((pcm_enable ^ 1) <<6);
126
static void UNLDSOUNDV1Power(void) {
127
reg[0] = reg[1] = reg[2] = reg[3] = 0;
129
pcmwrite = GetWriteHandler(0x4011);
130
SetWriteHandler(0x4800, 0x4fff, UNLDSOUNDV1Write);
131
SetWriteHandler(0x5800, 0x5fff, UNLDSOUNDV1WriteSnd);
132
SetReadHandler(0x5800, 0x5fff, UNLDSOUNDV1ReadSnd);
133
SetReadHandler(0x6000, 0x7fff, CartBR);
134
SetWriteHandler(0x6000, 0x7fff, CartBW);
135
SetReadHandler(0x8000, 0xffff, CartBR);
138
static void UNLDSOUNDV1SndClk(int a) {
141
if (pcm_latch <= 0) {
142
pcm_latch += pcm_clock;
148
static void UNLDSOUNDV1Close(void) {
154
static void StateRestore(int version) {
158
void UNLDSOUNDV1_Init(CartInfo *info) {
159
info->Power = UNLDSOUNDV1Power;
160
info->Close = UNLDSOUNDV1Close;
161
GameStateRestore = StateRestore;
162
MapIRQHook = UNLDSOUNDV1SndClk;
167
WRAM = (uint8*)FCEU_gmalloc(WRAMSIZE);
168
SetupCartPRGMapping(0x10, WRAM, WRAMSIZE, 1);
169
AddExState(WRAM, WRAMSIZE, 0, "WRAM");
171
info->SaveGame[0] = WRAM;
172
info->SaveGameLen[0] = WRAMSIZE;
174
AddExState(&StateRegs, ~0, 0, 0);