2 Gijutsu-Hyoron-Sha Babbage-2nd Emulator 'eBabbage-2nd'
4 Author : Takeda.Toshiya
10 #include "babbage2nd.h"
11 #include "../../emu.h"
12 #include "../device.h"
16 #include "../memory.h"
18 #include "../z80ctc.h"
19 #include "../z80pio.h"
22 #include "../debugger.h"
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
32 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
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
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)"));
48 display = new DISPLAY(this, emu);
49 keyboard = new KEYBOARD(this, emu);
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)"));
58 event->set_context_cpu(cpu);
60 pio2->set_context_port_b(display, SIG_DISPLAY_7SEG_LED, 0xff, 0);
61 keyboard->set_context_pio(pio2);
63 ctc->set_context_zc2(ctc, SIG_Z80CTC_TRIG_1, 1);
64 ctc->set_context_zc1(ctc, SIG_Z80CTC_TRIG_0, 1);
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);
70 cpu->set_context_mem(memory);
71 cpu->set_context_io(io);
72 cpu->set_context_intr(ctc);
74 cpu->set_context_debugger(new DEBUGGER(this, emu));
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);
85 memset(ram, 0, sizeof(ram));
86 memset(rom, 0xff, sizeof(rom));
88 memory->read_bios(_T("MON.ROM"), rom, sizeof(rom));
90 memory->set_memory_r(0x0000, 0x07ff, rom);
91 memory->set_memory_rw(0x1000, 0x17ff, ram);
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);
98 // initialize all devices
99 for(DEVICE* device = first_device; device; device = device->next_device) {
100 device->initialize();
107 // delete all devices
108 for(DEVICE* device = first_device; device;) {
109 DEVICE *next_device = device->next_device;
112 device = next_device;
116 DEVICE* VM::get_device(int id)
118 for(DEVICE* device = first_device; device; device = device->next_device) {
119 if(device->this_device_id == id) {
126 // ----------------------------------------------------------------------------
127 // drive virtual machine
128 // ----------------------------------------------------------------------------
133 for(DEVICE* device = first_device; device; device = device->next_device) {
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
148 DEVICE *VM::get_cpu(int index)
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 void VM::draw_screen()
163 display->draw_screen();
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 void VM::initialize_sound(int rate, int samples)
172 // init sound manager
173 event->initialize_sound(rate, samples);
176 uint16_t* VM::create_sound(int* extra_frames)
178 return event->create_sound(extra_frames);
181 int VM::get_sound_buffer_ptr()
183 return event->get_sound_buffer_ptr();
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 void VM::key_down(int code, bool repeat)
192 keyboard->key_down(code);
195 void VM::key_up(int code)
197 //keyboard->key_up(code);
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 void VM::load_binary(int drv, const _TCHAR* file_path)
207 memory->read_image(file_path, ram, sizeof(ram));
211 void VM::save_binary(int drv, const _TCHAR* file_path)
214 memory->write_image(file_path, ram, sizeof(ram));
218 bool VM::is_frame_skippable()
220 return event->is_frame_skippable();
223 void VM::update_config()
225 for(DEVICE* device = first_device; device; device = device->next_device) {
226 device->update_config();
230 #define STATE_VERSION 2
232 #include "../../statesub.h"
233 #include "../../qt/gui/csp_logger.h"
234 extern CSP_Logger DLL_PREFIX_I *csp_logger;
236 void VM::decl_state(void)
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();
245 void VM::save_state(FILEIO* state_fio)
247 //state_fio->FputUint32(STATE_VERSION);
249 if(state_entry != NULL) {
250 state_entry->save_state(state_fio);
252 for(DEVICE* device = first_device; device; device = device->next_device) {
253 device->save_state(state_fio);
255 //state_fio->Fwrite(ram, sizeof(ram), 1);
258 bool VM::load_state(FILEIO* state_fio)
261 if(state_entry != NULL) {
262 mb = state_entry->load_state(state_fio);
265 emu->out_debug_log("INFO: HEADER DATA ERROR");
268 for(DEVICE* device = first_device; device; device = device->next_device) {
269 if(!device->load_state(state_fio)) {