initial upstream import
[drnoksnes] / sa1.h
1 /*
2  * Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
3  *
4  * (c) Copyright 1996 - 2001 Gary Henderson (gary.henderson@ntlworld.com) and
5  *                           Jerremy Koot (jkoot@snes9x.com)
6  *
7  * Super FX C emulator code 
8  * (c) Copyright 1997 - 1999 Ivar (ivar@snes9x.com) and
9  *                           Gary Henderson.
10  * Super FX assembler emulator code (c) Copyright 1998 zsKnight and _Demo_.
11  *
12  * DSP1 emulator code (c) Copyright 1998 Ivar, _Demo_ and Gary Henderson.
13  * C4 asm and some C emulation code (c) Copyright 2000 zsKnight and _Demo_.
14  * C4 C code (c) Copyright 2001 Gary Henderson (gary.henderson@ntlworld.com).
15  *
16  * DOS port code contains the works of other authors. See headers in
17  * individual files.
18  *
19  * Snes9x homepage: http://www.snes9x.com
20  *
21  * Permission to use, copy, modify and distribute Snes9x in both binary and
22  * source form, for non-commercial purposes, is hereby granted without fee,
23  * providing that this license information and copyright notice appear with
24  * all copies and any derived work.
25  *
26  * This software is provided 'as-is', without any express or implied
27  * warranty. In no event shall the authors be held liable for any damages
28  * arising from the use of this software.
29  *
30  * Snes9x is freeware for PERSONAL USE only. Commercial users should
31  * seek permission of the copyright holders first. Commercial use includes
32  * charging money for Snes9x or software derived from Snes9x.
33  *
34  * The copyright holders request that bug fixes and improvements to the code
35  * should be forwarded to them so everyone can benefit from the modifications
36  * in future versions.
37  *
38  * Super NES and Super Nintendo Entertainment System are trademarks of
39  * Nintendo Co., Limited and its subsidiary companies.
40  */
41 #ifndef _sa1_h_
42 #define _sa1_h_
43
44 #include "memmap.h"
45
46 struct SSA1Registers {
47     uint8   PB;
48     uint8   DB;
49     pair    P;
50     pair    A;
51     pair    D;
52     pair    S;
53     pair    X;
54     pair    Y;
55     uint16  PC;
56 };
57
58 struct SSA1 {
59     struct  SOpcodes *S9xOpcodes;
60     uint8   _Carry;
61     uint8   _Zero;
62     uint8   _Negative;
63     uint8   _Overflow;
64     bool8   CPUExecuting;
65     uint32  ShiftedPB;
66     uint32  ShiftedDB;
67     uint32  Flags;
68     bool8   Executing;
69     bool8   NMIActive;
70     bool8   IRQActive;
71     bool8   WaitingForInterrupt;
72     bool8   Waiting;
73 //    uint8   WhichEvent;
74     uint8   *PC;
75     uint8   *PCBase;
76     uint8   *BWRAM;
77     uint8   *PCAtOpcodeStart;
78     uint8   *WaitAddress;
79     uint32  WaitCounter;
80     uint8   *WaitByteAddress1;
81     uint8   *WaitByteAddress2;
82 //    long    Cycles;
83 //    long    NextEvent;
84 //    long    V_Counter;
85     uint8   *Map [MEMMAP_NUM_BLOCKS];
86     uint8   *WriteMap [MEMMAP_NUM_BLOCKS];
87     int16   op1;
88     int16   op2;
89     int     arithmetic_op;
90     int64   sum;
91     bool8   overflow;
92     uint8   VirtualBitmapFormat;
93     bool8   in_char_dma;
94     uint8   variable_bit_pos;
95 };
96
97 extern struct SSA1Registers SA1Registers;
98 extern struct SSA1 SA1;
99
100 #ifdef USE_SA1
101
102 #define SA1CheckZero() (SA1._Zero == 0)
103 #define SA1CheckCarry() (SA1._Carry)
104 #define SA1CheckIRQ() (SA1Registers.PL & IRQ)
105 #define SA1CheckDecimal() (SA1Registers.PL & Decimal)
106 #define SA1CheckIndex() (SA1Registers.PL & IndexFlag)
107 #define SA1CheckMemory() (SA1Registers.PL & MemoryFlag)
108 #define SA1CheckOverflow() (SA1._Overflow)
109 #define SA1CheckNegative() (SA1._Negative & 0x80)
110 #define SA1CheckEmulation() (SA1Registers.P.W & Emulation)
111
112 #define SA1ClearFlags(f) (SA1Registers.P.W &= ~(f))
113 #define SA1SetFlags(f)   (SA1Registers.P.W |=  (f))
114 #define SA1CheckFlag(f)  (SA1Registers.PL & (f))
115
116
117 START_EXTERN_C
118 uint8 S9xSA1GetByte (uint32);
119 uint16 S9xSA1GetWord (uint32);
120 void S9xSA1SetByte (uint8, uint32);
121 void S9xSA1SetWord (uint16, uint32);
122 void S9xSA1SetPCBase (uint32);
123 uint8 S9xGetSA1 (uint32);
124 void S9xSetSA1 (uint8, uint32);
125
126 extern struct SOpcodes S9xSA1OpcodesM1X1 [256];
127 extern struct SOpcodes S9xSA1OpcodesM1X0 [256];
128 extern struct SOpcodes S9xSA1OpcodesM0X1 [256];
129 extern struct SOpcodes S9xSA1OpcodesM0X0 [256];
130
131 void S9xSA1MainLoop ();
132 void S9xSA1Init ();
133 void S9xFixSA1AfterSnapshotLoad ();
134 void S9xSA1ExecuteDuringSleep ();
135 END_EXTERN_C
136
137 #define SNES_IRQ_SOURCE     (1 << 7)
138 #define TIMER_IRQ_SOURCE    (1 << 6)
139 #define DMA_IRQ_SOURCE      (1 << 5)
140
141 STATIC inline void S9xSA1UnpackStatus()
142 {
143     SA1._Zero = (SA1Registers.PL & Zero) == 0;
144     SA1._Negative = (SA1Registers.PL & Negative);
145     SA1._Carry = (SA1Registers.PL & Carry);
146     SA1._Overflow = (SA1Registers.PL & Overflow) >> 6;
147 }
148
149 STATIC inline void S9xSA1PackStatus()
150 {
151     SA1Registers.PL &= ~(Zero | Negative | Carry | Overflow);
152     SA1Registers.PL |= SA1._Carry | ((SA1._Zero == 0) << 1) |
153                        (SA1._Negative & 0x80) | (SA1._Overflow << 6);
154 }
155
156 STATIC inline void S9xSA1FixCycles ()
157 {
158     if (SA1CheckEmulation ())
159         SA1.S9xOpcodes = S9xSA1OpcodesM1X1;
160     else
161     if (SA1CheckMemory ())
162     {
163         if (SA1CheckIndex ())
164             SA1.S9xOpcodes = S9xSA1OpcodesM1X1;
165         else
166             SA1.S9xOpcodes = S9xSA1OpcodesM1X0;
167     }
168     else
169     {
170         if (SA1CheckIndex ())
171             SA1.S9xOpcodes = S9xSA1OpcodesM0X1;
172         else
173             SA1.S9xOpcodes = S9xSA1OpcodesM0X0;
174     }
175 }
176
177 #endif // USE_SA1
178
179 #endif