OSDN Git Service

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