OSDN Git Service

[VM] Apply new APIs to all VMs.
[csp-qt/common_source_project-fm7.git] / source / src / vm / tvboy / tvboy.cpp
1 /*
2         GAKKEN TV BOY Emulator 'yaTVBOY'
3
4         Author : tanam
5         Date   : 2020.06.13
6
7         [ virtual machine ]
8 */
9
10 #include "tvboy.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../mc6847.h"
16 #include "../mc6801.h"
17 #include "../pcm1bit.h"
18
19 #ifdef USE_DEBUGGER
20 #include "../debugger.h"
21 #endif
22
23 #include "./memory.h"
24
25 // ----------------------------------------------------------------------------
26 // initialize
27 // ----------------------------------------------------------------------------
28 using TVBOY::MEMORY;
29
30 VM::VM(EMU_TEMPLATE* parent_emu) : VM_TEMPLATE(parent_emu)
31 {
32         // create devices
33         first_device = last_device = NULL;
34         dummy = new DEVICE(this, emu);  // must be 1st device
35         event = new EVENT(this, emu);   // must be 2nd device
36         
37         vdp = new MC6847(this, emu);
38         cpu = new MC6801(this, emu);
39         
40         memory = new MEMORY(this, emu);
41         memory->set_context_cpu(cpu);
42         memory->set_context_vdp(vdp);
43
44         pcm = new PCM1BIT(this, emu);
45
46         cpu->set_context_port1(pcm, SIG_PCM1BIT_SIGNAL, 0x20, 0);
47         cpu->set_context_port1(pcm, SIG_PCM1BIT_SIGNAL, 0x40, 0);
48         cpu->set_context_port1(memory, SIG_MEMORY_PORT_1, 0x0F, 0);
49
50         // set contexts
51         event->set_context_cpu(cpu);
52         event->set_context_sound(pcm);
53         
54         vdp->set_vram_ptr(memory->get_vram(), 0x800);
55         vdp->set_context_cpu(cpu);
56                 
57         // cpu bus
58         cpu->set_context_mem(memory);
59 #ifdef USE_DEBUGGER
60         cpu->set_context_debugger(new DEBUGGER(this, emu));
61 #endif
62         pcm = new PCM1BIT(this, emu);
63
64         // initialize all devices
65 #if defined(__GIT_REPO_VERSION)
66         set_git_repo_version(__GIT_REPO_VERSION);
67 #endif
68         initialize_devices();
69 }
70
71 VM::~VM()
72 {
73         // delete all devices
74         for(DEVICE* device = first_device; device;) {
75                 DEVICE *next_device = device->next_device;
76                 device->release();
77                 delete device;
78                 device = next_device;
79         }
80 }
81
82 DEVICE* VM::get_device(int id)
83 {
84         for(DEVICE* device = first_device; device; device = device->next_device) {
85                 if(device->this_device_id == id) {
86                         return device;
87                 }
88         }
89         return NULL;
90 }
91
92 // ----------------------------------------------------------------------------
93 // drive virtual machine
94 // ----------------------------------------------------------------------------
95
96 void VM::reset()
97 {
98         // reset all devices
99         for(DEVICE* device = first_device; device; device = device->next_device) {
100                 device->reset();
101         }
102 }
103
104 void VM::run()
105 {
106         event->drive();
107 }
108
109 // ----------------------------------------------------------------------------
110 // debugger
111 // ----------------------------------------------------------------------------
112
113 #ifdef USE_DEBUGGER
114 DEVICE *VM::get_cpu(int index)
115 {
116         if(index == 0) {
117                 return cpu;
118         }
119         return NULL;
120 }
121 #endif
122
123 // ----------------------------------------------------------------------------
124 // draw screen
125 // ----------------------------------------------------------------------------
126
127 void VM::draw_screen()
128 {
129         vdp->draw_screen();
130 }
131
132 // ----------------------------------------------------------------------------
133 // notify key
134 // ----------------------------------------------------------------------------
135
136 void VM::key_down(int code, bool repeat)
137 {
138         memory->key_down(code);
139 }
140
141 void VM::key_up(int code)
142 {
143         memory->key_up(code);
144 }
145
146 // ----------------------------------------------------------------------------
147 // soud manager
148 // ----------------------------------------------------------------------------
149
150 void VM::initialize_sound(int rate, int samples)
151 {
152         // init sound manager
153         event->initialize_sound(rate, samples);
154         pcm->initialize_sound(rate, 8000);
155 }
156
157 uint16_t* VM::create_sound(int* extra_frames)
158 {
159         return event->create_sound(extra_frames);
160 }
161
162 int VM::get_sound_buffer_ptr()
163 {
164         return event->get_sound_buffer_ptr();
165 }
166
167 #ifdef USE_SOUND_VOLUME
168 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
169 {
170         if(ch == 0) {
171                 pcm->set_volume(0, decibel_l, decibel_r);
172         }
173 }
174 #endif
175
176 // ----------------------------------------------------------------------------
177 // user interface
178 // ----------------------------------------------------------------------------
179
180 void VM::open_cart(int drv, const _TCHAR* file_path)
181 {
182         if(drv == 0) {
183                 memory->open_cart(file_path);
184                 reset();
185         }
186 }
187
188 void VM::close_cart(int drv)
189 {
190         if(drv == 0) {
191                 memory->close_cart();
192                 reset();
193         }
194 }
195
196 bool VM::is_cart_inserted(int drv)
197 {
198         if(drv == 0) {
199                 return memory->is_cart_inserted();
200         } else {
201                 return false;
202         }
203 }
204
205 bool VM::is_frame_skippable()
206 {
207         return event->is_frame_skippable();
208 }
209
210 void VM::update_config()
211 {
212         for(DEVICE* device = first_device; device; device = device->next_device) {
213                 device->update_config();
214         }
215 }
216
217 #define STATE_VERSION   3
218
219 bool VM::process_state(FILEIO* state_fio, bool loading)
220 {
221         if(!(VM_TEMPLATE::process_state_core(state_fio, loading, STATE_VERSION))) {
222                 return false;
223         }
224         return true;
225 }