2 Score-keeping for GBA with SRAM cartridge (originally part of gbatris)
3 Copyright (C) 2019-2022 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
25 #define MAGIC "VOXBURG1"
29 #define read_sram(offs, buf, sz) \
30 sramcpy(buf, (unsigned char*)SRAM_ADDR + (offs), sz)
31 #define write_sram(offs, buf, sz) \
32 sramcpy((unsigned char*)SRAM_ADDR + (offs), buf, sz)
34 static void sramcpy(void *dst, void *src, int size);
36 int last_score_rank = -1;
43 read_sram(0, magic, 8);
44 if(memcmp(magic, MAGIC, sizeof magic) != 0) {
46 memcpy(scores[i].name, "----", NAME_SIZE + 1);
48 /* scores[10] keeps the last entered name. initialize it empty */
49 scores[10].name[0] = 0;
53 read_sram(SCORE_OFFS, scores, sizeof scores);
57 void save_scores(void)
59 write_sram(0, MAGIC, 8);
60 write_sram(SCORE_OFFS, scores, sizeof scores);
63 void save_score(char *name, int score, int time, int level)
73 if(scores[i].score <= score) {
78 last_score_rank = rank;
80 if(rank == -1) return;
82 memmove(scores + rank + 1, scores + rank, (9 - rank) * sizeof(struct score_entry));
84 if(strlen(name) > NAME_SIZE) {
87 sprintf(scores[rank].name, "%s", name);
88 scores[rank].score = score;
89 scores[rank].time = time;
90 scores[rank].level = level;
92 /* also copy to the last entered name slot */
93 scores[10] = scores[rank];
98 int is_highscore(int score)
100 return scores[9].score <= score;
103 static void sramcpy(void *dst, void *src, int size)
105 unsigned char *sptr = src;
106 unsigned char *dptr = dst;