-
Notifications
You must be signed in to change notification settings - Fork 1
/
rewind.c
executable file
·296 lines (255 loc) · 8.18 KB
/
rewind.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
Author: davex
e-mail: [email protected]
*/
#include "rewind.h"
#include "tools.h"
#include "menu.h"
int num_rwnd_states = 0;
int rwnd_state_size = 0;
int number_of_used_states = 0;
const int TOTAL_REWIND_MEMORY = 5*1024*1024; //reserves 5 MB for rewind states
const int SAFE_MEMORY_MARGIN = 1*1024*1024;//left for system to use
unsigned max_rewind_memory = 5*1024*1024;
int g_rwnd_period = 0;
struct rewind_state{
int have_data;
unsigned char *data;
struct rewind_state *next;
struct rewind_state *prev;
};
struct rewind_state *ptr_rewind_states, *prev_state, *next_state;
int get_used_rewind_states_number(){
// int retVal = 0;
// if(setting.rewind_enabled && ptr_rewind_states != NULL){
// if(ptr_rewind_states->have_data){
// retVal++;
// }
// struct rewind_state* act = ptr_rewind_states->next;
// while(act != ptr_rewind_states){
// if(act->have_data){
// retVal++;
// }
// act = act->next;
// }
// }
// return retVal;
// printf("NUM STATES DIRECT: %d\n",retVal);
// printf("NUM STATES VARIAB: %d\n\n",number_of_used_states);
return number_of_used_states;
}
char* get_rewind_progress_bar(){
int max = num_rwnd_states;
int act = get_used_rewind_states_number();
return get_progress_bar(max,act);
}
static void set_number_of_rewind_states(int *number_of_states);
void allocate_rewind_states(void){
if(setting.rewind_enabled){
struct rewind_state *created_state, *first_state;
int i;
rwnd_state_size = gb_save_state(NULL); //declared in "gbcore/gb.h"
set_number_of_rewind_states(&num_rwnd_states);
//reserves first state
created_state = (struct rewind_state *)malloc( sizeof(struct rewind_state) );
created_state->have_data = 0;
created_state->data = (byte *) malloc( rwnd_state_size);
first_state = created_state;
prev_state = first_state;
//reserves remaining states
for( i = 1; i< num_rwnd_states; i++){
created_state = (struct rewind_state *)malloc( sizeof(struct rewind_state) );
created_state->have_data = 0;
created_state->data = (byte *) malloc( rwnd_state_size);
created_state ->prev = prev_state;
prev_state->next = created_state;
prev_state = created_state;
}
//make list be circular
created_state->next = first_state;
first_state->prev = created_state;
ptr_rewind_states = first_state;
number_of_used_states = 0;
}
print_rewind_debug_info(setting,"allocate_rewind_states");
}
static void set_number_of_rewind_states(int *number_of_states) {
switch (setting.rewind_limit_mode){
case REWIND_MODE_LIMIT_MEMORY_AMOUNT:
(*number_of_states) = (int) ( (float)get_current_rewind_memory(setting) / (float) rwnd_state_size );
break;
case REWIND_MODE_LIMIT_STATES_AMOUNT:{
int max_states = (int) ( (float)max_rewind_memory / (float) rwnd_state_size );
if(setting.rewind_always_use_max_states){
(*number_of_states) = max_states;
}else{
(*number_of_states) = MIN(max_states,setting.rewind_user_max_states_ammount);
}
}
break;
default:
break;
}
}
void free_rewind_states(void){
struct rewind_state *now_state;
now_state = ptr_rewind_states;
if(ptr_rewind_states){
prev_state = now_state->prev;
prev_state->next = NULL;
while(1){
if ( now_state == NULL)
break;
next_state = now_state->next;
free(now_state->data );
free(now_state);
now_state = next_state;
}
ptr_rewind_states = NULL;
number_of_used_states = 0;
}
}
void save_rewind_state(void){
gb_save_state(ptr_rewind_states->data); //declared in "gbcore/gb.h"
if(!ptr_rewind_states->have_data){
number_of_used_states++;
}
ptr_rewind_states->have_data = 1;
ptr_rewind_states = ptr_rewind_states->next;
}
int read_rewind_state(void){
int ret_val = -999;
prev_state = ptr_rewind_states->prev;
if (prev_state->have_data > 0 ){
load_state_tmp( prev_state->data); //declarado en "saveload.h"
prev_state->have_data = 0;
number_of_used_states--;
ptr_rewind_states = ptr_rewind_states->prev;
ret_val = 1;
}
return ret_val;
}
//Papiex : Rewind extension
static unsigned checkAvailableMemoryBiggestSingleChunk(){
void* mem = NULL;
unsigned size = 1024;
unsigned incr = 1024;
while((mem = malloc(size))){
free(mem);
size += incr;
}
return size-incr;
}
typedef struct head{
void * mem;
struct head* next;
} list;
list* list_alloc(){
return calloc(1,sizeof(list));
}
void list_destroy(list** head){
if(*head){
list* iter = *head;
while(iter){
list* next = iter->next;
free(iter->mem);
free(iter);
iter = next;
}
(*head) = NULL;
}
}
list* list_add(list** head, void* mem){
if(head && (*head)){
list* elem = list_alloc();
if(elem){
elem->mem = mem;
elem->next = (*head);
(*head) = elem;
}
return (*head);
}
return NULL;
}
static unsigned checkAvailableMemoryMixedChunks(){
unsigned retval = 0;
void* mem = NULL;
list* head = list_alloc();
size_t size = 1024*512;
int len = 0;
while((mem = malloc(size))){
if(!list_add(&head,mem)){
free(mem);
break;
}
else {
len++;
}
}
retval = len * size;
list_destroy(&head);
return retval;
}
#ifdef MAX_MEMORY_ESTIMATION
static int checkIfButtonRepressed(){
int retVal = 0;
ctrl_data_t paddata;
sceCtrlPeekBufferPositive(&paddata, 1);
if((paddata.buttons & PSP_CTRL_LEFT)
&& (paddata.buttons & PSP_CTRL_START))
{
retVal = 1;
}
return retVal;
}
void test_available_memory(void){
static int testNumber=0;
#define MSG_LEN 255
static char tmp[MSG_LEN]={0};
ctrl_data_t paddata;
sceCtrlPeekBufferPositive(&paddata, 1);
if(checkIfButtonRepressed()){
unsigned availBiggest = checkAvailableMemoryBiggestSingleChunk();
unsigned availMixed = checkAvailableMemoryMixedChunks();
snprintf(tmp,MSG_LEN,"Mem:%d,big chunk: %d mb (%d kb);small:%d mb (%d kb)\n",
testNumber,byte2mb(availBiggest),byte2kb(availBiggest),byte2mb(availMixed),byte2kb(availMixed));
printf(tmp);
testNumber++;
pgWaitVn(20);
}
pgPrintf(0,1,RGB(255,0,0),tmp);
}
#endif //MAX_MEMORY_ESTIMATION
unsigned establish_max_rewind_memory(void) {
unsigned retval = checkAvailableMemoryBiggestSingleChunk() - SAFE_MEMORY_MARGIN;
return retval;
}
unsigned get_current_rewind_memory(SETTING local) {
unsigned retval;
if(local.rewind_limit_mode == REWIND_MODE_LIMIT_MEMORY_AMOUNT){
if(local.rewind_always_use_max_memory){
retval = max_rewind_memory;
}else{
retval = MIN(local.rewind_user_max_memory_ammount,max_rewind_memory);
}
}else{//just informative estimation for displaying in menu
retval = num_rwnd_states * (sizeof(struct rewind_state)+rwnd_state_size);
}
return retval;
}
void print_rewind_debug_info(SETTING local, char *info) {
printf("\nINFO TYPE : %s\n",info);
printf("\tGlobal current rewind memory - get_current_rewind_memory() : %d bytes / %d kb / %.1f mb\n",
get_current_rewind_memory(local),byte2kb(get_current_rewind_memory(local)),byte2mb_asFloat(get_current_rewind_memory(local)));
printf("\tGlobal number of states - num_rwnd_states : %d\n",num_rwnd_states);
printf("\tGlobal state size - rwnd_state_size : %d bytes / %d kb\n",rwnd_state_size,byte2kb(rwnd_state_size));
printf("\tGlobal max avail. rewind memory - max_rewind_memory : %d bytes / %d kb / %.1f mb\n",
max_rewind_memory,byte2kb(max_rewind_memory),byte2mb_asFloat(max_rewind_memory));
printf("\tRewind : enabled - rewind_enabled : %d\n",local.rewind_enabled);
printf("\tRewind : limit mode - rewind_limit_mode : %s\n",local.rewind_limit_mode?"REWIND_MODE_LIMIT_STATES_AMOUNT":"REWIND_MODE_LIMIT_MEMORY_AMOUNT");
printf("\tRewind : always use max states - rewind_always_use_max_states : %d\n",local.rewind_always_use_max_states);
printf("\tRewind : user max states amount - rewind_user_max_states_ammount : %d\n",local.rewind_user_max_states_ammount);
printf("\tRewind : always use max memory - rewind_always_use_max_memory : %d\n",local.rewind_always_use_max_memory);
printf("\tRewind : user max memory amount - rewind_user_max_memory_ammount : %d bytes / %d kb / %.1f mb\n\n",
local.rewind_user_max_memory_ammount,byte2kb(local.rewind_user_max_memory_ammount),byte2mb_asFloat(local.rewind_user_max_memory_ammount));
}