uae.cpp
  1  #include <string.h>
  2  
  3  #include "emuapi.h"
  4  #include "tft_t_dma.h"
  5  #include "iopins.h" 
  6  
  7  extern "C" {
  8  #include "dtypes.h"
  9  
 10  #include "memory.h"
 11  #include "custom.h"
 12  #include "newcpu.h"
 13  #include "disk.h"
 14  #include "keybuf.h"
 15  #include "xwin.h"
 16  #include "gui.h"
 17  #include "events.h"
 18  #include "keyboard.h"
 19  }
 20  
 21  
 22  
 23  #define MAX_FILENAME 2
 24  
 25  int framerate = 1;
 26  int dont_want_aspect = 1;
 27  int use_debugger = 0;
 28  int use_slow_mem = 0;
 29  int use_gfxlib = 0;
 30  int use_xhair = 0;
 31  int use_lores = 1;
 32  int automount_uaedev = 1;
 33  int produce_sound = 0;
 34  int fake_joystick = 0;
 35  int screen_res = 0;
 36  int color_mode = 0;
 37  ULONG fastmem_size = 0;
 38  
 39  int quit_program = 0;
 40  int buttonstate[3];
 41  int lastmx, lastmy;
 42  int newmousecounters;
 43  char prtname[MAX_FILENAME] = "lpr ";
 44  
 45  xcolnr xcolors[4096];
 46  struct vidbuf_description gfxvidinfo;
 47  
 48  CPTR audlc[4], audpt[4];
 49  UWORD audvol[4], audper[4], audlen[4], audwlen[4];
 50  int audwper[4];
 51  UWORD auddat[4];
 52  int audsnum[4];
 53  
 54  int audst[4];
 55  
 56  
 57  
 58  #define MOUSE_STEP 2
 59  static int prev_hk = 0;
 60  static int hk = 0;
 61  static int prev_key = 0;
 62  static int k = 0;
 63  static bool isMouse = true;
 64  
 65  const int keyconv[] = 
 66  {
 67    AK_1,AK_2,AK_3,AK_4,AK_5,AK_6,AK_7,AK_8,AK_9,AK_0,
 68    AK_Q,AK_W,AK_E,AK_R,AK_T,AK_Y,AK_U,AK_I,AK_O,AK_P,
 69    AK_A,AK_S,AK_D,AK_F,AK_G,AK_H,AK_J,AK_K,AK_L,AK_ENT,
 70    AK_Z,AK_C,AK_C,AK_V,AK_B,AK_N,AK_M,AK_COMMA,AK_BS,AK_SPC,
 71    AK_F1,AK_F2,AK_F3,AK_F4,AK_F5,AK_F6,AK_F7,AK_F8,AK_F9,AK_F10,  
 72  };
 73  
 74  void uae_Input(int click) {
 75    hk = emu_ReadI2CKeyboard();
 76    k = emu_ReadKeys(); 
 77  
 78    // Toggle mouse/joystick
 79    if (k & MASK_KEY_USER1) {
 80      if (isMouse) isMouse = false;
 81      else isMouse = true;
 82    }
 83    // Toggle keymap
 84    if (k & MASK_KEY_USER2) {
 85      emu_setKeymap(0);
 86    }   
 87  
 88    if (hk != prev_hk) {
 89      prev_hk = hk;
 90      if ( (hk != 0) && (hk != prev_key) ) {
 91        prev_key = hk;
 92        int iAmigaKeyCode = keyconv[hk-1];
 93        record_key(iAmigaKeyCode << 1);
 94      }     
 95    }
 96    if ( (hk == 0) && (prev_key) ) {
 97        int iAmigaKeyCode = keyconv[prev_key-1];
 98        record_key((iAmigaKeyCode << 1) | 1);      
 99        prev_key = 0;
100    }           
101    if (isMouse)
102    {
103      if (( k & MASK_JOY1_RIGHT) || ( k & MASK_JOY2_RIGHT)) {
104        if ( lastmx < 320 ) {
105          lastmx += MOUSE_STEP;
106          newmousecounters = 1; 
107        } 
108      }
109      else if (( k & MASK_JOY1_LEFT) || ( k & MASK_JOY2_LEFT)) {
110        if ( lastmx >= MOUSE_STEP ) {
111          lastmx -= MOUSE_STEP;
112          newmousecounters = 1;       
113        }
114      }
115      else if (( k & MASK_JOY1_UP) || ( k & MASK_JOY2_UP)) {
116        if ( lastmy >= MOUSE_STEP ) {
117          lastmy -= MOUSE_STEP;
118          newmousecounters = 1;       
119        }
120      }
121      else if (( k & MASK_JOY1_DOWN) || ( k & MASK_JOY2_DOWN)) {
122        if ( lastmy < 240 ) {
123          lastmy += MOUSE_STEP;
124          newmousecounters = 1;        
125        }
126      } 
127  
128      buttonstate[0] = 0;
129      if (k & MASK_JOY2_BTN) buttonstate[0] = 1;             
130    }
131  }
132  
133  void handle_events(void)
134  {
135    uae_Input(0);
136      
137  } 
138  
139  extern "C" void read_joystick (UWORD *dir, int *button);
140  void read_joystick(UWORD *dir, int *button)
141  {
142    int left = 0, right = 0, top = 0, bot = 0;
143    *dir = 0;
144    *button = 0;
145  
146    if (!isMouse)
147    {
148      if (k & MASK_JOY2_DOWN)  bot=1;
149      if (k & MASK_JOY2_UP)    top=1;
150      if (k & MASK_JOY2_LEFT)  left=1;
151      if (k & MASK_JOY2_RIGHT) right=1;
152      if (k & MASK_JOY2_BTN) *button |=0xFF;
153      //buttonstate[0] = 0;
154      //if (k & MASK_KEY_USER2) buttonstate[0] = 1;  
155    
156      if (left) top = !top;
157      if (right) bot = !bot;
158      *dir = bot | (right << 1) | (top << 8) | (left << 9);
159    }  
160  }
161  
162  int debuggable(void)
163  {
164    return 1;
165  }
166  
167  int needmousehack(void)
168  {
169    return 1;
170  }
171  
172  //static short sound_table[256][64];
173  static int smplcnt = 0;
174  static long count = 0;
175  /* The buffer is too large... */
176  //static UWORD buffer[44100], *bufpt;
177  static UBYTE snddata[4];
178  static int frq_divisor = (22050 / 50);
179  
180  /*
181  static void init_sound_table16(void)
182  {
183      int i,j;
184      
185      for (i = 0; i < 256; i++)
186        for (j = 0; j < 64; j++)
187          sound_table[i][j] = j * (BYTE)i;
188  }
189  */
190  
191  static void channel_reload (int c)
192  {
193      audst[c] = 1;
194      audpt[c] = audlc[c];
195      audwper[c] = 0;
196      audwlen[c] = audlen[c];
197      audsnum[c] = 1;    
198  }
199  
200  extern "C" void do_sound (void);
201  void do_sound (void)
202  {
203    smplcnt -= 227;
204    while (smplcnt < 0) {
205      int i;
206      smplcnt += 80;
207    
208      for(i = 0; i < 4; i++) 
209      {
210        if (dmaen (1<<i)) {
211          if (audst[i] == 0) {    
212            /* DMA was turned on for this channel */
213            channel_reload (i);
214            continue;
215          }
216    
217          if (audwper[i] <= 0) {
218            audwper[i] += audper[i];
219            if (audst[i] == 1) {
220              /*  Starting a sample, cause interrupt */
221              put_word (0xDFF09C, 0x8000 | (0x80 << i));
222              audst[i] = 2;
223            }
224            audsnum[i] ^= 1;
225            if (audsnum[i] == 0) {
226              auddat[i] = get_word (audpt[i]);
227              audpt[i] += 2;
228              audwlen[i]--;
229              if (audwlen[i] == 0) {
230                channel_reload (i);
231              }
232            }
233          }
234          if (adkcon & (0x11 << i)) {
235            snddata[i] = 0;
236            audsnum[i] ^= 2;
237            audsnum[i] |= 1;
238            if (i < 3) {
239              if (adkcon & (0x11 << i)) {
240                if (audsnum[i] & 2)
241                  audvol[i+1] = auddat[i];
242                else
243                  audper[i+1] = auddat[i];
244              } 
245              else 
246                if (adkcon & (1 << i)) 
247                {
248                  audvol[i+1] = auddat[i];
249                } 
250                else 
251                {
252                  audper[i+1] = auddat[i];
253                }
254            }
255          } 
256          else 
257          {
258            snddata[i] = audsnum[i] & 1 ? auddat[i] : auddat[i] >> 8;
259          }
260          audwper[i] -= 80;   
261        } 
262        else 
263          audst[i] = snddata[i] = 0;
264      }
265      
266      if ((count++ % frq_divisor) == 0) {
267        /*
268        long size;
269        *bufpt++ = (sound_table[snddata[0]][audvol[0]] 
270        + sound_table[snddata[1]][audvol[1]]
271        + sound_table[snddata[2]][audvol[2]] 
272        + sound_table[snddata[3]][audvol[3]];
273        size = (char *)bufpt - (char *)buffer;
274        if (size >= sndbufsize) {
275          if (AFGetTime(ac) > aftime)
276            aftime = AFGetTime(ac);
277          AFPlaySamples(ac, aftime, size, (unsigned char*) buffer);
278          aftime += size / 2;
279          bufpt = buffer;
280        }
281        */
282      }
283    } 
284  }
285  
286  void gui_led(int led, int on)
287  {
288  }
289  
290  void LED(int on)
291  {
292  }
293  
294  void flush_line(int y)
295  {
296  }
297  
298  void flush_block(int ystart,int ystop)
299  {
300  }
301  
302  void flush_screen(int ystart,int ystop)
303  {
304    emu_DrawVsync();    
305  }
306  
307  #ifdef HAS_PSRAM
308  #include "psram_t.h"
309  
310  PSRAM_T psram = PSRAM_T(PSRAM_CS, PSRAM_MOSI, PSRAM_SCLK, PSRAM_MISO);
311  
312  extern "C" unsigned char read_rom(int address) {
313    return (psram.psread(address)); 
314  }
315  
316  extern "C" void  write_rom(int address, unsigned char val)  {
317    psram.pswrite(address,val); 
318  
319  }
320  #endif
321  
322  char df0[MAX_FILENAME]="",df1[MAX_FILENAME]="", df2[MAX_FILENAME]="", df3[MAX_FILENAME]="";
323  
324  void uae_Init(void)
325  {
326    emu_printf("Init");
327    gfxvidinfo.pixbytes   = 2;
328    
329    buttonstate[0] = buttonstate[1] = buttonstate[2] = 0;
330    lastmx = lastmy = 0;
331    newmousecounters = 0;
332    int i;
333    // Palette
334    for (int i = 0; i < 4096; i++) {
335      int r = i >> 8;
336      int g = (i >> 4) & 0xF;
337      int b = i & 0xF;
338      xcolors[i] = RGBVAL16(r<<4,g<<4,b<<4);
339    } 
340    keybuf_init();
341    memory_init();
342    custom_init();
343    DISK_init();
344    MC68000_reset();
345    MC68000_run();
346    produce_sound = 1;
347  #ifdef HAS_SND  
348    emu_sndInit();
349  //#ifdef SOUND_PRESENT
350    //init_sound_table16();
351  //#else
352  //  produce_sound = 0;
353  //#endif   
354  #endif  
355  }
356  
357  
358  
359  
360  
361  void uae_Start(char * floppy)
362  {
363    emu_printf("Start");
364  
365  #ifdef HAS_PSRAM
366    psram.begin();
367    int offset = 0;
368    int pos = 0;
369    int n;
370    int i;
371    char buf[512];
372    int size = 0;  
373    if (emu_FileOpen(floppy)) {
374      while ( (n = emu_FileRead(buf,512) ) ) {
375        size += n;
376        for (int i=0; i<n; i++) {
377          write_rom(pos++,buf[i]);
378        }
379        //emu_printi(size);            
380        //emu_printi(n);          
381      }
382      emu_FileClose();
383    }
384  emu_printi(size);  
385  #else
386    strcpy(df0,floppy);
387  #endif  
388  
389    emu_printf("Start done");
390  }
391  
392  void uae_Step(void) {
393    /*
394    JoyState = 0;
395    if (k & MASK_JOY2_DOWN)  JoyState|=0x02;
396    if (k & MASK_JOY2_UP)    JoyState|=0x01;
397    if (k & MASK_JOY2_LEFT)  JoyState|=0x04;
398    if (k & MASK_JOY2_RIGHT) JoyState|=0x08;
399    if (k & MASK_JOY2_BTN) JoyState|=0x10; 
400    if (k & MASK_KEY_USER2) JoyState|=0x20;  
401  */
402  /*
403    if (hk != 0) {
404      emu_printh(hk);
405      KeyMap[Keys[hk-1].Pos] &=~ Keys[hk-1].Mask;   
406    }
407    else  {
408      memset(KeyMap,0xFF,16);
409    }
410  */
411  
412    //emu_DrawVsync();    
413  }
414  
415  
416  
417  
418  
419  
420  void SND_Process(void *stream, int len) {
421    //psg_update(stream, 0, len); 
422    //mixaudio(stream, len); 
423  } 
424  
425  
426  
427  
428