OSDN Git Service

[VM] TRY:Use namespace {VMNAME} to separate around VMs. This feature still apply...
[csp-qt/common_source_project-fm7.git] / source / src / vm / colecovision / colecovision.cpp
1 /*
2         COLECO ColecoVision Emulator 'yaCOLECOVISION'
3
4         Author : tanam
5         Date   : 2016.08.14-
6
7         [ virtual machine ]
8 */
9
10 #include "colecovision.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../io.h"
16 #include "../sn76489an.h"
17 #include "../tms9918a.h"
18 #include "../z80.h"
19
20 #ifdef USE_DEBUGGER
21 #include "../debugger.h"
22 #endif
23
24 #include "./keyboard.h"
25 #include "./memory.h"
26
27 using COLECOVISION::KEYBOARD;
28 using COLECOVISION::MEMORY;
29 // ----------------------------------------------------------------------------
30 // initialize
31 // ----------------------------------------------------------------------------
32
33 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
34 {
35         // create devices
36         first_device = last_device = NULL;
37         dummy = new DEVICE(this, emu);  // must be 1st device
38         event = new EVENT(this, emu);   // must be 2nd device
39         
40         io = new IO(this, emu);
41         psg = new SN76489AN(this, emu);
42         vdp = new TMS9918A(this, emu);
43         cpu = new Z80(this, emu);
44         
45         key = new KEYBOARD(this, emu);
46         memory = new MEMORY(this, emu);
47         
48         // set contexts
49         event->set_context_cpu(cpu);
50         event->set_context_sound(psg);
51         
52         vdp->set_context_irq(cpu, SIG_CPU_NMI, 1);
53         key->set_context_cpu(cpu);
54         
55         // cpu bus
56         cpu->set_context_mem(memory);
57         cpu->set_context_io(io);
58         cpu->set_context_intr(dummy);
59 #ifdef USE_DEBUGGER
60         cpu->set_context_debugger(new DEBUGGER(this, emu));
61 #endif
62         
63         // i/o bus
64         io->set_iomap_range_w(0x80, 0x9f, key);
65         io->set_iomap_range_rw(0xbe, 0xbf, vdp);
66         io->set_iomap_range_w(0xc0, 0xdf, key);
67         io->set_iomap_range_r(0xfc, 0xff, key);
68         io->set_iomap_range_w(0xff, 0xff, psg);
69         
70         // initialize all devices
71 #if defined(__GIT_REPO_VERSION)
72         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
73 #endif
74         for(DEVICE* device = first_device; device; device = device->next_device) {
75                 device->initialize();
76         }
77 }
78
79 VM::~VM()
80 {
81         // delete all devices
82         for(DEVICE* device = first_device; device;) {
83                 DEVICE *next_device = device->next_device;
84                 device->release();
85                 delete device;
86                 device = next_device;
87         }
88 }
89
90 DEVICE* VM::get_device(int id)
91 {
92         for(DEVICE* device = first_device; device; device = device->next_device) {
93                 if(device->this_device_id == id) {
94                         return device;
95                 }
96         }
97         return NULL;
98 }
99
100 // ----------------------------------------------------------------------------
101 // debugger
102 // ----------------------------------------------------------------------------
103
104 #ifdef USE_DEBUGGER
105 DEVICE *VM::get_cpu(int index)
106 {
107         if(index == 0) {
108                 return cpu;
109         }
110         return NULL;
111 }
112 #endif
113
114 // ----------------------------------------------------------------------------
115 // drive virtual machine
116 // ----------------------------------------------------------------------------
117
118 void VM::reset()
119 {
120         // reset all devices
121         for(DEVICE* device = first_device; device; device = device->next_device) {
122                 device->reset();
123         }
124 }
125
126 void VM::run()
127 {
128         event->drive();
129 }
130
131 // ----------------------------------------------------------------------------
132 // draw screen
133 // ----------------------------------------------------------------------------
134
135 void VM::draw_screen()
136 {
137         vdp->draw_screen();
138 }
139
140 // ----------------------------------------------------------------------------
141 // soud manager
142 // ----------------------------------------------------------------------------
143
144 void VM::initialize_sound(int rate, int samples)
145 {
146         // init sound manager
147         event->initialize_sound(rate, samples);
148         
149         // init sound gen
150         psg->initialize_sound(rate, 3579545, 8000);
151 }
152
153 uint16_t* VM::create_sound(int* extra_frames)
154 {
155         return event->create_sound(extra_frames);
156 }
157
158 int VM::get_sound_buffer_ptr()
159 {
160         return event->get_sound_buffer_ptr();
161 }
162
163 #ifdef USE_SOUND_VOLUME
164 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
165 {
166         if(ch == 0) {
167                 psg->set_volume(0, decibel_l, decibel_r);
168         }
169 }
170 #endif
171
172 // ----------------------------------------------------------------------------
173 // user interface
174 // ----------------------------------------------------------------------------
175
176 void VM::open_cart(int drv, const _TCHAR* file_path)
177 {
178         if(drv == 0) {
179                 memory->open_cart(file_path);
180                 reset();
181         }
182 }
183
184 void VM::close_cart(int drv)
185 {
186         if(drv == 0) {
187                 memory->close_cart();
188                 reset();
189         }
190 }
191
192 bool VM::is_cart_inserted(int drv)
193 {
194         if(drv == 0) {
195                 return memory->is_cart_inserted();
196         } else {
197                 return false;
198         }
199 }
200
201 bool VM::is_frame_skippable()
202 {
203         return event->is_frame_skippable();
204 }
205
206 void VM::update_config()
207 {
208         for(DEVICE* device = first_device; device; device = device->next_device) {
209                 device->update_config();
210         }
211 }
212
213 #define STATE_VERSION   2
214
215 bool VM::process_state(FILEIO* state_fio, bool loading)
216 {
217         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
218                 return false;
219         }
220         for(DEVICE* device = first_device; device; device = device->next_device) {
221                 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
222                 // const char *name = typeid(*device).name();
223                 //       But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
224                 const char *name = device->get_device_name();
225                 int len = strlen(name);
226                 
227                 if(!state_fio->StateCheckInt32(len)) {
228                         if(loading) {
229                                 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
230                         }
231                         return false;
232                 }
233                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
234                         if(loading) {
235                                 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
236                         }
237                         return false;
238                 }
239                 if(!device->process_state(state_fio, loading)) {
240                         if(loading) {
241                                 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
242                         }
243                         return false;
244                 }
245         }
246         // Machine specified.
247 //      if(loading) {
248 //              update_config();
249 //      }
250         return true;
251 }