diff options
Diffstat (limited to 'gba/source/main.c')
-rw-r--r-- | gba/source/main.c | 134 |
1 files changed, 114 insertions, 20 deletions
diff --git a/gba/source/main.c b/gba/source/main.c index 9d05189..567165a 100644 --- a/gba/source/main.c +++ b/gba/source/main.c | |||
@@ -7,12 +7,15 @@ | |||
7 | #include <gba_sio.h> | 7 | #include <gba_sio.h> |
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | #include <stdlib.h> | 9 | #include <stdlib.h> |
10 | #include "libSave.h" | ||
10 | 11 | ||
11 | u32 getGameSize(void) | 12 | u8 save_data[0x20000] __attribute__ ((section (".sbss"))); |
13 | |||
14 | s32 getGameSize(void) | ||
12 | { | 15 | { |
13 | if(*(vu32*)(0x08000004) != 0x51AEFF24) | 16 | if(*(vu32*)(0x08000004) != 0x51AEFF24) |
14 | return 0; | 17 | return -1; |
15 | u32 i; | 18 | s32 i; |
16 | for(i = (1<<20); i < (1<<25); i<<=1) | 19 | for(i = (1<<20); i < (1<<25); i<<=1) |
17 | { | 20 | { |
18 | vu16 *rompos = (vu16*)(0x08000000+i); | 21 | vu16 *rompos = (vu16*)(0x08000000+i); |
@@ -30,6 +33,8 @@ u32 getGameSize(void) | |||
30 | } | 33 | } |
31 | return i; | 34 | return i; |
32 | } | 35 | } |
36 | #define JOY_WRITE 2 | ||
37 | #define JOY_READ 4 | ||
33 | //--------------------------------------------------------------------------------- | 38 | //--------------------------------------------------------------------------------- |
34 | // Program entry point | 39 | // Program entry point |
35 | //--------------------------------------------------------------------------------- | 40 | //--------------------------------------------------------------------------------- |
@@ -49,43 +54,132 @@ int main(void) { | |||
49 | u32 i; | 54 | u32 i; |
50 | iprintf("\x1b[9;10HROM Dumper\n"); | 55 | iprintf("\x1b[9;10HROM Dumper\n"); |
51 | iprintf("\x1b[10;5HPlease look at the TV\n"); | 56 | iprintf("\x1b[10;5HPlease look at the TV\n"); |
52 | REG_HS_CTRL |= 6; | 57 | REG_HS_CTRL |= (JOY_WRITE|JOY_READ); |
58 | u32 prevIrqMask = REG_IME; | ||
53 | while (1) { | 59 | while (1) { |
54 | if((REG_HS_CTRL&4)) | 60 | if((REG_HS_CTRL&JOY_READ)) |
55 | { | 61 | { |
56 | REG_HS_CTRL |= 4; | 62 | irqDisable(IRQ_VBLANK); |
57 | u32 gamesize = getGameSize(); | 63 | REG_IME = 0; |
64 | REG_HS_CTRL |= (JOY_WRITE|JOY_READ); | ||
65 | s32 gamesize = getGameSize(); | ||
66 | u32 savesize = SaveSize(save_data,gamesize); | ||
58 | REG_JOYTR = gamesize; | 67 | REG_JOYTR = gamesize; |
59 | while((REG_HS_CTRL&4) == 0) ; | 68 | //wait for a cmd receive for safety |
60 | REG_HS_CTRL |= 4; | 69 | while((REG_HS_CTRL&JOY_WRITE) == 0) ; |
61 | if(gamesize == 0) | 70 | REG_HS_CTRL |= (JOY_WRITE|JOY_READ); |
71 | REG_JOYTR = savesize; | ||
72 | //wait for a cmd receive for safety | ||
73 | while((REG_HS_CTRL&JOY_WRITE) == 0) ; | ||
74 | REG_HS_CTRL |= (JOY_WRITE|JOY_READ); | ||
75 | if(gamesize == -1) | ||
62 | { | 76 | { |
63 | REG_JOYTR = 0; | 77 | REG_JOYTR = 0; |
78 | REG_IME = prevIrqMask; | ||
79 | irqEnable(IRQ_VBLANK); | ||
64 | continue; //nothing to read | 80 | continue; //nothing to read |
65 | } | 81 | } |
66 | //game in, send header | 82 | //game in, send header |
67 | for(i = 0; i < 0xC0; i+=4) | 83 | for(i = 0; i < 0xC0; i+=4) |
68 | { | 84 | { |
69 | REG_JOYTR = *(vu32*)(0x08000000+i); | 85 | REG_JOYTR = *(vu32*)(0x08000000+i); |
70 | while((REG_HS_CTRL&4) == 0) ; | 86 | while((REG_HS_CTRL&JOY_READ) == 0) ; |
71 | REG_HS_CTRL |= 4; | 87 | REG_HS_CTRL |= (JOY_WRITE|JOY_READ); |
72 | } | 88 | } |
89 | REG_JOYTR = 0; | ||
73 | //wait for other side to choose | 90 | //wait for other side to choose |
74 | while((REG_HS_CTRL&2) == 0) ; | 91 | while((REG_HS_CTRL&JOY_WRITE) == 0) ; |
75 | REG_HS_CTRL |= 2; | 92 | REG_HS_CTRL |= (JOY_WRITE|JOY_READ); |
76 | if(REG_JOYRE == 0) | 93 | u32 choseval = REG_JOYRE; |
94 | if(choseval == 0) | ||
77 | { | 95 | { |
78 | REG_JOYTR = 0; | 96 | REG_JOYTR = 0; |
97 | REG_IME = prevIrqMask; | ||
98 | irqEnable(IRQ_VBLANK); | ||
79 | continue; //nothing to read | 99 | continue; //nothing to read |
80 | } | 100 | } |
81 | //dump the game | 101 | else if(choseval == 1) |
82 | for(i = 0; i < gamesize; i+=4) | ||
83 | { | 102 | { |
84 | REG_JOYTR = *(vu32*)(0x08000000+i); | 103 | //dump the game |
85 | while((REG_HS_CTRL&4) == 0) ; | 104 | for(i = 0; i < gamesize; i+=4) |
86 | REG_HS_CTRL |= 4; | 105 | { |
106 | REG_JOYTR = *(vu32*)(0x08000000+i); | ||
107 | while((REG_HS_CTRL&JOY_READ) == 0) ; | ||
108 | REG_HS_CTRL |= (JOY_WRITE|JOY_READ); | ||
109 | } | ||
110 | } | ||
111 | else if(choseval == 2) | ||
112 | { | ||
113 | //backup save | ||
114 | switch (savesize){ | ||
115 | case 0x200: | ||
116 | GetSave_EEPROM_512B(save_data); | ||
117 | break; | ||
118 | case 0x2000: | ||
119 | GetSave_EEPROM_8KB(save_data); | ||
120 | break; | ||
121 | case 0x8000: | ||
122 | GetSave_SRAM_32KB(save_data); | ||
123 | break; | ||
124 | case 0x10000: | ||
125 | GetSave_FLASH_64KB(save_data); | ||
126 | break; | ||
127 | case 0x20000: | ||
128 | GetSave_FLASH_128KB(save_data); | ||
129 | break; | ||
130 | default: | ||
131 | break; | ||
132 | } | ||
133 | REG_JOYTR = savesize; | ||
134 | //wait for a cmd receive for safety | ||
135 | while((REG_HS_CTRL&JOY_WRITE) == 0) ; | ||
136 | REG_HS_CTRL |= (JOY_WRITE|JOY_READ); | ||
137 | //send the save | ||
138 | for(i = 0; i < savesize; i+=4) | ||
139 | { | ||
140 | REG_JOYTR = *(vu32*)(save_data+i); | ||
141 | while((REG_HS_CTRL&JOY_READ) == 0) ; | ||
142 | REG_HS_CTRL |= (JOY_WRITE|JOY_READ); | ||
143 | } | ||
144 | } | ||
145 | else if(choseval == 3) | ||
146 | { | ||
147 | REG_JOYTR = savesize; | ||
148 | //receive the save | ||
149 | for(i = 0; i < savesize; i+=4) | ||
150 | { | ||
151 | while((REG_HS_CTRL&JOY_WRITE) == 0) ; | ||
152 | REG_HS_CTRL |= (JOY_WRITE|JOY_READ); | ||
153 | *(vu32*)(save_data+i) = REG_JOYRE; | ||
154 | } | ||
155 | //write it | ||
156 | switch (savesize){ | ||
157 | case 0x200: | ||
158 | PutSave_EEPROM_512B(save_data); | ||
159 | break; | ||
160 | case 0x2000: | ||
161 | PutSave_EEPROM_8KB(save_data); | ||
162 | break; | ||
163 | case 0x8000: | ||
164 | PutSave_SRAM_32KB(save_data); | ||
165 | break; | ||
166 | case 0x10000: | ||
167 | PutSave_FLASH_64KB(save_data); | ||
168 | break; | ||
169 | case 0x20000: | ||
170 | PutSave_FLASH_128KB(save_data); | ||
171 | break; | ||
172 | default: | ||
173 | break; | ||
174 | } | ||
175 | REG_JOYTR = 0; | ||
176 | //wait for a cmd receive for safety | ||
177 | while((REG_HS_CTRL&JOY_WRITE) == 0) ; | ||
178 | REG_HS_CTRL |= (JOY_WRITE|JOY_READ); | ||
87 | } | 179 | } |
88 | REG_JOYTR = 0; | 180 | REG_JOYTR = 0; |
181 | REG_IME = prevIrqMask; | ||
182 | irqEnable(IRQ_VBLANK); | ||
89 | } | 183 | } |
90 | Halt(); | 184 | Halt(); |
91 | } | 185 | } |