2 SHARP SM-B-80TE Emulator 'eSM-B-80TE'
4 Author : Takeda.Toshiya
11 #include "../../emu.h"
12 #include "../device.h"
15 #include "../datarec.h"
19 #include "../z80pio.h"
22 #include "../debugger.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 VM::VM(EMU* parent_emu) : emu(parent_emu)
34 first_device = last_device = NULL;
35 dummy = new DEVICE(this, emu); // must be 1st device
36 event = new EVENT(this, emu); // must be 2nd device
38 drec = new DATAREC(this, emu);
39 io = new IO(this, emu);
40 not_ear = new NOT(this, emu);
41 cpu = new Z80(this, emu);
42 pio1 = new Z80PIO(this, emu);
43 pio1->set_device_name(_T("8255 PIO (LEDs/Keyboard/CMT)"));
44 pio2 = new Z80PIO(this, emu);
45 pio2->set_device_name(_T("8255 PIO (User)"));
47 memory = new MEMORY(this, emu);
50 event->set_context_cpu(cpu);
52 // PIO1 PA0-7 -> 7seg-LED data
53 pio1->set_context_port_a(memory, SIG_MEMORY_PIO1_PA, 0xff, 0);
54 // PIO1 PB0-2 <- Keyboard data
55 // PIO1 PB3-5 -> 7seg-LED/Keyboard column
56 pio1->set_context_port_b(memory, SIG_MEMORY_PIO1_PB, 0x38, 0);
58 pio1->set_context_port_b(drec, SIG_DATAREC_MIC, 0x40, 0);
59 // PIO1 PB7 <- NOT <- EAR
60 drec->set_context_ear(not_ear, SIG_NOT_INPUT, 1);
61 not_ear->set_context_out(pio1, SIG_Z80PIO_PORT_B, 0x80);
63 memory->set_context_cpu(cpu);
64 memory->set_context_drec(drec);
65 memory->set_context_pio1(pio1);
68 cpu->set_context_mem(memory);
69 cpu->set_context_io(io);
70 cpu->set_context_intr(pio1);
72 // z80 family daisy chain
73 pio1->set_context_intr(cpu, 0);
74 pio1->set_context_child(pio2);
75 pio2->set_context_intr(cpu, 1);
77 cpu->set_context_debugger(new DEBUGGER(this, emu));
81 io->set_iomap_range_rw(0xd0, 0xd3, pio1);
82 io->set_iomap_range_rw(0xd4, 0xd7, pio2);
83 io->set_iomap_range_rw(0xd8, 0xdb, memory);
85 // initialize all devices
86 for(DEVICE* device = first_device; device; device = device->next_device) {
94 for(DEVICE* device = first_device; device;) {
95 DEVICE *next_device = device->next_device;
102 DEVICE* VM::get_device(int id)
104 for(DEVICE* device = first_device; device; device = device->next_device) {
105 if(device->this_device_id == id) {
112 // ----------------------------------------------------------------------------
113 // drive virtual machine
114 // ----------------------------------------------------------------------------
119 for(DEVICE* device = first_device; device; device = device->next_device) {
129 double VM::get_frame_rate()
131 return event->get_frame_rate();
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
139 DEVICE *VM::get_cpu(int index)
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 void VM::draw_screen()
154 memory->draw_screen();
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 void VM::initialize_sound(int rate, int samples)
163 // init sound manager
164 event->initialize_sound(rate, samples);
167 uint16_t* VM::create_sound(int* extra_frames)
169 return event->create_sound(extra_frames);
172 int VM::get_sound_buffer_ptr()
174 return event->get_sound_buffer_ptr();
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
181 void VM::key_down(int code, bool repeat)
183 if(code == 0x97 && !repeat) {
188 void VM::key_up(int code)
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
196 void VM::play_tape(const _TCHAR* file_path)
198 drec->play_tape(file_path);
199 drec->set_remote(true);
202 void VM::rec_tape(const _TCHAR* file_path)
204 drec->rec_tape(file_path);
205 drec->set_remote(true);
208 void VM::close_tape()
213 drec->set_remote(false);
216 bool VM::is_tape_inserted()
218 return drec->is_tape_inserted();
221 bool VM::is_tape_playing()
223 return drec->is_tape_playing();
226 bool VM::is_tape_recording()
228 return drec->is_tape_recording();
231 int VM::get_tape_position()
233 return drec->get_tape_position();
236 void VM::load_binary(int drv, const _TCHAR* file_path)
239 memory->load_ram(file_path);
243 void VM::save_binary(int drv, const _TCHAR* file_path)
246 memory->save_ram(file_path);
250 bool VM::is_frame_skippable()
252 return event->is_frame_skippable();
255 void VM::update_config()
257 for(DEVICE* device = first_device; device; device = device->next_device) {
258 device->update_config();
262 #define STATE_VERSION 1
264 void VM::save_state(FILEIO* state_fio)
266 state_fio->FputUint32(STATE_VERSION);
268 for(DEVICE* device = first_device; device; device = device->next_device) {
269 device->save_state(state_fio);
273 bool VM::load_state(FILEIO* state_fio)
275 if(state_fio->FgetUint32() != STATE_VERSION) {
278 for(DEVICE* device = first_device; device; device = device->next_device) {
279 if(!device->load_state(state_fio)) {