OSDN Git Service

[VM][STATE] Use namespace {VMNAME} to separate per VMs.
[csp-qt/common_source_project-fm7.git] / source / src / vm / pc8201 / pc8201.cpp
1 /*
2         NEC PC-8201 Emulator 'ePC-8201'
3
4         Author : Takeda.Toshiya
5         Date   : 2009.03.31-
6
7         [ virtual machine ]
8 */
9
10 #include "pc8201.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../datarec.h"
16 #include "../i8080.h"
17 #include "../i8155.h"
18 #include "../io.h"
19 #include "../noise.h"
20 #include "../pcm1bit.h"
21 #include "../upd1990a.h"
22
23 #ifdef USE_DEBUGGER
24 #include "../debugger.h"
25 #endif
26
27 #include "cmt.h"
28 #include "keyboard.h"
29 #include "lcd.h"
30 #include "./memory.h"
31
32 using PC8201::CMT;
33 using PC8201::KEYBOARD;
34 using PC8201::LCD;
35 using PC8201::MEMORY;
36
37 // ----------------------------------------------------------------------------
38 // initialize
39 // ----------------------------------------------------------------------------
40
41 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
42 {
43         // create devices
44         first_device = last_device = NULL;
45         dummy = new DEVICE(this, emu);  // must be 1st device
46         event = new EVENT(this, emu);   // must be 2nd device
47         
48         drec = new DATAREC(this, emu);
49         drec->set_context_noise_play(new NOISE(this, emu));
50         drec->set_context_noise_stop(new NOISE(this, emu));
51         drec->set_context_noise_fast(new NOISE(this, emu));
52         cpu = new I8080(this, emu);
53         pio = new I8155(this, emu);
54         io = new IO(this, emu);
55         pcm = new PCM1BIT(this, emu);
56         rtc = new UPD1990A(this, emu);
57         
58         cmt = new CMT(this, emu);
59         keyboard = new KEYBOARD(this, emu);
60         lcd = new LCD(this, emu);
61         memory = new MEMORY(this, emu);
62         
63         // set contexts
64         event->set_context_cpu(cpu);
65         event->set_context_sound(pcm);
66         event->set_context_sound(drec);
67         event->set_context_sound(drec->get_context_noise_play());
68         event->set_context_sound(drec->get_context_noise_stop());
69         event->set_context_sound(drec->get_context_noise_fast());
70         
71         drec->set_context_ear(cpu, SIG_I8085_SID, 1);
72         cpu->set_context_sod(cmt, SIG_CMT_SOD, 1);
73         pio->set_context_port_a(rtc, SIG_UPD1990A_C0, 1, 0);
74         pio->set_context_port_a(rtc, SIG_UPD1990A_C1, 2, 0);
75         pio->set_context_port_a(rtc, SIG_UPD1990A_C2, 4, 0);
76         pio->set_context_port_a(rtc, SIG_UPD1990A_CLK, 8, 0);
77         pio->set_context_port_a(rtc, SIG_UPD1990A_DIN, 0x10, 0);
78         pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN_L, 0xff, 0);
79         pio->set_context_port_a(lcd, SIG_LCD_CHIPSEL_L, 0xff, 0);
80         pio->set_context_port_b(keyboard, SIG_KEYBOARD_COLUMN_H, 1, 0);
81         pio->set_context_port_b(lcd, SIG_LCD_CHIPSEL_H, 3, 0);
82         pio->set_context_port_b(pcm, SIG_PCM1BIT_MUTE, 0x20, 0);
83         pio->set_context_timer(pcm, SIG_PCM1BIT_SIGNAL, 1);
84         pio->set_constant_clock(CPU_CLOCKS);
85         rtc->set_context_dout(pio, SIG_I8155_PORT_C, 1);
86         rtc->set_context_tp(cpu, SIG_I8085_RST7, 1);
87         
88         memory->set_context_cmt(cmt);
89         memory->set_context_drec(drec);
90         memory->set_context_rtc(rtc);
91         
92         // cpu bus
93         cpu->set_context_mem(memory);
94         cpu->set_context_io(io);
95         cpu->set_context_intr(io);
96 #ifdef USE_DEBUGGER
97         cpu->set_context_debugger(new DEBUGGER(this, emu));
98 #endif
99         
100         // i/o bus
101         io->set_iomap_range_w(0x90, 0x9f, memory);
102         io->set_iomap_range_rw(0xa0, 0xaf, memory);
103         io->set_iomap_range_rw(0xb0, 0xbf, pio);
104         io->set_iomap_range_r(0xe0, 0xef, keyboard);
105         io->set_iomap_range_rw(0xf0, 0xff, lcd);
106         
107         // initialize all devices
108 #if defined(__GIT_REPO_VERSION)
109         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
110 #endif
111         for(DEVICE* device = first_device; device; device = device->next_device) {
112                 device->initialize();
113         }
114         rtc->write_signal(SIG_UPD1990A_STB, 0, 0);
115 }
116
117 VM::~VM()
118 {
119         // delete all devices
120         for(DEVICE* device = first_device; device;) {
121                 DEVICE *next_device = device->next_device;
122                 device->release();
123                 delete device;
124                 device = next_device;
125         }
126 }
127
128 DEVICE* VM::get_device(int id)
129 {
130         for(DEVICE* device = first_device; device; device = device->next_device) {
131                 if(device->this_device_id == id) {
132                         return device;
133                 }
134         }
135         return NULL;
136 }
137
138 // ----------------------------------------------------------------------------
139 // drive virtual machine
140 // ----------------------------------------------------------------------------
141
142 void VM::reset()
143 {
144         // reset all devices
145         for(DEVICE* device = first_device; device; device = device->next_device) {
146                 device->reset();
147         }
148 }
149
150 void VM::run()
151 {
152         event->drive();
153 }
154
155 // ----------------------------------------------------------------------------
156 // debugger
157 // ----------------------------------------------------------------------------
158
159 #ifdef USE_DEBUGGER
160 DEVICE *VM::get_cpu(int index)
161 {
162         if(index == 0) {
163                 return cpu;
164         }
165         return NULL;
166 }
167 #endif
168
169 // ----------------------------------------------------------------------------
170 // draw screen
171 // ----------------------------------------------------------------------------
172
173 void VM::draw_screen()
174 {
175         lcd->draw_screen();
176 }
177
178 // ----------------------------------------------------------------------------
179 // soud manager
180 // ----------------------------------------------------------------------------
181
182 void VM::initialize_sound(int rate, int samples)
183 {
184         // init sound manager
185         event->initialize_sound(rate, samples);
186         
187         // init sound gen
188         pcm->initialize_sound(rate, 8000);
189 }
190
191 uint16_t* VM::create_sound(int* extra_frames)
192 {
193         return event->create_sound(extra_frames);
194 }
195
196 int VM::get_sound_buffer_ptr()
197 {
198         return event->get_sound_buffer_ptr();
199 }
200
201 #ifdef USE_SOUND_VOLUME
202 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
203 {
204         if(ch == 0) {
205                 pcm->set_volume(0, decibel_l, decibel_r);
206         } else if(ch == 1) {
207                 drec->set_volume(0, decibel_l, decibel_r);
208         } else if(ch == 2) {
209                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
210                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
211                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
212         }
213 }
214 #endif
215
216 // ----------------------------------------------------------------------------
217 // notify key
218 // ----------------------------------------------------------------------------
219
220 void VM::key_down(int code, bool repeat)
221 {
222         keyboard->key_down(code);
223 }
224
225 void VM::key_up(int code)
226 {
227 }
228
229 bool VM::get_caps_locked()
230 {
231         return keyboard->get_caps_locked();
232 }
233
234 bool VM::get_kana_locked()
235 {
236         return keyboard->get_kana_locked();
237 }
238
239 // ----------------------------------------------------------------------------
240 // user interface
241 // ----------------------------------------------------------------------------
242
243 void VM::play_tape(int drv, const _TCHAR* file_path)
244 {
245         cmt->close_tape();
246         drec->play_tape(file_path);
247 //      drec->set_remote(true);
248 }
249
250 void VM::rec_tape(int drv, const _TCHAR* file_path)
251 {
252         emu->lock_vm();
253         drec->close_tape();
254         emu->unlock_vm();
255 //      drec->set_remote(false);
256         cmt->rec_tape(file_path);
257 }
258
259 void VM::close_tape(int drv)
260 {
261         emu->lock_vm();
262         drec->close_tape();
263         emu->unlock_vm();
264 //      drec->set_remote(false);
265         cmt->close_tape();
266 }
267
268 bool VM::is_tape_inserted(int drv)
269 {
270         return drec->is_tape_inserted() || cmt->is_tape_inserted();
271 }
272
273 bool VM::is_tape_playing(int drv)
274 {
275         if(drec->is_tape_inserted()) {
276                 return drec->is_tape_playing();
277         } else {
278                 return cmt->is_tape_playing();
279         }
280 }
281
282 bool VM::is_tape_recording(int drv)
283 {
284         if(drec->is_tape_inserted()) {
285                 return drec->is_tape_recording();
286         } else {
287                 return cmt->is_tape_recording();
288         }
289 }
290
291 int VM::get_tape_position(int drv)
292 {
293         if(drec->is_tape_inserted()) {
294                 return drec->get_tape_position();
295         } else {
296                 return cmt->get_tape_position();
297         }
298 }
299
300 const _TCHAR* VM::get_tape_message(int drv)
301 {
302         if(drec->is_tape_inserted()) {
303                 return drec->get_message();
304         } else {
305                 return NULL;
306         }
307 }
308
309 void VM::push_play(int drv)
310 {
311         if(drec->is_tape_inserted()) {
312                 drec->set_ff_rew(0);
313                 drec->set_remote(true);
314         }
315 }
316
317 void VM::push_stop(int drv)
318 {
319         if(drec->is_tape_inserted()) {
320                 drec->set_remote(false);
321         }
322 }
323
324 void VM::push_fast_forward(int drv)
325 {
326         if(drec->is_tape_inserted()) {
327                 drec->set_ff_rew(1);
328                 drec->set_remote(true);
329         }
330 }
331
332 void VM::push_fast_rewind(int drv)
333 {
334         if(drec->is_tape_inserted()) {
335                 drec->set_ff_rew(-1);
336                 drec->set_remote(true);
337         }
338 }
339
340 bool VM::is_frame_skippable()
341 {
342         return event->is_frame_skippable();
343 }
344
345 void VM::update_config()
346 {
347         for(DEVICE* device = first_device; device; device = device->next_device) {
348                 device->update_config();
349         }
350 }
351
352 #define STATE_VERSION   3
353
354 bool VM::process_state(FILEIO* state_fio, bool loading)
355 {
356         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
357                 return false;
358         }
359         for(DEVICE* device = first_device; device; device = device->next_device) {
360                 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
361                 // const char *name = typeid(*device).name();
362                 //       But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
363                 const char *name = device->get_device_name();
364                 int len = strlen(name);
365                 
366                 if(!state_fio->StateCheckInt32(len)) {
367                         if(loading) {
368                                 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
369                         }
370                         return false;
371                 }
372                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
373                         if(loading) {
374                                 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
375                         }
376                         return false;
377                 }
378                 if(!device->process_state(state_fio, loading)) {
379                         if(loading) {
380                                 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
381                         }
382                         return false;
383                 }
384         }
385         // Machine specified.
386         return true;
387 }