Debian Codegraph @

gba/player/player.cpp

raw
1 #include <gba/gba.hpp>
2
3 namespace GameBoyAdvance {
4
5 //Game Boy Player emulation
6
7 #include "serialization.cpp"
8 Player player;
9
10 void Player::power() {
11 status.enable = false;
12 status.rumble = false;
13
14 status.logoDetected = false;
15 status.logoCounter = 0;
16
17 status.packet = 0;
18 status.send = 0;
19 status.recv = 0;
20 }
21
22 void Player::frame() {
23 uint32 hash = crc32_calculate((const uint8*)ppu.output, 240 * 160 * sizeof(uint32));
24 status.logoDetected = (hash == 0x7776eb55);
25
26 if(status.logoDetected) {
27 status.enable = true;
28 status.logoCounter = (status.logoCounter + 1) % 3;
29 status.packet = 0;
30 }
31
32 if(status.enable == false) return;
33
34 if(cpu.regs.joybus.settings == 0x0000 && cpu.regs.serial.control == 0x5088) {
35 status.packet = (status.packet + 1) % 17;
36 switch(status.packet) {
37 case 0: status.send = 0x0000494e; break;
38 case 1: status.send = 0xb6b1494e; break;
39 case 2: status.send = 0xb6b1494e; break;
40 case 3: status.send = 0xb6b1544e; break;
41 case 4: status.send = 0xabb1544e; break;
42 case 5: status.send = 0xabb14e45; break;
43 case 6: status.send = 0xb1ba4e45; break;
44 case 7: status.send = 0xb1ba4f44; break;
45 case 8: status.send = 0xb0bb4f44; break;
46 case 9: status.send = 0xb0bb8002; break;
47 case 10: status.send = 0x10000010; break;
48 case 11: status.send = 0x20000013; break;
49 case 12: status.send = 0x30000003; break;
50 case 13: status.send = 0x30000003; break;
51 case 14: status.send = 0x30000003; break;
52 case 15: status.send = 0x30000003; break;
53 case 16: status.send = 0x30000003; break;
54 }
55 cpu.regs.irq.flag.serial = true;
56 }
57 }
58
59 optional<uint16> Player::keyinput() {
60 if(status.logoDetected == false) return false;
61
62 switch(status.logoCounter) {
63 case 0: return {true, 0x03ff};
64 case 1: return {true, 0x03ff};
65 case 2: return {true, 0x030f};
66 }
67 unreachable;
68 }
69
70 optional<uint32> Player::read() {
71 if(status.enable == false) return false;
72
73 return {true, status.send};
74 }
75
76 void Player::write(uint8 byte, uint2 addr) {
77 if(status.enable == false) return;
78
79 unsigned shift = addr << 3;
80 status.recv &= ~(255 << shift);
81 status.recv |= byte << shift;
82
83 if(addr == 3 && status.packet == 15) {
84 status.rumble = (status.recv & 0xff) == 0x26; //on = 0x26, off = 0x04
85 interface->inputRumble(0, 0, 10, status.rumble);
86 }
87 }
88
89 }