OSDN Git Service

[VM][General] Merge Upstream 2017-12-15.
[csp-qt/common_source_project-fm7.git] / source / src / vm / huc6280.cpp
1 /*
2         Skelton for retropc emulator
3
4         Origin : MESS 0.147
5         Author : Takeda.Toshiya
6         Date   : 2012.10.23-
7
8         [ HuC6280 ]
9 */
10
11 #include "huc6280.h"
12 #ifdef USE_DEBUGGER
13 #include "debugger.h"
14 #endif
15
16 /* ----------------------------------------------------------------------------
17         MAME h6280
18 ---------------------------------------------------------------------------- */
19
20 #define INLINE inline
21 #define PAIR pair_t
22 #define offs_t UINT16
23
24 /*****************************************************************************/
25 /* src/emu/devcpu.h */
26
27 // CPU interface functions
28 #define READ8_HANDLER(name)                     UINT8 name(h6280_Regs *cpustate, offs_t offset)
29 #define WRITE8_HANDLER(name)                    void name(h6280_Regs *cpustate, offs_t offset, UINT8 data)
30
31 #include "mame/emu/cpu/h6280/h6280.h"
32 //#include "mame/emu/cpu/h6280/h6280.c"
33 //#ifdef USE_DEBUGGER
34 //#include "mame/emu/cpu/h6280/6280dasm.c"
35 //#endif
36
37
38 void HUC6280::initialize()
39 {
40         HUC6280_BASE::initialize();
41         
42 #ifdef USE_DEBUGGER
43         h6280_Regs *cpustate = (h6280_Regs *)opaque;
44         cpustate->emu = emu;
45         cpustate->debugger = d_debugger;
46         cpustate->program_stored = d_mem;
47         cpustate->io_stored = d_io;
48         
49         d_debugger->set_context_mem(d_mem);
50         d_debugger->set_context_io(d_io);
51 #endif
52 }
53
54 void HUC6280::release()
55 {
56         free(opaque);
57 }
58
59 void HUC6280::reset()
60 {
61         HUC6280_BASE::reset();
62 #ifdef USE_DEBUGGER
63         h6280_Regs *cpustate = (h6280_Regs *)opaque;
64         cpustate->emu = emu;
65         cpustate->debugger = d_debugger;
66         cpustate->program_stored = d_mem;
67         cpustate->io_stored = d_io;
68 #endif
69 }
70
71 int HUC6280::run(int clock)
72 {
73         h6280_Regs *cpustate = (h6280_Regs *)opaque;
74         
75         if(clock == -1) {
76                 if(busreq) {
77                         // don't run cpu!
78 #ifdef USE_DEBUGGER
79                         total_icount += 1;
80 #endif
81                         return 1;
82                 } else {
83                         // run only one opcode
84                         return run_one_opecode();
85                 }
86         } else {
87                 icount += clock;
88                 int first_icount = icount;
89                 
90                 // run cpu while given clocks
91                 while(icount > 0 && !busreq) {
92                         icount -= run_one_opecode();
93                 }
94                 // if busreq is raised, spin cpu while remained clock
95                 if(icount > 0 && busreq) {
96 #ifdef USE_DEBUGGER
97                         total_icount += icount;
98 #endif
99                         icount = 0;
100                 }
101                 return first_icount - icount;
102         }
103 }
104
105 int HUC6280::run_one_opecode()
106 {
107         h6280_Regs *cpustate = (h6280_Regs *)opaque;
108 #ifdef USE_DEBUGGER
109         d_debugger->add_cpu_trace(cpustate->pc.w.l);
110 #endif
111         int passed_icount = HUC6280_BASE::run_one_opecode();
112 #ifdef USE_DEBUGGER
113         total_icount += passed_icount;
114 #endif
115         return passed_icount;
116 }
117
118 #define STATE_VERSION   5
119
120 void HUC6280::save_state(FILEIO* state_fio)
121 {
122         state_fio->FputUint32(STATE_VERSION);
123         state_fio->FputInt32(this_device_id);
124         
125
126         save_state_registers(state_fio);
127 #ifdef USE_DEBUGGER
128         state_fio->FputUint64(total_icount);
129 #endif
130         state_fio->FputInt32(icount);
131         state_fio->FputBool(busreq);
132 }
133
134 bool HUC6280::load_state(FILEIO* state_fio)
135 {
136         if(state_fio->FgetUint32() != STATE_VERSION) {
137                 return false;
138         }
139         if(state_fio->FgetInt32() != this_device_id) {
140                 return false;
141         }
142 #ifdef USE_DEBUGGER
143         total_icount = prev_total_icount = state_fio->FgetUint64();
144 #endif
145         load_state_registers(state_fio);
146         icount = state_fio->FgetInt32();
147         busreq = state_fio->FgetBool();
148
149         // post process   
150         h6280_Regs *cpustate = (h6280_Regs *)opaque;
151         cpustate->program = d_mem;
152         cpustate->io = d_io;
153 #ifdef USE_DEBUGGER
154         cpustate->emu = emu;
155         cpustate->debugger = d_debugger;
156         cpustate->program_stored = d_mem;
157         cpustate->io_stored = d_io;
158 #endif
159         return true;
160 }