OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc3/] [or1ksim/] [tick/] [tick.c] - Blame information for rev 1545

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 91 lampret
/* tick.c -- Simulation of OpenRISC 1000 tick timer
2
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
3 1540 nogj
   Copyright (C) 2005 György `nog' Jeney, nog@sdf.lonestar.org
4 91 lampret
 
5
This file is part of OpenRISC 1000 Architectural Simulator.
6
 
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
11
 
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
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 program; if not, write to the Free Software
19
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
 
21
/* This is functional simulation of OpenRISC 1000 architectural
22
   tick timer.
23
*/
24
 
25
#include <stdlib.h>
26
#include <stdio.h>
27
#include <string.h>
28
 
29 1350 nogj
#include "config.h"
30
 
31
#ifdef HAVE_INTTYPES_H
32
#include <inttypes.h>
33
#endif
34
 
35
#include "port.h"
36
#include "arch.h"
37
#include "abstract.h"
38 561 simons
#include "except.h"
39 91 lampret
#include "tick.h"
40 1432 nogj
#include "opcode/or32.h"
41 728 markom
#include "spr_defs.h"
42 1432 nogj
#include "execute.h"
43 102 lampret
#include "pic.h"
44 189 chris
#include "sprs.h"
45 333 markom
#include "sim-config.h"
46 728 markom
#include "sched.h"
47 1408 nogj
#include "debug.h"
48 91 lampret
 
49 1408 nogj
DEFAULT_DEBUG_CHANNEL(tick);
50
 
51 728 markom
/* When did the timer start to count */
52 1540 nogj
long long cycles_start = 0;
53 133 markom
 
54 1540 nogj
/* Indicates if the timer is actually counting.  Needed to simulate one-shot
55
 * mode correctly */
56
int tick_count;
57 728 markom
 
58 91 lampret
/* Reset. It initializes TTCR register. */
59 1446 nogj
void tick_reset(void)
60 91 lampret
{
61 728 markom
  if (config.sim.verbose)
62 997 markom
    PRINTF("Resetting Tick Timer.\n");
63 1540 nogj
  cpu_state.sprs[SPR_TTCR] = 0;
64
  cpu_state.sprs[SPR_TTMR] = 0;
65
  tick_count = 0;
66 91 lampret
}
67
 
68 1540 nogj
/* Raises a timer exception */
69
void tick_raise_except(void *dat)
70 91 lampret
{
71 1540 nogj
  cpu_state.sprs[SPR_TTMR] |= SPR_TTMR_IP;
72
  /* be sure not to issue a timer exception if an exception occured before it */
73
  if(cpu_state.sprs[SPR_SR] & SPR_SR_TEE)
74
    except_handle(EXCEPT_TICK, cpu_state.sprs[SPR_EEAR_BASE]);
75
 
76
  /* Reschedule unconditionally, since we have to raise the exception until
77
   * TTMR_IP has been cleared */
78 1545 nogj
  sched_next_insn(tick_raise_except, NULL);
79 1540 nogj
}
80
 
81
/* Restarts the tick timer */
82
void tick_restart(void *dat)
83
{
84
  cpu_state.sprs[SPR_TTCR] = 0;
85
  cycles_start = runtime.sim.cycles;
86
  TRACE("Scheduleing timer restart job for %"PRIdREG"\n",
87
        cpu_state.sprs[SPR_TTMR] & SPR_TTMR_PERIOD);
88
  SCHED_ADD(tick_restart, NULL, cpu_state.sprs[SPR_TTMR] & SPR_TTMR_PERIOD);
89
}
90
 
91
/* Stops the timer */
92
void tick_one_shot(void *dat)
93
{
94
  TRACE("Stopping one-shot timer\n");
95
  cpu_state.sprs[SPR_TTCR] = cpu_state.sprs[SPR_TTMR] & SPR_TTMR_PERIOD;
96
  tick_count = 0;
97
}
98
 
