aboutsummaryrefslogtreecommitdiff
path: root/src/fake86/ports.c
blob: 88f047aae4ef42f7201aa1cc88b04b8690f8a723 (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
/*
  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.
*/

/* ports.c: functions to handle port I/O from the CPU module, as well
   as functions for emulated hardware components to register their
   read/write callback functions across the port address range. */

#include <stdint.h>
#include <stdio.h>
#include "cpu.h"

extern uint8_t portram[0x10000];
extern uint8_t speakerenabled;
extern uint8_t keyboardwaitack;

void (*do_callback_write) (uint16_t portnum, uint8_t value) = NULL;
uint8_t (*do_callback_read) (uint16_t portnum) = NULL;
void (*do_callback_write16) (uint16_t portnum, uint16_t value) = NULL;
uint16_t (*do_callback_read16) (uint16_t portnum) = NULL;
void * (port_write_callback[0x10000]);
void * (port_read_callback[0x10000]);
void * (port_write_callback16[0x10000]);
void * (port_read_callback16[0x10000]);

extern uint8_t verbose;
void portout (uint16_t portnum, uint8_t value) {
	portram[portnum] = value;
	//if (verbose) printf("portout(0x%X, 0x%02X);\n", portnum, value);
	switch (portnum) {
			case 0x61:
				if ( (value & 3) == 3) speakerenabled = 1;
				else speakerenabled = 0;
				return;
		}
	do_callback_write = (void (*) (uint16_t portnum, uint8_t value) ) port_write_callback[portnum];
	if (do_callback_write != (void *) 0) (*do_callback_write) (portnum, value);
}

uint8_t portin (uint16_t portnum) {
	//if (verbose) printf("portin(0x%X);\n", portnum);
	switch (portnum) {
			case 0x62:
				return (0x00);
			case 0x60:
			case 0x61:
			case 0x63:
			case 0x64:
				return (portram[portnum]);
		}
	do_callback_read = (uint8_t (*) (uint16_t portnum) ) port_read_callback[portnum];
	if (do_callback_read != (void *) 0) return ( (*do_callback_read) (portnum) );
	return (0xFF);
}

void portout16 (uint16_t portnum, uint16_t value) {
	do_callback_write16 = (void (*) (uint16_t portnum, uint16_t value) ) port_write_callback16[portnum];
	if (do_callback_write16 != (void *) 0) {
			(*do_callback_write16) (portnum, value);
			return;
		}

	portout (portnum, (uint8_t) value);
	portout (portnum + 1, (uint8_t) (value >> 8) );
}

uint16_t portin16 (uint16_t portnum) {
	uint16_t ret;

	do_callback_read16 = (uint16_t (*) (uint16_t portnum) ) port_read_callback16[portnum];
	if (do_callback_read16 != (void *) 0) return ( (*do_callback_read16) (portnum) );

	ret = (uint16_t) portin (portnum);
	ret |= (uint16_t) portin (portnum+1) << 8;
	return (ret);
}

void set_port_write_redirector (uint16_t startport, uint16_t endport, void *callback) {
	uint16_t i;
	for (i=startport; i<=endport; i++) {
			port_write_callback[i] = callback;
		}
}

void set_port_read_redirector (uint16_t startport, uint16_t endport, void *callback) {
	uint16_t i;
	for (i=startport; i<=endport; i++) {
			port_read_callback[i] = callback;
		}
}

void set_port_write_redirector_16 (uint16_t startport, uint16_t endport, void *callback) {
	uint16_t i;
	for (i=startport; i<=endport; i++) {
			port_write_callback16[i] = callback;
		}
}

void set_port_read_redirector_16 (uint16_t startport, uint16_t endport, void *callback) {
	uint16_t i;
	for (i=startport; i<=endport; i++) {
			port_read_callback16[i] = callback;
		}
}