OSDN Git Service

[BUILD] Set SOVERSION and GIT hash automatically.
[csp-qt/common_source_project-fm7.git] / source / src / vm / yalky / yalky.cpp
1 /*
2         Yuasa Kyouiku System YALKY Emulator 'eYALKY'
3
4         Author : Takeda.Toshiya
5         Date   : 2016.03.28-
6
7         [ virtual machine ]
8 */
9
10 #include "yalky.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 "../memory.h"
19 #include "../noise.h"
20
21 #ifdef USE_DEBUGGER
22 #include "../debugger.h"
23 #endif
24
25 #include "io.h"
26
27 // ----------------------------------------------------------------------------
28 // initialize
29 // ----------------------------------------------------------------------------
30
31 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
32 {
33         config.sound_play_tape = false;
34         config.wave_shaper[0] = false;
35         
36         // create devices
37         first_device = last_device = NULL;
38         dummy = new DEVICE(this, emu);  // must be 1st device
39         event = new EVENT(this, emu);   // must be 2nd device
40         dummy->set_device_name(_T("1st Dummy"));
41         
42         drec = new DATAREC(this, emu);
43         drec->set_context_noise_play(new NOISE(this, emu));
44         drec->set_context_noise_stop(new NOISE(this, emu));
45         drec->set_context_noise_fast(new NOISE(this, emu));
46         cpu = new I8080(this, emu);     // 8085
47         pio = new I8155(this, emu);     // 8156
48         memory = new MEMORY(this, emu);
49         
50         io = new IO(this, emu);
51         
52         // set contexts
53         event->set_context_cpu(cpu);
54         event->set_context_sound(drec);
55         event->set_context_sound(drec->get_context_noise_play());
56         event->set_context_sound(drec->get_context_noise_stop());
57         event->set_context_sound(drec->get_context_noise_fast());
58         
59         drec->set_context_ear(io, SIG_IO_DREC_EAR, 1);
60         cpu->set_context_sod(drec, SIG_DATAREC_MIC, 1);
61         pio->set_context_port_b(io, SIG_IO_PORT_B, 0xff, 0);
62         pio->set_context_port_c(io, SIG_IO_PORT_C, 0xff, 0);
63         pio->set_context_timer(cpu, SIG_I8085_RST7, 1);
64         pio->set_constant_clock(CPU_CLOCKS);    // from 8085 CLOCK OUT
65         
66         io->set_context_drec(drec);
67         io->set_context_cpu(cpu);
68         io->set_context_pio(pio);
69         io->set_vram_ptr(vram);
70         
71         // cpu bus
72         cpu->set_context_mem(memory);
73         cpu->set_context_io(io);
74         cpu->set_context_intr(io);
75 #ifdef USE_DEBUGGER
76         cpu->set_context_debugger(new DEBUGGER(this, emu));
77 #endif
78         
79         // memory bus
80         memset(rom, 0xff, sizeof(rom));
81         
82         memory->read_bios(_T("BIOS.ROM"), rom, sizeof(rom));
83         
84         memory->set_memory_r(0x0000, 0x1fff, rom);
85         memory->set_memory_rw(0x4000, 0x43ff, vram);
86         memory->set_memory_rw(0x6000, 0x60ff, ram);     // 8156
87         
88         // initialize all devices
89 #if defined(__GIT_REPO_VERSION)
90         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
91 #endif
92         for(DEVICE* device = first_device; device; device = device->next_device) {
93                 device->initialize();
94         }
95         decl_state();
96 }
97
98 VM::~VM()
99 {
100         // delete all devices
101         for(DEVICE* device = first_device; device;) {
102                 DEVICE *next_device = device->next_device;
103                 device->release();
104                 delete device;
105                 device = next_device;
106         }
107 }
108
109 DEVICE* VM::get_device(int id)
110 {
111         for(DEVICE* device = first_device; device; device = device->next_device) {
112                 if(device->this_device_id == id) {
113                         return device;
114                 }
115         }
116         return NULL;
117 }
118
119 // ----------------------------------------------------------------------------
120 // drive virtual machine
121 // ----------------------------------------------------------------------------
122
123 void VM::reset()
124 {
125         // reset all devices
126         for(DEVICE* device = first_device; device; device = device->next_device) {
127                 device->reset();
128         }
129         pio->write_signal(SIG_I8155_PORT_A, 0xff, 0x80); // PA7=1
130         memset(ram, 0, sizeof(ram));
131         memset(vram, 0, sizeof(vram));
132 }
133
134 void VM::special_reset()
135 {
136         cpu->reset();
137 }
138
139 void VM::run()
140 {
141         event->drive();
142 }
143
144 // ----------------------------------------------------------------------------
145 // debugger
146 // ----------------------------------------------------------------------------
147
148 #ifdef USE_DEBUGGER
149 DEVICE *VM::get_cpu(int index)
150 {
151         if(index == 0) {
152                 return cpu;
153         }
154         return NULL;
155 }
156 #endif
157
158 // ----------------------------------------------------------------------------
159 // draw screen
160 // ----------------------------------------------------------------------------
161
162 void VM::draw_screen()
163 {
164         io->draw_screen();
165 }
166
167 // ----------------------------------------------------------------------------
168 // soud manager
169 // ----------------------------------------------------------------------------
170
171 void VM::initialize_sound(int rate, int samples)
172 {
173         // init sound manager
174         event->initialize_sound(rate, samples);
175 }
176
177 uint16_t* VM::create_sound(int* extra_frames)
178 {
179         return event->create_sound(extra_frames);
180 }
181
182 int VM::get_sound_buffer_ptr()
183 {
184         return event->get_sound_buffer_ptr();
185 }
186
187 #ifdef USE_SOUND_VOLUME
188 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
189 {
190         if(ch == 0) {
191                 drec->set_volume(0, decibel_l, decibel_r);
192         } else if(ch == 1) {
193                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
194                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
195                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
196         }
197 }
198 #endif
199
200 // ----------------------------------------------------------------------------
201 // user interface
202 // ----------------------------------------------------------------------------
203
204 void VM::play_tape(int drv, const _TCHAR* file_path)
205 {
206         if(drec->play_tape(file_path)) {
207                 drec->set_remote(true);
208                 io->open_tape();
209         }
210 }
211
212 void VM::rec_tape(int drv, const _TCHAR* file_path)
213 {
214         if(drec->rec_tape(file_path)) {
215                 drec->set_remote(true);
216                 io->open_tape();
217         }
218 }
219
220 void VM::close_tape(int drv)
221 {
222         emu->lock_vm();
223         drec->close_tape();
224         emu->unlock_vm();
225         drec->set_remote(false);
226 }
227
228 bool VM::is_tape_inserted(int drv)
229 {
230         return drec->is_tape_inserted();
231 }
232
233 bool VM::is_tape_playing(int drv)
234 {
235         return drec->is_tape_playing();
236 }
237
238 bool VM::is_tape_recording(int drv)
239 {
240         return drec->is_tape_recording();
241 }
242
243 int VM::get_tape_position(int drv)
244 {
245         return drec->get_tape_position();
246 }
247
248 const _TCHAR* VM::get_tape_message(int drv)
249 {
250         return drec->get_message();
251 }
252
253 void VM::push_play(int drv)
254 {
255         drec->set_ff_rew(0);
256         drec->set_remote(true);
257 }
258
259 void VM::push_stop(int drv)
260 {
261         drec->set_remote(false);
262 }
263
264 void VM::push_fast_forward(int drv)
265 {
266         drec->set_ff_rew(1);
267         drec->set_remote(true);
268 }
269
270 void VM::push_fast_rewind(int drv)
271 {
272         drec->set_ff_rew(-1);
273         drec->set_remote(true);
274 }
275
276 bool VM::is_frame_skippable()
277 {
278         return event->is_frame_skippable();
279 }
280
281 void VM::update_config()
282 {
283         for(DEVICE* device = first_device; device; device = device->next_device) {
284                 device->update_config();
285         }
286 }
287
288 #define STATE_VERSION   4
289
290 #include "../../statesub.h"
291 #include "../../qt/gui/csp_logger.h"
292 extern CSP_Logger DLL_PREFIX_I *csp_logger;
293
294 void VM::decl_state(void)
295 {
296
297         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::YALKY_HEAD")), csp_logger);
298         DECL_STATE_ENTRY_1D_ARRAY(ram, sizeof(ram));
299         DECL_STATE_ENTRY_1D_ARRAY(vram, sizeof(vram));
300         for(DEVICE* device = first_device; device; device = device->next_device) {
301                 device->decl_state();
302         }
303 }
304
305 void VM::save_state(FILEIO* state_fio)
306 {
307         //state_fio->FputUint32(STATE_VERSION);
308         
309         if(state_entry != NULL) {
310                 state_entry->save_state(state_fio);
311         }
312         for(DEVICE* device = first_device; device; device = device->next_device) {
313                 device->save_state(state_fio);
314         }
315         //state_fio->Fwrite(ram, sizeof(ram), 1);
316         //state_fio->Fwrite(vram, sizeof(vram), 1);
317 }
318
319 bool VM::load_state(FILEIO* state_fio)
320 {
321         //if(state_fio->FgetUint32() != STATE_VERSION) {
322         //      return false;
323         //}
324         bool mb = false;
325         if(state_entry != NULL) {
326                 mb = state_entry->load_state(state_fio);
327         }
328         if(!mb) {
329                 emu->out_debug_log("INFO: HEADER DATA ERROR");
330                 return false;
331         }
332         for(DEVICE* device = first_device; device; device = device->next_device) {
333                 if(!device->load_state(state_fio)) {
334                         return false;
335                 }
336         }
337         //state_fio->Fread(ram, sizeof(ram), 1);
338         //state_fio->Fread(vram, sizeof(vram), 1);
339         return true;
340 }
341