Debian Codegraph @

gba/cpu/cpu.cpp

raw
1 #include <gba/gba.hpp>
2
3 namespace GameBoyAdvance {
4
5 #include "registers.cpp"
6 #include "mmio.cpp"
7 #include "memory.cpp"
8 #include "dma.cpp"
9 #include "timer.cpp"
10 #include "serialization.cpp"
11 CPU cpu;
12
13 void CPU::Enter() {
14 while(true) {
15 if(scheduler.sync == Scheduler::SynchronizeMode::CPU) {
16 scheduler.sync = Scheduler::SynchronizeMode::All;
17 scheduler.exit(Scheduler::ExitReason::SynchronizeEvent);
18 }
19
20 cpu.main();
21 }
22 }
23
24 void CPU::main() {
25 #if defined(DEBUG)
26 if(crash) {
27 print(cpsr().t ? disassemble_thumb_instruction(pipeline.execute.address)
28 : disassemble_arm_instruction(pipeline.execute.address), "\n");
29 print(disassemble_registers(), "\n");
30 print("Executed: ", instructions, "\n");
31 while(true) step(frequency);
32 }
33 #endif
34
35 processor.irqline = regs.ime && (regs.irq.enable & regs.irq.flag);
36
37 if(regs.mode == Registers::Mode::Stop) {
38 if((regs.irq.enable.keypad & regs.irq.flag.keypad) == 0) {
39 sync_step(16); //STOP does not advance timers
40 } else {
41 regs.mode = Registers::Mode::Normal;
42 }
43 return;
44 }
45
46 dma_run();
47
48 if(regs.mode == Registers::Mode::Halt) {
49 if((regs.irq.enable & regs.irq.flag) == 0) {
50 step(16);
51 } else {
52 regs.mode = Registers::Mode::Normal;
53 }
54 return;
55 }
56
57 exec();
58 }
59
60 void CPU::step(unsigned clocks) {
61 timer_step(clocks);
62 sync_step(clocks);
63 }
64
65 void CPU::sync_step(unsigned clocks) {
66 ppu.clock -= clocks;
67 if(ppu.clock < 0) co_switch(ppu.thread);
68
69 apu.clock -= clocks;
70 if(apu.clock < 0) co_switch(apu.thread);
71 }
72
73 void CPU::bus_idle(uint32 addr) {
74 step(1);
75 return bus.idle(addr);
76 }
77
78 uint32 CPU::bus_read(uint32 addr, uint32 size) {
79 step(bus.speed(addr, size));
80 return bus.read(addr, size);
81 }
82
83 void CPU::bus_write(uint32 addr, uint32 size, uint32 word) {
84 step(bus.speed(addr, size));
85 return bus.write(addr, size, word);
86 }
87
88 void CPU::keypad_run() {
89 if(regs.keypad.control.enable == false) return;
90
91 bool test = regs.keypad.control.condition; //0 = OR, 1 = AND
92 for(unsigned n = 0; n < 10; n++) {
93 if(regs.keypad.control.flag[n] == false) continue;
94 bool input = interface->inputPoll(0, 0, n);
95 if(regs.keypad.control.condition == 0) test |= input;
96 if(regs.keypad.control.condition == 1) test &= input;
97 }
98 if(test) regs.irq.flag.keypad = true;
99 }
100
101 void CPU::power() {
102 create(CPU::Enter, 16777216);
103
104 ARM::power();
105 for(unsigned n = 0; n < 32 * 1024; n++) iwram[n] = 0;
106 for(unsigned n = 0; n < 256 * 1024; n++) ewram[n] = 0;
107
108 for(auto& dma : regs.dma) {
109 dma.source = 0;
110 dma.target = 0;
111 dma.length = 0;
112 dma.control = 0;
113 dma.pending = 0;
114 dma.run.target = 0;
115 dma.run.source = 0;
116 dma.run.length = 0;
117 }
118 for(auto& timer : regs.timer) {
119 timer.period = 0;
120 timer.reload = 0;
121 timer.control = 0;
122 }
123 regs.keypad.control = 0;
124 regs.ime = 0;
125 regs.irq.enable = 0;
126 regs.irq.flag = 0;
127 regs.wait.control = 0;
128 regs.postboot = 0;
129 regs.mode = Registers::Mode::Normal;
130 regs.clock = 0;
131 regs.memory.control = 0x0d000020;
132
133 pending.dma.vblank = 0;
134 pending.dma.hblank = 0;
135 pending.dma.hdma = 0;
136
137 for(unsigned n = 0x0b0; n <= 0x0df; n++) bus.mmio[n] = this; //DMA
138 for(unsigned n = 0x100; n <= 0x10f; n++) bus.mmio[n] = this; //Timers
139 for(unsigned n = 0x120; n <= 0x12b; n++) bus.mmio[n] = this; //Serial
140 for(unsigned n = 0x130; n <= 0x133; n++) bus.mmio[n] = this; //Keypad
141 for(unsigned n = 0x134; n <= 0x159; n++) bus.mmio[n] = this; //Serial
142 for(unsigned n = 0x200; n <= 0x209; n++) bus.mmio[n] = this; //System
143 for(unsigned n = 0x300; n <= 0x301; n++) bus.mmio[n] = this; //System
144 //0x080-0x083 mirrored via gba/memory/memory.cpp //System
145 }
146
147 CPU::CPU() {
148 iwram = new uint8[ 32 * 1024];
149 ewram = new uint8[256 * 1024];
150
151 regs.dma[0].source.bits(27); regs.dma[0].run.source.bits(27);
152 regs.dma[0].target.bits(27); regs.dma[0].run.target.bits(27);
153 regs.dma[0].length.bits(14); regs.dma[0].run.length.bits(14);
154
155 regs.dma[1].source.bits(28); regs.dma[1].run.source.bits(28);
156 regs.dma[1].target.bits(27); regs.dma[1].run.target.bits(27);
157 regs.dma[1].length.bits(14); regs.dma[1].run.length.bits(14);
158
159 regs.dma[2].source.bits(28); regs.dma[2].run.source.bits(28);
160 regs.dma[2].target.bits(27); regs.dma[2].run.target.bits(27);
161 regs.dma[2].length.bits(14); regs.dma[2].run.length.bits(14);
162
163 regs.dma[3].source.bits(28); regs.dma[3].run.source.bits(28);
164 regs.dma[3].target.bits(28); regs.dma[3].run.target.bits(28);
165 regs.dma[3].length.bits(16); regs.dma[3].run.length.bits(16);
166 }
167
168 CPU::~CPU() {
169 delete[] iwram;
170 delete[] ewram;
171 }
172
173 }