OSDN Git Service

[VM][STATE][WIP] Apply csp_state_utils:: to some VMs.This is WIP.
[csp-qt/common_source_project-fm7.git] / source / src / vm / jr800 / jr800.cpp
1 /*
2         National JR-800 Emulator 'eJR-800'
3
4         Author : Takeda.Toshiya
5         Date   : 2017.03.13-
6
7         [ virtual machine ]
8 */
9
10 #include "jr800.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../datarec.h"
16 #include "../hd44102.h"
17 //#include "../mc6800.h"
18 #include "../hd6301.h"
19 #include "../memory.h"
20 #include "../noise.h"
21 #include "../pcm1bit.h"
22
23 #ifdef USE_DEBUGGER
24 #include "../debugger.h"
25 #endif
26
27 #include "io.h"
28
29 // ----------------------------------------------------------------------------
30 // initialize
31 // ----------------------------------------------------------------------------
32
33 VM::VM(EMU* parent_emu) : emu(parent_emu)
34 {
35         // create devices
36         first_device = last_device = NULL;
37         dummy = new DEVICE(this, emu);  // must be 1st device
38         event = new EVENT(this, emu);   // must be 2nd device
39         
40         drec = new DATAREC(this, emu);
41         drec->set_context_noise_play(new NOISE(this, emu));
42         drec->set_context_noise_stop(new NOISE(this, emu));
43         drec->set_context_noise_fast(new NOISE(this, emu));
44         for(int i = 0; i < 8; i++) {
45                 lcd[i] = new HD44102(this, emu);
46         }
47 //      cpu = new MC6800(this, emu);
48         cpu = new HD6301(this, emu);
49         memory = new MEMORY(this, emu);
50         pcm = new PCM1BIT(this, emu);
51         
52         io = new IO(this, emu);
53         
54         // set contexts
55         event->set_context_cpu(cpu);
56         event->set_context_sound(drec);
57         event->set_context_sound(pcm);
58         event->set_context_sound(drec->get_context_noise_play());
59         event->set_context_sound(drec->get_context_noise_stop());
60         event->set_context_sound(drec->get_context_noise_fast());
61         
62 //      cpu->set_context_port1(drec, SIG_DATAREC_MIC, 0x01, 0);
63         cpu->set_context_port1(pcm, SIG_PCM1BIT_ON, 0x08, 0);
64         cpu->set_context_port1(pcm, SIG_PCM1BIT_SIGNAL, 0x10, 0);
65         
66         for(int i = 0; i < 8; i++) {
67                 io->set_context_lcd(i, lcd[i]);
68         }
69         
70         // cpu bus
71         cpu->set_context_mem(memory);
72 #ifdef USE_DEBUGGER
73         cpu->set_context_debugger(new DEBUGGER(this, emu));
74 #endif
75         
76         // memory bus
77         memset(ram, 0x00, sizeof(ram));
78         memset(rom, 0xff, sizeof(rom));
79         
80         memory->read_bios(_T("BASIC.ROM"), rom, sizeof(rom));
81         
82         memory->set_memory_rw(0x2000, 0x7fff, ram);
83         memory->set_memory_r(0x8000, 0xffff, rom);
84         memory->set_memory_mapped_io_rw(0x0a00, 0x0bff, io);
85         memory->set_memory_mapped_io_rw(0x0d00, 0x0fff, io);
86         
87         // initialize all devices
88         for(DEVICE* device = first_device; device; device = device->next_device) {
89                 device->initialize();
90         }
91         decl_state();
92 }
93
94 VM::~VM()
95 {
96         // delete all devices
97         for(DEVICE* device = first_device; device;) {
98                 DEVICE *next_device = device->next_device;
99                 device->release();
100                 delete device;
101                 device = next_device;
102         }
103 }
104
105 DEVICE* VM::get_device(int id)
106 {
107         for(DEVICE* device = first_device; device; device = device->next_device) {
108                 if(device->this_device_id == id) {
109                         return device;
110                 }
111         }
112         return NULL;
113 }
114
115 // ----------------------------------------------------------------------------
116 // drive virtual machine
117 // ----------------------------------------------------------------------------
118
119 void VM::reset()
120 {
121         // reset all devices
122         for(DEVICE* device = first_device; device; device = device->next_device) {
123                 device->reset();
124         }
125         cpu->write_signal(SIG_MC6801_PORT_1, 0x00, 0xff);
126         cpu->write_signal(SIG_MC6801_PORT_2, 0x00, 0xff);
127         cpu->write_signal(SIG_MC6801_PORT_3, 0x00, 0xff);
128         cpu->write_signal(SIG_MC6801_PORT_4, 0x00, 0xff);
129 }
130
131 void VM::run()
132 {
133         event->drive();
134 }
135
136 // ----------------------------------------------------------------------------
137 // debugger
138 // ----------------------------------------------------------------------------
139
140 #ifdef USE_DEBUGGER
141 DEVICE *VM::get_cpu(int index)
142 {
143         if(index == 0) {
144                 return cpu;
145         }
146         return NULL;
147 }
148 #endif
149
150 // ----------------------------------------------------------------------------
151 // draw screen
152 // ----------------------------------------------------------------------------
153
154 void VM::draw_screen()
155 {
156         for(int y = 0; y < 2; y++) {
157                 for(int x = 0; x < 4; x++) {
158                         lcd[x + 4 * y]->screen_update(50 * x - 4, 32 * y, y == 0);
159                 }
160         }
161 }
162
163 // ----------------------------------------------------------------------------
164 // soud manager
165 // ----------------------------------------------------------------------------
166
167 void VM::initialize_sound(int rate, int samples)
168 {
169         // init sound manager
170         event->initialize_sound(rate, samples);
171         
172         // init sound gen
173         pcm->initialize_sound(rate, 8000);
174 }
175
176 uint16_t* VM::create_sound(int* extra_frames)
177 {
178         return event->create_sound(extra_frames);
179 }
180
181 int VM::get_sound_buffer_ptr()
182 {
183         return event->get_sound_buffer_ptr();
184 }
185
186 #ifdef USE_SOUND_VOLUME
187 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
188 {
189         if(ch == 0) {
190                 pcm->set_volume(0, decibel_l, decibel_r);
191         } if(ch == 1) {
192                 drec->set_volume(0, decibel_l, decibel_r);
193         } else if(ch == 2) {
194                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
195                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
196                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
197         }
198 }
199 #endif
200
201 // ----------------------------------------------------------------------------
202 // user interface
203 // ----------------------------------------------------------------------------
204
205 void VM::play_tape(int drv, const _TCHAR* file_path)
206 {
207         drec->play_tape(file_path);
208 //      drec->set_remote(true);
209 }
210
211 void VM::rec_tape(int drv, const _TCHAR* file_path)
212 {
213         drec->rec_tape(file_path);
214 //      drec->set_remote(true);
215 }
216
217 void VM::close_tape(int drv)
218 {
219         emu->lock_vm();
220         drec->close_tape();
221         emu->unlock_vm();
222 //      drec->set_remote(false);
223 }
224
225 bool VM::is_tape_inserted(int drv)
226 {
227         return drec->is_tape_inserted();
228 }
229
230 bool VM::is_tape_playing(int drv)
231 {
232         return drec->is_tape_playing();
233 }
234
235 bool VM::is_tape_recording(int drv)
236 {
237         return drec->is_tape_recording();
238 }
239
240 int VM::get_tape_position(int drv)
241 {
242         return drec->get_tape_position();
243 }
244
245 const _TCHAR* VM::get_tape_message(int drv)
246 {
247         return drec->get_message();
248 }
249
250 void VM::push_play(int drv)
251 {
252         drec->set_ff_rew(0);
253         drec->set_remote(true);
254 }
255
256 void VM::push_stop(int drv)
257 {
258         drec->set_remote(false);
259 }
260
261 void VM::push_fast_forward(int drv)
262 {
263         drec->set_ff_rew(1);
264         drec->set_remote(true);
265 }
266
267 void VM::push_fast_rewind(int drv)
268 {
269         drec->set_ff_rew(-1);
270         drec->set_remote(true);
271 }
272
273 bool VM::is_frame_skippable()
274 {
275         return event->is_frame_skippable();
276 }
277
278 void VM::update_config()
279 {
280         for(DEVICE* device = first_device; device; device = device->next_device) {
281                 device->update_config();
282         }
283 }
284
285 #define STATE_VERSION   3
286
287 #include "../../statesub.h"
288
289 void VM::decl_state(void)
290 {
291         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::JR_800_HEAD")));
292         DECL_STATE_ENTRY_MULTI(void, ram, sizeof(ram));
293         for(DEVICE* device = first_device; device; device = device->next_device) {
294                 device->decl_state();
295         }
296 }
297
298 void VM::save_state(FILEIO* state_fio)
299 {
300         //state_fio->FputUint32(STATE_VERSION);
301         if(state_entry != NULL) {
302                 state_entry->save_state(state_fio);
303         }
304         
305         for(DEVICE* device = first_device; device; device = device->next_device) {
306                 device->save_state(state_fio);
307         }
308         //state_fio->Fwrite(ram, sizeof(ram), 1);
309 }
310
311 bool VM::load_state(FILEIO* state_fio)
312 {
313         //if(state_fio->FgetUint32() != STATE_VERSION) {
314         //      return false;
315         //}
316         bool mb = false;
317         if(state_entry != NULL) {
318                 mb = state_entry->load_state(state_fio);
319         }
320         if(!mb) {
321                 emu->out_debug_log("INFO: HEADER DATA ERROR");
322                 return false;
323         }
324         for(DEVICE* device = first_device; device; device = device->next_device) {
325                 if(!device->load_state(state_fio)) {
326                         return false;
327                 }
328         }
329         //state_fio->Fread(ram, sizeof(ram), 1);
330         return true;
331 }
332