1/* Copyright (C) 1995-2023 Free Software Foundation, Inc.
2
3   This program is free software; you can redistribute it and/or modify
4   it under the terms of the GNU General Public License as published by
5   the Free Software Foundation; either version 3 of the License, or
6   (at your option) any later version.
7
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   GNU General Public License for more details.
12
13   You should have received a copy of the GNU General Public License
14   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
15
16#include "ansidecl.h"
17#include "sim/callback.h"
18#include "sim/sim.h"
19#include <sim-config.h>
20#include <stdint.h>
21#include "dis-asm.h"
22
23#if HOST_BYTE_ORDER == BIG_ENDIAN
24#define HOST_BIG_ENDIAN
25#define EBT 0
26#else
27#define HOST_LITTLE_ENDIAN
28#define EBT 3
29#endif
30
31#define I_ACC_EXC 1
32
33/* Maximum events in event queue */
34#define EVENT_MAX	256
35
36/* Maximum # of floating point queue */
37#define FPUQN	1
38
39/* Maximum # of breakpoints */
40#define BPT_MAX	256
41
42struct histype {
43    unsigned        addr;
44    unsigned        time;
45};
46
47/* type definitions */
48
49typedef float   float32;	/* 32-bit float */
50typedef double  float64;	/* 64-bit float */
51
52struct pstate {
53
54    float64         fd[16];	/* FPU registers */
55#ifdef HOST_LITTLE_ENDIAN
56    float32         fs[32];
57    float32        *fdp;
58#else
59    float32        *fs;
60#endif
61    int32_t          *fsi;
62    uint32_t          fsr;
63    int32_t           fpstate;
64    uint32_t          fpq[FPUQN * 2];
65    uint32_t          fpqn;
66    uint32_t          ftime;
67    uint32_t          flrd;
68    uint32_t          frd;
69    uint32_t          frs1;
70    uint32_t          frs2;
71    uint32_t          fpu_pres;	/* FPU present (0 = No, 1 = Yes) */
72
73    uint32_t          psr;	/* IU registers */
74    uint32_t          tbr;
75    uint32_t          wim;
76    uint32_t          g[8];
77    uint32_t          r[128];
78    uint32_t          y;
79    uint32_t          asr17;      /* Single vector trapping */
80    uint32_t          pc, npc;
81
82
83    uint32_t          trap;	/* Current trap type */
84    uint32_t          annul;	/* Instruction annul */
85    uint32_t          data;	/* Loaded data	     */
86    uint32_t          inst;	/* Current instruction */
87    uint32_t          asi;	/* Current ASI */
88    uint32_t          err_mode;	/* IU error mode */
89    uint32_t          breakpoint;
90    uint32_t          bptnum;
91    uint32_t          bphit;
92    uint32_t          bpts[BPT_MAX];	/* Breakpoints */
93
94    uint32_t          ltime;	/* Load interlock time */
95    uint32_t          hold;	/* IU hold cycles in current inst */
96    uint32_t          fhold;	/* FPU hold cycles in current inst */
97    uint32_t          icnt;	/* Instruction cycles in curr inst */
98
99    uint32_t          histlen;	/* Trace history management */
100    uint32_t          histind;
101    struct histype *histbuf;
102    float32         freq;	/* Simulated processor frequency */
103
104
105    double          tottime;
106    uint64_t          ninst;
107    uint64_t          fholdt;
108    uint64_t          holdt;
109    uint64_t          icntt;
110    uint64_t          finst;
111    uint64_t          simstart;
112    double          starttime;
113    uint64_t          tlimit;	/* Simulation time limit */
114    uint64_t          pwdtime;	/* Cycles in power-down mode */
115    uint64_t          nstore;	/* Number of load instructions */
116    uint64_t          nload;	/* Number of store instructions */
117    uint64_t          nannul;	/* Number of annuled instructions */
118    uint64_t          nbranch;	/* Number of branch instructions */
119    uint32_t          ildreg;	/* Destination of last load instruction */
120    uint64_t          ildtime;	/* Last time point for load dependency */
121
122    int             rett_err;	/* IU in jmpl/restore error state (Rev.0) */
123    int             jmpltime;
124};
125
126struct evcell {
127    void            (*cfunc) (int32_t);
128    int32_t           arg;
129    uint64_t          time;
130    struct evcell  *nxt;
131};
132
133struct estate {
134    struct evcell   eq;
135    struct evcell  *freeq;
136    uint64_t          simtime;
137};
138
139struct irqcell {
140    void            (*callback) (int32_t);
141    int32_t           arg;
142};
143
144
145#define OK 0
146#define TIME_OUT 1
147#define BPT_HIT 2
148#define ERROR 3
149#define CTRL_C 4
150
151/* Prototypes  */
152
153/* erc32.c */
154extern void	init_sim (void);
155extern void	reset (void);
156extern void	error_mode (uint32_t pc);
157extern void	sim_halt (void);
158extern void	exit_sim (void);
159extern void	init_stdio (void);
160extern void	restore_stdio (void);
161extern int	memory_iread (uint32_t addr, uint32_t *data, uint32_t *ws);
162extern int	memory_read (int32_t asi, uint32_t addr, void *data,
163			     int32_t sz, int32_t *ws);
164extern int	memory_write (int32_t asi, uint32_t addr, uint32_t *data,
165			      int32_t sz, int32_t *ws);
166extern int	sis_memory_write (uint32_t addr,
167				  const void *data, uint32_t length);
168extern int	sis_memory_read (uint32_t addr, void *data,
169				 uint32_t length);
170extern void	boot_init (void);
171
172/* func.c */
173extern struct pstate  sregs;
174extern void	set_regi (struct pstate *sregs, int32_t reg,
175			  uint32_t rval);
176extern void	get_regi (struct pstate *sregs, int32_t reg, unsigned char *buf);
177extern int	exec_cmd (struct pstate *sregs, const char *cmd);
178extern void	reset_stat (struct pstate  *sregs);
179extern void	show_stat (struct pstate  *sregs);
180extern void	init_bpt (struct pstate  *sregs);
181extern void	init_signals (void);
182
183struct disassemble_info;
184extern void	dis_mem (uint32_t addr, uint32_t len,
185			 struct disassemble_info *info);
186extern void	event (void (*cfunc) (int32_t), int32_t arg, uint64_t delta);
187extern void	set_int (int32_t level, void (*callback) (int32_t), int32_t arg);
188extern void	advance_time (struct pstate  *sregs);
189extern uint32_t	now (void);
190extern int	wait_for_irq (void);
191extern int	check_bpt (struct pstate *sregs);
192extern void	reset_all (void);
193extern void	sys_reset (void);
194extern void	sys_halt (void);
195extern int	bfd_load (const char *fname);
196extern double	get_time (void);
197
198/* exec.c */
199extern int	dispatch_instruction (struct pstate *sregs);
200extern int	execute_trap (struct pstate *sregs);
201extern int	check_interrupts (struct pstate  *sregs);
202extern void	init_regs (struct pstate *sregs);
203
204/* interf.c */
205extern int	run_sim (struct pstate *sregs,
206			 uint64_t icount, int dis);
207
208/* float.c */
209extern int	get_accex (void);
210extern void	clear_accex (void);
211extern void	set_fsr (uint32_t fsr);
212
213/* help.c */
214extern void	usage (void);
215extern void	gen_help (void);
216