OSDN Git Service

[BUILD] Set SOVERSION and GIT hash automatically.
[csp-qt/common_source_project-fm7.git] / source / src / vm / ys6464a / ys6464a.cpp
1 /*
2         SHINKO SANGYO YS-6464A Emulator 'eYS-6464A'
3
4         Author : Takeda.Toshiya
5         Date   : 2009.12.30 -
6
7         [ virtual machine ]
8 */
9
10 #include "ys6464a.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../io.h"
16 #include "../i8255.h"
17 #include "../memory.h"
18 #include "../pcm1bit.h"
19 #include "../z80.h"
20
21 #ifdef USE_DEBUGGER
22 #include "../debugger.h"
23 #endif
24
25 #include "display.h"
26 #include "keyboard.h"
27
28 // ----------------------------------------------------------------------------
29 // initialize
30 // ----------------------------------------------------------------------------
31
32 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
33 {
34         // create devices
35         first_device = last_device = NULL;
36         dummy = new DEVICE(this, emu);  // must be 1st device
37         event = new EVENT(this, emu);   // must be 2nd device
38         dummy->set_device_name(_T("1st Dummy"));
39         io = new IO(this, emu);
40         pio = new I8255(this, emu);
41         memory = new MEMORY(this, emu);
42 //      pcm = new PCM1BIT(this, emu);
43         cpu = new Z80(this, emu);
44         
45         display = new DISPLAY(this, emu);
46         keyboard = new KEYBOARD(this, emu);
47         
48         // set contexts
49         event->set_context_cpu(cpu);
50 //      event->set_context_sound(pcm);
51         
52 //      pio->set_context_port_b(pcm, SIG_PCM1BIT_SIGNAL, 0x01, 0);
53         pio->set_context_port_b(display, SIG_DISPLAY_PORT_B, 0xf0, 0);
54         pio->set_context_port_c(display, SIG_DISPLAY_PORT_C, 0xf0, 0);
55         pio->set_context_port_c(keyboard, SIG_KEYBOARD_PORT_C, 0xf0, 0);
56         keyboard->set_context_pio(pio);
57         
58         // cpu bus
59         cpu->set_context_mem(memory);
60         cpu->set_context_io(io);
61         cpu->set_context_intr(dummy);
62 #ifdef USE_DEBUGGER
63         cpu->set_context_debugger(new DEBUGGER(this, emu));
64 #endif
65         
66         // memory bus
67         memset(ram, 0, sizeof(ram));
68         memset(rom, 0xff, sizeof(rom));
69         
70         memory->read_bios(_T("MON.ROM"), rom, sizeof(rom));
71         
72         memory->set_memory_r(0x0000, 0x1fff, rom);
73         memory->set_memory_r(0x2000, 0x3fff, rom);
74         memory->set_memory_r(0x4000, 0x5fff, rom);
75         memory->set_memory_r(0x6000, 0x7fff, rom);
76         memory->set_memory_rw(0x8000, 0x9fff, ram);
77         memory->set_memory_rw(0xa000, 0xbfff, ram);
78         memory->set_memory_rw(0xc000, 0xdfff, ram);
79         memory->set_memory_rw(0xe000, 0xffff, ram);
80         
81         // i/o bus
82         io->set_iomap_range_w(0xf8, 0xfb, pio);
83         io->set_iomap_range_r(0xf8, 0xfb, pio);
84         
85         // initialize all devices
86 #if defined(__GIT_REPO_VERSION)
87         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
88 #endif
89         for(DEVICE* device = first_device; device; device = device->next_device) {
90                 device->initialize();
91         }
92         decl_state();
93 }
94
95 VM::~VM()
96 {
97         // delete all devices
98         for(DEVICE* device = first_device; device;) {
99                 DEVICE *next_device = device->next_device;
100                 device->release();
101                 delete device;
102                 device = next_device;
103         }
104 }
105
106 DEVICE* VM::get_device(int id)
107 {
108         for(DEVICE* device = first_device; device; device = device->next_device) {
109                 if(device->this_device_id == id) {
110                         return device;
111                 }
112         }
113         return NULL;
114 }
115
116 // ----------------------------------------------------------------------------
117 // drive virtual machine
118 // ----------------------------------------------------------------------------
119
120 void VM::reset()
121 {
122         // reset all devices
123         for(DEVICE* device = first_device; device; device = device->next_device) {
124                 device->reset();
125         }
126 }
127
128 void VM::run()
129 {
130         event->drive();
131 }
132
133 // ----------------------------------------------------------------------------
134 // debugger
135 // ----------------------------------------------------------------------------
136
137 #ifdef USE_DEBUGGER
138 DEVICE *VM::get_cpu(int index)
139 {
140         if(index == 0) {
141                 return cpu;
142         }
143         return NULL;
144 }
145 #endif
146
147 // ----------------------------------------------------------------------------
148 // draw screen
149 // ----------------------------------------------------------------------------
150
151 void VM::draw_screen()
152 {
153         display->draw_screen();
154 }
155
156 // ----------------------------------------------------------------------------
157 // soud manager
158 // ----------------------------------------------------------------------------
159
160 void VM::initialize_sound(int rate, int samples)
161 {
162         // init sound manager
163         event->initialize_sound(rate, samples);
164         
165         // init sound gen
166 //      pcm->initialize_sound(rate, 8000);
167 }
168
169 uint16_t* VM::create_sound(int* extra_frames)
170 {
171         return event->create_sound(extra_frames);
172 }
173
174 int VM::get_sound_buffer_ptr()
175 {
176         return event->get_sound_buffer_ptr();
177 }
178
179 // ----------------------------------------------------------------------------
180 // user interface
181 // ----------------------------------------------------------------------------
182
183 void VM::load_binary(int drv, const _TCHAR* file_path)
184 {
185         if(drv == 0) {
186                 memory->read_image(file_path, ram, sizeof(ram));
187         }
188 }
189
190 void VM::save_binary(int drv, const _TCHAR* file_path)
191 {
192         if(drv == 0) {
193                 memory->write_image(file_path, ram, sizeof(ram));
194         }
195 }
196
197 bool VM::is_frame_skippable()
198 {
199         return event->is_frame_skippable();
200 }
201
202 void VM::update_config()
203 {
204         for(DEVICE* device = first_device; device; device = device->next_device) {
205                 device->update_config();
206         }
207 }
208
209 #define STATE_VERSION   2
210
211 #include "../../statesub.h"
212 #include "../../qt/gui/csp_logger.h"
213 extern CSP_Logger DLL_PREFIX_I *csp_logger;
214
215 void VM::decl_state(void)
216 {
217
218         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::YS_6464A_HEAD")), csp_logger);
219         DECL_STATE_ENTRY_1D_ARRAY(ram, sizeof(ram));
220         for(DEVICE* device = first_device; device; device = device->next_device) {
221                 device->decl_state();
222         }
223 }
224
225 void VM::save_state(FILEIO* state_fio)
226 {
227         //state_fio->FputUint32(STATE_VERSION);
228         
229         if(state_entry != NULL) {
230                 state_entry->save_state(state_fio);
231         }
232         for(DEVICE* device = first_device; device; device = device->next_device) {
233                 device->save_state(state_fio);
234         }
235         //state_fio->Fwrite(ram, sizeof(ram), 1);
236 }
237
238 bool VM::load_state(FILEIO* state_fio)
239 {
240         //if(state_fio->FgetUint32() != STATE_VERSION) {
241         //      return false;
242         //}
243         bool mb = false;
244         if(state_entry != NULL) {
245                 mb = state_entry->load_state(state_fio);
246         }
247         if(!mb) {
248                 emu->out_debug_log("INFO: HEADER DATA ERROR");
249                 return false;
250         }
251         for(DEVICE* device = first_device; device; device = device->next_device) {
252                 if(!device->load_state(state_fio)) {
253                         return false;
254                 }
255         }
256         //state_fio->Fread(ram, sizeof(ram), 1);
257         return true;
258 }
259