OSDN Git Service

[VM][HuC6280] Add HUC6280:: to libCSPcommon_vm .
[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 // main
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                         return 1;
79                 } else {
80                         // run only one opcode
81 #ifdef USE_DEBUGGER
82                         return exec_call_debug();
83 #else
84                         return exec_call();
85 #endif
86                 }
87         } else {
88                 icount += clock;
89                 int first_icount = icount;
90                 
91                 // run cpu while given clocks
92                 while(icount > 0 && !busreq) {
93 #ifdef USE_DEBUGGER
94                         icount -= exec_call_debug();
95 #else
96                         icount -= exec_call();
97 #endif
98                 }
99                 // if busreq is raised, spin cpu while remained clock
100                 if(icount > 0 && busreq) {
101                         icount = 0;
102                 }
103                 return first_icount - icount;
104         }
105 }
106
107 #define STATE_VERSION   4
108
109 void HUC6280::save_state(FILEIO* state_fio)
110 {
111         state_fio->FputUint32(STATE_VERSION);
112         state_fio->FputInt32(this_device_id);
113         
114         //state_fio->Fwrite(opaque, sizeof(h6280_Regs), 1);
115         save_state_registers(state_fio);
116         state_fio->FputInt32(icount);
117         state_fio->FputBool(busreq);
118 }
119
120 bool HUC6280::load_state(FILEIO* state_fio)
121 {
122         if(state_fio->FgetUint32() != STATE_VERSION) {
123                 return false;
124         }
125         if(state_fio->FgetInt32() != this_device_id) {
126                 return false;
127         }
128         //state_fio->Fread(opaque, sizeof(h6280_Regs), 1);
129         load_state_registers(state_fio);
130         icount = state_fio->FgetInt32();
131         busreq = state_fio->FgetBool();
132
133         // post process   
134         h6280_Regs *cpustate = (h6280_Regs *)opaque;
135         cpustate->program = d_mem;
136         cpustate->io = d_io;
137 #ifdef USE_DEBUGGER
138         cpustate->emu = emu;
139         cpustate->debugger = d_debugger;
140         cpustate->program_stored = d_mem;
141         cpustate->io_stored = d_io;
142 #endif
143         return true;
144 }