summaryrefslogtreecommitdiff
path: root/nios2.h
blob: a24ab1d5722bf11510958206e6a189a28ec1bd3f (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
/*
 * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
 * Copyright (C) 2010 chysun2000@gmail.com
 *
 * This file is part of nios2sim-ng.
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License. See the file "COPYING" in the main directory of this archive
 * for more details.
 */

#ifndef _NIOS2_H_
#define _NIOS2_H_

#include "instruction.h"
#include "memory.h"

#define NIOS2_GP_REG_COUNT	32
/* there are really 32, but 16-31 are reserved for future use */
#define NIOS2_CTRL_REG_COUNT	16

struct nios2 {
	/* General-Purpose Registers */
	uint32_t gp_regs[NIOS2_GP_REG_COUNT];
	/* Control Registers */
	uint32_t ctrl_regs[NIOS2_CTRL_REG_COUNT];
	/* Program counter */
	uint32_t pc;
	/* User or Supervisor mode */
	int mode;

	/* Configurable processor features */
	bool has_mul;	/* mul, muli */
	bool has_mulx;	/* mulxss, mulxsu, mulxuu */
	bool has_div;	/* div, divu */

	bool has_mmu;	/* Memory Management Unit */

	struct memory *mem;
	/* Exception handler address */
	uint32_t exception_handler_addr;
};

/* Aliases for general-purpose registers */
enum {
	zero = 0,
	at,
	et = 24,
	bt,
	gp,
	sp,
	fp,
	ea,
	ba,
	ra,
};

/* Aliases for control registers */
enum {
	status = 0,
	estatus,
	bstatus,
	ienable,
	ipending,
	cpuid,
	/* reserved */
	exception = 7,
	pteaddr,
	tlbacc,
	tlbmisc,
	/* reserved */
	badaddr = 12,
	config,
	mpubase,
	mpuacc,
};

/* status register fields */
#define NIOS2_STATUS_PIE	0
#define NIOS2_STATUS_U		1
#define NIOS2_STATUS_EH		2

/* exception causes */
#define NIOS2_EX_RESET			0
#define NIOS2_EX_PROC_ONLY_RESET	1
#define NIOS2_EX_IRQ			2
#define NIOS2_EX_UNIMPLEMENTED		4
#define NIOS2_EX_DIV_ERR		8
#define NIOS2_EX_SUPERVISOR_ONLY_I	9
#define NIOS2_EX_SUPERVISOR_ONLY_D	11
#define NIOS2_EX_FAST_TLB_MISS		12

enum {
	NIOS2_SUPERVISOR_MODE,
	NIOS2_USER_MODE,
};

extern void nios2_cpu_reset(struct nios2 *cpu);
extern void nios2_cpu_init(struct nios2 *cpu);
extern void nios2_simulate(struct nios2 *cpu);
extern void nios2_cpu_inc_pc(struct nios2 *cpu);
extern bool nios2_in_user_mode(struct nios2 *cpu);
extern bool nios2_in_supervisor_mode(struct nios2 *cpu);
extern void nios2_exception(struct nios2 *cpu, uint8_t cause);
extern uint32_t nios2_fetch_instr(struct nios2 *cpu);
extern int nios2_execute_instr(struct nios2 *cpu, uint32_t instr);

extern int nios2_load(struct nios2 *cpu, uint32_t addr, void *data, size_t size);
extern int nios2_store(struct nios2 *cpu, uint32_t addr, void *data, size_t size);

extern void nios2_dump_registers(struct nios2 *cpu);

#endif /* _NIOS2_H_ */