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 using BABBAGE2ND::DISPLAY;
29 using BABBAGE2ND::KEYBOARD;
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
38 first_device = last_device = NULL;
39 dummy = new DEVICE(this, emu); // must be 1st device
40 event = new EVENT(this, emu); // must be 2nd device
42 io = new IO(this, emu);
43 memory = new MEMORY(this, emu);
44 cpu = new Z80(this, emu);
45 ctc = new Z80CTC(this, emu);
46 pio1 = new Z80PIO(this, emu);
47 pio1->set_device_name(_T("Z80 PIO (LEDs)"));
48 pio2 = new Z80PIO(this, emu);
49 pio2->set_device_name(_T("Z80 PIO (7-Seg/Keyboard)"));
51 display = new DISPLAY(this, emu);
52 keyboard = new KEYBOARD(this, emu);
56 dummy->set_device_name(_T("1st Dummy"));
57 pio1->set_device_name(_T("Z80 PIO(LEDs)"));
58 pio2->set_device_name(_T("Z80 PIO(7SEG/KEYBOARD)"));
61 event->set_context_cpu(cpu);
63 pio2->set_context_port_b(display, SIG_DISPLAY_7SEG_LED, 0xff, 0);
64 keyboard->set_context_pio(pio2);
66 ctc->set_context_zc2(ctc, SIG_Z80CTC_TRIG_1, 1);
67 ctc->set_context_zc1(ctc, SIG_Z80CTC_TRIG_0, 1);
69 pio1->set_context_port_b(display, SIG_DISPLAY_8BIT_LED, 0xff, 0);
70 //pio1->set_context_port_b(pio1, SIG_Z80PIO_PORT_A, 0xff, 0);
73 cpu->set_context_mem(memory);
74 cpu->set_context_io(io);
75 cpu->set_context_intr(ctc);
77 cpu->set_context_debugger(new DEBUGGER(this, emu));
80 // z80 family daisy chain
81 ctc->set_context_intr(cpu, 0);
82 ctc->set_context_child(pio1);
83 pio1->set_context_intr(cpu, 1);
84 pio1->set_context_child(pio2);
85 pio2->set_context_intr(cpu, 2);
88 memset(ram, 0, sizeof(ram));
89 memset(rom, 0xff, sizeof(rom));
91 memory->read_bios(_T("MON.ROM"), rom, sizeof(rom));
93 memory->set_memory_r(0x0000, 0x07ff, rom);
94 memory->set_memory_rw(0x1000, 0x17ff, ram);
97 io->set_iomap_range_rw(0x00, 0x03, ctc);
98 io->set_iomap_range_rw(0x10, 0x13, pio1);
99 io->set_iomap_range_rw(0x20, 0x23, pio2);
101 // initialize all devices
102 #if defined(__GIT_REPO_VERSION)
103 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
105 for(DEVICE* device = first_device; device; device = device->next_device) {
106 device->initialize();
112 // delete all devices
113 for(DEVICE* device = first_device; device;) {
114 DEVICE *next_device = device->next_device;
117 device = next_device;
121 DEVICE* VM::get_device(int id)
123 for(DEVICE* device = first_device; device; device = device->next_device) {
124 if(device->this_device_id == id) {
131 // ----------------------------------------------------------------------------
132 // drive virtual machine
133 // ----------------------------------------------------------------------------
138 for(DEVICE* device = first_device; device; device = device->next_device) {
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
153 DEVICE *VM::get_cpu(int index)
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 void VM::draw_screen()
168 display->draw_screen();
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
175 void VM::initialize_sound(int rate, int samples)
177 // init sound manager
178 event->initialize_sound(rate, samples);
181 uint16_t* VM::create_sound(int* extra_frames)
183 return event->create_sound(extra_frames);
186 int VM::get_sound_buffer_ptr()
188 return event->get_sound_buffer_ptr();
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
195 void VM::key_down(int code, bool repeat)
197 keyboard->key_down(code);
200 void VM::key_up(int code)
202 //keyboard->key_up(code);
205 // ----------------------------------------------------------------------------
207 // ----------------------------------------------------------------------------
209 void VM::load_binary(int drv, const _TCHAR* file_path)
212 memory->read_image(file_path, ram, sizeof(ram));
216 void VM::save_binary(int drv, const _TCHAR* file_path)
219 memory->write_image(file_path, ram, sizeof(ram));
223 bool VM::is_frame_skippable()
225 return event->is_frame_skippable();
228 void VM::update_config()
230 for(DEVICE* device = first_device; device; device = device->next_device) {
231 device->update_config();
235 #define STATE_VERSION 2
237 bool VM::process_state(FILEIO* state_fio, bool loading)
239 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
242 for(DEVICE* device = first_device; device; device = device->next_device) {
243 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
244 // const char *name = typeid(*device).name();
245 // But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
246 const char *name = device->get_device_name();
247 int len = strlen(name);
249 if(!state_fio->StateCheckInt32(len)) {
251 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
255 if(!state_fio->StateCheckBuffer(name, len, 1)) {
257 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
261 if(!device->process_state(state_fio, loading)) {
263 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
268 // Machine specified.
269 state_fio->StateBuffer(ram, sizeof(ram), 1);