1 #ifndef EMULATOR_H
2 #define EMULATOR_H
3 //this is the only header a frontend needs to include from the emulator
4 
5 #ifdef __cplusplus
6 extern "C" {
7 #endif
8 
9 #include <stdint.h>
10 #include <stdbool.h>
11 #include <stdarg.h>
12 #include <stdio.h>
13 
14 #include "audio/blip_buf.h"
15 #include "m5XXBus.h"//for size macros
16 
17 //DEFINE INFO!!!
18 //define EMU_SUPPORT_PALM_OS5 to compile in Tungsten T3 support(not reccomended for low power devices)
19 //define EMU_MULTITHREADED to speed up long loops
20 //define EMU_MANAGE_HOST_CPU_PIPELINE to optimize the CPU pipeline for the most common cases
21 //define EMU_NO_SAFETY to remove all safety checks
22 //define EMU_BIG_ENDIAN on big endian systems
23 //define EMU_HAVE_FILE_LAUNCHER to enable launching files from the host system
24 //to enable degguging define EMU_DEBUG, all options below do nothing unless EMU_DEBUG is defined
25 //to enable memory access logging define EMU_SANDBOX_LOG_MEMORY_ACCESSES
26 //to enable opcode level debugging define EMU_SANDBOX_OPCODE_LEVEL_DEBUG
27 //to enable flow control logging define EMU_SANDBOX_LOG_JUMPS, EMU_SANDBOX_OPCODE_LEVEL_DEBUG must also be defined for this to work
28 //to log all API calls define EMU_SANDBOX_LOG_APIS, EMU_SANDBOX_OPCODE_LEVEL_DEBUG must also be defined for this to work
29 
30 //debug
31 #if defined(EMU_DEBUG)
32 #if defined(EMU_CUSTOM_DEBUG_LOG_HANDLER)
33 extern uint32_t frontendDebugStringSize;
34 extern char*    frontendDebugString;
35 void frontendHandleDebugPrint();
36 #define debugLog(...) (snprintf(frontendDebugString, frontendDebugStringSize, __VA_ARGS__), frontendHandleDebugPrint())
37 #else
38 #define debugLog(...) printf(__VA_ARGS__)
39 #endif
40 #else
41 //msvc2003 doesnt support variadic macros, so just use an empty variadic function instead, EMU_DEBUG is not supported at all on msvc2003
42 static void debugLog(char* str, ...){};
43 #endif
44 
45 //config options
46 #define EMU_FPS 60
47 #define DBVZ_SYSCLK_PRECISION 2000000//the amount of cycles to run before adding SYSCLKs, higher = faster, higher values may skip timer events and lower audio accuracy
48 #define AUDIO_SAMPLE_RATE 48000
49 #define AUDIO_SPEAKER_RANGE 0x6000//prevent hitting the top or bottom of the speaker when switching direction rapidly
50 #define SD_CARD_NCR_BYTES 1//how many 0xFF bytes come before the R1 response
51 #define SAVE_STATE_VERSION 0x00000001
52 
53 //shared constants
54 #define AUDIO_SAMPLES_PER_FRAME (AUDIO_SAMPLE_RATE / EMU_FPS)
55 #define SD_CARD_BLOCK_SIZE 512//all newer SDSC cards have this fixed at 512
56 #define SD_CARD_BLOCK_DATA_PACKET_SIZE (1 + SD_CARD_BLOCK_SIZE + 2)
57 #define SD_CARD_RESPONSE_FIFO_SIZE (SD_CARD_BLOCK_DATA_PACKET_SIZE * 3)
58 
59 //system constants
60 #define DBVZ_CPU_PERCENT_WAITING 0.30//account for wait states when reading memory, tested with SysInfo.prc
61 #define DBVZ_AUDIO_MAX_CLOCK_RATE 235929600//smallest amount of time a second can be split into:(2.0 * (14.0 * (255 + 1.0) + 15 + 1.0)) * 32768 == 235929600, used to convert the variable timing of SYSCLK and CLK32 to a fixed location in the current frame 0<->AUDIO_END_OF_FRAME
62 #define DBVZ_AUDIO_END_OF_FRAME (DBVZ_AUDIO_MAX_CLOCK_RATE / EMU_FPS)
63 #define M5XX_CRYSTAL_FREQUENCY 32768
64 #define SAVE_STATE_FOR_M500 0x40000000
65 #if defined(EMU_SUPPORT_PALM_OS5)
66 #define TUNGSTEN_T3_CPU_PERCENT_WAITING 0.30//TODO: dont know ARM CPU speeds yet
67 #define TUNGSTEN_T3_CPU_CRYSTAL_FREQUENCY 3686400
68 #define TUNGSTEN_T3_CPU_PLL_FREQUENCY 368640000//TODO: dont know ARM CPU speeds yet
69 #define TUNGSTEN_T3_RTC_CRYSTAL_FREQUENCY 32768
70 #define SAVE_STATE_FOR_TUNGSTEN_T3 0x80000000
71 #endif
72 
73 //emu errors
74 enum{
75    EMU_ERROR_NONE = 0,
76    EMU_ERROR_UNKNOWN,
77    EMU_ERROR_NOT_IMPLEMENTED,
78    EMU_ERROR_CALLBACKS_NOT_SET,
79    EMU_ERROR_OUT_OF_MEMORY,
80    EMU_ERROR_INVALID_PARAMETER,
81    EMU_ERROR_RESOURCE_LOCKED
82 };
83 
84 //port types
85 enum{
86    EMU_PORT_NONE = 0,
87    EMU_PORT_USB_CRADLE,
88    EMU_PORT_SERIAL_CRADLE,
89    EMU_PORT_USB_PERIPHERAL,
90    EMU_PORT_SERIAL_PERIPHERAL,
91    EMU_PORT_END
92 };
93 
94 //serial codes, behaviors of a serial connection other then the raw bytes, the data bytes are stored as uint16_t's with the top 8 bits being the special codes
95 #define EMU_SERIAL_USE_EXTERNAL_CLOCK_SOURCE 0//check baud rate against this when a serial port is reconfigured, if its equal use the clock from the other device
96 #define EMU_SERIAL_PARITY_ERROR 0x100
97 #define EMU_SERIAL_FRAME_ERROR 0x200
98 #define EMU_SERIAL_BREAK (EMU_SERIAL_PARITY_ERROR | 0x00)
99 
100 //emulated devices
101 enum{
102    EMU_DEVICE_PALM_M500 = 0,
103    EMU_DEVICE_PALM_M515
104 #if defined(EMU_SUPPORT_PALM_OS5)
105    ,EMU_DEVICE_TUNGSTEN_T3
106 #endif
107 };
108 
109 //types
110 typedef struct{
111    bool     enable;
112    bool     enableParity;
113    bool     oddParity;
114    uint8_t  stopBits;
115    bool     use8BitMode;
116    uint32_t baudRate;
117 }serial_port_properties_t;
118 
119 typedef struct{
120    bool  buttonUp;
121    bool  buttonDown;
122 #if defined(EMU_SUPPORT_PALM_OS5)
123    bool  buttonLeft;
124    bool  buttonRight;
125    bool  buttonCenter;
126 #endif
127 
128    bool  buttonCalendar;//hw button 1
129    bool  buttonAddress;//hw button 2
130    bool  buttonTodo;//hw button 3
131    bool  buttonNotes;//hw button 4
132 #if defined(EMU_SUPPORT_PALM_OS5)
133    bool  buttonVoiceMemo;
134 #endif
135 
136    bool  buttonPower;
137 
138    float touchscreenX;//0.0 = left, 1.0 = right
139    float touchscreenY;//0.0 = top, 1.0 = bottom
140    bool  touchscreenTouched;
141 }input_t;
142 
143 typedef struct{
144    uint8_t  csd[16];
145    uint8_t  cid[16];
146    uint8_t  scr[8];
147    uint32_t ocr;
148    bool     writeProtectSwitch;
149 }sd_card_info_t;
150 
151 typedef struct{
152    uint64_t       command;
153    uint8_t        commandBitsRemaining;
154    uint8_t        runningCommand;
155    uint32_t       runningCommandVars[3];
156    uint8_t        runningCommandPacket[SD_CARD_BLOCK_DATA_PACKET_SIZE];
157    uint8_t        responseFifo[SD_CARD_RESPONSE_FIFO_SIZE];
158    uint16_t       responseReadPosition;
159    int8_t         responseReadPositionBit;
160    uint16_t       responseWritePosition;
161    bool           commandIsAcmd;
162    bool           allowInvalidCrc;
163    bool           chipSelect;
164    bool           receivingCommand;
165    bool           inIdleState;
166    sd_card_info_t sdInfo;
167    uint8_t*       flashChipData;
168    uint32_t       flashChipSize;
169 }sd_card_t;
170 
171 typedef struct{
172    bool    greenLed;
173 #if defined(EMU_SUPPORT_PALM_OS5)
174    bool    redLed;
175 #endif
176    bool    lcdOn;
177    uint8_t backlightLevel;
178    bool    vibratorOn;
179    bool    batteryCharging;
180    uint8_t batteryLevel;
181    uint8_t dataPort;
182 }misc_hw_t;
183 
184 //emulator data, some are GUI interface variables, some should be left alone
185 #if defined(EMU_SUPPORT_PALM_OS5)
186 extern bool      palmEmulatingTungstenT3;//read allowed, but not advised
187 #endif
188 extern bool      palmEmulatingM500;//dont touch
189 extern uint8_t*  palmRom;//dont touch
190 extern uint8_t*  palmRam;//access allowed to read save RAM without allocating a giant buffer, but endianness must be taken into account
191 extern input_t   palmInput;//write allowed
192 extern sd_card_t palmSdCard;//access allowed to read flash chip data without allocating a giant buffer
193 extern misc_hw_t palmMisc;//read/write allowed
194 extern uint16_t* palmFramebuffer;//read allowed
195 extern uint16_t  palmFramebufferWidth;//read allowed
196 extern uint16_t  palmFramebufferHeight;//read allowed
197 extern int16_t*  palmAudio;//read allowed, 2 channel signed 16 bit audio
198 extern blip_t*   palmAudioResampler;//dont touch
199 extern double    palmCycleCounter;//dont touch
200 extern double    palmClockMultiplier;//dont touch
201 extern bool      palmSyncRtc;//dont touch
202 extern bool      palmAllowInvalidBehavior;//dont touch
203 extern void      (*palmIrSetPortProperties)(serial_port_properties_t* properties);//configure port I/O behavior, used for proxyed native I/R connections
204 extern uint32_t  (*palmIrDataSize)(void);//returns the current number of bytes in the hosts IR receive FIFO
205 extern uint16_t  (*palmIrDataReceive)(void);//called by the emulator to read the hosts IR receive FIFO
206 extern void      (*palmIrDataSend)(uint16_t data);//called by the emulator to send IR data
207 extern void      (*palmIrDataFlush)(void);//called by the emulator to delete all data in the hosts IR receive FIFO
208 extern void      (*palmSerialSetPortProperties)(serial_port_properties_t* properties);//configure port I/O behavior, used for proxyed native serial connections
209 extern uint32_t  (*palmSerialDataSize)(void);//returns the current number of bytes in the hosts serial receive FIFO
210 extern uint16_t  (*palmSerialDataReceive)(void);//called by the emulator to read the hosts serial receive FIFO
211 extern void      (*palmSerialDataSend)(uint16_t data);//called by the emulator to send serial data
212 extern void      (*palmSerialDataFlush)(void);//called by the emulator to delete all data in the hosts serial receive FIFO
213 extern void      (*palmGetRtcFromHost)(uint8_t* writeBack);//[0] = hours, [1] = minutes, [2] = seconds
214 
215 //functions
216 uint32_t emulatorInit(uint8_t emulatedDevice, uint8_t* palmRomData, uint32_t palmRomSize, uint8_t* palmBootloaderData, uint32_t palmBootloaderSize, bool syncRtc, bool allowInvalidBehavior);
217 void emulatorDeinit(void);
218 void emulatorHardReset(void);
219 void emulatorSoftReset(void);
220 void emulatorSetRtc(uint16_t days, uint8_t hours, uint8_t minutes, uint8_t seconds);
221 void emulatorSetCpuSpeed(double speed);
222 uint32_t emulatorGetStateSize(void);
223 bool emulatorSaveState(uint8_t* data, uint32_t size);//true = success
224 bool emulatorLoadState(uint8_t* data, uint32_t size);//true = success
225 uint32_t emulatorGetRamSize(void);
226 bool emulatorSaveRam(uint8_t* data, uint32_t size);//true = success
227 bool emulatorLoadRam(uint8_t* data, uint32_t size);//true = success
228 uint32_t emulatorInsertSdCard(uint8_t* data, uint32_t size, sd_card_info_t* sdInfo);//use (NULL, desired size) to create a new empty SD card, pass NULL for sdInfo to use defaults
229 uint32_t emulatorGetSdCardSize(void);
230 uint32_t emulatorGetSdCardData(uint8_t* data, uint32_t size);
231 void emulatorEjectSdCard(void);
232 void emulatorRunFrame(void);
233 void emulatorSkipFrame(void);
234 
235 #ifdef __cplusplus
236 }
237 #endif
238 
239 #endif
240