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

  1. /*****************************************************************************
  2.  
  3.    This file is part of x2600, 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: vmachine.c,v 2.19 1996/11/24 16:55:40 ahornby Exp $
  14.  
  15.    Tweaked by Matthew Stroup for Amiga v2600, February 5, 1997.
  16.  
  17. ******************************************************************************/
  18.  
  19. /* 
  20.    The virtual machine. Contains the RIOT timer code, and hardware 
  21.    initialisation.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include "types.h"
  27. #include "address.h"
  28. #include "options.h"
  29. #include "display.h"
  30. #include "raster.h"
  31. //#include "limiter.h"
  32. #include "cpu.h"
  33. #include "misc.h"
  34. #include "collision.h"
  35. #include "sound.h"
  36. #include "keyboard.h"
  37. #include "realjoy.h"
  38.  
  39. /* The Rom define might need altering for paged carts */
  40. /* Enough for 16k carts */
  41. int rom_size;
  42.  
  43. /* Used as a simple file buffer to store the data */
  44. UBYTE theCart[16384];
  45.  
  46. /* Pointer to start of ROM data */
  47. UBYTE *theRom;
  48.  
  49. /* Scratch area for those banking types that require memcpy() */
  50. UBYTE cartScratch[4096];
  51.  
  52. /* Area for those carts containing RAM */
  53. UBYTE cartRam[1024];
  54.  
  55. UBYTE theRam[128];
  56. UBYTE tiaRead[0x0e];
  57. UBYTE tiaWrite[0x2d];
  58. UBYTE keypad[2][4];
  59.  
  60. /* These don't strictly need so much space */
  61. UBYTE riotRead[0x298];
  62. UBYTE riotWrite[0x298];
  63.  
  64. /* 
  65.    Hardware addresses not programmer accessible 
  66.  */
  67.  
  68. /* Set if whole emulator is reset */
  69. int reset_flag = 0;
  70.  
  71. /* The timer resolution, can be 1,8,64,1024 */
  72. int timer_res = 32;
  73. int timer_count = 0;
  74. int timer_clks = 0;
  75. extern CLOCK clk;
  76. extern int beamadj;
  77.  
  78. /* Electron beam position */
  79. int ebeamx, ebeamy, sbeamx;
  80.  
  81. /* The state of the electron beam */
  82. #define VSYNCSTATE 1
  83. #define VBLANKSTATE 2
  84. #define HSYNCSTATE 4
  85. #define DRAWSTATE 8
  86. #define OVERSTATE 16
  87. int vbeam_state;        /* 1 2 8 or 16 */
  88. int hbeam_state;        /* 4 8 or 16 */
  89.  
  90. /* The tv size, varies with PAL/NTSC */
  91. int tv_width, tv_height, tv_vsync, tv_vblank, tv_overscan, tv_frame, tv_hertz,
  92.   tv_hsync;
  93.  
  94. /* The PlayField structure */
  95. struct PlayField
  96.   {
  97.     UBYTE pf0, pf1, pf2, ref;
  98.   }
  99. pf[2];
  100.  
  101. struct Paddle
  102.   {
  103.     int pos;
  104.     int val;
  105.   }
  106. paddle[4];
  107.  
  108. /* The player variables */
  109. struct Player
  110.   {
  111.     int x;
  112.     UBYTE grp;
  113.     UBYTE hmm;
  114.     UBYTE vdel;
  115.     UBYTE vdel_flag;
  116.     UBYTE nusize;
  117.     UBYTE reflect;
  118.     UBYTE mask;
  119.   }
  120. pl[2];
  121.  
  122. /* The element used in display lists */
  123. struct RasterChange
  124.   {
  125.     int x;            /* Position at which change happened */
  126.     int type;            /* Type of change */
  127.     int val;            /* Value of change */
  128.   };
  129.  
  130. #define MAXLIST 80
  131. /* The various display lists */
  132. struct RasterChange pl_change[2][MAXLIST], pf_change[1][MAXLIST], unified[MAXLIST];
  133.  
  134. /* The display list counters */
  135. int pl_change_count[2], pf_change_count[1], unified_count;
  136.  
  137. /* The missile an ball positions */
  138. struct Missile
  139.   {
  140.     int x;
  141.     UBYTE hmm;
  142.     UBYTE enabled;
  143.     UBYTE locked;
  144.     UBYTE width;
  145.     UBYTE vdel;
  146.     UBYTE vdel_flag;
  147.     UBYTE mask;
  148.   }
  149. ml[3];
  150.  
  151.  
  152.  
  153. /***************************************************************************
  154.                            Let the functions begin!
  155. ****************************************************************************/
  156.  
  157. /* Device independent screen initialisations */
  158. void
  159. init_screen (void)
  160. {
  161.   /* Set the electron beam to the top left */
  162.   ebeamx = -tv_hsync;
  163.   ebeamy = 0;
  164.   vbeam_state = VSYNCSTATE;
  165.   hbeam_state = OVERSTATE;
  166.  
  167.   tv_vsync = 3;
  168.   tv_hsync = 68;
  169.   switch (base_opts.tvtype) 
  170.     {
  171.     case NTSC:      
  172.       tv_width = 160;
  173.       tv_height = 192;
  174.       tv_vblank = 40;
  175.       tv_overscan = 30;
  176.       tv_frame = 262;
  177.       tv_hertz = 60;
  178.     break;
  179.     case PAL:
  180.     case SECAM:
  181.       tv_width = 160;
  182.       tv_height = 228;
  183.       tv_vblank = 48;
  184.       tv_overscan = 36;
  185.       tv_frame = 312;
  186.       tv_hertz = 50;
  187.       break;
  188.     }
  189.  
  190. //  limiter_init ();
  191. //  limiter_setFrameRate (tv_hertz);
  192. }
  193.  
  194. /* Initialise the RIOT (also known as PIA) */
  195. void
  196. init_riot (void)
  197. {
  198.   int i;
  199.   /* Wipe the RAM */
  200.   for (i = 0; i < 0x80; i++)
  201.     theRam[i] = 0;
  202.   /* Set the timer to zero */
  203.   riotRead[INTIM] = 0;
  204.   /* Set the joysticks and switches to input */
  205.   riotWrite[SWACNT] = 0;
  206.   riotWrite[SWBCNT] = 0;
  207.  
  208.   /* Centre the joysticks */
  209.   riotRead[SWCHA] = 0xff;
  210.   riotRead[SWCHB] = 0x0b;
  211.  
  212.   /* Set the counter resolution */
  213.   timer_res = 32;
  214.   timer_count = 0;
  215.   timer_clks = 0;
  216. }
  217.  
  218. /* Initialise the television interface adaptor (TIA) */
  219. void
  220. init_tia (void)
  221. {
  222.   int i;
  223.   tiaWrite[CTRLPF] = 0x00;
  224.   for (i = 0; i < 2; i++)
  225.     {
  226.       pl[i].hmm = 0x0;
  227.       pl[i].x = 0x0;
  228.       pl[i].nusize = 0;
  229.       pl[i].grp = 0;
  230.       pl[i].vdel = 0;
  231.       pl[i].vdel_flag = 0;
  232.       pl_change_count[i] = 0;
  233.     }
  234.  
  235.   pl[0].mask = PL0_MASK;
  236.   pl[1].mask = PL1_MASK;
  237.   ml[0].mask = ML0_MASK;
  238.   ml[1].mask = ML1_MASK;
  239.   reset_collisions ();
  240.  
  241.   pf_change_count[0] = 0;
  242.   unified_count = 0;
  243.   for (i = 0; i < 3; i++)
  244.     {
  245.       ml[i].enabled = 0;
  246.     }
  247.   
  248.   tiaWrite[VBLANK] = 0;
  249.   tiaRead[INPT4] = 0x80;
  250.   tiaRead[INPT5] = 0x80;
  251.  
  252.   /* Set up the colour table */
  253.   colour_table[P0M0_COLOUR]=0xf4;
  254.   colour_table[P1M1_COLOUR]=0xf4;
  255.   colour_table[PFBL_COLOUR] = 0xf6;
  256.   colour_table[BK_COLOUR] = 0xf6;
  257. }
  258.  
  259. void
  260. init_memory(void)
  261. {
  262.   int i;
  263.   for(i=0;i<1024; i++)
  264.     cartRam[i]=0;
  265. }
  266.  
  267. void
  268. init_banking (void)
  269. {
  270.   /* Set to the first bank */
  271.   if (Verbose) printf ("rom_size is %d\n", rom_size);
  272.   if (rom_size == 2048)
  273.     theRom = &theCart[rom_size - 2048];
  274.   else
  275.     theRom = &theCart[rom_size - 4096];
  276.   
  277.   switch(base_opts.bank)
  278.     {
  279.     case 3:
  280.       /* Parker Brothers 8k E0 */
  281.       memcpy(&cartScratch[0xc00],&theCart[0x1c00],1024);
  282.       memcpy(&cartScratch[0],&theCart[0],3072);
  283.       theRom=cartScratch;
  284.       break;
  285.     default:
  286.       break;
  287.     }
  288. }
  289.  
  290. /* Main hardware startup */
  291. void
  292. init_hardware (void)
  293. {
  294.   init_screen ();
  295.   init_riot ();
  296.   init_tia ();
  297.   init_raster ();
  298.   init_memory();
  299.   init_banking();
  300.   init_cpu (0xfffc);
  301. }
  302.  
  303. /* Do a raster change */
  304. __inline void do_raster_change (int i, int type, int val, struct RasterChange *rc)
  305. {
  306.   if (beamadj == 0)
  307.     {
  308.       printf ("BEAMADJ == 0\n");
  309.       show ();
  310.     }
  311.   rc->x = ebeamx + beamadj;
  312.   rc->type = type;
  313.   rc->val = val;
  314. }
  315.  
  316. /* Do a raster change on the unified list */
  317. /* type: type of change */
  318. /* val: value of change */
  319. __inline void do_unified_change (int type, int val)
  320. {
  321.   if (unified_count < MAXLIST)
  322.     {
  323.       unified[unified_count].x = ebeamx + beamadj;
  324.       unified[unified_count].type = type;
  325.       unified[unified_count].val = val;
  326.       unified_count++;
  327.     }
  328. }
  329.  
  330. /* Do a player raster change */
  331. /* i: player to change. 0 or 1 */
  332. /* type: type of change */
  333. /* val: value of change */
  334. __inline void do_plraster_change (int i, int type, int val)
  335. {
  336.   int plc = pl_change_count[i];
  337.   /*printf("Raster change i=%d, x=%d, type=%d, val=%d\n", i, x, type, val); */
  338.   if (plc < MAXLIST)
  339.     {
  340.       do_raster_change (i, type, val, &pl_change[i][plc]);
  341.       if (type == 1)
  342.     pl_change[i][plc].x -= 3;
  343.       pl_change_count[i]++;
  344.     }
  345. }
  346.  
  347. /* Do a playfield raster change */
  348. /* i: playfield to change. Depreciated, as 0 is now only one used */
  349. /* type: type of change */
  350. /* val: value of change */
  351. __inline void do_pfraster_change (int i, int type, int val)
  352. {
  353.   int pfc = pf_change_count[i];
  354.   /*  
  355.      if(ebeamy>=100) {
  356.      printf("Raster change i=%d, x=%d, type=%d, val=%d\n", 
  357.      i, ebeamx+beamadj, type, val);
  358.      show();
  359.      }
  360.    */
  361.   if (pfc < MAXLIST)
  362.     {
  363.       do_raster_change (i, type, val, &pf_change[i][pfc]);
  364.       pf_change_count[i]++;
  365.     }
  366. }
  367.  
  368.  
  369. /* Use a unified change */
  370. /* rc: unified change structure to use */
  371. __inline void use_unified_change (struct RasterChange *rc)
  372. {
  373.   switch (rc->type)
  374.     {
  375.     case 0:
  376.       /* P0MO colour */
  377.       colour_table[P0M0_COLOUR] = rc->val;
  378.       break;
  379.     case 1:
  380.       /* POM0 colour */
  381.       colour_table[P1M1_COLOUR] = rc->val;
  382.       break;
  383.     case 2:
  384.       /* PFBL colour */
  385.       colour_table[PFBL_COLOUR] = rc->val;
  386.       break;
  387.     case 3:
  388.       /* BK colour */
  389.       colour_table[BK_COLOUR] = rc->val;
  390.       break;
  391.     case 4:
  392.       /* Priority change Normal */
  393.       if(rc->val)
  394.     norm_val=1;
  395.       else
  396.     norm_val=0;
  397.       colour_lookup=colour_ptrs[norm_val][scores_val];
  398.       break;
  399.     case 5:
  400.       /* Priority change Scores */
  401.       if(rc->val)
  402.     {
  403.       if(rc->x < 80)
  404.         scores_val=1;
  405.       else
  406.         scores_val=2;
  407.     }
  408.       else
  409.     scores_val=0;
  410.       colour_lookup=colour_ptrs[norm_val][scores_val];
  411.       break;
  412.     }
  413. }
  414.  
  415. /* Use a playfield change */
  416. /* pl: playfield to change */
  417. /* rc: change to make */
  418. __inline void use_pfraster_change (struct PlayField *pl, struct RasterChange *rc)
  419. {
  420.   switch (rc->type)
  421.     {
  422.     case 0:
  423.       /* PF0 */
  424.       pl->pf0 = rc->val;
  425.       break;
  426.     case 1:
  427.       /* PF1 */
  428.       pl->pf1 = rc->val;
  429.       break;
  430.     case 2:
  431.       /* PF2 */
  432.       pl->pf2 = rc->val;
  433.       break;
  434.     case 3:
  435.       /* Reflection */
  436.       pl->ref = rc->val;
  437.       break;
  438.     }
  439. }
  440.  
  441. /* Use a player change */
  442. /* pl: player to change */
  443. /* rc: change to make */
  444. __inline void use_plraster_change (struct Player *pl, struct RasterChange *rc)
  445. {
  446.   switch (rc->type)
  447.     {
  448.     case 0:
  449.       /* GRP */
  450.       pl->grp = rc->val;
  451.       break;
  452.       /* Vertical delay */
  453.     case 1:
  454.       pl->vdel = pl->grp;
  455.       break;
  456.     }
  457. }
  458.  
  459.  
  460. int
  461. do_paddle (int padnum)
  462. {
  463.   int res = 0x80;
  464.   int x;
  465.   if ((tiaWrite[VBLANK] & 0x80) == 0)
  466.     {
  467.       x = 320 - mouse_position ();
  468.       x = x * 45;
  469.       x += paddle[padnum].val;
  470.       if (x > clk)
  471.     res = 0x00;
  472.     }
  473.   return res;
  474. }
  475.  
  476. /* Calculate the keypad rows */
  477. /* i.e. when reading from INPTx we don't know the row */
  478. UBYTE
  479. do_keypad (int pad, int col)
  480. {
  481.   UBYTE res= 0x80;
  482.  
  483.   read_keypad(pad);
  484.   
  485.   /* Bottom row */
  486.   if(pad==0) {
  487.   if( (riotWrite[SWCHA] & 0x80) && keypad[pad][col]==3)
  488.     res=0x00;
  489.   /* Third row */
  490.   if( (riotWrite[SWCHA] & 0x40) && keypad[pad][col]==2)
  491.     res=0x00;
  492.   if( (riotWrite[SWCHA] & 0x20) && keypad[pad][col]==1)
  493.     res=0x00;
  494.   if( (riotWrite[SWCHA] & 0x10) && keypad[pad][col]==0)
  495.     res=0x00;
  496.   } 
  497.   else {
  498.   /* Bottom row */
  499.   if( (riotWrite[SWCHA] & 0x80) && keypad[pad][col]==3)
  500.     res=0x00;
  501.   /* Third row */
  502.   if( (riotWrite[SWCHA] & 0x40) && keypad[pad][col]==2)
  503.     res=0x00;
  504.   if( (riotWrite[SWCHA] & 0x20) && keypad[pad][col]==1)
  505.     res=0x00;
  506.   if( (riotWrite[SWCHA] & 0x10) && keypad[pad][col]==0)
  507.     res=0x00;
  508.   }
  509.   return res;
  510. }
  511.  
  512.  
  513. /* 
  514.    Called when the timer is set .
  515.    Note that res is the bit shift, not absolute value.
  516.    Assumes that any timer interval set will last longer than the instruction
  517.    setting it.
  518.  */
  519. /* res: timer interval resolution as a bit shift value */
  520. /* count: the number of intervals to set */
  521. /* clkadj: the number of CPU cycles into the current instruction  */
  522. void
  523. set_timer (int res, int count, int clkadj)
  524. {
  525.   timer_count = count << res;
  526.   timer_clks = clk + clkadj;
  527.   timer_res = res;
  528. }
  529.  
  530. /* New timer code, now only called on a read of INTIM */
  531. /* clkadj: the number of CPU cycles into the current instruction  */
  532. /* returns: the current timer value */
  533. UBYTE
  534. do_timer (int clkadj)
  535. {
  536.   UBYTE result;
  537.   int delta;
  538.   int value;
  539.  
  540.   delta = clk - timer_clks;
  541.   value = delta >> timer_res;
  542.   if (delta <= timer_count)
  543.     {                /* Timer is still going down in res intervals */
  544.       result = value;
  545.     }
  546.   else
  547.     {
  548.       if (value == 0)
  549.     /* Timer is in holding period */
  550.     result = 0;
  551.       else
  552.     {
  553.       /* Timer is descending from 0xff in clock intervals */
  554.       set_timer (0, 0xff, clkadj);
  555.       result = 0;
  556.     }
  557.     }
  558.  
  559.   /*  printf("Timer result=%d\n", result); */
  560.   return result;
  561. }
  562.  
  563. /* Do the screen related part of a write to VBLANK */
  564. /* b: the byte written */
  565. void
  566. do_vblank (UBYTE b)
  567. {
  568.   if (b & 0x02)
  569.     {
  570.       /* Start vertical blank */
  571.       vbeam_state = VBLANKSTATE;
  572.       /* Also means we can update screen */
  573.       sound_update ();
  574.       update_realjoy ();
  575.       tv_event ();
  576.       tv_display ();
  577. //    /* Only wait if we're on a faster display */
  578. //    if (base_opts.rr == 1)
  579. //     limiter_sync ();
  580.     }
  581.   else
  582.     {
  583.       /* End vblank, and start first hsync drawing */
  584.       int i;
  585.  
  586.       vbeam_state = DRAWSTATE;
  587.       hbeam_state = HSYNCSTATE;
  588.       /* Set up the screen */
  589.       for (i = 0; i < unified_count; i++)
  590.     use_unified_change (&unified[i]);
  591.       /* Hope for a WSYNC, but just in case */
  592.       ebeamx = -tv_hsync;
  593.       ebeamy = 0;
  594.     }
  595. }
  596.  
  597.  
  598. /* do a horizontal sync */
  599. void
  600. do_hsync (void)
  601. {
  602.   /* Only perform heavy stuff if electron beam is in correct position */
  603.   if (vbeam_state == DRAWSTATE && (ebeamx > -tv_hsync))
  604.     {
  605.       tv_raster (ebeamy);
  606.       /* Fix the clock value */
  607.       clk += (ebeamx - tv_width) / 3;
  608.       ebeamy++;
  609.     }
  610.   hbeam_state = HSYNCSTATE;
  611.   ebeamx = -tv_hsync;
  612.   sbeamx = 0;
  613. }
  614.  
  615. /* Main screen logic */
  616. /* clks: CPU clock length of last instruction */
  617. void
  618. do_screen (int clks)
  619. {
  620.   switch (vbeam_state)
  621.     {
  622.     case VSYNCSTATE:
  623.     case VBLANKSTATE:
  624.       switch (hbeam_state)
  625.     {
  626.     case HSYNCSTATE:
  627.       ebeamx += clks * 3;
  628.       if (ebeamx >= 0)
  629.         {
  630.           hbeam_state = DRAWSTATE;
  631.         }
  632.       break;
  633.     case DRAWSTATE:
  634.       ebeamx += clks * 3;
  635.       if (ebeamx >= tv_width)
  636.         {
  637.           ebeamx -= (tv_hsync + tv_width);
  638.           /* Insert hsync stuff here */
  639.           sbeamx = ebeamx;
  640.           hbeam_state = HSYNCSTATE;
  641.         }
  642.       break;
  643.     case OVERSTATE:
  644.       break;
  645.     }
  646.       break;
  647.     case DRAWSTATE:
  648.       switch (hbeam_state)
  649.     {
  650.     case HSYNCSTATE:
  651.       ebeamx += clks * 3;
  652.       if (ebeamx >= 0)
  653.         {
  654.           hbeam_state = DRAWSTATE;
  655.         }
  656.       break;
  657.     case DRAWSTATE:
  658.       ebeamx += clks * 3;
  659.       if (ebeamx >= tv_width)
  660.         {
  661.           /* Insert hsync stuff here */
  662.           sbeamx = ebeamx;
  663.           ebeamx -= (tv_hsync + tv_width);
  664.           tv_raster (ebeamy);
  665.           ebeamy++;
  666.           hbeam_state = HSYNCSTATE;
  667.         }
  668.       if (ebeamy >= tv_height + tv_overscan)
  669.         {
  670.           vbeam_state = OVERSTATE;
  671.           ebeamy = 0;
  672.         }
  673.       break;
  674.     case OVERSTATE:
  675.       break;
  676.     }
  677.       break;
  678.     case OVERSTATE:
  679.       break;
  680.     }
  681. }
  682.