dev_rtc.cc Source File

Back to the index.

dev_rtc.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-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 generic Real-Time Clock device, for the test machines
29  *
30  * It can be used to retrieve the current system time, and to cause periodic
31  * interrupts.
32  */
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/time.h>
38 
39 #include "cpu.h"
40 #include "device.h"
41 #include "emul.h"
42 #include "machine.h"
43 #include "memory.h"
44 #include "misc.h"
45 #include "timer.h"
46 
47 #include "testmachine/dev_rtc.h"
48 
49 
50 #define DEV_RTC_TICK_SHIFT 14
51 
52 struct rtc_data {
53  struct interrupt irq;
55 
56  int hz;
57  struct timer *timer;
58 
59  struct timeval cur_time;
60 };
61 
62 
63 /*
64  * timer_tick():
65  *
66  * This function is called d->hz times per second.
67  */
68 static void timer_tick(struct timer *t, void *extra)
69 {
70  struct rtc_data *d = (struct rtc_data *) extra;
71  d->pending_interrupts ++;
72 }
73 
74 
76 {
77  struct rtc_data *d = (struct rtc_data *) extra;
78 
79  if (d->pending_interrupts > 0)
81  else
83 }
84 
85 
87 {
88  struct rtc_data *d = (struct rtc_data *) extra;
89  uint64_t idata = 0, odata = 0;
90 
91  if (writeflag == MEM_WRITE)
92  idata = memory_readmax64(cpu, data, len);
93 
94  switch (relative_addr) {
95 
97  gettimeofday(&d->cur_time, NULL);
98  break;
99 
100  case DEV_RTC_SEC:
101  odata = d->cur_time.tv_sec;
102  break;
103 
104  case DEV_RTC_USEC:
105  odata = d->cur_time.tv_usec;
106  break;
107 
108  case DEV_RTC_HZ:
109  if (writeflag == MEM_READ) {
110  odata = d->hz;
111  } else {
112  d->hz = idata;
113 
114  if (d->hz == 0) {
115  /* Remove the timer, if any: */
116  if (d->timer != NULL)
117  timer_remove(d->timer);
118 
119  d->timer = NULL;
120  d->pending_interrupts = 0;
121  } else {
122  /* Add a timer, or update the existing one: */
123  if (d->timer == NULL)
124  d->timer = timer_add(d->hz,
125  timer_tick, d);
126  else
128  }
129  }
130  break;
131 
133  if (d->pending_interrupts > 0)
134  d->pending_interrupts --;
135 
137 
138  /* TODO: Reassert the interrupt here, if
139  d->pending_interrupts is still above zero? */
140 
141  break;
142 
143  default:if (writeflag == MEM_WRITE) {
144  fatal("[ rtc: unimplemented write to "
145  "offset 0x%x: data=0x%x ]\n", (int)
146  relative_addr, (int)idata);
147  } else {
148  fatal("[ rtc: unimplemented read from "
149  "offset 0x%x ]\n", (int)relative_addr);
150  }
151  }
152 
153  if (writeflag == MEM_READ)
154  memory_writemax64(cpu, data, len, odata);
155 
156  return 1;
157 }
158 
159 
161 {
162  struct rtc_data *d;
163 
164  CHECK_ALLOCATION(d = (struct rtc_data *) malloc(sizeof(struct rtc_data)));
165  memset(d, 0, sizeof(struct rtc_data));
166 
168 
170  devinit->addr, DEV_RTC_LENGTH, dev_rtc_access, (void *)d,
171  DM_DEFAULT, NULL);
172 
174  dev_rtc_tick, d, DEV_RTC_TICK_SHIFT);
175 
176  return 1;
177 }
178 
uint64_t memory_readmax64(struct cpu *cpu, unsigned char *buf, int len)
Definition: memory.cc:55
void fatal(const char *fmt,...)
Definition: main.cc:152
#define DM_DEFAULT
Definition: memory.h:130
#define DEV_RTC_TICK_SHIFT
Definition: dev_rtc.cc:50
int hz
Definition: dev_rtc.cc:56
char * name
Definition: device.h:43
DEVINIT(rtc)
Definition: dev_rtc.cc:160
#define MEM_READ
Definition: memory.h:116
struct memory * memory
Definition: machine.h:126
#define DEV_RTC_HZ
Definition: dev_rtc.h:18
Definition: timer.cc:45
#define DEV_RTC_TRIGGER_READ
Definition: dev_rtc.h:14
struct timeval cur_time
Definition: dev_rtc.cc:59
#define CHECK_ALLOCATION(ptr)
Definition: misc.h:239
void timer_update_frequency(struct timer *t, double new_freq)
Definition: timer.cc:132
#define DEV_RTC_INTERRUPT_ACK
Definition: dev_rtc.h:19
u_short data
Definition: siireg.h:79
struct timer * timer_add(double freq, void(*timer_tick)(struct timer *timer, void *extra), void *extra)
Definition: timer.cc:75
#define INTERRUPT_ASSERT(istruct)
Definition: interrupt.h:74
#define MEM_WRITE
Definition: memory.h:117
Definition: device.h:40
struct interrupt irq
Definition: dev_rtc.cc:53
#define INTERRUPT_CONNECT(name, istruct)
Definition: interrupt.h:77
Definition: cpu.h:326
struct machine * machine
Definition: device.h:41
#define DEV_RTC_USEC
Definition: dev_rtc.h:16
DEVICE_TICK(rtc)
Definition: dev_rtc.cc:75
void memory_writemax64(struct cpu *cpu, unsigned char *buf, int len, uint64_t data)
Definition: memory.cc:89
void timer_remove(struct timer *t)
Definition: timer.cc:104
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
#define DEV_RTC_LENGTH
Definition: dev_rtc.h:12
int pending_interrupts
Definition: dev_rtc.cc:54
struct timer * timer
Definition: dev_rtc.cc:57
uint64_t addr
Definition: device.h:46
void machine_add_tickfunction(struct machine *machine, void(*func)(struct cpu *, void *), void *extra, int clockshift)
Definition: machine.cc:280
vmrs t
Definition: armreg.h:750
#define DEV_RTC_SEC
Definition: dev_rtc.h:15
DEVICE_ACCESS(rtc)
Definition: dev_rtc.cc:86
char * interrupt_path
Definition: device.h:50
#define INTERRUPT_DEASSERT(istruct)
Definition: interrupt.h:75

Generated on Fri Dec 7 2018 19:52:23 for GXemul by doxygen 1.8.13