OSDN Git Service

[VM][General] Merge upstream 2015-09-23.
[csp-qt/common_source_project-fm7.git] / source / src / vm / mycomz80a / mycomz80a.cpp
1 /*
2         Japan Electronics College MYCOMZ-80A Emulator 'eMYCOMZ-80A'
3
4         Author : Takeda.Toshiya
5         Date   : 2009.05.13-
6
7         [ virtual machine ]
8 */
9
10 #include "mycomz80a.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../datarec.h"
16 #include "../hd46505.h"
17 #include "../i8255.h"
18 #include "../io.h"
19 #include "../msm58321.h"
20 #include "../sn76489an.h"
21 #include "../z80.h"
22
23 #ifdef USE_DEBUGGER
24 #include "../debugger.h"
25 #endif
26
27 #include "display.h"
28 #include "keyboard.h"
29 #include "memory.h"
30
31 // ----------------------------------------------------------------------------
32 // initialize
33 // ----------------------------------------------------------------------------
34
35 VM::VM(EMU* parent_emu) : emu(parent_emu)
36 {
37         // create devices
38         first_device = last_device = NULL;
39         dummy = new DEVICE(this, emu);  // must be 1st device
40         event = new EVENT(this, emu);   // must be 2nd device
41         
42         drec = new DATAREC(this, emu);
43         crtc = new HD46505(this, emu);
44         pio1 = new I8255(this, emu);
45         pio2 = new I8255(this, emu);
46         pio3 = new I8255(this, emu);
47         io = new IO(this, emu);
48         rtc = new MSM58321(this, emu);  // MSM5832
49         psg = new SN76489AN(this, emu);
50         cpu = new Z80(this, emu);
51         
52         display = new DISPLAY(this, emu);
53         keyboard = new KEYBOARD(this, emu);
54         memory = new MEMORY(this, emu);
55         
56         // set contexts
57         event->set_context_cpu(cpu);
58         event->set_context_sound(psg);
59         event->set_context_sound(drec);
60         
61         //      00      out     system control
62         //      01      in/out  vram data
63         //      02      out     crtc addr
64         //      03      in/out  crtc data
65         //      04-07   in/ou   pio1
66         //              pa0-7   out     vram addr low, psg data
67         //              pb0-7   in      keyboard data
68         //              pc0-2   out     vram addr high
69         //              pc3     out     fdc format write
70         //              pc4     in      keyboard s2
71         //              pc5     in      keyboard s3
72         //              pc6     in      keyboard s4 (motor on/off)
73         //              pc7     in      keyboard s5 (slow)
74         //      08-0b   in/ou   pio2
75         //              pa0     in      keyboard strobe (1=pressed)
76         //              pa1     in      keyboard shift (1=pressed)
77         //              pa2     in      cmt in
78         //              pa3-6   in      printer ctrl
79         //              pa7     in      crtc disptmg
80         //              pb0-7   out     printer data
81         //              pc0     out     printer strobe
82         //              pc1     out     printer reset
83         //              pc2     out     cmt out
84         //              pc3     out     cmt remote
85         //              pc4     out     psg we
86         //              pc5     out     psg cs
87         //              pc6     out     display chr/cg (0=chr,1=cg)
88         //              pc7     out     display freq (0=80column,1=40column)
89         //      0c-0f   in/ou   pio3
90         //              pa0-6   in      fdc control
91         //              pb0-3   in/out  rtc data
92         //              pc0-3   out     rtc address
93         //              pc4     out     rtc hold
94         //              pc5     out     rtc rd
95         //              pc6     out     rtc wr
96         //              pc7     out     rtc cs
97         drec->set_context_ear(pio2, SIG_I8255_PORT_A, 4);
98         crtc->set_context_disp(pio2, SIG_I8255_PORT_A, 0x80);
99         pio1->set_context_port_a(display, SIG_DISPLAY_ADDR_L, 0xff, 0);
100         pio1->set_context_port_a(psg, SIG_SN76489AN_DATA, 0xff, 0);
101         pio1->set_context_port_c(display, SIG_DISPLAY_ADDR_H, 7, 0);
102         pio2->set_context_port_c(drec, SIG_DATAREC_MIC, 4, 0);
103         pio2->set_context_port_c(drec, SIG_DATAREC_REMOTE, 8, 0);
104         pio2->set_context_port_c(psg, SIG_SN76489AN_WE, 0x10, 0);
105         pio2->set_context_port_c(psg, SIG_SN76489AN_CS, 0x20, 0);
106         pio2->set_context_port_c(display, SIG_DISPLAY_MODE, 0xc0, 0);
107         pio3->set_context_port_b(rtc, SIG_MSM58321_DATA, 0x0f, 0);
108         pio3->set_context_port_c(rtc, SIG_MSM5832_ADDR, 0x0f, 0);
109         pio3->set_context_port_c(rtc, SIG_MSM5832_HOLD, 0x10, 0);
110         pio3->set_context_port_c(rtc, SIG_MSM58321_READ, 0x20, 0);
111         pio3->set_context_port_c(rtc, SIG_MSM58321_WRITE, 0x40, 0);
112         pio3->set_context_port_c(rtc, SIG_MSM58321_CS, 0x80, 0);
113         rtc->set_context_data(pio3, SIG_I8255_PORT_B, 0x0f, 0);
114         
115         display->set_regs_ptr(crtc->get_regs());
116         keyboard->set_context_cpu(cpu);
117         keyboard->set_context_pio1(pio1);
118         keyboard->set_context_pio2(pio2);
119         
120         // cpu bus
121         cpu->set_context_mem(memory);
122         cpu->set_context_io(io);
123         cpu->set_context_intr(dummy);
124 #ifdef USE_DEBUGGER
125         cpu->set_context_debugger(new DEBUGGER(this, emu));
126 #endif
127         
128         // i/o bus
129         io->set_iomap_single_w(0x00, memory);
130         io->set_iomap_single_rw(0x01, display);
131         io->set_iomap_range_rw(0x02, 0x03, crtc);
132         io->set_iomap_range_rw(0x04, 0x07, pio1);
133         io->set_iomap_range_rw(0x08, 0x0b, pio2);
134         io->set_iomap_range_rw(0x0c, 0x0f, pio3);
135         
136         // initialize all devices
137         for(DEVICE* device = first_device; device; device = device->next_device) {
138                 device->initialize();
139         }
140 }
141
142 VM::~VM()
143 {
144         // delete all devices
145         for(DEVICE* device = first_device; device;) {
146                 DEVICE *next_device = device->next_device;
147                 device->release();
148                 delete device;
149                 device = next_device;
150         }
151 }
152
153 DEVICE* VM::get_device(int id)
154 {
155         for(DEVICE* device = first_device; device; device = device->next_device) {
156                 if(device->this_device_id == id) {
157                         return device;
158                 }
159         }
160         return NULL;
161 }
162
163 // ----------------------------------------------------------------------------
164 // drive virtual machine
165 // ----------------------------------------------------------------------------
166
167 void VM::reset()
168 {
169         // reset all devices
170         for(DEVICE* device = first_device; device; device = device->next_device) {
171                 device->reset();
172         }
173 }
174
175 void VM::run()
176 {
177         event->drive();
178 }
179
180 double VM::frame_rate()
181 {
182         return event->frame_rate();
183 }
184
185 // ----------------------------------------------------------------------------
186 // debugger
187 // ----------------------------------------------------------------------------
188
189 #ifdef USE_DEBUGGER
190 DEVICE *VM::get_cpu(int index)
191 {
192         if(index == 0) {
193                 return cpu;
194         }
195         return NULL;
196 }
197 #endif
198
199 // ----------------------------------------------------------------------------
200 // draw screen
201 // ----------------------------------------------------------------------------
202
203 void VM::draw_screen()
204 {
205         display->draw_screen();
206 }
207
208 // ----------------------------------------------------------------------------
209 // soud manager
210 // ----------------------------------------------------------------------------
211
212 void VM::initialize_sound(int rate, int samples)
213 {
214         // init sound manager
215         event->initialize_sound(rate, samples);
216         
217         // init sound gen
218         psg->init(rate, 2500800, 10000);
219 }
220
221 uint16* VM::create_sound(int* extra_frames)
222 {
223         return event->create_sound(extra_frames);
224 }
225
226 int VM::sound_buffer_ptr()
227 {
228         return event->sound_buffer_ptr();
229 }
230
231 // ----------------------------------------------------------------------------
232 // notify key
233 // ----------------------------------------------------------------------------
234
235 void VM::key_down(int code, bool repeat)
236 {
237         keyboard->key_down(code);
238 }
239
240 void VM::key_up(int code)
241 {
242         keyboard->key_up(code);
243 }
244
245 // ----------------------------------------------------------------------------
246 // user interface
247 // ----------------------------------------------------------------------------
248
249 void VM::play_tape(const _TCHAR* file_path)
250 {
251         drec->play_tape(file_path);
252 //      drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
253 }
254
255 void VM::rec_tape(const _TCHAR* file_path)
256 {
257         drec->rec_tape(file_path);
258 //      drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
259 }
260
261 void VM::close_tape()
262 {
263         drec->close_tape();
264 //      drec->write_signal(SIG_DATAREC_REMOTE, 0, 1);
265 }
266
267 bool VM::tape_inserted()
268 {
269         return drec->tape_inserted();
270 }
271
272 bool VM::now_skip()
273 {
274         return event->now_skip();
275 }
276
277 void VM::update_config()
278 {
279         for(DEVICE* device = first_device; device; device = device->next_device) {
280                 device->update_config();
281         }
282 }
283
284 #define STATE_VERSION   1
285
286 void VM::save_state(FILEIO* state_fio)
287 {
288         state_fio->FputUint32(STATE_VERSION);
289         
290         for(DEVICE* device = first_device; device; device = device->next_device) {
291                 device->save_state(state_fio);
292         }
293 }
294
295 bool VM::load_state(FILEIO* state_fio)
296 {
297         if(state_fio->FgetUint32() != STATE_VERSION) {
298                 return false;
299         }
300         for(DEVICE* device = first_device; device; device = device->next_device) {
301                 if(!device->load_state(state_fio)) {
302                         return false;
303                 }
304         }
305         return true;
306 }
307