home *** CD-ROM | disk | FTP | other *** search
/ MACD 4 / MACD4.iso / Emulatory / v2600 / source.lha / source / keyboard.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-11  |  4.7 KB  |  239 lines

  1. /*****************************************************************************
  2.  
  3.    This file is part of Virtual 2600, the Atari 2600 Emulator
  4.    ==========================================================
  5.    
  6.    Copyright 1996 Alex Hornby. For contributions see the file CREDITS.
  7.  
  8.    This software is distributed under the terms of the GNU General Public
  9.    License. This is free software with ABSOLUTELY NO WARRANTY.
  10.    
  11.    See the file COPYING for details.
  12.    
  13.    $Id: keyboard.c,v 1.17 1996/08/28 14:32:34 ahornby Exp $
  14.  
  15.    Tweaked by Matthew Stroup for Amiga v2600, February 5, 1997.
  16.  
  17. ******************************************************************************/
  18.  
  19. /* The device independant keyboard and mouse interface. */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include "config.h"
  24. #include "options.h"
  25. #include "types.h"
  26. #include "address.h"
  27. #include "vmachine.h"
  28. #include "macro.h"
  29. #include "extern.h"
  30. #include "memory.h"
  31. #include "kmap.h"
  32. #include "realjoy.h"
  33. #include "display.h"
  34. #include "keybdrv.h"
  35. #include "moudrv.h"
  36.  
  37. enum control {STICK, PADDLE, KEYPAD};
  38.  
  39. enum joydirs {JLEFT, JRIGHT, JUP, JDOWN, JFIRE};
  40. int joykeys[2][5]={ 
  41.   {kmapLARROW, kmapRARROW, kmapUARROW, kmapDARROW, kmapRIGHTALT},
  42.   {kmapA, kmapS, kmapW, kmapZ, kmapLEFTALT}};
  43.  
  44. int padkeys[2][4][3]={ { {kmap1,kmap2,kmap3}, 
  45.              {kmapQ, kmapW, kmapE},
  46.              {kmapA, kmapS, kmapD},
  47.              {kmapZ, kmapX, kmapD}},
  48.                { {kmap5,kmap6,kmap7},
  49.              {kmapT, kmapY, kmapU},
  50.              {kmapG, kmapH, kmapJ},
  51.              {kmapB, kmapN, kmapM}}};
  52.  
  53. int moudrv_x, moudrv_y, moudrv_but;
  54.  
  55. static int raw;
  56.  
  57. /* Stores the keycodes */
  58. int keymap[103];
  59.  
  60. /* Returns mouse position */
  61. int
  62. mouse_position (void)
  63. {
  64.   int res;
  65.   moudrv_read();
  66.   
  67.   if(base_opts.mousey==1)
  68.     res=(((moudrv_y)*320)/200);
  69.   else
  70.     res=moudrv_x;
  71.   return res;
  72. }
  73.  
  74. /* Returns mouse button state */
  75. int
  76. mouse_button (void)
  77. {
  78.   moudrv_read();
  79.   return moudrv_but;
  80. }
  81.  
  82. /* Read from the keypad */
  83. void 
  84. read_keypad(int pad) 
  85. {
  86.   int i,row,col;
  87.   
  88.   for(i=0; i<4; i++) 
  89.     keypad[pad][i]=0xff;
  90.  
  91.   for(row=0;row<4;row++)
  92.     {
  93.       for(col=0;col<3;col++)
  94.     {
  95.       if (keybdrv_pressed(padkeys[pad][row][col])) 
  96.         keypad[pad][row]=col;
  97.     }
  98.     }
  99. }
  100.  
  101.  
  102. /* Read from the emulated joystick */
  103. void
  104. read_stick (void)
  105. {
  106.   UBYTE v[2];
  107.   int i,jres;
  108.  
  109.   v[0] = v[1] = 0x0f;
  110.  
  111.   for(i=0;i<2;i++)
  112.     {      
  113.       jres = get_realjoy (i);
  114.       if (keybdrv_pressed(joykeys[i][JUP]) || (jres & UJOYMASK))
  115.     v[i] &= 0x0E;
  116.       else if (keybdrv_pressed(joykeys[i][JDOWN]) || (jres & DJOYMASK))
  117.     v[i] &= 0x0D;
  118.       if (keybdrv_pressed(joykeys[i][JLEFT]) || (jres & LJOYMASK))
  119.     v[i] &= 0x0B;
  120.       else if (keybdrv_pressed(joykeys[i][JRIGHT]) || (jres & RJOYMASK))
  121.     v[i] &= 0x07;
  122.     }
  123.  
  124.   /* Swap if necessary */
  125.   if (base_opts.swap)
  126.     riotRead[SWCHA] = (v[1] << 4) | v[0];
  127.   else
  128.     riotRead[SWCHA] = (v[0] << 4) | v[1];
  129. }
  130.  
  131. /* Read from emulated joystick trigger */
  132. void
  133. read_trigger (void)
  134. {
  135.   int kr = 0x80, kl = 0x80, temp;
  136.   int jres;
  137.  
  138.   /* keyboard_update(); */
  139.   /* update_realjoy(); */
  140.  
  141.   /* Left Player */
  142.   jres = get_realjoy (1);
  143.   if (keybdrv_pressed(joykeys[1][JFIRE]) || (jres & B1JOYMASK))
  144.     kl = 0x00;
  145.  
  146.   /* Right Player */
  147.   jres = get_realjoy (0);
  148.   if (keybdrv_pressed(joykeys[0][JFIRE]) || (jres & B1JOYMASK))
  149.     kr = 0x00;
  150.  
  151.   /* Swap the buttons if necessary */
  152.   if (base_opts.swap)
  153.     {
  154.       temp = kl;
  155.       kl = kr;
  156.       kr = temp;
  157.     }
  158.  
  159.   /* The normal non-latched mode */
  160.   if (!(tiaWrite[VBLANK] & 0x40))
  161.     {
  162.       tiaRead[INPT4] = kr;
  163.       tiaRead[INPT5] = kl;
  164.     }
  165.   /* The latched mode */
  166.   else
  167.     {
  168.       if (kr == 0)
  169.     tiaRead[INPT4] = 0x00;
  170.       if (kl == 0)
  171.     tiaRead[INPT5] = 0x00;
  172.     }
  173. }
  174.  
  175. /* Reads the keyboard */
  176. void
  177. read_keyboard (void)
  178. {
  179.   if (raw)
  180.     {
  181.       keybdrv_update ();
  182.       moudrv_update ();
  183.     }
  184.   if (keybdrv_pressed(kmapF10))
  185.     exit (0);
  186. }
  187.  
  188. /* Read console switches */
  189. void
  190. read_console (void)
  191. {
  192.   riotRead[SWCHB] |= 0x03;
  193.   if (keybdrv_pressed(kmapF2))
  194.     riotRead[SWCHB] &= 0xFE;    /* Reset */
  195.   if (keybdrv_pressed(kmapF3))
  196.     riotRead[SWCHB] &= 0xFD;    /* Select */
  197.  
  198.   if (keybdrv_pressed(kmapF4))
  199.     {
  200.       if (riotRead[SWCHB] & 0x08)
  201.     /* BW */
  202.     riotRead[SWCHB] &= 0xF7;
  203.       else
  204.     /* Color */
  205.     riotRead[SWCHB] |= 0x08;
  206.       while (keybdrv_pressed(kmapF4))
  207.     keybdrv_update ();
  208.     }
  209.  
  210.   if (keybdrv_pressed(kmapF5))
  211.     riotRead[SWCHB] &= 0xBF;    /* P0 amateur */
  212.   else if (keybdrv_pressed(kmapF6))
  213.     riotRead[SWCHB] |= 0x40;    /* P0 pro */
  214.   if (keybdrv_pressed(kmapF7))
  215.     riotRead[SWCHB] &= 0x7f;    /* P1 amateur */
  216.   else if (keybdrv_pressed(kmapF8))
  217.     riotRead[SWCHB] |= 0x80;    /* P1 pro */
  218. }
  219.  
  220. /* Close the keyboard */
  221. void
  222. close_keyboard (void)
  223. {
  224.   moudrv_close();
  225.   keybdrv_close ();
  226.   raw=0;
  227. }
  228.  
  229. /* Initialises the keyboard */
  230. void
  231. init_keyboard (void)
  232. {
  233.   keybdrv_init ();
  234.   keybdrv_setmap();
  235.   moudrv_init();
  236.   raw=1;
  237. }
  238.  
  239.