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[2] = 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[2] = abs(result.quot);
123 }
124 break;
125 case 0xB:
126 _CpuSet(gba);
127 break;
128 case 0xC:
129 _FastCpuSet(gba);
130 break;
131 case 0x11:
132 case 0x12:
133 switch (gba->cpu.gprs[1] >> BASE_OFFSET) {
134 case REGION_WORKING_RAM:
135 _unLz77(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->memory.wram)[(gba->cpu.gprs[1] & (SIZE_WORKING_RAM - 1))]);
136 break;
137 case REGION_VRAM:
138 _unLz77(&gba->memory, gba->cpu.gprs[0], &((uint8_t*) gba->video.vram)[(gba->cpu.gprs[1] & 0x0001FFFF)]);
139 break;
140 default:
141 GBALog(GBA_LOG_WARN, "Bad LZ77 destination");
142 break;
143 }
144 break;
145 case 0x1F:
146 _MidiKey2Freq(gba);
147 break;
148 default:
149 GBALog(GBA_LOG_STUB, "Stub software interrupt: %02x", immediate);
150 }
151}
152
153void GBASwi32(struct ARMBoard* board, int immediate) {
154 GBASwi16(board, immediate >> 16);
155}
156
157static void _unLz77(struct GBAMemory* memory, uint32_t source, uint8_t* dest) {
158 int remaining = (GBALoad32(&memory->d, source) & 0xFFFFFF00) >> 8;
159 // We assume the signature byte (0x10) is correct
160 int blockheader;
161 uint32_t sPointer = source + 4;
162 uint8_t* dPointer = dest;
163 int blocksRemaining = 0;
164 int block;
165 uint8_t* disp;
166 int bytes;
167 while (remaining > 0) {
168 if (blocksRemaining) {
169 if (blockheader & 0x80) {
170 // Compressed
171 block = GBALoadU8(&memory->d, sPointer) | (GBALoadU8(&memory->d, sPointer + 1) << 8);
172 sPointer += 2;
173 disp = dPointer - (((block & 0x000F) << 8) | ((block & 0xFF00) >> 8)) - 1;
174 bytes = ((block & 0x00F0) >> 4) + 3;
175 while (bytes-- && remaining) {
176 --remaining;
177 *dPointer = *disp;
178 ++disp;
179 ++dPointer;
180 }
181 } else {
182 // Uncompressed
183 *dPointer = GBALoadU8(&memory->d, sPointer++);
184 ++dPointer;
185 --remaining;
186 }
187 blockheader <<= 1;
188 --blocksRemaining;
189 } else {
190 blockheader = GBALoadU8(&memory->d, sPointer++);
191 blocksRemaining = 8;
192 }
193 }
194}