OSDN Git Service

[VM][Qt] Add SANYOPHC-20, PHC-25 and SEIKO MAP-1010 .
[csp-qt/common_source_project-fm7.git] / source / src / vm / phc25 / phc25.cpp
1 /*
2         SANYO PHC-25 Emulator 'ePHC-25'
3         SEIKO MAP-1010 Emulator 'eMAP-1010'
4
5         Author : Takeda.Toshiya
6         Date   : 2010.08.03-
7
8         [ virtual machine ]
9 */
10
11 #include "phc25.h"
12 #include "../../emu.h"
13 #include "../device.h"
14 #include "../event.h"
15
16 #include "../datarec.h"
17 #include "../io.h"
18 #include "../mc6847.h"
19 #include "../not.h"
20 #include "../ym2203.h"
21 #include "../z80.h"
22
23 #ifdef USE_DEBUGGER
24 #include "../debugger.h"
25 #endif
26
27 #include "joystick.h"
28 #include "keyboard.h"
29 #include "memory.h"
30 #include "system.h"
31
32 // ----------------------------------------------------------------------------
33 // initialize
34 // ----------------------------------------------------------------------------
35
36 VM::VM(EMU* parent_emu) : emu(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         io = new IO(this, emu);
45         vdp = new MC6847(this, emu);
46         g_not = new NOT(this, emu);
47         psg = new YM2203(this, emu);
48         cpu = new Z80(this, emu);
49         
50         joystick = new JOYSTICK(this, emu);
51         keyboard = new KEYBOARD(this, emu);
52         memory = new MEMORY(this, emu);
53         system = new SYSTEM(this, emu);
54         
55         // set contexts
56         event->set_context_cpu(cpu);
57         event->set_context_sound(psg);
58         event->set_context_sound(drec);
59         
60         vdp->load_font_image(emu->bios_path(_T("FONT.ROM")));
61         vdp->set_vram_ptr(memory->get_vram(), 0x1800);
62 //      vdp->set_context_cpu(cpu);
63         vdp->set_context_vsync(g_not, SIG_NOT_INPUT, 1);
64         g_not->set_context_out(cpu, SIG_CPU_IRQ, 1);
65         
66         vdp->set_context_vsync(system, SIG_SYSTEM_PORT, 0x10);
67         drec->set_context_ear(system, SIG_SYSTEM_PORT, 0x20);
68         // bit6: printer busy
69         vdp->set_context_hsync(system, SIG_SYSTEM_PORT, 0x80);
70         
71         joystick->set_context_psg(psg);
72 #ifdef _MAP1010
73         memory->set_context_keyboard(keyboard);
74 #endif
75         system->set_context_drec(drec);
76         system->set_context_vdp(vdp);
77         
78         // cpu bus
79         cpu->set_context_mem(memory);
80         cpu->set_context_io(io);
81         cpu->set_context_intr(dummy);
82 #ifdef USE_DEBUGGER
83         cpu->set_context_debugger(new DEBUGGER(this, emu));
84 #endif
85         
86         // i/o bus
87         io->set_iomap_single_rw(0x40, system);
88 #ifndef _MAP1010
89         io->set_iomap_range_r(0x80, 0x88, keyboard);
90 #endif
91         io->set_iomap_alias_w(0xc0, psg, 1);    // PSG data
92         io->set_iomap_alias_w(0xc1, psg, 0);    // PSG ch
93 //      io->set_iomap_alias_r(0xc0, psg, 1);
94         io->set_iomap_alias_r(0xc1, psg, 1);    // PSG data
95         
96         // initialize all devices
97         for(DEVICE* device = first_device; device; device = device->next_device) {
98                 device->initialize();
99         }
100 }
101
102 VM::~VM()
103 {
104         // delete all devices
105         for(DEVICE* device = first_device; device;) {
106                 DEVICE *next_device = device->next_device;
107                 device->release();
108                 delete device;
109                 device = next_device;
110         }
111 }
112
113 DEVICE* VM::get_device(int id)
114 {
115         for(DEVICE* device = first_device; device; device = device->next_device) {
116                 if(device->this_device_id == id) {
117                         return device;
118                 }
119         }
120         return NULL;
121 }
122
123 // ----------------------------------------------------------------------------
124 // drive virtual machine
125 // ----------------------------------------------------------------------------
126
127 void VM::reset()
128 {
129         // reset all devices
130         for(DEVICE* device = first_device; device; device = device->next_device) {
131                 device->reset();
132         }
133 }
134
135 void VM::run()
136 {
137         event->drive();
138 }
139
140 // ----------------------------------------------------------------------------
141 // debugger
142 // ----------------------------------------------------------------------------
143
144 #ifdef USE_DEBUGGER
145 DEVICE *VM::get_cpu(int index)
146 {
147         if(index == 0) {
148                 return cpu;
149         }
150         return NULL;
151 }
152 #endif
153
154 // ----------------------------------------------------------------------------
155 // draw screen
156 // ----------------------------------------------------------------------------
157
158 void VM::draw_screen()
159 {
160         vdp->draw_screen();
161 }
162
163 // ----------------------------------------------------------------------------
164 // soud manager
165 // ----------------------------------------------------------------------------
166
167 void VM::initialize_sound(int rate, int samples)
168 {
169         // init sound manager
170         event->initialize_sound(rate, samples);
171         
172         // init sound gen
173         psg->init(rate, 1996750, samples, 0, 0);
174 }
175
176 uint16* VM::create_sound(int* extra_frames)
177 {
178         return event->create_sound(extra_frames);
179 }
180
181 int VM::sound_buffer_ptr()
182 {
183         return event->sound_buffer_ptr();
184 }
185
186 // ----------------------------------------------------------------------------
187 // user interface
188 // ----------------------------------------------------------------------------
189
190 void VM::play_tape(const _TCHAR* file_path)
191 {
192         drec->play_tape(file_path);
193 }
194
195 void VM::rec_tape(const _TCHAR* file_path)
196 {
197         drec->rec_tape(file_path);
198 }
199
200 void VM::close_tape()
201 {
202         drec->close_tape();
203 }
204
205 bool VM::tape_inserted()
206 {
207         return drec->tape_inserted();
208 }
209
210 bool VM::now_skip()
211 {
212         return event->now_skip();
213 }
214
215 void VM::update_config()
216 {
217         for(DEVICE* device = first_device; device; device = device->next_device) {
218                 device->update_config();
219         }
220 }
221
222 #define STATE_VERSION   1
223
224 void VM::save_state(FILEIO* state_fio)
225 {
226         state_fio->FputUint32(STATE_VERSION);
227         
228         for(DEVICE* device = first_device; device; device = device->next_device) {
229                 device->save_state(state_fio);
230         }
231 }
232
233 bool VM::load_state(FILEIO* state_fio)
234 {
235         if(state_fio->FgetUint32() != STATE_VERSION) {
236                 return false;
237         }
238         for(DEVICE* device = first_device; device; device = device->next_device) {
239                 if(!device->load_state(state_fio)) {
240                         return false;
241                 }
242         }
243         return true;
244 }
245