summaryrefslogtreecommitdiff
path: root/io_device.c
blob: e65060845ceb39093e53e682c981b15148b60c23 (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
/*
    Nios-sim - one simple NIOSII simulator only for personal interest and fun.
    Copyright (C) 2010  chysun2000@gmail.com

    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 <stdio.h>
#include "public.h"
#include "io_device.h"
#include "jtag_uart.h"
#include "timer.h"
#include "uart_core.h"
#include "nor_flash.h"

static struct io_device * devices[] = {
	&jtag_uart_io_device,
	&timer_core,
	&uart_core,
	&nor_flash_core
};

#define DEVICES_COUNT (sizeof(devices)/sizeof(devices[0]))

static struct io_device * last_used_device = NULL;

struct io_device * get_device(uint32_t address)
{
	struct io_device * ret_val = NULL;
	uint32_t i = 0;
	
	if (last_used_device != NULL){
		if(last_used_device->is_belong(address) == ADDR_IS_DEV){
			return last_used_device;
		}
	}

	for (i=0;i<DEVICES_COUNT; i++){
		if (devices[i]->is_belong != NULL){
			if (devices[i]->is_belong(address) == ADDR_IS_DEV){
				ret_val = devices[i];
				last_used_device = ret_val;
				break;
			}
		}
	}
	
	return ret_val;
}

void init_devices(void)
{
	uint32_t i = 0;
	
	printf("--------------------------------------------------\n");
	printf(" Init H/W Device Module!\n");
	printf("--------------------------------------------------\n");
	for (i=0;i<DEVICES_COUNT; i++){
		if (devices[i]->init != NULL){
			devices[i]->init(devices[i]);
		}
	}
	printf("--------------------------------------------------\n");
}

static const uint32_t data_mask[5] = {
	[1] = 0xFF,
	[2] = 0xFFFF,
	[4] = 0xFFFFFFFF,
};

uint32_t io_write_data(uint32_t old_data, uint32_t new_data, 
							uint32_t data_len)
{
	uint32_t mask = data_mask[data_len];

	old_data = old_data & (~mask);
	new_data = new_data & mask;
	return (old_data | new_data);
}

uint32_t io_write_data_mask(uint32_t old_data, uint32_t new_data,
								uint32_t data_len, uint32_t valid_mask,
								 uint32_t only_read_mask)
{
	uint32_t mask = data_mask[data_len];
	
	new_data = new_data & mask; /* remove as the access bus width */
	new_data = new_data & valid_mask; /* remove as the valid bits */
	new_data = new_data & (~only_read_mask); /* remove the read-only bits */
	
	old_data = old_data &(~mask | only_read_mask);
	
	return (old_data | new_data);
}

uint32_t io_read_data(unsigned old_data, uint32_t data_len)
{
	uint32_t mask = data_mask[data_len];
	return (old_data & mask);
}

void hw_simulating(void)
{
	int i = 0;
	struct io_device * device = NULL;
	
	for (i=0;i<DEVICES_COUNT; i++){
		device = devices[i];
		if(device->simulate != NULL){
			device->simulate(device);
		}
	}
}

uint32_t get_io_irq_status(void)
{
	int i = 0;
	uint32_t irq_mask = 0;
	struct io_device * device = NULL;
	
	for (i=0;i<DEVICES_COUNT; i++){
		device = devices[i];
		if(device->has_irq != NULL){
			if (device->has_irq(device) == DEV_HAS_IRQ){
				irq_mask |= device->irq_enable_mask;
			}
		}
	}
	
	return irq_mask;
}

uint32_t check_reg_bit(uint32_t value, uint32_t mask)
{
	if ((value & mask) == mask){
		return SIM_TRUE;
	}
	else {
		return SIM_FALSE;
	}
}
/*--------------------------------------------------------------------------*/