libsidplayfp  1.0.3
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 <stdio.h>
27 #include <stdint.h>
28 
29 #include "sidplayfp/sidendian.h"
30 #include "sidplayfp/component.h"
31 #include "sidplayfp/EventScheduler.h"
32 
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
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 
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 
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