OSDN Git Service

[VM][STATE] Remove typeid() from state data, this seems to be buggy.
[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 }
92
93 VM::~VM()
94 {
95         // delete all devices
96         for(DEVICE* device = first_device; device;) {
97                 DEVICE *next_device = device->next_device;
98                 device->release();
99                 delete device;
100                 device = next_device;
101         }
102 }
103
104 DEVICE* VM::get_device(int id)
105 {
106         for(DEVICE* device = first_device; device; device = device->next_device) {
107                 if(device->this_device_id == id) {
108                         return device;
109                 }
110         }
111         return NULL;
112 }
113
114 // ----------------------------------------------------------------------------
115 // drive virtual machine
116 // ----------------------------------------------------------------------------
117
118 void VM::reset()
119 {
120         // reset all devices
121         for(DEVICE* device = first_device; device; device = device->next_device) {
122                 device->reset();
123         }
124         cpu->write_signal(SIG_MC6801_PORT_1, 0x00, 0xff);
125         cpu->write_signal(SIG_MC6801_PORT_2, 0x00, 0xff);
126         cpu->write_signal(SIG_MC6801_PORT_3, 0x00, 0xff);
127         cpu->write_signal(SIG_MC6801_PORT_4, 0x00, 0xff);
128 }
129
130 void VM::run()
131 {
132         event->drive();
133 }
134
135 // ----------------------------------------------------------------------------
136 // debugger
137 // ----------------------------------------------------------------------------
138
139 #ifdef USE_DEBUGGER
140 DEVICE *VM::get_cpu(int index)
141 {
142         if(index == 0) {
143                 return cpu;
144         }
145         return NULL;
146 }
147 #endif
148
149 // ----------------------------------------------------------------------------
150 // draw screen
151 // ----------------------------------------------------------------------------
152
153 void VM::draw_screen()
154 {
155         for(int y = 0; y < 2; y++) {
156                 for(int x = 0; x < 4; x++) {
157                         lcd[x + 4 * y]->screen_update(50 * x - 4, 32 * y, y == 0);
158                 }
159         }
160 }
161
162 // ----------------------------------------------------------------------------
163 // soud manager
164 // ----------------------------------------------------------------------------
165
166 void VM::initialize_sound(int rate, int samples)
167 {
168         // init sound manager
169         event->initialize_sound(rate, samples);
170         
171         // init sound gen
172         pcm->initialize_sound(rate, 8000);
173 }
174
175 uint16_t* VM::create_sound(int* extra_frames)
176 {
177         return event->create_sound(extra_frames);
178 }
179
180 int VM::get_sound_buffer_ptr()
181 {
182         return event->get_sound_buffer_ptr();
183 }
184
185 #ifdef USE_SOUND_VOLUME
186 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
187 {
188         if(ch == 0) {
189                 pcm->set_volume(0, decibel_l, decibel_r);
190         } if(ch == 1) {
191                 drec->set_volume(0, decibel_l, decibel_r);
192         } else if(ch == 2) {
193                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
194                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
195                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
196         }
197 }
198 #endif
199
200 // ----------------------------------------------------------------------------
201 // user interface
202 // ----------------------------------------------------------------------------
203
204 void VM::play_tape(int drv, const _TCHAR* file_path)
205 {
206         drec->play_tape(file_path);
207 }
208
209 void VM::rec_tape(int drv, const _TCHAR* file_path)
210 {
211         drec->rec_tape(file_path);
212 }
213
214 void VM::close_tape(int drv)
215 {
216         emu->lock_vm();
217         drec->close_tape();
218         emu->unlock_vm();
219 }
220
221 bool VM::is_tape_inserted(int drv)
222 {
223         return drec->is_tape_inserted();
224 }
225
226 bool VM::is_tape_playing(int drv)
227 {
228         return drec->is_tape_playing();
229 }
230
231 bool VM::is_tape_recording(int drv)
232 {
233         return drec->is_tape_recording();
234 }
235
236 int VM::get_tape_position(int drv)
237 {
238         return drec->get_tape_position();
239 }
240
241 const _TCHAR* VM::get_tape_message(int drv)
242 {
243         return drec->get_message();
244 }
245
246 bool VM::is_frame_skippable()
247 {
248         return event->is_frame_skippable();
249 }
250
251 void VM::update_config()
252 {
253         for(DEVICE* device = first_device; device; device = device->next_device) {
254                 device->update_config();
255         }
256 }
257
258 #define STATE_VERSION   3
259
260 void VM::save_state(FILEIO* state_fio)
261 {
262         state_fio->FputUint32(STATE_VERSION);
263         
264         for(DEVICE* device = first_device; device; device = device->next_device) {
265                 device->save_state(state_fio);
266         }
267         state_fio->Fwrite(ram, sizeof(ram), 1);
268 }
269
270 bool VM::load_state(FILEIO* state_fio)
271 {
272         if(state_fio->FgetUint32() != STATE_VERSION) {
273                 return false;
274         }
275         for(DEVICE* device = first_device; device; device = device->next_device) {
276                 if(!device->load_state(state_fio)) {
277                         return false;
278                 }
279         }
280         state_fio->Fread(ram, sizeof(ram), 1);
281         return true;
282 }
283