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 / babbage2nd / babbage2nd.cpp
1 /*
2         Gijutsu-Hyoron-Sha Babbage-2nd Emulator 'eBabbage-2nd'
3
4         Author : Takeda.Toshiya
5         Date   : 2009.12.26 -
6
7         [ virtual machine ]
8 */
9
10 #include "babbage2nd.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../io.h"
16 #include "../memory.h"
17 #include "../z80.h"
18 #include "../z80ctc.h"
19 #include "../z80pio.h"
20
21 #include "display.h"
22 #include "keyboard.h"
23
24 // ----------------------------------------------------------------------------
25 // initialize
26 // ----------------------------------------------------------------------------
27
28 VM::VM(EMU* parent_emu) : emu(parent_emu)
29 {
30         // create devices
31         first_device = last_device = NULL;
32         dummy = new DEVICE(this, emu);  // must be 1st device
33         event = new EVENT(this, emu);   // must be 2nd device
34         
35         io = new IO(this, emu);
36         memory = new MEMORY(this, emu);
37         cpu = new Z80(this, emu);
38         ctc = new Z80CTC(this, emu);
39         pio1 = new Z80PIO(this, emu);
40         pio2 = new Z80PIO(this, emu);
41         
42         display = new DISPLAY(this, emu);
43         keyboard = new KEYBOARD(this, emu);
44         
45         // set contexts
46         event->set_context_cpu(cpu);
47         
48         pio2->set_context_port_b(display, SIG_DISPLAY_7SEG_LED, 0xff, 0);
49         keyboard->set_context_pio(pio2);
50         // p.145, fig.3-4
51         ctc->set_context_zc2(ctc, SIG_Z80CTC_TRIG_1, 1);
52         ctc->set_context_zc1(ctc, SIG_Z80CTC_TRIG_0, 1);
53         // p.114, fig.2-52
54         pio1->set_context_port_b(display, SIG_DISPLAY_8BIT_LED, 0xff, 0);
55         //pio1->set_context_port_b(pio1, SIG_Z80PIO_PORT_A, 0xff, 0);
56         
57         // cpu bus
58         cpu->set_context_mem(memory);
59         cpu->set_context_io(io);
60         cpu->set_context_intr(ctc);
61         
62         // z80 family daisy chain
63         ctc->set_context_intr(cpu, 0);
64         ctc->set_context_child(pio1);
65         pio1->set_context_intr(cpu, 1);
66         pio1->set_context_child(pio2);
67         pio2->set_context_intr(cpu, 2);
68         
69         // memory bus
70         memset(ram, 0, sizeof(ram));
71         memset(rom, 0xff, sizeof(rom));
72         
73         memory->read_bios(_T("MON.ROM"), rom, sizeof(rom));
74         
75         memory->set_memory_r(0x0000, 0x07ff, rom);
76         memory->set_memory_rw(0x1000, 0x17ff, ram);
77         
78         // i/o bus
79         io->set_iomap_range_rw(0x00, 0x03, ctc);
80         io->set_iomap_range_rw(0x10, 0x13, pio1);
81         io->set_iomap_range_rw(0x20, 0x23, pio2);
82         
83         // initialize all devices
84         for(DEVICE* device = first_device; device; device = device->next_device) {
85                 device->initialize();
86         }
87 }
88
89 VM::~VM()
90 {
91         // delete all devices
92         for(DEVICE* device = first_device; device;) {
93                 DEVICE *next_device = device->next_device;
94                 device->release();
95                 delete device;
96                 device = next_device;
97         }
98 }
99
100 DEVICE* VM::get_device(int id)
101 {
102         for(DEVICE* device = first_device; device; device = device->next_device) {
103                 if(device->this_device_id == id) {
104                         return device;
105                 }
106         }
107         return NULL;
108 }
109
110 // ----------------------------------------------------------------------------
111 // drive virtual machine
112 // ----------------------------------------------------------------------------
113
114 void VM::reset()
115 {
116         // reset all devices
117         for(DEVICE* device = first_device; device; device = device->next_device) {
118                 device->reset();
119         }
120 }
121
122 void VM::run()
123 {
124         event->drive();
125 }
126
127 // ----------------------------------------------------------------------------
128 // draw screen
129 // ----------------------------------------------------------------------------
130
131 void VM::draw_screen()
132 {
133         display->draw_screen();
134 }
135
136 // ----------------------------------------------------------------------------
137 // soud manager
138 // ----------------------------------------------------------------------------
139
140 void VM::initialize_sound(int rate, int samples)
141 {
142         // init sound manager
143         event->initialize_sound(rate, samples);
144 }
145
146 uint16* VM::create_sound(int* extra_frames)
147 {
148         return event->create_sound(extra_frames);
149 }
150
151 int VM::sound_buffer_ptr()
152 {
153         return event->sound_buffer_ptr();
154 }
155
156 // ----------------------------------------------------------------------------
157 // notify key
158 // ----------------------------------------------------------------------------
159
160 void VM::key_down(int code, bool repeat)
161 {
162         keyboard->key_down(code);
163 }
164
165 void VM::key_up(int code)
166 {
167         //keyboard->key_up(code);
168 }
169
170 // ----------------------------------------------------------------------------
171 // user interface
172 // ----------------------------------------------------------------------------
173
174 void VM::load_binary(int drv, _TCHAR* file_path)
175 {
176         if(drv == 0) {
177                 memory->read_image(file_path, ram, sizeof(ram));
178         }
179 }
180
181 void VM::save_binary(int drv, _TCHAR* file_path)
182 {
183         if(drv == 0) {
184                 memory->write_image(file_path, ram, sizeof(ram));
185         }
186 }
187
188 bool VM::now_skip()
189 {
190         return event->now_skip();
191 }
192
193 void VM::update_config()
194 {
195         for(DEVICE* device = first_device; device; device = device->next_device) {
196                 device->update_config();
197         }
198 }
199