OSDN Git Service

[BUILD] Set SOVERSION and GIT hash automatically.
[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 // ----------------------------------------------------------------------------
33 // initialize
34 // ----------------------------------------------------------------------------
35
36 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
37 {
38         // create devices
39         first_device = last_device = NULL;
40         dummy = new DEVICE(this, emu);  // must be 1st device
41         event = new EVENT(this, emu);   // must be 2nd device
42         
43         drec = new DATAREC(this, emu);
44         drec->set_context_noise_play(new NOISE(this, emu));
45         drec->set_context_noise_stop(new NOISE(this, emu));
46         drec->set_context_noise_fast(new NOISE(this, emu));
47         cpu = new I8080(this, emu);
48         pio = new I8155(this, emu);
49         io = new IO(this, emu);
50         pcm = new PCM1BIT(this, emu);
51         rtc = new UPD1990A(this, emu);
52         
53         cmt = new CMT(this, emu);
54         keyboard = new KEYBOARD(this, emu);
55         lcd = new LCD(this, emu);
56         memory = new MEMORY(this, emu);
57         
58         // set contexts
59         event->set_context_cpu(cpu);
60         event->set_context_sound(pcm);
61         event->set_context_sound(drec);
62         event->set_context_sound(drec->get_context_noise_play());
63         event->set_context_sound(drec->get_context_noise_stop());
64         event->set_context_sound(drec->get_context_noise_fast());
65         
66         drec->set_context_ear(cpu, SIG_I8085_SID, 1);
67         cpu->set_context_sod(cmt, SIG_CMT_SOD, 1);
68         pio->set_context_port_a(rtc, SIG_UPD1990A_C0, 1, 0);
69         pio->set_context_port_a(rtc, SIG_UPD1990A_C1, 2, 0);
70         pio->set_context_port_a(rtc, SIG_UPD1990A_C2, 4, 0);
71         pio->set_context_port_a(rtc, SIG_UPD1990A_CLK, 8, 0);
72         pio->set_context_port_a(rtc, SIG_UPD1990A_DIN, 0x10, 0);
73         pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN_L, 0xff, 0);
74         pio->set_context_port_a(lcd, SIG_LCD_CHIPSEL_L, 0xff, 0);
75         pio->set_context_port_b(keyboard, SIG_KEYBOARD_COLUMN_H, 1, 0);
76         pio->set_context_port_b(lcd, SIG_LCD_CHIPSEL_H, 3, 0);
77         pio->set_context_port_b(pcm, SIG_PCM1BIT_MUTE, 0x20, 0);
78         pio->set_context_timer(pcm, SIG_PCM1BIT_SIGNAL, 1);
79         pio->set_constant_clock(CPU_CLOCKS);
80         rtc->set_context_dout(pio, SIG_I8155_PORT_C, 1);
81         rtc->set_context_tp(cpu, SIG_I8085_RST7, 1);
82         
83         memory->set_context_cmt(cmt);
84         memory->set_context_drec(drec);
85         memory->set_context_rtc(rtc);
86         
87         // cpu bus
88         cpu->set_context_mem(memory);
89         cpu->set_context_io(io);
90         cpu->set_context_intr(io);
91 #ifdef USE_DEBUGGER
92         cpu->set_context_debugger(new DEBUGGER(this, emu));
93 #endif
94         
95         // i/o bus
96         io->set_iomap_range_w(0x90, 0x9f, memory);
97         io->set_iomap_range_rw(0xa0, 0xaf, memory);
98         io->set_iomap_range_rw(0xb0, 0xbf, pio);
99         io->set_iomap_range_r(0xe0, 0xef, keyboard);
100         io->set_iomap_range_rw(0xf0, 0xff, lcd);
101         
102         // initialize all devices
103 #if defined(__GIT_REPO_VERSION)
104         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
105 #endif
106         for(DEVICE* device = first_device; device; device = device->next_device) {
107                 device->initialize();
108         }
109         decl_state();
110         rtc->write_signal(SIG_UPD1990A_STB, 0, 0);
111 }
112
113 VM::~VM()
114 {
115         // delete all devices
116         for(DEVICE* device = first_device; device;) {
117                 DEVICE *next_device = device->next_device;
118                 device->release();
119                 delete device;
120                 device = next_device;
121         }
122 }
123
124 DEVICE* VM::get_device(int id)
125 {
126         for(DEVICE* device = first_device; device; device = device->next_device) {
127                 if(device->this_device_id == id) {
128                         return device;
129                 }
130         }
131         return NULL;
132 }
133
134 // ----------------------------------------------------------------------------
135 // drive virtual machine
136 // ----------------------------------------------------------------------------
137
138 void VM::reset()
139 {
140         // reset all devices
141         for(DEVICE* device = first_device; device; device = device->next_device) {
142                 device->reset();
143         }
144 }
145
146 void VM::run()
147 {
148         event->drive();
149 }
150
151 // ----------------------------------------------------------------------------
152 // debugger
153 // ----------------------------------------------------------------------------
154
155 #ifdef USE_DEBUGGER
156 DEVICE *VM::get_cpu(int index)
157 {
158         if(index == 0) {
159                 return cpu;
160         }
161         return NULL;
162 }
163 #endif
164
165 // ----------------------------------------------------------------------------
166 // draw screen
167 // ----------------------------------------------------------------------------
168
169 void VM::draw_screen()
170 {
171         lcd->draw_screen();
172 }
173
174 // ----------------------------------------------------------------------------
175 // soud manager
176 // ----------------------------------------------------------------------------
177
178 void VM::initialize_sound(int rate, int samples)
179 {
180         // init sound manager
181         event->initialize_sound(rate, samples);
182         
183         // init sound gen
184         pcm->initialize_sound(rate, 8000);
185 }
186
187 uint16_t* VM::create_sound(int* extra_frames)
188 {
189         return event->create_sound(extra_frames);
190 }
191
192 int VM::get_sound_buffer_ptr()
193 {
194         return event->get_sound_buffer_ptr();
195 }
196
197 #ifdef USE_SOUND_VOLUME
198 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
199 {
200         if(ch == 0) {
201                 pcm->set_volume(0, decibel_l, decibel_r);
202         } else if(ch == 1) {
203                 drec->set_volume(0, decibel_l, decibel_r);
204         } else if(ch == 2) {
205                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
206                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
207                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
208         }
209 }
210 #endif
211
212 // ----------------------------------------------------------------------------
213 // notify key
214 // ----------------------------------------------------------------------------
215
216 void VM::key_down(int code, bool repeat)
217 {
218         keyboard->key_down(code);
219 }
220
221 void VM::key_up(int code)
222 {
223 }
224
225 bool VM::get_caps_locked()
226 {
227         return keyboard->get_caps_locked();
228 }
229
230 bool VM::get_kana_locked()
231 {
232         return keyboard->get_kana_locked();
233 }
234
235 // ----------------------------------------------------------------------------
236 // user interface
237 // ----------------------------------------------------------------------------
238
239 void VM::play_tape(int drv, const _TCHAR* file_path)
240 {
241         cmt->close_tape();
242         drec->play_tape(file_path);
243 //      drec->set_remote(true);
244 }
245
246 void VM::rec_tape(int drv, const _TCHAR* file_path)
247 {
248         emu->lock_vm();
249         drec->close_tape();
250         emu->unlock_vm();
251 //      drec->set_remote(false);
252         cmt->rec_tape(file_path);
253 }
254
255 void VM::close_tape(int drv)
256 {
257         emu->lock_vm();
258         drec->close_tape();
259         emu->unlock_vm();
260 //      drec->set_remote(false);
261         cmt->close_tape();
262 }
263
264 bool VM::is_tape_inserted(int drv)
265 {
266         return drec->is_tape_inserted() || cmt->is_tape_inserted();
267 }
268
269 bool VM::is_tape_playing(int drv)
270 {
271         if(drec->is_tape_inserted()) {
272                 return drec->is_tape_playing();
273         } else {
274                 return cmt->is_tape_playing();
275         }
276 }
277
278 bool VM::is_tape_recording(int drv)
279 {
280         if(drec->is_tape_inserted()) {
281                 return drec->is_tape_recording();
282         } else {
283                 return cmt->is_tape_recording();
284         }
285 }
286
287 int VM::get_tape_position(int drv)
288 {
289         if(drec->is_tape_inserted()) {
290                 return drec->get_tape_position();
291         } else {
292                 return cmt->get_tape_position();
293         }
294 }
295
296 const _TCHAR* VM::get_tape_message(int drv)
297 {
298         if(drec->is_tape_inserted()) {
299                 return drec->get_message();
300         } else {
301                 return NULL;
302         }
303 }
304
305 void VM::push_play(int drv)
306 {
307         if(drec->is_tape_inserted()) {
308                 drec->set_ff_rew(0);
309                 drec->set_remote(true);
310         }
311 }
312
313 void VM::push_stop(int drv)
314 {
315         if(drec->is_tape_inserted()) {
316                 drec->set_remote(false);
317         }
318 }
319
320 void VM::push_fast_forward(int drv)
321 {
322         if(drec->is_tape_inserted()) {
323                 drec->set_ff_rew(1);
324                 drec->set_remote(true);
325         }
326 }
327
328 void VM::push_fast_rewind(int drv)
329 {
330         if(drec->is_tape_inserted()) {
331                 drec->set_ff_rew(-1);
332                 drec->set_remote(true);
333         }
334 }
335
336 bool VM::is_frame_skippable()
337 {
338         return event->is_frame_skippable();
339 }
340
341 void VM::update_config()
342 {
343         for(DEVICE* device = first_device; device; device = device->next_device) {
344                 device->update_config();
345         }
346 }
347
348 #define STATE_VERSION   3
349
350 #include "../../statesub.h"
351 #include "../../qt/gui/csp_logger.h"
352 extern CSP_Logger DLL_PREFIX_I *csp_logger;
353
354 void VM::decl_state(void)
355 {
356 #if defined(_PC8201)
357         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PC_8201_HEAD")), csp_logger);
358 #elif defined(_PC8201A)
359         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PC_8201A_HEAD")), csp_logger);
360 #else
361         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PC_8201_SERIES_HEAD")), csp_logger);
362 #endif
363
364         for(DEVICE* device = first_device; device; device = device->next_device) {
365                 device->decl_state();
366         }
367 }
368
369 void VM::save_state(FILEIO* state_fio)
370 {
371         //state_fio->FputUint32(STATE_VERSION);
372         
373         if(state_entry != NULL) {
374                 state_entry->save_state(state_fio);
375         }
376         for(DEVICE* device = first_device; device; device = device->next_device) {
377                 device->save_state(state_fio);
378         }
379 }
380
381 bool VM::load_state(FILEIO* state_fio)
382 {
383         //if(state_fio->FgetUint32() != STATE_VERSION) {
384         //      return false;
385         //}
386         bool mb = false;
387         if(state_entry != NULL) {
388                 mb = state_entry->load_state(state_fio);
389         }
390         if(!mb) {
391                 emu->out_debug_log("INFO: HEADER DATA ERROR");
392                 return false;
393         }
394         for(DEVICE* device = first_device; device; device = device->next_device) {
395                 if(!device->load_state(state_fio)) {
396                         return false;
397                 }
398         }
399         return true;
400 }
401