OSDN Git Service

[VM][General] Merge Upstream 2017-12-15.
[csp-qt/common_source_project-fm7.git] / source / src / vm / babbage2nd / babbage2nd.cpp
1 /*
2         Gijutsu-Hyoron-Sha Babbage-2nd Emulator 'eBabbage-2nd'
3
4         Author : Takeda.Toshiya
5         Date   : 2009.12.26 -
6
7         [ virtual machine ]
8 */
9
10 #include "babbage2nd.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../io.h"
16 #include "../memory.h"
17 #include "../z80.h"
18 #include "../z80ctc.h"
19 #include "../z80pio.h"
20
21 #ifdef USE_DEBUGGER
22 #include "../debugger.h"
23 #endif
24
25 #include "display.h"
26 #include "keyboard.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         io = new IO(this, emu);
40         memory = new MEMORY(this, emu);
41         cpu = new Z80(this, emu);
42         ctc = new Z80CTC(this, emu);
43         pio1 = new Z80PIO(this, emu);
44         pio1->set_device_name(_T("Z80 PIO (LEDs)"));
45         pio2 = new Z80PIO(this, emu);
46         pio2->set_device_name(_T("Z80 PIO (7-Seg/Keyboard)"));
47         
48         display = new DISPLAY(this, emu);
49         keyboard = new KEYBOARD(this, emu);
50
51         // Set names
52 #if defined(_USE_QT)
53         dummy->set_device_name(_T("1st Dummy"));
54         pio1->set_device_name(_T("Z80 PIO(LEDs)"));
55         pio2->set_device_name(_T("Z80 PIO(7SEG/KEYBOARD)"));
56 #endif
57         // set contexts
58         event->set_context_cpu(cpu);
59         
60         pio2->set_context_port_b(display, SIG_DISPLAY_7SEG_LED, 0xff, 0);
61         keyboard->set_context_pio(pio2);
62         // p.145, fig.3-4
63         ctc->set_context_zc2(ctc, SIG_Z80CTC_TRIG_1, 1);
64         ctc->set_context_zc1(ctc, SIG_Z80CTC_TRIG_0, 1);
65         // p.114, fig.2-52
66         pio1->set_context_port_b(display, SIG_DISPLAY_8BIT_LED, 0xff, 0);
67         //pio1->set_context_port_b(pio1, SIG_Z80PIO_PORT_A, 0xff, 0);
68         
69         // cpu bus
70         cpu->set_context_mem(memory);
71         cpu->set_context_io(io);
72         cpu->set_context_intr(ctc);
73 #ifdef USE_DEBUGGER
74         cpu->set_context_debugger(new DEBUGGER(this, emu));
75 #endif
76         
77         // z80 family daisy chain
78         ctc->set_context_intr(cpu, 0);
79         ctc->set_context_child(pio1);
80         pio1->set_context_intr(cpu, 1);
81         pio1->set_context_child(pio2);
82         pio2->set_context_intr(cpu, 2);
83         
84         // memory bus
85         memset(ram, 0, sizeof(ram));
86         memset(rom, 0xff, sizeof(rom));
87         
88         memory->read_bios(_T("MON.ROM"), rom, sizeof(rom));
89         
90         memory->set_memory_r(0x0000, 0x07ff, rom);
91         memory->set_memory_rw(0x1000, 0x17ff, ram);
92         
93         // i/o bus
94         io->set_iomap_range_rw(0x00, 0x03, ctc);
95         io->set_iomap_range_rw(0x10, 0x13, pio1);
96         io->set_iomap_range_rw(0x20, 0x23, pio2);
97         
98         // initialize all devices
99         for(DEVICE* device = first_device; device; device = device->next_device) {
100                 device->initialize();
101         }
102 }
103
104 VM::~VM()
105 {
106         // delete all devices
107         for(DEVICE* device = first_device; device;) {
108                 DEVICE *next_device = device->next_device;
109                 device->release();
110                 delete device;
111                 device = next_device;
112         }
113 }
114
115 DEVICE* VM::get_device(int id)
116 {
117         for(DEVICE* device = first_device; device; device = device->next_device) {
118                 if(device->this_device_id == id) {
119                         return device;
120                 }
121         }
122         return NULL;
123 }
124
125 // ----------------------------------------------------------------------------
126 // drive virtual machine
127 // ----------------------------------------------------------------------------
128
129 void VM::reset()
130 {
131         // reset all devices
132         for(DEVICE* device = first_device; device; device = device->next_device) {
133                 device->reset();
134         }
135 }
136
137 void VM::run()
138 {
139         event->drive();
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         display->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 // ----------------------------------------------------------------------------
186 // notify key
187 // ----------------------------------------------------------------------------
188
189 void VM::key_down(int code, bool repeat)
190 {
191         keyboard->key_down(code);
192 }
193
194 void VM::key_up(int code)
195 {
196         //keyboard->key_up(code);
197 }
198
199 // ----------------------------------------------------------------------------
200 // user interface
201 // ----------------------------------------------------------------------------
202
203 void VM::load_binary(int drv, const _TCHAR* file_path)
204 {
205         if(drv == 0) {
206                 memory->read_image(file_path, ram, sizeof(ram));
207         }
208 }
209
210 void VM::save_binary(int drv, const _TCHAR* file_path)
211 {
212         if(drv == 0) {
213                 memory->write_image(file_path, ram, sizeof(ram));
214         }
215 }
216
217 bool VM::is_frame_skippable()
218 {
219         return event->is_frame_skippable();
220 }
221
222 void VM::update_config()
223 {
224         for(DEVICE* device = first_device; device; device = device->next_device) {
225                 device->update_config();
226         }
227 }
228
229 #define STATE_VERSION   2
230
231 void VM::save_state(FILEIO* state_fio)
232 {
233         state_fio->FputUint32(STATE_VERSION);
234         
235         for(DEVICE* device = first_device; device; device = device->next_device) {
236                 const char *name = typeid(*device).name() + 6; // skip "class "
237                 
238                 state_fio->FputInt32(strlen(name));
239                 state_fio->Fwrite(name, strlen(name), 1);
240                 device->save_state(state_fio);
241         }
242         state_fio->Fwrite(ram, sizeof(ram), 1);
243 }
244
245 bool VM::load_state(FILEIO* state_fio)
246 {
247         if(state_fio->FgetUint32() != STATE_VERSION) {
248                 return false;
249         }
250         for(DEVICE* device = first_device; device; device = device->next_device) {
251                 const char *name = typeid(*device).name() + 6; // skip "class "
252                 
253                 if(!(state_fio->FgetInt32() == strlen(name) && state_fio->Fcompare(name, strlen(name)))) {
254                         return false;
255                 }
256                 if(!device->load_state(state_fio)) {
257                         return false;
258                 }
259         }
260         state_fio->Fread(ram, sizeof(ram), 1);
261         return true;
262 }
263