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