OSDN Git Service

[General][VM] Re-factoring VMs, sync to Upstream.
[csp-qt/common_source_project-fm7.git] / source / src / vm / z80tvgame / z80tvgame.cpp
1 /*
2         Homebrew Z80 TV GAME SYSTEM Emulator 'eZ80TVGAME'
3
4         Author : Takeda.Toshiya
5         Date   : 2015.04.28-
6
7         [ virtual machine ]
8 */
9
10 // http://w01.tp1.jp/~a571632211/z80tvgame/index.html
11
12 #include "z80tvgame.h"
13 #include "../../emu.h"
14 #include "../device.h"
15 #include "../event.h"
16
17 #ifdef _USE_I8255
18 #include "../i8255.h"
19 #else
20 #include "../z80pio.h"
21 #endif
22 #include "../pcm1bit.h"
23 #include "../z80.h"
24
25 #ifdef USE_DEBUGGER
26 #include "../debugger.h"
27 #endif
28
29 #include "joystick.h"
30 #include "memory.h"
31
32 // ----------------------------------------------------------------------------
33 // initialize
34 // ----------------------------------------------------------------------------
35
36 VM::VM(EMU* parent_emu) : emu(parent_emu)
37 {
38         // create devices
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"));
43         
44 #ifdef _USE_I8255
45         pio = new I8255(this, emu);
46 #else
47         pio = new Z80PIO(this, emu);
48 #endif
49         pcm = new PCM1BIT(this, emu);
50         cpu = new Z80(this, emu);
51         
52         joystick = new JOYSTICK(this, emu);
53         memory = new MEMORY(this, emu);
54         
55         // set contexts
56         event->set_context_cpu(cpu);
57         event->set_context_sound(pcm);
58
59         // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
60         //pcm->set_realtime_render(true);
61
62 #ifdef _USE_I8255
63         pio->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 1, 0);
64 #else
65         pio->set_context_port_b(pcm, SIG_PCM1BIT_SIGNAL, 1, 0);
66 #endif
67         joystick->set_context_pio(pio);
68         
69         // cpu bus
70         cpu->set_context_mem(memory);
71         cpu->set_context_io(pio);
72         cpu->set_context_intr(dummy);
73 #ifdef USE_DEBUGGER
74         cpu->set_context_debugger(new DEBUGGER(this, emu));
75 #endif
76         
77         // initialize all devices
78         for(DEVICE* device = first_device; device; device = device->next_device) {
79                 device->initialize();
80         }
81 }
82
83 VM::~VM()
84 {
85         // delete all devices
86         for(DEVICE* device = first_device; device;) {
87                 DEVICE *next_device = device->next_device;
88                 device->release();
89                 delete device;
90                 device = next_device;
91         }
92 }
93
94 DEVICE* VM::get_device(int id)
95 {
96         for(DEVICE* device = first_device; device; device = device->next_device) {
97                 if(device->this_device_id == id) {
98                         return device;
99                 }
100         }
101         return NULL;
102 }
103
104 // ----------------------------------------------------------------------------
105 // debugger
106 // ----------------------------------------------------------------------------
107
108 #ifdef USE_DEBUGGER
109 DEVICE *VM::get_cpu(int index)
110 {
111         if(index == 0) {
112                 return cpu;
113         }
114         return NULL;
115 }
116 #endif
117
118 // ----------------------------------------------------------------------------
119 // drive virtual machine
120 // ----------------------------------------------------------------------------
121
122 void VM::reset()
123 {
124         // reset all devices
125         for(DEVICE* device = first_device; device; device = device->next_device) {
126                 device->reset();
127         }
128 }
129
130 void VM::run()
131 {
132         event->drive();
133 }
134
135 // ----------------------------------------------------------------------------
136 // draw screen
137 // ----------------------------------------------------------------------------
138
139 void VM::draw_screen()
140 {
141         memory->draw_screen();
142 }
143
144 // ----------------------------------------------------------------------------
145 // soud manager
146 // ----------------------------------------------------------------------------
147
148 void VM::initialize_sound(int rate, int samples)
149 {
150         // init sound manager
151         event->initialize_sound(rate, samples);
152         
153         // init sound gen
154         pcm->initialize_sound(rate, 8000);
155 }
156
157 uint16_t* VM::create_sound(int* extra_frames)
158 {
159         return event->create_sound(extra_frames);
160 }
161
162 int VM::get_sound_buffer_ptr()
163 {
164         return event->get_sound_buffer_ptr();
165 }
166
167 #ifdef USE_SOUND_VOLUME
168 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
169 {
170         if(ch == 0) {
171                 pcm->set_volume(0, decibel_l, decibel_r);
172         }
173 }
174 #endif
175
176 // ----------------------------------------------------------------------------
177 // user interface
178 // ----------------------------------------------------------------------------
179
180 void VM::open_cart(int drv, const _TCHAR* file_path)
181 {
182         if(drv == 0) {
183                 memory->open_cart(file_path);
184                 reset();
185         }
186 }
187
188 void VM::close_cart(int drv)
189 {
190         if(drv == 0) {
191                 memory->close_cart();
192                 reset();
193         }
194 }
195
196 bool VM::is_cart_inserted(int drv)
197 {
198         if(drv == 0) {
199                 return memory->is_cart_inserted();
200         } else {
201                 return false;
202         }
203 }
204
205 bool VM::is_frame_skippable()
206 {
207         return event->is_frame_skippable();
208 }
209
210 void VM::update_config()
211 {
212         for(DEVICE* device = first_device; device; device = device->next_device) {
213                 device->update_config();
214         }
215 }
216
217 #define STATE_VERSION   1
218
219 void VM::save_state(FILEIO* state_fio)
220 {
221         state_fio->FputUint32(STATE_VERSION);
222         
223         for(DEVICE* device = first_device; device; device = device->next_device) {
224                 device->save_state(state_fio);
225         }
226 }
227
228 bool VM::load_state(FILEIO* state_fio)
229 {
230         if(state_fio->FgetUint32() != STATE_VERSION) {
231                 return false;
232         }
233         for(DEVICE* device = first_device; device; device = device->next_device) {
234                 if(!device->load_state(state_fio)) {
235                         return false;
236                 }
237         }
238         return true;
239 }
240