OSDN Git Service

[VM][General][WIP] Apply new (Upstream 2016-02-21) APIs to VMs.
[csp-qt/common_source_project-fm7.git] / source / src / vm / pc100 / pc100.cpp
1 /*
2         NEC PC-100 Emulator 'ePC-100'
3
4         Author : Takeda.Toshiya
5         Date   : 2008.07.12 -
6
7         [ virtual machine ]
8 */
9
10 #include "pc100.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../and.h"
16 #include "../beep.h"
17 #include "../disk.h"
18 #include "../i8251.h"
19 #include "../i8255.h"
20 #include "../i8259.h"
21 #include "../i286.h"
22 #include "../io.h"
23 #include "../memory.h"
24 #include "../msm58321.h"
25 #include "../pcm1bit.h"
26 #include "../upd765a.h"
27
28 #ifdef USE_DEBUGGER
29 #include "../debugger.h"
30 #endif
31
32 #include "crtc.h"
33 #include "ioctrl.h"
34 #include "kanji.h"
35
36 // ----------------------------------------------------------------------------
37 // initialize
38 // ----------------------------------------------------------------------------
39
40 VM::VM(EMU* parent_emu) : emu(parent_emu)
41 {
42         // create devices
43         first_device = last_device = NULL;
44         dummy = new DEVICE(this, emu);  // must be 1st device
45         event = new EVENT(this, emu);   // must be 2nd device
46         
47         and_drq = new AND(this, emu);
48         beep = new BEEP(this, emu);
49         sio = new I8251(this, emu);
50         pio0 = new I8255(this, emu);
51         pio1 = new I8255(this, emu);
52         pic = new I8259(this, emu);
53         cpu = new I286(this, emu);
54         io = new IO(this, emu);
55         memory = new MEMORY(this, emu);
56         rtc = new MSM58321(this, emu);
57         pcm = new PCM1BIT(this, emu);
58         fdc = new UPD765A(this, emu);
59         
60         crtc = new CRTC(this, emu);
61         ioctrl = new IOCTRL(this, emu);
62         kanji = new KANJI(this, emu);
63         
64         // set contexts
65         event->set_context_cpu(cpu);
66         event->set_context_sound(beep);
67         event->set_context_sound(pcm);
68         
69         and_drq->set_context_out(cpu, SIG_CPU_NMI, 1);
70         and_drq->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);
71         sio->set_context_rxrdy(pic, SIG_I8259_IR1, 1);
72         pio0->set_context_port_a(rtc, SIG_MSM58321_READ, 1, 0);
73         pio0->set_context_port_a(rtc, SIG_MSM58321_WRITE, 2, 0);
74         pio0->set_context_port_a(rtc, SIG_MSM58321_ADDR_WRITE, 4, 0);
75         pio0->set_context_port_c(rtc, SIG_MSM58321_DATA, 0x0f, 0);
76         pio1->set_context_port_a(crtc, SIG_CRTC_BITMASK_LOW, 0xff, 0);
77         pio1->set_context_port_b(crtc, SIG_CRTC_BITMASK_HIGH, 0xff, 0);
78         pio1->set_context_port_c(crtc, SIG_CRTC_VRAM_PLANE, 0x3f, 0);
79         pio1->set_context_port_c(and_drq, SIG_AND_BIT_0, 0x80, 0);
80         pio1->set_context_port_c(ioctrl, SIG_IOCTRL_RESET, 0x40, 0);
81         pic->set_context_cpu(cpu);
82         rtc->set_context_data(pio0, SIG_I8255_PORT_C, 0x0f, 0);
83         rtc->set_context_busy(pio0, SIG_I8255_PORT_C, 0x10);
84         fdc->set_context_irq(cpu, SIG_CPU_NMI, 1);
85         fdc->set_context_drq(and_drq, SIG_AND_BIT_1, 1);
86         
87         crtc->set_context_pic(pic);
88         ioctrl->set_context_pic(pic);
89         ioctrl->set_context_fdc(fdc);
90         ioctrl->set_context_beep(beep);
91         ioctrl->set_context_pcm(pcm);
92         
93         // cpu bus
94         cpu->set_context_mem(memory);
95         cpu->set_context_io(io);
96         cpu->set_context_intr(pic);
97 #ifdef USE_DEBUGGER
98         cpu->set_context_debugger(new DEBUGGER(this, emu));
99 #endif
100         
101         // memory bus
102         memset(ram, 0, sizeof(ram));
103         memset(ipl, 0xff, sizeof(ipl));
104         
105         memory->read_bios(_T("IPL.ROM"), ipl, sizeof(ipl));
106         
107         memory->set_memory_rw(0x00000, 0xbffff, ram);
108         memory->set_memory_mapped_io_rw(0xc0000, 0xdffff, crtc);
109         memory->set_memory_r(0xf8000, 0xfffff, ipl);
110         
111         // i/o bus
112         io->set_iomap_alias_rw(0x00, pic, 0);
113         io->set_iomap_alias_rw(0x02, pic, 1);
114 //      io->set_iomap_alias_rw(0x04, dma, 0);
115 //      io->set_iomap_alias_rw(0x06, dma, 1);
116         io->set_iomap_alias_r(0x08, fdc, 0);
117         io->set_iomap_alias_rw(0x0a, fdc, 1);
118         io->set_iomap_alias_rw(0x10, pio0, 0);
119         io->set_iomap_alias_rw(0x12, pio0, 1);
120         io->set_iomap_alias_rw(0x14, pio0, 2);
121         io->set_iomap_alias_rw(0x16, pio0, 3);
122         io->set_iomap_alias_rw(0x18, pio1, 0);
123         io->set_iomap_alias_rw(0x1a, pio1, 1);
124         io->set_iomap_alias_rw(0x1c, pio1, 2);
125         io->set_iomap_alias_rw(0x1e, pio1, 3);
126         io->set_iomap_single_r(0x20, ioctrl);
127         io->set_iomap_single_rw(0x22, ioctrl);
128         io->set_iomap_single_w(0x24, ioctrl);
129         io->set_iomap_alias_rw(0x28, sio, 0);
130         io->set_iomap_alias_rw(0x2a, sio, 1);
131         io->set_iomap_single_rw(0x30, crtc);
132         io->set_iomap_single_rw(0x38, crtc);
133         io->set_iomap_single_rw(0x3a, crtc);
134         io->set_iomap_single_rw(0x3c, crtc);
135         io->set_iomap_single_rw(0x3e, crtc);
136         for(int i = 0x40; i < 0x62; i++) {
137                 io->set_iomap_single_rw(i, crtc);
138         }
139         io->set_iomap_single_rw(0x80, kanji);
140         io->set_iomap_single_rw(0x81, kanji);
141         io->set_iomap_single_w(0x84, kanji);
142         io->set_iomap_single_w(0x86, kanji);
143         
144         // initialize all devices
145         for(DEVICE* device = first_device; device; device = device->next_device) {
146                 device->initialize();
147         }
148         for(int i = 0; i < 4; i++) {
149                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
150         }
151 }
152
153 VM::~VM()
154 {
155         // delete all devices
156         for(DEVICE* device = first_device; device;) {
157                 DEVICE *next_device = device->next_device;
158                 device->release();
159                 delete device;
160                 device = next_device;
161         }
162 }
163
164 DEVICE* VM::get_device(int id)
165 {
166         for(DEVICE* device = first_device; device; device = device->next_device) {
167                 if(device->this_device_id == id) {
168                         return device;
169                 }
170         }
171         return NULL;
172 }
173
174 // ----------------------------------------------------------------------------
175 // drive virtual machine
176 // ----------------------------------------------------------------------------
177
178 void VM::reset()
179 {
180         // reset all devices
181         for(DEVICE* device = first_device; device; device = device->next_device) {
182                 device->reset();
183         }
184 }
185
186 void VM::run()
187 {
188         event->drive();
189 }
190
191 // ----------------------------------------------------------------------------
192 // debugger
193 // ----------------------------------------------------------------------------
194
195 #ifdef USE_DEBUGGER
196 DEVICE *VM::get_cpu(int index)
197 {
198         if(index == 0) {
199                 return cpu;
200         }
201         return NULL;
202 }
203 #endif
204
205 // ----------------------------------------------------------------------------
206 // draw screen
207 // ----------------------------------------------------------------------------
208
209 void VM::draw_screen()
210 {
211         crtc->draw_screen();
212 }
213
214 int VM::get_access_lamp_status()
215 {
216         uint32 status = fdc->read_signal(0);
217         return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
218 }
219
220 // ----------------------------------------------------------------------------
221 // soud manager
222 // ----------------------------------------------------------------------------
223
224 void VM::initialize_sound(int rate, int samples)
225 {
226         // init sound manager
227         event->initialize_sound(rate, samples);
228         
229         // init sound gen
230         beep->initialize_sound(rate, 2400, 8000);
231         pcm->initialize_sound(rate, 8000);
232 }
233
234 uint16* VM::create_sound(int* extra_frames)
235 {
236         return event->create_sound(extra_frames);
237 }
238
239 int VM::get_sound_buffer_ptr()
240 {
241         return event->get_sound_buffer_ptr();
242 }
243
244 #ifdef USE_SOUND_VOLUME
245 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
246 {
247         if(ch == 0) {
248                 beep->set_volume(0, decibel_l, decibel_r);
249         } else if(ch == 1) {
250                 pcm->set_volume(0, decibel_l, decibel_r);
251         }
252 }
253 #endif
254
255 // ----------------------------------------------------------------------------
256 // notify key
257 // ----------------------------------------------------------------------------
258
259 void VM::key_down(int code, bool repeat)
260 {
261         ioctrl->key_down(code);
262 }
263
264 void VM::key_up(int code)
265 {
266         ioctrl->key_up(code);
267 }
268
269 // ----------------------------------------------------------------------------
270 // user interface
271 // ----------------------------------------------------------------------------
272
273 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
274 {
275         fdc->open_disk(drv, file_path, bank);
276 }
277
278 void VM::close_floppy_disk(int drv)
279 {
280         fdc->close_disk(drv);
281 }
282
283 bool VM::is_floppy_disk_inserted(int drv)
284 {
285         return fdc->is_disk_inserted(drv);
286 }
287
288 void VM::is_floppy_disk_protected(int drv, bool value)
289 {
290         fdc->is_disk_protected(drv, value);
291 }
292
293 bool VM::is_floppy_disk_protected(int drv)
294 {
295         return fdc->is_disk_protected(drv);
296 }
297
298 bool VM::is_frame_skippable()
299 {
300         return event->is_frame_skippable();
301 }
302
303 void VM::update_config()
304 {
305         for(DEVICE* device = first_device; device; device = device->next_device) {
306                 device->update_config();
307         }
308 }
309
310 #define STATE_VERSION   1
311
312 void VM::save_state(FILEIO* state_fio)
313 {
314         state_fio->FputUint32(STATE_VERSION);
315         
316         for(DEVICE* device = first_device; device; device = device->next_device) {
317                 device->save_state(state_fio);
318         }
319         state_fio->Fwrite(ram, sizeof(ram), 1);
320 }
321
322 bool VM::load_state(FILEIO* state_fio)
323 {
324         if(state_fio->FgetUint32() != STATE_VERSION) {
325                 return false;
326         }
327         for(DEVICE* device = first_device; device; device = device->next_device) {
328                 if(!device->load_state(state_fio)) {
329                         return false;
330                 }
331         }
332         state_fio->Fread(ram, sizeof(ram), 1);
333         return true;
334 }
335