summaryrefslogtreecommitdiffstats
path: root/emulator/memory.c
blob: 3c5d78241d9bb49bcd53490d611339e7d43d7f00 (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
/*
 *   AVR emulator
 *
 *   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 "memory.h"
#include "util.h"
#include "cpu.h"
#include "hardware.h"

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


/* The memory (SRAM and MMIO). */
struct avr_memory memory;


int register_io_write_handler(io_write_handler_t handler, enum avr_io_port virt)
{
	uint16_t phys;

	if (virt >= NR_VIRT_IO_PORTS)
		goto unavailable;
	phys = io_virt_to_phys(virt);
	if (phys == IO_PORT_UNAVAILABLE)
		goto unavailable;
	phys -= IO_MEM_OFFSET;
	if (memory.io_write_handlers[phys])
		goto busy;
	memory.io_write_handlers[phys] = handler;

	return 0;
unavailable:
	fprintf(stderr, "IO-write: Tried to register handler for unavailable IO port\n");
	return -EEXIST;
busy:
	fprintf(stderr, "IO-write: Tried to register an IO handler twice\n");
	return -EALREADY;
}

int register_io_read_handler(io_read_handler_t handler, enum avr_io_port virt)
{
	uint16_t phys;

	if (virt >= NR_VIRT_IO_PORTS)
		goto unavailable;
	phys = io_virt_to_phys(virt);
	if (phys == IO_PORT_UNAVAILABLE)
		goto unavailable;
	phys -= IO_MEM_OFFSET;
	if (memory.io_read_handlers[phys])
		goto busy;
	memory.io_read_handlers[phys] = handler;

	return 0;
unavailable:
	fprintf(stderr, "IO-read: Tried to register handler for unavailable IO port\n");
	return -EEXIST;
busy:
	fprintf(stderr, "IO-read: Tried to register an IO handler twice\n");
	return -EALREADY;
}

void memory_cleanup(void)
{
	//TODO
}

int memory_initialize(void)
{
	int err;
	size_t nr_io;

	memory.ram = active_setup.memory_data;
	memory.size = active_setup.memory_size;
	memory.io = memory.ram + IO_MEM_OFFSET;
	memory.sram_offset = active_setup.sram_offset;
	err = pthread_spin_init(&memory.io_lock, 0);
	if (err) {
		fprintf(stderr, "Failed to init the I/O spinlock\n");
		return -ENOMEM;
	}
	memory.sp_offset = active_setup.sp_offset;

	nr_io = active_setup.sram_offset - IO_MEM_OFFSET;
	memory.iospace_size = nr_io;

	memory.io_write_handlers = malloc(sizeof(io_write_handler_t) * nr_io);
	if (!memory.io_write_handlers) {
		fprintf(stderr, "Failed to alloc IO write handlers\n");
		return -ENOMEM;
	}
	memset(memory.io_write_handlers, 0, sizeof(io_write_handler_t) * nr_io);

	memory.io_read_handlers = malloc(sizeof(io_read_handler_t) * nr_io);
	if (!memory.io_read_handlers) {
		fprintf(stderr, "Failed to alloc IO read handlers\n");
		return -ENOMEM;
	}
	memset(memory.io_read_handlers, 0, sizeof(io_read_handler_t) * nr_io);

	active_setup.io_setup(&memory);

	return 0;
}
bues.ch cgit interface