OSDN Git Service

[General] Merge Updtream 2017-03-15 . Still not build all of VMs.
[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) : emu(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 }
98
99 VM::~VM()
100 {
101         // delete all devices
102         for(DEVICE* device = first_device; device;) {
103                 DEVICE *next_device = device->next_device;
104                 device->release();
105                 delete device;
106                 device = next_device;
107         }
108 }
109
110 DEVICE* VM::get_device(int id)
111 {
112         for(DEVICE* device = first_device; device; device = device->next_device) {
113                 if(device->this_device_id == id) {
114                         return device;
115                 }
116         }
117         return NULL;
118 }
119
120 // ----------------------------------------------------------------------------
121 // drive virtual machine
122 // ----------------------------------------------------------------------------
123
124 void VM::reset()
125 {
126         // reset all devices
127         for(DEVICE* device = first_device; device; device = device->next_device) {
128                 device->reset();
129         }
130 }
131
132 void VM::run()
133 {
134         event->drive();
135 }
136
137 double VM::get_frame_rate()
138 {
139         return event->get_frame_rate();
140 }
141
142 // ----------------------------------------------------------------------------
143 // debugger
144 // ----------------------------------------------------------------------------
145
146 #ifdef USE_DEBUGGER
147 DEVICE *VM::get_cpu(int index)
148 {
149         if(index == 0) {
150                 return cpu;
151         }
152         return NULL;
153 }
154 #endif
155
156 // ----------------------------------------------------------------------------
157 // draw screen
158 // ----------------------------------------------------------------------------
159
160 void VM::draw_screen()
161 {
162         memory->draw_screen();
163 }
164
165 // ----------------------------------------------------------------------------
166 // soud manager
167 // ----------------------------------------------------------------------------
168
169 void VM::initialize_sound(int rate, int samples)
170 {
171         // init sound manager
172         event->initialize_sound(rate, samples);
173 }
174
175 uint16_t* VM::create_sound(int* extra_frames)
176 {
177         return event->create_sound(extra_frames);
178 }
179
180 int VM::get_sound_buffer_ptr()
181 {
182         return event->get_sound_buffer_ptr();
183 }
184
185 #ifdef USE_SOUND_VOLUME
186 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
187 {
188         if(ch == 0) {
189                 drec->set_volume(0, decibel_l, decibel_r);
190         } else if(ch == 1) {
191                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
192                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
193                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
194         }
195 }
196 #endif
197
198 // ----------------------------------------------------------------------------
199 // notify key
200 // ----------------------------------------------------------------------------
201
202 void VM::key_down(int code, bool repeat)
203 {
204         if(code == 0x97 && !repeat) {
205                 reset();
206         }
207 }
208
209 void VM::key_up(int code)
210 {
211 }
212
213 // ----------------------------------------------------------------------------
214 // user interface
215 // ----------------------------------------------------------------------------
216
217 void VM::play_tape(const _TCHAR* file_path)
218 {
219         drec->play_tape(file_path);
220         drec->set_remote(true);
221 }
222
223 void VM::rec_tape(const _TCHAR* file_path)
224 {
225         drec->rec_tape(file_path);
226         drec->set_remote(true);
227 }
228
229 void VM::close_tape()
230 {
231         emu->lock_vm();
232         drec->close_tape();
233         emu->unlock_vm();
234         drec->set_remote(false);
235 }
236
237 bool VM::is_tape_inserted()
238 {
239         return drec->is_tape_inserted();
240 }
241
242 bool VM::is_tape_playing()
243 {
244         return drec->is_tape_playing();
245 }
246
247 bool VM::is_tape_recording()
248 {
249         return drec->is_tape_recording();
250 }
251
252 int VM::get_tape_position()
253 {
254         return drec->get_tape_position();
255 }
256
257 const _TCHAR* VM::get_tape_message()
258 {
259         return drec->get_message();
260 }
261
262 void VM::load_binary(int drv, const _TCHAR* file_path)
263 {
264         if(drv == 0) {
265                 memory->load_ram(file_path);
266         }
267 }
268
269 void VM::save_binary(int drv, const _TCHAR* file_path)
270 {
271         if(drv == 0) {
272                 memory->save_ram(file_path);
273         }
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   2
289
290 void VM::save_state(FILEIO* state_fio)
291 {
292         state_fio->FputUint32(STATE_VERSION);
293         
294         for(DEVICE* device = first_device; device; device = device->next_device) {
295                 device->save_state(state_fio);
296         }
297 }
298
299 bool VM::load_state(FILEIO* state_fio)
300 {
301         if(state_fio->FgetUint32() != STATE_VERSION) {
302                 return false;
303         }
304         for(DEVICE* device = first_device; device; device = device->next_device) {
305                 if(!device->load_state(state_fio)) {
306                         return false;
307                 }
308         }
309         return true;
310 }
311