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 1778

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
 
73
  /* Reschedule unconditionally, since we have to raise the exception until
74
   * TTMR_IP has been cleared */
75 1545 nogj
  sched_next_insn(tick_raise_except, NULL);
76 1559 nogj
 
77
  /* be sure not to issue a timer exception if an exception occured before it */
78
  if(cpu_state.sprs[SPR_SR] & SPR_SR_TEE)
79
    except_handle(EXCEPT_TICK, cpu_state.sprs[SPR_EEAR_BASE]);
80 1540 nogj
}
81
 
82
/* Restarts the tick timer */
83
void tick_restart(void *dat)
84
{
85
  cpu_state.sprs[SPR_TTCR] = 0;
86
  cycles_start = runtime.sim.cycles;
87
  TRACE("Scheduleing timer restart job for %"PRIdREG"\n",
88
        cpu_state.sprs[SPR_TTMR] & SPR_TTMR_PERIOD);
89
  SCHED_ADD(tick_restart, NULL, cpu_state.sprs[SPR_TTMR] & SPR_TTMR_PERIOD);
90
}
91
 
92
/* Stops the timer */
93
void tick_one_shot(void *dat)
94
{
95
  TRACE("Stopping one-shot timer\n");
96
  cpu_state.sprs[SPR_TTCR] = cpu_state.sprs[SPR_TTMR] & SPR_TTMR_PERIOD;
97
  tick_count = 0;
98
}
99
 
100
/* Schedules the timer jobs */
101
static void sched_timer_job(uorreg_t prev_ttmr)
102
{
103
  uorreg_t ttmr = cpu_state.sprs[SPR_TTMR];
104
  uint32_t match_time = ttmr & SPR_TTMR_PERIOD;
105
  uint32_t ttcr_period = spr_read_ttcr() & SPR_TTCR_PERIOD;
106
 
107
  /* Remove previous jobs if they exists */
108 1560 nogj
  if((prev_ttmr & SPR_TTMR_IE) && !(ttmr & SPR_TTMR_IP))
109 1540 nogj
    SCHED_FIND_REMOVE(tick_raise_except, NULL);
110
 
111
  switch(prev_ttmr & SPR_TTMR_M) {
112
  case SPR_TTMR_RT:
113
    SCHED_FIND_REMOVE(tick_restart, NULL);
114
    break;
115
  case SPR_TTMR_SR:
116
    SCHED_FIND_REMOVE(tick_one_shot, NULL);
117
    break;
118
  }
119
 
120
  if(match_time >= ttcr_period)
121
    match_time -= ttcr_period;
122
  else
123
    match_time += (0xfffffffu - ttcr_period) + 1;
124
 
125
  TRACE("Cycles to go until match: %"PRIu32" (0x%"PRIx32")\n", match_time,
126
        match_time);
127
 
128
  switch(ttmr & SPR_TTMR_M) {
129
  case 0: /* Disabled timer */
130
    TRACE("Scheduleing exception when timer match (in disabled mode).\n");
131
    if(!match_time && (ttmr & SPR_TTMR_IE) && !(ttmr & SPR_TTMR_IP))
132
      SCHED_ADD(tick_raise_except, NULL, 0);
133
    break;
134
  case SPR_TTMR_RT: /* Auto-restart timer */
135
    TRACE("Scheduleing auto-restarting timer.\n");
136
    SCHED_ADD(tick_restart, NULL, match_time);
137
    if((ttmr & SPR_TTMR_IE) && !(ttmr & SPR_TTMR_IP))
138
      SCHED_ADD(tick_raise_except, NULL, match_time);
139
    break;
140
  case SPR_TTMR_SR: /* One-shot timer */
141
    TRACE("Scheduleing one-shot timer.\n");
142
    if(tick_count) {
143
      SCHED_ADD(tick_one_shot, NULL, match_time);
144
      if((ttmr & SPR_TTMR_IE) && !(ttmr & SPR_TTMR_IP))
145
        SCHED_ADD(tick_raise_except, NULL, match_time);
146 802 simons
    }
147 728 markom
    break;
148 1540 nogj
  case SPR_TTMR_CR: /* Continuos timer */
149
    TRACE("Scheduleing exception when timer match (in cont. mode).\n");
150
    if((ttmr & SPR_TTMR_IE) && !(ttmr & SPR_TTMR_IP))
151
      SCHED_ADD(tick_raise_except, NULL, match_time);
152 728 markom
  }
153
}
154 189 chris
 
155 1540 nogj
/* Handles a write to the ttcr spr */
156 1410 nogj
void spr_write_ttcr (uorreg_t value)
157 728 markom
{
158 1408 nogj
  TRACE("set ttcr = %"PRIxREG"\n", value);
159 1540 nogj
  cycles_start = runtime.sim.cycles - value;
160
 
161
  sched_timer_job(cpu_state.sprs[SPR_TTMR]);
162 728 markom
}
163 611 simons
 
164 1540 nogj
/* Value is the *previous* value of SPR_TTMR.  The new one can be found in
165
 * cpu_state.sprs[SPR_TTMR] */
166
void spr_write_ttmr (uorreg_t prev_val)
167 728 markom
{
168 1540 nogj
  uorreg_t value = cpu_state.sprs[SPR_TTMR];
169
 
170
  TRACE("set ttmr = %"PRIxREG" (previous: %"PRIxREG")\n", value, prev_val);
171
 
172 1560 nogj
  /* Code running on or1k can't set SPR_TTMR_IP so make sure it isn't */
173
  cpu_state.sprs[SPR_TTMR] &= ~SPR_TTMR_IP;
174 1540 nogj
 
175
  /* If the timer was already disabled, ttcr should not be updated */
176
  if(tick_count)
177
    cpu_state.sprs[SPR_TTCR] = runtime.sim.cycles - cycles_start;
178
 
179
  cycles_start = runtime.sim.cycles - cpu_state.sprs[SPR_TTCR];
180
 
181
  tick_count = value & SPR_TTMR_M;
182
 
183
  if((tick_count == 0xc0000000) &&
184
     (cpu_state.sprs[SPR_TTCR] == (value & SPR_TTMR_PERIOD)))
185
    tick_count = 0;
186
 
187
  sched_timer_job(prev_val);
188 728 markom
}
189 611 simons
 
190 1410 nogj
uorreg_t spr_read_ttcr (void)
191 728 markom
{
192 1540 nogj
  uorreg_t ret;
193
 
194
  if(!tick_count)
195
    /* Report the time when the counter stoped (and don't carry on counting) */
196
    ret = cpu_state.sprs[SPR_TTCR];
197
  else
198
    ret = runtime.sim.cycles - cycles_start;
199
 
200
  TRACE("read ttcr %"PRIdREG" (0x%"PRIxREG")\n", ret, ret);
201
  return ret;
202 91 lampret
}

powered by: WebSVN 2.1.0

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