aboutsummaryrefslogtreecommitdiff
path: root/sim/tic80/interp.c
blob: 5845e068d12d0bd6e54d89df669caf560b31bfbc (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
/*  This file is part of the GDB simulators.

    Copyright (C) 1997, Free Software Foundation
    Condtributed by Cyngnus Solutions.

    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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
    */



#include "sim-main.h"

#include "idecode.h"

#include <signal.h>

void
engine_init (SIM_DESC sd)
{
  memset (&STATE_CPU (sd, 0)->reg, 0, sizeof STATE_CPU (sd, 0)->reg);
  memset (&STATE_CPU (sd, 0)->cia, 0, sizeof STATE_CPU (sd, 0)->cia);
  CPU_STATE (STATE_CPU (sd, 0)) = sd;
}


/* Mechanisms for stopping/restarting the simulation */

void
engine_error (SIM_DESC sd,
	      instruction_address cia,
	      const char *fmt,
	      ...)
{
  va_list ap;
  va_start (ap, fmt);
  sim_io_evprintf (sd, fmt, ap);
  va_end (ap);

  if (sd->halt_ok)
    {
      sim_io_printf (sd, "\n");
      engine_halt (sd, cia, sim_signalled, SIGABRT);
    }
  else
    sim_io_error (sd, " - aborting simulation");
}

void
engine_halt (SIM_DESC sd,
	     instruction_address cia,
	     enum sim_stop reason,
	     int siggnal)
{
  if (!sd->halt_ok)
    sim_io_error (sd, "engine_halt - bad longjmp");
  sd->reason = reason;
  sd->siggnal = siggnal;
  sd->halt_ok = 0;
  sd->restart_ok = 0;
  sd->cpu.cia = cia;
  longjmp (sd->path_to_halt, 1);
}

void
engine_restart (SIM_DESC sd,
		instruction_address cia)
{
  if (!sd->restart_ok)
    sim_io_error (sd, "engine_restart - bad longjmp");
  sd->restart_ok = 0;
  sd->cpu.cia = cia;
  longjmp(sd->path_to_restart, 1);
}


void
engine_run_until_stop (SIM_DESC sd,
		       volatile int *keep_running)
{
  if (!setjmp (sd->path_to_halt))
    {
      instruction_address cia;
      sd->halt_ok = 1;
      setjmp (sd->path_to_restart);
      sd->restart_ok = 1;
      cia = STATE_CPU (sd, 0)->cia;
      do
	{
	  if (cia.ip == -1)
	    {
	      /* anulled instruction */
	      cia.ip = cia.dp;
	      cia.dp = cia.dp + sizeof (instruction_word);
	    }
	  else
	    {
	      instruction_word insn = IMEM (cia.ip);
	      cia = idecode_issue (sd, insn, cia);
	    }
	}
      while (*keep_running);
      engine_halt (sd, cia, sim_stopped, SIGINT);
    }
}