1 |
/* |
/* |
2 |
* Copyright (C) 2003-2005 Anders Gavare. All rights reserved. |
* Copyright (C) 2003-2006 Anders Gavare. All rights reserved. |
3 |
* |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
* modification, are permitted provided that the following conditions are met: |
25 |
* SUCH DAMAGE. |
* SUCH DAMAGE. |
26 |
* |
* |
27 |
* |
* |
28 |
* $Id: dev_cons.c,v 1.26 2005/06/26 11:43:48 debug Exp $ |
* $Id: dev_cons.c,v 1.37 2006/07/23 14:37:34 debug Exp $ |
29 |
* |
* |
30 |
* A console device. (Fake, only useful for simple tests.) |
* A simple console device, useful for simple tests. |
|
* It is hardwared to the lowest available MIPS hardware IRQ, and only |
|
|
* interrupts CPU 0. |
|
31 |
* |
* |
32 |
* This device provides memory mapped I/O for a simple console supporting |
* This device provides memory mapped I/O for a simple console supporting |
33 |
* putchar (writing to memory) and getchar (reading from memory), and |
* putchar (writing to memory) and getchar (reading from memory), and |
41 |
|
|
42 |
#include "console.h" |
#include "console.h" |
43 |
#include "cpu.h" |
#include "cpu.h" |
44 |
|
#include "device.h" |
45 |
#include "devices.h" |
#include "devices.h" |
46 |
#include "machine.h" |
#include "machine.h" |
47 |
#include "memory.h" |
#include "memory.h" |
48 |
#include "misc.h" |
#include "misc.h" |
49 |
|
|
50 |
|
#include "testmachine/dev_cons.h" |
51 |
|
|
|
#define CONS_TICK_SHIFT 14 |
|
52 |
|
|
53 |
struct cons_data { |
#define CONS_TICK_SHIFT 14 |
|
int console_handle; |
|
|
int irq_nr; |
|
|
}; |
|
54 |
|
|
55 |
|
|
56 |
/* |
DEVICE_TICK(cons) |
|
* dev_cons_tick(): |
|
|
*/ |
|
|
void dev_cons_tick(struct cpu *cpu, void *extra) |
|
57 |
{ |
{ |
58 |
struct cpu *c = cpu->machine->cpus[0]; |
struct cpu *c = cpu->machine->cpus[0]; |
59 |
struct cons_data *d = extra; |
struct cons_data *d = extra; |
65 |
} |
} |
66 |
|
|
67 |
|
|
68 |
/* |
DEVICE_ACCESS(cons) |
|
* dev_cons_access(): |
|
|
*/ |
|
|
int dev_cons_access(struct cpu *cpu, struct memory *mem, |
|
|
uint64_t relative_addr, unsigned char *data, size_t len, |
|
|
int writeflag, void *extra) |
|
69 |
{ |
{ |
70 |
struct cons_data *d = extra; |
struct cons_data *d = extra; |
71 |
int i; |
unsigned int i; |
72 |
|
|
73 |
/* Exit the emulator: */ |
/* Exit the emulator: */ |
74 |
if (relative_addr == DEV_CONS_HALT) { |
if (relative_addr == DEV_CONS_HALT) { |
75 |
cpu->running = 0; |
/* cpu->running = 0; |
76 |
cpu->machine->exit_without_entering_debugger = 1; |
cpu->machine->exit_without_entering_debugger = 1; |
77 |
return 1; |
return 1; */ |
78 |
|
/* TODO: this doesn't work yet. for now, let's |
79 |
|
simply use exit() */ |
80 |
|
exit(1); |
81 |
} |
} |
82 |
|
|
83 |
if (writeflag == MEM_WRITE) { |
if (writeflag == MEM_WRITE) { |
109 |
} |
} |
110 |
|
|
111 |
|
|
112 |
/* |
DEVINIT(cons) |
|
* dev_cons_init(): |
|
|
*/ |
|
|
int dev_cons_init(struct machine *machine, struct memory *mem, |
|
|
uint64_t baseaddr, char *name, int irq_nr) |
|
113 |
{ |
{ |
114 |
struct cons_data *d; |
struct cons_data *d; |
115 |
char *name2; |
char *name3; |
116 |
size_t nlen; |
size_t nlen; |
117 |
|
|
118 |
d = malloc(sizeof(struct cons_data)); |
d = malloc(sizeof(struct cons_data)); |
121 |
exit(1); |
exit(1); |
122 |
} |
} |
123 |
memset(d, 0, sizeof(struct cons_data)); |
memset(d, 0, sizeof(struct cons_data)); |
|
d->irq_nr = irq_nr; |
|
|
d->console_handle = console_start_slave(machine, name); |
|
124 |
|
|
125 |
nlen = strlen(name) + 20; |
nlen = strlen(devinit->name) + 10; |
126 |
name2 = malloc(nlen); |
if (devinit->name2 != NULL) |
127 |
if (name2 == NULL) { |
nlen += strlen(devinit->name2) + 10; |
128 |
|
name3 = malloc(nlen); |
129 |
|
if (name3 == NULL) { |
130 |
fprintf(stderr, "out of memory in dev_cons_init()\n"); |
fprintf(stderr, "out of memory in dev_cons_init()\n"); |
131 |
exit(1); |
exit(1); |
132 |
} |
} |
133 |
if (name != NULL && name[0]) |
if (devinit->name2 != NULL && devinit->name2[0]) |
134 |
snprintf(name2, nlen, "cons [%s]", name); |
snprintf(name3, nlen, "%s [%s]", devinit->name, devinit->name2); |
135 |
else |
else |
136 |
snprintf(name2, nlen, "cons"); |
snprintf(name3, nlen, "%s", devinit->name); |
137 |
|
|
138 |
memory_device_register(mem, name2, baseaddr, DEV_CONS_LENGTH, |
d->irq_nr = devinit->irq_nr; |
139 |
dev_cons_access, d, MEM_DEFAULT, NULL); |
d->in_use = devinit->in_use; |
140 |
machine_add_tickfunction(machine, dev_cons_tick, d, CONS_TICK_SHIFT); |
d->console_handle = console_start_slave(devinit->machine, name3, |
141 |
|
d->in_use); |
142 |
|
|
143 |
|
memory_device_register(devinit->machine->memory, name3, |
144 |
|
devinit->addr, DEV_CONS_LENGTH, dev_cons_access, d, |
145 |
|
DM_DEFAULT, NULL); |
146 |
|
machine_add_tickfunction(devinit->machine, dev_cons_tick, |
147 |
|
d, CONS_TICK_SHIFT, 0.0); |
148 |
|
|
149 |
return d->console_handle; |
/* NOTE: Ugly cast into pointer */ |
150 |
|
devinit->return_ptr = (void *)(size_t)d->console_handle; |
151 |
|
return 1; |
152 |
} |
} |
153 |
|
|