OSDN Git Service

a1c1a6e89dd2d1fe9ae3497e2e38b9aac21e23e3
[csp-qt/common_source_project-fm7.git] / source / src / vm / smb80te / smb80te.cpp
1 /*
2         SHARP SM-B-80TE Emulator 'eSM-B-80TE'
3
4         Author : Takeda.Toshiya
5         Date   : 2016.12.29-
6
7         [ virtual machine ]
8 */
9
10 #include "smb80te.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../datarec.h"
16 #include "../io.h"
17 #include "../noise.h"
18 #include "../not.h"
19 #include "../z80.h"
20 #include "../z80pio.h"
21
22 #ifdef USE_DEBUGGER
23 #include "../debugger.h"
24 #endif
25
26 #include "memory.h"
27
28 // ----------------------------------------------------------------------------
29 // initialize
30 // ----------------------------------------------------------------------------
31
32 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
33 {
34         // create devices
35         first_device = last_device = NULL;
36         dummy = new DEVICE(this, emu);  // must be 1st device
37         event = new EVENT(this, emu);   // must be 2nd device
38         
39         drec = new DATAREC(this, emu);
40         drec->set_context_noise_play(new NOISE(this, emu));
41         drec->set_context_noise_stop(new NOISE(this, emu));
42         drec->set_context_noise_fast(new NOISE(this, emu));
43         io = new IO(this, emu);
44         not_ear = new NOT(this, emu);
45         cpu = new Z80(this, emu);
46         pio1 = new Z80PIO(this, emu);
47         pio1->set_device_name(_T("8255 PIO (LEDs/Keyboard/CMT)"));
48         pio2 = new Z80PIO(this, emu);
49         pio2->set_device_name(_T("8255 PIO (User)"));
50         
51         memory = new MEMORY(this, emu);
52         
53         // set contexts
54         event->set_context_cpu(cpu);
55         event->set_context_sound(drec);
56         event->set_context_sound(drec->get_context_noise_play());
57         event->set_context_sound(drec->get_context_noise_stop());
58         event->set_context_sound(drec->get_context_noise_fast());
59         
60         // PIO1 PA0-7   -> 7seg-LED data
61         pio1->set_context_port_a(memory, SIG_MEMORY_PIO1_PA, 0xff, 0);
62         // PIO1 PB0-2   <- Keyboard data
63         // PIO1 PB3-5   -> 7seg-LED/Keyboard column
64         pio1->set_context_port_b(memory, SIG_MEMORY_PIO1_PB, 0x38, 0);
65         // PIO1 PB6     -> MIC
66         pio1->set_context_port_b(drec, SIG_DATAREC_MIC, 0x40, 0);
67         // PIO1 PB7     <- NOT <- EAR
68         drec->set_context_ear(not_ear, SIG_NOT_INPUT, 1);
69         not_ear->set_context_out(pio1, SIG_Z80PIO_PORT_B, 0x80);
70         
71         memory->set_context_cpu(cpu);
72         memory->set_context_drec(drec);
73         memory->set_context_pio1(pio1);
74         
75         // cpu bus
76         cpu->set_context_mem(memory);
77         cpu->set_context_io(io);
78         cpu->set_context_intr(pio1);
79         
80         // z80 family daisy chain
81         pio1->set_context_intr(cpu, 0);
82         pio1->set_context_child(pio2);
83         pio2->set_context_intr(cpu, 1);
84 #ifdef USE_DEBUGGER
85         cpu->set_context_debugger(new DEBUGGER(this, emu));
86 #endif
87         
88         // i/o bus
89         io->set_iomap_range_rw(0xd0, 0xd3, pio1);
90         io->set_iomap_range_rw(0xd4, 0xd7, pio2);
91         io->set_iomap_range_rw(0xd8, 0xdb, memory);
92         
93         // initialize all devices
94         for(DEVICE* device = first_device; device; device = device->next_device) {
95                 device->initialize();
96         }
97         decl_state();
98 }
99
100 VM::~VM()
101 {
102         // delete all devices
103         for(DEVICE* device = first_device; device;) {
104                 DEVICE *next_device = device->next_device;
105                 device->release();
106                 delete device;
107                 device = next_device;
108         }
109 }
110
111 DEVICE* VM::get_device(int id)
112 {
113         for(DEVICE* device = first_device; device; device = device->next_device) {
114                 if(device->this_device_id == id) {
115                         return device;
116                 }
117         }
118         return NULL;
119 }
120
121 // ----------------------------------------------------------------------------
122 // drive virtual machine
123 // ----------------------------------------------------------------------------
124
125 void VM::reset()
126 {
127         // reset all devices
128         for(DEVICE* device = first_device; device; device = device->next_device) {
129                 device->reset();
130         }
131 }
132
133 void VM::run()
134 {
135         event->drive();
136 }
137
138 double VM::get_frame_rate()
139 {
140         return event->get_frame_rate();
141 }
142
143 // ----------------------------------------------------------------------------
144 // debugger
145 // ----------------------------------------------------------------------------
146
147 #ifdef USE_DEBUGGER
148 DEVICE *VM::get_cpu(int index)
149 {
150         if(index == 0) {
151                 return cpu;
152         }
153         return NULL;
154 }
155 #endif
156
157 // ----------------------------------------------------------------------------
158 // draw screen
159 // ----------------------------------------------------------------------------
160
161 void VM::draw_screen()
162 {
163         memory->draw_screen();
164 }
165
166 // ----------------------------------------------------------------------------
167 // soud manager
168 // ----------------------------------------------------------------------------
169
170 void VM::initialize_sound(int rate, int samples)
171 {
172         // init sound manager
173         event->initialize_sound(rate, samples);
174 }
175
176 uint16_t* VM::create_sound(int* extra_frames)
177 {
178         return event->create_sound(extra_frames);
179 }
180
181 int VM::get_sound_buffer_ptr()
182 {
183         return event->get_sound_buffer_ptr();
184 }
185
186 #ifdef USE_SOUND_VOLUME
187 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
188 {
189         if(ch == 0) {
190                 drec->set_volume(0, decibel_l, decibel_r);
191         } else if(ch == 1) {
192                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
193                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
194                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
195         }
196 }
197 #endif
198
199 // ----------------------------------------------------------------------------
200 // notify key
201 // ----------------------------------------------------------------------------
202
203 void VM::key_down(int code, bool repeat)
204 {
205         if(code == 0x97 && !repeat) {
206                 reset();
207         }
208 }
209
210 void VM::key_up(int code)
211 {
212 }
213
214 // ----------------------------------------------------------------------------
215 // user interface
216 // ----------------------------------------------------------------------------
217
218 void VM::play_tape(int drv, const _TCHAR* file_path)
219 {
220         drec->play_tape(file_path);
221 //      drec->set_remote(true);
222 }
223
224 void VM::rec_tape(int drv, const _TCHAR* file_path)
225 {
226         drec->rec_tape(file_path);
227 //      drec->set_remote(true);
228 }
229
230 void VM::close_tape(int drv)
231 {
232         emu->lock_vm();
233         drec->close_tape();
234         emu->unlock_vm();
235 //      drec->set_remote(false);
236 }
237
238 bool VM::is_tape_inserted(int drv)
239 {
240         return drec->is_tape_inserted();
241 }
242
243 bool VM::is_tape_playing(int drv)
244 {
245         return drec->is_tape_playing();
246 }
247
248 bool VM::is_tape_recording(int drv)
249 {
250         return drec->is_tape_recording();
251 }
252
253 int VM::get_tape_position(int drv)
254 {
255         return drec->get_tape_position();
256 }
257
258 const _TCHAR* VM::get_tape_message(int drv)
259 {
260         return drec->get_message();
261 }
262
263 void VM::push_play(int drv)
264 {
265         drec->set_ff_rew(0);
266         drec->set_remote(true);
267 }
268
269 void VM::push_stop(int drv)
270 {
271         drec->set_remote(false);
272 }
273
274 void VM::push_fast_forward(int drv)
275 {
276         drec->set_ff_rew(1);
277         drec->set_remote(true);
278 }
279
280 void VM::push_fast_rewind(int drv)
281 {
282         drec->set_ff_rew(-1);
283         drec->set_remote(true);
284 }
285
286 void VM::load_binary(int drv, const _TCHAR* file_path)
287 {
288         if(drv == 0) {
289                 memory->load_ram(file_path);
290         }
291 }
292
293 void VM::save_binary(int drv, const _TCHAR* file_path)
294 {
295         if(drv == 0) {
296                 memory->save_ram(file_path);
297         }
298 }
299
300 bool VM::is_frame_skippable()
301 {
302         return event->is_frame_skippable();
303 }
304
305 void VM::update_config()
306 {
307         for(DEVICE* device = first_device; device; device = device->next_device) {
308                 device->update_config();
309         }
310 }
311
312 #define STATE_VERSION   3
313
314 #include "../../statesub.h"
315 #include "../../qt/gui/csp_logger.h"
316 extern CSP_Logger DLL_PREFIX_I *csp_logger;
317
318 void VM::decl_state(void)
319 {
320         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::SM_B_80TE_HEAD")), csp_logger);
321         for(DEVICE* device = first_device; device; device = device->next_device) {
322                 device->decl_state();
323         }
324 }
325
326 void VM::save_state(FILEIO* state_fio)
327 {
328         //state_fio->FputUint32(STATE_VERSION);
329         
330         if(state_entry != NULL) {
331                 state_entry->save_state(state_fio);
332         }
333         for(DEVICE* device = first_device; device; device = device->next_device) {
334                 device->save_state(state_fio);
335         }
336 }
337
338 bool VM::load_state(FILEIO* state_fio)
339 {
340         //if(state_fio->FgetUint32() != STATE_VERSION) {
341         //      return false;
342         //}
343         bool mb = false;
344         if(state_entry != NULL) {
345                 mb = state_entry->load_state(state_fio);
346         }
347         if(!mb) {
348                 emu->out_debug_log("INFO: HEADER DATA ERROR");
349                 return false;
350         }
351         for(DEVICE* device = first_device; device; device = device->next_device) {
352                 if(!device->load_state(state_fio)) {
353                         return false;
354                 }
355         }
356         return true;
357 }
358