cart.h
  1  //
  2  // Copyright (c) 2004 K. Wilkins
  3  //
  4  // This software is provided 'as-is', without any express or implied warranty.
  5  // In no event will the authors be held liable for any damages arising from
  6  // the use of this software.
  7  //
  8  // Permission is granted to anyone to use this software for any purpose,
  9  // including commercial applications, and to alter it and redistribute it
 10  // freely, subject to the following restrictions:
 11  //
 12  // 1. The origin of this software must not be misrepresented; you must not
 13  //    claim that you wrote the original software. If you use this software
 14  //    in a product, an acknowledgment in the product documentation would be
 15  //    appreciated but is not required.
 16  //
 17  // 2. Altered source versions must be plainly marked as such, and must not
 18  //    be misrepresented as being the original software.
 19  //
 20  // 3. This notice may not be removed or altered from any source distribution.
 21  //
 22  
 23  //////////////////////////////////////////////////////////////////////////////
 24  //                       Handy - An Atari Lynx Emulator                     //
 25  //                          Copyright (c) 1996,1997                         //
 26  //                                 K. Wilkins                               //
 27  //////////////////////////////////////////////////////////////////////////////
 28  // Lynx cartridge class header file                                         //
 29  //////////////////////////////////////////////////////////////////////////////
 30  //                                                                          //
 31  // This header file provides the interface definition and code for some of  //
 32  // the simpler cartridge API.                                               //
 33  //                                                                          //
 34  //    K. Wilkins                                                            //
 35  // August 1997                                                              //
 36  //                                                                          //
 37  //////////////////////////////////////////////////////////////////////////////
 38  // Revision History:                                                        //
 39  // -----------------                                                        //
 40  //                                                                          //
 41  // 01Aug1997 KW Document header added & class documented.                   //
 42  //                                                                          //
 43  //////////////////////////////////////////////////////////////////////////////
 44  
 45  #ifndef CART_H
 46  #define CART_H
 47  
 48  #define EPYX_HEADER_OLD 512
 49  #define EPYX_HEADER_NEW 410
 50  
 51  #ifndef __max
 52  #define __max(a,b) \
 53     ({ __typeof__ (a) _a = (a); \
 54      __typeof__ (b) _b = (b); \
 55      _a > _b ? _a : _b; })
 56  #endif
 57  
 58  #ifndef __min
 59  #define __min(a,b) \
 60     ({ __typeof__ (a) _a = (a); \
 61         __typeof__ (b) _b = (b); \
 62       _a > _b ? _b : _a; })
 63  #endif
 64  
 65  #ifdef TRACE_CART
 66  
 67  #define TRACE_CART0(msg)					_RPT1(_CRT_WARN,"CCart::"msg" (Time=%012d)\n",gSystemCycleCount)
 68  #define TRACE_CART1(msg,arg1)				_RPT2(_CRT_WARN,"CCart::"msg" (Time=%012d)\n",arg1,gSystemCycleCount)
 69  #define TRACE_CART2(msg,arg1,arg2)			_RPT3(_CRT_WARN,"CCart::"msg" (Time=%012d)\n",arg1,arg2,gSystemCycleCount)
 70  #define TRACE_CART3(msg,arg1,arg2,arg3)		_RPT4(_CRT_WARN,"CCart::"msg" (Time=%012d)\n",arg1,arg2,arg3,gSystemCycleCount)
 71  
 72  #else
 73  
 74  #define TRACE_CART0(msg)
 75  #define TRACE_CART1(msg,arg1)
 76  #define TRACE_CART2(msg,arg1,arg2)
 77  #define TRACE_CART3(msg,arg1,arg2,arg3)
 78  
 79  #endif
 80  
 81  #define DEFAULT_CART_CONTENTS	0xFF
 82  
 83  enum CTYPE {UNUSED,C64K,C128K,C256K,C512K,C1024K};
 84  
 85  #define CART_NO_ROTATE		0
 86  #define CART_ROTATE_LEFT	1
 87  #define CART_ROTATE_RIGHT	2
 88  
 89  typedef struct
 90  {
 91     UBYTE   magic[4];
 92     UWORD   page_size_bank0;
 93     UWORD   page_size_bank1;
 94     UWORD   version;
 95     UBYTE   cartname[32];
 96     UBYTE   manufname[16];
 97     UBYTE   rotation;
 98     UBYTE   aud_bits;
 99     UBYTE   eeprom;
100     UBYTE   spare[3];
101  }LYNX_HEADER;
102  
103  
104  class CCart : public CLynxBase
105  {
106  
107     // Function members
108  
109     public:
110        CCart(UBYTE *gamedata,ULONG gamesize);
111        ~CCart();
112  
113     public:
114  
115        // Access for sensible members of the clan
116  
117        void	Reset(void);
118        bool	ContextSave(LSS_FILE *fp);
119        bool	ContextLoad(LSS_FILE *fp);
120        bool	ContextLoadLegacy(LSS_FILE *fp);
121  
122        void	Poke(ULONG addr,UBYTE data);
123        UBYTE	Peek(ULONG addr);
124        ULONG	ReadCycle(void) {return 15;};
125        ULONG	WriteCycle(void) {return 15;};
126        void	BankSelect(EMMODE newbank) {mBank=newbank;}
127        ULONG	ObjectSize(void) {return (mBank==bank0)?mMaskBank0+1:mMaskBank1+1;};
128  
129        const char*	CartGetName(void) { return (const char*)mFileHeader.cartname;};
130        const char*	CartGetManufacturer(void) { return (const char*)mFileHeader.manufname; };
131        ULONG	CartGetRotate(void) {return (mFileHeader.rotation > 2) ? CART_NO_ROTATE : mFileHeader.rotation;};
132        bool	CartGetAudin(void) { return mFileHeader.aud_bits&0x01;};
133        UBYTE	CartGetEEPROMType(void) { return mFileHeader.eeprom;};
134        ULONG	CRC32(void) { return mCRC32; };
135  
136        // Access for the lynx itself, it has no idea of address etc as this is done by the
137        // cartridge emulation hardware
138        void	CartAddressStrobe(bool strobe);
139        void	CartAddressData(bool data);
140        void	Poke0(UBYTE data);
141        void	Poke1(UBYTE data);
142        void	Poke0A(UBYTE data);
143        void	Poke1A(UBYTE data);
144        UBYTE	Peek0(void);
145        UBYTE	Peek1(void);
146        UBYTE	Peek0A(void);
147        UBYTE	Peek1A(void);
148  
149        void SetShifterValue(UBYTE a){mShifter=a; mCounter=0;}; // for fake bios
150        inline ULONG GetCounterValue(void) {return mCounter;}; // for eeprom
151  
152        // Data members
153  
154     public:
155        ULONG	mWriteEnableBank1;
156  
157     private:
158        EMMODE mBank;
159        ULONG  mCartRAM;
160  
161        ULONG  mMaskBank0;
162        ULONG  mMaskBank1;
163        UBYTE	*mCartBank0;
164        UBYTE	*mCartBank1;
165        UBYTE	*mCartBank0A;
166        UBYTE	*mCartBank1A;
167  
168        LYNX_HEADER mFileHeader;
169        ULONG	      mCRC32;
170  
171        ULONG	mCounter;
172        ULONG	mShifter;
173        ULONG	mAddrData;
174        ULONG	mStrobe;
175  
176        ULONG	mShiftCount0;
177        ULONG	mCountMask0;
178        ULONG	mShiftCount1;
179        ULONG	mCountMask1;
180  
181        ULONG	mDummy;
182  };
183  
184  #endif