summaryrefslogtreecommitdiff
path: root/niosii.h
blob: cd4ba80c14120aab2e88f952d87f17c94a7b94a3 (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
/*
    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.
*/

#ifndef __NIOS_II_H__
#define __NIOS_II_H__

#include <stdio.h>
#include "public.h"

#define NIOS_REG_CNT (32)
#define D_CACHE_SIZE (16 * 1024)
#define I_CACHE_SIZE (16 * 1024)
#define PC_TRACE_CNT (128)

enum {
	NORMAL_MODE = 0x00,
	SINGLE_STEP = 0x01,
	BREAK_MODE = 0x02,
};

struct NIOS_CPU {
	uint32_t gp_regs[NIOS_REG_CNT]; /* default value is 0 */
 	uint32_t ctrl_regs[NIOS_REG_CNT]; /* is defined in Processor Reference Handbook */
	uint32_t pc;
	uint32_t break_pc;
	uint32_t mode;
	uint32_t pc_trace[PC_TRACE_CNT];
	uint32_t trace_index;
};

#define EXCEPTION_HANDLER_ADDR (0x00800020) /* exc_hook, exception handler address */
#define BREAK_HANDLER_ADDRESS  (0x00000000) /* break handler address */

#define OP_R_TYPE 0x3A
#define OP_J_TYPE 0x00

struct i_type_handler {
	uint32_t (*handler)(struct NIOS_CPU * cpu, uint32_t code);
};

struct r_type_handler{
	uint32_t (*handler)(struct NIOS_CPU * cpu, uint32_t code);
};

struct j_type_handler{
	uint32_t (*handler)(struct NIOS_CPU * cpu, uint32_t code);
};

struct custom_type_handler{
	uint32_t (*handler)(struct NIOS_CPU * cpu, uint32_t code);
};

extern struct i_type_handler i_type_handlers[];
extern struct r_type_handler r_type_handlers[];
extern struct j_type_handler j_type_handlers[];

struct i_type_code{
	uint32_t op:6;
	uint32_t imm16:16;
	uint32_t b:5;
	uint32_t a:5;
}__attribute__ ((__packed__));

struct j_type_code{
	uint32_t op:6;
	uint32_t imm26:26;
}__attribute__ ((__packed__));

struct r_type_code{
	uint32_t op:6;
	uint32_t n:5;
	uint32_t opx:6;
	uint32_t c:5;
	uint32_t b:5;
	uint32_t a:5;
}__attribute__ ((__packed__));

#define handler_item(func) {.handler = func}

struct custom_type_code {
	uint32_t op:6;
	uint32_t n:8;
	uint32_t rc:1;
	uint32_t rb:1;
	uint32_t ra:1;
	uint32_t c:5;
	uint32_t b:5;
	uint32_t a:5;
};

enum GP_REG_ALIAS{
	zero = 0,
	at = 1,
	et = 24,
	bt = 25,
	gp = 26,
	sp = 27,
	fp = 28,
	ea = 29,
	ba = 30,
	ra = 31,
};
enum CTRL_REG_ALIAS{
	status = 0,
	estatus,
	bstatus,
	ienable,
	ipending,
	cpuid,
	exception = 7,
	pteaddr,
	tlbacc,
	tlbmisc,
	badaddr = 12,
	config,
	mpubase,
	mpuacc
};



#define MEM_ADDR (0)
#define IO_ADDR (1)

#define PC_INC_NORMAL (0)
#define PC_INC_BY_INSTR (1)

extern uint32_t custom_instr(struct NIOS_CPU * cpu, uint32_t code);
extern void reset_cpu(void);
extern struct NIOS_CPU * get_nios_cpu(void);
extern uint32_t execute(uint32_t code);
extern int32_t get_addr_type(uint32_t addr);
extern uint8_t get_byte(uint32_t addr);
extern void store_byte(uint32_t addr, uint8_t data);
extern uint16_t get_half_word(uint32_t addr);
extern void store_half_word(uint32_t addr, uint16_t data);
extern uint32_t get_word(uint32_t addr);
extern void store_word(uint32_t addr, uint32_t data);
extern uint32_t get_instruct(struct NIOS_CPU * cpu, uint32_t * mem_base, 
								 uint32_t base_addr);
extern void dump_register(struct NIOS_CPU * cpu);
extern void dump_curr_code(uint32_t code);
extern void dump_next_code(struct NIOS_CPU * cpu);
extern void dump_pc(struct NIOS_CPU * cpu);
extern void set_break(struct NIOS_CPU * cpu, char * input);
extern void dump_mem(struct NIOS_CPU * cpu, char * input);
extern uint32_t ascii_to_hex(char * buf, uint32_t buf_len);

#define def_i_type_code struct i_type_code * instr = (struct i_type_code *)&code
#define def_j_type_code struct j_type_code * instr = (struct j_type_code *)&code
#define def_r_type_code struct r_type_code * instr = (struct r_type_code *)&code


/* BIT Definition for Control register */
#define REG_STATUS_PIE 	(0x01)
#define REG_STATUS_EH	(1<<2)

#define CPU_HAS_IRQ (0x01)
#define CPU_HAS_EXCEPTION (0x02)
#define CPU_HAS_NO_EVENT (0)

extern void handle_irq_exception(struct NIOS_CPU * cpu);
extern void clean_ipending(uint32_t mask);
extern void trace_pc(struct NIOS_CPU * cpu);
#endif