OSDN Git Service

a05a15d1743d56a42f1b2fe436904bf6667f9a16
[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         for(DEVICE* device = first_device; device; device = device->next_device) {
87                 device->initialize();
88         }
89         decl_state();
90 }
91
92 VM::~VM()
93 {
94         // delete all devices
95         for(DEVICE* device = first_device; device;) {
96                 DEVICE *next_device = device->next_device;
97                 device->release();
98                 delete device;
99                 device = next_device;
100         }
101 }
102
103 DEVICE* VM::get_device(int id)
104 {
105         for(DEVICE* device = first_device; device; device = device->next_device) {
106                 if(device->this_device_id == id) {
107                         return device;
108                 }
109         }
110         return NULL;
111 }
112
113 // ----------------------------------------------------------------------------
114 // drive virtual machine
115 // ----------------------------------------------------------------------------
116
117 void VM::reset()
118 {
119         // reset all devices
120         for(DEVICE* device = first_device; device; device = device->next_device) {
121                 device->reset();
122         }
123 }
124
125 void VM::run()
126 {
127         event->drive();
128 }
129
130 // ----------------------------------------------------------------------------
131 // debugger
132 // ----------------------------------------------------------------------------
133
134 #ifdef USE_DEBUGGER
135 DEVICE *VM::get_cpu(int index)
136 {
137         if(index == 0) {
138                 return cpu;
139         }
140         return NULL;
141 }
142 #endif
143
144 // ----------------------------------------------------------------------------
145 // draw screen
146 // ----------------------------------------------------------------------------
147
148 void VM::draw_screen()
149 {
150         display->draw_screen();
151 }
152
153 // ----------------------------------------------------------------------------
154 // soud manager
155 // ----------------------------------------------------------------------------
156
157 void VM::initialize_sound(int rate, int samples)
158 {
159         // init sound manager
160         event->initialize_sound(rate, samples);
161         
162         // init sound gen
163 //      pcm->initialize_sound(rate, 8000);
164 }
165
166 uint16_t* VM::create_sound(int* extra_frames)
167 {
168         return event->create_sound(extra_frames);
169 }
170
171 int VM::get_sound_buffer_ptr()
172 {
173         return event->get_sound_buffer_ptr();
174 }
175
176 // ----------------------------------------------------------------------------
177 // user interface
178 // ----------------------------------------------------------------------------
179
180 void VM::load_binary(int drv, const _TCHAR* file_path)
181 {
182         if(drv == 0) {
183                 memory->read_image(file_path, ram, sizeof(ram));
184         }
185 }
186
187 void VM::save_binary(int drv, const _TCHAR* file_path)
188 {
189         if(drv == 0) {
190                 memory->write_image(file_path, ram, sizeof(ram));
191         }
192 }
193
194 bool VM::is_frame_skippable()
195 {
196         return event->is_frame_skippable();
197 }
198
199 void VM::update_config()
200 {
201         for(DEVICE* device = first_device; device; device = device->next_device) {
202                 device->update_config();
203         }
204 }
205
206 #define STATE_VERSION   2
207
208 #include "../../statesub.h"
209 #include "../../qt/gui/csp_logger.h"
210 extern CSP_Logger DLL_PREFIX_I *csp_logger;
211
212 void VM::decl_state(void)
213 {
214
215         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::YS_6464A_HEAD")), csp_logger);
216         DECL_STATE_ENTRY_1D_ARRAY(ram, sizeof(ram));
217         for(DEVICE* device = first_device; device; device = device->next_device) {
218                 device->decl_state();
219         }
220 }
221
222 void VM::save_state(FILEIO* state_fio)
223 {
224         //state_fio->FputUint32(STATE_VERSION);
225         
226         if(state_entry != NULL) {
227                 state_entry->save_state(state_fio);
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 }
234
235 bool VM::load_state(FILEIO* state_fio)
236 {
237         //if(state_fio->FgetUint32() != STATE_VERSION) {
238         //      return false;
239         //}
240         bool mb = false;
241         if(state_entry != NULL) {
242                 mb = state_entry->load_state(state_fio);
243         }
244         if(!mb) {
245                 emu->out_debug_log("INFO: HEADER DATA ERROR");
246                 return false;
247         }
248         for(DEVICE* device = first_device; device; device = device->next_device) {
249                 if(!device->load_state(state_fio)) {
250                         return false;
251                 }
252         }
253         //state_fio->Fread(ram, sizeof(ram), 1);
254         return true;
255 }
256