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

Subversion Repositories scarm

[/] [scarm/] [trunk/] [src/] [components/] [barrelshifter/] [scBarrelShifter.cpp] - Blame information for rev 10

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

Line No. Rev Author Line
1 2 zhong
///////////////////////////////////////////////////////////////////////////////
2
// This program is free software; you can redistribute it and/or
3
// modify it under the terms of the GNU General Public License
4
// as published by the Free Software Foundation; either version 2
5
// of the License, or (at your option) any later version.
6
//
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
// GNU General Public License for more details.
11
//
12
// You should have received a copy of the GNU General Public License
13
// along with this program; if not, write to the Free Software
14
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15
//////////////////////////////////////////////////////////////////////
16
 
17
///////////////////////////////////////////////////////////////////              
18
//          
19
//  Original Author: Allen Tao Zhong,
20
//  University of Electronic Science and Technology in China
21
//  email: zhong@opencores.org
22
//  info   This is a SystemC ARM model 
23
//   
24
//
25
///////////////////////////////////////////////////////////////////////////////
26
 
27
/*****************************************************************************
28
 
29
  scBarrelShifter.cpp -- a barrel shifter
30
 
31
  Original Author: Allen Tao Zhong, University of Electronic Science and
32
                   Technology in China
33
 
34
 *****************************************************************************/
35
 
36
// scBarrelShifter.cpp: implementation of the scBarrelShifter class.
37
//
38
//////////////////////////////////////////////////////////////////////
39
 
40
#include "scBarrelShifter.h"
41
 
42
//////////////////////////////////////////////////////////////////////
43
// Construction/Destruction
44
//////////////////////////////////////////////////////////////////////
45
 
46
#define SHIFT_LEFT(_x,_y)  ((_y >= 32) ? 0 : _x << _y)
47
#define SHIFT_RIGHT(_x,_y) ((_y >= 32) ? 0 : _x >> _y)
48
#define SIGNED_SHIFT_RIGHT(_x,_y) ((_y >= 32) ? ((_x >> 31) * 0xFFFFFFFF) : ((int32_t)_x) >> _y)
49
 
50
///////////////////////////////////////////////////////////////////////////////
51
//
52
//
53
uint32_t scBarrelShifter::Shift(uint32_t nVal,  SHIFT type, int nDist)
54
{
55
        uint32_t nCPSR=inout_n_Flag;
56
  switch (type)
57
    {
58
      ////////////////////////////////////////////////////////////////
59
      case S_LSL: case S_ASL:
60
      {
61
                 if (nDist == 0)
62
                 {
63
                        m_nShiftCarryBit = (nCPSR & C_FLAG) ? 1 : 0;
64
                        return nVal;
65
                 }
66
                 else if (nDist == 32)
67
                 {
68
                        m_nShiftCarryBit = nVal & 0x1;
69
                        return 0;
70
                 }
71
             else if (nDist > 32)
72
                 {
73
                        m_nShiftCarryBit = 0;
74
                        return 0;
75
                 }
76
                 else
77
                 {
78
                        m_nShiftCarryBit = SHIFT_RIGHT(nVal, (32 - nDist)) & 0x1;
79
                        return (nVal << nDist); break;
80
                 }
81
      }
82
      break;
83
      ////////////////////////////////////////////////////////////////
84
    case S_LSR:
85
      {
86
        if (nDist == 0)
87
          {
88
            m_nShiftCarryBit = (nCPSR & C_FLAG) ? 1 : 0;
89
            return nVal;
90
          }
91
        else if (nDist == 32)
92
          {
93
            m_nShiftCarryBit = (nVal >> 31);
94
            return 0;
95
          }
96
        else if (nDist > 32)
97
          {
98
            m_nShiftCarryBit = 0;
99
            return 0;
100
          }
101
        else
102
          {
103
            m_nShiftCarryBit = (nVal >> (nDist - 1)) & 0x1;
104
            return (nVal >> nDist);
105
          }
106
      }
107
      break;
108
      ////////////////////////////////////////////////////////////////
109
    case S_ASR:
110
      {
111
        if (nDist == 0)
112
          {
113
            m_nShiftCarryBit = (nCPSR & C_FLAG) ? 1 : 0;
114
            return nVal;
115
          }
116
        else if (nDist >= 32)
117
          {
118
            if (nVal & 0x80000000)
119
              {
120
                m_nShiftCarryBit = 1;
121
                return 0xFFFFFFFF;
122
              }
123
            else
124
              {
125
                m_nShiftCarryBit = 0;
126
                return 0;
127
              }
128
          }
129
        else
130
          {
131
            uint32_t temp = 0xFFFFFFFF;
132
            m_nShiftCarryBit = (nVal >> (nDist - 1)) & 0x1;
133
            if ((nVal & 0x80000000) != 0)
134
              nVal = (nVal >> nDist) | (~(temp >> (nDist /* - 1 */)));
135
            else
136
              nVal = (nVal >> nDist);
137
 
138
            return nVal;
139
          }
140
      }
141
      break;
142
      ////////////////////////////////////////////////////////////////
143
    case S_ROR:
144
      {
145
        if (nDist == 0)
146
          {
147
            m_nShiftCarryBit = (nCPSR & C_FLAG) ? 1 : 0;
148
            return nVal;
149
          }
150
        else if ((nDist & 0x1F) == 0)
151
          {
152
            m_nShiftCarryBit = nVal >> 31;
153
            return nVal;
154
          }
155
        else
156
          {
157
            nDist &= 0x1F;
158
            m_nShiftCarryBit = SHIFT_RIGHT(nVal, (nDist - 1)) & 0x1;
159
            return (nVal >> nDist) | (nVal << (32 - nDist));
160
          }
161
      }
162
      break;
163
      ////////////////////////////////////////////////////////////////
164
    case S_RRX:
165
      {
166
            m_nShiftCarryBit = nVal & 0x1;
167
 
168
        uint32_t temp = nVal & 0x1;
169
        nVal = nVal >> 1;
170
        if (nCPSR & C_FLAG)
171
          nVal |= 0x80000000;
172
 
173
        return nVal;
174
      }
175
      break;
176
    default :
177
        {
178
          cout<<"error in scBarrelShifter.Shift\n";
179
                  return 0;
180
 
181
        }
182
   }//end of switch(type)
183
}
184
 
185
void scBarrelShifter::entry()
186
{
187
 
188
 
189
   uint32_t val,dist,new_val;
190
   val=in_n_Val;
191
   dist=in_n_Dist;
192
   new_val=Shift(val, in_SHIFT_TYPE, dist);
193
   out_n_NewVal=new_val;
194
   cout<<"   Value="<<hex<<in_n_Val<<"  ShitfDist="<<in_n_Dist<<" shifted_value="<<hex<<new_val<<endl;
195
}
196
 

powered by: WebSVN 2.1.0

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