OSDN Git Service

[BUILD] Set SOVERSION and GIT hash automatically.
[csp-qt/common_source_project-fm7.git] / source / src / vm / babbage2nd / babbage2nd.cpp
1 /*
2         Gijutsu-Hyoron-Sha Babbage-2nd Emulator 'eBabbage-2nd'
3
4         Author : Takeda.Toshiya
5         Date   : 2009.12.26 -
6
7         [ virtual machine ]
8 */
9
10 #include "babbage2nd.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../io.h"
16 #include "../memory.h"
17 #include "../z80.h"
18 #include "../z80ctc.h"
19 #include "../z80pio.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         
39         io = new IO(this, emu);
40         memory = new MEMORY(this, emu);
41         cpu = new Z80(this, emu);
42         ctc = new Z80CTC(this, emu);
43         pio1 = new Z80PIO(this, emu);
44         pio1->set_device_name(_T("Z80 PIO (LEDs)"));
45         pio2 = new Z80PIO(this, emu);
46         pio2->set_device_name(_T("Z80 PIO (7-Seg/Keyboard)"));
47         
48         display = new DISPLAY(this, emu);
49         keyboard = new KEYBOARD(this, emu);
50
51         // Set names
52 #if defined(_USE_QT)
53         dummy->set_device_name(_T("1st Dummy"));
54         pio1->set_device_name(_T("Z80 PIO(LEDs)"));
55         pio2->set_device_name(_T("Z80 PIO(7SEG/KEYBOARD)"));
56 #endif
57         // set contexts
58         event->set_context_cpu(cpu);
59         
60         pio2->set_context_port_b(display, SIG_DISPLAY_7SEG_LED, 0xff, 0);
61         keyboard->set_context_pio(pio2);
62         // p.145, fig.3-4
63         ctc->set_context_zc2(ctc, SIG_Z80CTC_TRIG_1, 1);
64         ctc->set_context_zc1(ctc, SIG_Z80CTC_TRIG_0, 1);
65         // p.114, fig.2-52
66         pio1->set_context_port_b(display, SIG_DISPLAY_8BIT_LED, 0xff, 0);
67         //pio1->set_context_port_b(pio1, SIG_Z80PIO_PORT_A, 0xff, 0);
68         
69         // cpu bus
70         cpu->set_context_mem(memory);
71         cpu->set_context_io(io);
72         cpu->set_context_intr(ctc);
73 #ifdef USE_DEBUGGER
74         cpu->set_context_debugger(new DEBUGGER(this, emu));
75 #endif
76         
77         // z80 family daisy chain
78         ctc->set_context_intr(cpu, 0);
79         ctc->set_context_child(pio1);
80         pio1->set_context_intr(cpu, 1);
81         pio1->set_context_child(pio2);
82         pio2->set_context_intr(cpu, 2);
83         
84         // memory bus
85         memset(ram, 0, sizeof(ram));
86         memset(rom, 0xff, sizeof(rom));
87         
88         memory->read_bios(_T("MON.ROM"), rom, sizeof(rom));
89         
90         memory->set_memory_r(0x0000, 0x07ff, rom);
91         memory->set_memory_rw(0x1000, 0x17ff, ram);
92         
93         // i/o bus
94         io->set_iomap_range_rw(0x00, 0x03, ctc);
95         io->set_iomap_range_rw(0x10, 0x13, pio1);
96         io->set_iomap_range_rw(0x20, 0x23, pio2);
97         
98         // initialize all devices
99 #if defined(__GIT_REPO_VERSION)
100         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
101 #endif
102         for(DEVICE* device = first_device; device; device = device->next_device) {
103                 device->initialize();
104         }
105         decl_state();
106 }
107
108 VM::~VM()
109 {
110         // delete all devices
111         for(DEVICE* device = first_device; device;) {
112                 DEVICE *next_device = device->next_device;
113                 device->release();
114                 delete device;
115                 device = next_device;
116         }
117 }
118
119 DEVICE* VM::get_device(int id)
120 {
121         for(DEVICE* device = first_device; device; device = device->next_device) {
122                 if(device->this_device_id == id) {
123                         return device;
124                 }
125         }
126         return NULL;
127 }
128
129 // ----------------------------------------------------------------------------
130 // drive virtual machine
131 // ----------------------------------------------------------------------------
132
133 void VM::reset()
134 {
135         // reset all devices
136         for(DEVICE* device = first_device; device; device = device->next_device) {
137                 device->reset();
138         }
139 }
140
141 void VM::run()
142 {
143         event->drive();
144 }
145
146 // ----------------------------------------------------------------------------
147 // debugger
148 // ----------------------------------------------------------------------------
149
150 #ifdef USE_DEBUGGER
151 DEVICE *VM::get_cpu(int index)
152 {
153         if(index == 0) {
154                 return cpu;
155         }
156         return NULL;
157 }
158 #endif
159
160 // ----------------------------------------------------------------------------
161 // draw screen
162 // ----------------------------------------------------------------------------
163
164 void VM::draw_screen()
165 {
166         display->draw_screen();
167 }
168
169 // ----------------------------------------------------------------------------
170 // soud manager
171 // ----------------------------------------------------------------------------
172
173 void VM::initialize_sound(int rate, int samples)
174 {
175         // init sound manager
176         event->initialize_sound(rate, samples);
177 }
178
179 uint16_t* VM::create_sound(int* extra_frames)
180 {
181         return event->create_sound(extra_frames);
182 }
183
184 int VM::get_sound_buffer_ptr()
185 {
186         return event->get_sound_buffer_ptr();
187 }
188
189 // ----------------------------------------------------------------------------
190 // notify key
191 // ----------------------------------------------------------------------------
192
193 void VM::key_down(int code, bool repeat)
194 {
195         keyboard->key_down(code);
196 }
197
198 void VM::key_up(int code)
199 {
200         //keyboard->key_up(code);
201 }
202
203 // ----------------------------------------------------------------------------
204 // user interface
205 // ----------------------------------------------------------------------------
206
207 void VM::load_binary(int drv, const _TCHAR* file_path)
208 {
209         if(drv == 0) {
210                 memory->read_image(file_path, ram, sizeof(ram));
211         }
212 }
213
214 void VM::save_binary(int drv, const _TCHAR* file_path)
215 {
216         if(drv == 0) {
217                 memory->write_image(file_path, ram, sizeof(ram));
218         }
219 }
220
221 bool VM::is_frame_skippable()
222 {
223         return event->is_frame_skippable();
224 }
225
226 void VM::update_config()
227 {
228         for(DEVICE* device = first_device; device; device = device->next_device) {
229                 device->update_config();
230         }
231 }
232
233 #define STATE_VERSION   2
234
235 #include "../../statesub.h"
236 #include "../../qt/gui/csp_logger.h"
237 extern CSP_Logger DLL_PREFIX_I *csp_logger;
238
239 void VM::decl_state(void)
240 {
241         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::BABBAGE2ND_HEAD")), csp_logger);
242         DECL_STATE_ENTRY_MULTI(void, ram, sizeof(ram));
243         for(DEVICE* device = first_device; device; device = device->next_device) {
244                 device->decl_state();
245         }
246 }
247
248 void VM::save_state(FILEIO* state_fio)
249 {
250         //state_fio->FputUint32(STATE_VERSION);
251         
252         if(state_entry != NULL) {
253                 state_entry->save_state(state_fio);
254         }
255         for(DEVICE* device = first_device; device; device = device->next_device) {
256                 device->save_state(state_fio);
257         }
258         //state_fio->Fwrite(ram, sizeof(ram), 1);
259 }
260
261 bool VM::load_state(FILEIO* state_fio)
262 {
263         bool mb = false;
264         if(state_entry != NULL) {
265                 mb = state_entry->load_state(state_fio);
266         }
267         if(!mb) {
268                 emu->out_debug_log("INFO: HEADER DATA ERROR");
269                 return false;
270         }
271         for(DEVICE* device = first_device; device; device = device->next_device) {
272                 if(!device->load_state(state_fio)) {
273                         return false;
274                 }
275         }
276         return true;
277 }
278