summaryrefslogtreecommitdiffstats
path: root/emulator/devices/ports.c
blob: 7d278ff3cdfd3d8a67faa360214eeca4c92d4275 (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
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
250
251
252
253
254
255
256
257
258
259
/*
 *   AVR emulator
 *   Ports
 *
 *   Copyright (C) 2007 Michael Buesch <mb@bu3sch.de>
 *
 *   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.
 */

#include "ports.h"
#include "memory.h"
#include "util.h"

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>


enum port_voltage_level {
	PORT_VOLT_TRISTATE = 0,
	PORT_VOLT_HIGH,
	PORT_VOLT_LOW,
};

struct port {
	/* Spinlock for this port. */
	pthread_spinlock_t lock;
	/* Data direction of the individual port bits.
	 * Bit-set means output. */
	uint8_t ddr;
	/* PORTX register status. */
	uint8_t port;
	/* Voltage level status on the physical pin. */
	enum port_voltage_level levels[8];
};


static struct port portb;
static struct port portc;
static struct port portd;


static void port_read(struct port *port, uint16_t mem_offset)
{
	pthread_spin_lock(&port->lock);
	memory.ram[mem_offset] = port->port;
	pthread_spin_lock(&port->lock);
}

static void port_write(struct port *port, uint8_t new_val)
{
	pthread_spin_lock(&port->lock);
	port->port = new_val;
	//TODO
	pthread_spin_unlock(&port->lock);
}

static void ddr_read(struct port *port, uint16_t mem_offset)
{
	pthread_spin_lock(&port->lock);
	memory.ram[mem_offset] = port->ddr;
	pthread_spin_unlock(&port->lock);
}

static void ddr_write(struct port *port, uint8_t new_val)
{
	pthread_spin_lock(&port->lock);
	port->ddr = new_val;
	pthread_spin_unlock(&port->lock);
}

static void pin_read(struct port *port, uint16_t mem_offset)
{
	int i;
	uint8_t mask, data = 0;

	pthread_spin_lock(&port->lock);
	for (i = 0; i < 8; i++) {
		mask = (1 << i);
		if (unlikely(port->ddr & mask)) {
			/* This is an output pin. */
			if (port->port & mask)
				data |= mask;
		} else {
			/* This is an input pin. */
			switch (port->levels[i]) {
			case PORT_VOLT_TRISTATE:
				if (port->port & mask)
					data |= mask;
				break;
			case PORT_VOLT_HIGH:
				data |= mask;
				break;
			case PORT_VOLT_LOW:
				break;
			}
		}
	}
	memory.ram[mem_offset] = data;
	pthread_spin_lock(&port->lock);
}

static void io_portb_write(uint16_t mem_offset, uint8_t old_val, uint8_t new_val)
{
	port_write(&portb, new_val);
}

static void io_portc_write(uint16_t mem_offset, uint8_t old_val, uint8_t new_val)
{
	port_write(&portc, new_val);
}

static void io_portd_write(uint16_t mem_offset, uint8_t old_val, uint8_t new_val)
{
	port_write(&portd, new_val);
}

static void io_ddrb_write(uint16_t mem_offset, uint8_t old_val, uint8_t new_val)
{
	ddr_write(&portb, new_val);
}

static void io_ddrc_write(uint16_t mem_offset, uint8_t old_val, uint8_t new_val)
{
	ddr_write(&portc, new_val);
}

static void io_ddrd_write(uint16_t mem_offset, uint8_t old_val, uint8_t new_val)
{
	ddr_write(&portd, new_val);
}

static void io_portb_read(uint16_t mem_offset)
{
	port_read(&portb, mem_offset);
}

static void io_portc_read(uint16_t mem_offset)
{
	port_read(&portc, mem_offset);
}

static void io_portd_read(uint16_t mem_offset)
{
	port_read(&portd, mem_offset);
}

static void io_pinb_read(uint16_t mem_offset)
{
	pin_read(&portb, mem_offset);
}

static void io_pinc_read(uint16_t mem_offset)
{
	pin_read(&portc, mem_offset);
}

static void io_pind_read(uint16_t mem_offset)
{
	pin_read(&portd, mem_offset);
}

static void io_ddrb_read(uint16_t mem_offset)
{
	ddr_read(&portb, mem_offset);
}

static void io_ddrc_read(uint16_t mem_offset)
{
	ddr_read(&portc, mem_offset);
}

static void io_ddrd_read(uint16_t mem_offset)
{
	ddr_read(&portd, mem_offset);
}

static int port_init(struct port *port)
{
	int err;

	memset(port, 0, sizeof(struct port));
	err = pthread_spin_init(&port->lock, 0);
	if (err) {
		fprintf(stderr, "Ports: Failed to init spinlock\n");
		return err;
	}

	return 0;
}

static void port_cleanup(struct port *port)
{
	pthread_spin_destroy(&port->lock);
}

int avrdev_ports_init(void)
{
	int err;

	err = port_init(&portb);
	if (err)
		goto error;
	err = port_init(&portc);
	if (err)
		goto err_cleanup_portb;
	err = port_init(&portd);
	if (err)
		goto err_cleanup_portc;

	err = 0;
	err |= register_io_write_handler(io_portb_write, IO_PORTB);
	err |= register_io_write_handler(io_portc_write, IO_PORTC);
	err |= register_io_write_handler(io_portd_write, IO_PORTD);
	err |= register_io_write_handler(io_ddrb_write, IO_DDRB);
	err |= register_io_write_handler(io_ddrc_write, IO_DDRC);
	err |= register_io_write_handler(io_ddrd_write, IO_DDRD);
	err |= register_io_read_handler(io_portb_read, IO_PORTB);
	err |= register_io_read_handler(io_portc_read, IO_PORTC);
	err |= register_io_read_handler(io_portd_read, IO_PORTD);
	err |= register_io_read_handler(io_pinb_read, IO_PINB);
	err |= register_io_read_handler(io_pinc_read, IO_PINC);
	err |= register_io_read_handler(io_pind_read, IO_PIND);
	err |= register_io_read_handler(io_ddrb_read, IO_DDRB);
	err |= register_io_read_handler(io_ddrc_read, IO_DDRC);
	err |= register_io_read_handler(io_ddrd_read, IO_DDRD);
	if (err) {
		err = -ENOMEM;
		fprintf(stderr, "Failed to register port handlers.\n");
		goto err_cleanup_portd;
	}

	return 0;

err_cleanup_portd:
	port_cleanup(&portd);
err_cleanup_portc:
	port_cleanup(&portc);
err_cleanup_portb:
	port_cleanup(&portb);
error:
	return err;
}

void avrdev_ports_exit(void)
{//FIXME somebody must call me
	port_cleanup(&portd);
	port_cleanup(&portc);
	port_cleanup(&portb);
}
bues.ch cgit interface