summaryrefslogtreecommitdiff
path: root/src/apple.c
blob: 5877bc866f30d890c77689e6508685e6c6086740 (plain)
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
#include"apple.h"

// The Incredible Wozmon!
const byte ROM[256] = {
	0xd8, 0x58, 0xa0, 0x7f, 0x8c, 0x12, 0xd0, 0xa9,
	0xa7, 0x8d, 0x11, 0xd0, 0x8d, 0x13, 0xd0, 0xc9,
	0xdf, 0xf0, 0x13, 0xc9, 0x9b, 0xf0, 0x03, 0xc8,
	0x10, 0x0f, 0xa9, 0xdc, 0x20, 0xef, 0xff, 0xa9,
	0x8d, 0x20, 0xef, 0xff, 0xa0, 0x01, 0x88, 0x30,
	0xf6, 0xad, 0x11, 0xd0, 0x10, 0xfb, 0xad, 0x10,
	0xd0, 0x99, 0x00, 0x02, 0x20, 0xef, 0xff, 0xc9,
	0x8d, 0xd0, 0xd4, 0xa0, 0xff, 0xa9, 0x00, 0xaa,
	0x0a, 0x85, 0x2b, 0xc8, 0xb9, 0x00, 0x02, 0xc9,
	0x8d, 0xf0, 0xd4, 0xc9, 0xae, 0x90, 0xf4, 0xf0,
	0xf0, 0xc9, 0xba, 0xf0, 0xeb, 0xc9, 0xd2, 0xf0,
	0x3b, 0x86, 0x28, 0x86, 0x29, 0x84, 0x2a, 0xb9,
	0x00, 0x02, 0x49, 0xb0, 0xc9, 0x0a, 0x90, 0x06,
	0x69, 0x88, 0xc9, 0xfa, 0x90, 0x11, 0x0a, 0x0a,
	0x0a, 0x0a, 0xa2, 0x04, 0x0a, 0x26, 0x28, 0x26,
	0x29, 0xca, 0xd0, 0xf8, 0xc8, 0xd0, 0xe0, 0xc4,
	0x2a, 0xf0, 0x97, 0x24, 0x2b, 0x50, 0x10, 0xa5,
	0x28, 0x81, 0x26, 0xe6, 0x26, 0xd0, 0xb5, 0xe6,
	0x27, 0x4c, 0x44, 0xff, 0x6c, 0x24, 0x00, 0x30,
	0x2b, 0xa2, 0x02, 0xb5, 0x27, 0x95, 0x25, 0x95,
	0x23, 0xca, 0xd0, 0xf7, 0xd0, 0x14, 0xa9, 0x8d,
	0x20, 0xef, 0xff, 0xa5, 0x25, 0x20, 0xdc, 0xff,
	0xa5, 0x24, 0x20, 0xdc, 0xff, 0xa9, 0xba, 0x20,
	0xef, 0xff, 0xa9, 0xa0, 0x20, 0xef, 0xff, 0xa1,
	0x24, 0x20, 0xdc, 0xff, 0x86, 0x2b, 0xa5, 0x24,
	0xc5, 0x28, 0xa5, 0x25, 0xe5, 0x29, 0xb0, 0xc1,
	0xe6, 0x24, 0xd0, 0x02, 0xe6, 0x25, 0xa5, 0x24,
	0x29, 0x07, 0x10, 0xc8, 0x48, 0x4a, 0x4a, 0x4a,
	0x4a, 0x20, 0xe5, 0xff, 0x68, 0x29, 0x0f, 0x09,
	0xb0, 0xc9, 0xba, 0x90, 0x02, 0x69, 0x06, 0x2c,
	0x12, 0xd0, 0x30, 0xfb, 0x8d, 0x12, 0xd0, 0x60,
	0x00, 0x00, 0x00, 0x0f, 0x00, 0xff, 0x00, 0x00
};

void AppleOn() {
	Memory = calloc(MEMORY_SIZE, sizeof(byte));
	//ROM = calloc(256, sizeof(byte));
	InitInstructionTable();
	PC = 0xFF00;
	// Load ROM (alternative)
	/*FILE *ROM_File = fopen ("rom.bin", "rb");
	if (ROM_File == NULL) {
		printf("\n\rROM does not exist.\n");
		abort();
	}
	for (int i = 0; i < 128; i++) {
		// Retrieve in reversed order.
		ROM[(2*i)+1] = fgetc(ROM_File);
		ROM[(2*i)  ] = fgetc(ROM_File);
	}*/
}

void AppleReset(){
	acc = 0; X = 0; Y = 0; P = 0; S = 0;
	idata.cycles = 0; idata.length = 0; idata.add = 0; idata.value = 0;
	PC = 0xFF00;
	free(Memory);
	Memory = calloc(MEMORY_SIZE, sizeof(byte));
}


//byte KBD_CR_val;

byte GetMemory(address x){
	switch(x)
	{
	// The keyboard does not act precisely as the real hardware, because there is no need to wait for the keyboard.
	case KBD:
		char c = -1;
		while(c == -1)
			c = UserInput();
		return 0b10000000 | c;
	case KBD_CR:
		return 0b10100111;
	case DSP:
		return 0b00000000;
	}

	if (x >= 0xFF00 && x <= 0xFFFF) {
		return ROM[0x00FF & x];
	}

	if (x < MEMORY_SIZE)
		return Memory[x];

	return -1;
}

void SetMemory(address x, byte y){
	switch(x)
		{
		  //case KBD_CR:
		  //KBD_CR_val = x;
		case DSP:
			if (y & 0x80)
				DisplayInput(y);
		}
	if (x < MEMORY_SIZE) {
		Memory[x] = y;
	}
}



byte UserInput()
{
	#ifdef GRAPHICAL
	// SDL code
	#else
	int c = getch();
	#endif
	
	switch(c)
		{
		case 0x08:
			return BS;
		case 0x0A:
			return CR;
		case 0x1B:
			return ESC;
		}
		
	if (c < 0x20)
		return -1;

	if (c >= 0x60)
		return -1;

	return c;
}