2 Skelton for retropc emulator
4 Author : Takeda.Toshiya
10 #ifndef _LIBCPU_NEWDEV_DEVICE_H_
11 #define _LIBCPU_NEWDEV_DEVICE_H_
18 #include "csp_logger.h"
19 //#define USE_DEVICE_NAME
21 // max devices connected to the output port
25 #define SIG_CPU_IRQ 101
26 #define SIG_CPU_FIRQ 102
27 #define SIG_CPU_NMI 103
28 #define SIG_CPU_BUSREQ 104
29 #define SIG_CPU_DEBUG 105
31 #define SIG_PRINTER_DATA 201
32 #define SIG_PRINTER_STROBE 202
33 #define SIG_PRINTER_RESET 203
34 #define SIG_PRINTER_BUSY 204
35 #define SIG_PRINTER_ACK 205
36 #define SIG_PRINTER_SELECT 206
38 #define SIG_SCSI_DAT 301
39 #define SIG_SCSI_BSY 302
40 #define SIG_SCSI_CD 303
41 #define SIG_SCSI_IO 304
42 #define SIG_SCSI_MSG 305
43 #define SIG_SCSI_REQ 306
44 #define SIG_SCSI_SEL 307
45 #define SIG_SCSI_ATN 308
46 #define SIG_SCSI_ACK 309
47 #define SIG_SCSI_RST 310
60 DEVICE(VM* parent_vm, EMU* parent_emu);
61 //ToDo: Will implement real destructor per real classes and below destructor decl. with "virtual".
62 // This makes warning:
63 //"deleting object of polymorphic class type 'DEVICE' which has non-virtual
64 // destructor might cause undefined behavior [-Wdelete-non-virtual-dtor]".
68 virtual void initialize() { /* osd = emu->get_osd(); */}
69 virtual void release() {}
71 virtual void update_config() {}
72 virtual void save_state(FILEIO* state_fio) {}
73 virtual bool load_state(FILEIO* state_fio)
79 virtual void reset() {}
80 virtual void special_reset()
85 // NOTE: the virtual bus interface functions for 16/32bit access invite the cpu is little endian.
86 // if the cpu is big endian, you need to implement them in the virtual machine memory/io classes.
89 virtual void write_data8(uint32_t addr, uint32_t data) {}
90 virtual uint32_t read_data8(uint32_t addr)
94 virtual void write_data16(uint32_t addr, uint32_t data)
96 write_data8(addr, data & 0xff);
97 write_data8(addr + 1, (data >> 8) & 0xff);
99 virtual uint32_t read_data16(uint32_t addr)
101 uint32_t val = read_data8(addr);
102 val |= read_data8(addr + 1) << 8;
105 virtual void write_data32(uint32_t addr, uint32_t data)
107 write_data16(addr, data & 0xffff);
108 write_data16(addr + 2, (data >> 16) & 0xffff);
110 virtual uint32_t read_data32(uint32_t addr)
112 uint32_t val = read_data16(addr);
113 val |= read_data16(addr + 2) << 16;
116 virtual void write_data8w(uint32_t addr, uint32_t data, int* wait)
119 write_data8(addr, data);
121 virtual uint32_t read_data8w(uint32_t addr, int* wait)
124 return read_data8(addr);
126 virtual void write_data16w(uint32_t addr, uint32_t data, int* wait)
129 write_data8w(addr, data & 0xff, &wait_l);
130 write_data8w(addr + 1, (data >> 8) & 0xff, &wait_h);
131 *wait = wait_l + wait_h;
133 virtual uint32_t read_data16w(uint32_t addr, int* wait)
136 uint32_t val = read_data8w(addr, &wait_l);
137 val |= read_data8w(addr + 1, &wait_h) << 8;
138 *wait = wait_l + wait_h;
141 virtual void write_data32w(uint32_t addr, uint32_t data, int* wait)
144 write_data16w(addr, data & 0xffff, &wait_l);
145 write_data16w(addr + 2, (data >> 16) & 0xffff, &wait_h);
146 *wait = wait_l + wait_h;
148 virtual uint32_t read_data32w(uint32_t addr, int* wait)
151 uint32_t val = read_data16w(addr, &wait_l);
152 val |= read_data16w(addr + 2, &wait_h) << 16;
153 *wait = wait_l + wait_h;
156 virtual uint32_t fetch_op(uint32_t addr, int *wait)
158 return read_data8w(addr, wait);
160 virtual void write_dma_data8(uint32_t addr, uint32_t data)
162 write_data8(addr, data);
164 virtual uint32_t read_dma_data8(uint32_t addr)
166 return read_data8(addr);
168 virtual void write_dma_data16(uint32_t addr, uint32_t data)
170 write_data16(addr, data);
172 virtual uint32_t read_dma_data16(uint32_t addr)
174 return read_data16(addr);
176 virtual void write_dma_data32(uint32_t addr, uint32_t data)
178 write_data32(addr, data);
180 virtual uint32_t read_dma_data32(uint32_t addr)
182 return read_data32(addr);
184 virtual void write_dma_data8w(uint32_t addr, uint32_t data, int* wait)
186 write_data8w(addr, data, wait);
188 virtual uint32_t read_dma_data8w(uint32_t addr, int* wait)
190 return read_data8w(addr, wait);
192 virtual void write_dma_data16w(uint32_t addr, uint32_t data, int* wait)
194 write_data16w(addr, data, wait);
196 virtual uint32_t read_dma_data16w(uint32_t addr, int* wait)
198 return read_data16w(addr, wait);
200 virtual void write_dma_data32w(uint32_t addr, uint32_t data, int* wait)
202 write_data32w(addr, data, wait);
204 virtual uint32_t read_dma_data32w(uint32_t addr, int* wait)
206 return read_data32w(addr, wait);
210 virtual void write_io8(uint32_t addr, uint32_t data) {}
211 virtual uint32_t read_io8(uint32_t addr);
212 virtual void write_io16(uint32_t addr, uint32_t data)
214 write_io8(addr, data & 0xff);
215 write_io8(addr + 1, (data >> 8) & 0xff);
217 virtual uint32_t read_io16(uint32_t addr)
219 uint32_t val = read_io8(addr);
220 val |= read_io8(addr + 1) << 8;
223 virtual void write_io32(uint32_t addr, uint32_t data)
225 write_io16(addr, data & 0xffff);
226 write_io16(addr + 2, (data >> 16) & 0xffff);
228 virtual uint32_t read_io32(uint32_t addr)
230 uint32_t val = read_io16(addr);
231 val |= read_io16(addr + 2) << 16;
234 virtual void write_io8w(uint32_t addr, uint32_t data, int* wait)
237 write_io8(addr, data);
239 virtual uint32_t read_io8w(uint32_t addr, int* wait)
242 return read_io8(addr);
244 virtual void write_io16w(uint32_t addr, uint32_t data, int* wait)
247 write_io8w(addr, data & 0xff, &wait_l);
248 write_io8w(addr + 1, (data >> 8) & 0xff, &wait_h);
249 *wait = wait_l + wait_h;
251 virtual uint32_t read_io16w(uint32_t addr, int* wait)
254 uint32_t val = read_io8w(addr, &wait_l);
255 val |= read_io8w(addr + 1, &wait_h) << 8;
256 *wait = wait_l + wait_h;
259 virtual void write_io32w(uint32_t addr, uint32_t data, int* wait)
262 write_io16w(addr, data & 0xffff, &wait_l);
263 write_io16w(addr + 2, (data >> 16) & 0xffff, &wait_h);
264 *wait = wait_l + wait_h;
266 virtual uint32_t read_io32w(uint32_t addr, int* wait)
269 uint32_t val = read_io16w(addr, &wait_l);
270 val |= read_io16w(addr + 2, &wait_h) << 16;
271 *wait = wait_l + wait_h;
274 virtual void write_dma_io8(uint32_t addr, uint32_t data)
276 write_io8(addr, data);
278 virtual uint32_t read_dma_io8(uint32_t addr)
280 return read_io8(addr);
282 virtual void write_dma_io16(uint32_t addr, uint32_t data)
284 write_io16(addr, data);
286 virtual uint32_t read_dma_io16(uint32_t addr)
288 return read_io16(addr);
290 virtual void write_dma_io32(uint32_t addr, uint32_t data)
292 write_io32(addr, data);
294 virtual uint32_t read_dma_io32(uint32_t addr)
296 return read_io32(addr);
298 virtual void write_dma_io8w(uint32_t addr, uint32_t data, int* wait)
300 write_io8w(addr, data, wait);
302 virtual uint32_t read_dma_io8w(uint32_t addr, int* wait)
304 return read_io8w(addr, wait);
306 virtual void write_dma_io16w(uint32_t addr, uint32_t data, int* wait)
308 write_io16w(addr, data, wait);
310 virtual uint32_t read_dma_io16w(uint32_t addr, int* wait)
312 return read_io16w(addr, wait);
314 virtual void write_dma_io32w(uint32_t addr, uint32_t data, int* wait)
316 write_io32w(addr, data, wait);
318 virtual uint32_t read_dma_io32w(uint32_t addr, int* wait)
320 return read_io32w(addr, wait);
324 virtual void write_memory_mapped_io8(uint32_t addr, uint32_t data)
326 write_io8(addr, data);
328 virtual uint32_t read_memory_mapped_io8(uint32_t addr)
330 return read_io8(addr);
332 virtual void write_memory_mapped_io16(uint32_t addr, uint32_t data)
334 write_memory_mapped_io8(addr, data & 0xff);
335 write_memory_mapped_io8(addr + 1, (data >> 8) & 0xff);
337 virtual uint32_t read_memory_mapped_io16(uint32_t addr)
339 uint32_t val = read_memory_mapped_io8(addr);
340 val |= read_memory_mapped_io8(addr + 1) << 8;
343 virtual void write_memory_mapped_io32(uint32_t addr, uint32_t data)
345 write_memory_mapped_io16(addr, data & 0xffff);
346 write_memory_mapped_io16(addr + 2, (data >> 16) & 0xffff);
348 virtual uint32_t read_memory_mapped_io32(uint32_t addr)
350 uint32_t val = read_memory_mapped_io16(addr);
351 val |= read_memory_mapped_io16(addr + 2) << 16;
354 virtual void write_memory_mapped_io8w(uint32_t addr, uint32_t data, int* wait)
357 write_memory_mapped_io8(addr, data);
359 virtual uint32_t read_memory_mapped_io8w(uint32_t addr, int* wait)
362 return read_memory_mapped_io8(addr);
364 virtual void write_memory_mapped_io16w(uint32_t addr, uint32_t data, int* wait)
367 write_memory_mapped_io8w(addr, data & 0xff, &wait_l);
368 write_memory_mapped_io8w(addr + 1, (data >> 8) & 0xff, &wait_h);
369 *wait = wait_l + wait_h;
371 virtual uint32_t read_memory_mapped_io16w(uint32_t addr, int* wait)
374 uint32_t val = read_memory_mapped_io8w(addr, &wait_l);
375 val |= read_memory_mapped_io8w(addr + 1, &wait_h) << 8;
376 *wait = wait_l + wait_h;
379 virtual void write_memory_mapped_io32w(uint32_t addr, uint32_t data, int* wait)
382 write_memory_mapped_io16w(addr, data & 0xffff, &wait_l);
383 write_memory_mapped_io16w(addr + 2, (data >> 16) & 0xffff, &wait_h);
384 *wait = wait_l + wait_h;
386 virtual uint32_t read_memory_mapped_io32w(uint32_t addr, int* wait)
389 uint32_t val = read_memory_mapped_io16w(addr, &wait_l);
390 val |= read_memory_mapped_io16w(addr + 2, &wait_h) << 16;
391 *wait = wait_l + wait_h;
405 output_t item[MAX_OUTPUT];
408 virtual void initialize_output_signals(outputs_t *items)
412 virtual void register_output_signal(outputs_t *items, DEVICE *device, int id, uint32_t mask, int shift)
414 int c = items->count++;
415 items->item[c].device = device;
416 items->item[c].id = id;
417 items->item[c].mask = mask;
418 items->item[c].shift = shift;
420 virtual void register_output_signal(outputs_t *items, DEVICE *device, int id, uint32_t mask)
422 int c = items->count++;
423 items->item[c].device = device;
424 items->item[c].id = id;
425 items->item[c].mask = mask;
426 items->item[c].shift = 0;
428 virtual void write_signals(outputs_t *items, uint32_t data)
430 for(int i = 0; i < items->count; i++) {
431 output_t *item = &items->item[i];
432 int shift = item->shift;
433 uint32_t val = (shift < 0) ? (data >> (-shift)) : (data << shift);
434 uint32_t mask = (shift < 0) ? (item->mask >> (-shift)) : (item->mask << shift);
435 item->device->write_signal(item->id, val, mask);
438 virtual void write_signal(int id, uint32_t data, uint32_t mask) {}
439 virtual uint32_t read_signal(int ch)
445 virtual void set_context_intr(DEVICE* device, uint32_t bit) {}
446 virtual void set_context_child(DEVICE* device) {}
448 // interrupt device to device
449 virtual void set_intr_iei(bool val) {}
451 // interrupt device to cpu
452 virtual void set_intr_line(bool line, bool pending, uint32_t bit) {}
454 // interrupt cpu to device
455 virtual uint32_t get_intr_ack()
459 virtual void notify_intr_reti() {}
460 virtual void notify_intr_ei() {}
463 virtual void do_dma() {}
466 virtual int run(int clock)
468 // when clock == -1, run one opecode
469 return (clock == -1 ? 1 : clock);
471 virtual void set_extra_clock(int clock) {}
472 virtual int get_extra_clock()
476 virtual uint32_t get_pc()
480 virtual uint32_t get_next_pc()
486 virtual bool bios_call_far_i86(uint32_t PC, uint16_t regs[], uint16_t sregs[], int32_t* ZeroFlag, int32_t* CarryFlag)
490 virtual bool bios_int_i86(int intnum, uint16_t regs[], uint16_t sregs[], int32_t* ZeroFlag, int32_t* CarryFlag)
494 virtual bool bios_ret_z80(uint16_t PC, pair_t* af, pair_t* bc, pair_t* de, pair_t* hl, pair_t* ix, pair_t* iy, uint8_t* iff1) {
498 const _TCHAR *get_device_name(void)
500 return (const _TCHAR *)this_device_name;
504 DEVICE* event_manager;
506 virtual void set_context_event_manager(DEVICE* device)
508 event_manager = device;
510 virtual int get_event_manager_id();
511 virtual void register_event(DEVICE* device, int event_id, double usec, bool loop, int* register_id);
512 virtual void register_event_by_clock(DEVICE* device, int event_id, uint64_t clock, bool loop, int* register_id);
513 virtual void cancel_event(DEVICE* device, int register_id);
514 virtual void register_frame_event(DEVICE* device);
515 virtual void register_vline_event(DEVICE* device);
516 virtual uint32_t get_event_remaining_clock(int register_id);
517 virtual double get_event_remaining_usec(int register_id);
518 virtual uint32_t get_current_clock();
519 virtual uint32_t get_passed_clock(uint32_t prev);
520 virtual double get_passed_usec(uint32_t prev);
521 virtual uint32_t get_cpu_pc(int index);
522 virtual void request_skip_frames();
523 virtual void set_frames_per_sec(double frames);
524 virtual void set_lines_per_frame(int lines);
525 // Force render sound immediately when device's status has changed.
526 // You must call this after you changing registers (or enything).
527 // If has problems, try set_realtime_render.
528 // See mb8877.cpp and ym2203.cpp.
530 virtual void touch_sound(void);
531 // Force render per 1 sample automatically.
534 virtual void set_realtime_render(DEVICE *device, bool flag);
535 virtual void set_realtime_render(bool flag)
537 set_realtime_render(this, flag);
539 virtual void update_timing(int new_clocks, double new_frames_per_sec, int new_lines_per_frame) {}
542 virtual void event_callback(int event_id, int err) {}
543 virtual void event_pre_frame() {} // this event is to update timing settings
544 virtual void event_frame() {}
545 virtual void event_vline(int v, int clock) {}
546 virtual void event_hsync(int v, int h, int clock) {}
549 virtual void mix(int32_t* buffer, int cnt) {}
550 virtual void set_volume(int ch, int decibel_l, int decibel_r) {} // +1 equals +0.5dB (same as fmgen)
551 virtual void set_device_name(const _TCHAR *format, ...);
552 virtual void out_debug_log(const char *fmt, ...);
553 virtual void force_out_debug_log(const char *fmt, ...);
556 // DEBUGGER is enabled by default.
557 virtual void *get_debugger();
558 virtual uint32_t get_debug_prog_addr_mask();
559 virtual uint32_t get_debug_data_addr_mask();
560 virtual void write_debug_data8(uint32_t addr, uint32_t data);
561 virtual uint32_t read_debug_data8(uint32_t addr);
562 virtual void write_debug_data16(uint32_t addr, uint32_t data);
563 virtual uint32_t read_debug_data16(uint32_t addr);
564 virtual void write_debug_data32(uint32_t addr, uint32_t data);
565 virtual uint32_t read_debug_data32(uint32_t addr);
566 virtual void write_debug_io8(uint32_t addr, uint32_t data);
567 virtual uint32_t read_debug_io8(uint32_t addr);
568 virtual void write_debug_io16(uint32_t addr, uint32_t data);
569 virtual uint32_t read_debug_io16(uint32_t addr);
570 virtual void write_debug_io32(uint32_t addr, uint32_t data);
571 virtual uint32_t read_debug_io32(uint32_t addr);
572 virtual bool write_debug_reg(const _TCHAR *reg, uint32_t data);
573 virtual void get_debug_regs_info(_TCHAR *buffer, size_t buffer_len);
574 virtual int debug_dasm(uint32_t pc, _TCHAR *buffer, size_t buffer_len);
576 _TCHAR this_device_name[128];