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) : emu(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
44 pio = new I8255(this, emu);
46 pio = new Z80PIO(this, emu);
48 pcm = new PCM1BIT(this, emu);
49 cpu = new Z80(this, emu);
51 joystick = new JOYSTICK(this, emu);
52 memory = new MEMORY(this, emu);
55 event->set_context_cpu(cpu);
56 event->set_context_sound(pcm);
59 pio->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 1, 0);
61 pio->set_context_port_b(pcm, SIG_PCM1BIT_SIGNAL, 1, 0);
63 joystick->set_context_pio(pio);
66 cpu->set_context_mem(memory);
67 cpu->set_context_io(pio);
68 cpu->set_context_intr(dummy);
70 cpu->set_context_debugger(new DEBUGGER(this, emu));
73 // initialize all devices
74 for(DEVICE* device = first_device; device; device = device->next_device) {
82 for(DEVICE* device = first_device; device;) {
83 DEVICE *next_device = device->next_device;
90 DEVICE* VM::get_device(int id)
92 for(DEVICE* device = first_device; device; device = device->next_device) {
93 if(device->this_device_id == id) {
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
105 DEVICE *VM::get_cpu(int index)
114 // ----------------------------------------------------------------------------
115 // drive virtual machine
116 // ----------------------------------------------------------------------------
121 for(DEVICE* device = first_device; device; device = device->next_device) {
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 void VM::draw_screen()
137 memory->draw_screen();
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 void VM::initialize_sound(int rate, int samples)
146 // init sound manager
147 event->initialize_sound(rate, samples);
150 pcm->init(rate, 8000);
153 uint16* VM::create_sound(int* extra_frames)
155 return event->create_sound(extra_frames);
158 int VM::sound_buffer_ptr()
160 return event->sound_buffer_ptr();
163 #ifdef USE_SOUND_VOLUME
164 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
167 pcm->set_volume(0, decibel_l, decibel_r);
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
176 void VM::open_cart(int drv, const _TCHAR* file_path)
179 memory->open_cart(file_path);
184 void VM::close_cart(int drv)
187 memory->close_cart();
192 bool VM::cart_inserted(int drv)
195 return memory->cart_inserted();
203 return event->now_skip();
206 void VM::update_config()
208 for(DEVICE* device = first_device; device; device = device->next_device) {
209 device->update_config();
213 #define STATE_VERSION 1
215 void VM::save_state(FILEIO* state_fio)
217 state_fio->FputUint32(STATE_VERSION);
219 for(DEVICE* device = first_device; device; device = device->next_device) {
220 device->save_state(state_fio);
224 bool VM::load_state(FILEIO* state_fio)
226 if(state_fio->FgetUint32() != STATE_VERSION) {
229 for(DEVICE* device = first_device; device; device = device->next_device) {
230 if(!device->load_state(state_fio)) {