OSDN Git Service

[General][WIP] Merge upstream 2017-03-20.Still not implement UIs.
[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 "../memory.h"
19 #include "../noise.h"
20 #include "../pcm1bit.h"
21
22 #ifdef USE_DEBUGGER
23 #include "../debugger.h"
24 #endif
25
26 #include "io.h"
27
28 // ----------------------------------------------------------------------------
29 // initialize
30 // ----------------------------------------------------------------------------
31
32 VM::VM(EMU* parent_emu) : emu(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         drec = new DATAREC(this, emu);
40         drec->set_context_noise_play(new NOISE(this, emu));
41         drec->set_context_noise_stop(new NOISE(this, emu));
42         drec->set_context_noise_fast(new NOISE(this, emu));
43         for(int i = 0; i < 8; i++) {
44                 lcd[i] = new HD44102(this, emu);
45         }
46         cpu = new MC6800(this, emu);
47         memory = new MEMORY(this, emu);
48         pcm = new PCM1BIT(this, emu);
49         
50         io = new IO(this, emu);
51         
52         // set contexts
53         event->set_context_cpu(cpu);
54         event->set_context_sound(drec);
55         event->set_context_sound(pcm);
56         event->set_context_sound(drec->get_context_noise_play());
57         event->set_context_sound(drec->get_context_noise_stop());
58         event->set_context_sound(drec->get_context_noise_fast());
59         
60 //      cpu->set_context_port1(drec, SIG_DATAREC_MIC, 0x01, 0);
61         cpu->set_context_port1(pcm, SIG_PCM1BIT_ON, 0x08, 0);
62         cpu->set_context_port1(pcm, SIG_PCM1BIT_SIGNAL, 0x10, 0);
63         
64         for(int i = 0; i < 8; i++) {
65                 io->set_context_lcd(i, lcd[i]);
66         }
67         
68         // cpu bus
69         cpu->set_context_mem(memory);
70 #ifdef USE_DEBUGGER
71         cpu->set_context_debugger(new DEBUGGER(this, emu));
72 #endif
73         
74         // memory bus
75         memset(ram, 0x00, sizeof(ram));
76         memset(rom, 0xff, sizeof(rom));
77         
78         memory->read_bios(_T("BASIC.ROM"), rom, sizeof(rom));
79         
80         memory->set_memory_rw(0x2000, 0x7fff, ram);
81         memory->set_memory_r(0x8000, 0xffff, rom);
82         memory->set_memory_mapped_io_rw(0x0a00, 0x0bff, io);
83         memory->set_memory_mapped_io_rw(0x0d00, 0x0fff, io);
84         
85         // initialize all devices
86         for(DEVICE* device = first_device; device; device = device->next_device) {
87                 device->initialize();
88         }
89 }
90
91 VM::~VM()
92 {
93         // delete all devices
94         for(DEVICE* device = first_device; device;) {
95                 DEVICE *next_device = device->next_device;
96                 device->release();
97                 delete device;
98                 device = next_device;
99         }
100 }
101
102 DEVICE* VM::get_device(int id)
103 {
104         for(DEVICE* device = first_device; device; device = device->next_device) {
105                 if(device->this_device_id == id) {
106                         return device;
107                 }
108         }
109         return NULL;
110 }
111
112 // ----------------------------------------------------------------------------
113 // drive virtual machine
114 // ----------------------------------------------------------------------------
115
116 void VM::reset()
117 {
118         // reset all devices
119         for(DEVICE* device = first_device; device; device = device->next_device) {
120                 device->reset();
121         }
122         cpu->write_signal(SIG_MC6801_PORT_1, 0x00, 0xff);
123         cpu->write_signal(SIG_MC6801_PORT_2, 0x00, 0xff);
124         cpu->write_signal(SIG_MC6801_PORT_3, 0x00, 0xff);
125         cpu->write_signal(SIG_MC6801_PORT_4, 0x00, 0xff);
126 }
127
128 void VM::run()
129 {
130         event->drive();
131 }
132
133 // ----------------------------------------------------------------------------
134 // debugger
135 // ----------------------------------------------------------------------------
136
137 #ifdef USE_DEBUGGER
138 DEVICE *VM::get_cpu(int index)
139 {
140         if(index == 0) {
141                 return cpu;
142         }
143         return NULL;
144 }
145 #endif
146
147 // ----------------------------------------------------------------------------
148 // draw screen
149 // ----------------------------------------------------------------------------
150
151 void VM::draw_screen()
152 {
153         for(int y = 0; y < 2; y++) {
154                 for(int x = 0; x < 4; x++) {
155                         lcd[x + 4 * y]->screen_update(50 * x - 4, 32 * y, y == 0);
156                 }
157         }
158 }
159
160 // ----------------------------------------------------------------------------
161 // soud manager
162 // ----------------------------------------------------------------------------
163
164 void VM::initialize_sound(int rate, int samples)
165 {
166         // init sound manager
167         event->initialize_sound(rate, samples);
168         
169         // init sound gen
170         pcm->initialize_sound(rate, 8000);
171 }
172
173 uint16_t* VM::create_sound(int* extra_frames)
174 {
175         return event->create_sound(extra_frames);
176 }
177
178 int VM::get_sound_buffer_ptr()
179 {
180         return event->get_sound_buffer_ptr();
181 }
182
183 #ifdef USE_SOUND_VOLUME
184 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
185 {
186         if(ch == 0) {
187                 pcm->set_volume(0, decibel_l, decibel_r);
188         } if(ch == 1) {
189                 drec->set_volume(0, decibel_l, decibel_r);
190         } else if(ch == 2) {
191                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
192                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
193                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
194         }
195 }
196 #endif
197
198 // ----------------------------------------------------------------------------
199 // user interface
200 // ----------------------------------------------------------------------------
201
202 void VM::play_tape(int drv, const _TCHAR* file_path)
203 {
204         drec->play_tape(file_path);
205 }
206
207 void VM::rec_tape(int drv, const _TCHAR* file_path)
208 {
209         drec->rec_tape(file_path);
210 }
211
212 void VM::close_tape(int drv)
213 {
214         emu->lock_vm();
215         drec->close_tape();
216         emu->unlock_vm();
217 }
218
219 bool VM::is_tape_inserted(int drv)
220 {
221         return drec->is_tape_inserted();
222 }
223
224 bool VM::is_tape_playing(int drv)
225 {
226         return drec->is_tape_playing();
227 }
228
229 bool VM::is_tape_recording(int drv)
230 {
231         return drec->is_tape_recording();
232 }
233
234 int VM::get_tape_position(int drv)
235 {
236         return drec->get_tape_position();
237 }
238
239 const _TCHAR* VM::get_tape_message(int drv)
240 {
241         return drec->get_message();
242 }
243
244 bool VM::is_frame_skippable()
245 {
246         return event->is_frame_skippable();
247 }
248
249 void VM::update_config()
250 {
251         for(DEVICE* device = first_device; device; device = device->next_device) {
252                 device->update_config();
253         }
254 }
255
256 #define STATE_VERSION   2
257
258 void VM::save_state(FILEIO* state_fio)
259 {
260         state_fio->FputUint32(STATE_VERSION);
261         
262         for(DEVICE* device = first_device; device; device = device->next_device) {
263                 device->save_state(state_fio);
264         }
265         state_fio->Fwrite(ram, sizeof(ram), 1);
266 }
267
268 bool VM::load_state(FILEIO* state_fio)
269 {
270         if(state_fio->FgetUint32() != STATE_VERSION) {
271                 return false;
272         }
273         for(DEVICE* device = first_device; device; device = device->next_device) {
274                 if(!device->load_state(state_fio)) {
275                         return false;
276                 }
277         }
278         state_fio->Fread(ram, sizeof(ram), 1);
279         return true;
280 }
281