This repository has been archived by the owner on Jun 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
mem.c
160 lines (108 loc) · 2.73 KB
/
mem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "types.h"
#include "everdrive.h"
#include "sys.h"
u8 spi_dma;
#include "errors.h"
#include "mem.h"
u8 memSpiReadDma(void *dst, u16 slen);
u8 memSpiReadPio(void *dst, u16 slen);
u8 mem_buff[512];
void memSpiSetDma(u8 mode) {
spi_dma = mode;
}
void memcopy(void *src, void *dst, u16 len) {
u8 *s = (u8 *) src;
u8 *d = (u8 *) dst;
while (len--)*d++ = *s++;
}
void memfill(void *dst, u8 val, u16 len) {
u8 *d = (u8 *) dst;
while (len--)*d++ = val;
}
// buff len
u8 memSpiRead(void *dst, u16 slen) {
u8 copy_to_rom = 0;
u32 addr = (u32) dst; //if ROM_ADDR 0xb0000000
if (addr >= ROM_ADDR && addr < ROM_END_ADDR)copy_to_rom = 1;
//if (copy_to_rom || spi_dma) {
if ((copy_to_rom || spi_dma) && addr % 4 == 0) {
return memSpiReadDma(dst, slen);
} else {
return memSpiReadPio(dst, slen);
}
}
u8 memSpiReadPio(void *dst, u16 slen) {
u16 i;
u16 u;
u8 *ptr8 = (u8 *) dst;
for (i = 0; i < slen; i++) {
evd_SDdatReadMode(1);
for (u = 0; u < 65535; u++)if ((evd_SPI(0xff) & 0xf1) == 0xf0)break;
evd_SDdatReadMode(0);
if (u == 65535) {
evd_spiSSOff();
return DISK_RD_FE_TOUT;
}
for (u = 0; u < 512; u++)*ptr8++ = evd_SPI(0xff);
u = evd_isSDMode() ? 8 : 2;
while (u--) {
// console_printf("XRC: %02X", evd_SPI(0xff));
//console_printf("%02X\n", evd_SPI(0xff));
u--;
evd_SPI(0xff);
evd_SPI(0xff);
}
//evd_SPI(0xff);
//evd_SPI(0xff);
}
return 0;
}
u8 memSpiReadDma(void *dst, u16 slen) {
u8 resp = 0;
u8 copy_to_rom = 0;
u32 addr = (u32) dst;
evd_SDdatReadMode(0);
// console_printf("dma\n");
if (addr >= ROM_ADDR && addr < ROM_END_ADDR)copy_to_rom = 1;
if (copy_to_rom) {
return evd_mmcReadToCart(addr, slen);
} else {
resp = evd_mmcReadToCart(0, slen);
dma_read_s(dst, ROM_ADDR, slen * 512);
}
return resp;
}
u8 memSpiWrite(void *src) {
u16 i;
u8 *ptr8 = (u8 *) src;
if ((u32) src >= ROM_ADDR && (u32) src < ROM_END_ADDR) {
dma_read_s(mem_buff, (u32) src, 512);
for (i = 0; i < 512; i++)mem_spi(mem_buff[i]);
} else {
for (i = 0; i < 512; i++)mem_spi(*ptr8++);
}
return 0;
}
void memSpiBusy() {
while (evd_isSpiBusy());
}
void memRomWrite32(u32 addr, u32 val) {
vu32 *ptr = (u32 *) (addr + ROM_ADDR);
vu8 tmp;
tmp = *ptr;
*ptr = val;
tmp = *ptr;
}
u32 memRomRead32(u32 addr) {
vu32 *ptr = (u32 *) (addr + ROM_ADDR);
vu8 tmp;
vu32 val;
tmp = *ptr;
val = *ptr;
return val;
}
/*
u8 mem_spi(u8 dat) {
return evd_SPI(dat);
}
*/