1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  *   Mupen64plus - sram.c                                                  *
3  *   Mupen64Plus homepage: http://code.google.com/p/mupen64plus/           *
4  *   Copyright (C) 2014 Bobby Smiles                                       *
5  *                                                                         *
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.                                   *
10  *                                                                         *
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.                          *
15  *                                                                         *
16  *   You should have received a copy of the GNU General Public License     *
17  *   along with this program; if not, write to the                         *
18  *   Free Software Foundation, Inc.,                                       *
19  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
20  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
21 
22 #include "sram.h"
23 #include "pi_controller.h"
24 
25 #include "memory/memory.h"
26 
27 #include "ri/ri_controller.h"
28 
29 #include <stddef.h>
30 #include <stdint.h>
31 #include <string.h>
32 
init_sram(struct sram * sram,void * user_data,void (* save)(void *),uint8_t * data)33 void init_sram(struct sram* sram, void* user_data, void (*save)(void*), uint8_t* data)
34 {
35    sram->user_data = user_data;
36    sram->save      = save;
37    sram->data      = data;
38 }
39 
sram_save(struct sram * sram)40 void sram_save(struct sram* sram)
41 {
42    sram->save(sram->user_data);
43 }
44 
format_sram(uint8_t * sram)45 void format_sram(uint8_t* sram)
46 {
47    memset(sram, 0, SRAM_SIZE);
48 }
49 
dma_write_sram(struct pi_controller * pi)50 void dma_write_sram(struct pi_controller* pi)
51 {
52    size_t i;
53    size_t length = (pi->regs[PI_RD_LEN_REG] & 0xffffff) + 1;
54 
55    uint8_t* sram = pi->sram.data;
56    uint8_t* dram = (uint8_t*)pi->ri->rdram.dram;
57    uint32_t cart_addr = pi->regs[PI_CART_ADDR_REG] - 0x08000000;
58    uint32_t dram_addr = pi->regs[PI_DRAM_ADDR_REG];
59 
60    for(i = 0; i < length; ++i)
61       sram[(cart_addr+i)^S8] = dram[(dram_addr+i)^S8];
62 
63    sram_save(&pi->sram);
64 }
65 
dma_read_sram(struct pi_controller * pi)66 void dma_read_sram(struct pi_controller* pi)
67 {
68    size_t i;
69    size_t length = (pi->regs[PI_WR_LEN_REG] & 0xffffff) + 1;
70 
71    uint8_t* sram = pi->sram.data;
72    uint8_t* dram = (uint8_t*)pi->ri->rdram.dram;
73    uint32_t cart_addr = (pi->regs[PI_CART_ADDR_REG] - 0x08000000) & 0xffff;
74    uint32_t dram_addr = pi->regs[PI_DRAM_ADDR_REG];
75 
76    for(i = 0; i < length; ++i)
77       dram[(dram_addr+i)^S8] = sram[(cart_addr+i)^S8];
78 }
79