2 Homebrew Z80 TV GAME SYSTEM Emulator 'eZ80TVGAME'
4 Author : Takeda.Toshiya
10 // http://w01.tp1.jp/~a571632211/z80tvgame/index.html
12 #include "z80tvgame.h"
13 #include "../../emu.h"
14 #include "../device.h"
20 #include "../z80pio.h"
22 #include "../pcm1bit.h"
26 #include "../debugger.h"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
39 first_device = last_device = NULL;
40 dummy = new DEVICE(this, emu); // must be 1st device
41 event = new EVENT(this, emu); // must be 2nd device
42 dummy->set_device_name(_T("1st Dummy"));
45 pio = new I8255(this, emu);
47 pio = new Z80PIO(this, emu);
49 pcm = new PCM1BIT(this, emu);
50 cpu = new Z80(this, emu);
52 joystick = new JOYSTICK(this, emu);
53 memory = new MEMORY(this, emu);
56 event->set_context_cpu(cpu);
57 event->set_context_sound(pcm);
59 // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
60 //pcm->set_realtime_render(true);
63 pio->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 1, 0);
65 pio->set_context_port_b(pcm, SIG_PCM1BIT_SIGNAL, 1, 0);
67 joystick->set_context_pio(pio);
70 cpu->set_context_mem(memory);
71 cpu->set_context_io(pio);
72 cpu->set_context_intr(dummy);
74 cpu->set_context_debugger(new DEBUGGER(this, emu));
77 // initialize all devices
78 for(DEVICE* device = first_device; device; device = device->next_device) {
87 for(DEVICE* device = first_device; device;) {
88 DEVICE *next_device = device->next_device;
95 DEVICE* VM::get_device(int id)
97 for(DEVICE* device = first_device; device; device = device->next_device) {
98 if(device->this_device_id == id) {
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
110 DEVICE *VM::get_cpu(int index)
119 // ----------------------------------------------------------------------------
120 // drive virtual machine
121 // ----------------------------------------------------------------------------
126 for(DEVICE* device = first_device; device; device = device->next_device) {
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
140 void VM::draw_screen()
142 memory->draw_screen();
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
149 void VM::initialize_sound(int rate, int samples)
151 // init sound manager
152 event->initialize_sound(rate, samples);
155 pcm->initialize_sound(rate, 8000);
158 uint16_t* VM::create_sound(int* extra_frames)
160 return event->create_sound(extra_frames);
163 int VM::get_sound_buffer_ptr()
165 return event->get_sound_buffer_ptr();
168 #ifdef USE_SOUND_VOLUME
169 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
172 pcm->set_volume(0, decibel_l, decibel_r);
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
181 void VM::open_cart(int drv, const _TCHAR* file_path)
184 memory->open_cart(file_path);
189 void VM::close_cart(int drv)
192 memory->close_cart();
197 bool VM::is_cart_inserted(int drv)
200 return memory->is_cart_inserted();
206 bool VM::is_frame_skippable()
208 return event->is_frame_skippable();
211 void VM::update_config()
213 for(DEVICE* device = first_device; device; device = device->next_device) {
214 device->update_config();
218 #define STATE_VERSION 2
220 #include "../../statesub.h"
221 #include "../../qt/gui/csp_logger.h"
222 extern CSP_Logger DLL_PREFIX_I *csp_logger;
224 void VM::decl_state(void)
226 #if defined(_USE_I8255)
227 state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::Z80_TV_GAME_I8255_HEAD")), csp_logger);
229 state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::Z80_TV_GAME_Z80PIO_HEAD")), csp_logger);
231 for(DEVICE* device = first_device; device; device = device->next_device) {
232 device->decl_state();
236 void VM::save_state(FILEIO* state_fio)
238 //state_fio->FputUint32(STATE_VERSION);
240 if(state_entry != NULL) {
241 state_entry->save_state(state_fio);
243 for(DEVICE* device = first_device; device; device = device->next_device) {
244 device->save_state(state_fio);
248 bool VM::load_state(FILEIO* state_fio)
250 //if(state_fio->FgetUint32() != STATE_VERSION) {
254 if(state_entry != NULL) {
255 mb = state_entry->load_state(state_fio);
258 emu->out_debug_log("INFO: HEADER DATA ERROR");
261 for(DEVICE* device = first_device; device; device = device->next_device) {
262 if(!device->load_state(state_fio)) {