OSDN Git Service

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