yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
snes.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EMU_SNES_H
2#define YAZE_APP_EMU_SNES_H
3
4#include <cstdint>
5#include <memory>
6#include <string>
7#include <thread>
8
9#include "app/emu/audio/apu.h"
11#include "app/emu/cpu/clock.h"
12#include "app/emu/cpu/cpu.h"
14#include "app/emu/video/ppu.h"
15#include "app/rom.h"
16
17namespace yaze {
18namespace app {
19namespace emu {
20
21struct Input {
22 uint8_t type;
23 // latchline
25 // for controller
26 uint16_t current_state_; // actual state
28};
29
30class SNES {
31 public:
32 SNES() = default;
33 ~SNES() = default;
34
35 // Initialization
36 void Init(std::vector<uint8_t>& rom_data);
37 void Reset(bool hard = false);
38
39 // Emulation
40 void RunFrame();
41 void CatchUpApu();
42
43 // Controller input handling
44 void HandleInput();
45
46 // Clock cycling and synchronization
47 void RunCycle();
48 void RunCycles(int cycles);
49 void SyncCycles(bool start, int sync_cycles);
50
51 uint8_t ReadBBus(uint8_t adr);
52 uint8_t ReadReg(uint16_t adr);
53 uint8_t Rread(uint32_t adr);
54 uint8_t Read(uint32_t adr);
55
56 void WriteBBus(uint8_t adr, uint8_t val);
57 void WriteReg(uint16_t adr, uint8_t val);
58 void Write(uint32_t adr, uint8_t val);
59
60 int GetAccessTime(uint32_t adr);
61 uint8_t CpuRead(uint32_t adr);
62 void CpuWrite(uint32_t adr, uint8_t val);
63 void CpuIdle(bool waiting);
64
65 void SetSamples(int16_t* sample_data, int wanted_samples);
66 void SetPixels(uint8_t* pixel_data);
67 void SetButtonState(int player, int button, bool pressed);
68 bool running() const { return running_; }
69 auto cpu() -> Cpu& { return cpu_; }
70 auto ppu() -> video::Ppu& { return ppu_; }
71 auto apu() -> audio::Apu& { return apu_; }
72 auto Memory() -> memory::MemoryImpl& { return memory_; }
73 auto get_ram() -> uint8_t* { return ram; }
74 auto mutable_cycles() -> uint64_t& { return cycles_; }
75 void InitAccessTime(bool recalc);
76
77 std::vector<uint8_t> access_time;
78
79 private:
80 // Components of the SNES
83
85 [&](uint32_t adr) { return CpuRead(adr); },
86 [&](uint32_t adr, uint8_t val) { CpuWrite(adr, val); },
87 [&](bool waiting) { CpuIdle(waiting); },
88 };
92
93 // Currently loaded ROM
94 std::vector<uint8_t> rom_data;
95
96 // Emulation state
97 bool running_ = false;
98
99 // ram
100 uint8_t ram[0x20000];
101 uint32_t ram_adr_;
102
103 // Frame timing
104 uint32_t frames_ = 0;
105 uint64_t cycles_ = 0;
106 uint64_t sync_cycle_ = 0;
109
110 // Nmi / Irq
111 bool h_irq_enabled_ = false;
112 bool v_irq_enabled_ = false;
113 bool nmi_enabled_ = false;
114 uint16_t h_timer_ = 0;
115 uint16_t v_timer_ = 0;
116 bool in_nmi_ = false;
117 bool irq_condition_ = false;
118 bool in_irq_ = false;
120
121 // Multiplication / Division
122 uint8_t multiply_a_;
124 uint16_t divide_a_;
126
127 // Joypad State
130 uint16_t port_auto_read_[4]; // as read by auto-joypad read
131 bool auto_joy_read_ = false;
132 uint16_t auto_joy_timer_ = 0;
134
135 bool fast_mem_ = false;
136};
137
138} // namespace emu
139} // namespace app
140} // namespace yaze
141
142#endif // YAZE_APP_EMU_SNES_H
uint64_t cycles_
Definition snes.h:105
uint16_t port_auto_read_[4]
Definition snes.h:130
auto apu() -> audio::Apu &
Definition snes.h:71
uint8_t multiply_a_
Definition snes.h:122
audio::Apu apu_
Definition snes.h:91
void RunCycles(int cycles)
Definition snes.cc:232
uint32_t ram_adr_
Definition snes.h:101
memory::MemoryImpl memory_
Definition snes.h:82
void InitAccessTime(bool recalc)
Definition snes.cc:568
void Init(std::vector< uint8_t > &rom_data)
Definition snes.cc:36
void SyncCycles(bool start, int sync_cycles)
Definition snes.cc:242
uint8_t ReadReg(uint16_t adr)
Definition snes.cc:269
uint8_t Rread(uint32_t adr)
Definition snes.cc:322
void CpuIdle(bool waiting)
Definition snes.cc:539
memory::CpuCallbacks cpu_callbacks_
Definition snes.h:84
ClockImpl clock_
Definition snes.h:81
auto Memory() -> memory::MemoryImpl &
Definition snes.h:72
uint64_t sync_cycle_
Definition snes.h:106
void SetSamples(int16_t *sample_data, int wanted_samples)
Definition snes.cc:545
uint16_t divide_result_
Definition snes.h:125
void CatchUpApu()
Definition snes.cc:98
uint8_t CpuRead(uint32_t adr)
Definition snes.cc:520
std::vector< uint8_t > access_time
Definition snes.h:77
auto mutable_cycles() -> uint64_t &
Definition snes.h:74
auto ppu() -> video::Ppu &
Definition snes.h:70
void Write(uint32_t adr, uint8_t val)
Definition snes.cc:476
double apu_catchup_cycles_
Definition snes.h:107
uint16_t auto_joy_timer_
Definition snes.h:132
uint32_t frames_
Definition snes.h:104
void CpuWrite(uint32_t adr, uint8_t val)
Definition snes.cc:531
void WriteBBus(uint8_t adr, uint8_t val)
Definition snes.cc:358
int GetAccessTime(uint32_t adr)
Definition snes.cc:506
auto cpu() -> Cpu &
Definition snes.h:69
void SetButtonState(int player, int button, bool pressed)
Definition snes.cc:551
auto get_ram() -> uint8_t *
Definition snes.h:73
uint16_t h_timer_
Definition snes.h:114
uint8_t ram[0x20000]
Definition snes.h:100
void SetPixels(uint8_t *pixel_data)
Definition snes.cc:549
uint32_t next_horiz_event
Definition snes.h:108
uint16_t v_timer_
Definition snes.h:115
bool running() const
Definition snes.h:68
uint16_t multiply_result_
Definition snes.h:123
void WriteReg(uint16_t adr, uint8_t val)
Definition snes.cc:389
uint16_t divide_a_
Definition snes.h:124
std::vector< uint8_t > rom_data
Definition snes.h:94
video::Ppu ppu_
Definition snes.h:90
uint8_t ReadBBus(uint8_t adr)
Definition snes.cc:253
void Reset(bool hard=false)
Definition snes.cc:48
uint8_t Read(uint32_t adr)
Definition snes.cc:352
The Apu class represents the Audio Processing Unit (APU) of a system.
Definition apu.h:53
Implementation of the Memory interface for emulating memory in a SNES system.
Definition memory.h:102
Definition common.cc:22
uint16_t current_state_
Definition snes.h:26
uint16_t latched_state_
Definition snes.h:27