0e4e21ea2ae12d1399e210defe0244ada13ba5cd
[external/binutils.git] / sim / m68hc11 / emulos.c
1 /* emulos.c -- Small OS emulation
2    Copyright 1999, 2000, 2007 Free Software Foundation, Inc.
3    Written by Stephane Carrez (stcarrez@worldnet.fr)
4
5 This file is part of GDB, GAS, and the GNU binutils.
6
7 GDB, GAS, and the GNU binutils are free software; you can redistribute
8 them and/or modify them under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either version
10 1, or (at your option) any later version.
11
12 GDB, GAS, and the GNU binutils are distributed in the hope that they
13 will be useful, but WITHOUT ANY WARRANTY; without even the implied
14 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
15 the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this file; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "sim-main.h"
22 #ifdef HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25
26 #ifndef WIN32
27 #include <sys/types.h>
28 #include <sys/time.h>
29
30 /* This file emulates some OS system calls.
31    It's basically used to give access to the host OS facilities
32    like: stdin, stdout, files, time of day.  */
33 static int bench_mode = -1;
34 static struct timeval bench_start;
35 static struct timeval bench_stop;
36
37 void
38 emul_bench (struct _sim_cpu* cpu)
39 {
40   int op;
41
42   op = cpu_get_d (cpu);
43   switch (op)
44     {
45     case 0:
46       bench_mode = 0;
47       gettimeofday (&bench_start, 0);
48       break;
49
50     case 1:
51       gettimeofday (&bench_stop, 0);
52       if (bench_mode != 0)
53         printf ("bench start not called...\n");
54       bench_mode = 1;
55       break;
56
57     case 2:
58       {
59         int sz = 0;
60         int addr = cpu_get_x (cpu);
61         double t_start, t_stop, t;
62         char buf[1024];
63
64         op = cpu_get_y (cpu);
65         t_start = (double) (bench_start.tv_sec) * 1.0e6;
66         t_start += (double) (bench_start.tv_usec);
67         t_stop  = (double) (bench_stop.tv_sec) * 1.0e6;
68         t_stop  += (double) (bench_stop.tv_usec);
69         
70         while (sz < 1024)
71           {
72             buf[sz] = memory_read8 (cpu, addr);
73             if (buf[sz] == 0)
74               break;
75
76             sz ++;
77             addr++;
78           }
79         buf[1023] = 0;
80
81         if (bench_mode != 1)
82           printf ("bench_stop not called");
83
84         bench_mode = -1;
85         t = t_stop - t_start;
86         printf ("%-40.40s [%6d] %3.3f us\n", buf,
87                 op, t / (double) (op));
88         break;
89       }
90     }
91 }
92 #endif
93
94 void
95 emul_write(struct _sim_cpu* state)
96 {
97   int addr = cpu_get_x (state) & 0x0FFFF;
98   int size = cpu_get_d (state) & 0x0FFFF;
99
100   if (addr + size > 0x0FFFF) {
101     size = 0x0FFFF - addr;
102   }
103   state->cpu_running = 0;
104   while (size)
105     {
106       uint8 val = memory_read8 (state, addr);
107         
108       write(0, &val, 1);
109       addr ++;
110       size--;
111     }
112 }
113
114 /* emul_exit () is used by the default startup code of GCC to implement
115    the exit ().  For a real target, this will create an ILLEGAL fault.
116    But doing an exit () on a real target is really a non-sense.
117    exit () is important for the validation of GCC.  The exit status
118    is passed in 'D' register.  */
119 void
120 emul_exit (sim_cpu *cpu)
121 {
122   sim_engine_halt (CPU_STATE (cpu), cpu,
123                    NULL, NULL_CIA, sim_exited,
124                    cpu_get_d (cpu));
125 }
126
127 void
128 emul_os (int code, sim_cpu *proc)
129 {
130   proc->cpu_current_cycle = 8;
131   switch (code)
132     {
133     case 0x0:
134       break;
135
136       /* 0xCD 0x01 */
137     case 0x01:
138       emul_write (proc);
139       break;
140
141       /* 0xCD 0x02 */
142     case 0x02:
143       break;
144
145       /* 0xCD 0x03 */
146     case 0x03:
147       emul_exit (proc);
148       break;
149
150       /* 0xCD 0x04 */
151     case 0x04:
152 #ifndef WIN32
153       emul_bench (proc);
154 #endif
155       break;
156         
157     default:
158       break;
159     }
160 }
161