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 #if defined(__GIT_REPO_VERSION)
100 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
102 for(DEVICE* device = first_device; device; device = device->next_device) {
103 device->initialize();
110 // delete all devices
111 for(DEVICE* device = first_device; device;) {
112 DEVICE *next_device = device->next_device;
115 device = next_device;
119 DEVICE* VM::get_device(int id)
121 for(DEVICE* device = first_device; device; device = device->next_device) {
122 if(device->this_device_id == id) {
129 // ----------------------------------------------------------------------------
130 // drive virtual machine
131 // ----------------------------------------------------------------------------
136 for(DEVICE* device = first_device; device; device = device->next_device) {
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
151 DEVICE *VM::get_cpu(int index)
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
164 void VM::draw_screen()
166 display->draw_screen();
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
173 void VM::initialize_sound(int rate, int samples)
175 // init sound manager
176 event->initialize_sound(rate, samples);
179 uint16_t* VM::create_sound(int* extra_frames)
181 return event->create_sound(extra_frames);
184 int VM::get_sound_buffer_ptr()
186 return event->get_sound_buffer_ptr();
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
193 void VM::key_down(int code, bool repeat)
195 keyboard->key_down(code);
198 void VM::key_up(int code)
200 //keyboard->key_up(code);
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
207 void VM::load_binary(int drv, const _TCHAR* file_path)
210 memory->read_image(file_path, ram, sizeof(ram));
214 void VM::save_binary(int drv, const _TCHAR* file_path)
217 memory->write_image(file_path, ram, sizeof(ram));
221 bool VM::is_frame_skippable()
223 return event->is_frame_skippable();
226 void VM::update_config()
228 for(DEVICE* device = first_device; device; device = device->next_device) {
229 device->update_config();
233 #define STATE_VERSION 2
235 #include "../../statesub.h"
236 #include "../../qt/gui/csp_logger.h"
237 extern CSP_Logger DLL_PREFIX_I *csp_logger;
239 void VM::decl_state(void)
241 state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::BABBAGE2ND_HEAD")), csp_logger);
242 DECL_STATE_ENTRY_MULTI(void, ram, sizeof(ram));
243 for(DEVICE* device = first_device; device; device = device->next_device) {
244 device->decl_state();
248 void VM::save_state(FILEIO* state_fio)
250 //state_fio->FputUint32(STATE_VERSION);
252 if(state_entry != NULL) {
253 state_entry->save_state(state_fio);
255 for(DEVICE* device = first_device; device; device = device->next_device) {
256 device->save_state(state_fio);
258 //state_fio->Fwrite(ram, sizeof(ram), 1);
261 bool VM::load_state(FILEIO* state_fio)
264 if(state_entry != NULL) {
265 mb = state_entry->load_state(state_fio);
268 emu->out_debug_log("INFO: HEADER DATA ERROR");
271 for(DEVICE* device = first_device; device; device = device->next_device) {
272 if(!device->load_state(state_fio)) {