OSDN Git Service

[VM][General] Merge upstream 2016-02-13. Still don't implement OSD/Gui part of joysti...
[csp-qt/common_source_project-fm7.git] / source / src / vm / x07 / x07.cpp
1 /*
2         CANON X-07 Emulator 'eX-07'
3
4         Author : Takeda.Toshiya
5         Date   : 2007.12.26 -
6
7         [ virtual machine ]
8 */
9
10 #include "x07.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../beep.h"
16 #include "../memory.h"
17 #include "../z80.h"
18
19 #ifdef USE_DEBUGGER
20 #include "../debugger.h"
21 #endif
22
23 #include "io.h"
24
25 // ----------------------------------------------------------------------------
26 // initialize
27 // ----------------------------------------------------------------------------
28
29 VM::VM(EMU* parent_emu) : emu(parent_emu)
30 {
31         // create devices
32         first_device = last_device = NULL;
33         dummy = new DEVICE(this, emu);  // must be 1st device
34         event = new EVENT(this, emu);   // must be 2nd device
35         
36         beep = new BEEP(this, emu);
37         memory = new MEMORY(this, emu);
38         cpu = new Z80(this, emu);
39         
40         io = new IO(this, emu);
41         
42         // set contexts
43         event->set_context_cpu(cpu);
44         event->set_context_sound(beep);
45         
46         io->set_context_beep(beep);
47         io->set_context_cpu(cpu);
48         io->set_context_mem(memory, ram);
49         
50         // memory bus
51         memset(ram, 0, sizeof(ram));
52         memset(app, 0xff, sizeof(app));
53         memset(tv, 0xff, sizeof(tv));
54         memset(bas, 0xff, sizeof(bas));
55         
56         memory->read_bios(_T("APP.ROM"), app, sizeof(app));
57         memory->read_bios(_T("TV.ROM"), tv, sizeof(tv));
58         memory->read_bios(_T("BASIC.ROM"), bas, sizeof(bas));
59         
60         memory->set_memory_rw(0x0000, 0x5fff, ram);
61         memory->set_memory_r(0x6000, 0x7fff, app);
62         memory->set_memory_rw(0x8000, 0x97ff, vram);
63         memory->set_memory_r(0xa000, 0xafff, tv);
64         memory->set_memory_r(0xb000, 0xffff, bas);
65         
66         // cpu bus
67         cpu->set_context_mem(memory);
68         cpu->set_context_io(io);
69         cpu->set_context_intr(io);
70 #ifdef USE_DEBUGGER
71         cpu->set_context_debugger(new DEBUGGER(this, emu));
72 #endif
73         
74         // initialize all devices
75         for(DEVICE* device = first_device; device; device = device->next_device) {
76                 device->initialize();
77         }
78 }
79
80 VM::~VM()
81 {
82         // delete all devices
83         for(DEVICE* device = first_device; device;) {
84                 DEVICE *next_device = device->next_device;
85                 device->release();
86                 delete device;
87                 device = next_device;
88         }
89 }
90
91 DEVICE* VM::get_device(int id)
92 {
93         for(DEVICE* device = first_device; device; device = device->next_device) {
94                 if(device->this_device_id == id) {
95                         return device;
96                 }
97         }
98         return NULL;
99 }
100
101 // ----------------------------------------------------------------------------
102 // drive virtual machine
103 // ----------------------------------------------------------------------------
104
105 void VM::reset()
106 {
107         // reset all devices
108         for(DEVICE* device = first_device; device; device = device->next_device) {
109                 device->reset();
110         }
111 }
112
113 void VM::run()
114 {
115         event->drive();
116 }
117
118 // ----------------------------------------------------------------------------
119 // debugger
120 // ----------------------------------------------------------------------------
121
122 #ifdef USE_DEBUGGER
123 DEVICE *VM::get_cpu(int index)
124 {
125         if(index == 0) {
126                 return cpu;
127         }
128         return NULL;
129 }
130 #endif
131
132 // ----------------------------------------------------------------------------
133 // draw screen
134 // ----------------------------------------------------------------------------
135
136 void VM::draw_screen()
137 {
138         io->draw_screen();
139 }
140
141 // ----------------------------------------------------------------------------
142 // soud manager
143 // ----------------------------------------------------------------------------
144
145 void VM::initialize_sound(int rate, int samples)
146 {
147         // init sound manager
148         event->initialize_sound(rate, samples);
149         
150         // init sound gen
151         beep->init(rate, 1000, 8000);
152 }
153
154 uint16* VM::create_sound(int* extra_frames)
155 {
156         return event->create_sound(extra_frames);
157 }
158
159 int VM::sound_buffer_ptr()
160 {
161         return event->sound_buffer_ptr();
162 }
163
164 #ifdef USE_SOUND_VOLUME
165 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
166 {
167         if(ch == 0) {
168                 beep->set_volume(0, decibel_l, decibel_r);
169         }
170 }
171 #endif
172
173 // ----------------------------------------------------------------------------
174 // notify key
175 // ----------------------------------------------------------------------------
176
177 void VM::key_down(int code, bool repeat)
178 {
179         io->key_down(code);
180 }
181
182 void VM::key_up(int code)
183 {
184         io->key_up(code);
185 }
186
187 // ----------------------------------------------------------------------------
188 // user interface
189 // ----------------------------------------------------------------------------
190
191 void VM::play_tape(const _TCHAR* file_path)
192 {
193         io->play_tape(file_path);
194 }
195
196 void VM::rec_tape(const _TCHAR* file_path)
197 {
198         io->rec_tape(file_path);
199 }
200
201 void VM::close_tape()
202 {
203         io->close_tape();
204 }
205
206 bool VM::tape_inserted()
207 {
208         return io->tape_inserted();
209 }
210
211 bool VM::now_skip()
212 {
213         return event->now_skip();
214 }
215
216 void VM::update_config()
217 {
218         for(DEVICE* device = first_device; device; device = device->next_device) {
219                 device->update_config();
220         }
221 }
222
223 #define STATE_VERSION   1
224
225 void VM::save_state(FILEIO* state_fio)
226 {
227         state_fio->FputUint32(STATE_VERSION);
228         
229         for(DEVICE* device = first_device; device; device = device->next_device) {
230                 device->save_state(state_fio);
231         }
232         state_fio->Fwrite(ram, sizeof(ram), 1);
233         state_fio->Fwrite(vram, sizeof(vram), 1);
234 }
235
236 bool VM::load_state(FILEIO* state_fio)
237 {
238         if(state_fio->FgetUint32() != STATE_VERSION) {
239                 return false;
240         }
241         for(DEVICE* device = first_device; device; device = device->next_device) {
242                 if(!device->load_state(state_fio)) {
243                         return false;
244                 }
245         }
246         state_fio->Fread(ram, sizeof(ram), 1);
247         state_fio->Fread(vram, sizeof(vram), 1);
248         return true;
249 }
250