OSDN Git Service

ff57688221ea543edd2260e95852c629b697ca62
[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) : VM_TEMPLATE(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         decl_state();
103 }
104
105 VM::~VM()
106 {
107         // delete all devices
108         for(DEVICE* device = first_device; device;) {
109                 DEVICE *next_device = device->next_device;
110                 device->release();
111                 delete device;
112                 device = next_device;
113         }
114 }
115
116 DEVICE* VM::get_device(int id)
117 {
118         for(DEVICE* device = first_device; device; device = device->next_device) {
119                 if(device->this_device_id == id) {
120                         return device;
121                 }
122         }
123         return NULL;
124 }
125
126 // ----------------------------------------------------------------------------
127 // drive virtual machine
128 // ----------------------------------------------------------------------------
129
130 void VM::reset()
131 {
132         // reset all devices
133         for(DEVICE* device = first_device; device; device = device->next_device) {
134                 device->reset();
135         }
136 }
137
138 void VM::run()
139 {
140         event->drive();
141 }
142
143 // ----------------------------------------------------------------------------
144 // debugger
145 // ----------------------------------------------------------------------------
146
147 #ifdef USE_DEBUGGER
148 DEVICE *VM::get_cpu(int index)
149 {
150         if(index == 0) {
151                 return cpu;
152         }
153         return NULL;
154 }
155 #endif
156
157 // ----------------------------------------------------------------------------
158 // draw screen
159 // ----------------------------------------------------------------------------
160
161 void VM::draw_screen()
162 {
163         display->draw_screen();
164 }
165
166 // ----------------------------------------------------------------------------
167 // soud manager
168 // ----------------------------------------------------------------------------
169
170 void VM::initialize_sound(int rate, int samples)
171 {
172         // init sound manager
173         event->initialize_sound(rate, samples);
174 }
175
176 uint16_t* VM::create_sound(int* extra_frames)
177 {
178         return event->create_sound(extra_frames);
179 }
180
181 int VM::get_sound_buffer_ptr()
182 {
183         return event->get_sound_buffer_ptr();
184 }
185
186 // ----------------------------------------------------------------------------
187 // notify key
188 // ----------------------------------------------------------------------------
189
190 void VM::key_down(int code, bool repeat)
191 {
192         keyboard->key_down(code);
193 }
194
195 void VM::key_up(int code)
196 {
197         //keyboard->key_up(code);
198 }
199
200 // ----------------------------------------------------------------------------
201 // user interface
202 // ----------------------------------------------------------------------------
203
204 void VM::load_binary(int drv, const _TCHAR* file_path)
205 {
206         if(drv == 0) {
207                 memory->read_image(file_path, ram, sizeof(ram));
208         }
209 }
210
211 void VM::save_binary(int drv, const _TCHAR* file_path)
212 {
213         if(drv == 0) {
214                 memory->write_image(file_path, ram, sizeof(ram));
215         }
216 }
217
218 bool VM::is_frame_skippable()
219 {
220         return event->is_frame_skippable();
221 }
222
223 void VM::update_config()
224 {
225         for(DEVICE* device = first_device; device; device = device->next_device) {
226                 device->update_config();
227         }
228 }
229
230 #define STATE_VERSION   2
231
232 #include "../../statesub.h"
233 #include "../../qt/gui/csp_logger.h"
234 extern CSP_Logger DLL_PREFIX_I *csp_logger;
235
236 void VM::decl_state(void)
237 {
238         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::BABBAGE2ND_HEAD")), csp_logger);
239         DECL_STATE_ENTRY_MULTI(void, ram, sizeof(ram));
240         for(DEVICE* device = first_device; device; device = device->next_device) {
241                 device->decl_state();
242         }
243 }
244
245 void VM::save_state(FILEIO* state_fio)
246 {
247         //state_fio->FputUint32(STATE_VERSION);
248         
249         if(state_entry != NULL) {
250                 state_entry->save_state(state_fio);
251         }
252         for(DEVICE* device = first_device; device; device = device->next_device) {
253                 device->save_state(state_fio);
254         }
255         //state_fio->Fwrite(ram, sizeof(ram), 1);
256 }
257
258 bool VM::load_state(FILEIO* state_fio)
259 {
260         bool mb = false;
261         if(state_entry != NULL) {
262                 mb = state_entry->load_state(state_fio);
263         }
264         if(!mb) {
265                 emu->out_debug_log("INFO: HEADER DATA ERROR");
266                 return false;
267         }
268         for(DEVICE* device = first_device; device; device = device->next_device) {
269                 if(!device->load_state(state_fio)) {
270                         return false;
271                 }
272         }
273         return true;
274 }
275