summaryrefslogtreecommitdiff
path: root/src/apple.c
blob: 52ef358b2dc0123d2e2005975c0c34d8bce0beaf (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
#include"apple.h"

byte* ROM;

void AppleOn() {
	Memory = calloc(MEMORY_SIZE, sizeof(byte));
	ROM = calloc(256, sizeof(byte));
	InitInstructionTable();

	// Load ROM.
	FILE *ROM_File = fopen ("rom.bin", "rb");
	if (ROM_File == NULL) {
		printf("\n\rROM does not exist.\n");
		abort();
	}
	for (int i = 0; i < 256; i++) {
		ROM[i] = fgetc(ROM_File);
		printf("%x", ROM[i]);
	}
}

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 ToAppleAscii(char x)
{
	if (x < 0x20 || x >= 0x60)
		return -1;
	if (x >= 0x40)
		x -= 0x40;
	return x;
}

byte ToAscii(char x)
{
	if (x < 0x20)
		x += 0x40;
	return x;
}*/


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.
		// So KBD_CR and DSP always return a high high-order bit, and when the keyboard data is requested it will wait for UserInput() to finish.
	case KBD:
		return 0b10000000 | UserInput();
	case KBD_CR: case DSP:
		return 0b10000000;
	}

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

void SetMemory(address x, byte y){
	if (x < MEMORY_SIZE) {
		Memory[x] = y;
	}
}