OSDN Git Service

14daf698c8d8c07d1dae8133e7d003dc57f82e73
[csp-qt/common_source_project-fm7.git] / source / src / vm / hc20 / hc20.cpp
1 /*
2         EPSON HC-20 Emulator 'eHC-20'
3
4         Author : Takeda.Toshiya
5         Date   : 2011.05.23-
6
7         [ virtual machine ]
8 */
9
10 #include "hc20.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../beep.h"
16 #include "../disk.h"
17 #include "../hd146818p.h"
18 #include "../i8255.h"
19 //#include "../mc6800.h"
20 #include "../hd6301.h"
21 #include "../noise.h"
22 #include "../tf20.h"
23 #include "../upd765a.h"
24 #include "../z80.h"
25 #include "../z80sio.h"
26
27 #ifdef USE_DEBUGGER
28 #include "../debugger.h"
29 #endif
30
31 #include "memory.h"
32
33 // ----------------------------------------------------------------------------
34 // initialize
35 // ----------------------------------------------------------------------------
36
37 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
38 {
39         // create devices
40         first_device = last_device = NULL;
41         dummy = new DEVICE(this, emu);  // must be 1st device
42         event = new EVENT(this, emu);   // must be 2nd device
43         dummy->set_device_name(_T("1st Dummy"));
44         
45         beep = new BEEP(this, emu);
46         rtc = new HD146818P(this, emu);
47         //cpu = new MC6800(this, emu);
48         cpu = new HD6301(this, emu);
49         tf20 = new TF20(this, emu);
50         pio_tf20 = new I8255(this, emu);
51         fdc_tf20 = new UPD765A(this, emu);
52         fdc_tf20->set_context_noise_seek(new NOISE(this, emu));
53         fdc_tf20->set_context_noise_head_down(new NOISE(this, emu));
54         fdc_tf20->set_context_noise_head_up(new NOISE(this, emu));
55         cpu_tf20 = new Z80(this, emu);
56         sio_tf20 = new Z80SIO(this, emu);
57         pio_tf20->set_device_name(_T("TF20 PIO(i8255)"));
58         cpu_tf20->set_device_name(_T("TF20 CPU(Z80)"));
59         sio_tf20->set_device_name(_T("TF20 SIO(Z80 SIO)"));
60         fdc_tf20->set_device_name(_T("TF20 FDC(uPD765A)"));
61         memory = new MEMORY(this, emu);
62         // set contexts
63         event->set_context_cpu(cpu);
64         event->set_context_cpu(cpu_tf20, 4000000);
65         event->set_context_sound(beep);
66         event->set_context_sound(fdc_tf20->get_context_noise_seek());
67         event->set_context_sound(fdc_tf20->get_context_noise_head_down());
68         event->set_context_sound(fdc_tf20->get_context_noise_head_up());
69         
70 /*
71         memory:
72                 0002    in      ---     port1 (cpu)
73                 0003    in/out  ---     port2 (cpu)
74                 0006            ---     port3 (cpu)
75                 0007            ---     port4 (cpu)
76                 0020    out     bit0-7  key scan line
77                 0022    in      bit0-7  key scan result (lo)
78                 0026    out     bit0-2  selection of lcd driver (0,1-6)
79                                 bit3    output selection for lcd driver (0=data 1=command)
80                                 bit4    key input interrupt mask (0=Mask)
81                                 bit5    pout (serial control line)
82                                 bit6    shift/load select for rom cartridge (0=load 1=shift)
83                                 bit7    clock for rom cartridge
84                 0028    in      bit0-1  key scan result (hi)
85                                 bit6    power switch interrupt flag (0=active)
86                                 bit7    busy signal of lcd controller (0=busy)
87                 002a    out     bit0-7  output data to lcd controller
88                         in      ---     serial clock to lcd controller
89                 002b    in      ---     serial clock to lcd controller
90                 002c    in/out  ---     used for interrupt mask setting in sleep mode
91                 0030    in/out  ---     select expansion unit rom (bank1)
92                 0032    in/out  ---     select internal rom (bank0)
93                 0033    in/out  ---     select internal rom (bank0)
94                 003c    in      ---     XXX: unknown
95
96         port1:
97                 p10     in      dsr (RS-232C)
98                 p11     in      cts (RS-232C)
99                 p12     in      error status of slave mcu (P34)
100                 p13     in      external interrupt flag(0=active)
101                 p14     in      battery voltage interrupt flag (0=active)
102                 p15     in      key input inerrupt flag (0=active)
103                 p16     in      pin (serial control line)
104                 p17     in      counter status of microcassete / rom data / plug-in option
105
106         port 2:
107                 p20     in      barcode input signal (1=mark 0=space)
108                 p21     out     txd (RS-232C)
109                 p22     out     selection for CPU serial communication (0=slave 1=serial)
110 */      
111         cpu->set_context_port2(memory, SIG_MEMORY_PORT_2, 0xff, 0);
112         cpu->set_context_port3(memory, SIG_MEMORY_PORT_3, 0xff, 0);
113         cpu->set_context_port4(memory, SIG_MEMORY_PORT_4, 0xff, 0);
114         cpu->set_context_sio(memory, SIG_MEMORY_SIO_MAIN);
115         rtc->set_context_intr(memory, SIG_MEMORY_RTC_IRQ, 1);
116         
117         memory->set_context_beep(beep);
118         memory->set_context_cpu(cpu);
119         memory->set_context_rtc(rtc);
120         memory->set_context_sio_tf20(sio_tf20);
121         
122         // cpu bus
123         cpu->set_context_mem(memory);
124 #ifdef USE_DEBUGGER
125         cpu->set_context_debugger(new DEBUGGER(this, emu));
126 #endif
127         
128         // tf-20
129         tf20->set_context_cpu(cpu_tf20);
130         tf20->set_context_fdc(fdc_tf20);
131         tf20->set_context_pio(pio_tf20);
132         tf20->set_context_sio(sio_tf20);
133         cpu_tf20->set_context_mem(tf20);
134         cpu_tf20->set_context_io(tf20);
135         cpu_tf20->set_context_intr(tf20);
136 #ifdef USE_DEBUGGER
137         cpu_tf20->set_context_debugger(new DEBUGGER(this, emu));
138 #endif
139         fdc_tf20->set_context_irq(cpu_tf20, SIG_CPU_IRQ, 1);
140         sio_tf20->set_context_send(0, memory, SIG_MEMORY_SIO_TF20);
141         sio_tf20->set_tx_clock(0, 4915200 / 8); // 4.9152MHz / 8 (38.4kbps)
142         sio_tf20->set_rx_clock(0, 4915200 / 8); // baud-rate can be changed by jumper pin
143         sio_tf20->set_tx_clock(1, 4915200 / 8);
144         sio_tf20->set_rx_clock(1, 4915200 / 8);
145         
146         // initialize all devices
147 #if defined(__GIT_REPO_VERSION)
148         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
149 #endif
150         for(DEVICE* device = first_device; device; device = device->next_device) {
151                 device->initialize();
152         }
153         for(int i = 0; i < MAX_DRIVE; i++) {
154                 fdc_tf20->set_drive_type(i, DRIVE_TYPE_2D);
155         }
156 }
157
158 VM::~VM()
159 {
160         // delete all devices
161         for(DEVICE* device = first_device; device;) {
162                 DEVICE *next_device = device->next_device;
163                 device->release();
164                 delete device;
165                 device = next_device;
166         }
167 }
168
169 DEVICE* VM::get_device(int id)
170 {
171         for(DEVICE* device = first_device; device; device = device->next_device) {
172                 if(device->this_device_id == id) {
173                         return device;
174                 }
175         }
176         return NULL;
177 }
178
179 // ----------------------------------------------------------------------------
180 // drive virtual machine
181 // ----------------------------------------------------------------------------
182
183 void VM::reset()
184 {
185         // reset all devices
186         for(DEVICE* device = first_device; device; device = device->next_device) {
187                 device->reset();
188         }
189         cpu->write_signal(SIG_MC6801_PORT_1, 0x78, 0xff);
190         cpu->write_signal(SIG_MC6801_PORT_2, 0x9e, 0xff);
191 }
192
193 void VM::notify_power_off()
194 {
195 //      this->out_debug_log(T("--- POWER OFF ---\n"));
196         memory->notify_power_off();
197 }
198
199 void VM::run()
200 {
201         event->drive();
202 }
203
204 // ----------------------------------------------------------------------------
205 // debugger
206 // ----------------------------------------------------------------------------
207
208 #ifdef USE_DEBUGGER
209 DEVICE *VM::get_cpu(int index)
210 {
211         if(index == 0) {
212                 return cpu;
213         } else if(index == 1) {
214                 return cpu_tf20;
215         }
216         return NULL;
217 }
218 #endif
219
220 // ----------------------------------------------------------------------------
221 // draw screen
222 // ----------------------------------------------------------------------------
223
224 void VM::draw_screen()
225 {
226         memory->draw_screen();
227 }
228
229 // ----------------------------------------------------------------------------
230 // soud manager
231 // ----------------------------------------------------------------------------
232
233 void VM::initialize_sound(int rate, int samples)
234 {
235         // init sound manager
236         event->initialize_sound(rate, samples);
237         
238         // init sound gen
239         beep->initialize_sound(rate, 1000, 8000);
240 }
241
242 uint16_t* VM::create_sound(int* extra_frames)
243 {
244         return event->create_sound(extra_frames);
245 }
246
247 int VM::get_sound_buffer_ptr()
248 {
249         return event->get_sound_buffer_ptr();
250 }
251
252 #ifdef USE_SOUND_VOLUME
253 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
254 {
255         if(ch == 0) {
256                 beep->set_volume(0, decibel_l, decibel_r);
257         } else if(ch == 1) {
258                 fdc_tf20->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
259                 fdc_tf20->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
260                 fdc_tf20->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
261         }
262 }
263 #endif
264
265 // ----------------------------------------------------------------------------
266 // notify key
267 // ----------------------------------------------------------------------------
268
269 void VM::key_down(int code, bool repeat)
270 {
271         memory->key_down(code);
272 }
273
274 void VM::key_up(int code)
275 {
276         memory->key_up(code);
277 }
278
279 // ----------------------------------------------------------------------------
280 // user interface
281 // ----------------------------------------------------------------------------
282
283 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
284 {
285         fdc_tf20->open_disk(drv, file_path, bank);
286 }
287
288 void VM::close_floppy_disk(int drv)
289 {
290         fdc_tf20->close_disk(drv);
291 }
292
293 bool VM::is_floppy_disk_inserted(int drv)
294 {
295         return fdc_tf20->is_disk_inserted(drv);
296 }
297
298 void VM::is_floppy_disk_protected(int drv, bool value)
299 {
300         fdc_tf20->is_disk_protected(drv, value);
301 }
302
303 bool VM::is_floppy_disk_protected(int drv)
304 {
305         return fdc_tf20->is_disk_protected(drv);
306 }
307
308 uint32_t VM::is_floppy_disk_accessed()
309 {
310         return fdc_tf20->read_signal(0);
311 }
312
313 void VM::play_tape(int drv, const _TCHAR* file_path)
314 {
315         memory->play_tape(file_path);
316 }
317
318 void VM::rec_tape(int drv, const _TCHAR* file_path)
319 {
320         memory->rec_tape(file_path);
321 }
322
323 void VM::close_tape(int drv)
324 {
325         memory->close_tape();
326 }
327
328 bool VM::is_tape_inserted(int drv)
329 {
330         return memory->is_tape_inserted();
331 }
332
333 bool VM::is_frame_skippable()
334 {
335         return event->is_frame_skippable();
336 }
337
338 void VM::update_config()
339 {
340         for(DEVICE* device = first_device; device; device = device->next_device) {
341                 device->update_config();
342         }
343 }
344
345 #define STATE_VERSION   3
346
347 #include "../../statesub.h"
348 #include "../../qt/gui/csp_logger.h"
349 extern CSP_Logger DLL_PREFIX_I *csp_logger;
350
351 void VM::decl_state(void)
352 {
353         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::HC_20_HEAD")), csp_logger);
354
355         for(DEVICE* device = first_device; device; device = device->next_device) {
356                 device->decl_state();
357         }
358 }
359
360 void VM::save_state(FILEIO* state_fio)
361 {
362         //state_fio->FputUint32(STATE_VERSION);
363         
364         if(state_entry != NULL) {
365                 state_entry->save_state(state_fio);
366         }
367         for(DEVICE* device = first_device; device; device = device->next_device) {
368                 device->save_state(state_fio);
369         }
370 }
371
372 bool VM::load_state(FILEIO* state_fio)
373 {
374         //if(state_fio->FgetUint32() != STATE_VERSION) {
375         //      return false;
376         //}
377         bool mb = false;
378         if(state_entry != NULL) {
379                 mb = state_entry->load_state(state_fio);
380         }
381         if(!mb) {
382                 emu->out_debug_log("INFO: HEADER DATA ERROR");
383                 return false;
384         }
385         for(DEVICE* device = first_device; device; device = device->next_device) {
386                 if(!device->load_state(state_fio)) {
387                         return false;
388                 }
389         }
390         return true;
391 }
392