OSDN Git Service

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