-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathflash.cpp
More file actions
208 lines (159 loc) · 6.65 KB
/
flash.cpp
File metadata and controls
208 lines (159 loc) · 6.65 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <array>
#include <cassert>
#include <ultramodern/ultra64.h>
#include <ultramodern/ultramodern.hpp>
#include "recomp.h"
#include "librecomp/addresses.hpp"
#include "librecomp/game.hpp"
// TODO move this out into ultramodern code
constexpr uint32_t flash_size = 1024 * 1024 / 8; // 1Mbit
constexpr uint32_t page_size = 128;
constexpr uint32_t pages_per_sector = 128;
constexpr uint32_t page_count = flash_size / page_size;
constexpr uint32_t sector_size = page_size * pages_per_sector;
constexpr uint32_t sector_count = flash_size / sector_size;
void save_write_ptr(const void* in, uint32_t offset, uint32_t count);
void save_write(RDRAM_ARG PTR(void) rdram_address, uint32_t offset, uint32_t count);
void save_read(RDRAM_ARG PTR(void) rdram_address, uint32_t offset, uint32_t count);
void save_clear(uint32_t start, uint32_t size, char value);
std::array<char, page_size> write_buffer;
extern "C" void osFlashInit_recomp(uint8_t * rdram, recomp_context * ctx) {
if (!recomp::flashram_allowed()) {
ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type");
ULTRAMODERN_QUICK_EXIT();
}
ctx->r2 = recomp::flash_handle;
}
extern "C" void osFlashReadStatus_recomp(uint8_t * rdram, recomp_context * ctx) {
if (!recomp::flashram_allowed()) {
ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type");
ULTRAMODERN_QUICK_EXIT();
}
PTR(u8) flash_status = ctx->r4;
MEM_B(0, flash_status) = 0;
}
extern "C" void osFlashReadId_recomp(uint8_t * rdram, recomp_context * ctx) {
if (!recomp::flashram_allowed()) {
ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type");
ULTRAMODERN_QUICK_EXIT();
}
PTR(u32) flash_type = ctx->r4;
PTR(u32) flash_maker = ctx->r5;
// Mimic a real flash chip's type and maker, as some games actually check if one is present.
MEM_W(0, flash_type) = 0x11118001;
MEM_W(0, flash_maker) = 0x00C2001E;
}
extern "C" void osFlashClearStatus_recomp(uint8_t * rdram, recomp_context * ctx) {
if (!recomp::flashram_allowed()) {
ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type");
ULTRAMODERN_QUICK_EXIT();
}
// Nothing to do here.
}
extern "C" void osFlashAllErase_recomp(uint8_t * rdram, recomp_context * ctx) {
if (!recomp::flashram_allowed()) {
ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type");
ULTRAMODERN_QUICK_EXIT();
}
save_clear(0, ultramodern::save_size, 0xFF);
ctx->r2 = 0;
}
extern "C" void osFlashAllEraseThrough_recomp(uint8_t * rdram, recomp_context * ctx) {
if (!recomp::flashram_allowed()) {
ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type");
ULTRAMODERN_QUICK_EXIT();
}
save_clear(0, ultramodern::save_size, 0xFF);
ctx->r2 = 0;
}
// This function is named sector but really means page.
extern "C" void osFlashSectorErase_recomp(uint8_t * rdram, recomp_context * ctx) {
if (!recomp::flashram_allowed()) {
ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type");
ULTRAMODERN_QUICK_EXIT();
}
uint32_t page_num = (uint32_t)ctx->r4;
// Prevent out of bounds erase
if (page_num >= page_count) {
ctx->r2 = -1;
return;
}
save_clear(page_num * page_size, page_size, 0xFF);
ctx->r2 = 0;
}
// Same naming issue as above.
extern "C" void osFlashSectorEraseThrough_recomp(uint8_t * rdram, recomp_context * ctx) {
if (!recomp::flashram_allowed()) {
ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type");
ULTRAMODERN_QUICK_EXIT();
}
uint32_t page_num = (uint32_t)ctx->r4;
// Prevent out of bounds erase
if (page_num >= page_count) {
ctx->r2 = -1;
return;
}
save_clear(page_num * page_size, page_size, 0xFF);
ctx->r2 = 0;
}
extern "C" void osFlashCheckEraseEnd_recomp(uint8_t * rdram, recomp_context * ctx) {
if (!recomp::flashram_allowed()) {
ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type");
ULTRAMODERN_QUICK_EXIT();
}
// All erases are blocking in this implementation, so this should always return OK.
ctx->r2 = 0; // FLASH_STATUS_ERASE_OK
}
extern "C" void osFlashWriteBuffer_recomp(uint8_t * rdram, recomp_context * ctx) {
if (!recomp::flashram_allowed()) {
ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type");
ULTRAMODERN_QUICK_EXIT();
}
OSIoMesg* mb = TO_PTR(OSIoMesg, ctx->r4);
int32_t pri = ctx->r5;
PTR(void) dramAddr = ctx->r6;
PTR(OSMesgQueue) mq = ctx->r7;
// Copy the input data into the write buffer
for (size_t i = 0; i < page_size; i++) {
write_buffer[i] = MEM_B(i, dramAddr);
}
// Send the message indicating write completion
ultramodern::enqueue_external_message(mq, 0, false, true);
ctx->r2 = 0;
}
extern "C" void osFlashWriteArray_recomp(uint8_t * rdram, recomp_context * ctx) {
if (!recomp::flashram_allowed()) {
ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type");
ULTRAMODERN_QUICK_EXIT();
}
uint32_t page_num = ctx->r4;
// Copy the write buffer into the save file
save_write_ptr(write_buffer.data(), page_num * page_size, page_size);
ctx->r2 = 0;
}
extern "C" void osFlashReadArray_recomp(uint8_t * rdram, recomp_context * ctx) {
if (!recomp::flashram_allowed()) {
ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type");
ULTRAMODERN_QUICK_EXIT();
}
OSIoMesg* mb = TO_PTR(OSIoMesg, ctx->r4);
int32_t pri = ctx->r5;
uint32_t page_num = ctx->r6;
PTR(void) dramAddr = ctx->r7;
uint32_t n_pages = MEM_W(0x10, ctx->r29);
PTR(OSMesgQueue) mq = MEM_W(0x14, ctx->r29);
uint32_t offset = page_num * page_size;
uint32_t count = n_pages * page_size;
// Read from the save file into the provided buffer
save_read(PASS_RDRAM dramAddr, offset, count);
// Send the message indicating read completion
ultramodern::enqueue_external_message(mq, 0, false, true);
ctx->r2 = 0;
}
extern "C" void osFlashChange_recomp(uint8_t * rdram, recomp_context * ctx) {
if (!recomp::flashram_allowed()) {
ultramodern::error_handling::message_box("Attempted to use FlashRAM saving with other save type");
ULTRAMODERN_QUICK_EXIT();
}
assert(false);
}