OSDN Git Service

0fa2ad97c0e222d8f2d3591e53acdcf94b131b75
[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 using JR800::IO;
29
30 // ----------------------------------------------------------------------------
31 // initialize
32 // ----------------------------------------------------------------------------
33
34 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
35 {
36         // create devices
37         first_device = last_device = NULL;
38         dummy = new DEVICE(this, emu);  // must be 1st device
39         event = new EVENT(this, emu);   // must be 2nd device
40         
41         drec = new DATAREC(this, emu);
42         drec->set_context_noise_play(new NOISE(this, emu));
43         drec->set_context_noise_stop(new NOISE(this, emu));
44         drec->set_context_noise_fast(new NOISE(this, emu));
45         for(int i = 0; i < 8; i++) {
46                 lcd[i] = new HD44102(this, emu);
47         }
48 //      cpu = new MC6800(this, emu);
49         cpu = new HD6301(this, emu);
50         memory = new MEMORY(this, emu);
51         pcm = new PCM1BIT(this, emu);
52         
53         io = new IO(this, emu);
54         
55         // set contexts
56         event->set_context_cpu(cpu);
57         event->set_context_sound(drec);
58         event->set_context_sound(pcm);
59         event->set_context_sound(drec->get_context_noise_play());
60         event->set_context_sound(drec->get_context_noise_stop());
61         event->set_context_sound(drec->get_context_noise_fast());
62         
63 //      cpu->set_context_port1(drec, SIG_DATAREC_MIC, 0x01, 0);
64         cpu->set_context_port1(pcm, SIG_PCM1BIT_ON, 0x08, 0);
65         cpu->set_context_port1(pcm, SIG_PCM1BIT_SIGNAL, 0x10, 0);
66         
67         for(int i = 0; i < 8; i++) {
68                 io->set_context_lcd(i, lcd[i]);
69         }
70         
71         // cpu bus
72         cpu->set_context_mem(memory);
73 #ifdef USE_DEBUGGER
74         cpu->set_context_debugger(new DEBUGGER(this, emu));
75 #endif
76         
77         // memory bus
78         memset(ram, 0x00, sizeof(ram));
79         memset(rom, 0xff, sizeof(rom));
80         
81         memory->read_bios(_T("BASIC.ROM"), rom, sizeof(rom));
82         
83         memory->set_memory_rw(0x2000, 0x7fff, ram);
84         memory->set_memory_r(0x8000, 0xffff, rom);
85         memory->set_memory_mapped_io_rw(0x0a00, 0x0bff, io);
86         memory->set_memory_mapped_io_rw(0x0d00, 0x0fff, io);
87         
88         // initialize all devices
89 #if defined(__GIT_REPO_VERSION)
90         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
91 #endif
92         for(DEVICE* device = first_device; device; device = device->next_device) {
93                 device->initialize();
94         }
95 }
96
97 VM::~VM()
98 {
99         // delete all devices
100         for(DEVICE* device = first_device; device;) {
101                 DEVICE *next_device = device->next_device;
102                 device->release();
103                 delete device;
104                 device = next_device;
105         }
106 }
107
108 DEVICE* VM::get_device(int id)
109 {
110         for(DEVICE* device = first_device; device; device = device->next_device) {
111                 if(device->this_device_id == id) {
112                         return device;
113                 }
114         }
115         return NULL;
116 }
117
118 // ----------------------------------------------------------------------------
119 // drive virtual machine
120 // ----------------------------------------------------------------------------
121
122 void VM::reset()
123 {
124         // reset all devices
125         for(DEVICE* device = first_device; device; device = device->next_device) {
126                 device->reset();
127         }
128         cpu->write_signal(SIG_MC6801_PORT_1, 0x00, 0xff);
129         cpu->write_signal(SIG_MC6801_PORT_2, 0x00, 0xff);
130         cpu->write_signal(SIG_MC6801_PORT_3, 0x00, 0xff);
131         cpu->write_signal(SIG_MC6801_PORT_4, 0x00, 0xff);
132 }
133
134 void VM::run()
135 {
136         event->drive();
137 }
138
139 // ----------------------------------------------------------------------------
140 // debugger
141 // ----------------------------------------------------------------------------
142
143 #ifdef USE_DEBUGGER
144 DEVICE *VM::get_cpu(int index)
145 {
146         if(index == 0) {
147                 return cpu;
148         }
149         return NULL;
150 }
151 #endif
152
153 // ----------------------------------------------------------------------------
154 // draw screen
155 // ----------------------------------------------------------------------------
156
157 void VM::draw_screen()
158 {
159         for(int y = 0; y < 2; y++) {
160                 for(int x = 0; x < 4; x++) {
161                         lcd[x + 4 * y]->screen_update(50 * x - 4, 32 * y, y == 0);
162                 }
163         }
164 }
165
166 // ----------------------------------------------------------------------------
167 // soud manager
168 // ----------------------------------------------------------------------------
169
170 void VM::initialize_sound(int rate, int samples)
171 {
172         // init sound manager
173         event->initialize_sound(rate, samples);
174         
175         // init sound gen
176         pcm->initialize_sound(rate, 8000);
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 #ifdef USE_SOUND_VOLUME
190 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
191 {
192         if(ch == 0) {
193                 pcm->set_volume(0, decibel_l, decibel_r);
194         } if(ch == 1) {
195                 drec->set_volume(0, decibel_l, decibel_r);
196         } else if(ch == 2) {
197                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
198                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
199                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
200         }
201 }
202 #endif
203
204 // ----------------------------------------------------------------------------
205 // user interface
206 // ----------------------------------------------------------------------------
207
208 void VM::play_tape(int drv, const _TCHAR* file_path)
209 {
210         drec->play_tape(file_path);
211 //      drec->set_remote(true);
212 }
213
214 void VM::rec_tape(int drv, const _TCHAR* file_path)
215 {
216         drec->rec_tape(file_path);
217 //      drec->set_remote(true);
218 }
219
220 void VM::close_tape(int drv)
221 {
222         emu->lock_vm();
223         drec->close_tape();
224         emu->unlock_vm();
225 //      drec->set_remote(false);
226 }
227
228 bool VM::is_tape_inserted(int drv)
229 {
230         return drec->is_tape_inserted();
231 }
232
233 bool VM::is_tape_playing(int drv)
234 {
235         return drec->is_tape_playing();
236 }
237
238 bool VM::is_tape_recording(int drv)
239 {
240         return drec->is_tape_recording();
241 }
242
243 int VM::get_tape_position(int drv)
244 {
245         return drec->get_tape_position();
246 }
247
248 const _TCHAR* VM::get_tape_message(int drv)
249 {
250         return drec->get_message();
251 }
252
253 void VM::push_play(int drv)
254 {
255         drec->set_ff_rew(0);
256         drec->set_remote(true);
257 }
258
259 void VM::push_stop(int drv)
260 {
261         drec->set_remote(false);
262 }
263
264 void VM::push_fast_forward(int drv)
265 {
266         drec->set_ff_rew(1);
267         drec->set_remote(true);
268 }
269
270 void VM::push_fast_rewind(int drv)
271 {
272         drec->set_ff_rew(-1);
273         drec->set_remote(true);
274 }
275
276 bool VM::is_frame_skippable()
277 {
278         return event->is_frame_skippable();
279 }
280
281 void VM::update_config()
282 {
283         for(DEVICE* device = first_device; device; device = device->next_device) {
284                 device->update_config();
285         }
286 }
287
288 #define STATE_VERSION   3
289
290 bool VM::process_state(FILEIO* state_fio, bool loading)
291 {
292         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
293                 return false;
294         }
295         for(DEVICE* device = first_device; device; device = device->next_device) {
296                 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
297                 // const char *name = typeid(*device).name();
298                 //       But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
299                 const char *name = device->get_device_name();
300                 int len = strlen(name);
301                 
302                 if(!state_fio->StateCheckInt32(len)) {
303                         if(loading) {
304                                 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
305                         }
306                         return false;
307                 }
308                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
309                         if(loading) {
310                                 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
311                         }
312                         return false;
313                 }
314                 if(!device->process_state(state_fio, loading)) {
315                         if(loading) {
316                                 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
317                         }
318                         return false;
319                 }
320         }
321         state_fio->StateBuffer(ram, sizeof(ram), 1);
322         return true;
323 }