99
/* Schedules the timer jobs */
100
static void sched_timer_job(uorreg_t prev_ttmr)
101
{
102
  uorreg_t ttmr = cpu_state.sprs[SPR_TTMR];
103
  uint32_t match_time = ttmr & SPR_TTMR_PERIOD;
104
  uint32_t ttcr_period = spr_read_ttcr() & SPR_TTCR_PERIOD;
105
 
106
  /* Remove previous jobs if they exists */
107
  if(prev_ttmr & SPR_TTMR_IE)
108
    SCHED_FIND_REMOVE(tick_raise_except, NULL);
109
 
110
  switch(prev_ttmr & SPR_TTMR_M) {
111
  case SPR_TTMR_RT:
112
    SCHED_FIND_REMOVE(tick_restart, NULL);
113
    break;
114
  case SPR_TTMR_SR:
115
    SCHED_FIND_REMOVE(tick_one_shot, NULL);
116
    break;
117
  }
118
 
119
  if(match_time >= ttcr_period)
120
    match_time -= ttcr_period;
121
  else
122
    match_time += (0xfffffffu - ttcr_period) + 1;
123
 
124
  TRACE("Cycles to go until match: %"PRIu32" (0x%"PRIx32")\n", match_time,
125
        match_time);
126
 
127
  switch(ttmr & SPR_TTMR_M) {
128
  case 0: /* Disabled timer */
129
    TRACE("Scheduleing exception when timer match (in disabled mode).\n");
130
    if(!match_time && (ttmr & SPR_TTMR_IE) && !(ttmr & SPR_TTMR_IP))
131
      SCHED_ADD(tick_raise_except, NULL, 0);
132
    break;
133
  case SPR_TTMR_RT: /* Auto-restart timer */
134
    TRACE("Scheduleing auto-restarting timer.\n");
135
    SCHED_ADD(tick_restart, NULL, match_time);
136
    if((ttmr & SPR_TTMR_IE) && !(ttmr & SPR_TTMR_IP))
137
      SCHED_ADD(tick_raise_except, NULL, match_time);
138
    break;
139
  case SPR_TTMR_SR: /* One-shot timer */
140
    TRACE("Scheduleing one-shot timer.\n");
141
    if(tick_count) {
142
      SCHED_ADD(tick_one_shot, NULL, match_time);
143
      if((ttmr & SPR_TTMR_IE) && !(ttmr & SPR_TTMR_IP))
144
        SCHED_ADD(tick_raise_except, NULL, match_time);
145 802 simons
    }
146 728 markom
    break;
147 1540 nogj
  case SPR_TTMR_CR: /* Continuos timer */
148
    TRACE("Scheduleing exception when timer match (in cont. mode).\n");
149
    if((ttmr & SPR_TTMR_IE) && !(ttmr & SPR_TTMR_IP))
150
      SCHED_ADD(tick_raise_except, NULL, match_time);
151 728 markom
  }
152
}
153 189 chris
 
154 1540 nogj
/* Handles a write to the ttcr spr */
155 1410 nogj
void spr_write_ttcr (uorreg_t value)
156 728 markom
{
157 1408 nogj
  TRACE("set ttcr = %"PRIxREG"\n", value);
158 1540 nogj
  cycles_start = runtime.sim.cycles - value;
159
 
160
  sched_timer_job(cpu_state.sprs[SPR_TTMR]);
161 728 markom
}
162 611 simons
 
163 1540 nogj
/* Value is the *previous* value of SPR_TTMR.  The new one can be found in
164
 * cpu_state.sprs[SPR_TTMR] */
165
void spr_write_ttmr (uorreg_t prev_val)
166 728 markom
{
167 1540 nogj
  uorreg_t value = cpu_state.sprs[SPR_TTMR];
168
 
169
  TRACE("set ttmr = %"PRIxREG" (previous: %"PRIxREG")\n", value, prev_val);
170
 
171
  if(value & SPR_TTMR_IP) {
172
    if(prev_val & SPR_TTMR_IP)
173
      /* SPR_TTMR_IP has not been cleared, continue sending timer interrupts */
174
      SCHED_ADD(tick_raise_except, NULL, 0);
175
    else
176
      /* Code running on or1k can't set SPR_TTMR_IP so make sure it isn't */
177
      cpu_state.sprs[SPR_TTMR] &= ~SPR_TTMR_IP;
178 728 markom
  }
179 1540 nogj
 
180
  /* If the timer was already disabled, ttcr should not be updated */
181
  if(tick_count)
182
    cpu_state.sprs[SPR_TTCR] = runtime.sim.cycles - cycles_start;
183
 
184
  cycles_start = runtime.sim.cycles - cpu_state.sprs[SPR_TTCR];
185
 
186
  tick_count = value & SPR_TTMR_M;
187
 
188
  if((tick_count == 0xc0000000) &&
189
     (cpu_state.sprs[SPR_TTCR] == (value & SPR_TTMR_PERIOD)))
190
    tick_count = 0;
191
 
192
  sched_timer_job(prev_val);
193 728 markom
}
194 611 simons
 
195 1410 nogj
uorreg_t spr_read_ttcr (void)
196 728 markom
{
197 1540 nogj
  uorreg_t ret;
198
 
199
  if(!tick_count)
200
    /* Report the time when the counter stoped (and don't carry on counting) */
201
    ret = cpu_state.sprs[SPR_TTCR];
202
  else
203
    ret = runtime.sim.cycles - cycles_start;
204
 
205
  TRACE("read ttcr %"PRIdREG" (0x%"PRIxREG")\n", ret, ret);
206
  return ret;
207 91 lampret
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.