OSDN Git Service

[General] Merge Upstream 2017-03-07.
[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 "../not.h"
18 #include "../z80.h"
19 #include "../z80pio.h"
20
21 #ifdef USE_DEBUGGER
22 #include "../debugger.h"
23 #endif
24
25 #include "memory.h"
26
27 // ----------------------------------------------------------------------------
28 // initialize
29 // ----------------------------------------------------------------------------
30
31 VM::VM(EMU* parent_emu) : emu(parent_emu)
32 {
33         // create devices
34         first_device = last_device = NULL;
35         dummy = new DEVICE(this, emu);  // must be 1st device
36         event = new EVENT(this, emu);   // must be 2nd device
37         
38         drec = new DATAREC(this, emu);
39         io = new IO(this, emu);
40         not_ear = new NOT(this, emu);
41         cpu = new Z80(this, emu);
42         pio1 = new Z80PIO(this, emu);
43         pio1->set_device_name(_T("8255 PIO (LEDs/Keyboard/CMT)"));
44         pio2 = new Z80PIO(this, emu);
45         pio2->set_device_name(_T("8255 PIO (User)"));
46         
47         memory = new MEMORY(this, emu);
48         
49         // set contexts
50         event->set_context_cpu(cpu);
51         
52         // PIO1 PA0-7   -> 7seg-LED data
53         pio1->set_context_port_a(memory, SIG_MEMORY_PIO1_PA, 0xff, 0);
54         // PIO1 PB0-2   <- Keyboard data
55         // PIO1 PB3-5   -> 7seg-LED/Keyboard column
56         pio1->set_context_port_b(memory, SIG_MEMORY_PIO1_PB, 0x38, 0);
57         // PIO1 PB6     -> MIC
58         pio1->set_context_port_b(drec, SIG_DATAREC_MIC, 0x40, 0);
59         // PIO1 PB7     <- NOT <- EAR
60         drec->set_context_ear(not_ear, SIG_NOT_INPUT, 1);
61         not_ear->set_context_out(pio1, SIG_Z80PIO_PORT_B, 0x80);
62         
63         memory->set_context_cpu(cpu);
64         memory->set_context_drec(drec);
65         memory->set_context_pio1(pio1);
66         
67         // cpu bus
68         cpu->set_context_mem(memory);
69         cpu->set_context_io(io);
70         cpu->set_context_intr(pio1);
71         
72         // z80 family daisy chain
73         pio1->set_context_intr(cpu, 0);
74         pio1->set_context_child(pio2);
75         pio2->set_context_intr(cpu, 1);
76 #ifdef USE_DEBUGGER
77         cpu->set_context_debugger(new DEBUGGER(this, emu));
78 #endif
79         
80         // i/o bus
81         io->set_iomap_range_rw(0xd0, 0xd3, pio1);
82         io->set_iomap_range_rw(0xd4, 0xd7, pio2);
83         io->set_iomap_range_rw(0xd8, 0xdb, memory);
84         
85         // initialize all devices
86         for(DEVICE* device = first_device; device; device = device->next_device) {
87                 device->initialize();
88         }
89 }
90
91 VM::~VM()
92 {
93         // delete all devices
94         for(DEVICE* device = first_device; device;) {
95                 DEVICE *next_device = device->next_device;
96                 device->release();
97                 delete device;
98                 device = next_device;
99         }
100 }
101
102 DEVICE* VM::get_device(int id)
103 {
104         for(DEVICE* device = first_device; device; device = device->next_device) {
105                 if(device->this_device_id == id) {
106                         return device;
107                 }
108         }
109         return NULL;
110 }
111
112 // ----------------------------------------------------------------------------
113 // drive virtual machine
114 // ----------------------------------------------------------------------------
115
116 void VM::reset()
117 {
118         // reset all devices
119         for(DEVICE* device = first_device; device; device = device->next_device) {
120                 device->reset();
121         }
122 }
123
124 void VM::run()
125 {
126         event->drive();
127 }
128
129 double VM::get_frame_rate()
130 {
131         return event->get_frame_rate();
132 }
133
134 // ----------------------------------------------------------------------------
135 // debugger
136 // ----------------------------------------------------------------------------
137
138 #ifdef USE_DEBUGGER
139 DEVICE *VM::get_cpu(int index)
140 {
141         if(index == 0) {
142                 return cpu;
143         }
144         return NULL;
145 }
146 #endif
147
148 // ----------------------------------------------------------------------------
149 // draw screen
150 // ----------------------------------------------------------------------------
151
152 void VM::draw_screen()
153 {
154         memory->draw_screen();
155 }
156
157 // ----------------------------------------------------------------------------
158 // soud manager
159 // ----------------------------------------------------------------------------
160
161 void VM::initialize_sound(int rate, int samples)
162 {
163         // init sound manager
164         event->initialize_sound(rate, samples);
165 }
166
167 uint16_t* VM::create_sound(int* extra_frames)
168 {
169         return event->create_sound(extra_frames);
170 }
171
172 int VM::get_sound_buffer_ptr()
173 {
174         return event->get_sound_buffer_ptr();
175 }
176
177 // ----------------------------------------------------------------------------
178 // notify key
179 // ----------------------------------------------------------------------------
180
181 void VM::key_down(int code, bool repeat)
182 {
183         if(code == 0x97 && !repeat) {
184                 reset();
185         }
186 }
187
188 void VM::key_up(int code)
189 {
190 }
191
192 // ----------------------------------------------------------------------------
193 // user interface
194 // ----------------------------------------------------------------------------
195
196 void VM::play_tape(const _TCHAR* file_path)
197 {
198         drec->play_tape(file_path);
199         drec->set_remote(true);
200 }
201
202 void VM::rec_tape(const _TCHAR* file_path)
203 {
204         drec->rec_tape(file_path);
205         drec->set_remote(true);
206 }
207
208 void VM::close_tape()
209 {
210         emu->lock_vm();
211         drec->close_tape();
212         emu->unlock_vm();
213         drec->set_remote(false);
214 }
215
216 bool VM::is_tape_inserted()
217 {
218         return drec->is_tape_inserted();
219 }
220
221 bool VM::is_tape_playing()
222 {
223         return drec->is_tape_playing();
224 }
225
226 bool VM::is_tape_recording()
227 {
228         return drec->is_tape_recording();
229 }
230
231 int VM::get_tape_position()
232 {
233         return drec->get_tape_position();
234 }
235
236 void VM::load_binary(int drv, const _TCHAR* file_path)
237 {
238         if(drv == 0) {
239                 memory->load_ram(file_path);
240         }
241 }
242
243 void VM::save_binary(int drv, const _TCHAR* file_path)
244 {
245         if(drv == 0) {
246                 memory->save_ram(file_path);
247         }
248 }
249
250 bool VM::is_frame_skippable()
251 {
252         return event->is_frame_skippable();
253 }
254
255 void VM::update_config()
256 {
257         for(DEVICE* device = first_device; device; device = device->next_device) {
258                 device->update_config();
259         }
260 }
261
262 #define STATE_VERSION   1
263
264 void VM::save_state(FILEIO* state_fio)
265 {
266         state_fio->FputUint32(STATE_VERSION);
267         
268         for(DEVICE* device = first_device; device; device = device->next_device) {
269                 device->save_state(state_fio);
270         }
271 }
272
273 bool VM::load_state(FILEIO* state_fio)
274 {
275         if(state_fio->FgetUint32() != STATE_VERSION) {
276                 return false;
277         }
278         for(DEVICE* device = first_device; device; device = device->next_device) {
279                 if(!device->load_state(state_fio)) {
280                         return false;
281                 }
282         }
283         return true;
284 }
285