OSDN Git Service

[VM] Apply new APIs to all VMs.
[csp-qt/common_source_project-fm7.git] / source / src / vm / jr100 / jr100.cpp
1 /*
2         National JR-100 Emulator 'eJR-100'
3
4         Author : Takeda.Toshiya
5         Date   : 2015.08.27-
6
7         [ virtual machine ]
8 */
9
10 #include "jr100.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../datarec.h"
16 //#include "../mc6800.h"
17 #include "../mb8861.h"
18 #include "../noise.h"
19 #include "../not.h"
20 #include "../pcm1bit.h"
21 #include "../sy6522.h"
22
23 #ifdef USE_DEBUGGER
24 #include "../debugger.h"
25 #endif
26
27 #include "./memory.h"
28
29 using JR100::MEMORY;
30 // ----------------------------------------------------------------------------
31 // initialize
32 // ----------------------------------------------------------------------------
33
34 VM::VM(EMU_TEMPLATE* parent_emu) : VM_TEMPLATE(parent_emu)
35 {
36         // create devices
37         first_device = last_device = NULL;
38         dummy = new DEVICE(this, emu);  // must be 1st device
39         event = new EVENT(this, emu);   // must be 2nd device
40         dummy->set_device_name(_T("1st Dummy"));
41         
42         drec = new DATAREC(this, emu);
43         drec->set_context_noise_play(new NOISE(this, emu));
44         drec->set_context_noise_stop(new NOISE(this, emu));
45         drec->set_context_noise_fast(new NOISE(this, emu));
46         //cpu = new MC6800(this, emu);  // MB8861N
47         cpu = new MB8861(this, emu);    // MB8861N
48         not_mic = new NOT(this, emu);
49         not_mic->set_device_name(_T("NOT Gate (Mic)"));
50         not_ear = new NOT(this, emu);
51         not_ear->set_device_name(_T("NOT Gate (Ear)"));
52         pcm = new PCM1BIT(this, emu);
53         via = new SY6522(this, emu);
54         
55         memory = new MEMORY(this, emu);
56         // set contexts
57         event->set_context_cpu(cpu);
58         event->set_context_sound(pcm);
59         event->set_context_sound(drec);
60         event->set_context_sound(drec->get_context_noise_play());
61         event->set_context_sound(drec->get_context_noise_stop());
62         event->set_context_sound(drec->get_context_noise_fast());
63         
64         via->set_context_port_a(memory, SIG_MEMORY_VIA_PORT_A, 0xff, 0);
65         via->set_context_port_b(memory, SIG_MEMORY_VIA_PORT_B, 0xff, 0);
66         via->set_context_port_b(pcm, SIG_PCM1BIT_SIGNAL, 0x80, 0);      // PB7 -> Speaker
67         via->set_context_port_b(via, SIG_MEMORY_VIA_PORT_B, 0x80, -1);  // PB7 -> PB6
68         via->set_context_cb2(not_mic, SIG_NOT_INPUT, 1);                // CB2 -> NOT -> MIC
69         via->set_constant_clock(CPU_CLOCKS >> 2);
70         not_mic->set_context_out(drec, SIG_DATAREC_MIC, 1);
71         drec->set_context_ear(not_ear, SIG_NOT_INPUT, 1);               // EAR -> NOT -> CA1,CB1
72         not_ear->set_context_out(via, SIG_SY6522_PORT_CA1, 1);
73         not_ear->set_context_out(via, SIG_SY6522_PORT_CB1, 1);
74         // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
75         //pcm->set_realtime_render(true);
76         
77         memory->set_context_via(via);
78         
79         // cpu bus
80         cpu->set_context_mem(memory);
81 #ifdef USE_DEBUGGER
82         cpu->set_context_debugger(new DEBUGGER(this, emu));
83 //      pcm->set_context_debugger(new DEBUGGER(this, emu));
84 #endif
85         
86         // initialize all devices
87 #if defined(__GIT_REPO_VERSION)
88         set_git_repo_version(__GIT_REPO_VERSION);
89 #endif
90         initialize_devices();
91 }
92
93 VM::~VM()
94 {
95         // delete all devices
96         for(DEVICE* device = first_device; device;) {
97                 DEVICE *next_device = device->next_device;
98                 device->release();
99                 delete device;
100                 device = next_device;
101         }
102 }
103
104 DEVICE* VM::get_device(int id)
105 {
106         for(DEVICE* device = first_device; device; device = device->next_device) {
107                 if(device->this_device_id == id) {
108                         return device;
109                 }
110         }
111         return NULL;
112 }
113
114 // ----------------------------------------------------------------------------
115 // drive virtual machine
116 // ----------------------------------------------------------------------------
117
118 void VM::reset()
119 {
120         // reset all devices
121         for(DEVICE* device = first_device; device; device = device->next_device) {
122                 device->reset();
123         }
124 }
125
126 void VM::special_reset(int num)
127 {
128         // reset all devices
129         for(DEVICE* device = first_device; device; device = device->next_device) {
130                 device->reset();
131         }
132 }
133
134 void VM::run()
135 {
136         event->drive();
137 }
138
139 double VM::get_frame_rate()
140 {
141         return event->get_frame_rate();
142 }
143
144 // ----------------------------------------------------------------------------
145 // debugger
146 // ----------------------------------------------------------------------------
147
148 #ifdef USE_DEBUGGER
149 DEVICE *VM::get_cpu(int index)
150 {
151         if(index == 0) {
152                 return cpu;
153         }
154         return NULL;
155 }
156 #endif
157
158 // ----------------------------------------------------------------------------
159 // draw screen
160 // ----------------------------------------------------------------------------
161
162 void VM::draw_screen()
163 {
164         memory->draw_screen();
165 }
166
167 // ----------------------------------------------------------------------------
168 // soud manager
169 // ----------------------------------------------------------------------------
170
171 void VM::initialize_sound(int rate, int samples)
172 {
173         // init sound manager
174         event->initialize_sound(rate, samples);
175         
176         // init sound gen
177         pcm->initialize_sound(rate, 5000);
178 }
179
180 uint16_t* VM::create_sound(int* extra_frames)
181 {
182         return event->create_sound(extra_frames);
183 }
184
185 int VM::get_sound_buffer_ptr()
186 {
187         return event->get_sound_buffer_ptr();
188 }
189
190 #ifdef USE_SOUND_VOLUME
191 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
192 {
193         if(ch == 0) {
194                 pcm->set_volume(0, decibel_l, decibel_r);
195         } else if(ch == 1) {
196                 drec->set_volume(0, decibel_l, decibel_r);
197         } else if(ch == 2) {
198                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
199                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
200                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
201         }
202 }
203 #endif
204
205 // ----------------------------------------------------------------------------
206 // user interface
207 // ----------------------------------------------------------------------------
208
209 void VM::play_tape(int drv, const _TCHAR* file_path)
210 {
211         bool remote = drec->get_remote();
212         
213         if(drec->play_tape(file_path) && remote) {
214                 // if machine already sets remote on, start playing now
215                 push_play(drv);
216         }
217 }
218
219 void VM::rec_tape(int drv, const _TCHAR* file_path)
220 {
221         bool remote = drec->get_remote();
222         
223         if(drec->rec_tape(file_path) && remote) {
224                 // if machine already sets remote on, start recording now
225                 push_play(drv);
226         }
227 }
228
229 void VM::close_tape(int drv)
230 {
231         emu->lock_vm();
232         drec->close_tape();
233         emu->unlock_vm();
234         drec->set_remote(false);
235 }
236
237 bool VM::is_tape_inserted(int drv)
238 {
239         return drec->is_tape_inserted();
240 }
241
242 bool VM::is_tape_playing(int drv)
243 {
244         return drec->is_tape_playing();
245 }
246
247 bool VM::is_tape_recording(int drv)
248 {
249         return drec->is_tape_recording();
250 }
251
252 int VM::get_tape_position(int drv)
253 {
254         return drec->get_tape_position();
255 }
256
257 const _TCHAR* VM::get_tape_message(int drv)
258 {
259         return drec->get_message();
260 }
261
262 void VM::push_play(int drv)
263 {
264         drec->set_remote(false);
265         drec->set_ff_rew(0);
266         drec->set_remote(true);
267 }
268
269 void VM::push_stop(int drv)
270 {
271         drec->set_remote(false);
272 }
273
274 void VM::push_fast_forward(int drv)
275 {
276         drec->set_remote(false);
277         drec->set_ff_rew(1);
278         drec->set_remote(true);
279 }
280
281 void VM::push_fast_rewind(int drv)
282 {
283         drec->set_remote(false);
284         drec->set_ff_rew(-1);
285         drec->set_remote(true);
286 }
287
288 bool VM::is_frame_skippable()
289 {
290         return event->is_frame_skippable();
291 }
292
293 void VM::update_config()
294 {
295         for(DEVICE* device = first_device; device; device = device->next_device) {
296                 device->update_config();
297         }
298 }
299
300 double VM::get_current_usec()
301 {
302         if(event == NULL) return 0.0;
303         return event->get_current_usec();
304 }
305
306 uint64_t VM::get_current_clock_uint64()
307 {
308                 if(event == NULL) return (uint64_t)0;
309                 return event->get_current_clock_uint64();
310 }
311
312 #define STATE_VERSION   3
313
314 bool VM::process_state(FILEIO* state_fio, bool loading)
315 {
316         if(!(VM_TEMPLATE::process_state_core(state_fio, loading, STATE_VERSION))) {
317                 return false;
318         }
319         return true;
320 }