src/gba/gba-bios.c (view raw)
1#include "gba-bios.h"
2
3#include "gba.h"
4#include "gba-io.h"
5#include "gba-memory.h"
6
7#include <math.h>
8#include <stdlib.h>
9
10static void _unLz77(struct GBAMemory* memory, uint32_t source, uint8_t* dest);
11
12static void _RegisterRamReset(struct GBA* gba) {
13 uint32_t registers = gba->cpu.gprs[0];
14 (void)(registers);
15 GBALog(GBA_LOG_STUB, "RegisterRamReset unimplemented");
16}
17
18static void _CpuSet(struct GBA* gba) {
19 uint32_t source = gba->cpu.gprs[0];
20 uint32_t dest = gba->cpu.gprs[1];
21 uint32_t mode = gba->cpu.gprs[2];
22 int count = mode & 0x000FFFFF;
23 int fill = mode & 0x01000000;
24 int wordsize = (mode & 0x04000000) ? 4 : 2;
25 int i;
26 if (fill) {
27 if (wordsize == 4) {
28 source &= 0xFFFFFFFC;
29 dest &= 0xFFFFFFFC;
30 int32_t word = GBALoad32(&gba->memory.d, source);
31 for (i = 0; i < count; ++i) {
32 GBAStore32(&gba->memory.d, dest + (i << 2), word);
33 }
34 } else {
35 source &= 0xFFFFFFFE;
36 dest &= 0xFFFFFFFE;
37 uint16_t word = GBALoad16(&gba->memory.d, source);
38 for (i = 0; i < count; ++i) {
39 GBAStore16(&gba->memory.d, dest + (i << 1), word);
40 }
41 }
42 } else {
43 if (wordsize == 4) {
44 source &= 0xFFFFFFFC;
45 dest &= 0xFFFFFFFC;
46 for (i = 0; i < count; ++i) {
47 int32_t word = GBALoad32(&gba->memory.d, source + (i << 2));
48 GBAStore32(&gba->memory.d, dest + (i << 2), word);
49 }
50 } else {
51 source &= 0xFFFFFFFE;
52 dest &= 0xFFFFFFFE;
53 for (i = 0; i < count; ++i) {
54 uint16_t word = GBALoad16(&gba->memory.d, source + (i << 1));
55 GBAStore16(&gba->memory.d, dest + (i << 1), word);
56 }
57 }
58 }
59}
60
61static void _FastCpuSet(struct GBA* gba) {
62 uint32_t source = gba->cpu.gprs[0] & 0xFFFFFFFC;
63 uint32_t dest = gba->cpu.gprs[1] & 0xFFFFFFFC;
64 uint32_t mode = gba->cpu.gprs[2];
65 int count = mode & 0x000FFFFF;
66 count = ((count + 7) >> 3) << 3;
67 int i;
68 if (mode & 0x01000000) {
69 int32_t word = GBALoad32(&gba->memory.d, source);
70 for (i = 0; i < count; ++i) {
71 GBAStore32(&gba->memory.d, dest + (i << 2), word);
72 }
73 } else {
74 for (i = 0; i < count; ++i) {
75 int32_t word = GBALoad32(&gba->memory.d, source + (i << 2));
76 GBAStore32(&gba->memory.d, dest + (i << 2), word);
77 }
78 }
79}
80
81static void _MidiKey2Freq(struct GBA* gba) {
82 uint32_t key = GBALoad32(&gba->memory.d, gba->cpu.gprs[0] + 4);
83 gba->cpu.gprs[0] = key / powf(2, (180.f - gba->cpu.gprs[1] - gba->cpu.gprs[2] / 256.f) / 12.f);
84}
85
86void GBASwi16(struct ARMBoard* board, int immediate) {
87 struct GBA* gba = ((struct GBABoard*) board)->p;
88 switch (immediate) {
89 case 0x1:
90 _RegisterRamReset(gba);
91 break;
92 case 0x2:
93 GBAHalt(gba);
94 break;
95 case 0x05:
96 // VBlankIntrWait
97 gba->cpu.gprs[0] = 1;
98 gba->cpu.gprs[1] = 1;
99 // Fall through:
100 case 0x04:
101 // IntrWait
102 gba->memory.io[REG_IME >> 1] = 1;
103 if (!gba->cpu.gprs[0] && gba->memory.io[REG_IF >> 1] & gba->cpu.gprs[1]) {
104 break;
105 }
106 gba->memory.io[REG_IF >> 1] = 0;
107 ARMRaiseSWI(&gba->cpu);
108 break;
109 case 0x6:
110 {
111 div_t result = div(gba->cpu.gprs[0], gba->cpu.gprs[1]);
112 gba->cpu.gprs[0] = result.quot;
113 gba->cpu.gprs[1] = result.rem;
114 gba->cpu.gprs[3] = abs(result.quot);
115 }
116 break;
117 case 0x7:
118 {
119 div_t result = div(gba->cpu.gprs[1], gba->cpu.gprs[0]);
120 gba->cpu.gprs[0] = result.quot;
121 gba->cpu.gprs[1] = result.rem;
122 gba->cpu.gprs[3] = abs(result.quot);
123 }
124 break;
125 case 0x8:
126 gba->cpu.gprs[0] = sqrt(gba->cpu.gprs[0]);
127 break;
128 case 0xB:
129 _CpuSet(gba);
130 break;
131 case 0xC:
132 _FastCpuSet(gba);
133 break;
134 case 0x11:
135 case 0x12:
136 switch (gba->cpu.gprs[1] >> BASE_OFFSET) {
137 case REGION_WORKING_RAM:
138 _unLz77(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->memory.wram)[(gba->cpu.gprs[1] & (SIZE_WORKING_RAM - 1))]);
139 break;
140 case REGION_WORKING_IRAM:
141 _unLz77(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->memory.iwram)[(gba->cpu.gprs[1] & (SIZE_WORKING_IRAM - 1))]);
142 break;
143 case REGION_VRAM:
144 _unLz77(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->video.vram)[(gba->cpu.gprs[1] & 0x0001FFFF)]);
145 break;
146 default:
147 GBALog(GBA_LOG_WARN, "Bad LZ77 destination");
148 break;
149 }
150 break;
151 case 0x1F:
152 _MidiKey2Freq(gba);
153 break;
154 default:
155 GBALog(GBA_LOG_STUB, "Stub software interrupt: %02x", immediate);
156 }
157}
158
159void GBASwi32(struct ARMBoard* board, int immediate) {
160 GBASwi16(board, immediate >> 16);
161}
162
163static void _unLz77(struct GBAMemory* memory, uint32_t source, uint8_t* dest) {
164 int remaining = (GBALoad32(&memory->d, source) & 0xFFFFFF00) >> 8;
165 // We assume the signature byte (0x10) is correct
166 int blockheader;
167 uint32_t sPointer = source + 4;
168 uint8_t* dPointer = dest;
169 int blocksRemaining = 0;
170 int block;
171 uint8_t* disp;
172 int bytes;
173 while (remaining > 0) {
174 if (blocksRemaining) {
175 if (blockheader & 0x80) {
176 // Compressed
177 block = GBALoadU8(&memory->d, sPointer) | (GBALoadU8(&memory->d, sPointer + 1) << 8);
178 sPointer += 2;
179 disp = dPointer - (((block & 0x000F) << 8) | ((block & 0xFF00) >> 8)) - 1;
180 bytes = ((block & 0x00F0) >> 4) + 3;
181 while (bytes-- && remaining) {
182 --remaining;
183 *dPointer = *disp;
184 ++disp;
185 ++dPointer;
186 }
187 } else {
188 // Uncompressed
189 *dPointer = GBALoadU8(&memory->d, sPointer++);
190 ++dPointer;
191 --remaining;
192 }
193 blockheader <<= 1;
194 --blocksRemaining;
195 } else {
196 blockheader = GBALoadU8(&memory->d, sPointer++);
197 blocksRemaining = 8;
198 }
199 }
200}