OSDN Git Service

[VM][General] Merge upstream 2015-08-25.
[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 = 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->set_context_out(cpu, SIG_CPU_NMI, 1);
70         and->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, 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, 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::access_lamp()
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->init(rate, 2400, 8000);
231         pcm->init(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::sound_buffer_ptr()
240 {
241         return event->sound_buffer_ptr();
242 }
243
244 // ----------------------------------------------------------------------------
245 // notify key
246 // ----------------------------------------------------------------------------
247
248 void VM::key_down(int code, bool repeat)
249 {
250         ioctrl->key_down(code);
251 }
252
253 void VM::key_up(int code)
254 {
255         ioctrl->key_up(code);
256 }
257
258 // ----------------------------------------------------------------------------
259 // user interface
260 // ----------------------------------------------------------------------------
261
262 void VM::open_disk(int drv, const _TCHAR* file_path, int bank)
263 {
264         fdc->open_disk(drv, file_path, bank);
265 }
266
267 void VM::close_disk(int drv)
268 {
269         fdc->close_disk(drv);
270 }
271
272 bool VM::disk_inserted(int drv)
273 {
274         return fdc->disk_inserted(drv);
275 }
276
277 void VM::set_disk_protected(int drv, bool value)
278 {
279         fdc->set_disk_protected(drv, value);
280 }
281
282 bool VM::get_disk_protected(int drv)
283 {
284         return fdc->get_disk_protected(drv);
285 }
286
287 bool VM::now_skip()
288 {
289         return event->now_skip();
290 }
291
292 void VM::update_config()
293 {
294         for(DEVICE* device = first_device; device; device = device->next_device) {
295                 device->update_config();
296         }
297 }
298
299 #define STATE_VERSION   1
300
301 void VM::save_state(FILEIO* state_fio)
302 {
303         state_fio->FputUint32(STATE_VERSION);
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         for(DEVICE* device = first_device; device; device = device->next_device) {
317                 if(!device->load_state(state_fio)) {
318                         return false;
319                 }
320         }
321         state_fio->Fread(ram, sizeof(ram), 1);
322         return true;
323 }
324