dev_cons.cc Source File
Back to the index.
src
devices
dev_cons.cc
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2003-2009 Anders Gavare. All rights reserved.
3
*
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
6
*
7
* 1. Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* 2. Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
12
* 3. The name of the author may not be used to endorse or promote products
13
* derived from this software without specific prior written permission.
14
*
15
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
* SUCH DAMAGE.
26
*
27
*
28
* COMMENT: A simple console device, for the test machines
29
*
30
* This device provides memory mapped I/O for a simple console supporting
31
* putchar (writing to memory) and getchar (reading from memory), and
32
* support for halting the emulator. (This is useful for regression tests,
33
* Hello World-style test programs, and other simple experiments.)
34
*/
35
36
#include <stdio.h>
37
#include <stdlib.h>
38
#include <string.h>
39
40
#include "
console.h
"
41
#include "
cpu.h
"
42
#include "
device.h
"
43
#include "
machine.h
"
44
#include "
memory.h
"
45
#include "
misc.h
"
46
47
#include "
testmachine/dev_cons.h
"
48
49
#define CONS_TICK_SHIFT 14
50
51
struct
cons_data
{
52
int
console_handle
;
53
int
in_use
;
54
struct
interrupt
irq
;
55
};
56
57
58
DEVICE_TICK
(cons)
59
{
60
struct
cons_data
*d = (
struct
cons_data
*) extra;
61
62
if
(
console_charavail
(d->
console_handle
))
63
INTERRUPT_ASSERT
(d->
irq
);
64
else
65
INTERRUPT_DEASSERT
(d->
irq
);
66
}
67
68
69
DEVICE_ACCESS
(cons)
70
{
71
struct
cons_data
*d = (
struct
cons_data
*) extra;
72
unsigned
int
i;
73
74
/* Exit the emulator: */
75
if
(relative_addr ==
DEV_CONS_HALT
) {
76
/* cpu->running = 0;
77
cpu->machine->exit_without_entering_debugger = 1;
78
return 1; */
79
/* TODO: this doesn't work yet. for now, let's
80
simply use exit() */
81
exit(0);
82
}
83
84
if
(writeflag ==
MEM_WRITE
) {
85
for
(i=0; i<len; i++) {
86
if
(
data
[i] != 0) {
87
if
(
cpu
->
machine
->
register_dump
||
88
cpu
->
machine
->
instruction_trace
)
89
debug
(
"putchar '"
);
90
91
console_putchar
(d->
console_handle
,
data
[i]);
92
93
if
(
cpu
->
machine
->
register_dump
||
94
cpu
->
machine
->
instruction_trace
)
95
debug
(
"'\n"
);
96
fflush(stdout);
97
}
98
}
99
}
else
{
100
int
ch =
console_readchar
(d->
console_handle
);
101
if
(ch < 0)
102
ch = 0;
103
for
(i=0; i<len; i++)
104
data
[i] = ch;
105
}
106
107
dev_cons_tick(
cpu
, extra);
108
109
return
1;
110
}
111
112
113
DEVINIT
(cons)
114
{
115
struct
cons_data
*d;
116
char
*name3;
117
size_t
nlen;
118
119
CHECK_ALLOCATION
(d = (
struct
cons_data
*) malloc(
sizeof
(
struct
cons_data
)));
120
memset(d, 0,
sizeof
(
struct
cons_data
));
121
122
nlen =
strlen
(
devinit
->
name
) + 10;
123
if
(
devinit
->
name2
!= NULL)
124
nlen +=
strlen
(
devinit
->
name2
) + 10;
125
CHECK_ALLOCATION
(name3 = (
char
*) malloc(nlen));
126
if
(
devinit
->
name2
!= NULL &&
devinit
->
name2
[0])
127
snprintf(name3, nlen,
"%s [%s]"
,
devinit
->
name
,
devinit
->
name2
);
128
else
129
snprintf(name3, nlen,
"%s"
,
devinit
->
name
);
130
131
INTERRUPT_CONNECT
(
devinit
->
interrupt_path
, d->
irq
);
132
133
d->
in_use
=
devinit
->
in_use
;
134
d->
console_handle
=
console_start_slave
(
devinit
->
machine
, name3,
135
d->
in_use
);
136
137
memory_device_register
(
devinit
->
machine
->
memory
, name3,
138
devinit
->
addr
,
DEV_CONS_LENGTH
, dev_cons_access, d,
139
DM_DEFAULT
, NULL);
140
machine_add_tickfunction
(
devinit
->
machine
, dev_cons_tick,
141
d,
CONS_TICK_SHIFT
);
142
143
/* NOTE: Ugly cast into pointer */
144
devinit
->
return_ptr
= (
void
*)(
size_t
)d->
console_handle
;
145
return
1;
146
}
147
DEVINIT
DEVINIT(cons)
Definition:
dev_cons.cc:113
data
u_short data
Definition:
siireg.h:79
DEV_CONS_LENGTH
#define DEV_CONS_LENGTH
Definition:
dev_cons.h:12
console_putchar
void console_putchar(int handle, int ch)
Definition:
console.cc:405
INTERRUPT_CONNECT
#define INTERRUPT_CONNECT(name, istruct)
Definition:
interrupt.h:77
dev_cons.h
INTERRUPT_ASSERT
#define INTERRUPT_ASSERT(istruct)
Definition:
interrupt.h:74
machine::register_dump
int register_dump
Definition:
machine.h:150
cons_data::irq
struct interrupt irq
Definition:
dev_cons.cc:54
debug
#define debug
Definition:
dev_adb.cc:57
DEVICE_ACCESS
DEVICE_ACCESS(cons)
Definition:
dev_cons.cc:69
devinit::addr
uint64_t addr
Definition:
device.h:46
memory_device_register
void memory_device_register(struct memory *mem, const char *, uint64_t baseaddr, uint64_t len, int(*f)(struct cpu *, struct memory *, uint64_t, unsigned char *, size_t, int, void *), void *extra, int flags, unsigned char *dyntrans_data)
Definition:
memory.cc:339
DM_DEFAULT
#define DM_DEFAULT
Definition:
memory.h:130
devinit::machine
struct machine * machine
Definition:
device.h:41
console.h
console_charavail
int console_charavail(int handle)
Definition:
console.cc:336
DEVICE_TICK
DEVICE_TICK(cons)
Definition:
dev_cons.cc:58
device.h
MEM_WRITE
#define MEM_WRITE
Definition:
memory.h:117
machine::instruction_trace
int instruction_trace
Definition:
machine.h:162
machine_add_tickfunction
void machine_add_tickfunction(struct machine *machine, void(*func)(struct cpu *, void *), void *extra, int clockshift)
Definition:
machine.cc:280
devinit::interrupt_path
char * interrupt_path
Definition:
device.h:50
strlen
void COMBINE() strlen(struct cpu *cpu, struct arm_instr_call *ic, int low_addr)
Definition:
cpu_arm_instr.cc:2333
misc.h
machine.h
console_readchar
int console_readchar(int handle)
Definition:
console.cc:385
devinit::name
char * name
Definition:
device.h:43
devinit
Definition:
device.h:40
console_start_slave
int console_start_slave(struct machine *machine, const char *consolename, int use_for_input)
Definition:
console.cc:668
cpu.h
machine::memory
struct memory * memory
Definition:
machine.h:126
cpu::machine
struct machine * machine
Definition:
cpu.h:328
cons_data
Definition:
dev_cons.cc:51
devinit::return_ptr
void * return_ptr
Definition:
device.h:56
DEV_CONS_HALT
#define DEV_CONS_HALT
Definition:
dev_cons.h:14
INTERRUPT_DEASSERT
#define INTERRUPT_DEASSERT(istruct)
Definition:
interrupt.h:75
interrupt
Definition:
interrupt.h:36
devinit::name2
char * name2
Definition:
device.h:44
cons_data::in_use
int in_use
Definition:
dev_cons.cc:53
cpu
Definition:
cpu.h:326
devinit::in_use
int in_use
Definition:
device.h:52
cons_data::console_handle
int console_handle
Definition:
dev_cons.cc:52
CONS_TICK_SHIFT
#define CONS_TICK_SHIFT
Definition:
dev_cons.cc:49
memory.h
CHECK_ALLOCATION
#define CHECK_ALLOCATION(ptr)
Definition:
misc.h:239
Generated on Tue Mar 24 2020 14:04:48 for GXemul by
1.8.17