OSDN Git Service

[BUILD] Set SOVERSION and GIT hash automatically.
[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 // ----------------------------------------------------------------------------
31 // initialize
32 // ----------------------------------------------------------------------------
33
34 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
35 {
36         // create devices
37         first_device = last_device = NULL;
38         dummy = new DEVICE(this, emu);  // must be 1st device
39         event = new EVENT(this, emu);   // must be 2nd device
40         
41         sio = new I8251(this, emu);
42         pio = new I8255(this, emu);
43         io = new IO(this, emu);
44         pcm = new PCM1BIT(this, emu);
45         cpu = new I8080(this, emu);
46         
47         cmt = new CMT(this, emu);
48         display = new DISPLAY(this, emu);
49         keyboard = new KEYBOARD(this, emu);
50         memory = new MEMORY(this, emu);
51         // Set names
52 #if defined(_USE_QT)
53         dummy->set_device_name(_T("1st Dummy"));
54         
55         pio->set_device_name(_T("i8255(SOUND/KEY/DISPLAY)"));
56         sio->set_device_name(_T("i8251(CMT)"));
57         pcm->set_device_name(_T("SOUND OUT"));
58 #endif
59         
60         // set contexts
61         event->set_context_cpu(cpu);
62         event->set_context_sound(pcm);
63         
64         sio->set_context_out(cmt, SIG_CMT_OUT);
65         pio->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x08, 0);
66         pio->set_context_port_c(keyboard, SIG_KEYBOARD_COLUMN, 0x70, 0);
67         pio->set_context_port_c(display, SIG_DISPLAY_DMA, 0x80, 0);
68         // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
69         //pcm->set_realtime_render(true);
70         
71         cmt->set_context_sio(sio);
72         display->set_context_cpu(cpu);
73         display->set_ram_ptr(memory->get_ram());
74         keyboard->set_context_pio(pio);
75         memory->set_context_cpu(cpu);
76         
77         // cpu bus
78         cpu->set_context_mem(memory);
79         cpu->set_context_io(io);
80         cpu->set_context_intr(dummy);
81 #ifdef USE_DEBUGGER
82         cpu->set_context_debugger(new DEBUGGER(this, emu));
83 #endif
84         
85         // io bus
86         io->set_iomap_range_rw(0xdc, 0xdd, sio);
87         io->set_iomap_range_rw(0xf8, 0xfb, pio);
88         
89         // initialize all devices
90 #if defined(__GIT_REPO_VERSION)
91         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
92 #endif
93         for(DEVICE* device = first_device; device; device = device->next_device) {
94                 device->initialize();
95         }
96         decl_state();
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 // ----------------------------------------------------------------------------
138 // debugger
139 // ----------------------------------------------------------------------------
140
141 #ifdef USE_DEBUGGER
142 DEVICE *VM::get_cpu(int index)
143 {
144         if(index == 0) {
145                 return cpu;
146         }
147         return NULL;
148 }
149 #endif
150
151 // ----------------------------------------------------------------------------
152 // draw screen
153 // ----------------------------------------------------------------------------
154
155 void VM::draw_screen()
156 {
157         display->draw_screen();
158 }
159
160 int VM::max_draw_ranges()
161 {
162         return (config.monitor_type == 0) ? 9 : 8;
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         // init sound gen
175         pcm->initialize_sound(rate, 8000);
176 }
177
178 uint16_t* VM::create_sound(int* extra_frames)
179 {
180         return event->create_sound(extra_frames);
181 }
182
183 int VM::get_sound_buffer_ptr()
184 {
185         return event->get_sound_buffer_ptr();
186 }
187
188 #ifdef USE_SOUND_VOLUME
189 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
190 {
191         if(ch == 0) {
192                 pcm->set_volume(0, decibel_l, decibel_r);
193         }
194 }
195 #endif
196
197 // ----------------------------------------------------------------------------
198 // user interface
199 // ----------------------------------------------------------------------------
200
201 void VM::load_binary(int drv, const _TCHAR* file_path)
202 {
203         if(drv == 0) {
204                 memory->load_binary(file_path);
205         }
206 }
207
208 void VM::save_binary(int drv, const _TCHAR* file_path)
209 {
210         if(drv == 0) {
211                 memory->save_binary(file_path);
212         }
213 }
214
215 void VM::play_tape(int drv, const _TCHAR* file_path)
216 {
217         cmt->play_tape(file_path);
218 }
219
220 void VM::rec_tape(int drv, const _TCHAR* file_path)
221 {
222         cmt->rec_tape(file_path);
223 }
224
225 void VM::close_tape(int drv)
226 {
227         cmt->close_tape();
228 }
229
230 bool VM::is_tape_inserted(int drv)
231 {
232         return cmt->is_tape_inserted();
233 }
234
235 bool VM::is_frame_skippable()
236 {
237         return event->is_frame_skippable();
238 }
239
240 void VM::update_config()
241 {
242         for(DEVICE* device = first_device; device; device = device->next_device) {
243                 device->update_config();
244         }
245 }
246
247 #define STATE_VERSION   2
248
249 #include "../../statesub.h"
250 #include "../../qt/gui/csp_logger.h"
251 extern CSP_Logger DLL_PREFIX_I *csp_logger;
252
253 void VM::decl_state(void)
254 {
255         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::EX80_HEAD")), csp_logger);
256         for(DEVICE* device = first_device; device; device = device->next_device) {
257                 device->decl_state();
258         }
259 }
260
261 void VM::save_state(FILEIO* state_fio)
262 {
263         //state_fio->FputUint32(STATE_VERSION);
264         
265         if(state_entry != NULL) {
266                 state_entry->save_state(state_fio);
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         bool mb = false;
279         if(state_entry != NULL) {
280                 mb = state_entry->load_state(state_fio);
281         }
282         if(!mb) {
283                 emu->out_debug_log("INFO: HEADER DATA ERROR");
284                 return false;
285         }
286         for(DEVICE* device = first_device; device; device = device->next_device) {
287                 if(!device->load_state(state_fio)) {
288                         return false;
289                 }
290         }
291         return true;
292 }
293