OSDN Git Service

[General] Merge Updtream 2020-08-10.
[csp-qt/common_source_project-fm7.git] / source / src / vm / ex80 / ex80.cpp
1 /*
2         TOSHIBA EX-80 Emulator 'eEX-80'
3
4         Author : Takeda.Toshiya
5         Date   : 2015.12.10-
6
7         [ virtual machine ]
8 */
9
10 #include "ex80.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../i8080.h"
16 #include "../i8251.h"
17 #include "../i8255.h"
18 #include "../io.h"
19 #include "../pcm1bit.h"
20
21 #ifdef USE_DEBUGGER
22 #include "../debugger.h"
23 #endif
24
25 #include "./cmt.h"
26 #include "./display.h"
27 #include "./keyboard.h"
28 #include "./memory.h"
29
30 using EX80::CMT;
31 using EX80::DISPLAY;
32 using EX80::KEYBOARD;
33 using EX80::MEMORY;
34
35 // ----------------------------------------------------------------------------
36 // initialize
37 // ----------------------------------------------------------------------------
38
39 VM::VM(EMU_TEMPLATE* parent_emu) : VM_TEMPLATE(parent_emu)
40 {
41         // create devices
42         first_device = last_device = NULL;
43         dummy = new DEVICE(this, emu);  // must be 1st device
44         event = new EVENT(this, emu);   // must be 2nd device
45         
46         sio = new I8251(this, emu);
47         pio = new I8255(this, emu);
48         io = new IO(this, emu);
49         pcm = new PCM1BIT(this, emu);
50 #ifdef USE_DEBUGGER
51 //      pcm->set_context_debugger(new DEBUGGER(this, emu));
52 #endif
53         cpu = new I8080(this, emu);
54         
55         cmt = new CMT(this, emu);
56         display = new DISPLAY(this, emu);
57         keyboard = new KEYBOARD(this, emu);
58         memory = new MEMORY(this, emu);
59         // Set names
60 #if defined(_USE_QT)
61         dummy->set_device_name(_T("1st Dummy"));
62         
63         pio->set_device_name(_T("i8255(SOUND/KEY/DISPLAY)"));
64         sio->set_device_name(_T("i8251(CMT)"));
65         pcm->set_device_name(_T("SOUND OUT"));
66 #endif
67         
68         // set contexts
69         event->set_context_cpu(cpu);
70         event->set_context_sound(pcm);
71         
72         sio->set_context_out(cmt, SIG_CMT_OUT);
73         pio->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x08, 0);
74         pio->set_context_port_c(keyboard, SIG_KEYBOARD_COLUMN, 0x70, 0);
75         pio->set_context_port_c(display, SIG_DISPLAY_DMA, 0x80, 0);
76         // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
77         //pcm->set_realtime_render(true);
78         
79         cmt->set_context_sio(sio);
80         display->set_context_cpu(cpu);
81         display->set_ram_ptr(memory->get_ram());
82         keyboard->set_context_pio(pio);
83         memory->set_context_cpu(cpu);
84         
85         // cpu bus
86         cpu->set_context_mem(memory);
87         cpu->set_context_io(io);
88         cpu->set_context_intr(dummy);
89 #ifdef USE_DEBUGGER
90         cpu->set_context_debugger(new DEBUGGER(this, emu));
91 #endif
92         
93         // io bus
94         io->set_iomap_range_rw(0xdc, 0xdd, sio);
95         io->set_iomap_range_rw(0xf8, 0xfb, pio);
96         
97         // initialize all devices
98 #if defined(__GIT_REPO_VERSION)
99         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
100 #endif
101         for(DEVICE* device = first_device; device; device = device->next_device) {
102                 device->initialize();
103         }
104 }
105
106 VM::~VM()
107 {
108         // delete all devices
109         for(DEVICE* device = first_device; device;) {
110                 DEVICE *next_device = device->next_device;
111                 device->release();
112                 delete device;
113                 device = next_device;
114         }
115 }
116
117 DEVICE* VM::get_device(int id)
118 {
119         for(DEVICE* device = first_device; device; device = device->next_device) {
120                 if(device->this_device_id == id) {
121                         return device;
122                 }
123         }
124         return NULL;
125 }
126
127 // ----------------------------------------------------------------------------
128 // drive virtual machine
129 // ----------------------------------------------------------------------------
130
131 void VM::reset()
132 {
133         // reset all devices
134         for(DEVICE* device = first_device; device; device = device->next_device) {
135                 device->reset();
136         }
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         display->draw_screen();
165 }
166
167 int VM::max_draw_ranges()
168 {
169         return (config.monitor_type == 0) ? 9 : 8;
170 }
171
172 // ----------------------------------------------------------------------------
173 // soud manager
174 // ----------------------------------------------------------------------------
175
176 void VM::initialize_sound(int rate, int samples)
177 {
178         // init sound manager
179         event->initialize_sound(rate, samples);
180         
181         // init sound gen
182         pcm->initialize_sound(rate, 8000);
183 }
184
185 uint16_t* VM::create_sound(int* extra_frames)
186 {
187         return event->create_sound(extra_frames);
188 }
189
190 int VM::get_sound_buffer_ptr()
191 {
192         return event->get_sound_buffer_ptr();
193 }
194
195 #ifdef USE_SOUND_VOLUME
196 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
197 {
198         if(ch == 0) {
199                 pcm->set_volume(0, decibel_l, decibel_r);
200         }
201 }
202 #endif
203
204 // ----------------------------------------------------------------------------
205 // user interface
206 // ----------------------------------------------------------------------------
207
208 void VM::load_binary(int drv, const _TCHAR* file_path)
209 {
210         if(drv == 0) {
211                 memory->load_binary(file_path);
212         }
213 }
214
215 void VM::save_binary(int drv, const _TCHAR* file_path)
216 {
217         if(drv == 0) {
218                 memory->save_binary(file_path);
219         }
220 }
221
222 void VM::play_tape(int drv, const _TCHAR* file_path)
223 {
224         cmt->play_tape(file_path);
225 }
226
227 void VM::rec_tape(int drv, const _TCHAR* file_path)
228 {
229         cmt->rec_tape(file_path);
230 }
231
232 void VM::close_tape(int drv)
233 {
234         cmt->close_tape();
235 }
236
237 bool VM::is_tape_inserted(int drv)
238 {
239         return cmt->is_tape_inserted();
240 }
241
242 bool VM::is_frame_skippable()
243 {
244         return event->is_frame_skippable();
245 }
246
247 void VM::update_config()
248 {
249         for(DEVICE* device = first_device; device; device = device->next_device) {
250                 device->update_config();
251         }
252 }
253
254 double VM::get_current_usec()
255 {
256         if(event == NULL) return 0.0;
257         return event->get_current_usec();
258 }
259
260 uint64_t VM::get_current_clock_uint64()
261 {
262                 if(event == NULL) return (uint64_t)0;
263                 return event->get_current_clock_uint64();
264 }
265
266 #define STATE_VERSION   2
267
268 bool VM::process_state(FILEIO* state_fio, bool loading)
269 {
270         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
271                 return false;
272         }
273         for(DEVICE* device = first_device; device; device = device->next_device) {
274                 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
275                 // const char *name = typeid(*device).name();
276                 //       But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
277                 const char *name = device->get_device_name();
278                 int len = (int)strlen(name);
279                 
280                 if(!state_fio->StateCheckInt32(len)) {
281                         if(loading) {
282                                 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
283                         }
284                         return false;
285                 }
286                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
287                         if(loading) {
288                                 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
289                         }
290                         return false;
291                 }
292                 if(!device->process_state(state_fio, loading)) {
293                         if(loading) {
294                                 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
295                         }
296                         return false;
297                 }
298         }
299         // Machine specified.
300         return true;
301 }
302