OSDN Git Service

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