libsidplayfp  1.4.2
mos6510.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2013 Leandro Nini <drfiemost@users.sourceforge.net>
5  * Copyright 2007-2010 Antti Lankila
6  * Copyright 2000 Simon White
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22 
23 #ifndef MOS6510_H
24 #define MOS6510_H
25 
26 #include <stdint.h>
27 #include <cstdio>
28 
29 #include "sidplayfp/EventScheduler.h"
30 
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34 
35 class EventContext;
36 
46 class MOS6510
47 {
48  friend class MOS6510Debug;
49 private:
50  static const char *credit;
51 
52 private:
58  static const int MAX = 65536;
59 
61  static const uint8_t SP_PAGE = 0x01;
62 
63 public:
65  static const int SR_INTERRUPT = 2;
66 
67 protected:
69  {
70  void (MOS6510::*func)();
71  bool nosteal;
72  ProcessorCycle () :
73  func(0),
74  nosteal(false) {}
75  };
76 
77 protected:
80 
83 
86 
89 
91  bool nmiFlag;
92 
94  bool rstFlag;
95 
97  bool rdy;
98 
99  bool flagN;
100  bool flagC;
101  bool flagD;
102  bool flagZ;
103  bool flagV;
104  bool flagI;
105  bool flagB;
106 
107  /* Data regarding current instruction */
108  uint_least16_t Register_ProgramCounter;
109  uint_least16_t Cycle_EffectiveAddress;
110  uint_least16_t Cycle_HighByteWrongEffectiveAddress;
111  uint_least16_t Cycle_Pointer;
112 
113  uint8_t Cycle_Data;
114  uint8_t Register_StackPointer;
115  uint8_t Register_Accumulator;
116  uint8_t Register_X;
117  uint8_t Register_Y;
118 
119 #ifdef DEBUG
120  /* Debug info */
121  uint_least16_t instrStartPC;
122  uint_least16_t instrOperand;
123 
124  FILE *m_fdbg;
125 
126  bool dodump;
127 #endif
128 
130  struct ProcessorCycle instrTable[0x101 << 3];
131 
132 protected:
133  MOS6510(EventContext *context);
134  ~MOS6510() {}
135 
138 
141 
142  void eventWithoutSteals();
143  void eventWithSteals();
144 
145  void Initialise();
146 
147  // Flag utility functions
148  inline void setFlagsNZ(uint8_t value);
149  inline uint8_t getStatusRegister();
150  inline void setStatusRegister(uint8_t sr);
151 
152  // Declare Interrupt Routines
153  inline void IRQLoRequest();
154  inline void IRQHiRequest();
155  inline void interruptsAndNextOpcode();
156  inline void calculateInterruptTriggerCycle();
157 
158  // Declare Instruction Routines
159  inline void fetchNextOpcode();
160  inline void throwAwayFetch();
161  inline void throwAwayRead();
162  inline void FetchDataByte();
163  inline void FetchLowAddr();
164  inline void FetchLowAddrX();
165  inline void FetchLowAddrY();
166  inline void FetchHighAddr();
167  inline void FetchHighAddrX();
168  inline void FetchHighAddrX2();
169  inline void FetchHighAddrY();
170  inline void FetchHighAddrY2();
171  inline void FetchLowEffAddr();
172  inline void FetchHighEffAddr();
173  inline void FetchHighEffAddrY();
174  inline void FetchHighEffAddrY2();
175  inline void FetchLowPointer();
176  inline void FetchLowPointerX();
177  inline void FetchHighPointer();
178  inline void FetchEffAddrDataByte ();
179  inline void PutEffAddrDataByte();
180  inline void PushLowPC();
181  inline void PushHighPC();
182  inline void PushSR();
183  inline void PopLowPC();
184  inline void PopHighPC();
185  inline void PopSR();
186  inline void brkPushLowPC();
187  inline void WasteCycle();
188 
189  // Delcare Instruction Operation Routines
190  inline void adc_instr();
191  inline void alr_instr();
192  inline void anc_instr();
193  inline void and_instr();
194  inline void ane_instr();
195  inline void arr_instr();
196  inline void asl_instr();
197  inline void asla_instr();
198  inline void aso_instr();
199  inline void axa_instr();
200  inline void axs_instr();
201  inline void bcc_instr();
202  inline void bcs_instr();
203  inline void beq_instr();
204  inline void bit_instr();
205  inline void bmi_instr();
206  inline void bne_instr();
207  inline void branch_instr(bool condition);
208  inline void bpl_instr();
209  inline void brk_instr();
210  inline void bvc_instr();
211  inline void bvs_instr();
212  inline void clc_instr();
213  inline void cld_instr();
214  inline void cli_instr();
215  inline void clv_instr();
216  inline void cmp_instr();
217  inline void cpx_instr();
218  inline void cpy_instr();
219  inline void dcm_instr();
220  inline void dec_instr();
221  inline void dex_instr();
222  inline void dey_instr();
223  inline void eor_instr();
224  inline void inc_instr();
225  inline void ins_instr();
226  inline void inx_instr();
227  inline void iny_instr();
228  inline void jmp_instr();
229  inline void las_instr();
230  inline void lax_instr();
231  inline void lda_instr();
232  inline void ldx_instr();
233  inline void ldy_instr();
234  inline void lse_instr();
235  inline void lsr_instr();
236  inline void lsra_instr();
237  inline void oal_instr();
238  inline void ora_instr();
239  inline void pha_instr();
240  inline void pla_instr();
241  inline void plp_instr();
242  inline void rla_instr();
243  inline void rol_instr();
244  inline void rola_instr();
245  inline void ror_instr();
246  inline void rora_instr();
247  inline void rra_instr();
248  inline void rti_instr();
249  inline void rts_instr();
250  inline void sbx_instr();
251  inline void say_instr();
252  inline void sbc_instr();
253  inline void sec_instr();
254  inline void sed_instr();
255  inline void sei_instr();
256  inline void shs_instr();
257  inline void sta_instr();
258  inline void stx_instr();
259  inline void sty_instr();
260  inline void tax_instr();
261  inline void tay_instr();
262  inline void tsx_instr();
263  inline void txa_instr();
264  inline void txs_instr();
265  inline void tya_instr();
266  inline void xas_instr();
267 
268  void illegal_instr();
269 
270  // Declare Arithmetic Operations
271  inline void doADC();
272  inline void doSBC();
273 
274  inline void doJSR();
275 
276 public:
283  virtual uint8_t cpuRead(uint_least16_t addr) =0;
284 
291  virtual void cpuWrite(uint_least16_t addr, uint8_t data) =0;
292 
293 #ifdef PC64_TESTSUITE
294  virtual void loadFile (const char *file) =0;
295 #endif
296 
297  virtual void reset();
298 
299  const char *credits() const { return credit; }
300 
301  void debug(bool enable, FILE *out);
302  void setRDY(bool newRDY);
303 
304  // Non-standard functions
305  void triggerRST();
306  void triggerNMI();
307  void triggerIRQ();
308  void clearIRQ();
309 };
310 
311 #endif // MOS6510_H
void PushLowPC()
Definition: mos6510.cpp:619
MOS6510(EventContext *context)
Definition: mos6510.cpp:1522
void setRDY(bool newRDY)
Definition: mos6510.cpp:156
virtual void reset()
Definition: mos6510.cpp:2215
void FetchHighAddrY2()
Definition: mos6510.cpp:484
void FetchLowAddrX()
Definition: mos6510.cpp:401
static const int SR_INTERRUPT
Definition: mos6510.h:65
void say_instr()
Definition: mos6510.cpp:867
void calculateInterruptTriggerCycle()
Definition: mos6510.cpp:310
void axa_instr()
Definition: mos6510.cpp:832
void FetchLowEffAddr()
Definition: mos6510.cpp:551
virtual void cpuWrite(uint_least16_t addr, uint8_t data)=0
Definition: mos6510.h:68
bool nmiFlag
Definition: mos6510.h:91
void setFlagsNZ(uint8_t value)
Definition: mos6510.cpp:98
int cycleCount
Definition: mos6510.h:82
void doSBC()
Definition: mos6510.cpp:923
void FetchHighEffAddrY2()
Definition: mos6510.cpp:588
void FetchHighEffAddrY()
Definition: mos6510.cpp:579
void throwAwayRead()
Definition: mos6510.cpp:343
void FetchLowAddrY()
Definition: mos6510.cpp:415
Definition: event.h:101
void ins_instr()
Definition: mos6510.cpp:1432
void doADC()
Definition: mos6510.cpp:889
void FetchLowAddr()
Definition: mos6510.cpp:383
void eventWithSteals()
Definition: mos6510.cpp:74
void FetchHighAddrY()
Definition: mos6510.cpp:476
Definition: mos6510.h:46
void las_instr()
Definition: mos6510.cpp:1444
bool rdy
Definition: mos6510.h:97
void PushHighPC()
Definition: mos6510.cpp:629
void FetchHighPointer()
Definition: mos6510.cpp:531
void FetchLowPointerX()
Definition: mos6510.cpp:518
struct ProcessorCycle instrTable[0x101<< 3]
Definition: mos6510.h:130
virtual uint8_t cpuRead(uint_least16_t addr)=0
void FetchLowPointer()
Definition: mos6510.cpp:500
void PopSR()
Definition: mos6510.cpp:186
void Initialise()
Definition: mos6510.cpp:2185
void lse_instr()
Definition: mos6510.cpp:1467
void PutEffAddrDataByte()
Definition: mos6510.cpp:611
void aso_instr()
Definition: mos6510.cpp:1407
EventCallback< MOS6510 > m_nosteal
Definition: mos6510.h:137
void anc_instr()
Definition: mos6510.cpp:1364
void eventWithoutSteals()
Definition: mos6510.cpp:66
void triggerNMI()
Definition: mos6510.cpp:225
bool irqAssertedOnPin
Definition: mos6510.h:88
void PushSR()
Definition: mos6510.cpp:176
void PopHighPC()
Definition: mos6510.cpp:649
void triggerIRQ()
Definition: mos6510.cpp:239
void rla_instr()
Definition: mos6510.cpp:1490
void PopLowPC()
Definition: mos6510.cpp:639
void FetchHighAddrX()
Definition: mos6510.cpp:451
void alr_instr()
Definition: mos6510.cpp:1350
void clearIRQ()
Definition: mos6510.cpp:253
int interruptCycle
Definition: mos6510.h:85
void dcm_instr()
Definition: mos6510.cpp:1419
void xas_instr()
Definition: mos6510.cpp:879
void rra_instr()
Definition: mos6510.cpp:1504
void FetchHighEffAddr()
Definition: mos6510.cpp:565
void FetchDataByte()
Definition: mos6510.cpp:357
void FetchHighAddr()
Definition: mos6510.cpp:431
void throwAwayFetch()
Definition: mos6510.cpp:335
EventContext & eventContext
Definition: mos6510.h:79
EventCallback< MOS6510 > m_steal
Definition: mos6510.h:140
void triggerRST()
Definition: mos6510.cpp:211
void axs_instr()
Definition: mos6510.cpp:845
void oal_instr()
Definition: mos6510.cpp:1480
void FetchHighAddrX2()
Definition: mos6510.cpp:459
void arr_instr()
Definition: mos6510.cpp:1375
bool rstFlag
Definition: mos6510.h:94
void lax_instr()
Definition: mos6510.cpp:1457