File: pia.c

package info (click to toggle)
atari800 0.8.6-2
  • links: PTS
  • area: contrib
  • in suites: hamm
  • size: 1,364 kB
  • ctags: 2,457
  • sloc: ansic: 22,055; asm: 2,812; cpp: 581; makefile: 68; sh: 8
file content (249 lines) | stat: -rw-r--r-- 5,050 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <stdio.h>

#include "atari.h"
#include "cpu.h"
#include "pia.h"
#include "platform.h"

#define FALSE 0
#define TRUE 1

static char *rcsid = "$Id: pia.c,v 1.12 1998/02/21 14:55:43 david Exp $";

UBYTE PACTL;
UBYTE PBCTL;
UBYTE PORTA;
UBYTE PORTB;

int xe_bank = -1;

int rom_inserted;
UBYTE atari_basic[8192];
UBYTE atarixl_os[16384];
static UBYTE under_atari_basic[8192];
static UBYTE under_atarixl_os[16384];
static UBYTE atarixe_memory[65536];
static UBYTE atarixe_16kbuffer[16384];

static UBYTE PORTA_mask = 0xff;
static UBYTE PORTB_mask = 0xff;

void PIA_Initialise(int *argc, char *argv[])
{
	PORTA = 0xff;
	PORTB = 0xff;
}

UBYTE PIA_GetByte(UWORD addr)
{
	UBYTE byte;

	if (machine == Atari)
		addr &= 0xff03;
	switch (addr) {
	case _PACTL:
		byte = PACTL;
		break;
	case _PBCTL:
		byte = PBCTL;
#ifdef DEBUG1
		printf("RD: PBCTL = %x, PC = %x\n", PBCTL, PC);
#endif
		break;
	case _PORTA:
		byte = Atari_PORT(0);
		byte &= PORTA_mask;
		break;
	case _PORTB:
		switch (machine) {
		case Atari:
			byte = Atari_PORT(1);
			byte &= PORTB_mask;
			break;
		case AtariXL:
		case AtariXE:
			byte = PORTB;
			break;
		default:
			printf("Fatal Error in pia.c: PIA_GetByte(): Unknown machine\n");
			Atari800_Exit(FALSE);
			exit(1);
			break;
		}
		break;
	}

	return byte;
}

int PIA_PutByte(UWORD addr, UBYTE byte)
{
	if (machine == Atari)
		addr &= 0xff03;

	switch (addr) {
	case _PACTL:
		PACTL = byte;
		break;
	case _PBCTL:
		PBCTL = byte;
#ifdef DEBUG1
		printf("WR: PBCTL = %x, PC = %x\n", PBCTL, PC);
#endif
		break;
	case _PORTA:
		if (!(PACTL & 0x04))
			PORTA_mask = ~byte;
		break;
	case _PORTB:
		if ((byte == 0) && (machine == AtariXL || machine == AtariXE))
			break;				/* special hack for old Atari800 games like is Tapper, for example */

		switch (machine) {
		case Atari:
			if (!(PBCTL & 0x04))
				PORTB_mask = ~byte;
			break;
		case AtariXE:
			{
				int cpu_flag = (byte & 0x10);
#ifdef DEBUG
				int antic_flag = (byte & 0x20);
#endif
				int bank = (byte & 0x0c) >> 2;

#ifdef DEBUG
				printf("CPU = %d, ANTIC = %d, XE BANK = %d\n",
					   cpu_flag, antic_flag, bank);
#endif

/*
 * Possible Bank Transitions
 *
 * Main        -> Main
 * Main        -> Bank1,2,3,4
 * Bank1,2,3,4 -> Main
 * Bank1,2,3,4 -> Bank1,2,3,4
 */
				if (cpu_flag) {
					if (xe_bank != -1) {
						memcpy(&atarixe_memory[xe_bank * 16384],
							   &memory[0x4000],
							   16384);
						memcpy(&memory[0x4000], atarixe_16kbuffer, 16384);
						xe_bank = -1;
					}
				}
				else if (bank != xe_bank) {
					if (xe_bank == -1) {
						memcpy(atarixe_16kbuffer,
							   &memory[0x4000],
							   16384);
					}
					else {
						memcpy(&atarixe_memory[xe_bank * 16384],
							   &memory[0x4000],
							   16384);
					}

					memcpy(&memory[0x4000],
						   &atarixe_memory[bank * 16384],
						   16384);
					xe_bank = bank;
				}
			}
		case AtariXL:
#ifdef DEBUG
			printf("Storing %x to PORTB, PC = %x\n", byte, regPC);
#endif
/*
 * Enable/Disable OS ROM 0xc000-0xcfff and 0xd800-0xffff
 */
			if (!(PORTB & 0x01)) {
				memcpy(under_atarixl_os, memory + 0xc000, 0x1000);
				memcpy(under_atarixl_os + 0x1800, memory + 0xd800, 0x2800);
			}
			if (byte & 0x01) {
#ifdef DEBUG
				printf("OS ROM Enabled\n");
#endif
				memcpy(memory + 0xc000, atarixl_os, 0x1000);
				memcpy(memory + 0xd800, atarixl_os + 0x1800, 0x2800);
				SetROM(0xc000, 0xcfff);
				SetROM(0xd800, 0xffff);
			}
			else {
#ifdef DEBUG
				printf("OS ROM Disabled\n");
#endif
				memcpy(memory + 0xc000, under_atarixl_os, 0x1000);
				memcpy(memory + 0xd800, under_atarixl_os + 0x1800, 0x2800);
				SetRAM(0xc000, 0xcfff);
				SetRAM(0xd800, 0xffff);
			}
/*
   =====================================
   An Atari XL/XE can only disable Basic
   Other cartridge cannot be disable
   =====================================
 */
			if (!rom_inserted) {
/*
 * If RAM is currently enabled between 0xa000 and
 * 0xbfff then under_atari_basic is updated
 */
				if (PORTB & 0x02) {
					memcpy(under_atari_basic, memory + 0xa000, 0x2000);
				}
/*
 * Enable/Disable BASIC ROM
 */
				if (byte & 0x02) {
#ifdef DEBUG
					printf("BASIC disabled\n");
#endif
					memcpy(memory + 0xa000, under_atari_basic, 0x2000);
					SetRAM(0xa000, 0xbfff);
				}
				else {
#ifdef DEBUG
					printf("BASIC enabled\n");
#endif
					memcpy(memory + 0xa000, atari_basic, 0x2000);
					SetROM(0xa000, 0xbfff);
				}
			}
/*
 * Enable/Disable Self Test ROM
 */
			if (PORTB & 0x80) {
				memcpy(under_atarixl_os + 0x1000, memory + 0x5000, 0x800);
			}
			if (byte & 0x80) {
#ifdef DEBUG
				printf("Self Test ROM Disabled\n");
#endif
				memcpy(memory + 0x5000, under_atarixl_os + 0x1000, 0x800);
				SetRAM(0x5000, 0x57ff);
			}
			else {
#ifdef DEBUG
				printf("Self Test ROM Enabled\n");
#endif
				memcpy(memory + 0x5000, atarixl_os + 0x1000, 0x800);
				SetROM(0x5000, 0x57ff);
			}

			PORTB = byte;
			break;
		default:
			printf("Fatal Error in pia.c: PIA_PutByte(): Unknown machine\n");
			Atari800_Exit(FALSE);
			exit(1);
			break;
		}
		break;
	}

	return FALSE;
}