home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / atari / atari800-0.8.6 / pia.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-10  |  10.3 KB  |  311 lines

  1. #include <stdio.h>
  2.  
  3. #ifdef AMIGA_PPC
  4. #include <exec/types.h>
  5. #include <clib/powerpc_protos.h>
  6. #endif
  7.  
  8. #include "atari.h"
  9. #include "cpu.h"
  10. #include "pia.h"
  11. #include "platform.h"
  12.  
  13. #define FALSE 0
  14. #define TRUE 1
  15.  
  16. static char *rcsid = "$Id: pia.c,v 1.12 1998/02/21 14:55:43 david Exp $";
  17.  
  18. UBYTE PACTL;
  19. UBYTE PBCTL;
  20. UBYTE PORTA;
  21. UBYTE PORTB;
  22.  
  23. int xe_bank = -1;
  24.  
  25. int rom_inserted;
  26. UBYTE atari_basic[8192];
  27. UBYTE atarixl_os[16384];
  28. static UBYTE under_atari_basic[8192];
  29. static UBYTE under_atarixl_os[16384];
  30. static UBYTE atarixe_memory[65536];
  31. static UBYTE atarixe_16kbuffer[16384];
  32.  
  33. static UBYTE PORTA_mask = 0xff;
  34. static UBYTE PORTB_mask = 0xff;
  35.  
  36. void PIA_Initialise(int *argc, char *argv[])
  37. {
  38.         PORTA = 0xff;
  39.         PORTB = 0xff;
  40. }
  41.  
  42. UBYTE PIA_GetByte(UWORD addr)
  43. {
  44.         UBYTE byte;
  45.  
  46.         if (machine == Atari)
  47.                 addr &= 0xff03;
  48.         switch (addr) {
  49.         case _PACTL:
  50.                 byte = PACTL;
  51.                 break;
  52.         case _PBCTL:
  53.                 byte = PBCTL;
  54. #ifdef DEBUG1
  55.                 printf("RD: PBCTL = %x, PC = %x\n", PBCTL, PC);
  56. #endif
  57.                 break;
  58.         case _PORTA:
  59.                 byte = Atari_PORT(0);
  60.                 byte &= PORTA_mask;
  61.                 break;
  62.         case _PORTB:
  63.                 switch (machine) {
  64.                 case Atari:
  65.                         byte = Atari_PORT(1);
  66.                         byte &= PORTB_mask;
  67.                         break;
  68.                 case AtariXL:
  69.                 case AtariXE:
  70.                         byte = PORTB;
  71.                         break;
  72.                 default:
  73.                         printf("Fatal Error in pia.c: PIA_GetByte(): Unknown machine\n");
  74.                         Atari800_Exit(FALSE);
  75.                         exit(1);
  76.                         break;
  77.                 }
  78.                 break;
  79.         }
  80.  
  81.         return byte;
  82. }
  83.  
  84. int PIA_PutByte(UWORD addr, UBYTE byte)
  85. {
  86.         if (machine == Atari)
  87.                 addr &= 0xff03;
  88.  
  89.         switch (addr) {
  90.         case _PACTL:
  91.                 PACTL = byte;
  92.                 break;
  93.         case _PBCTL:
  94.                 PBCTL = byte;
  95. #ifdef DEBUG1
  96.                 printf("WR: PBCTL = %x, PC = %x\n", PBCTL, PC);
  97. #endif
  98.                 break;
  99.         case _PORTA:
  100.                 if (!(PACTL & 0x04))
  101.                         PORTA_mask = ~byte;
  102.                 break;
  103.         case _PORTB:
  104.                 if ((byte == 0) && (machine == AtariXL || machine == AtariXE))
  105.                         break;                          /* special hack for old Atari800 games like is Tapper, for example */
  106.  
  107.                 switch (machine) {
  108.                 case Atari:
  109.                         if (!(PBCTL & 0x04))
  110.                                 PORTB_mask = ~byte;
  111.                         break;
  112.                 case AtariXE:
  113.                         {
  114.                                 int cpu_flag = (byte & 0x10);
  115. #ifdef DEBUG
  116.                                 int antic_flag = (byte & 0x20);
  117. #endif
  118.                                 int bank = (byte & 0x0c) >> 2;
  119.  
  120. #ifdef DEBUG
  121.                                 printf("CPU = %d, ANTIC = %d, XE BANK = %d\n",
  122.                                            cpu_flag, antic_flag, bank);
  123. #endif
  124.  
  125. /*
  126.  * Possible Bank Transitions
  127.  *
  128.  * Main        -> Main
  129.  * Main        -> Bank1,2,3,4
  130.  * Bank1,2,3,4 -> Main
  131.  * Bank1,2,3,4 -> Bank1,2,3,4
  132.  */
  133.                                 if (cpu_flag) {
  134.                                         if (xe_bank != -1) {
  135. #ifdef AMIGA_PPC
  136.                                                 CopyMemPPC(&memory[0x4000],&atarixe_memory[xe_bank*16384],16384);
  137.                                                 CopyMemPPC(atarixe_16kbuffer,&memory[0x4000],16384);
  138. #else
  139.                                                 memcpy(&atarixe_memory[xe_bank * 16384],
  140.                                                            &memory[0x4000],
  141.                                                            16384);
  142.                                                 memcpy(&memory[0x4000], atarixe_16kbuffer, 16384);
  143. #endif
  144.                                                 xe_bank = -1;
  145.                                         }
  146.                                 }
  147.                                 else if (bank != xe_bank) {
  148.                                         if (xe_bank == -1) {
  149. #ifdef AMIGA_PPC
  150.                                                 CopyMemPPC(&memory[0x4000],atarixe_16kbuffer,16384);
  151. #else
  152.                                                 memcpy(atarixe_16kbuffer,
  153.                                                            &memory[0x4000],
  154.                                                            16384);
  155. #endif
  156.                                         }
  157.                                         else {
  158. #ifdef AMIGA_PPC
  159.                                                 CopyMemPPC(&memory[0x4000],&atarixe_memory[xe_bank*16384],16384);
  160. #else
  161.                                                 memcpy(&atarixe_memory[xe_bank * 16384],
  162.                                                            &memory[0x4000],
  163.                                                            16384);
  164. #endif
  165.                                         }
  166.  
  167. #ifdef AMIGA_PPC
  168.                                         CopyMemPPC(&atarixe_memory[bank*16384],&memory[0x4000],16384);
  169. #else
  170.                                         memcpy(&memory[0x4000],
  171.                                                    &atarixe_memory[bank * 16384],
  172.                                                    16384);
  173. #endif
  174.                                         xe_bank = bank;
  175.                                 }
  176.                         }
  177.                 case AtariXL:
  178. #ifdef DEBUG
  179.                         printf("Storing %x to PORTB, PC = %x\n", byte, regPC);
  180. #endif
  181. /*
  182.  * Enable/Disable OS ROM 0xc000-0xcfff and 0xd800-0xffff
  183.  */
  184.                         if (!(PORTB & 0x01)) {
  185. #ifdef AMIGA_PPC
  186.                                 CopyMemPPC(memory+0xc000,under_atarixl_os,0x1000);
  187.                                 CopyMemPPC(memory+0xd800,under_atarixl_os,0x2800);
  188. #else
  189.                                 memcpy(under_atarixl_os, memory + 0xc000, 0x1000);
  190.                                 memcpy(under_atarixl_os + 0x1800, memory + 0xd800, 0x2800);
  191. #endif
  192.                         }
  193.                         if (byte & 0x01) {
  194. #ifdef DEBUG
  195.                                 printf("OS ROM Enabled\n");
  196. #endif
  197. #ifdef AMIGA_PPC
  198.                                 CopyMemPPC(atarixl_os,memory+0xc000,0x1000);
  199.                                 CopyMemPPC(atarixl_os+0x1800,memory+0xd800,0x2800);
  200. #else
  201.                                 memcpy(memory + 0xc000, atarixl_os, 0x1000);
  202.                                 memcpy(memory + 0xd800, atarixl_os + 0x1800, 0x2800);
  203. #endif
  204.                                 SetROM(0xc000, 0xcfff);
  205.                                 SetROM(0xd800, 0xffff);
  206.                         }
  207.                         else {
  208. #ifdef DEBUG
  209.                                 printf("OS ROM Disabled\n");
  210. #endif
  211. #ifdef AMIGA_PPC
  212.                                 CopyMemPPC(under_atarixl_os,memory+0xc000,0x1000);
  213.                                 CopyMemPPC(under_atarixl_os+0x1800,memory+0xd800,0x2800);
  214. #else
  215.                                 memcpy(memory + 0xc000, under_atarixl_os, 0x1000);
  216.                                 memcpy(memory + 0xd800, under_atarixl_os + 0x1800, 0x2800);
  217. #endif
  218.                                 SetRAM(0xc000, 0xcfff);
  219.                                 SetRAM(0xd800, 0xffff);
  220.                         }
  221. /*
  222.    =====================================
  223.    An Atari XL/XE can only disable Basic
  224.    Other cartridge cannot be disable
  225.    =====================================
  226.  */
  227.                         if (!rom_inserted) {
  228. /*
  229.  * If RAM is currently enabled between 0xa000 and
  230.  * 0xbfff then under_atari_basic is updated
  231.  */
  232.                                 if (PORTB & 0x02) {
  233. #ifdef AMIGA_PPC
  234.                                         CopyMemPPC(memory+0xa000,under_atari_basic,0x2000);
  235. #else
  236.                                         memcpy(under_atari_basic, memory + 0xa000, 0x2000);
  237. #endif
  238.                                 }
  239. /*
  240.  * Enable/Disable BASIC ROM
  241.  */
  242.                                 if (byte & 0x02) {
  243. #ifdef DEBUG
  244.                                         printf("BASIC disabled\n");
  245. #endif
  246. #ifdef AMIGA_PPC
  247.                                         CopyMemPPC(under_atari_basic,memory+0xa000,0x2000);
  248. #else
  249.                                         memcpy(memory + 0xa000, under_atari_basic, 0x2000);
  250. #endif
  251.                                         SetRAM(0xa000, 0xbfff);
  252.                                 }
  253.                                 else {
  254. #ifdef DEBUG
  255.                                         printf("BASIC enabled\n");
  256. #endif
  257. #ifdef AMIGA_PPC
  258.                                         CopyMemPPC(atari_basic,memory+0xa000,0x2000);
  259. #else
  260.                                         memcpy(memory + 0xa000, atari_basic, 0x2000);
  261. #endif
  262.                                         SetROM(0xa000, 0xbfff);
  263.                                 }
  264.                         }
  265. /*
  266.  * Enable/Disable Self Test ROM
  267.  */
  268.                         if (PORTB & 0x80) {
  269. #ifdef AMIGA_PPC
  270.                                 CopyMemPPC(memory+0x5000,under_atarixl_os+0x1000,0x800);
  271. #else
  272.                                 memcpy(under_atarixl_os + 0x1000, memory + 0x5000, 0x800);
  273. #endif
  274.                         }
  275.                         if (byte & 0x80) {
  276. #ifdef DEBUG
  277.                                 printf("Self Test ROM Disabled\n");
  278. #endif
  279. #ifdef AMIGA_PPC
  280.                                 CopyMemPPC(under_atarixl_os+0x1000,memory+0x5000,0x800);
  281. #else
  282.                                 memcpy(memory + 0x5000, under_atarixl_os + 0x1000, 0x800);
  283. #endif
  284.                                 SetRAM(0x5000, 0x57ff);
  285.                         }
  286.                         else {
  287. #ifdef DEBUG
  288.                                 printf("Self Test ROM Enabled\n");
  289. #endif
  290. #ifdef AMIGA_PPC
  291.                                 CopyMemPPC(atarixl_os+0x1000,memory+0x5000,0x800);
  292. #else
  293.                                 memcpy(memory + 0x5000, atarixl_os + 0x1000, 0x800);
  294. #endif
  295.                                 SetROM(0x5000, 0x57ff);
  296.                         }
  297.  
  298.                         PORTB = byte;
  299.                         break;
  300.                 default:
  301.                         printf("Fatal Error in pia.c: PIA_PutByte(): Unknown machine\n");
  302.                         Atari800_Exit(FALSE);
  303.                         exit(1);
  304.                         break;
  305.                 }
  306.                 break;
  307.         }
  308.  
  309.         return FALSE;
  310. }
  311.