2 NEC TK-80BS (COMPO BS/80) Emulator 'eTK-80BS'
3 NEC TK-80 Emulator 'eTK-80'
4 NEC TK-85 Emulator 'eTK-85'
6 Author : Takeda.Toshiya
13 #include "../../emu.h"
14 #include "../device.h"
17 #include "../datarec.h"
24 //#include "../memory.h"
26 #include "../pcm1bit.h"
29 #include "../debugger.h"
32 #if defined(_TK80BS) || defined(_TK80)
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
47 // boot_mode = config.boot_mode;
50 #if defined(_TK80BS) || defined(_TK80)
51 config.wave_shaper[0] = false;
55 first_device = last_device = NULL;
56 dummy = new DEVICE(this, emu); // must be 1st device
57 dummy->set_device_name(_T("1st Dummy"));
58 event = new EVENT(this, emu); // must be 2nd device
60 cpu = new I8080(this, emu);
62 sio_b = new I8251(this, emu); // on TK-80BS
63 sio_b->set_device_name(_T("i8251 SIO (TK-80BS/CMT)"));
64 pio_b = new I8255(this, emu);
65 pio_b->set_device_name(_T("i8255 PIO (TK-80BS/DISPLAY)"));
66 memio = new IO(this, emu);
67 memio->set_device_name(_T("Memory Mapped I/O (TK-80BS)"));
69 drec = new DATAREC(this, emu);
70 drec->set_context_noise_play(new NOISE(this, emu));
71 drec->set_context_noise_stop(new NOISE(this, emu));
72 drec->set_context_noise_fast(new NOISE(this, emu));
73 pio_t = new I8255(this, emu); // on TK-80
74 // memory = new MEMORY(this, emu);
76 pcm0 = new PCM1BIT(this, emu);
77 pcm0->set_device_name(_T("1-Bit PCM Sound #1"));
78 pcm1 = new PCM1BIT(this, emu);
79 pcm1->set_device_name(_T("1-Bit PCM Sound #2"));
81 #if defined(_TK80BS) || defined(_TK80)
82 cmt = new CMT(this, emu);
84 display = new DISPLAY(this, emu);
85 keyboard = new KEYBOARD(this, emu);
86 memory = new MEMBUS(this, emu);
88 pio_t->set_device_name(_T("i8255 PIO (TK-80/SOUND/KEYBOARD/DISPLAY)"));
90 pio_t->set_device_name(_T("i8255 PIO (TK-80/SOUND/KEYBOARD)"));
94 event->set_context_cpu(cpu);
95 event->set_context_sound(pcm0);
96 event->set_context_sound(pcm1);
97 event->set_context_sound(drec);
98 event->set_context_sound(drec->get_context_noise_play());
99 event->set_context_sound(drec->get_context_noise_stop());
100 event->set_context_sound(drec->get_context_noise_fast());
113 sio_b->set_context_out(cmt, SIG_CMT_OUT);
114 pio_b->set_context_port_c(display, SIG_DISPLAY_MODE, 3, 0);
116 #if defined(_TK80BS) || defined(_TK80)
117 drec->set_context_ear(cmt, SIG_CMT_EAR, 1);
118 pio_t->set_context_port_c(cmt, SIG_CMT_MIC, 1, 0);
120 drec->set_context_ear(cpu, SIG_I8085_SID, 1);
121 cpu->set_context_sod(drec, SIG_DATAREC_MIC, 1);
123 pio_t->set_context_port_c(pcm0, SIG_PCM1BIT_SIGNAL, 2, 0);
124 pio_t->set_context_port_c(pcm1, SIG_PCM1BIT_SIGNAL, 4, 0);
125 pio_t->set_context_port_c(keyboard, SIG_KEYBOARD_COLUMN, 0x70, 0);
126 #if defined(_TK80BS) || defined(_TK80)
127 pio_t->set_context_port_c(display, SIG_DISPLAY_DMA, 0x80, 0);
129 pio_t->set_context_port_c(memory, SIG_MEMBUS_PC7, 0x80, 0);
131 // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
132 //pcm0->set_realtime_render(true);
133 //pcm1->set_realtime_render(true);
135 #if defined(_TK80BS) || defined(_TK80)
136 cmt->set_context_drec(drec);
137 cmt->set_context_pio(pio_t);
140 cmt->set_context_sio(sio_b);
141 display->set_vram_ptr(vram);
142 keyboard->set_context_pio_b(pio_b);
143 keyboard->set_context_cpu(cpu);
145 display->set_led_ptr(ram + 0x3f8);
146 keyboard->set_context_pio_t(pio_t);
147 memory->set_context_cpu(cpu);
150 cpu->set_context_mem(memory);
151 cpu->set_context_io(pio_t);
152 cpu->set_context_intr(keyboard);
154 cpu->set_context_debugger(new DEBUGGER(this, emu));
158 memset(mon, 0xff, sizeof(mon));
160 memset(bsmon, 0xff, sizeof(bsmon));
162 memset(ext, 0xff, sizeof(ext));
165 static const uint8_t top[3] = {0xc3, 0x00, 0xf0};
166 static const uint8_t rst[3] = {0xc3, 0xdd, 0x83};
168 if(!memory->read_bios(_T("TK80.ROM"), mon, sizeof(mon))) {
170 memcpy(mon, top, sizeof(top));
171 memcpy(mon + 0x38, rst, sizeof(rst));
173 if(memory->read_bios(_T("BSMON.ROM"), bsmon, sizeof(bsmon))) {
175 memcpy(mon + 0x38, rst, sizeof(rst));
178 memory->read_bios(_T("TK80.ROM"), mon, sizeof(mon));
180 memory->read_bios(_T("TK85.ROM"), mon, sizeof(mon));
182 memory->read_bios(_T("EXT.ROM"), ext, sizeof(ext));
184 memory->set_memory_r(0x0000, 0x07ff, mon);
185 memory->set_memory_r(0x0c00, 0x7bff, ext);
187 memory->set_memory_mapped_io_rw(0x7c00, 0x7dff, memio);
188 memory->set_memory_rw(0x7e00, 0x7fff, vram);
190 memory->set_memory_rw(0x8000, 0xcfff, ram);
192 memory->set_memory_r(0xd000, 0xefff, basic);
193 memory->set_memory_r(0xf000, 0xffff, bsmon);
196 memio->set_iomap_alias_rw(0x7df8, sio_b, 0);
197 memio->set_iomap_alias_rw(0x7df9, sio_b, 1);
198 memio->set_iomap_alias_rw(0x7dfc, pio_b, 0);
199 memio->set_iomap_alias_rw(0x7dfd, pio_b, 1);
200 memio->set_iomap_alias_rw(0x7dfe, pio_b, 2);
201 memio->set_iomap_alias_w(0x7dff, pio_b, 3);
204 // initialize all devices
205 #if defined(__GIT_REPO_VERSION)
206 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
208 for(DEVICE* device = first_device; device; device = device->next_device) {
209 device->initialize();
216 // delete all devices
217 for(DEVICE* device = first_device; device;) {
218 DEVICE *next_device = device->next_device;
221 device = next_device;
225 DEVICE* VM::get_device(int id)
227 for(DEVICE* device = first_device; device; device = device->next_device) {
228 if(device->this_device_id == id) {
235 // ----------------------------------------------------------------------------
236 // drive virtual machine
237 // ----------------------------------------------------------------------------
243 if(boot_mode != config.boot_mode) {
244 memset(basic, 0xff, sizeof(basic));
245 if(config.boot_mode == 0) {
246 memory->read_bios(_T("LV1BASIC.ROM"), basic + 0x1000, 0x1000);
248 memory->read_bios(_T("LV2BASIC.ROM"), basic, sizeof(basic));
250 boot_mode = config.boot_mode;
252 memset(ram, 0, sizeof(ram));
256 emu->reload_bitmap();
258 memset(vram, 0x20, sizeof(vram));
262 for(DEVICE* device = first_device; device; device = device->next_device) {
266 // init 8255 on TK-80
267 pio_t->write_io8(0xfb, 0x92);
268 pio_t->write_signal(SIG_I8255_PORT_A, 0xff, 0xff);
276 // ----------------------------------------------------------------------------
278 // ----------------------------------------------------------------------------
281 DEVICE *VM::get_cpu(int index)
290 // ----------------------------------------------------------------------------
292 // ----------------------------------------------------------------------------
294 void VM::draw_screen()
296 display->draw_screen();
300 int VM::max_draw_ranges()
306 // ----------------------------------------------------------------------------
308 // ----------------------------------------------------------------------------
310 void VM::initialize_sound(int rate, int samples)
312 // init sound manager
313 event->initialize_sound(rate, samples);
316 pcm0->initialize_sound(rate, 8000);
317 pcm1->initialize_sound(rate, 8000);
320 uint16_t* VM::create_sound(int* extra_frames)
322 return event->create_sound(extra_frames);
325 int VM::get_sound_buffer_ptr()
327 return event->get_sound_buffer_ptr();
330 #ifdef USE_SOUND_VOLUME
331 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
334 pcm0->set_volume(0, decibel_l, decibel_r);
336 pcm1->set_volume(0, decibel_l, decibel_r);
338 drec->set_volume(0, decibel_l, decibel_r);
340 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
341 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
342 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
347 // ----------------------------------------------------------------------------
349 // ----------------------------------------------------------------------------
351 void VM::key_down(int code, bool repeat)
355 cpu->write_signal(SIG_CPU_NMI, 1, 1);
359 keyboard->key_down(code);
362 void VM::key_up(int code)
369 keyboard->key_up(code);
372 bool VM::get_caps_locked()
374 return keyboard->get_caps_locked();
377 bool VM::get_kana_locked()
379 return keyboard->get_kana_locked();
382 // ----------------------------------------------------------------------------
384 // ----------------------------------------------------------------------------
386 void VM::load_binary(int drv, const _TCHAR* file_path)
389 memory->read_image(file_path, ram, sizeof(ram));
393 void VM::save_binary(int drv, const _TCHAR* file_path)
396 memory->write_image(file_path, ram, sizeof(ram));
400 void VM::play_tape(int drv, const _TCHAR* file_path)
403 drec->play_tape(file_path);
404 // drec->set_remote(true);
406 } else if(drv == 1) {
407 cmt->play_tape(file_path);
412 void VM::rec_tape(int drv, const _TCHAR* file_path)
415 drec->rec_tape(file_path);
416 // drec->set_remote(true);
418 } else if(drv == 1) {
419 cmt->rec_tape(file_path);
424 void VM::close_tape(int drv)
430 // drec->set_remote(false);
432 } else if(drv == 1) {
438 bool VM::is_tape_inserted(int drv)
441 return drec->is_tape_inserted();
443 } else if(drv == 1) {
444 return cmt->is_tape_inserted();
450 bool VM::is_tape_playing(int drv)
453 return drec->is_tape_playing();
455 } else if(drv == 1) {
456 return cmt->is_tape_playing();
462 bool VM::is_tape_recording(int drv)
465 return drec->is_tape_recording();
467 } else if(drv == 1) {
468 return cmt->is_tape_recording();
474 int VM::get_tape_position(int drv)
477 return drec->get_tape_position();
479 } else if(drv == 1) {
480 return cmt->get_tape_position();
486 const _TCHAR* VM::get_tape_message(int drv)
489 return drec->get_message();
494 void VM::push_play(int drv)
498 drec->set_remote(true);
502 void VM::push_stop(int drv)
505 drec->set_remote(false);
509 void VM::push_fast_forward(int drv)
513 drec->set_remote(true);
517 void VM::push_fast_rewind(int drv)
520 drec->set_ff_rew(-1);
521 drec->set_remote(true);
525 bool VM::is_frame_skippable()
527 // return event->is_frame_skippable();
531 void VM::update_config()
534 if(boot_mode != config.boot_mode) {
535 // boot mode is changed !!!
536 // boot_mode = config.boot_mode;
540 for(DEVICE* device = first_device; device; device = device->next_device) {
541 device->update_config();
548 #define STATE_VERSION 6
550 #include "../../statesub.h"
551 #include "../../qt/gui/csp_logger.h"
552 extern CSP_Logger DLL_PREFIX_I *csp_logger;
554 void VM::decl_state(void)
557 state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::TK_80_HEAD")), csp_logger);
559 state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::TK_85_HEAD")), csp_logger);
560 #elif defined(_TK80BS)
561 state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::TK_80BS_HEAD")), csp_logger);
563 state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::TK_80_SERIES_HEAD")), csp_logger);
565 DECL_STATE_ENTRY_1D_ARRAY(ram, sizeof(ram));
567 DECL_STATE_ENTRY_1D_ARRAY(vram, sizeof(vram));
568 DECL_STATE_ENTRY_INT32(boot_mode);
569 //DECL_STATE_ENTRY_INT32(draw_ranges);
571 for(DEVICE* device = first_device; device; device = device->next_device) {
572 device->decl_state();
576 void VM::save_state(FILEIO* state_fio)
578 //state_fio->FputUint32(STATE_VERSION);
580 if(state_entry != NULL) {
581 state_entry->save_state(state_fio);
583 for(DEVICE* device = first_device; device; device = device->next_device) {
584 device->save_state(state_fio);
586 // state_fio->Fwrite(ram, sizeof(ram), 1);
587 //#if defined(_TK80BS)
588 // state_fio->Fwrite(vram, sizeof(vram), 1);
589 // state_fio->FputInt32(boot_mode);
590 //// state_fio->FputInt32(draw_ranges);
594 bool VM::load_state(FILEIO* state_fio)
596 //if(state_fio->FgetUint32() != STATE_VERSION) {
600 if(state_entry != NULL) {
601 mb = state_entry->load_state(state_fio);
604 emu->out_debug_log("INFO: HEADER DATA ERROR");
607 for(DEVICE* device = first_device; device; device = device->next_device) {
608 if(!device->load_state(state_fio)) {
612 // state_fio->Fread(ram, sizeof(ram), 1);
614 // state_fio->Fread(vram, sizeof(vram), 1);
615 // boot_mode = state_fio->FgetInt32();
616 //// draw_ranges = state_fio->FgetInt32();
619 emu->reload_bitmap();