aboutsummaryrefslogtreecommitdiff
path: root/src/fake86/ata.c
blob: df3b50aa190d37dd32a0d0e6f3e2f93aecac021f (plain) (blame)
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
/*
  Fake86: A portable, open-source 8086 PC emulator.
  Copyright (C)2010-2013 Mike Chambers

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "config.h"
#ifdef DISK_CONTROLLER_ATA
#include <stdint.h>
#include <stdio.h>
#include <memory.h>
#include <string.h>

uint8_t idATA[512];

#define ATA_STATUS_ERROR 0x01
#define ATA_STATUS_DRQ   0x08
#define ATA_STATUS_SRV   0x10
#define ATA_STATUS_FAULT 0x20
#define ATA_STATUS_READY 0x40
#define ATA_STATUS_BUSY  0x80

#define flip16(x) ((x&255) | (x>>8))
#define flip32(x) ((x>>24) | (((x>>16)&255)<<8) | (((x>>8)&255)<<16) | ((x&255)<<24))

uint8_t statusreg = 0, errorreg = 0, drivesel = 0, databuf[512];
uint16_t dataptr = 512;

void bufclear() {
	memset (databuf, 0, 512);
}

void bufcook() {
	uint16_t i;
	uint8_t cc;
	for (i=0; i<512; i+=2) {
			cc = databuf[i];
			databuf[i] = databuf[i+1];
			databuf[i+1] = cc;
		}
}

void bufwrite8 (uint16_t bufpos, uint8_t value) {
	databuf[bufpos] = value;
}

void bufwrite16 (uint16_t bufpos, uint16_t value) {
	databuf[bufpos] = (uint8_t) value;
	databuf[bufpos+1] = (uint8_t) (value>>8);
}

void flipstring (uint8_t *dest, uint8_t *src) {
	uint16_t i;
	uint8_t cc;
	strcpy (dest, src);
	for (i=0; i<strlen (dest); i+=2) {
			cc = databuf[i];
			databuf[i] = databuf[i+1];
			databuf[i+1] = cc;
		}
}

void cmdATA (uint8_t value) {
	switch (value) {
			case 0x91: //INITIALIZE DEVICE PARAMETERS
				dataptr = 512;
				break;
			case 0xEC: //IDENTIFY
				memset (&idATA, 0, sizeof (idATA) );
				memcpy (databuf, &idATA, 512);
				dataptr = 0;
				break;
		}
}

void outATA (uint16_t portnum, uint8_t value) {
	//printf("[DEBUG] ATA port %Xh write: %02X\n", portnum, value);
	//getch();
	switch (portnum) {
			case 0x1F6:
				drivesel = (value >> 4) & 1;
				break;
			case 0x1F7: //command register
				cmdATA (value);
				break;
		}
}

uint8_t inATA (uint16_t portnum) {
	//if (portnum != 0x1F0) printf("[DEBUG] ATA port %Xh read\n", portnum);
	//getch();
	switch (portnum) {
			case 0x1F0: //data read
				if (dataptr < 512) {
						//printf("%c", databuf[dataptr]);
						return (databuf[dataptr++]);
					}
				else return (0);
			case 0x1F1: //error register
				if (drivesel == 1) return (1);
				else return (0);
			case 0x1F7: //status register
				statusreg = ATA_STATUS_READY;
				if (drivesel == 1) statusreg |= ATA_STATUS_ERROR;
				if (dataptr < 512) statusreg |= ATA_STATUS_DRQ;
				return (statusreg);
		}
	return (0);
}
#endif