2 Skelton for retropc emulator
4 Origin : MAME i286 core
5 Author : Takeda.Toshiya
16 /* ----------------------------------------------------------------------------
18 ---------------------------------------------------------------------------- */
20 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
21 #pragma warning( disable : 4018 )
22 #pragma warning( disable : 4146 )
23 #pragma warning( disable : 4244 )
24 #pragma warning( disable : 4996 )
28 #define CPU_MODEL i8086
29 #elif defined(HAS_I88)
30 #define CPU_MODEL i8088
31 #elif defined(HAS_I186)
32 #define CPU_MODEL i80186
33 #elif defined(HAS_V30)
35 #elif defined(HAS_I286)
36 #define CPU_MODEL i80286
39 #ifndef __BIG_ENDIAN__
49 /*****************************************************************************/
50 /* src/emu/devcpu.h */
52 // CPU interface functions
53 #define CPU_INIT_NAME(name) cpu_init_##name
54 #define CPU_INIT(name) void* CPU_INIT_NAME(name)()
55 #define CPU_INIT_CALL(name) CPU_INIT_NAME(name)()
57 #define CPU_RESET_NAME(name) cpu_reset_##name
58 #define CPU_RESET(name) void CPU_RESET_NAME(name)(cpu_state *cpustate)
59 #define CPU_RESET_CALL(name) CPU_RESET_NAME(name)(cpustate)
61 #define CPU_EXECUTE_NAME(name) cpu_execute_##name
62 #define CPU_EXECUTE(name) int CPU_EXECUTE_NAME(name)(cpu_state *cpustate, int icount)
63 #define CPU_EXECUTE_CALL(name) CPU_EXECUTE_NAME(name)(cpustate, icount)
65 #define CPU_DISASSEMBLE_NAME(name) cpu_disassemble_##name
66 #define CPU_DISASSEMBLE(name) int CPU_DISASSEMBLE_NAME(name)(_TCHAR *buffer, offs_t eip, const UINT8 *oprom)
67 #define CPU_DISASSEMBLE_CALL(name) CPU_DISASSEMBLE_NAME(name)(buffer, eip, oprom)
69 /*****************************************************************************/
70 /* src/emu/didisasm.h */
72 // Disassembler constants
73 const UINT32 DASMFLAG_SUPPORTED = 0x80000000; // are disassembly flags supported?
74 const UINT32 DASMFLAG_STEP_OUT = 0x40000000; // this instruction should be the end of a step out sequence
75 const UINT32 DASMFLAG_STEP_OVER = 0x20000000; // this instruction should be stepped over by setting a breakpoint afterwards
76 const UINT32 DASMFLAG_OVERINSTMASK = 0x18000000; // number of extra instructions to skip when stepping over
77 const UINT32 DASMFLAG_OVERINSTSHIFT = 27; // bits to shift after masking to get the value
78 const UINT32 DASMFLAG_LENGTHMASK = 0x0000ffff; // the low 16-bits contain the actual length
80 /*****************************************************************************/
81 /* src/emu/diexec.h */
86 CLEAR_LINE = 0, // clear (a fired or held) line
87 ASSERT_LINE, // assert an interrupt immediately
88 HOLD_LINE, // hold interrupt line until acknowledged
89 PULSE_LINE // pulse interrupt line instantaneously (only for NMI, RESET)
98 /*****************************************************************************/
99 /* src/emu/emucore.h */
101 // constants for expression endianness
108 // declare native endianness to be one or the other
110 const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_LITTLE;
112 const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_BIG;
114 // endian-based value: first value is if 'endian' is little-endian, second is if 'endian' is big-endian
115 #define ENDIAN_VALUE_LE_BE(endian,leval,beval) (((endian) == ENDIANNESS_LITTLE) ? (leval) : (beval))
116 // endian-based value: first value is if native endianness is little-endian, second is if native is big-endian
117 #define NATIVE_ENDIAN_VALUE_LE_BE(leval,beval) ENDIAN_VALUE_LE_BE(ENDIANNESS_NATIVE, leval, beval)
118 // endian-based value: first value is if 'endian' matches native, second is if 'endian' doesn't match native
119 #define ENDIAN_VALUE_NE_NNE(endian,leval,beval) (((endian) == ENDIANNESS_NATIVE) ? (neval) : (nneval))
121 /*****************************************************************************/
122 /* src/emu/memory.h */
124 // offsets and addresses are 32-bit (for now...)
125 typedef UINT32 offs_t;
127 /*****************************************************************************/
128 /* src/osd/osdcomm.h */
130 /* Highly useful macro for compile-time knowledge of an array size */
131 #define ARRAY_LENGTH(x) (sizeof(x) / sizeof(x[0]))
133 #if defined(HAS_I86) || defined(HAS_I88) || defined(HAS_I186) || defined(HAS_V30)
134 #define cpu_state i8086_state
135 #include "mame/emu/cpu/i86/i86.c"
136 #elif defined(HAS_I286)
137 #define cpu_state i80286_state
138 #include "mame/emu/cpu/i86/i286.c"
142 #include "mame/emu/cpu/nec/necdasm.c"
144 #include "mame/emu/cpu/i386/i386dasm.c"
148 void I286::initialize()
150 DEVICE::initialize();
151 opaque = CPU_INIT_CALL(CPU_MODEL);
153 cpu_state *cpustate = (cpu_state *)opaque;
154 cpustate->pic = d_pic;
155 cpustate->program = d_mem;
157 #ifdef I86_PSEUDO_BIOS
158 cpustate->bios = d_bios;
160 #ifdef SINGLE_MODE_DMA
161 cpustate->dma = d_dma;
165 cpustate->debugger = d_debugger;
166 cpustate->program_stored = d_mem;
167 cpustate->io_stored = d_io;
169 d_debugger->set_context_mem(d_mem);
170 d_debugger->set_context_io(d_io);
181 cpu_state *cpustate = (cpu_state *)opaque;
182 int busreq = cpustate->busreq;
184 CPU_RESET_CALL(CPU_MODEL);
186 cpustate->pic = d_pic;
187 cpustate->program = d_mem;
189 #ifdef I86_PSEUDO_BIOS
190 cpustate->bios = d_bios;
192 #ifdef SINGLE_MODE_DMA
193 cpustate->dma = d_dma;
197 cpustate->debugger = d_debugger;
198 cpustate->program_stored = d_mem;
199 cpustate->io_stored = d_io;
201 cpustate->busreq = busreq;
204 int I286::run(int icount)
206 cpu_state *cpustate = (cpu_state *)opaque;
207 return CPU_EXECUTE_CALL(CPU_MODEL);
210 void I286::write_signal(int id, uint32_t data, uint32_t mask)
212 cpu_state *cpustate = (cpu_state *)opaque;
214 if(id == SIG_CPU_NMI) {
215 set_irq_line(cpustate, INPUT_LINE_NMI, (data & mask) ? HOLD_LINE : CLEAR_LINE);
216 } else if(id == SIG_CPU_IRQ) {
217 set_irq_line(cpustate, INPUT_LINE_IRQ, (data & mask) ? HOLD_LINE : CLEAR_LINE);
218 } else if(id == SIG_CPU_BUSREQ) {
219 cpustate->busreq = (data & mask) ? 1 : 0;
220 } else if(id == SIG_I86_TEST) {
221 cpustate->test_state = (data & mask) ? 1 : 0;
223 } else if(id == SIG_I286_A20) {
224 i80286_set_a20_line(cpustate, data & mask);
229 void I286::set_intr_line(bool line, bool pending, uint32_t bit)
231 cpu_state *cpustate = (cpu_state *)opaque;
232 set_irq_line(cpustate, INPUT_LINE_IRQ, line ? HOLD_LINE : CLEAR_LINE);
235 void I286::set_extra_clock(int icount)
237 cpu_state *cpustate = (cpu_state *)opaque;
238 cpustate->extra_cycles += icount;
241 int I286::get_extra_clock()
243 cpu_state *cpustate = (cpu_state *)opaque;
244 return cpustate->extra_cycles;
247 uint32_t I286::get_pc()
249 cpu_state *cpustate = (cpu_state *)opaque;
250 return cpustate->prevpc;
253 uint32_t I286::get_next_pc()
255 cpu_state *cpustate = (cpu_state *)opaque;
260 void I286::write_debug_data8(uint32_t addr, uint32_t data)
263 d_mem->write_data8w(addr, data, &wait);
266 uint32_t I286::read_debug_data8(uint32_t addr)
269 return d_mem->read_data8w(addr, &wait);
272 void I286::write_debug_data16(uint32_t addr, uint32_t data)
275 d_mem->write_data16w(addr, data, &wait);
278 uint32_t I286::read_debug_data16(uint32_t addr)
281 return d_mem->read_data16w(addr, &wait);
284 void I286::write_debug_io8(uint32_t addr, uint32_t data)
287 d_io->write_io8w(addr, data, &wait);
290 uint32_t I286::read_debug_io8(uint32_t addr) {
292 return d_io->read_io8w(addr, &wait);
295 void I286::write_debug_io16(uint32_t addr, uint32_t data)
298 d_io->write_io16w(addr, data, &wait);
301 uint32_t I286::read_debug_io16(uint32_t addr) {
303 return d_io->read_io16w(addr, &wait);
306 bool I286::write_debug_reg(const _TCHAR *reg, uint32_t data)
308 cpu_state *cpustate = (cpu_state *)opaque;
309 if(_tcsicmp(reg, _T("IP")) == 0) {
310 cpustate->pc = ((data & 0xffff) + cpustate->base[CS]) & AMASK;
311 CHANGE_PC(cpustate->pc);
312 } else if(_tcsicmp(reg, _T("AX")) == 0) {
313 cpustate->regs.w[AX] = data;
314 } else if(_tcsicmp(reg, _T("BX")) == 0) {
315 cpustate->regs.w[BX] = data;
316 } else if(_tcsicmp(reg, _T("CX")) == 0) {
317 cpustate->regs.w[CX] = data;
318 } else if(_tcsicmp(reg, _T("DX")) == 0) {
319 cpustate->regs.w[DX] = data;
320 } else if(_tcsicmp(reg, _T("SP")) == 0) {
321 cpustate->regs.w[SP] = data;
322 } else if(_tcsicmp(reg, _T("BP")) == 0) {
323 cpustate->regs.w[BP] = data;
324 } else if(_tcsicmp(reg, _T("SI")) == 0) {
325 cpustate->regs.w[SI] = data;
326 } else if(_tcsicmp(reg, _T("DI")) == 0) {
327 cpustate->regs.w[DI] = data;
328 } else if(_tcsicmp(reg, _T("AL")) == 0) {
329 cpustate->regs.b[AL] = data;
330 } else if(_tcsicmp(reg, _T("AH")) == 0) {
331 cpustate->regs.b[AH] = data;
332 } else if(_tcsicmp(reg, _T("BL")) == 0) {
333 cpustate->regs.b[BL] = data;
334 } else if(_tcsicmp(reg, _T("BH")) == 0) {
335 cpustate->regs.b[BH] = data;
336 } else if(_tcsicmp(reg, _T("CL")) == 0) {
337 cpustate->regs.b[CL] = data;
338 } else if(_tcsicmp(reg, _T("CH")) == 0) {
339 cpustate->regs.b[CH] = data;
340 } else if(_tcsicmp(reg, _T("DL")) == 0) {
341 cpustate->regs.b[DL] = data;
342 } else if(_tcsicmp(reg, _T("DH")) == 0) {
343 cpustate->regs.b[DH] = data;
350 uint32_t I286::read_debug_reg(const _TCHAR *reg)
352 cpu_state *cpustate = (cpu_state *)opaque;
353 if(_tcsicmp(reg, _T("IP")) == 0) {
354 return cpustate->pc - cpustate->base[CS];
355 } else if(_tcsicmp(reg, _T("AX")) == 0) {
356 return cpustate->regs.w[AX];
357 } else if(_tcsicmp(reg, _T("BX")) == 0) {
358 return cpustate->regs.w[BX];
359 } else if(_tcsicmp(reg, _T("CX")) == 0) {
360 return cpustate->regs.w[CX];
361 } else if(_tcsicmp(reg, _T("DX")) == 0) {
362 return cpustate->regs.w[DX];
363 } else if(_tcsicmp(reg, _T("SP")) == 0) {
364 return cpustate->regs.w[SP];
365 } else if(_tcsicmp(reg, _T("BP")) == 0) {
366 return cpustate->regs.w[BP];
367 } else if(_tcsicmp(reg, _T("SI")) == 0) {
368 return cpustate->regs.w[SI];
369 } else if(_tcsicmp(reg, _T("DI")) == 0) {
370 return cpustate->regs.w[DI];
371 } else if(_tcsicmp(reg, _T("AL")) == 0) {
372 return cpustate->regs.b[AL];
373 } else if(_tcsicmp(reg, _T("AH")) == 0) {
374 return cpustate->regs.b[AH];
375 } else if(_tcsicmp(reg, _T("BL")) == 0) {
376 return cpustate->regs.b[BL];
377 } else if(_tcsicmp(reg, _T("BH")) == 0) {
378 return cpustate->regs.b[BH];
379 } else if(_tcsicmp(reg, _T("CL")) == 0) {
380 return cpustate->regs.b[CL];
381 } else if(_tcsicmp(reg, _T("CH")) == 0) {
382 return cpustate->regs.b[CH];
383 } else if(_tcsicmp(reg, _T("DL")) == 0) {
384 return cpustate->regs.b[DL];
385 } else if(_tcsicmp(reg, _T("DH")) == 0) {
386 return cpustate->regs.b[DH];
391 void I286::get_debug_regs_info(_TCHAR *buffer, size_t buffer_len)
393 cpu_state *cpustate = (cpu_state *)opaque;
394 my_stprintf_s(buffer, buffer_len,
395 _T("AX=%04X BX=%04X CX=%04X DX=%04X SP=%04X BP=%04X SI=%04X DI=%04X\nDS=%04X ES=%04X SS=%04X CS=%04X IP=%04X FLAG=[%c%c%c%c%c%c%c%c%c]\nClocks = %llu (%llu) Since Scanline = %d/%d (%d/%d)"),
396 cpustate->regs.w[AX], cpustate->regs.w[BX], cpustate->regs.w[CX], cpustate->regs.w[DX], cpustate->regs.w[SP], cpustate->regs.w[BP], cpustate->regs.w[SI], cpustate->regs.w[DI],
397 cpustate->sregs[DS], cpustate->sregs[ES], cpustate->sregs[SS], cpustate->sregs[CS], cpustate->pc - cpustate->base[CS],
398 OF ? _T('O') : _T('-'), DF ? _T('D') : _T('-'), cpustate->IF ? _T('I') : _T('-'), cpustate->TF ? _T('T') : _T('-'),
399 SF ? _T('S') : _T('-'), ZF ? _T('Z') : _T('-'), AF ? _T('A') : _T('-'), PF ? _T('P') : _T('-'), CF ? _T('C') : _T('-'),
400 cpustate->total_icount, cpustate->total_icount - cpustate->prev_total_icount,
401 get_passed_clock_since_vline(), get_cur_vline_clocks(), get_cur_vline(), get_lines_per_frame());
402 cpustate->prev_total_icount = cpustate->total_icount;
405 int I286::debug_dasm(uint32_t pc, _TCHAR *buffer, size_t buffer_len)
407 cpu_state *cpustate = (cpu_state *)opaque;
408 UINT64 eip = pc - cpustate->base[CS];
410 for(int i = 0; i < 16; i++) {
412 ops[i] = d_mem->read_data8w(pc + i, &wait);
417 return CPU_DISASSEMBLE_CALL(nec_generic) & DASMFLAG_LENGTHMASK;
419 return CPU_DISASSEMBLE_CALL(x86_16) & DASMFLAG_LENGTHMASK;
425 void I286::set_address_mask(uint32_t mask)
427 cpu_state *cpustate = (cpu_state *)opaque;
428 cpustate->amask = mask;
431 uint32_t I286::get_address_mask()
433 cpu_state *cpustate = (cpu_state *)opaque;
434 return cpustate->amask;
437 void I286::set_shutdown_flag(int shutdown)
439 cpu_state *cpustate = (cpu_state *)opaque;
440 cpustate->shutdown = shutdown;
443 int I286::get_shutdown_flag()
445 cpu_state *cpustate = (cpu_state *)opaque;
446 return cpustate->shutdown;
450 #define STATE_VERSION 5
452 void I286::process_state_cpustate(FILEIO* state_fio, bool loading)
454 #if defined(HAS_I86) || defined(HAS_I88) || defined(HAS_I186) || defined(HAS_V30)
455 struct i8086_state *cpustate = (struct i8086_state *)opaque;
457 state_fio->StateBuffer(&(cpustate->regs.b[0]), 16, 1);
458 state_fio->StateUint32(cpustate->pc);
459 state_fio->StateUint32(cpustate->prevpc);
460 for(int i = 0; i < 4; i++) {
461 state_fio->StateUint32(cpustate->base[i]);
463 for(int i = 0; i < 4; i++) {
464 state_fio->StateUint16(cpustate->sregs[i]);
466 state_fio->StateUint16(cpustate->flags);
467 state_fio->StateInt32(cpustate->AuxVal);
468 state_fio->StateInt32(cpustate->OverVal);
469 state_fio->StateInt32(cpustate->SignVal);
470 state_fio->StateInt32(cpustate->ZeroVal);
471 state_fio->StateInt32(cpustate->CarryVal);
472 state_fio->StateInt32(cpustate->DirVal);
473 state_fio->StateUint8(cpustate->ParityVal);
474 state_fio->StateUint8(cpustate->TF);
475 state_fio->StateUint8(cpustate->IF);
476 state_fio->StateUint8(cpustate->MF);
478 state_fio->StateUint8(cpustate->int_vector);
479 state_fio->StateInt8(cpustate->nmi_state);
480 state_fio->StateInt8(cpustate->irq_state);
481 state_fio->StateInt8(cpustate->test_state);
482 state_fio->StateUint8(cpustate->rep_in_progress);
483 state_fio->StateInt32(cpustate->extra_cycles);
485 state_fio->StateInt32(cpustate->halted);
486 state_fio->StateInt32(cpustate->busreq);
488 state_fio->StateUint16(cpustate->ip);
489 state_fio->StateUint32(cpustate->sp);
492 state_fio->StateUint64(cpustate->total_icount);
494 state_fio->StateInt32(cpustate->icount);
496 state_fio->StateUint8(cpustate->seg_prefix);
497 state_fio->StateUint8(cpustate->prefix_seg);
498 state_fio->StateUint32(cpustate->ea);
499 state_fio->StateUint16(cpustate->eo);
500 state_fio->StateUint8(cpustate->ea_seg);
502 #elif defined(HAS_I286)
504 struct i80286_state *cpustate = (struct i80286_state *)opaque;
506 state_fio->StateBuffer(&(cpustate->regs.b[0]), 16, 1);
507 state_fio->StateUint32(cpustate->amask);
508 state_fio->StateUint32(cpustate->pc);
509 state_fio->StateUint32(cpustate->prevpc);
510 state_fio->StateUint16(cpustate->flags);
511 state_fio->StateUint16(cpustate->msw);
512 for(int i = 0; i < 4; i++) {
513 state_fio->StateUint32(cpustate->base[i]);
515 for(int i = 0; i < 4; i++) {
516 state_fio->StateUint16(cpustate->sregs[i]);
518 for(int i = 0; i < 4; i++) {
519 state_fio->StateUint16(cpustate->limit[i]);
521 state_fio->StateBuffer(cpustate->rights, 4, 1);
522 for(int i = 0; i < 4; i++) {
523 state_fio->StateBool(cpustate->valid[i]);
525 state_fio->StateUint32(cpustate->gdtr.base);
526 state_fio->StateUint16(cpustate->gdtr.limit);
528 state_fio->StateUint32(cpustate->idtr.base);
529 state_fio->StateUint16(cpustate->idtr.limit);
531 state_fio->StateUint16(cpustate->ldtr.sel);
532 state_fio->StateUint32(cpustate->ldtr.base);
533 state_fio->StateUint16(cpustate->ldtr.limit);
534 state_fio->StateUint8(cpustate->ldtr.rights);
536 state_fio->StateUint16(cpustate->tr.sel);
537 state_fio->StateUint32(cpustate->tr.base);
538 state_fio->StateUint16(cpustate->tr.limit);
539 state_fio->StateUint8(cpustate->tr.rights);
541 state_fio->StateInt32(cpustate->AuxVal);
542 state_fio->StateInt32(cpustate->OverVal);
543 state_fio->StateInt32(cpustate->SignVal);
544 state_fio->StateInt32(cpustate->ZeroVal);
545 state_fio->StateInt32(cpustate->CarryVal);
546 state_fio->StateInt32(cpustate->DirVal);
547 state_fio->StateUint8(cpustate->ParityVal);
548 state_fio->StateUint8(cpustate->TF);
549 state_fio->StateUint8(cpustate->IF);
550 state_fio->StateUint8(cpustate->MF);
552 state_fio->StateInt8(cpustate->nmi_state);
553 state_fio->StateInt8(cpustate->irq_state);
554 state_fio->StateInt8(cpustate->test_state);
555 state_fio->StateUint8(cpustate->rep_in_progress);
556 state_fio->StateInt32(cpustate->extra_cycles);
558 state_fio->StateInt32(cpustate->halted);
559 state_fio->StateInt32(cpustate->busreq);
560 state_fio->StateInt32(cpustate->trap_level);
561 state_fio->StateInt32(cpustate->shutdown);
565 state_fio->StateUint64(cpustate->total_icount);
567 state_fio->StateInt32(cpustate->icount);
569 state_fio->StateUint8(cpustate->seg_prefix);
570 state_fio->StateUint8(cpustate->prefix_seg);
571 state_fio->StateUint32(cpustate->ea);
572 state_fio->StateUint16(cpustate->eo);
573 state_fio->StateUint8(cpustate->ea_seg);
577 bool I286::process_state(FILEIO* state_fio, bool loading)
579 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
582 if(!state_fio->StateCheckInt32(this_device_id)) {
585 //state_fio->StateBuffer(opaque, sizeof(cpu_state), 1);
586 process_state_cpustate(state_fio, loading);
590 cpu_state *cpustate = (cpu_state *)opaque;
591 cpustate->pic = d_pic;
592 cpustate->program = d_mem;
594 #ifdef I86_PSEUDO_BIOS
595 cpustate->bios = d_bios;
597 #ifdef SINGLE_MODE_DMA
598 cpustate->dma = d_dma;
602 cpustate->debugger = d_debugger;
603 cpustate->program_stored = d_mem;
604 cpustate->io_stored = d_io;
605 cpustate->prev_total_icount = cpustate->total_icount;